Debian Codegraph @

path_trie.go

raw symbols SCIP
1 package triescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/
2
3 // PathTrie is a trie of paths with string keys and interface{} values.
4
5 // PathTrie is a trie of string keys and interface{} values. Internal nodes
6 // have nil values so stored nil values cannot be distinguished and are
7 // excluded from walks. By default, PathTrie will segment keys by forward
8 // slashes with PathSegmenter (e.g. "/a/b/c" -> "/a", "/b", "/c"). A custom
9 // StringSegmenter may be used to customize how strings are segmented into
10 // nodes. A classic trie might segment keys by rune (i.e. unicode points).
11 type PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie# struct {
12 segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter. StringSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/StringSegmenter# // key segmenter, must not cause heap allocs
13 valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. interface{}
14 childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children. map[string]*PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#
15 }
16
17 // PathTrieConfig for building a path trie with different segmenter
18 type PathTrieConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig# struct {
19 Segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#Segmenter. StringSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/StringSegmenter#
20 }
21
22 // NewPathTrie allocates and returns a new *PathTrie.
23 func NewPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrie().() *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie# {
24 return &PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#{
25 segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.: PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().,
26 }
27 }
28
29 // NewPathTrieWithConfig allocates and returns a new *PathTrie with the given *PathTrieConfig
30 func NewPathTrieWithConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewPathTrieWithConfig().(config *PathTrieConfigscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#) *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie# {
31 segmenter := PathSegmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathSegmenter().
32 if config != nil && config.Segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#Segmenter. != nil {
33 segmenter = config.Segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrieConfig#Segmenter.
34 }
35
36 return &PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#{
37 segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.: segmenter,
38 }
39 }
40
41 // newPathTrieFromTrie returns new trie while preserving its config
42 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) newPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#newPathTrie().() *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie# {
43 return &PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#{
44 segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.: trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.,
45 }
46 }
47
48 // Get returns the value stored at the given key. Returns nil for internal
49 // nodes or for nodes with a value of nil.
50 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Get().(key string) interface{} {
51 node := trie
52 for part, i := trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, 0); part != ""; part, i = trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, i) {
53 node = node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.[part]
54 if node == nil {
55 return nil
56 }
57 }
58 return node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value.
59 }
60
61 // Put inserts the value into the trie at the given key, replacing any
62 // existing items. It returns true if the put adds a new value, false
63 // if it replaces an existing value.
64 // Note that internal nodes have nil values so a stored nil value will not
65 // be distinguishable and will not be included in Walks.
66 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Put().(key string, value interface{}) bool {
67 node := trie
68 for part, i := trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, 0); part != ""; part, i = trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, i) {
69 child := node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.[part]
70 if child == nil {
71 if node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children. == nil {
72 node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children. = map[string]*PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#{}
73 }
74 child = trie.newPathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#newPathTrie().()
75 node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.[part] = child
76 }
77 node = child
78 }
79 // does node have an existing value?
80 isNewVal := node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. == nil
81 node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. = value
82 return isNewVal
83 }
84
85 // Delete removes the value associated with the given key. Returns true if a
86 // node was found for the given key. If the node or any of its ancestors
87 // becomes childless as a result, it is removed from the trie.
88 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) Deletescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Delete().(key string) bool {
89 var path []nodeStrscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr# // record ancestors to check later
90 node := trie
91 for part, i := trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, 0); part != ""; part, i = trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, i) {
92 path = append(path, nodeStrscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#{partscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#part.: part, nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#node.: node})
93 node = node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.[part]
94 if node == nil {
95 // node does not exist
96 return false
97 }
98 }
99 // delete the node value
100 node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. = nil
101 // if leaf, remove it from its parent's children map. Repeat for ancestor path.
102 if node.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#isLeaf().() {
103 // iterate backwards over path
104 for i := len(path) - 1; i >= 0; i-- {
105 parent := path[i].nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#node.
106 part := path[i].partscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#part.
107 delete(parent.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children., part)
108 if !parent.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#isLeaf().() {
109 // parent has other children, stop
110 break
111 }
112 parent.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children. = nil
113 if parent.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. != nil {
114 // parent has a value, stop
115 break
116 }
117 }
118 }
119 return true // node (internal or not) existed and its value was nil'd
120 }
121
122 // Walk iterates over each key/value stored in the trie and calls the given
123 // walker function with the key and value. If the walker function returns
124 // an error, the walk is aborted.
125 // The traversal is depth first with no guaranteed order.
126 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) Walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#Walk().(walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error {
127 return trie.walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#walk().("", walker)
128 }
129
130 // WalkPath iterates over each key/value in the path in trie from the root to
131 // the node at the given key, calling the given walker function for each
132 // key/value. If the walker function returns an error, the walk is aborted.
133 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#WalkPath().(key string, walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error {
134 // Get root value if one exists.
135 if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. != nil {
136 if err := walker("", trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value.); err != nil {
137 return err
138 }
139 }
140 for part, i := trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, 0); ; part, i = trie.segmenterscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#segmenter.(key, i) {
141 if trie = trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.[part]; trie == nil {
142 return nil
143 }
144 if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. != nil {
145 var k string
146 if i == -1 {
147 k = key
148 } else {
149 k = key[0:i]
150 }
151 if err := walker(k, trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value.); err != nil {
152 return err
153 }
154 }
155 if i == -1 {
156 break
157 }
158 }
159 return nil
160 }
161
162 // PathTrie node and the part string key of the child the path descends into.
163 type nodeStrscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr# struct {
164 nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#node. *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#
165 partscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeStr#part. string
166 }
167
168 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#walk().(key string, walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error {
169 if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value. != nil {
170 if err := walker(key, trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#value.); err != nil {
171 return err
172 }
173 }
174 for part, child := range trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children. {
175 if err := child.walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#walk().(key+part, walker); err != nil {
176 return err
177 }
178 }
179 return nil
180 }
181
182 func (trie *PathTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#) isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#isLeaf().() bool {
183 return len(trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/PathTrie#children.) == 0
184 }