Skip to content

Commit

Permalink
Merge pull request #76 from Tochemey/master
Browse files Browse the repository at this point in the history
feat: add Order and Size methods to the graph
  • Loading branch information
hmdsefi committed Aug 1, 2024
2 parents f0f4727 + d93b069 commit ba840a1
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
20 changes: 20 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gograph

import "sync/atomic"

// baseGraph represents a basic implementation of Graph interface. It
// supports multiple types of graph.
//
Expand All @@ -16,6 +18,9 @@ type baseGraph[T comparable] struct {
edges map[T]map[T]*Edge[T]

properties GraphProperties

verticesCount uint32
edgesCount uint32
}

func newBaseGraph[T comparable](properties GraphProperties) *baseGraph[T] {
Expand All @@ -38,6 +43,7 @@ func (g *baseGraph[T]) addToEdgeMap(from, to *Vertex[T], options ...EdgeOptionFu
g.edges[from.label][to.label] = edge
}

atomic.AddUint32(&g.edgesCount, 1)
return edge
}

Expand Down Expand Up @@ -133,6 +139,8 @@ func (g *baseGraph[T]) addVertex(v *Vertex[T]) *Vertex[T] {
}

g.vertices[v.label] = v
atomic.AddUint32(&g.verticesCount, 1)

return v
}

Expand Down Expand Up @@ -294,6 +302,7 @@ func (g *baseGraph[T]) removeEdge(edge *Edge[T]) {
if len(destMap) == 0 {
delete(g.edges, edge.source.label)
}
atomic.AddUint32(&g.edgesCount, ^(uint32(1) - 1))
}
}

Expand Down Expand Up @@ -381,6 +390,7 @@ func (g *baseGraph[T]) removeVertex(in *Vertex[T]) {

delete(g.edges, v.label)
delete(g.vertices, v.label)
atomic.AddUint32(&g.verticesCount, ^(uint32(1) - 1))
}

// ContainsEdge returns 'true' if and only if this graph contains an edge
Expand Down Expand Up @@ -431,3 +441,13 @@ func (g *baseGraph[T]) AllEdges() []*Edge[T] {

return out
}

// Order returns the number of vertices in the graph.
func (g *baseGraph[T]) Order() uint32 {
return atomic.LoadUint32(&g.verticesCount)
}

// Size returns the number of edges in the graph
func (g *baseGraph[T]) Size() uint32 {
return atomic.LoadUint32(&g.edgesCount)
}
12 changes: 12 additions & 0 deletions base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ func TestBaseGraph_AddEdgeAcyclic(t *testing.T) {
g.AddVertex(v2)
g.AddVertex(v3)

expectedVerticesCount := uint32(3)
actual := g.Order()
if actual != expectedVerticesCount {
t.Errorf("expected (%d) but got (%d)", expectedVerticesCount, actual)
}

// Add edges from 1 to 2 and from 2 to 3
_, err := g.AddEdge(v1, v2)
if err != nil {
Expand All @@ -165,6 +171,12 @@ func TestBaseGraph_AddEdgeAcyclic(t *testing.T) {
if err == nil {
t.Error("Expected error, but got none")
}

expectedEdgesCount := uint32(2)
actual = g.Size()
if actual != expectedEdgesCount {
t.Errorf("expected (%d) but got (%d)", expectedEdgesCount, actual)
}
}

func TestBaseGraph_AddEdgeWeighted(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ type Graph[T comparable] interface {
//
// If the specified vertex is nil, returns 'false'.
ContainsVertex(v *Vertex[T]) bool

// Order returns the number of vertices in the graph.
Order() uint32

// Size returns the number of edges in the graph
Size() uint32
}

// New creates a new instance of base graph that implemented the Graph interface.
Expand Down

0 comments on commit ba840a1

Please sign in to comment.