/
trie.go
104 lines (96 loc) · 2.35 KB
/
trie.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
/*
* Copyright 2019 Dgraph Labs, Inc. and Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package trie
type node struct {
children map[byte]*node
ids []uint64
}
func newNode() *node {
return &node{
children: make(map[byte]*node),
ids: []uint64{},
}
}
// Trie datastructure.
type Trie struct {
root *node
}
// NewTrie returns Trie.
func NewTrie() *Trie {
return &Trie{
root: newNode(),
}
}
// Add adds the id in the trie for the given prefix path.
func (t *Trie) Add(prefix []byte, id uint64) {
node := t.root
for _, val := range prefix {
child, ok := node.children[val]
if !ok {
child = newNode()
node.children[val] = child
}
node = child
}
// We only need to add the id to the last node of the given prefix.
node.ids = append(node.ids, id)
}
// Get returns prefix matched ids for the given key.
func (t *Trie) Get(key []byte) map[uint64]struct{} {
out := make(map[uint64]struct{})
node := t.root
// If root has ids that means we have subscribers for "nil/[]byte{}"
// prefix. Add them to the list.
if len(node.ids) > 0 {
for _, i := range node.ids {
out[i] = struct{}{}
}
}
for _, val := range key {
child, ok := node.children[val]
if !ok {
break
}
// We need ids of the all the node in the matching key path.
for _, id := range child.ids {
out[id] = struct{}{}
}
node = child
}
return out
}
// Delete will delete the id if the id exist in the given index path.
func (t *Trie) Delete(index []byte, id uint64) {
node := t.root
for _, val := range index {
child, ok := node.children[val]
if !ok {
return
}
node = child
}
// We're just removing the id not the hanging path.
out := node.ids[:0]
for _, val := range node.ids {
if val != id {
out = append(out, val)
}
}
for i := len(out); i < len(node.ids); i++ {
node.ids[i] = 0 // garbage collecting
}
node.ids = out
}