-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.go
210 lines (160 loc) · 3.86 KB
/
graph.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package bricks
import (
"maps"
"slices"
"sync"
"github.com/janstoon/toolbox/tricks"
)
type Graph[Key, Weight comparable, Value any] map[Key]*GraphNode[Key, Weight, Value]
func (g Graph[Key, Weight, Value]) Add(k Key, v Value) *GraphNode[Key, Weight, Value] {
n := newGraphNode[Key, Weight, Value](v)
g[k] = n
return n
}
func (g Graph[Key, Weight, Value]) Connect(root Key, kk ...Key) {
var w Weight
for _, key := range kk {
g.connect(root, key, true, w)
}
}
func (g Graph[Key, Weight, Value]) connect(k1, k2 Key, bidirect bool, weight Weight) {
n1, ok := g[k1]
if !ok {
panic("connection origin not found")
}
n2, ok := g[k2]
if !ok {
panic("connection destination not found")
}
n1.connect(k2, weight)
if bidirect {
n2.connect(k1, weight)
}
}
func (g Graph[Key, Weight, Value]) BreadthFirstSearch(from, to Key) []Key {
origin, ok := g[from]
if !ok {
return nil
}
var nn Queue[[]Key]
nn.Enqueue(tricks.Map(slices.Collect(maps.Keys(origin.neighbors)), func(k Key) []Key {
return []Key{from, k}
})...)
visited := make(map[Key]bool)
for path := range nn.All() {
k := path[len(path)-1]
if _, ok := visited[k]; ok {
continue
}
visited[k] = true
if k == to {
return path
}
n, ok := g[k]
if !ok {
panic("alien key")
}
nn.Enqueue(tricks.Map(slices.Collect(maps.Keys(n.neighbors)), func(k Key) []Key {
kk := make([]Key, len(path))
copy(kk, path)
return append(kk, k)
})...)
}
return nil
}
type GraphNode[Key, Weight comparable, Value any] struct {
neighbors map[Key]Weight
Value Value
}
func newGraphNode[Key, Weight comparable, Value any](v Value) *GraphNode[Key, Weight, Value] {
return &GraphNode[Key, Weight, Value]{
neighbors: make(map[Key]Weight),
Value: v,
}
}
func (gn *GraphNode[Key, Weight, Value]) connect(key Key, weight Weight) {
gn.neighbors[key] = weight
}
type TrieNode[Key, KeyAtom comparable, Value any] struct {
lock sync.RWMutex
children map[KeyAtom]*TrieNode[Key, KeyAtom, Value]
atomizer func(Key) []KeyAtom
full bool
value Value
}
func Trie[Key, KeyAtom comparable, Value any](atomizer func(Key) []KeyAtom) *TrieNode[Key, KeyAtom, Value] {
return &TrieNode[Key, KeyAtom, Value]{
atomizer: atomizer,
}
}
func (t *TrieNode[Key, KeyAtom, Value]) Put(key Key, val Value) {
t.put(t.atomizer(key), val)
}
func (t *TrieNode[Key, KeyAtom, Value]) put(route []KeyAtom, val Value) {
t.lock.Lock()
defer t.lock.Unlock()
if len(route) == 0 {
t.full = true
t.value = val
return
}
if t.children == nil {
t.children = make(map[KeyAtom]*TrieNode[Key, KeyAtom, Value])
}
if _, ok := t.children[route[0]]; !ok {
t.children[route[0]] = Trie[Key, KeyAtom, Value](t.atomizer)
}
t.children[route[0]].put(route[1:], val)
}
func (t *TrieNode[Key, _, Value]) Get(key Key) *Value {
return t.get(t.atomizer(key))
}
func (t *TrieNode[_, KeyAtom, Value]) get(route []KeyAtom) *Value {
t.lock.RLock()
defer t.lock.RUnlock()
if len(route) == 0 {
return t.val()
}
if child, ok := t.children[route[0]]; ok {
return child.get(route[1:])
}
return nil
}
func (t *TrieNode[_, KeyAtom, Value]) val() *Value {
if t.full {
return &t.value
}
return nil
}
func (t *TrieNode[Key, _, Value]) BestMatch(key Key) *Value {
return t.bestMatch(t.atomizer(key))
}
func (t *TrieNode[_, KeyAtom, Value]) bestMatch(route []KeyAtom) *Value {
t.lock.RLock()
defer t.lock.RUnlock()
if len(route) == 0 {
return t.val()
}
if child, ok := t.children[route[0]]; ok {
if v := child.bestMatch(route[1:]); v != nil {
return v
}
}
return t.val()
}
func (t *TrieNode[Key, _, _]) Delete(key Key) {
t.delete(t.atomizer(key))
}
func (t *TrieNode[_, KeyAtom, Value]) delete(route []KeyAtom) {
t.lock.Lock()
defer t.lock.Unlock()
if len(route) == 0 {
var zero Value
t.full = false
t.value = zero
return
}
if child, ok := t.children[route[0]]; ok {
child.delete(route[1:])
}
}