-
Notifications
You must be signed in to change notification settings - Fork 2
/
gotemplate_orderMap.go
548 lines (509 loc) · 12.7 KB
/
gotemplate_orderMap.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// Code generated by gotemplate. DO NOT EDIT.
// Package treemap provides a generic key-sorted map. It uses red-black tree under the hood.
// You can use it as a template to generate a sorted map with specific key and value types.
// Iterators are designed after C++.
//
// Example:
//
// package main
//
// import "fmt"
//
// //go:generate gotemplate "github.com/igrmk/treemap" "intStringTreeMap(int, string)"
//
// func less(x, y int) bool { return x < y }
//
// func main() {
// tr := newIntStringTreeMap(less)
// tr.Set(0, "Hello")
// tr.Set(1, "World")
//
// for it := tr.Iterator(); it.Valid(); it.Next() {
// fmt.Println(it.Key(), it.Value())
// }
// }
package tome
// template type TreeMap(Key, Value)
// Key is a generic key type of the map
// Value is a generic value type of the map
// TreeMap is the red-black tree based map
type orderMap struct {
endNode *nodeOrderMap
beginNode *nodeOrderMap
count int
// Less returns a < b
Less func(a OrderTracker, b OrderTracker) bool
}
type nodeOrderMap struct {
right *nodeOrderMap
left *nodeOrderMap
parent *nodeOrderMap
isBlack bool
key OrderTracker
value bool
}
// New creates and returns new TreeMap.
// Parameter less is a function returning a < b.
func newOrderMap(less func(a OrderTracker, b OrderTracker) bool) *orderMap {
endNode := &nodeOrderMap{isBlack: true}
return &orderMap{beginNode: endNode, endNode: endNode, Less: less}
}
// Len returns total count of elements in a map.
// Complexity: O(1).
func (t *orderMap) Len() int { return t.count }
// Set sets the value and silently overrides previous value if it exists.
// Complexity: O(log N).
func (t *orderMap) Set(key OrderTracker, value bool) {
parent := t.endNode
current := parent.left
less := true
for current != nil {
parent = current
switch {
case t.Less(key, current.key):
current = current.left
less = true
case t.Less(current.key, key):
current = current.right
less = false
default:
current.value = value
return
}
}
x := &nodeOrderMap{parent: parent, value: value, key: key}
if less {
parent.left = x
} else {
parent.right = x
}
if t.beginNode.left != nil {
t.beginNode = t.beginNode.left
}
t.insertFixup(x)
t.count++
}
// Del deletes the value.
// Complexity: O(log N).
func (t *orderMap) Del(key OrderTracker) {
z := t.findNode(key)
if z == nil {
return
}
if t.beginNode == z {
if z.right != nil {
t.beginNode = z.right
} else {
t.beginNode = z.parent
}
}
t.count--
removeNodeOrderMap(t.endNode.left, z)
}
// Clear clears the map.
// Complexity: O(1).
func (t *orderMap) Clear() {
t.count = 0
t.beginNode = t.endNode
t.endNode.left = nil
}
// Get retrieves a value from a map for specified key and reports if it exists.
// Complexity: O(log N).
func (t *orderMap) Get(id OrderTracker) (bool, bool) {
node := t.findNode(id)
if node == nil {
node = t.endNode
}
return node.value, node != t.endNode
}
// Contains checks if key exists in a map.
// Complexity: O(log N)
func (t *orderMap) Contains(id OrderTracker) bool { return t.findNode(id) != nil }
// Range returns a pair of iterators that you can use to go through all the keys in the range [from, to].
// More specifically it returns iterators pointing to lower bound and upper bound.
// Complexity: O(log N).
func (t *orderMap) Range(from, to OrderTracker) (forwardIteratorOrderMap, forwardIteratorOrderMap) {
return t.LowerBound(from), t.UpperBound(to)
}
// LowerBound returns an iterator pointing to the first element that is not less than the given key.
// Complexity: O(log N).
func (t *orderMap) LowerBound(key OrderTracker) forwardIteratorOrderMap {
result := t.endNode
node := t.endNode.left
if node == nil {
return forwardIteratorOrderMap{tree: t, node: t.endNode}
}
for {
if t.Less(node.key, key) {
if node.right != nil {
node = node.right
} else {
return forwardIteratorOrderMap{tree: t, node: result}
}
} else {
result = node
if node.left != nil {
node = node.left
} else {
return forwardIteratorOrderMap{tree: t, node: result}
}
}
}
}
// UpperBound returns an iterator pointing to the first element that is greater than the given key.
// Complexity: O(log N).
func (t *orderMap) UpperBound(key OrderTracker) forwardIteratorOrderMap {
result := t.endNode
node := t.endNode.left
if node == nil {
return forwardIteratorOrderMap{tree: t, node: t.endNode}
}
for {
if !t.Less(key, node.key) {
if node.right != nil {
node = node.right
} else {
return forwardIteratorOrderMap{tree: t, node: result}
}
} else {
result = node
if node.left != nil {
node = node.left
} else {
return forwardIteratorOrderMap{tree: t, node: result}
}
}
}
}
// Iterator returns an iterator for tree map.
// It starts at the first element and goes to the one-past-the-end position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(1)
func (t *orderMap) Iterator() forwardIteratorOrderMap {
return forwardIteratorOrderMap{tree: t, node: t.beginNode}
}
// Reverse returns a reverse iterator for tree map.
// It starts at the last element and goes to the one-before-the-start position.
// You can iterate a map at O(N) complexity.
// Method complexity: O(log N)
func (t *orderMap) Reverse() reverseIteratorOrderMap {
node := t.endNode.left
if node != nil {
node = mostRightOrderMap(node)
}
return reverseIteratorOrderMap{tree: t, node: node}
}
func (t *orderMap) findNode(id OrderTracker) *nodeOrderMap {
current := t.endNode.left
for current != nil {
switch {
case t.Less(id, current.key):
current = current.left
case t.Less(current.key, id):
current = current.right
default:
return current
}
}
return nil
}
func mostLeftOrderMap(x *nodeOrderMap) *nodeOrderMap {
for x.left != nil {
x = x.left
}
return x
}
func mostRightOrderMap(x *nodeOrderMap) *nodeOrderMap {
for x.right != nil {
x = x.right
}
return x
}
func successorOrderMap(x *nodeOrderMap) *nodeOrderMap {
if x.right != nil {
return mostLeftOrderMap(x.right)
}
for x != x.parent.left {
x = x.parent
}
return x.parent
}
func predecessorOrderMap(x *nodeOrderMap) *nodeOrderMap {
if x.left != nil {
return mostRightOrderMap(x.left)
}
for x.parent != nil && x != x.parent.right {
x = x.parent
}
return x.parent
}
func rotateLeftOrderMap(x *nodeOrderMap) {
y := x.right
x.right = y.left
if x.right != nil {
x.right.parent = x
}
y.parent = x.parent
if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.left = x
x.parent = y
}
func rotateRightOrderMap(x *nodeOrderMap) {
y := x.left
x.left = y.right
if x.left != nil {
x.left.parent = x
}
y.parent = x.parent
if x == x.parent.left {
x.parent.left = y
} else {
x.parent.right = y
}
y.right = x
x.parent = y
}
func (t *orderMap) insertFixup(x *nodeOrderMap) {
root := t.endNode.left
x.isBlack = x == root
for x != root && !x.parent.isBlack {
if x.parent == x.parent.parent.left {
y := x.parent.parent.right
if y != nil && !y.isBlack {
x = x.parent
x.isBlack = true
x = x.parent
x.isBlack = x == root
y.isBlack = true
} else {
if x != x.parent.left {
x = x.parent
rotateLeftOrderMap(x)
}
x = x.parent
x.isBlack = true
x = x.parent
x.isBlack = false
rotateRightOrderMap(x)
break
}
} else {
y := x.parent.parent.left
if y != nil && !y.isBlack {
x = x.parent
x.isBlack = true
x = x.parent
x.isBlack = x == root
y.isBlack = true
} else {
if x == x.parent.left {
x = x.parent
rotateRightOrderMap(x)
}
x = x.parent
x.isBlack = true
x = x.parent
x.isBlack = false
rotateLeftOrderMap(x)
break
}
}
}
}
// nolint: gocyclo
//noinspection GoNilness
func removeNodeOrderMap(root *nodeOrderMap, z *nodeOrderMap) {
var y *nodeOrderMap
if z.left == nil || z.right == nil {
y = z
} else {
y = successorOrderMap(z)
}
var x *nodeOrderMap
if y.left != nil {
x = y.left
} else {
x = y.right
}
var w *nodeOrderMap
if x != nil {
x.parent = y.parent
}
if y == y.parent.left {
y.parent.left = x
if y != root {
w = y.parent.right
} else {
root = x // w == nil
}
} else {
y.parent.right = x
w = y.parent.left
}
removedBlack := y.isBlack
if y != z {
y.parent = z.parent
if z == z.parent.left {
y.parent.left = y
} else {
y.parent.right = y
}
y.left = z.left
y.left.parent = y
y.right = z.right
if y.right != nil {
y.right.parent = y
}
y.isBlack = z.isBlack
if root == z {
root = y
}
}
if removedBlack && root != nil {
if x != nil {
x.isBlack = true
} else {
for {
if w != w.parent.left {
if !w.isBlack {
w.isBlack = true
w.parent.isBlack = false
rotateLeftOrderMap(w.parent)
if root == w.left {
root = w
}
w = w.left.right
}
if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
w.isBlack = false
x = w.parent
if x == root || !x.isBlack {
x.isBlack = true
break
}
if x == x.parent.left {
w = x.parent.right
} else {
w = x.parent.left
}
} else {
if w.right == nil || w.right.isBlack {
w.left.isBlack = true
w.isBlack = false
rotateRightOrderMap(w)
w = w.parent
}
w.isBlack = w.parent.isBlack
w.parent.isBlack = true
w.right.isBlack = true
rotateLeftOrderMap(w.parent)
break
}
} else {
if !w.isBlack {
w.isBlack = true
w.parent.isBlack = false
rotateRightOrderMap(w.parent)
if root == w.right {
root = w
}
w = w.right.left
}
if (w.left == nil || w.left.isBlack) && (w.right == nil || w.right.isBlack) {
w.isBlack = false
x = w.parent
if !x.isBlack || x == root {
x.isBlack = true
break
}
if x == x.parent.left {
w = x.parent.right
} else {
w = x.parent.left
}
} else {
if w.left == nil || w.left.isBlack {
w.right.isBlack = true
w.isBlack = false
rotateLeftOrderMap(w)
w = w.parent
}
w.isBlack = w.parent.isBlack
w.parent.isBlack = true
w.left.isBlack = true
rotateRightOrderMap(w.parent)
break
}
}
}
}
}
}
// ForwardIterator represents a position in a tree map.
// It is designed to iterate a map in a forward order.
// It can point to any position from the first element to the one-past-the-end element.
type forwardIteratorOrderMap struct {
tree *orderMap
node *nodeOrderMap
}
// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-past-the-end position.
func (i forwardIteratorOrderMap) Valid() bool { return i.node != i.tree.endNode }
// Next moves an iterator to the next element.
// It panics if goes out of bounds.
func (i *forwardIteratorOrderMap) Next() {
if i.node == i.tree.endNode {
panic("out of bound iteration")
}
i.node = successorOrderMap(i.node)
}
// Prev moves an iterator to the previous element.
// It panics if goes out of bounds.
func (i *forwardIteratorOrderMap) Prev() {
i.node = predecessorOrderMap(i.node)
if i.node == nil {
panic("out of bound iteration")
}
}
// Key returns a key at an iterator's position
func (i forwardIteratorOrderMap) Key() OrderTracker { return i.node.key }
// Value returns a value at an iterator's position
func (i forwardIteratorOrderMap) Value() bool { return i.node.value }
// ReverseIterator represents a position in a tree map.
// It is designed to iterate a map in a reverse order.
// It can point to any position from the one-before-the-start element to the last element.
type reverseIteratorOrderMap struct {
tree *orderMap
node *nodeOrderMap
}
// Valid reports if an iterator's position is valid.
// In other words it returns true if an iterator is not at the one-before-the-start position.
func (i reverseIteratorOrderMap) Valid() bool { return i.node != nil }
// Next moves an iterator to the next element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorOrderMap) Next() {
if i.node == nil {
panic("out of bound iteration")
}
i.node = predecessorOrderMap(i.node)
}
// Prev moves an iterator to the previous element in reverse order.
// It panics if goes out of bounds.
func (i *reverseIteratorOrderMap) Prev() {
if i.node != nil {
i.node = successorOrderMap(i.node)
} else {
i.node = i.tree.beginNode
}
if i.node == i.tree.endNode {
panic("out of bound iteration")
}
}
// Key returns a key at an iterator's position
func (i reverseIteratorOrderMap) Key() OrderTracker { return i.node.key }
// Value returns a value at an iterator's position
func (i reverseIteratorOrderMap) Value() bool { return i.node.value }