-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
rune_trie.go
148 lines (137 loc) · 3.89 KB
/
rune_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
package trie
// RuneTrie is a trie of runes with string keys and interface{} values.
// Note that internal nodes have nil values so a stored nil value will not
// be distinguishable and will not be included in Walks.
type RuneTrie struct {
value interface{}
children map[rune]*RuneTrie
}
// NewRuneTrie allocates and returns a new *RuneTrie.
func NewRuneTrie() *RuneTrie {
return new(RuneTrie)
}
// Get returns the value stored at the given key. Returns nil for internal
// nodes or for nodes with a value of nil.
func (trie *RuneTrie) Get(key string) interface{} {
node := trie
for _, r := range key {
node = node.children[r]
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 *RuneTrie) Put(key string, value interface{}) bool {
node := trie
for _, r := range key {
child := node.children[r]
if child == nil {
if node.children == nil {
node.children = map[rune]*RuneTrie{}
}
child = new(RuneTrie)
node.children[r] = 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 *RuneTrie) Delete(key string) bool {
path := make([]nodeRune, len(key)) // record ancestors to check later
node := trie
for i, r := range key {
path[i] = nodeRune{r: r, node: node}
node = node.children[r]
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(key) - 1; i >= 0; i-- {
if path[i].node == nil {
continue
}
parent := path[i].node
r := path[i].r
delete(parent.children, r)
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 *RuneTrie) 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 *RuneTrie) 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 i, r := range key {
if trie = trie.children[r]; trie == nil {
return nil
}
if trie.value != nil {
if err := walker(string(key[0:i+1]), trie.value); err != nil {
return err
}
}
}
return nil
}
// RuneTrie node and the rune key of the child the path descends into.
type nodeRune struct {
node *RuneTrie
r rune
}
func (trie *RuneTrie) walk(key string, walker WalkFunc) error {
if trie.value != nil {
if err := walker(key, trie.value); err != nil {
return err
}
}
for r, child := range trie.children {
if err := child.walk(key+string(r), walker); err != nil {
return err
}
}
return nil
}
func (trie *RuneTrie) isLeaf() bool {
return len(trie.children) == 0
}