-
Notifications
You must be signed in to change notification settings - Fork 17
/
treap.go
627 lines (612 loc) · 12 KB
/
treap.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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package gotomic
import (
"bytes"
"fmt"
"math/rand"
"sync/atomic"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type match struct {
previousOk bool
previousKey Comparable
previousValue Thing
matchOk bool
matchKey Comparable
matchValue Thing
nextOk bool
nextKey Comparable
nextValue Thing
}
type TreapIterator func(k Comparable, v Thing)
/*
Transaction controlled treap
*/
type treap struct {
root *nodeHandle
}
func (self *treap) Clone() Clonable {
return &treap{self.root}
}
func merge(t *Transaction, left, right *nodeHandle) (result *nodeHandle, err error) {
if left == nil {
result = right
return
}
if right == nil {
result = left
return
}
var leftNode, rightNode *node
var subMerge *nodeHandle
if left.weight < right.weight {
leftNode, err = left.wopen(t)
if err != nil {
return
}
result = left
tmp := leftNode.right
subMerge, err = merge(t, tmp, right)
if err != nil {
return
}
leftNode.right = subMerge
return
}
rightNode, err = right.wopen(t)
if err != nil {
return
}
result = right
tmp := rightNode.left
subMerge, err = merge(t, left, tmp)
if err != nil {
return
}
rightNode.left = subMerge
return
}
/*
Non-transaction controlled "user space" type
*/
type Treap struct {
handle *Handle
size int64
}
func NewTreap() *Treap {
return &Treap{NewHandle(&treap{}), 0}
}
/*
Get a readable *treap from the Treap
*/
func (self *Treap) ropen(t *Transaction) (*treap, error) {
r, err := t.Read(self.handle)
if err != nil {
return nil, err
}
return r.(*treap), nil
}
/*
Get a writable *treap from the Treap
*/
func (self *Treap) wopen(t *Transaction) (*treap, error) {
r, err := t.Write(self.handle)
if err != nil {
return nil, err
}
return r.(*treap), nil
}
func (treap *Treap) Describe() string {
rval, err := treap.describe()
for err != nil {
rval, err = treap.describe()
}
return rval
}
func (treap *Treap) describe() (rval string, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
buf := bytes.NewBufferString(fmt.Sprintf("&Treap{%p size:%v}\n", treap, treap.size))
if self.root != nil {
err = self.root.describe(t, buf, 0)
if err != nil {
return
}
}
return string(buf.Bytes()), nil
}
func (treap *Treap) Delete(k Comparable) (old Thing, ok bool) {
old, ok, err := treap.del(k)
for err != nil {
old, ok, err = treap.del(k)
}
return
}
func (treap *Treap) del(k Comparable) (old Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
ok = false
return
}
newRoot, old, ok, err := self.root.del(t, k)
if err != nil {
return
}
if newRoot != self.root {
self, err = treap.wopen(t)
if err != nil {
return
}
self.root = newRoot
}
if t.Commit() {
atomic.AddInt64(&treap.size, -1)
} else {
err = fmt.Errorf("%v changed during delete", treap)
}
return
}
func (treap *Treap) Put(k Comparable, v Thing) (old Thing, ok bool) {
old, ok, err := treap.put(k, v)
for err != nil {
old, ok, err = treap.put(k, v)
}
return
}
func (treap *Treap) ToSlice() (keys []Comparable, values []Thing) {
iter := func(k Comparable, v Thing) {
keys = append(keys, k)
values = append(values, v)
}
err := treap.Each(iter)
for err != nil {
keys = nil
values = nil
err = treap.Each(iter)
}
return
}
func (treap *Treap) Each(iter TreapIterator) (err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
return
}
err = self.root.each(t, iter)
return
}
func (treap *Treap) Next(k Comparable) (key Comparable, value Thing, ok bool) {
key, value, ok, err := treap.next(k)
for err != nil {
key, value, ok, err = treap.next(k)
}
return
}
func (treap *Treap) next(k Comparable) (key Comparable, value Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
ok = true
return
}
m := &match{}
err = self.root.get(t, k, m, false, true)
key = m.nextKey
value = m.nextValue
ok = m.nextOk
return
}
func (treap *Treap) Previous(k Comparable) (key Comparable, value Thing, ok bool) {
key, value, ok, err := treap.previous(k)
for err != nil {
key, value, ok, err = treap.previous(k)
}
return
}
func (treap *Treap) previous(k Comparable) (key Comparable, value Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
ok = true
return
}
m := &match{}
err = self.root.get(t, k, m, true, false)
key = m.previousKey
value = m.previousValue
ok = m.previousOk
return
}
func (treap *Treap) Get(k Comparable) (v Thing, ok bool) {
v, ok, err := treap.get(k)
for err != nil {
v, ok, err = treap.get(k)
}
return
}
func (treap *Treap) get(k Comparable) (v Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
return
}
m := &match{}
err = self.root.get(t, k, m, false, false)
v = m.matchValue
ok = m.matchOk
return
}
func (treap *Treap) Min() (k Comparable, v Thing, ok bool) {
k, v, ok, err := treap.min()
for err != nil {
k, v, ok, err = treap.min()
}
return
}
func (treap *Treap) min() (k Comparable, v Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
ok = false
return
}
ok = true
k, v, err = self.root.min(t)
return
}
func (treap *Treap) Max() (k Comparable, v Thing, ok bool) {
k, v, ok, err := treap.max()
for err != nil {
k, v, ok, err = treap.max()
}
return
}
func (treap *Treap) max() (k Comparable, v Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
if self.root == nil {
ok = false
return
}
ok = true
k, v, err = self.root.max(t)
return
}
func (treap *Treap) put(k Comparable, v Thing) (old Thing, ok bool, err error) {
t := NewTransaction()
self, err := treap.ropen(t)
if err != nil {
return
}
newNode := newNodeHandle(k, v)
newRoot, old, ok, err := self.root.insert(t, newNode)
if err != nil {
return
}
if newRoot != self.root {
self, err = treap.wopen(t)
if err != nil {
return
}
self.root = newRoot
}
if t.Commit() {
atomic.AddInt64(&treap.size, 1)
} else {
err = fmt.Errorf("%v changed during put", treap)
}
return
}
type node struct {
left *nodeHandle
right *nodeHandle
value Thing
}
func (self *node) Clone() Clonable {
rval := *self
return &rval
}
type nodeHandle struct {
*Handle
key Comparable
weight int32
}
func (handle *nodeHandle) ropen(t *Transaction) (*node, error) {
n, err := t.Read((*Handle)(handle.Handle))
if err != nil {
return nil, err
}
return n.(*node), nil
}
func (handle *nodeHandle) wopen(t *Transaction) (*node, error) {
r, err := t.Write((*Handle)(handle.Handle))
if err != nil {
return nil, err
}
return r.(*node), nil
}
func (handle *nodeHandle) each(t *Transaction, iter TreapIterator) (err error) {
if handle == nil {
return
}
self, err := handle.ropen(t)
if err != nil {
return
}
err = self.left.each(t, iter)
if err != nil {
return
}
iter(handle.key, self.value)
err = self.right.each(t, iter)
return
}
func (handle *nodeHandle) get(t *Transaction, k Comparable, m *match, previous, next bool) (err error) {
if handle == nil {
return
}
self, err := handle.ropen(t)
if err != nil {
return
}
switch cmp := k.Compare(handle.key); {
case cmp < 0:
if next {
m.nextKey = handle.key
m.nextValue = self.value
m.nextOk = true
}
err = self.left.get(t, k, m, previous, next)
case cmp > 0:
if previous {
m.previousKey = handle.key
m.previousValue = self.value
m.previousOk = true
}
err = self.right.get(t, k, m, previous, next)
default:
m.matchKey = handle.key
m.matchValue = self.value
m.matchOk = true
if previous {
if self.left != nil {
m.previousKey, m.previousValue, err = self.left.max(t)
if err != nil {
return
}
m.previousOk = true
}
}
if next {
if self.right != nil {
m.nextKey, m.nextValue, err = self.right.min(t)
if err != nil {
return
}
m.nextOk = true
}
}
}
return
}
func (handle *nodeHandle) min(t *Transaction) (k Comparable, v Thing, err error) {
self, err := handle.ropen(t)
if err != nil {
return
}
if self.left == nil {
k = handle.key
v = self.value
return
}
return self.left.min(t)
}
func (handle *nodeHandle) max(t *Transaction) (k Comparable, v Thing, err error) {
self, err := handle.ropen(t)
if err != nil {
return
}
if self.right == nil {
k = handle.key
v = self.value
return
}
return self.right.max(t)
}
func (handle *nodeHandle) describe(t *Transaction, buf *bytes.Buffer, indent int) error {
self, err := handle.ropen(t)
if err != nil {
return err
}
for i := 0; i < indent; i++ {
fmt.Fprintf(buf, " ")
}
fmt.Fprintf(buf, "%v => %v (%v)\n", handle.key, self.value, handle.weight)
if self.left != nil {
fmt.Fprintf(buf, "l:")
err = self.left.describe(t, buf, indent+1)
if err != nil {
return err
}
}
if self.right != nil {
fmt.Fprintf(buf, "r:")
err = self.right.describe(t, buf, indent+1)
if err != nil {
return err
}
}
return nil
}
func newNodeHandle(k Comparable, v Thing) *nodeHandle {
return &nodeHandle{NewHandle(&node{nil, nil, v}), k, rand.Int31()}
}
func (handle *nodeHandle) rotateLeft(t *Transaction) (result *nodeHandle, err error) {
self, err := handle.wopen(t)
if err != nil {
return
}
result = self.left
resultNode, err := result.wopen(t)
if err != nil {
return
}
tmp := resultNode.right
resultNode.right = handle
self.left = tmp
return
}
func (handle *nodeHandle) rotateRight(t *Transaction) (result *nodeHandle, err error) {
self, err := handle.wopen(t)
if err != nil {
return
}
result = self.right
resultNode, err := result.wopen(t)
if err != nil {
return
}
tmp := resultNode.left
resultNode.left = handle
self.right = tmp
return
}
func (handle *nodeHandle) del(t *Transaction, k Comparable) (result *nodeHandle, old Thing, ok bool, err error) {
if handle == nil {
ok = false
return
}
result = handle
self, err := handle.ropen(t)
if err != nil {
return
}
switch cmp := k.Compare(handle.key); {
case cmp < 0:
var newLeft *nodeHandle
newLeft, old, ok, err = self.left.del(t, k)
if err != nil {
return
}
if newLeft != self.left {
self, err = handle.wopen(t)
if err != nil {
return
}
self.left = newLeft
}
case cmp > 0:
var newRight *nodeHandle
newRight, old, ok, err = self.right.del(t, k)
if err != nil {
return
}
if newRight != self.right {
self, err = handle.wopen(t)
if err != nil {
return
}
self.right = newRight
}
default:
ok = true
old = self.value
result, err = merge(t, self.left, self.right)
if err != nil {
return
}
}
return
}
func (handle *nodeHandle) insert(t *Transaction, newHandle *nodeHandle) (result *nodeHandle, old Thing, ok bool, err error) {
if handle == nil {
ok = false
result = newHandle
return
}
result = handle
self, err := handle.ropen(t)
if err != nil {
return
}
switch cmp := newHandle.key.Compare(handle.key); {
case cmp < 0:
var newLeft *nodeHandle
newLeft, old, ok, err = self.left.insert(t, newHandle)
if err != nil {
return
}
if newLeft != self.left {
self, err = handle.wopen(t)
if err != nil {
return
}
self.left = newLeft
if newLeft.weight < handle.weight {
result, err = handle.rotateLeft(t)
if err != nil {
return
}
}
}
case cmp > 0:
var newRight *nodeHandle
newRight, old, ok, err = self.right.insert(t, newHandle)
if err != nil {
return
}
if newRight != self.right {
self, err = handle.wopen(t)
if err != nil {
return
}
self.right = newRight
if newRight.weight < handle.weight {
result, err = handle.rotateRight(t)
if err != nil {
return
}
}
}
default:
if self, err = handle.wopen(t); err != nil {
return
}
var newNode *node
newNode, err = newHandle.ropen(t)
if err != nil {
return
}
old = self.value
ok = true
self.value = newNode.value
}
return
}