Skip to content

Commit

Permalink
Makes memoryStore public
Browse files Browse the repository at this point in the history
Allows reuse by embedding in user code store implementations.
  • Loading branch information
davidovich committed Dec 20, 2023
1 parent 8757b27 commit 0d89cb0
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion directed.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func (d *directed[K, T]) Clone() (Graph[K, T], error) {
clone := &directed[K, T]{
hash: d.hash,
traits: traits,
store: newMemoryStore[K, T](),
store: NewMemoryStore[K, T](),
}

if err := clone.AddVerticesFrom(d); err != nil {
Expand Down
26 changes: 13 additions & 13 deletions directed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestDirected_Traits(t *testing.T) {
}

for name, test := range tests {
g := newDirected(IntHash, test.traits, newMemoryStore[int, int]())
g := newDirected(IntHash, test.traits, NewMemoryStore[int, int]())
traits := g.Traits()

if !traitsAreEqual(traits, test.expected) {
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestDirected_AddVertex(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

var err error

Expand All @@ -81,7 +81,7 @@ func TestDirected_AddVertex(t *testing.T) {
t.Errorf("%s: error expectancy doesn't match: expected %v, got %v", name, test.finallyExpectedError, err)
}

graphStore := graph.store.(*memoryStore[int, int])
graphStore := graph.store.(*MemoryStore[int, int])
for _, vertex := range test.vertices {
if len(graphStore.vertices) != len(test.expectedVertices) {
t.Errorf("%s: vertex count doesn't match: expected %v, got %v", name, len(test.expectedVertices), len(graphStore.vertices))
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestDirected_Vertex(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestDirected_AddEdge(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, test.traits, newMemoryStore[int, int]())
graph := newDirected(IntHash, test.traits, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down Expand Up @@ -967,7 +967,7 @@ func TestDirected_AdjacencyList(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down Expand Up @@ -1050,7 +1050,7 @@ func TestDirected_PredecessorMap(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down Expand Up @@ -1197,7 +1197,7 @@ func TestDirected_OrderAndSize(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down Expand Up @@ -1241,7 +1241,7 @@ func TestDirected_edgesAreEqual(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())
actual := graph.edgesAreEqual(test.a, test.b)

if actual != test.edgesAreEqual {
Expand All @@ -1264,7 +1264,7 @@ func TestDirected_addEdge(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, edge := range test.edges {
sourceHash := graph.hash(edge.Source)
Expand All @@ -1275,11 +1275,11 @@ func TestDirected_addEdge(t *testing.T) {
}
}

outEdges := graph.store.(*memoryStore[int, int]).outEdges
outEdges := graph.store.(*MemoryStore[int, int]).outEdges
if len(outEdges) != len(test.edges) {
t.Errorf("%s: number of outgoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(outEdges))
}
inEdges := graph.store.(*memoryStore[int, int]).inEdges
inEdges := graph.store.(*MemoryStore[int, int]).inEdges
if len(inEdges) != len(test.edges) {
t.Errorf("%s: number of ingoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(inEdges))
}
Expand Down Expand Up @@ -1327,7 +1327,7 @@ func TestDirected_predecessors(t *testing.T) {
}

for name, test := range tests {
graph := newDirected(IntHash, &Traits{}, newMemoryStore[int, int]())
graph := newDirected(IntHash, &Traits{}, NewMemoryStore[int, int]())

for _, vertex := range test.vertices {
_ = graph.AddVertex(vertex)
Expand Down
2 changes: 1 addition & 1 deletion graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ type Hash[K comparable, T any] func(T) K
// The graph will use the default in-memory store for persisting vertices and
// edges. To use a different [Store], use [NewWithStore].
func New[K comparable, T any](hash Hash[K, T], options ...func(*Traits)) Graph[K, T] {
return NewWithStore(hash, newMemoryStore[K, T](), options...)
return NewWithStore(hash, NewMemoryStore[K, T](), options...)
}

// NewWithStore creates a new graph same as [New] but uses the provided store
Expand Down
28 changes: 14 additions & 14 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Store[K comparable, T any] interface {
ListEdges() ([]Edge[K], error)
}

type memoryStore[K comparable, T any] struct {
type MemoryStore[K comparable, T any] struct {
lock sync.RWMutex
vertices map[K]T
vertexProperties map[K]VertexProperties
Expand All @@ -75,16 +75,16 @@ type memoryStore[K comparable, T any] struct {
inEdges map[K]map[K]Edge[K] // target -> source
}

func newMemoryStore[K comparable, T any]() Store[K, T] {
return &memoryStore[K, T]{
func NewMemoryStore[K comparable, T any]() Store[K, T] {
return &MemoryStore[K, T]{
vertices: make(map[K]T),
vertexProperties: make(map[K]VertexProperties),
outEdges: make(map[K]map[K]Edge[K]),
inEdges: make(map[K]map[K]Edge[K]),
}
}

func (s *memoryStore[K, T]) AddVertex(k K, t T, p VertexProperties) error {
func (s *MemoryStore[K, T]) AddVertex(k K, t T, p VertexProperties) error {
s.lock.Lock()
defer s.lock.Unlock()

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

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

Expand All @@ -110,14 +110,14 @@ func (s *memoryStore[K, T]) ListVertices() ([]K, error) {
return hashes, nil
}

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

return len(s.vertices), nil
}

func (s *memoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
func (s *MemoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
s.lock.RLock()
defer s.lock.RUnlock()

Expand All @@ -131,7 +131,7 @@ func (s *memoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
return v, p, nil
}

func (s *memoryStore[K, T]) RemoveVertex(k K) error {
func (s *MemoryStore[K, T]) RemoveVertex(k K) error {
s.lock.RLock()
defer s.lock.RUnlock()

Expand Down Expand Up @@ -159,7 +159,7 @@ func (s *memoryStore[K, T]) RemoveVertex(k K) error {
return nil
}

func (s *memoryStore[K, T]) AddEdge(sourceHash, targetHash K, edge Edge[K]) error {
func (s *MemoryStore[K, T]) AddEdge(sourceHash, targetHash K, edge Edge[K]) error {
s.lock.Lock()
defer s.lock.Unlock()

Expand All @@ -178,7 +178,7 @@ func (s *memoryStore[K, T]) AddEdge(sourceHash, targetHash K, edge Edge[K]) erro
return nil
}

func (s *memoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) error {
func (s *MemoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) error {
if _, err := s.Edge(sourceHash, targetHash); err != nil {
return err
}
Expand All @@ -192,7 +192,7 @@ func (s *memoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) e
return nil
}

func (s *memoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
func (s *MemoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
s.lock.Lock()
defer s.lock.Unlock()

Expand All @@ -201,7 +201,7 @@ func (s *memoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
return nil
}

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

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

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

Expand All @@ -236,7 +236,7 @@ func (s *memoryStore[K, T]) ListEdges() ([]Edge[K], error) {
//
// Because CreatesCycle doesn't need to modify the PredecessorMap, we can use
// inEdges instead to compute the same thing without creating any copies.
func (s *memoryStore[K, T]) CreatesCycle(source, target K) (bool, error) {
func (s *MemoryStore[K, T]) CreatesCycle(source, target K) (bool, error) {
if _, _, err := s.Vertex(source); err != nil {
return false, fmt.Errorf("could not get vertex with hash %v: %w", source, err)
}
Expand Down
2 changes: 1 addition & 1 deletion store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestRemoveEdge(t *testing.T) {
}

build := func(nodes []string, edges [][]string) Store[string, string] {
store := newMemoryStore[string, string]()
store := NewMemoryStore[string, string]()
for _, n := range nodes {
noerr(store.AddVertex(n, n, VertexProperties{}))
}
Expand Down
2 changes: 1 addition & 1 deletion undirected.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (u *undirected[K, T]) Clone() (Graph[K, T], error) {
clone := &undirected[K, T]{
hash: u.hash,
traits: traits,
store: newMemoryStore[K, T](),
store: NewMemoryStore[K, T](),
}

if err := clone.AddVerticesFrom(u); err != nil {
Expand Down
Loading

0 comments on commit 0d89cb0

Please sign in to comment.