This repository has been archived by the owner on Mar 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tree.go
172 lines (135 loc) · 2.68 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package patricia
import "sync"
// Tree is a PATRICIA Tree.
type Tree struct {
// Safe tells whether the tree's operations
// should be thread-safe. By default, the tree's
// not thread-safe.
Safe bool
// name is the tree's name.
name string
// root is the tree's root.
root *Node
// size is the total number of nodes
// without the tree's root.
size uint
// mtx controls the operations' safety.
mtx *sync.RWMutex
}
// New creates a named PATRICIA tree with a single node (its root).
func New(name string) *Tree {
return &Tree{name: name, root: &Node{}, mtx: &sync.RWMutex{}}
}
// Add adds a new node to the tree.
func (t *Tree) Add(s string, v interface{}) {
if t.Safe {
defer t.mtx.Unlock()
t.mtx.Lock()
}
if v == nil {
return
}
tnode := t.root
walk:
for i := 0; i < len(s); i++ {
for j := uint8(8); j > 0; j-- {
exp := byte(1 << (j - 1))
mask := s[i] & exp
bit := uint8(0)
if mask > 0 {
bit = 1
}
if tnode.edges[bit] == nil {
if i == len(s)-1 && j-1 == 0 {
tnode.edges[bit] = newEdge(tnode.child(v))
t.size++
break walk
}
tnode.edges[bit] = newEdge(tnode.child(nil))
tnode = tnode.edges[bit].node
t.size++
continue
}
if i == len(s)-1 && j == 0 {
tnode.edges[bit].node.Value = v
break walk
}
tnode = tnode.edges[bit].node
}
}
}
// Del deletes a node.
func (t *Tree) Del(s string) {
if t.Safe {
defer t.mtx.Unlock()
t.mtx.Lock()
}
tnode := t.root
if tnode.IsLeaf() {
return
}
leaf := tnode
count := uint(0)
for i := 0; i < len(s); i++ {
for j := uint8(8); j > 0; j-- {
exp := byte(1 << (j - 1))
mask := s[i] & exp
bit := uint8(0)
if mask > 0 {
bit = 1
}
if tnode.edges[bit] == nil {
return
}
tnode = tnode.edges[bit].node
count++
if i == len(s)-1 && j-1 == 0 && tnode.IsLeaf() {
leaf.edges = [uint8(2)]*edge{}
t.size -= count
return
}
if tnode != nil && tnode.Value != nil {
leaf = tnode
count = 0
}
}
}
}
// Get retrieves a node.
func (t *Tree) Get(s string) *Node {
if t.Safe {
defer t.mtx.RUnlock()
t.mtx.RLock()
}
tnode := t.root
if tnode.IsLeaf() {
return nil
}
for i := 0; i < len(s); i++ {
for j := uint8(8); j > 0; j-- {
exp := byte(1 << (j - 1))
mask := s[i] & exp
bit := uint8(0)
if mask > 0 {
bit = 1
}
if tnode.edges[bit] == nil {
return nil
}
if i == len(s)-1 && j-1 == 0 {
return tnode.edges[bit].node
}
tnode = tnode.edges[bit].node
}
}
return nil
}
// Size returns the total numbers of nodes the tree has,
// including the root.
func (t *Tree) Size() uint {
if t.Safe {
defer t.mtx.RUnlock()
t.mtx.RLock()
}
return t.size + 1
}