| 1 | package trie |
| 2 | |
| 3 | // RuneTrie is a trie of runes with string keys and interface{} values. |
| 4 | // Note that internal nodes have nil values so a stored nil value will not |
| 5 | // be distinguishable and will not be included in Walks. |
| 6 | type RuneTrie struct { |
| 7 | value interface{} |
| 8 | children map[rune]*RuneTrie |
| 9 | } |
| 10 | |
| 11 | // NewRuneTrie allocates and returns a new *RuneTrie. |
| 12 | func NewRuneTrie() *RuneTrie { |
| 13 | return new(RuneTrie) |
| 14 | } |
| 15 | |
| 16 | // Get returns the value stored at the given key. Returns nil for internal |
| 17 | // nodes or for nodes with a value of nil. |
| 18 | func (trie *RuneTrie) Get(key string) interface{} { |
| 19 | node := trie |
| 20 | for _, r := range key { |
| 21 | node = node.children[r] |
| 22 | if node == nil { |
| 23 | return nil |
| 24 | } |
| 25 | } |
| 26 | return node.value |
| 27 | } |
| 28 | |
| 29 | // Put inserts the value into the trie at the given key, replacing any |
| 30 | // existing items. It returns true if the put adds a new value, false |
| 31 | // if it replaces an existing value. |
| 32 | // Note that internal nodes have nil values so a stored nil value will not |
| 33 | // be distinguishable and will not be included in Walks. |
| 34 | func (trie *RuneTrie) Put(key string, value interface{}) bool { |
| 35 | node := trie |
| 36 | for _, r := range key { |
| 37 | child := node.children[r] |
| 38 | if child == nil { |
| 39 | if node.children == nil { |
| 40 | node.children = map[rune]*RuneTrie{} |
| 41 | } |
| 42 | child = new(RuneTrie) |
| 43 | node.children[r] = child |
| 44 | } |
| 45 | node = child |
| 46 | } |
| 47 | // does node have an existing value? |
| 48 | isNewVal := node.value == nil |
| 49 | node.value = value |
| 50 | return isNewVal |
| 51 | } |
| 52 | |
| 53 | // Delete removes the value associated with the given key. Returns true if a |
| 54 | // node was found for the given key. If the node or any of its ancestors |
| 55 | // becomes childless as a result, it is removed from the trie. |
| 56 | func (trie *RuneTrie) Delete(key string) bool { |
| 57 | path := make([]nodeRune, len(key)) // record ancestors to check later |
| 58 | node := trie |
| 59 | for i, r := range key { |
| 60 | path[i] = nodeRune{r: r, node: node} |
| 61 | node = node.children[r] |
| 62 | if node == nil { |
| 63 | // node does not exist |
| 64 | return false |
| 65 | } |
| 66 | } |
| 67 | // delete the node value |
| 68 | node.value = nil |
| 69 | // if leaf, remove it from its parent's children map. Repeat for ancestor |
| 70 | // path. |
| 71 | if node.isLeaf() { |
| 72 | // iterate backwards over path |
| 73 | for i := len(key) - 1; i >= 0; i-- { |
| 74 | parent := path[i].node |
| 75 | r := path[i].r |
| 76 | delete(parent.children, r) |
| 77 | if !parent.isLeaf() { |
| 78 | // parent has other children, stop |
| 79 | break |
| 80 | } |
| 81 | parent.children = nil |
| 82 | if parent.value != nil { |
| 83 | // parent has a value, stop |
| 84 | break |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | return true // node (internal or not) existed and its value was nil'd |
| 89 | } |
| 90 | |
| 91 | // Walk iterates over each key/value stored in the trie and calls the given |
| 92 | // walker function with the key and value. If the walker function returns |
| 93 | // an error, the walk is aborted. |
| 94 | // The traversal is depth first with no guaranteed order. |
| 95 | func (trie *RuneTrie) Walk(walker WalkFunc) error { |
| 96 | return trie.walk("", walker) |
| 97 | } |
| 98 | |
| 99 | // WalkPath iterates over each key/value in the path in trie from the root to |
| 100 | // the node at the given key, calling the given walker function for each |
| 101 | // key/value. If the walker function returns an error, the walk is aborted. |
| 102 | func (trie *RuneTrie) WalkPath(key string, walker WalkFunc) error { |
| 103 | // Get root value if one exists. |
| 104 | if trie.value != nil { |
| 105 | if err := walker("", trie.value); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for i, r := range key { |
| 111 | if trie = trie.children[r]; trie == nil { |
| 112 | return nil |
| 113 | } |
| 114 | if trie.value != nil { |
| 115 | if err := walker(string(key[0:i+1]), trie.value); err != nil { |
| 116 | return err |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | return nil |
| 121 | } |
| 122 | |
| 123 | // RuneTrie node and the rune key of the child the path descends into. |
| 124 | type nodeRune struct { |
| 125 | node *RuneTrie |
| 126 | r rune |
| 127 | } |
| 128 | |
| 129 | func (trie *RuneTrie) walk(key string, walker WalkFunc) error { |
| 130 | if trie.value != nil { |
| 131 | if err := walker(key, trie.value); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | } |
| 135 | for r, child := range trie.children { |
| 136 | if err := child.walk(key+string(r), walker); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | func (trie *RuneTrie) isLeaf() bool { |
| 144 | return len(trie.children) == 0 |
| 145 | } |