-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
path_trie.go
184 lines (169 loc) · 5.21 KB
/
path_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
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
package trie
// PathTrie is a trie of paths with string keys and interface{} values.
// PathTrie is a trie of string keys and interface{} values. Internal nodes
// have nil values so stored nil values cannot be distinguished and are
// excluded from walks. By default, PathTrie will segment keys by forward
// slashes with PathSegmenter (e.g. "/a/b/c" -> "/a", "/b", "/c"). A custom
// StringSegmenter may be used to customize how strings are segmented into
// nodes. A classic trie might segment keys by rune (i.e. unicode points).
type PathTrie struct {
segmenter StringSegmenter // key segmenter, must not cause heap allocs
value interface{}
children map[string]*PathTrie
}
// PathTrieConfig for building a path trie with different segmenter
type PathTrieConfig struct {
Segmenter StringSegmenter
}
// NewPathTrie allocates and returns a new *PathTrie.
func NewPathTrie() *PathTrie {
return &PathTrie{
segmenter: PathSegmenter,
}
}
// NewPathTrieWithConfig allocates and returns a new *PathTrie with the given *PathTrieConfig
func NewPathTrieWithConfig(config *PathTrieConfig) *PathTrie {
segmenter := PathSegmenter
if config != nil && config.Segmenter != nil {
segmenter = config.Segmenter
}
return &PathTrie{
segmenter: segmenter,
}
}
// newPathTrieFromTrie returns new trie while preserving its config
func (trie *PathTrie) newPathTrie() *PathTrie {
return &PathTrie{
segmenter: trie.segmenter,
}
}
// Get returns the value stored at the given key. Returns nil for internal
// nodes or for nodes with a value of nil.
func (trie *PathTrie) Get(key string) interface{} {
node := trie
for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) {
node = node.children[part]
if node == nil {
return nil
}
}
return node.value
}
// Put inserts the value into the trie at the given key, replacing any
// existing items. It returns true if the put adds a new value, false
// if it replaces an existing value.
// Note that internal nodes have nil values so a stored nil value will not
// be distinguishable and will not be included in Walks.
func (trie *PathTrie) Put(key string, value interface{}) bool {
node := trie
for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) {
child := node.children[part]
if child == nil {
if node.children == nil {
node.children = map[string]*PathTrie{}
}
child = trie.newPathTrie()
node.children[part] = child
}
node = child
}
// does node have an existing value?
isNewVal := node.value == nil
node.value = value
return isNewVal
}
// Delete removes the value associated with the given key. Returns true if a
// node was found for the given key. If the node or any of its ancestors
// becomes childless as a result, it is removed from the trie.
func (trie *PathTrie) Delete(key string) bool {
var path []nodeStr // record ancestors to check later
node := trie
for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) {
path = append(path, nodeStr{part: part, node: node})
node = node.children[part]
if node == nil {
// node does not exist
return false
}
}
// delete the node value
node.value = nil
// if leaf, remove it from its parent's children map. Repeat for ancestor path.
if node.isLeaf() {
// iterate backwards over path
for i := len(path) - 1; i >= 0; i-- {
parent := path[i].node
part := path[i].part
delete(parent.children, part)
if !parent.isLeaf() {
// parent has other children, stop
break
}
parent.children = nil
if parent.value != nil {
// parent has a value, stop
break
}
}
}
return true // node (internal or not) existed and its value was nil'd
}
// Walk iterates over each key/value stored in the trie and calls the given
// walker function with the key and value. If the walker function returns
// an error, the walk is aborted.
// The traversal is depth first with no guaranteed order.
func (trie *PathTrie) Walk(walker WalkFunc) error {
return trie.walk("", walker)
}
// WalkPath iterates over each key/value in the path in trie from the root to
// the node at the given key, calling the given walker function for each
// key/value. If the walker function returns an error, the walk is aborted.
func (trie *PathTrie) WalkPath(key string, walker WalkFunc) error {
// Get root value if one exists.
if trie.value != nil {
if err := walker("", trie.value); err != nil {
return err
}
}
for part, i := trie.segmenter(key, 0); ; part, i = trie.segmenter(key, i) {
if trie = trie.children[part]; trie == nil {
return nil
}
if trie.value != nil {
var k string
if i == -1 {
k = key
} else {
k = key[0:i]
}
if err := walker(k, trie.value); err != nil {
return err
}
}
if i == -1 {
break
}
}
return nil
}
// PathTrie node and the part string key of the child the path descends into.
type nodeStr struct {
node *PathTrie
part string
}
func (trie *PathTrie) walk(key string, walker WalkFunc) error {
if trie.value != nil {
if err := walker(key, trie.value); err != nil {
return err
}
}
for part, child := range trie.children {
if err := child.walk(key+part, walker); err != nil {
return err
}
}
return nil
}
func (trie *PathTrie) isLeaf() bool {
return len(trie.children) == 0
}