| 1 | package 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 PathTrie struct { |
| 12 | segmenter StringSegmenter // key segmenter, must not cause heap allocs |
| 13 | value interface{} |
| 14 | children map[string]*PathTrie |
| 15 | } |
| 16 | |
| 17 | // PathTrieConfig for building a path trie with different segmenter |
| 18 | type PathTrieConfig struct { |
| 19 | Segmenter StringSegmenter |
| 20 | } |
| 21 | |
| 22 | // NewPathTrie allocates and returns a new *PathTrie. |
| 23 | func NewPathTrie() *PathTrie { |
| 24 | return &PathTrie{ |
| 25 | segmenter: PathSegmenter, |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | // NewPathTrieWithConfig allocates and returns a new *PathTrie with the given *PathTrieConfig |
| 30 | func NewPathTrieWithConfig(config *PathTrieConfig) *PathTrie { |
| 31 | segmenter := PathSegmenter |
| 32 | if config != nil && config.Segmenter != nil { |
| 33 | segmenter = config.Segmenter |
| 34 | } |
| 35 | |
| 36 | return &PathTrie{ |
| 37 | segmenter: segmenter, |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | // newPathTrieFromTrie returns new trie while preserving its config |
| 42 | func (trie *PathTrie) newPathTrie() *PathTrie { |
| 43 | return &PathTrie{ |
| 44 | segmenter: trie.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 *PathTrie) Get(key string) interface{} { |
| 51 | node := trie |
| 52 | for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) { |
| 53 | node = node.children[part] |
| 54 | if node == nil { |
| 55 | return nil |
| 56 | } |
| 57 | } |
| 58 | return node.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 *PathTrie) Put(key string, value interface{}) bool { |
| 67 | node := trie |
| 68 | for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) { |
| 69 | child := node.children[part] |
| 70 | if child == nil { |
| 71 | if node.children == nil { |
| 72 | node.children = map[string]*PathTrie{} |
| 73 | } |
| 74 | child = trie.newPathTrie() |
| 75 | node.children[part] = child |
| 76 | } |
| 77 | node = child |
| 78 | } |
| 79 | // does node have an existing value? |
| 80 | isNewVal := node.value == nil |
| 81 | node.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 *PathTrie) Delete(key string) bool { |
| 89 | var path []nodeStr // record ancestors to check later |
| 90 | node := trie |
| 91 | for part, i := trie.segmenter(key, 0); part != ""; part, i = trie.segmenter(key, i) { |
| 92 | path = append(path, nodeStr{part: part, node: node}) |
| 93 | node = node.children[part] |
| 94 | if node == nil { |
| 95 | // node does not exist |
| 96 | return false |
| 97 | } |
| 98 | } |
| 99 | // delete the node value |
| 100 | node.value = nil |
| 101 | // if leaf, remove it from its parent's children map. Repeat for ancestor path. |
| 102 | if node.isLeaf() { |
| 103 | // iterate backwards over path |
| 104 | for i := len(path) - 1; i >= 0; i-- { |
| 105 | parent := path[i].node |
| 106 | part := path[i].part |
| 107 | delete(parent.children, part) |
| 108 | if !parent.isLeaf() { |
| 109 | // parent has other children, stop |
| 110 | break |
| 111 | } |
| 112 | parent.children = nil |
| 113 | if parent.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 *PathTrie) Walk(walker WalkFunc) error { |
| 127 | return trie.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 *PathTrie) WalkPath(key string, walker WalkFunc) error { |
| 134 | // Get root value if one exists. |
| 135 | if trie.value != nil { |
| 136 | if err := walker("", trie.value); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | } |
| 140 | for part, i := trie.segmenter(key, 0); ; part, i = trie.segmenter(key, i) { |
| 141 | if trie = trie.children[part]; trie == nil { |
| 142 | return nil |
| 143 | } |
| 144 | if trie.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.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 nodeStr struct { |
| 164 | node *PathTrie |
| 165 | part string |
| 166 | } |
| 167 | |
| 168 | func (trie *PathTrie) walk(key string, walker WalkFunc) error { |
| 169 | if trie.value != nil { |
| 170 | if err := walker(key, trie.value); err != nil { |
| 171 | return err |
| 172 | } |
| 173 | } |
| 174 | for part, child := range trie.children { |
| 175 | if err := child.walk(key+part, walker); err != nil { |
| 176 | return err |
| 177 | } |
| 178 | } |
| 179 | return nil |
| 180 | } |
| 181 | |
| 182 | func (trie *PathTrie) isLeaf() bool { |
| 183 | return len(trie.children) == 0 |
| 184 | } |