-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnode.go
321 lines (293 loc) · 7.8 KB
/
node.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package immutableMap
import (
"fmt"
"math/bits"
)
type keyValueList struct {
next *keyValueList
key Object
value Object
}
type node struct {
keys *keyValueList
bitmask uint32
children []*node
}
type iteratorState struct {
next *iteratorState
currentNode *node
currentIndex int
currentKey *keyValueList
}
func (this *node) isEmpty() bool {
return this.keys == nil && this.bitmask == 0
}
func emptyNode() *node {
return &node{}
}
func (this *node) assign(hashCode HashCode, key Object, value Object, equals EqualsFunc) (*node, int) {
if hashCode == 0 {
return this.setKeyAndValue(key, value, equals)
} else {
index := indexForHash(hashCode)
oldChild := this.getChild(index)
if oldChild == nil {
oldChild = emptyNode()
}
newChild, delta := oldChild.assign(hashCode>>5, key, value, equals)
if newChild == oldChild {
return this, delta
} else {
return this.setChild(index, newChild), delta
}
}
}
func (this *node) get(hashCode HashCode, key Object, equals EqualsFunc) Object {
if hashCode == 0 {
return this.getValueForKey(key, equals)
} else {
index := indexForHash(hashCode)
oldChild := this.getChild(index)
if oldChild == nil {
return nil
} else {
return oldChild.get(hashCode>>5, key, equals)
}
}
}
func (this *node) contains(hashCode HashCode, key Object, equals EqualsFunc) bool {
if hashCode == 0 {
return this.containsValueForKey(key, equals)
} else {
index := indexForHash(hashCode)
child := this.getChild(index)
return child != nil && child.contains(hashCode>>5, key, equals)
}
}
func (this *node) delete(hashCode HashCode, key Object, equals EqualsFunc) (*node, int) {
if hashCode == 0 {
return this.deleteKey(key, equals)
} else {
index := indexForHash(hashCode)
oldChild := this.getChild(index)
if oldChild == nil {
return this, 0
} else {
newChild, delta := oldChild.delete(hashCode>>5, key, equals)
if newChild == oldChild {
return this, 0
} else if newChild == nil {
return this.deleteChild(index), delta
} else {
return this.setChild(index, newChild), delta
}
}
}
}
func indexForHash(hashCode HashCode) int {
return int(hashCode & 0x0f)
}
func (this *node) containsValueForKey(key Object, equals EqualsFunc) bool {
for kvp := this.keys; kvp != nil; kvp = kvp.next {
if equals(key, kvp.key) {
return true
}
}
return false
}
func (this *node) getValueForKey(key Object, equals EqualsFunc) Object {
for kvp := this.keys; kvp != nil; kvp = kvp.next {
if equals(key, kvp.key) {
return kvp.value
}
}
return nil
}
func (this *node) setKeyAndValue(key Object, value Object, equals EqualsFunc) (*node, int) {
var newKeys *keyValueList
delta := 0
if this.keys == nil {
newKeys = &keyValueList{key: key, value: value}
delta = 1
} else {
changed := false
for kvp := this.keys; kvp != nil; kvp = kvp.next {
if equals(kvp.key, key) {
if kvp.value == value {
return this, 0
}
newKeys = &keyValueList{key: key, value: value, next: newKeys}
changed = true
} else {
newKeys = &keyValueList{key: kvp.key, value: kvp.value, next: newKeys}
}
}
if !changed {
newKeys = &keyValueList{key: key, value: value, next: this.keys}
delta = 1
}
}
newNode := *this
newNode.keys = newKeys
return &newNode, delta
}
func (this *node) deleteKey(key Object, equals EqualsFunc) (*node, int) {
if this.keys == nil {
return this, 0
}
changed := false
var newKeys *keyValueList
for kvp := this.keys; kvp != nil; kvp = kvp.next {
if equals(kvp.key, key) {
changed = true
} else {
newKeys = &keyValueList{key: kvp.key, value: kvp.value, next: newKeys}
}
}
if !changed {
return this, 0
} else if newKeys == nil && this.childCount() == 0 {
return nil, -1
} else {
newNode := *this
newNode.keys = newKeys
return &newNode, -1
}
}
func (this *node) childCount() int {
return bits.OnesCount32(this.bitmask)
}
func (this *node) getChild(index int) *node {
indexBit := indexBit(index)
if this.bitmask&indexBit == 0 {
return nil
} else {
realIndex := this.realIndex(indexBit)
return this.children[realIndex]
}
}
func (this *node) realIndex(indexBit uint32) int {
trailingBits := indexBit - 1
realIndex := bits.OnesCount32(this.bitmask & trailingBits)
return realIndex
}
func indexBit(index int) uint32 {
var indexBit uint32 = 1 << uint32(index)
return indexBit
}
func (this *node) setChild(index int, child *node) *node {
newNode := *this
indexBit := indexBit(index)
if this.children == nil {
newNode.children = make([]*node, 1)
newNode.children[0] = child
newNode.bitmask = indexBit
} else {
realIndex := this.realIndex(indexBit)
if this.bitmask&indexBit != 0 {
newNode.children = make([]*node, len(this.children))
copy(newNode.children, this.children)
newNode.children[realIndex] = child
} else {
newNode.children = make([]*node, len(this.children)+1)
copy(newNode.children, this.children[0:realIndex])
newNode.children[realIndex] = child
copy(newNode.children[realIndex+1:], this.children[realIndex:])
newNode.bitmask |= indexBit
}
}
return &newNode
}
func (this *node) deleteChild(index int) *node {
newNode := *this
if this.childCount() == 1 {
if this.keys == nil {
return nil
} else {
newNode.children = nil
newNode.bitmask = 0
}
} else {
indexBit := indexBit(index)
realIndex := this.realIndex(indexBit)
newNode.children = make([]*node, len(this.children)-1)
copy(newNode.children, this.children[0:realIndex])
copy(newNode.children[realIndex:], this.children[realIndex+1:])
newNode.bitmask &= ^indexBit
}
return &newNode
}
func (this *node) forEach(v MapVisitor) {
for kvp := this.keys; kvp != nil; kvp = kvp.next {
v(kvp.key, kvp.value)
}
if this.children != nil {
for _, child := range this.children {
child.forEach(v)
}
}
}
func (this *node) createIteratorState(nextState *iteratorState) *iteratorState {
if this.isEmpty() {
return nextState
} else {
var startingIndex int
if this.keys == nil {
startingIndex = 0
} else {
startingIndex = -1
}
return &iteratorState{next: nextState, currentNode: this, currentIndex: startingIndex, currentKey: this.keys}
}
}
func (this *node) next(state *iteratorState) (*iteratorState, Object, Object) {
if state == nil || state.currentNode != this {
state = this.createIteratorState(state)
}
if state.currentIndex == -1 {
kvp := state.currentKey
state.currentKey = kvp.next
if state.currentKey != nil {
return state, kvp.key, kvp.value
} else {
state.currentIndex++
if len(this.children) > 0 {
return state, kvp.key, kvp.value
} else {
return state.next, kvp.key, kvp.value
}
}
}
child := this.children[state.currentIndex]
state.currentIndex++
if state.currentIndex == len(this.children) {
return child.next(state.next)
} else {
return child.next(state)
}
}
func (this *node) checkInvariants(hash HashFunc, equals EqualsFunc, shift uint, report reporter) {
for kvp := this.keys; kvp != nil; kvp = kvp.next {
for other := kvp.next; other != nil; other = other.next {
if equals(kvp.key, other.key) {
report(fmt.Sprintf("duplicate key detected: key=%v", kvp.key))
}
}
if shiftedHash := hash(kvp.key) >> shift; shiftedHash != 0 {
report(fmt.Sprintf("key with non-zero hash detected: key=%v shiftedHash=%d", kvp.key, shiftedHash))
}
}
if this.bitmask != 0 && this.children == nil {
report("nil children with non-zero bitmask")
} else if this.bitmask == 0 && this.children != nil {
report("non-nil children with zero bitmask")
}
if this.children != nil {
if bitsLength := bits.OnesCount32(this.bitmask); bitsLength != len(this.children) {
report(fmt.Sprintf("bitmask count differs from children length: bitmask=%x bitsLength=%d sliceLength=%d", this.bitmask, bitsLength, len(this.children)))
}
for _, c := range this.children {
c.checkInvariants(hash, equals, shift+5, report)
}
}
}