| 1 | package triescip-go gomod github.com/dghubble/trie . `github.com/dghubble/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 RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie# struct { |
| 7 | valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. interface{} |
| 8 | childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children. map[rune]*RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie# |
| 9 | } |
| 10 | |
| 11 | // NewRuneTrie allocates and returns a new *RuneTrie. |
| 12 | func NewRuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/NewRuneTrie().() *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie# { |
| 13 | return new(RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/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 *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) Getscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#Get().(key string) interface{} { |
| 19 | node := trie |
| 20 | for _, r := range key { |
| 21 | node = node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.[r] |
| 22 | if node == nil { |
| 23 | return nil |
| 24 | } |
| 25 | } |
| 26 | return node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#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 *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) Putscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#Put().(key string, value interface{}) bool { |
| 35 | node := trie |
| 36 | for _, r := range key { |
| 37 | child := node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.[r] |
| 38 | if child == nil { |
| 39 | if node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children. == nil { |
| 40 | node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children. = map[rune]*RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#{} |
| 41 | } |
| 42 | child = new(RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) |
| 43 | node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.[r] = child |
| 44 | } |
| 45 | node = child |
| 46 | } |
| 47 | // does node have an existing value? |
| 48 | isNewVal := node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. == nil |
| 49 | node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#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 *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) Deletescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#Delete().(key string) bool { |
| 57 | path := make([]nodeRunescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#, len(key)) // record ancestors to check later |
| 58 | node := trie |
| 59 | for i, r := range key { |
| 60 | path[i] = nodeRunescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#{rscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#r.: r, nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#node.: node} |
| 61 | node = node.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.[r] |
| 62 | if node == nil { |
| 63 | // node does not exist |
| 64 | return false |
| 65 | } |
| 66 | } |
| 67 | // delete the node value |
| 68 | node.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. = nil |
| 69 | // if leaf, remove it from its parent's children map. Repeat for ancestor |
| 70 | // path. |
| 71 | if node.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#isLeaf().() { |
| 72 | // iterate backwards over path |
| 73 | for i := len(key) - 1; i >= 0; i-- { |
| 74 | parent := path[i].nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#node. |
| 75 | r := path[i].rscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#r. |
| 76 | delete(parent.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children., r) |
| 77 | if !parent.isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#isLeaf().() { |
| 78 | // parent has other children, stop |
| 79 | break |
| 80 | } |
| 81 | parent.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children. = nil |
| 82 | if parent.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#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 *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) Walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#Walk().(walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error { |
| 96 | return trie.walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#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 *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) WalkPathscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#WalkPath().(key string, walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error { |
| 103 | // Get root value if one exists. |
| 104 | if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. != nil { |
| 105 | if err := walker("", trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value.); err != nil { |
| 106 | return err |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | for i, r := range key { |
| 111 | if trie = trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.[r]; trie == nil { |
| 112 | return nil |
| 113 | } |
| 114 | if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. != nil { |
| 115 | if err := walker(string(key[0:i+1]), trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#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 nodeRunescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune# struct { |
| 125 | nodescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#node. *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie# |
| 126 | rscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/nodeRune#r. rune |
| 127 | } |
| 128 | |
| 129 | func (trie *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#walk().(key string, walker WalkFuncscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/WalkFunc#) error { |
| 130 | if trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value. != nil { |
| 131 | if err := walker(key, trie.valuescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#value.); err != nil { |
| 132 | return err |
| 133 | } |
| 134 | } |
| 135 | for r, child := range trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children. { |
| 136 | if err := child.walkscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#walk().(key+string(r), walker); err != nil { |
| 137 | return err |
| 138 | } |
| 139 | } |
| 140 | return nil |
| 141 | } |
| 142 | |
| 143 | func (trie *RuneTriescip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#) isLeafscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#isLeaf().() bool { |
| 144 | return len(trie.childrenscip-go gomod github.com/dghubble/trie . `github.com/dghubble/trie`/RuneTrie#children.) == 0 |
| 145 | } |