Skip to content

Commit 5b9180b

Browse files
committed
Makes memoryStore public
Allows reuse by embedding in user code store implementations. Signed-off-by: davidovich <[email protected]>
1 parent a999520 commit 5b9180b

File tree

7 files changed

+50
-50
lines changed

7 files changed

+50
-50
lines changed

directed.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ func (d *directed[K, T]) Clone() (Graph[K, T], error) {
254254
clone := &directed[K, T]{
255255
hash: d.hash,
256256
traits: traits,
257-
store: newMemoryStore[K, T](),
257+
store: NewMemoryStore[K, T](),
258258
}
259259

260260
if err := clone.AddVerticesFrom(d); err != nil {

directed_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func TestDirected_Traits(t *testing.T) {
2121
}
2222

2323
for name, test := range tests {
24-
g := newDirected(IntHash, test.traits, newMemoryStore[int, int]())
24+
g := newDirected(IntHash, test.traits, NewMemoryStore[int, int]())
2525
traits := g.Traits()
2626

2727
if !traitsAreEqual(traits, test.expected) {
@@ -60,7 +60,7 @@ func TestDirected_AddVertex(t *testing.T) {
6060
}
6161

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

6565
var err error
6666

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

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

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

234234
for _, vertex := range test.vertices {
235235
_ = graph.AddVertex(vertex)
@@ -405,7 +405,7 @@ func TestDirected_AddEdge(t *testing.T) {
405405
}
406406

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

410410
for _, vertex := range test.vertices {
411411
_ = graph.AddVertex(vertex)
@@ -967,7 +967,7 @@ func TestDirected_AdjacencyList(t *testing.T) {
967967
}
968968

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

972972
for _, vertex := range test.vertices {
973973
_ = graph.AddVertex(vertex)
@@ -1050,7 +1050,7 @@ func TestDirected_PredecessorMap(t *testing.T) {
10501050
}
10511051

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

10551055
for _, vertex := range test.vertices {
10561056
_ = graph.AddVertex(vertex)
@@ -1197,7 +1197,7 @@ func TestDirected_OrderAndSize(t *testing.T) {
11971197
}
11981198

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

12021202
for _, vertex := range test.vertices {
12031203
_ = graph.AddVertex(vertex)
@@ -1241,7 +1241,7 @@ func TestDirected_edgesAreEqual(t *testing.T) {
12411241
}
12421242

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

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

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

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

1278-
outEdges := graph.store.(*memoryStore[int, int]).outEdges
1278+
outEdges := graph.store.(*MemoryStore[int, int]).outEdges
12791279
if len(outEdges) != len(test.edges) {
12801280
t.Errorf("%s: number of outgoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(outEdges))
12811281
}
1282-
inEdges := graph.store.(*memoryStore[int, int]).inEdges
1282+
inEdges := graph.store.(*MemoryStore[int, int]).inEdges
12831283
if len(inEdges) != len(test.edges) {
12841284
t.Errorf("%s: number of ingoing edges doesn't match: expected %v, got %v", name, len(test.edges), len(inEdges))
12851285
}
@@ -1327,7 +1327,7 @@ func TestDirected_predecessors(t *testing.T) {
13271327
}
13281328

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

13321332
for _, vertex := range test.vertices {
13331333
_ = graph.AddVertex(vertex)

graph.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ type Hash[K comparable, T any] func(T) K
253253
// The graph will use the default in-memory store for persisting vertices and
254254
// edges. To use a different [Store], use [NewWithStore].
255255
func New[K comparable, T any](hash Hash[K, T], options ...func(*Traits)) Graph[K, T] {
256-
return NewWithStore(hash, newMemoryStore[K, T](), options...)
256+
return NewWithStore(hash, NewMemoryStore[K, T](), options...)
257257
}
258258

259259
// NewWithStore creates a new graph same as [New] but uses the provided store

store.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,28 +68,28 @@ type Store[K comparable, T any] interface {
6868
EdgeCount() (int, error)
6969
}
7070

71-
type memoryStore[K comparable, T any] struct {
71+
type MemoryStore[K comparable, T any] struct {
7272
lock sync.RWMutex
7373
vertices map[K]T
7474
vertexProperties map[K]VertexProperties
7575

7676
// outEdges and inEdges store all outgoing and ingoing edges for all vertices. For O(1) access,
7777
// these edges themselves are stored in maps whose keys are the hashes of the target vertices.
78-
outEdges map[K]map[K]Edge[K] // source -> target
79-
inEdges map[K]map[K]Edge[K] // target -> source
78+
outEdges map[K]map[K]Edge[K] // source -> target
79+
inEdges map[K]map[K]Edge[K] // target -> source
8080
edgeCount int
8181
}
8282

83-
func newMemoryStore[K comparable, T any]() Store[K, T] {
84-
return &memoryStore[K, T]{
83+
func NewMemoryStore[K comparable, T any]() Store[K, T] {
84+
return &MemoryStore[K, T]{
8585
vertices: make(map[K]T),
8686
vertexProperties: make(map[K]VertexProperties),
8787
outEdges: make(map[K]map[K]Edge[K]),
8888
inEdges: make(map[K]map[K]Edge[K]),
8989
}
9090
}
9191

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

@@ -103,7 +103,7 @@ func (s *memoryStore[K, T]) AddVertex(k K, t T, p VertexProperties) error {
103103
return nil
104104
}
105105

106-
func (s *memoryStore[K, T]) ListVertices() ([]K, error) {
106+
func (s *MemoryStore[K, T]) ListVertices() ([]K, error) {
107107
s.lock.RLock()
108108
defer s.lock.RUnlock()
109109

@@ -115,14 +115,14 @@ func (s *memoryStore[K, T]) ListVertices() ([]K, error) {
115115
return hashes, nil
116116
}
117117

118-
func (s *memoryStore[K, T]) VertexCount() (int, error) {
118+
func (s *MemoryStore[K, T]) VertexCount() (int, error) {
119119
s.lock.RLock()
120120
defer s.lock.RUnlock()
121121

122122
return len(s.vertices), nil
123123
}
124124

125-
func (s *memoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
125+
func (s *MemoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
126126
s.lock.RLock()
127127
defer s.lock.RUnlock()
128128

@@ -136,7 +136,7 @@ func (s *memoryStore[K, T]) Vertex(k K) (T, VertexProperties, error) {
136136
return v, p, nil
137137
}
138138

139-
func (s *memoryStore[K, T]) RemoveVertex(k K) error {
139+
func (s *MemoryStore[K, T]) RemoveVertex(k K) error {
140140
s.lock.RLock()
141141
defer s.lock.RUnlock()
142142

@@ -164,7 +164,7 @@ func (s *memoryStore[K, T]) RemoveVertex(k K) error {
164164
return nil
165165
}
166166

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

@@ -185,7 +185,7 @@ func (s *memoryStore[K, T]) AddEdge(sourceHash, targetHash K, edge Edge[K]) erro
185185
return nil
186186
}
187187

188-
func (s *memoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) error {
188+
func (s *MemoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) error {
189189
s.lock.Lock()
190190
defer s.lock.Unlock()
191191

@@ -205,7 +205,7 @@ func (s *memoryStore[K, T]) UpdateEdge(sourceHash, targetHash K, edge Edge[K]) e
205205
return nil
206206
}
207207

208-
func (s *memoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
208+
func (s *MemoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
209209
s.lock.Lock()
210210
defer s.lock.Unlock()
211211

@@ -217,7 +217,7 @@ func (s *memoryStore[K, T]) RemoveEdge(sourceHash, targetHash K) error {
217217
return nil
218218
}
219219

220-
func (s *memoryStore[K, T]) Edge(sourceHash, targetHash K) (Edge[K], error) {
220+
func (s *MemoryStore[K, T]) Edge(sourceHash, targetHash K) (Edge[K], error) {
221221
s.lock.RLock()
222222
defer s.lock.RUnlock()
223223

@@ -234,14 +234,14 @@ func (s *memoryStore[K, T]) Edge(sourceHash, targetHash K) (Edge[K], error) {
234234
return edge, nil
235235
}
236236

237-
func (s *memoryStore[K, T]) EdgeCount() (int, error) {
237+
func (s *MemoryStore[K, T]) EdgeCount() (int, error) {
238238
s.lock.RLock()
239239
defer s.lock.RUnlock()
240240

241241
return s.edgeCount, nil
242242
}
243243

244-
func (s *memoryStore[K, T]) ListEdges() ([]Edge[K], error) {
244+
func (s *MemoryStore[K, T]) ListEdges() ([]Edge[K], error) {
245245
s.lock.RLock()
246246
defer s.lock.RUnlock()
247247

@@ -259,7 +259,7 @@ func (s *memoryStore[K, T]) ListEdges() ([]Edge[K], error) {
259259
//
260260
// Because CreatesCycle doesn't need to modify the PredecessorMap, we can use
261261
// inEdges instead to compute the same thing without creating any copies.
262-
func (s *memoryStore[K, T]) CreatesCycle(source, target K) (bool, error) {
262+
func (s *MemoryStore[K, T]) CreatesCycle(source, target K) (bool, error) {
263263
s.lock.RLock()
264264
defer s.lock.RUnlock()
265265

store_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func TestRemoveEdge(t *testing.T) {
1313
}
1414

1515
build := func(nodes []string, edges [][]string) Store[string, string] {
16-
store := newMemoryStore[string, string]()
16+
store := NewMemoryStore[string, string]()
1717
for _, n := range nodes {
1818
noerr(store.AddVertex(n, n, VertexProperties{}))
1919
}

undirected.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ func (u *undirected[K, T]) Clone() (Graph[K, T], error) {
292292
clone := &undirected[K, T]{
293293
hash: u.hash,
294294
traits: traits,
295-
store: newMemoryStore[K, T](),
295+
store: NewMemoryStore[K, T](),
296296
}
297297

298298
if err := clone.AddVerticesFrom(u); err != nil {

0 commit comments

Comments
 (0)