Skip to content

Commit 526d48a

Browse files
authored
fix(radix): disable export of size and root (#5)
1 parent 69e8d07 commit 526d48a

5 files changed

Lines changed: 35 additions & 30 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import qradix "github.com/ihexxa/q-radix"
2828

2929
rTree := qradix.NewRTree() // create a new radix tree
3030
ok := rTree.Insert("key", value) // insert value in any type with a string key
31-
treeSize := rTree.Size // get the size of radix tree
31+
treeSize := rTree.Size() // get the size of the radix tree
3232

3333
val, ok := rTree.Get("key") // get the value by key
3434
val, ok := rTree.GetAllMatches("key") // get all prefix matches of the key

radix.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ func newNode(prefix string, children *node, next *node, leaf *leafNode) *node {
2525

2626
// RTree is a radix tree
2727
type RTree struct {
28-
Root *node
29-
Size int
28+
root *node
29+
size int
3030
}
3131

3232
func 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
4343
func 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
4954
func (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) {
8085
func (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.
134139
func (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) {
185190
func (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
216221
func (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

radix_random_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func isEqual(tree *RTree, dict map[string]string) bool {
133133
}
134134

135135
// check if all keys in rtree are also in map
136-
return preOrderAndCompare(tree.Root, dict)
136+
return preOrderAndCompare(tree.root, dict)
137137
}
138138

139139
func preOrderAndCompare(n *node, M map[string]string) bool {

radix_unit_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func testInsert(t *testing.T) {
2424
t.Errorf("the insert key [%s] is not found", testCase[1])
2525
}
2626
}
27-
if rTree.Size != len(cases) {
27+
if rTree.Size() != len(cases) {
2828
t.Error("the size of radix tree is not correct")
2929
}
3030
}
@@ -61,7 +61,7 @@ func testRemove(t *testing.T) {
6161
t.Errorf("insert key [%s] is not removed", testCase[1])
6262
}
6363
}
64-
if rTree.Size != len(values)-len(cases) {
64+
if rTree.Size() != len(values)-len(cases) {
6565
t.Error("the size of radix tree is not correct")
6666
}
6767
}

utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
// BFS is breadth first traverse on the radix tree
1515
func BFS(T *RTree, function func(*node)) {
1616
Q := make([]*node, 0)
17-
Q = append(Q, T.Root)
17+
Q = append(Q, T.root)
1818

1919
for len(Q) > 0 {
2020
n := Q[0]

0 commit comments

Comments
 (0)