-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointertrie.go
More file actions
219 lines (207 loc) · 5.53 KB
/
pointertrie.go
File metadata and controls
219 lines (207 loc) · 5.53 KB
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
211
212
213
214
215
216
217
218
219
package kv
import (
"bytes"
"fmt"
"iter"
"strings"
)
//nolint:govet
type ptrTrieNode[V any] struct {
children []*ptrTrieNode[V]
root Optional[V]
keyByte byte
}
// NewPointerTrie returns a new, absurdly simple, and badly coded Store.
// Pointers to children are stored densely in slices.
// This is purely for fleshing out the unit tests, benchmarks, and fuzz tests.
//
//nolint:iface
func NewPointerTrie[V any]() Store[V] {
return &ptrTrieNode[V]{}
}
func (n *ptrTrieNode[V]) Get(key []byte) (V, bool) {
if key == nil {
panic("key must be non-nil")
}
var zero V
for _, keyByte := range key {
index, found := n.search(keyByte)
if !found {
return zero, false
}
n = n.children[index]
}
// n = found key
return n.root.Get()
}
func (n *ptrTrieNode[V]) Set(key []byte, value V) (V, bool) {
if key == nil {
panic("key must be non-nil")
}
var zero V
for i, keyByte := range key {
index, found := n.search(keyByte)
if !found {
k := len(key) - 1
child := &ptrTrieNode[V]{nil, OptionalOf(value), key[k]}
for k--; k >= i; k-- {
child = &ptrTrieNode[V]{[]*ptrTrieNode[V]{child}, Optional[V]{}, key[k]}
}
n.children = append(n.children, child)
copy(n.children[index+1:], n.children[index:])
n.children[index] = child
return zero, false
}
n = n.children[index]
}
// n = found key, replace value
return n.root.Set(value)
}
func (n *ptrTrieNode[V]) Delete(key []byte) (V, bool) {
if key == nil {
panic("key must be non-nil")
}
var zero V
// If the deleted node has no children, remove the subtree rooted at prune.children[pruneIndex].
var prune *ptrTrieNode[V]
var pruneIndex int
for i, keyByte := range key {
index, found := n.search(keyByte)
if !found {
return zero, false
}
// If either n is the root, or n has a value, or n has more than one child, then n itself cannot be pruned.
// If so, move the maybe-pruned subtree to n.children[index].
if i == 0 || !n.root.IsEmpty() || len(n.children) > 1 {
prune, pruneIndex = n, index
}
n = n.children[index]
}
// n = found key
value, ok := n.root.Clear()
if ok && len(key) > 0 && len(n.children) == 0 {
children := prune.children
copy(children[pruneIndex:], children[pruneIndex+1:])
children[len(children)-1] = nil
prune.children = children[:len(children)-1]
}
return value, ok
}
// An iter.Seq of these is returned from the adjFunction used internally by Range.
// key = path from root to node
// It is cached here for efficiency, otherwise an iter.Seq of []*ptrTrieNode[V] would be used directly.
// Note that the key must be cloned when yielded from Range.
type ptrTrieRangePath[V any] struct {
node *ptrTrieNode[V]
key []byte
}
func (n *ptrTrieNode[V]) Range(bounds *Bounds) iter.Seq2[[]byte, V] {
bounds = bounds.Clone()
root := ptrTrieRangePath[V]{n, []byte{}}
var pathItr iter.Seq[*ptrTrieRangePath[V]]
if bounds.IsReverse {
pathItr = postOrder(&root, ptrTrieReverseAdj[V](bounds))
} else {
pathItr = preOrder(&root, ptrTrieForwardAdj[V](bounds))
}
return func(yield func([]byte, V) bool) {
for path := range pathItr {
cmp := bounds.CompareKey(path.key)
if cmp < 0 {
continue
}
if cmp > 0 {
return
}
if value, ok := path.node.root.Get(); ok && !yield(bytes.Clone(path.key), value) {
return
}
}
}
}
func ptrTrieForwardAdj[V any](bounds *Bounds) adjFunction[*ptrTrieRangePath[V]] {
// Sometimes a child is not within the bounds, but one of its descendants is.
return func(path *ptrTrieRangePath[V]) iter.Seq[*ptrTrieRangePath[V]] {
if len(path.node.children) == 0 {
return emptySeq
}
start, stop, ok := bounds.childBounds(path.key)
if !ok {
// Unreachable because of how the trie is traversed forward.
panic("unreachable")
}
return func(yield func(*ptrTrieRangePath[V]) bool) {
for _, child := range path.node.children {
keyByte := child.keyByte
if keyByte < start {
continue
}
if keyByte > stop {
return
}
if !yield(&ptrTrieRangePath[V]{child, append(path.key, keyByte)}) {
return
}
}
}
}
}
func ptrTrieReverseAdj[V any](bounds *Bounds) adjFunction[*ptrTrieRangePath[V]] {
// Sometimes a child is not within the bounds, but one of its descendants is.
return func(path *ptrTrieRangePath[V]) iter.Seq[*ptrTrieRangePath[V]] {
if len(path.node.children) == 0 {
return emptySeq
}
start, stop, ok := bounds.childBounds(path.key)
if !ok {
return emptySeq
}
return func(yield func(*ptrTrieRangePath[V]) bool) {
for i := len(path.node.children) - 1; i >= 0; i-- {
child := path.node.children[i]
keyByte := child.keyByte
if keyByte > start {
continue
}
if keyByte < stop {
return
}
if !yield(&ptrTrieRangePath[V]{child, append(path.key, keyByte)}) {
return
}
}
}
}
}
func (n *ptrTrieNode[V]) search(byt byte) (int, bool) {
// Copied and tweaked from sort.Search. Inlining this is much, much faster.
// Invariant: child[i-1] < byt <= child[j]
i, j := 0, len(n.children)
for i < j {
//nolint:gosec
h := int(uint(i+j) >> 1) // avoid overflow when computing h
// i ≤ h < j
childByte := n.children[h].keyByte
if childByte == byt {
return h, true
}
if childByte < byt {
i = h + 1 // preserves child[i-1] < byt
} else {
j = h // preserves byt <= child[j]
}
}
return i, false
}
func (n *ptrTrieNode[V]) String() string {
var s strings.Builder
s.WriteString("{")
if value, ok := n.root.Get(); ok {
fmt.Fprintf(&s, ":%v, ", value)
}
for _, child := range n.children {
fmt.Fprintf(&s, "%02X:%s, ", child.keyByte, child)
}
s.WriteString("}")
return s.String()
}