forked from calibrae-project/spawn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutable.go
236 lines (223 loc) · 7.41 KB
/
mutable.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package treap
import (
"bytes"
"math/rand"
)
// Mutable represents a treap data structure which is used to hold ordered key/value pairs using a combination of binary
// search tree and heap semantics. It is a self-organizing and randomized data structure that doesn't require complex
// operations to maintain balance. Search, insert, and delete operations are all O(log n).
type Mutable struct {
root *treapNode
count int
// totalSize is the best estimate of the total size of of all data in the treap including the keys, values, and node txsizes.
totalSize uint64
}
// Len returns the number of items stored in the treap.
func (t *Mutable) Len() int {
return t.count
}
// Size returns a best estimate of the total number of bytes the treap is consuming including all of the fields used to
// represent the nodes as well as the size of the keys and values. Shared values are not detected, so the returned size
// assumes each value is pointing to different memory.
func (t *Mutable) Size() uint64 {
return t.totalSize
}
// get returns the treap node that contains the passed key and its parent. When the found node is the root of the tree,
// the parent will be nil. When the key does not exist, both the node and the parent will be nil.
func (t *Mutable) get(key []byte) (*treapNode, *treapNode) {
var parent *treapNode
for node := t.root; node != nil; {
// Traverse left or right depending on the result of the comparison.
compareResult := bytes.Compare(key, node.key)
if compareResult < 0 {
parent = node
node = node.left
continue
}
if compareResult > 0 {
parent = node
node = node.right
continue
}
// The key exists.
return node, parent
}
// A nil node was reached which means the key does not exist.
return nil, nil
}
// Has returns whether or not the passed key exists.
func (t *Mutable) Has(key []byte) bool {
if node, _ := t.get(key); node != nil {
return true
}
return false
}
// Get returns the value for the passed key. The function will return nil when the key does not exist.
func (t *Mutable) Get(key []byte) []byte {
if node, _ := t.get(key); node != nil {
return node.value
}
return nil
}
// relinkGrandparent relinks the node into the treap after it has been rotated by changing the passed grandparent's left
// or right pointer, depending on where the old parent was, to point at the passed node. Otherwise, when there is no
// grandparent, it means the node is now the root of the tree, so update it accordingly.
func (t *Mutable) relinkGrandparent(node, parent, grandparent *treapNode) {
// The node is now the root of the tree when there is no grandparent.
if grandparent == nil {
t.root = node
return
}
// Relink the grandparent's left or right pointer based on which side the old parent was.
if grandparent.left == parent {
grandparent.left = node
} else {
grandparent.right = node
}
}
// Put inserts the passed key/value pair.
func (t *Mutable) Put(key, value []byte) {
// Use an empty byte slice for the value when none was provided. This ultimately allows key existence to be
// determined from the value since an empty byte slice is distinguishable from nil.
if value == nil {
value = emptySlice
}
// The node is the root of the tree if there isn't already one.
if t.root == nil {
node := newTreapNode(key, value, rand.Int())
t.count = 1
t.totalSize = nodeSize(node)
t.root = node
return
}
// Find the binary tree insertion point and construct a list of parents while doing so. When the key matches an
// entry already in the treap, just update its value and return.
var parents parentStack
var compareResult int
for node := t.root; node != nil; {
parents.Push(node)
compareResult = bytes.Compare(key, node.key)
if compareResult < 0 {
node = node.left
continue
}
if compareResult > 0 {
node = node.right
continue
}
// The key already exists, so update its value.
t.totalSize -= uint64(len(node.value))
t.totalSize += uint64(len(value))
node.value = value
return
}
// Link the new node into the binary tree in the correct position.
node := newTreapNode(key, value, rand.Int())
t.count++
t.totalSize += nodeSize(node)
parent := parents.At(0)
if compareResult < 0 {
parent.left = node
} else {
parent.right = node
}
// Perform any rotations needed to maintain the min-heap.
for parents.Len() > 0 {
// There is nothing left to do when the node's priority is greater than or equal to its parent's priority.
parent = parents.Pop()
if node.priority >= parent.priority {
break
}
// Perform a right rotation if the node is on the left side or a left rotation if the node is on the right side.
if parent.left == node {
node.right, parent.left = parent, node.right
} else {
node.left, parent.right = parent, node.left
}
t.relinkGrandparent(node, parent, parents.At(0))
}
}
// Delete removes the passed key if it exists.
func (t *Mutable) Delete(key []byte) {
// Find the node for the key along with its parent. There is nothing to do if the key does not exist.
node, parent := t.get(key)
if node == nil {
return
}
// When the only node in the tree is the root node and it is the one being deleted, there is nothing else to do besides removing it.
if parent == nil && node.left == nil && node.right == nil {
t.root = nil
t.count = 0
t.totalSize = 0
return
}
// Perform rotations to move the node to delete to a leaf position while maintaining the min-heap.
var isLeft bool
var child *treapNode
for node.left != nil || node.right != nil {
// Choose the child with the higher priority.
if node.left == nil {
child = node.right
isLeft = false
} else if node.right == nil {
child = node.left
isLeft = true
} else if node.left.priority >= node.right.priority {
child = node.left
isLeft = true
} else {
child = node.right
isLeft = false
}
// Rotate left or right depending on which side the child node is on. This has the effect of moving the node to
// delete towards the bottom of the tree while maintaining the min-heap.
if isLeft {
child.right, node.left = node, child.right
} else {
child.left, node.right = node, child.left
}
t.relinkGrandparent(child, node, parent)
// The parent for the node to delete is now what was previously its child.
parent = child
}
// Delete the node, which is now a leaf node, by disconnecting it from its parent.
if parent != nil {
if parent.right == node {
parent.right = nil
} else {
parent.left = nil
}
t.count--
t.totalSize -= nodeSize(node)
}
}
// ForEach invokes the passed function with every key/value pair in the treap in ascending order.
func (t *Mutable) ForEach(fn func(k, v []byte) bool) {
// Add the root node and all children to the left of it to the list of nodes to traverse and loop until they, and
// all of their child nodes, been traversed.
var parents parentStack
for node := t.root; node != nil; node = node.left {
parents.Push(node)
}
for parents.Len() > 0 {
node := parents.Pop()
if !fn(node.key, node.value) {
return
}
// Extend the nodes to traverse by all children to the left of the current node's right child.
for node = node.right; node != nil; node = node.left {
parents.Push(node)
}
}
}
// Reset efficiently removes all items in the treap.
func (t *Mutable) Reset() {
t.count = 0
t.totalSize = 0
t.root = nil
}
// NewMutable returns a new empty mutable treap ready for use. See the documentation for the Mutable structure for more
// details.
func NewMutable() *Mutable {
return &Mutable{}
}