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

Makes memoryStore public #165

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
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
34 changes: 17 additions & 17 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,28 +68,28 @@ type Store[K comparable, T any] interface {
EdgeCount() (int, 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

// 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
}

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 @@ -103,7 +103,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 @@ -115,14 +115,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 @@ -136,7 +136,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 @@ -164,7 +164,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 @@ -185,7 +185,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 {
s.lock.Lock()
defer s.lock.Unlock()

Expand All @@ -205,7 +205,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 @@ -217,7 +217,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 @@ -234,14 +234,14 @@ func (s *memoryStore[K, T]) Edge(sourceHash, targetHash K) (Edge[K], error) {
return edge, nil
}

func (s *memoryStore[K, T]) EdgeCount() (int, error) {
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) {
func (s *MemoryStore[K, T]) ListEdges() ([]Edge[K], error) {
s.lock.RLock()
defer s.lock.RUnlock()

Expand All @@ -259,7 +259,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) {
s.lock.RLock()
defer s.lock.RUnlock()

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