-
Notifications
You must be signed in to change notification settings - Fork 2
/
fn.go
293 lines (242 loc) · 6.26 KB
/
fn.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
// Copyright 2016 Evans. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cedar
import (
"errors"
)
var (
// ErrNoKey not have key error
ErrNoKey = errors.New("cedar: not have key")
// ErrNoVal not have value error
ErrNoVal = errors.New("cedar: not have val")
// ErrInvalidKey invalid key error
ErrInvalidKey = errors.New("cedar: invalid key")
// ErrInvalidVal invalid value error
ErrInvalidVal = errors.New("cedar: invalid val")
)
func isReduced(reduced ...bool) bool {
if len(reduced) > 0 && !reduced[0] {
return false
}
return true
}
func (cd *Cedar) get(key []byte, from, pos int) *int {
to := cd.getNode(key, from, pos)
return &cd.array[to].baseV
}
// getNode get the follow node by key, split by update()
func (cd *Cedar) getNode(key []byte, from, pos int) int {
for ; pos < len(key); pos++ {
if cd.Reduced {
value := cd.array[from].baseV
if value >= 0 && value != ValLimit {
to := cd.follow(from, 0)
cd.array[to].baseV = value
}
}
from = cd.follow(from, key[pos])
}
to := from
if cd.array[from].baseV < 0 || !cd.Reduced {
to = cd.follow(from, 0)
}
return to
}
// Jump jump a node `from` to another node by following the `path`, split by find()
func (cd *Cedar) Jump(key []byte, from int) (to int, err error) {
// pos := 0
// recursively matching the key.
for _, k := range key {
if cd.array[from].baseV >= 0 && cd.Reduced {
return from, ErrNoKey
}
to = cd.array[from].base(cd.Reduced) ^ int(k)
if cd.array[to].check != from {
return from, ErrNoKey
}
from = to
}
return to, nil
}
// Find key from double array trie, with `from` as the cursor to traverse the nodes.
func (cd *Cedar) Find(key []byte, from int) (int, error) {
to, err := cd.Jump(key, from)
if cd.Reduced {
if cd.array[to].baseV >= 0 {
if err == nil && to != 0 {
return cd.array[to].baseV, nil
}
return 0, ErrNoKey
}
}
// return the value of the node if `check` is correctly marked fpr the ownership,
// otherwise it means no value is stored.
n := cd.array[cd.array[to].base(cd.Reduced)]
if n.check != to {
return 0, ErrNoKey
}
return n.baseV, nil
}
// Value get the path value
func (cd *Cedar) Value(path int) (val int, err error) {
val = cd.array[path].baseV
if val >= 0 {
return val, nil
}
to := cd.array[path].base(cd.Reduced)
if cd.array[to].check == path && cd.array[to].baseV >= 0 {
return cd.array[to].baseV, nil
}
return 0, ErrNoVal
}
// Insert the key for the value on []byte
func (cd *Cedar) Insert(key []byte, val int) error {
if val < 0 || val >= ValLimit {
return ErrInvalidVal
}
p := cd.get(key, 0, 0)
*p = val
return nil
}
// Update the key for the value, it is public interface that works on []byte
func (cd *Cedar) Update(key []byte, value int) error {
p := cd.get(key, 0, 0)
if *p == ValLimit && cd.Reduced {
*p = value
return nil
}
*p += value
return nil
}
// Delete the key from the trie, the internal interface that works on []byte
func (cd *Cedar) Delete(key []byte) error {
// move the cursor to the right place and use erase__ to delete it.
to, err := cd.Jump(key, 0)
if err != nil {
return ErrNoKey
}
if cd.array[to].baseV < 0 && cd.Reduced {
base := cd.array[to].base(cd.Reduced)
if cd.array[base].check == to {
to = base
}
}
if !cd.Reduced {
to = cd.array[to].base(cd.Reduced)
}
from := to
for to > 0 {
if cd.Reduced {
from = cd.array[to].check
}
base := cd.array[from].base(cd.Reduced)
label := byte(to ^ base)
hasSibling := cd.nInfos[to].sibling != 0 || cd.nInfos[from].child != label
// if the node has siblings, then remove `e` from the sibling.
if hasSibling {
cd.popSibling(from, base, label)
}
// maintain the data structures.
cd.pushENode(to)
// traverse to the parent.
to = from
// if it has sibling then this layer has more than one nodes, then we are done.
if hasSibling {
break
}
}
return nil
}
// Get get the key value on []byte
func (cd *Cedar) Get(key []byte) (value int, err error) {
to, err := cd.Jump(key, 0)
if err != nil {
return 0, err
}
return cd.Value(to)
}
// ExactMatch to check if `key` is in the dictionary.
func (cd *Cedar) ExactMatch(key []byte) (int, bool) {
from := 0
val, err := cd.Find(key, from)
if err != nil {
return 0, false
}
return val, true
}
// PrefixMatch return the collection of the common prefix
// in the dictionary with the `key`
func (cd *Cedar) PrefixMatch(key []byte, n ...int) (ids []int) {
num := 0
if len(n) > 0 {
num = n[0]
}
for from, i := 0, 0; i < len(key); i++ {
to, err := cd.Jump(key[i:i+1], from)
if err != nil {
break
}
_, err = cd.Value(to)
if err == nil {
ids = append(ids, to)
num--
if num == 0 {
return
}
}
from = to
}
return
}
// PrefixPredict eturn the list of words in the dictionary
// that has `key` as their prefix
func (cd *Cedar) PrefixPredict(key []byte, n ...int) (ids []int) {
num := 0
if len(n) > 0 {
num = n[0]
}
root, err := cd.Jump(key, 0)
if err != nil {
return
}
for from, err := cd.begin(root); err == nil; from, err = cd.next(from, root) {
ids = append(ids, from)
num--
if num == 0 {
return
}
}
return
}
// To get the cursor of the first leaf node starting by `from`
func (cd *Cedar) begin(from int) (to int, err error) {
// recursively traversing down to look for the first leaf.
for c := cd.nInfos[from].child; c != 0; {
from = cd.array[from].base(cd.Reduced) ^ int(c)
c = cd.nInfos[from].child
}
if cd.array[from].base() > 0 {
return cd.array[from].base(), nil
}
// To return the value of the leaf.
return from, nil
}
// To move the cursor from one leaf to the next for the common prefix predict.
func (cd *Cedar) next(from int, root int) (to int, err error) {
c := cd.nInfos[from].sibling
if !cd.Reduced {
c = cd.nInfos[cd.array[from].base(cd.Reduced)].sibling
}
// traversing up until there is a sibling or it has reached the root.
for c == 0 && from != root && cd.array[from].check >= 0 {
from = cd.array[from].check
c = cd.nInfos[from].sibling
}
if from == root || cd.array[from].check < 0 {
return 0, ErrNoKey
}
// it has a sibling so we leverage on `begin` to traverse the subtree down again.
from = cd.array[cd.array[from].check].base(cd.Reduced) ^ int(c)
return cd.begin(from)
}