Skip to content

Commit

Permalink
Add EdgeCount to Store interface (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonbrandenburg authored Nov 18, 2023
1 parent 3908bcf commit 89422d5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 23 deletions.
12 changes: 1 addition & 11 deletions directed.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,7 @@ func (d *directed[K, T]) Order() (int, error) {
}

func (d *directed[K, T]) Size() (int, error) {
size := 0
outEdges, err := d.AdjacencyMap()
if err != nil {
return 0, fmt.Errorf("failed to get adjacency map: %w", err)
}

for _, outEdges := range outEdges {
size += len(outEdges)
}

return size, nil
return d.store.EdgeCount()
}

func (d *directed[K, T]) edgesAreEqual(a, b Edge[T]) bool {
Expand Down
19 changes: 18 additions & 1 deletion store.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type Store[K comparable, T any] interface {

// ListEdges should return all edges in the graph in a slice.
ListEdges() ([]Edge[K], error)

// EdgeCount should return the number of edges in the graph. This should be equal to the
// length of the slice returned by ListEdges.
EdgeCount() (int, error)
}

type memoryStore[K comparable, T any] struct {
Expand All @@ -73,6 +77,7 @@ type memoryStore[K comparable, T any] struct {
// these edges themselves are stored in maps whose keys are the hashes of the target vertices.
outEdges map[K]map[K]Edge[K] // source -> target
inEdges map[K]map[K]Edge[K] // target -> source
edgeCount int
}

func newMemoryStore[K comparable, T any]() Store[K, T] {
Expand Down Expand Up @@ -175,6 +180,8 @@ func (s *memoryStore[K, T]) AddEdge(sourceHash, targetHash K, edge Edge[K]) erro

s.inEdges[targetHash][sourceHash] = edge

s.edgeCount++

return nil
}

Expand All @@ -198,6 +205,9 @@ func (s *memoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {

delete(s.inEdges[targetHash], sourceHash)
delete(s.outEdges[sourceHash], targetHash)

s.edgeCount--

return nil
}

Expand All @@ -218,11 +228,18 @@ func (s *memoryStore[K, T]) Edge(sourceHash, targetHash K) (Edge[K], error) {
return edge, nil
}

func (s *memoryStore[K, T]) EdgeCount() (int, error) {
s.lock.RLock()
defer s.lock.RUnlock()

return s.edgeCount, nil
}

func (s *memoryStore[K, T]) ListEdges() ([]Edge[K], error) {
s.lock.RLock()
defer s.lock.RUnlock()

res := make([]Edge[K], 0)
res := make([]Edge[K], 0, s.edgeCount)
for _, edges := range s.outEdges {
for _, edge := range edges {
res = append(res, edge)
Expand Down
13 changes: 2 additions & 11 deletions undirected.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,11 @@ func (u *undirected[K, T]) Order() (int, error) {
}

func (u *undirected[K, T]) Size() (int, error) {
size := 0

outEdges, err := u.AdjacencyMap()
if err != nil {
return 0, fmt.Errorf("failed to get adjacency map: %w", err)
}

for _, outEdges := range outEdges {
size += len(outEdges)
}
edgeCount, err := u.store.EdgeCount()

// Divide by 2 since every add edge operation on undirected graph is counted
// twice.
return size / 2, nil
return edgeCount / 2, err
}

func (u *undirected[K, T]) edgesAreEqual(a, b Edge[T]) bool {
Expand Down

0 comments on commit 89422d5

Please sign in to comment.