@@ -25,8 +25,8 @@ func newNode(prefix string, children *node, next *node, leaf *leafNode) *node {
2525
2626// RTree is a radix tree
2727type RTree struct {
28- Root * node
29- Size int
28+ root * node
29+ size int
3030}
3131
3232func commonPrefixOffset (s1 string , s2 string ) int {
@@ -41,20 +41,25 @@ func commonPrefixOffset(s1 string, s2 string) int {
4141
4242// NewRTree returns a new radix tree
4343func NewRTree () * RTree {
44- return & RTree {Root : & node {}}
44+ return & RTree {root : & node {}}
45+ }
46+
47+ // Size returns the size of the tree
48+ func (T * RTree ) Size () int {
49+ return T .size
4550}
4651
4752// Get returns a value according to key
4853// if no match is found, it returns (nil, false) instead
4954func (T * RTree ) Get (key string ) (interface {}, bool ) {
5055 if len (key ) == 0 {
51- if T .Root .Leaf != nil {
52- return T .Root .Leaf .Val , true
56+ if T .root .Leaf != nil {
57+ return T .root .Leaf .Val , true
5358 }
5459 return nil , false
5560 }
5661
57- n := T .Root
62+ n := T .root
5863 for {
5964 if n == nil {
6065 return nil , false
@@ -80,14 +85,14 @@ func (T *RTree) Get(key string) (interface{}, bool) {
8085func (T * RTree ) GetAllMatches (key string ) []interface {} {
8186 results := []interface {}{}
8287 if len (key ) == 0 {
83- if T .Root .Leaf != nil {
84- results = append (results , T .Root .Leaf .Val )
88+ if T .root .Leaf != nil {
89+ results = append (results , T .root .Leaf .Val )
8590 return results
8691 }
8792 return results
8893 }
8994
90- n := T .Root
95+ n := T .root
9196 for {
9297 if n == nil {
9398 return results
@@ -132,14 +137,14 @@ func split(n *node, offset int) (*node, bool) {
132137// Insert adds a value in the tree. The value can be found by the key.
133138// if path already exists, it updates the value and returns former value.
134139func (T * RTree ) Insert (key string , val interface {}) (interface {}, bool ) {
135- if T .Root == nil {
140+ if T .root == nil {
136141 return nil , false
137142 }
138143 if len (key ) == 0 {
139- return T .updateLeafVal (T .Root , "" , val )
144+ return T .updateLeafVal (T .root , "" , val )
140145 }
141146
142- n := T .Root
147+ n := T .root
143148 pathSuffix := key
144149 for {
145150 offset := commonPrefixOffset (n .Prefix , pathSuffix )
@@ -149,7 +154,7 @@ func (T *RTree) Insert(key string, val interface{}) (interface{}, bool) {
149154 continue
150155 }
151156 n .Next = newNode (pathSuffix , nil , nil , & leafNode {Key : key , Val : val })
152- T .Size ++
157+ T .size ++
153158 return nil , true
154159 } else if offset < len (n .Prefix )- 1 {
155160 childNode , ok := split (n , offset + 1 )
@@ -158,11 +163,11 @@ func (T *RTree) Insert(key string, val interface{}) (interface{}, bool) {
158163 }
159164 if offset < len (pathSuffix )- 1 {
160165 childNode .Next = newNode (pathSuffix [offset + 1 :], nil , nil , & leafNode {Key : key , Val : val })
161- T .Size ++
166+ T .size ++
162167 return nil , true
163168 }
164169 n .Leaf = & leafNode {Key : key , Val : val }
165- T .Size ++
170+ T .size ++
166171 return nil , true
167172 }
168173 if offset < len (pathSuffix )- 1 {
@@ -172,7 +177,7 @@ func (T *RTree) Insert(key string, val interface{}) (interface{}, bool) {
172177 continue
173178 }
174179 n .Children = newNode (pathSuffix [offset + 1 :], nil , nil , & leafNode {Key : key , Val : val })
175- T .Size ++
180+ T .size ++
176181 return nil , true
177182 }
178183 return T .updateLeafVal (n , key , val )
@@ -185,7 +190,7 @@ func (T *RTree) Insert(key string, val interface{}) (interface{}, bool) {
185190func (T * RTree ) updateLeafVal (n * node , key string , newVal interface {}) (interface {}, bool ) {
186191 if n .Leaf == nil {
187192 n .Leaf = & leafNode {Key : key , Val : newVal }
188- T .Size ++
193+ T .size ++
189194 return nil , true
190195 }
191196
@@ -215,15 +220,15 @@ func merge(parent *node, child *node) bool {
215220// if the leaf node doesn't exist, it will return false
216221func (T * RTree ) Remove (path string ) bool {
217222 if len (path ) == 0 {
218- if T .Root .Leaf != nil {
219- T .Root .Leaf = nil
223+ if T .root .Leaf != nil {
224+ T .root .Leaf = nil
220225 return true
221226 }
222227 return false
223228 }
224229
225- parent := T .Root
226- child := T .Root
230+ parent := T .root
231+ child := T .root
227232 for {
228233 if child == nil {
229234 return false
@@ -252,8 +257,8 @@ func (T *RTree) removeChild(parent *node, child *node) bool {
252257 }
253258
254259 child .Leaf = nil
255- if T .Size > 0 {
256- T .Size --
260+ if T .size > 0 {
261+ T .size --
257262 }
258263 if child .Children != nil {
259264 if child .Next == nil {
@@ -268,8 +273,8 @@ func (T *RTree) removeChild(parent *node, child *node) bool {
268273 }
269274 // child is not the first child, search for the previous node of child
270275 previousChild := parent .Children
271- if parent == T .Root {
272- previousChild = T .Root
276+ if parent == T .root {
277+ previousChild = T .root
273278 }
274279 for previousChild != nil && previousChild .Next != child {
275280 previousChild = previousChild .Next
0 commit comments