Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add method that returns all vertices #167

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions directed.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func (d *directed[K, T]) RemoveVertex(hash K) error {
return d.store.RemoveVertex(hash)
}

func (d *directed[K, T]) Vertices() map[K]T {
return d.store.AllVertices()
}

func (d *directed[K, T]) AddEdge(sourceHash, targetHash K, options ...func(*EdgeProperties)) error {
_, _, err := d.store.Vertex(sourceHash)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ type Graph[K comparable, T any] interface {
// be returned. If the vertex doesn't exist, ErrVertexNotFound is returned.
RemoveVertex(hash K) error

// Vertices returns a copy of all vertices as map
Vertices() map[K]T

// AddEdge creates an edge between the source and the target vertex.
//
// If either vertex cannot be found, ErrVertexNotFound will be returned. If
Expand Down
18 changes: 16 additions & 2 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graph

import (
"fmt"
"maps"
"sync"
)

Expand All @@ -25,6 +26,9 @@ type Store[K comparable, T any] interface {
// ErrVertexHasEdges should be returned.
RemoveVertex(hash K) error

// AllVertices returns a map, containing all vertices
AllVertices() map[K]T

// ListVertices should return all vertices in the graph in a slice.
ListVertices() ([]K, error)

Expand Down Expand Up @@ -75,8 +79,8 @@ type memoryStore[K comparable, T any] struct {

// outEdges and inEdges store all outgoing and ingoing edges for all vertices. For O(1) access,
// 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
outEdges map[K]map[K]Edge[K] // source -> target
inEdges map[K]map[K]Edge[K] // target -> source
edgeCount int
}

Expand All @@ -103,6 +107,16 @@ func (s *memoryStore[K, T]) AddVertex(k K, t T, p VertexProperties) error {
return nil
}

func (s *memoryStore[K, T]) AllVertices() map[K]T {
out := make(map[K]T, len(s.vertices))
s.lock.RLock()
defer s.lock.RUnlock()

maps.Copy(out, s.vertices)

return out
}

func (s *memoryStore[K, T]) ListVertices() ([]K, error) {
s.lock.RLock()
defer s.lock.RUnlock()
Expand Down
4 changes: 4 additions & 0 deletions undirected.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ func (u *undirected[K, T]) RemoveVertex(hash K) error {
return u.store.RemoveVertex(hash)
}

func (u *undirected[K, T]) Vertices() map[K]T {
return u.store.AllVertices()
}

func (u *undirected[K, T]) AddEdge(sourceHash, targetHash K, options ...func(*EdgeProperties)) error {
if _, _, err := u.store.Vertex(sourceHash); err != nil {
return fmt.Errorf("could not find source vertex with hash %v: %w", sourceHash, err)
Expand Down
Loading