/
TwoThreeTree.n
635 lines (605 loc) · 19.8 KB
/
TwoThreeTree.n
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
628
629
630
631
632
633
634
635
/*
* Copyright (c) 2009-2009 rampelstinskin@gmail.com
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of rampelstinskin may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using SC = System.Collections;
using SCG = System.Collections.Generic;
using Nemerle.Text;
using Nemerle.Utility;
namespace Nemerle.Collections
{
internal module TwoThreeTree
{
public enum InsertOptions
{
| Replace
| ThrowIfDuplicate
//| IgnoreDuplicate
}
public enum DeleteOptions
{
| IgnoreMissed
| ThrowIfMissed
}
public variant Node[T]
{
| Leaf
| Node2 { low : Node[T]; value1 : T; hi : Node[T] }
| Node3 { low : Node[T]; value1 : T; mid : Node[T]; value2 : T; hi : Node[T] }
public Insert(value : T, cmp : T * T -> int, options : InsertOptions, size : ref int) : Node[T]
{
mutable retL;
mutable retV;
mutable retH;
mutable replaced = false;
def eq(options, value)
{
match (options : InsertOptions)
{
| Replace => true
//| IgnoreDuplicate => ();
| ThrowIfDuplicate => throw ArgumentException($"\"$value\" already in the tree");
| _ => throw ArgumentException($<#The parameter options has invalid value: "$options"#>, "options");
}
}
def insert(_) : bool
{
| Leaf =>
retL = Leaf();
retV = value;
retH = Leaf();
false;
| Node2 as node =>
match (cmp(value, node.value1))
{
| c when c < 0 =>
if (insert(node.low))
{
retL = Node2(retL, node.value1, node.hi);
true;
}
else
{
retL = Node3(retL, retV, retH, node.value1, node.hi);
true;
}
| c when c > 0 =>
if (insert(node.hi))
{
retL = Node2(node.low, node.value1, retL);
true;
}
else
{
retL = Node3(node.low, node.value1, retL, retV, retH);
true;
}
| _ =>
replaced = eq(options, value);
retL = Node2(node.low, value, node.hi);
true;
}
| Node3 as node =>
match (cmp(value, node.value1))
{
| c when c < 0 =>
if (insert(node.low))
{
retL = Node3(retL, node.value1, node.mid, node.value2, node.hi);
true;
}
else
{
retL = Node2(retL, retV, retH);
retV = node.value1;
retH = Node2(node.mid, node.value2, node.hi);
false;
}
| c when c > 0 =>
match (cmp(value, node.value2))
{
| c when c < 0 =>
if (insert(node.mid))
{
retL = Node3(node.low, node.value1, retL, node.value2, node.hi);
true;
}
else
{
retL = Node2(node.low, node.value1, retL);
//retV = retV;
retH = Node2(retH, node.value2, node.hi);
false;
}
| c when c > 0 =>
if (insert(node.hi))
{
retL = Node3(node.low, node.value1, node.mid, node.value2, retL);
true;
}
else
{
retH = Node2(retL, retV, retH);
retL = Node2(node.low, node.value1, node.mid);
retV = node.value2;
false;
}
| _ =>
replaced = eq(options, value);
retL = Node3(node.low, node.value1, node.mid, value, node.hi);
true;
}
| _ =>
replaced = eq(options, value);
retL = Node3(node.low, value, node.mid, node.value2, node.hi);
true;
}
}
def ret = if (insert(this))
retL;
else
Node2(retL, retV, retH);
unless (replaced)
++size;
ret;
}
private abstract class CmpBase
{
public abstract Cmp21(node : Node[T].Node2) : int;
public abstract Cmp31(node : Node[T].Node3) : int;
public abstract Cmp32(node : Node[T].Node3) : int;
}
[Record]
private class CmpValue : CmpBase
{
private _value : T;
private _cmp : T * T -> int;
public override Cmp21(node : Node[T].Node2) : int { _cmp(_value, node.value1) }
public override Cmp31(node : Node[T].Node3) : int { _cmp(_value, node.value1) }
public override Cmp32(node : Node[T].Node3) : int { _cmp(_value, node.value2) }
}
private class CmpReplacement : CmpBase
{
public override Cmp21(node : Node[T].Node2) : int { match (node.low) { | Leaf => 0 | _ => -1 } }
public override Cmp31(node : Node[T].Node3) : int { match (node.low) { | Leaf => 0 | _ => -1 } }
public override Cmp32(node : Node[T].Node3) : int { match (node.low) { | Leaf => 0 | _ => -1 } }
}
private static _cmpReplacement : CmpBase = CmpReplacement();
public Delete(value : T, cmp : T * T -> int, options : DeleteOptions, size : ref int) : Node[T]
{
mutable retValue : T;
mutable retNode : Node[T];
mutable isMissed = false;
def missed(node : Node[T])
{
match (options)
{
| ThrowIfMissed => throw ArgumentException($"\"$value\" not in the tree");
| IgnoreMissed =>
isMissed = true;
retNode = node;
false;
}
}
//return isEmptyNode
def delete(node : Node[T], cmp : CmpBase) : bool
{
match (node)
{
| Leaf as node => missed(node);
| Node2 as node =>
def balanceLow(isEmpty, value, node)
{
if (isEmpty)
{
match (node.hi)
{
| Node2 as child =>
retNode = Node3(retNode, value, child.low, child.value1, child.hi);
true;
| Node3 as child =>
retNode = Node2(Node2(retNode, value, child.low), child.value1, Node2(child.mid, child.value2, child.hi));
false;
| Leaf => assert(false);
}
}
else
{
retNode = Node2(retNode, value, node.hi);
false;
}
}
def balanceHi(isEmpty, value, node)
{
if (isEmpty)
{
match (node.low)
{
| Node2 as child =>
retNode = Node3(child.low, child.value1, child.hi, value, retNode);
true;
| Node3 as child =>
retNode = Node2(Node2(child.low, child.value1, child.mid), child.value2, Node2(child.hi, value, retNode));
false;
| Leaf => assert(false);
}
}
else
{
retNode = Node2(node.low, value, retNode);
false;
}
}
match (cmp.Cmp21(node))
{
| c when c < 0 =>
balanceLow(delete(node.low, cmp), node.value1, node);
| c when c > 0 =>
balanceHi(delete(node.hi, cmp), node.value1, node);
| _ =>
match (node.low)
{
| Leaf =>
retValue = node.value1;
retNode = Leaf();
true;
| _ =>
balanceHi(delete(node.hi, _cmpReplacement), retValue, node);
}
}
| Node3 as node =>
def balanceLow(isEmpty, value, node)
{
if (isEmpty)
{
match (node.mid)
{
| Node2 as child =>
retNode = Node2(Node3(retNode, value, child.low, child.value1, child.hi), node.value2, node.hi);
false;
| Node3 as child =>
retNode = Node3(Node2(retNode, value, child.low), child.value1, Node2(child.mid, child.value2, child.hi), node.value2, node.hi);
false;
| Leaf => assert(false);
}
}
else
{
retNode = Node3(retNode, value, node.mid, node.value2, node.hi);
false;
}
}
def balanceMid(isEmpty, value, node)
{
if (isEmpty)
{
match (node.low)
{
| Node2 as child =>
retNode = Node2(Node3(child.low, child.value1, child.hi, value, retNode), node.value2, node.hi);
false;
| Node3 as child =>
retNode = Node3(Node2(child.low, child.value1, child.mid), child.value2, Node2(child.hi, value, retNode), node.value2, node.hi);
false;
| Leaf => assert(false);
}
}
else
{
retNode = Node3(node.low, value, retNode, node.value2, node.hi);
false;
}
}
def balanceHi(isEmpty, value, node)
{
if (isEmpty)
{
match (node.mid)
{
| Node2 as child =>
retNode = Node2(node.low, node.value1, Node3(child.low, child.value1, child.hi, value, retNode));
false;
| Node3 as child =>
retNode = Node3(node.low, node.value1, Node2(child.low, child.value1, child.mid), child.value2, Node2(child.hi, value, retNode));
false;
| Leaf => assert(false);
}
}
else
{
retNode = Node3(node.low, node.value1, node.mid, value, retNode);
false;
}
}
match (cmp.Cmp31(node))
{
| c when c < 0 =>
balanceLow(delete(node.low, cmp), node.value1, node);
| c when c > 0 =>
match (cmp.Cmp32(node))
{
| c when c < 0 =>
balanceMid(delete(node.mid, cmp), node.value1, node);
| c when c > 0 =>
balanceHi(delete(node.hi, cmp), node.value2, node);
| _ =>
match (node.low)
{
| Leaf =>
retValue = node.value2;
retNode = Node2(Leaf(), node.value1, Leaf());
false;
| _ =>
balanceHi(delete(node.hi, _cmpReplacement), retValue, node);
}
}
| _ =>
match (node.low)
{
| Leaf =>
retValue = node.value1;
retNode = Node2(Leaf(), node.value2, Leaf());
false;
| _ =>
balanceMid(delete(node.mid, _cmpReplacement), retValue, node);
}
}
}
}
_ = delete(this, CmpValue(value, cmp));
if (isMissed)
{
this;
}
else
{
--size;
retNode;
}
}
public Filter(cmp : T * T -> int, fn : T -> bool) : int * Node[T]
{
mutable size = 0;
def filterFn(value, tree)
{
if (fn(value))
tree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref size)
else
tree
}
(size, this.Fold(Node[T].Leaf(), filterFn));
}
public Partition(cmp : T * T -> int, fn : T -> bool) : int * Node[T] * int * Node[T]
{
mutable ysize = 0;
mutable ytree = Node[T].Leaf();
mutable nsize = 0;
mutable ntree = Node[T].Leaf();
def partitionFn(value, dummy)
{
if (fn(value))
ytree = ytree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref ysize);
else
ntree = ntree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref nsize);
dummy;
}
_ = this.Fold(null, partitionFn);
(ysize, ytree, nsize, ntree);
}
public TryGet(value : T, cmp : T * T -> int, outValue : out T) : bool
{
def tryGet(node)
{
| Leaf => false
| Node2 as node =>
match (cmp(value, node.value1))
{
| c when c < 0 => tryGet(node.low);
| c when c > 0 => tryGet(node.hi);
| _ =>
outValue = node.value1;
true;
}
| Node3 as node =>
match (cmp(value, node.value1))
{
| c when c < 0 => tryGet(node.low);
| c when c > 0 =>
match (cmp(value, node.value2))
{
| c when c < 0 => tryGet(node.mid);
| c when c > 0 => tryGet(node.hi);
| _ =>
outValue = node.value2;
true;
}
| _ =>
outValue = node.value1;
true;
}
}
outValue = value;
tryGet(this);
}
public Enumerate() : SCG.IEnumerable[T]
{
ToArray();
}
public Fold[TAccumulator](acc : TAccumulator, fn : T * TAccumulator -> TAccumulator) : TAccumulator
{
FoldLeft(acc, fn);
}
public FoldLeft[TAccumulator](acc : TAccumulator, fn : T * TAccumulator -> TAccumulator) : TAccumulator
{
match (this)
{
| Leaf => acc;
| Node2 as node =>
def acc = node.low.FoldLeft(acc, fn);
def acc = fn(node.value1, acc);
def acc = node.hi.FoldLeft(acc, fn);
acc;
| Node3 as node =>
def acc = node.low.FoldLeft(acc, fn);
def acc = fn(node.value1, acc);
def acc = node.mid.FoldLeft(acc, fn);
def acc = fn(node.value2, acc);
def acc = node.hi.FoldLeft(acc, fn);
acc;
}
}
public FoldRight[TAccumulator](acc : TAccumulator, fn : T * TAccumulator -> TAccumulator) : TAccumulator
{
match (this)
{
| Leaf => acc;
| Node2 as node =>
def acc = node.hi.FoldRight(acc, fn);
def acc = fn(node.value1, acc);
def acc = node.low.FoldRight(acc, fn);
acc;
| Node3 as node =>
def acc = node.hi.FoldRight(acc, fn);
def acc = fn(node.value2, acc);
def acc = node.mid.FoldRight(acc, fn);
def acc = fn(node.value1, acc);
def acc = node.low.FoldRight(acc, fn);
acc;
}
}
public ForAll[TAccumulator](fn : T -> bool) : bool
{
match (this)
{
| Leaf => true;
| Node2 as node => fn(node.value1) && node.low.ForAll(fn) && node.hi.ForAll(fn);
| Node3 as node => fn(node.value1) && fn(node.value2) && node.low.ForAll(fn) && node.mid.ForAll(fn) && node.hi.ForAll(fn);
}
}
public Exists[TAccumulator](fn : T -> bool) : bool
{
match (this)
{
| Leaf => false;
| Node2 as node => fn(node.value1) || node.low.Exists(fn) || node.hi.Exists(fn);
| Node3 as node => fn(node.value1) || fn(node.value2) || node.low.Exists(fn) || node.mid.Exists(fn) || node.hi.Exists(fn);
}
}
public Count : int
{
get
{
match (this)
{
| Leaf => 0;
| Node2 as node => 1 + node.low.Count + node.hi.Count;
| Node3 as node => 2 + node.low.Count + node.mid.Count + node.hi.Count;
}
}
}
public Sum[TAccumulator](size : ref int, that : Node[T], cmp : T * T -> int) : Node[T]
{
mutable sz = size;
def res = that.Fold(this, (value, tree) => tree.Insert(value, cmp, InsertOptions.Replace, ref sz));
size = sz;
res;
}
public Subtract[TAccumulator](size : ref int, that : Node[T], cmp : T * T -> int) : Node[T]
{
mutable sz = size;
def res = that.Fold(this, (value, tree) => tree.Delete(value, cmp, DeleteOptions.IgnoreMissed, ref sz));
size = sz;
res;
}
public Intersect[TAccumulator](size : ref int, that : Node[T], cmp : T * T -> int) : Node[T]
{
mutable sz = 0;
mutable outValue;
def res = that.Fold
(Node[T].Leaf(), (value, tree) =>
if (this.TryGet(value, cmp, out outValue))
tree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref sz)
else
tree
);
size = sz;
res;
}
public Xor[TAccumulator](size : ref int, that : Node[T], cmp : T * T -> int) : Node[T]
{
mutable sz = 0;
mutable outValue;
def tree = Node[T].Leaf();
def tree = that.Fold
(tree, (value, tree) =>
if (this.TryGet(value, cmp, out outValue))
tree
else
tree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref sz)
);
def tree = this.Fold
(tree, (value, tree) =>
if (that.TryGet(value, cmp, out outValue))
tree
else
tree.Insert(value, cmp, InsertOptions.ThrowIfDuplicate, ref sz)
);
size = sz;
tree
}
public ToArray() : array[T]
{
FoldLeft(SCG.List(), (value, lst) => { lst.Add(value); lst}).ToArray();
}
public ToList() : list[T]
{
FoldRight([], _ :: _);
}
public MapToArray[U](fn : T -> U) : array[U]
{
FoldLeft(SCG.List(), (value, lst) => { lst.Add(fn(value)); lst}).ToArray();
}
public MapToList[U](fn : T -> U) : list[U]
{
FoldRight([], (value, acc) => fn(value) :: acc);
}
public ToString(first : string, delim : string, last : string) : string
{
def sb = System.Text.StringBuilder().Append(first);
def sb = FoldLeft(sb, (value, sb) => sb.Append(value : object).Append(delim) );
when (sb.Length > first.Length)
sb.Length -= delim.Length;
_ = sb.Append(last);
sb.ToString();
}
public override GetHashCode() : int
{
FoldLeft(0, (val, hash) => unchecked { (hash ^ val.GetHashCode()) * 5 + hash } )
}
}
}
}