| 1 | package radixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/ |
| 2 | |
| 3 | import ( |
| 4 | "sortscip-go gomod github.com/golang/go/src go1.16 sort/" |
| 5 | "stringsscip-go gomod github.com/golang/go/src go1.16 strings/" |
| 6 | ) |
| 7 | |
| 8 | // WalkFn is used when walking the tree. Takes a |
| 9 | // key and value, returning if iteration should |
| 10 | // be terminated. |
| 11 | type WalkFnscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/WalkFn# func(s string, v interface{}) bool |
| 12 | |
| 13 | // leafNode is used to represent a value |
| 14 | type leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode# struct { |
| 15 | keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key. string |
| 16 | valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val. interface{} |
| 17 | } |
| 18 | |
| 19 | // edge is used to represent an edge node |
| 20 | type edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge# struct { |
| 21 | labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. byte |
| 22 | nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# |
| 23 | } |
| 24 | |
| 25 | type nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# struct { |
| 26 | // leaf is used to store possible leaf |
| 27 | leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. *leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode# |
| 28 | |
| 29 | // prefix is the common prefix we ignore |
| 30 | prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix. string |
| 31 | |
| 32 | // Edges should be stored in-order for iteration. |
| 33 | // We avoid a fully materialized slice to save memory, |
| 34 | // since in most cases we expect to be sparse |
| 35 | edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges# |
| 36 | } |
| 37 | |
| 38 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() bool { |
| 39 | return n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. != nil |
| 40 | } |
| 41 | |
| 42 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) addEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#addEdge().(e edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#) { |
| 43 | num := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) |
| 44 | idx := sortscip-go gomod github.com/golang/go/src go1.16 sort/.Searchscip-go gomod github.com/golang/go/src go1.16 sort/Search().(num, func(i int) bool { |
| 45 | return n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[i].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. >= e.labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. |
| 46 | }) |
| 47 | |
| 48 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. = append(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges., edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#{}) |
| 49 | copy(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx+1:], n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx:]) |
| 50 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx] = e |
| 51 | } |
| 52 | |
| 53 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) updateEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#updateEdge().(label byte, node *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) { |
| 54 | num := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) |
| 55 | idx := sortscip-go gomod github.com/golang/go/src go1.16 sort/.Searchscip-go gomod github.com/golang/go/src go1.16 sort/Search().(num, func(i int) bool { |
| 56 | return n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[i].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. >= label |
| 57 | }) |
| 58 | if idx < num && n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. == label { |
| 59 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx].nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. = node |
| 60 | return |
| 61 | } |
| 62 | panic("replacing missing edge") |
| 63 | } |
| 64 | |
| 65 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(label byte) *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# { |
| 66 | num := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) |
| 67 | idx := sortscip-go gomod github.com/golang/go/src go1.16 sort/.Searchscip-go gomod github.com/golang/go/src go1.16 sort/Search().(num, func(i int) bool { |
| 68 | return n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[i].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. >= label |
| 69 | }) |
| 70 | if idx < num && n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. == label { |
| 71 | return n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx].nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. |
| 72 | } |
| 73 | return nil |
| 74 | } |
| 75 | |
| 76 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) delEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#delEdge().(label byte) { |
| 77 | num := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) |
| 78 | idx := sortscip-go gomod github.com/golang/go/src go1.16 sort/.Searchscip-go gomod github.com/golang/go/src go1.16 sort/Search().(num, func(i int) bool { |
| 79 | return n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[i].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. >= label |
| 80 | }) |
| 81 | if idx < num && n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. == label { |
| 82 | copy(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx:], n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[idx+1:]) |
| 83 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.)-1] = edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#{} |
| 84 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. = n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[:len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.)-1] |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | type edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges# []edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge# |
| 89 | |
| 90 | func (e edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#) Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#Len().() int { |
| 91 | return len(e) |
| 92 | } |
| 93 | |
| 94 | func (e edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#) Lessscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#Less().(i, j int) bool { |
| 95 | return e[i].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. < e[j].labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label. |
| 96 | } |
| 97 | |
| 98 | func (e edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#) Swapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#Swap().(i, j int) { |
| 99 | e[i], e[j] = e[j], e[i] |
| 100 | } |
| 101 | |
| 102 | func (e edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#) Sortscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edges#Sort().() { |
| 103 | sortscip-go gomod github.com/golang/go/src go1.16 sort/.Sortscip-go gomod github.com/golang/go/src go1.16 sort/Sort().(e) |
| 104 | } |
| 105 | |
| 106 | // Tree implements a radix tree. This can be treated as a |
| 107 | // Dictionary abstract data type. The main advantage over |
| 108 | // a standard hash map is prefix-based lookups and |
| 109 | // ordered iteration, |
| 110 | type Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree# struct { |
| 111 | rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# |
| 112 | sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size. int |
| 113 | } |
| 114 | |
| 115 | // New returns an empty Tree |
| 116 | func Newscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/New().() *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree# { |
| 117 | return NewFromMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/NewFromMap().(nil) |
| 118 | } |
| 119 | |
| 120 | // NewFromMap returns a new tree containing the keys |
| 121 | // from an existing map |
| 122 | func NewFromMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/NewFromMap().(m map[string]interface{}) *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree# { |
| 123 | t := &Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#{rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root.: &nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#{}} |
| 124 | for k, v := range m { |
| 125 | t.Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(k, v) |
| 126 | } |
| 127 | return t |
| 128 | } |
| 129 | |
| 130 | // Len is used to return the number of elements in the tree |
| 131 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Lenscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Len().() int { |
| 132 | return t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size. |
| 133 | } |
| 134 | |
| 135 | // longestPrefix finds the length of the shared prefix |
| 136 | // of two strings |
| 137 | func longestPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/longestPrefix().(k1, k2 string) int { |
| 138 | max := len(k1) |
| 139 | if l := len(k2); l < max { |
| 140 | max = l |
| 141 | } |
| 142 | var i int |
| 143 | for i = 0; i < max; i++ { |
| 144 | if k1[i] != k2[i] { |
| 145 | break |
| 146 | } |
| 147 | } |
| 148 | return i |
| 149 | } |
| 150 | |
| 151 | // Insert is used to add a newentry or update |
| 152 | // an existing entry. Returns true if an existing record is updated. |
| 153 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Insertscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Insert().(s string, v interface{}) (interface{}, bool) { |
| 154 | var parent *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# |
| 155 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 156 | search := s |
| 157 | for { |
| 158 | // Handle key exhaution |
| 159 | if len(search) == 0 { |
| 160 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 161 | old := n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val. |
| 162 | n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val. = v |
| 163 | return old, true |
| 164 | } |
| 165 | |
| 166 | n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. = &leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#{ |
| 167 | keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key.: s, |
| 168 | valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val.: v, |
| 169 | } |
| 170 | t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size.++ |
| 171 | return nil, false |
| 172 | } |
| 173 | |
| 174 | // Look for the edge |
| 175 | parent = n |
| 176 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(search[0]) |
| 177 | |
| 178 | // No edge, create one |
| 179 | if n == nil { |
| 180 | e := edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#{ |
| 181 | labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label.: search[0], |
| 182 | nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node.: &nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#{ |
| 183 | leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf.: &leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#{ |
| 184 | keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key.: s, |
| 185 | valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val.: v, |
| 186 | }, |
| 187 | prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.: search, |
| 188 | }, |
| 189 | } |
| 190 | parent.addEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#addEdge().(e) |
| 191 | t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size.++ |
| 192 | return nil, false |
| 193 | } |
| 194 | |
| 195 | // Determine longest prefix of the search key on match |
| 196 | commonPrefix := longestPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/longestPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) |
| 197 | if commonPrefix == len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 198 | search = search[commonPrefix:] |
| 199 | continue |
| 200 | } |
| 201 | |
| 202 | // Split the node |
| 203 | t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size.++ |
| 204 | child := &nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#{ |
| 205 | prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.: search[:commonPrefix], |
| 206 | } |
| 207 | parent.updateEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#updateEdge().(search[0], child) |
| 208 | |
| 209 | // Restore the existing node |
| 210 | child.addEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#addEdge().(edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#{ |
| 211 | labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label.: n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.[commonPrefix], |
| 212 | nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node.: n, |
| 213 | }) |
| 214 | n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix. = n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.[commonPrefix:] |
| 215 | |
| 216 | // Create a new leaf node |
| 217 | leaf := &leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#{ |
| 218 | keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key.: s, |
| 219 | valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val.: v, |
| 220 | } |
| 221 | |
| 222 | // If the new key is a subset, add to this node |
| 223 | search = search[commonPrefix:] |
| 224 | if len(search) == 0 { |
| 225 | child.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. = leaf |
| 226 | return nil, false |
| 227 | } |
| 228 | |
| 229 | // Create a new edge for the node |
| 230 | child.addEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#addEdge().(edgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#{ |
| 231 | labelscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#label.: search[0], |
| 232 | nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node.: &nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#{ |
| 233 | leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf.: leaf, |
| 234 | prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.: search, |
| 235 | }, |
| 236 | }) |
| 237 | return nil, false |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // Delete is used to delete a key, returning the previous |
| 242 | // value and if it was deleted |
| 243 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Deletescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Delete().(s string) (interface{}, bool) { |
| 244 | var parent *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node# |
| 245 | var label byte |
| 246 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 247 | search := s |
| 248 | for { |
| 249 | // Check for key exhaution |
| 250 | if len(search) == 0 { |
| 251 | if !n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 252 | break |
| 253 | } |
| 254 | goto DELETE |
| 255 | } |
| 256 | |
| 257 | // Look for an edge |
| 258 | parent = n |
| 259 | label = search[0] |
| 260 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(label) |
| 261 | if n == nil { |
| 262 | break |
| 263 | } |
| 264 | |
| 265 | // Consume the search prefix |
| 266 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 267 | search = search[len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 268 | } else { |
| 269 | break |
| 270 | } |
| 271 | } |
| 272 | return nil, false |
| 273 | |
| 274 | DELETE: |
| 275 | // Delete the leaf |
| 276 | leaf := n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. |
| 277 | n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. = nil |
| 278 | t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size.-- |
| 279 | |
| 280 | // Check if we should delete this node from the parent |
| 281 | if parent != nil && len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) == 0 { |
| 282 | parent.delEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#delEdge().(label) |
| 283 | } |
| 284 | |
| 285 | // Check if we should merge this node |
| 286 | if n != t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. && len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) == 1 { |
| 287 | n.mergeChildscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#mergeChild().() |
| 288 | } |
| 289 | |
| 290 | // Check if we should merge the parent's other child |
| 291 | if parent != nil && parent != t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. && len(parent.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) == 1 && !parent.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 292 | parent.mergeChildscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#mergeChild().() |
| 293 | } |
| 294 | |
| 295 | return leaf.valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val., true |
| 296 | } |
| 297 | |
| 298 | // DeletePrefix is used to delete the subtree under a prefix |
| 299 | // Returns how many nodes were deleted |
| 300 | // Use this to delete large subtrees efficiently |
| 301 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) DeletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#DeletePrefix().(s string) int { |
| 302 | return t.deletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#deletePrefix().(nil, t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root., s) |
| 303 | } |
| 304 | |
| 305 | // delete does a recursive deletion |
| 306 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) deletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#deletePrefix().(parent, n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#, prefix string) int { |
| 307 | // Check for key exhaustion |
| 308 | if len(prefix) == 0 { |
| 309 | // Remove the leaf node |
| 310 | subTreeSize := 0 |
| 311 | //recursively walk from all edges of the node to be deleted |
| 312 | recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(n, func(s string, v interface{}) bool { |
| 313 | subTreeSize++ |
| 314 | return false |
| 315 | }) |
| 316 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 317 | n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. = nil |
| 318 | } |
| 319 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. = nil // deletes the entire subtree |
| 320 | |
| 321 | // Check if we should merge the parent's other child |
| 322 | if parent != nil && parent != t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. && len(parent.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) == 1 && !parent.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 323 | parent.mergeChildscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#mergeChild().() |
| 324 | } |
| 325 | t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size. -= subTreeSize |
| 326 | return subTreeSize |
| 327 | } |
| 328 | |
| 329 | // Look for an edge |
| 330 | label := prefix[0] |
| 331 | child := n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(label) |
| 332 | if child == nil || (!stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(child.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix., prefix) && !stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(prefix, child.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.)) { |
| 333 | return 0 |
| 334 | } |
| 335 | |
| 336 | // Consume the search prefix |
| 337 | if len(child.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) > len(prefix) { |
| 338 | prefix = prefix[len(prefix):] |
| 339 | } else { |
| 340 | prefix = prefix[len(child.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 341 | } |
| 342 | return t.deletePrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#deletePrefix().(n, child, prefix) |
| 343 | } |
| 344 | |
| 345 | func (n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#) mergeChildscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#mergeChild().() { |
| 346 | e := n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[0] |
| 347 | child := e.nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. |
| 348 | n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix. = n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix. + child.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix. |
| 349 | n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. = child.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. |
| 350 | n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. = child.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges. |
| 351 | } |
| 352 | |
| 353 | // Get is used to lookup a specific key, returning |
| 354 | // the value and if it was found |
| 355 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Getscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Get().(s string) (interface{}, bool) { |
| 356 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 357 | search := s |
| 358 | for { |
| 359 | // Check for key exhaution |
| 360 | if len(search) == 0 { |
| 361 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 362 | return n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val., true |
| 363 | } |
| 364 | break |
| 365 | } |
| 366 | |
| 367 | // Look for an edge |
| 368 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(search[0]) |
| 369 | if n == nil { |
| 370 | break |
| 371 | } |
| 372 | |
| 373 | // Consume the search prefix |
| 374 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 375 | search = search[len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 376 | } else { |
| 377 | break |
| 378 | } |
| 379 | } |
| 380 | return nil, false |
| 381 | } |
| 382 | |
| 383 | // LongestPrefix is like Get, but instead of an |
| 384 | // exact match, it will return the longest prefix match. |
| 385 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) LongestPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#LongestPrefix().(s string) (string, interface{}, bool) { |
| 386 | var last *leafNodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode# |
| 387 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 388 | search := s |
| 389 | for { |
| 390 | // Look for a leaf node |
| 391 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 392 | last = n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. |
| 393 | } |
| 394 | |
| 395 | // Check for key exhaution |
| 396 | if len(search) == 0 { |
| 397 | break |
| 398 | } |
| 399 | |
| 400 | // Look for an edge |
| 401 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(search[0]) |
| 402 | if n == nil { |
| 403 | break |
| 404 | } |
| 405 | |
| 406 | // Consume the search prefix |
| 407 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 408 | search = search[len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 409 | } else { |
| 410 | break |
| 411 | } |
| 412 | } |
| 413 | if last != nil { |
| 414 | return last.keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key., last.valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val., true |
| 415 | } |
| 416 | return "", nil, false |
| 417 | } |
| 418 | |
| 419 | // Minimum is used to return the minimum value in the tree |
| 420 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Minimumscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Minimum().() (string, interface{}, bool) { |
| 421 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 422 | for { |
| 423 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 424 | return n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key., n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val., true |
| 425 | } |
| 426 | if len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) > 0 { |
| 427 | n = n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[0].nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. |
| 428 | } else { |
| 429 | break |
| 430 | } |
| 431 | } |
| 432 | return "", nil, false |
| 433 | } |
| 434 | |
| 435 | // Maximum is used to return the maximum value in the tree |
| 436 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Maximumscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Maximum().() (string, interface{}, bool) { |
| 437 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 438 | for { |
| 439 | if num := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.); num > 0 { |
| 440 | n = n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[num-1].nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node. |
| 441 | continue |
| 442 | } |
| 443 | if n.isLeafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#isLeaf().() { |
| 444 | return n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key., n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val., true |
| 445 | } |
| 446 | break |
| 447 | } |
| 448 | return "", nil, false |
| 449 | } |
| 450 | |
| 451 | // Walk is used to walk the tree |
| 452 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) Walkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Walk().(fn WalkFnscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/WalkFn#) { |
| 453 | recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root., fn) |
| 454 | } |
| 455 | |
| 456 | // WalkPrefix is used to walk the tree under a prefix |
| 457 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) WalkPrefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#WalkPrefix().(prefix string, fn WalkFnscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/WalkFn#) { |
| 458 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 459 | search := prefix |
| 460 | for { |
| 461 | // Check for key exhaustion |
| 462 | if len(search) == 0 { |
| 463 | recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(n, fn) |
| 464 | return |
| 465 | } |
| 466 | |
| 467 | // Look for an edge |
| 468 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(search[0]) |
| 469 | if n == nil { |
| 470 | return |
| 471 | } |
| 472 | |
| 473 | // Consume the search prefix |
| 474 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 475 | search = search[len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 476 | continue |
| 477 | } |
| 478 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix., search) { |
| 479 | // Child may be under our search prefix |
| 480 | recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(n, fn) |
| 481 | } |
| 482 | return |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | // WalkPath is used to walk the tree, but only visiting nodes |
| 487 | // from the root down to a given leaf. Where WalkPrefix walks |
| 488 | // all the entries *under* the given prefix, this walks the |
| 489 | // entries *above* the given prefix. |
| 490 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) WalkPathscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#WalkPath().(path string, fn WalkFnscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/WalkFn#) { |
| 491 | n := t.rootscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#root. |
| 492 | search := path |
| 493 | for { |
| 494 | // Visit the leaf values if any |
| 495 | if n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. != nil && fn(n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key., n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val.) { |
| 496 | return |
| 497 | } |
| 498 | |
| 499 | // Check for key exhaution |
| 500 | if len(search) == 0 { |
| 501 | return |
| 502 | } |
| 503 | |
| 504 | // Look for an edge |
| 505 | n = n.getEdgescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#getEdge().(search[0]) |
| 506 | if n == nil { |
| 507 | return |
| 508 | } |
| 509 | |
| 510 | // Consume the search prefix |
| 511 | if stringsscip-go gomod github.com/golang/go/src go1.16 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.16 strings/HasPrefix().(search, n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.) { |
| 512 | search = search[len(n.prefixscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#prefix.):] |
| 513 | } else { |
| 514 | break |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | // recursiveWalk is used to do a pre-order walk of a node |
| 520 | // recursively. Returns true if the walk should be aborted |
| 521 | func recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(n *nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#, fn WalkFnscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/WalkFn#) bool { |
| 522 | // Visit the leaf values if any |
| 523 | if n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf. != nil && fn(n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..keyscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#key., n.leafscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#leaf..valscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/leafNode#val.) { |
| 524 | return true |
| 525 | } |
| 526 | |
| 527 | // Recurse on the children |
| 528 | i := 0 |
| 529 | k := len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) // keeps track of number of edges in previous iteration |
| 530 | for i < k { |
| 531 | e := n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.[i] |
| 532 | if recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(e.nodescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/edge#node., fn) { |
| 533 | return true |
| 534 | } |
| 535 | // It is a possibility that the WalkFn modified the node we are |
| 536 | // iterating on. If there are no more edges, mergeChild happened, |
| 537 | // so the last edge became the current node n, on which we'll |
| 538 | // iterate one last time. |
| 539 | if len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) == 0 { |
| 540 | return recursiveWalkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/recursiveWalk().(n, fn) |
| 541 | } |
| 542 | // If there are now less edges than in the previous iteration, |
| 543 | // then do not increment the loop index, since the current index |
| 544 | // points to a new edge. Otherwise, get to the next index. |
| 545 | if len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) >= k { |
| 546 | i++ |
| 547 | } |
| 548 | k = len(n.edgesscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/node#edges.) |
| 549 | } |
| 550 | return false |
| 551 | } |
| 552 | |
| 553 | // ToMap is used to walk the tree and convert it into a map |
| 554 | func (t *Treescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#) ToMapscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#ToMap().() map[string]interface{} { |
| 555 | out := make(map[string]interface{}, t.sizescip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#size.) |
| 556 | t.Walkscip-go gomod github.com/armon/go-radix . `github.com/armon/go-radix`/Tree#Walk().(func(k string, v interface{}) bool { |
| 557 | out[k] = v |
| 558 | return false |
| 559 | }) |
| 560 | return out |
| 561 | } |