renamed to simple walker

This commit is contained in:
pippellia-btc
2025-06-02 11:43:04 +02:00
parent 5ef3dd08dd
commit 7d82354540
3 changed files with 69 additions and 62 deletions

View File

@@ -11,23 +11,23 @@ type Walker interface {
Follows(ctx context.Context, node graph.ID) ([]graph.ID, error)
}
type MapWalker struct {
type SimpleWalker struct {
follows map[graph.ID][]graph.ID
}
func NewWalker(m map[graph.ID][]graph.ID) *MapWalker {
return &MapWalker{follows: m}
func NewSimpleWalker(m map[graph.ID][]graph.ID) *SimpleWalker {
return &SimpleWalker{follows: m}
}
func (m *MapWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
return m.follows[node], nil
func (w *SimpleWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
return w.follows[node], nil
}
func (m *MapWalker) Update(ctx context.Context, delta graph.Delta) {
m.follows[delta.Node] = delta.New()
func (w *SimpleWalker) Update(ctx context.Context, delta graph.Delta) {
w.follows[delta.Node] = delta.New()
}
func NewCyclicWalker(n int) *MapWalker {
func NewCyclicWalker(n int) *SimpleWalker {
follows := make(map[graph.ID][]graph.ID, n)
for i := range n {
node := graph.ID(strconv.Itoa(i))
@@ -35,18 +35,18 @@ func NewCyclicWalker(n int) *MapWalker {
follows[node] = []graph.ID{next}
}
return &MapWalker{follows: follows}
return &SimpleWalker{follows: follows}
}
// CachedWalker is a walker with optional fallback that stores follow relationships
// in a compact format (uint32) for reduced memory footprint.
type cachedWalker struct {
type CachedWalker struct {
follows map[graph.ID][]graph.ID
fallback Walker
}
func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *cachedWalker {
w := cachedWalker{
func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *CachedWalker {
w := CachedWalker{
follows: make(map[graph.ID][]graph.ID, len(nodes)),
fallback: fallback,
}
@@ -58,7 +58,7 @@ func NewCachedWalker(nodes []graph.ID, follows [][]graph.ID, fallback Walker) *c
return &w
}
func (w *cachedWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
func (w *CachedWalker) Follows(ctx context.Context, node graph.ID) ([]graph.ID, error) {
follows, exists := w.follows[node]
if !exists {
var err error

View File

@@ -102,12 +102,13 @@ func TestToRemove(t *testing.T) {
}
func TestUpdateRemove(t *testing.T) {
walker := NewWalker(map[graph.ID][]graph.ID{
"0": {"3"},
"1": {"2"},
"2": {"0"},
"3": {"2"},
})
walker := NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"3"},
"1": {"2"},
"2": {"0"},
"3": {"2"},
})
delta := graph.Delta{
Node: "0",

View File

@@ -8,7 +8,7 @@ import (
)
type Setup struct {
walker *walks.MapWalker
walker *walks.SimpleWalker
deltas []graph.Delta
nodes []graph.ID
@@ -54,7 +54,7 @@ func Dandlings(n int) Setup {
}
return Setup{
walker: walks.NewWalker(make(map[graph.ID][]graph.ID)),
walker: walks.NewSimpleWalker(make(map[graph.ID][]graph.ID)),
deltas: deltas,
nodes: nodes,
global: global,
@@ -93,13 +93,14 @@ func Cyclic(n int) Setup {
var Triangle = Cyclic(3)
var Acyclic1 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {"3"},
"3": {"1"},
"4": {},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {"3"},
"3": {"1"},
"4": {},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -136,14 +137,15 @@ var Acyclic1 = Setup{
}
var Acyclic2 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {},
"4": {"3", "5"},
"5": {},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {},
"4": {"3", "5"},
"5": {},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -167,12 +169,13 @@ var Acyclic2 = Setup{
}
var Acyclic3 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {"1", "2"},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {"1", "2"},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -190,12 +193,13 @@ var Acyclic3 = Setup{
}
var Acyclic4 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {"1"},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"1", "2"},
"1": {},
"2": {},
"3": {"1"},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"1"}, Keep: []graph.ID{"2"}},
@@ -218,12 +222,13 @@ var Acyclic4 = Setup{
}
var Acyclic5 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"3"},
"1": {"0"},
"2": {},
"3": {"2"},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"3"},
"1": {"0"},
"2": {},
"3": {"2"},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"3"}},
@@ -246,13 +251,14 @@ var Acyclic5 = Setup{
}
var Acyclic6 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
"0": {"4"},
"1": {"0"},
"2": {},
"3": {"1", "4"},
"4": {"2"},
}),
walker: walks.NewSimpleWalker(
map[graph.ID][]graph.ID{
"0": {"4"},
"1": {"0"},
"2": {},
"3": {"1", "4"},
"4": {"2"},
}),
deltas: []graph.Delta{
// removals
{Node: "0", Remove: []graph.ID{"4"}},
@@ -290,7 +296,7 @@ var Acyclic6 = Setup{
}
var Acyclic7 = Setup{
walker: walks.NewWalker(map[graph.ID][]graph.ID{
walker: walks.NewSimpleWalker(map[graph.ID][]graph.ID{
"0": {"1", "2", "3"},
"1": {},
"2": {},