forked from k-sone/critbitgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
critbit.go
307 lines (270 loc) · 6.17 KB
/
critbit.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package ipcritbit
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"os"
"strconv"
)
// The matrix of most significant bit
var msbMatrix [256]byte
func buildMsbMatrix() {
for i := 0; i < len(msbMatrix); i++ {
b := byte(i)
b |= b >> 1
b |= b >> 2
b |= b >> 4
msbMatrix[i] = b &^ (b >> 1)
}
}
func init() {
buildMsbMatrix()
}
type node struct {
internal *internal
external *external
}
type internal struct {
child [2]node
offset int
bit byte
cont bool // if true, key of child[1] contains key of child[0]
}
type external struct {
key []byte
value interface{}
}
// critBitTree
type critBitTree struct {
root node
items int
}
// create a tree.
func newTree() *critBitTree {
return &critBitTree{}
}
// finding the critical bit.
func (n *external) criticalBit(key []byte) (offset int, bit byte, cont bool) {
nlen := len(n.key)
klen := len(key)
mlen := nlen
if nlen > klen {
mlen = klen
}
// find first differing byte and bit
for offset = 0; offset < mlen; offset++ {
if a, b := key[offset], n.key[offset]; a != b {
bit = msbMatrix[a^b]
return
}
}
if nlen < klen {
bit = msbMatrix[key[offset]]
} else if nlen > klen {
bit = msbMatrix[n.key[offset]]
} else {
// two keys are equal
offset = -1
}
return offset, bit, true
}
// calculate direction.
func (n *internal) direction(key []byte) int {
if n.offset < len(key) && (key[n.offset]&n.bit != 0 || n.cont) {
return 1
}
return 0
}
// searching the tree.
func (t *critBitTree) search(key []byte) *node {
n := &t.root
for n.internal != nil {
n = &n.internal.child[n.internal.direction(key)]
}
return n
}
// membership testing.
func (t *critBitTree) contains(key []byte) bool {
if n := t.search(key); n.external != nil && bytes.Equal(n.external.key, key) {
return true
}
return false
}
// get member.
// if `key` is in Trie, `ok` is true.
func (t *critBitTree) get(key []byte) (value interface{}, ok bool) {
if n := t.search(key); n.external != nil && bytes.Equal(n.external.key, key) {
return n.external.value, true
}
return
}
// insertHelper into the tree (replaceable).
func (t *critBitTree) insertHelper(key []byte, value interface{}, replace bool) bool {
// an empty tree
if t.items == 0 {
t.root.external = &external{
key: key,
value: value,
}
t.items = 1
return true
}
n := t.search(key)
newOffset, newBit, newCont := n.external.criticalBit(key)
// already exists in the tree
if newOffset == -1 {
if replace {
n.external.value = value
return true
}
return false
}
// allocate new node
newNode := &internal{
offset: newOffset,
bit: newBit,
cont: newCont,
}
direction := newNode.direction(key)
newNode.child[direction].external = &external{
key: key,
value: value,
}
// insert new node
wherep := &t.root
for in := wherep.internal; in != nil; in = wherep.internal {
if in.offset > newOffset || (in.offset == newOffset && in.bit < newBit) {
break
}
wherep = &in.child[in.direction(key)]
}
if wherep.internal != nil {
newNode.child[1-direction].internal = wherep.internal
} else {
newNode.child[1-direction].external = wherep.external
wherep.external = nil
}
wherep.internal = newNode
t.items += 1
return true
}
// insert into the tree.
// if `key` is alredy in Trie, return false.
func (t *critBitTree) insert(key []byte, value interface{}) bool {
return t.insertHelper(key, value, false)
}
// set into the tree.
func (t *critBitTree) set(key []byte, value interface{}) {
t.insertHelper(key, value, true)
}
// deleting elements.
// if `key` is in Trie, `ok` is true.
func (t *critBitTree) delete(key []byte) (value interface{}, ok bool) {
// an empty tree
if t.items == 0 {
return
}
var direction int
var whereq *node // pointer to the grandparent
var wherep *node = &t.root
// finding the best candidate to delete
for in := wherep.internal; in != nil; in = wherep.internal {
direction = in.direction(key)
whereq = wherep
wherep = &in.child[direction]
}
// checking that we have the right element
if !bytes.Equal(wherep.external.key, key) {
return
}
value = wherep.external.value
ok = true
// removing the node
if whereq == nil {
wherep.external = nil
} else {
othern := whereq.internal.child[1-direction]
whereq.internal = othern.internal
whereq.external = othern.external
}
t.items -= 1
return
}
// clearing a tree.
func (t *critBitTree) clear() {
t.root.internal = nil
t.root.external = nil
t.items = 0
}
// return the number of key in a tree.
func (t *critBitTree) size() int {
return t.items
}
// Iterating elements from a given start key.
// handle is called with arguments key and value (if handle returns `false`, the iteration is aborted)
func (t *critBitTree) walk(handle func(key []byte, value interface{}) bool) bool {
if t.items == 0 {
return true
}
return walkHelper(&t.root, handle)
}
func walkHelper(n *node, handle func([]byte, interface{}) bool) bool {
if n.internal != nil {
var direction int
if !walkHelper(&n.internal.child[direction], handle) {
return false
}
if direction == 0 {
// iteration another side
return walkHelper(&n.internal.child[1], handle)
}
return true
} else {
return handle(n.external.key, n.external.value)
}
}
// dump tree. (for debugging)
func (t *critBitTree) dump(w io.Writer) {
if t.root.internal == nil && t.root.external == nil {
return
}
if w == nil {
w = os.Stdout
}
dumpHelper(w, &t.root, true, "")
}
func dumpHelper(w io.Writer, n *node, right bool, prefix string) {
var ownprefix string
if right {
ownprefix = prefix
} else {
ownprefix = prefix[:len(prefix)-1] + "`"
}
if in := n.internal; in != nil {
fmt.Fprintf(w, "%s-- off=%d, bit=%08b(%02x), cont=%v\n", ownprefix, in.offset, in.bit, in.bit, in.cont)
for i := 0; i < 2; i++ {
var nextprefix string
switch i {
case 0:
nextprefix = prefix + " |"
right = true
case 1:
nextprefix = prefix + " "
right = false
}
dumpHelper(w, &in.child[i], right, nextprefix)
}
} else {
fmt.Fprintf(w, "%s-- key=%d (%s)\n", ownprefix, n.external.key, key2str(n.external.key))
}
return
}
func key2str(key []byte) string {
for _, c := range key {
if !strconv.IsPrint(rune(c)) {
return hex.EncodeToString(key)
}
}
return string(key)
}