-
Notifications
You must be signed in to change notification settings - Fork 0
/
HP12c.js
12035 lines (11059 loc) · 310 KB
/
HP12c.js
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function() {
'use strict';
function F2(fun)
{
function wrapper(a) { return function(b) { return fun(a,b); }; }
wrapper.arity = 2;
wrapper.func = fun;
return wrapper;
}
function F3(fun)
{
function wrapper(a) {
return function(b) { return function(c) { return fun(a, b, c); }; };
}
wrapper.arity = 3;
wrapper.func = fun;
return wrapper;
}
function F4(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return fun(a, b, c, d); }; }; };
}
wrapper.arity = 4;
wrapper.func = fun;
return wrapper;
}
function F5(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return function(e) { return fun(a, b, c, d, e); }; }; }; };
}
wrapper.arity = 5;
wrapper.func = fun;
return wrapper;
}
function F6(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return function(e) { return function(f) {
return fun(a, b, c, d, e, f); }; }; }; }; };
}
wrapper.arity = 6;
wrapper.func = fun;
return wrapper;
}
function F7(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return function(e) { return function(f) {
return function(g) { return fun(a, b, c, d, e, f, g); }; }; }; }; }; };
}
wrapper.arity = 7;
wrapper.func = fun;
return wrapper;
}
function F8(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return function(e) { return function(f) {
return function(g) { return function(h) {
return fun(a, b, c, d, e, f, g, h); }; }; }; }; }; }; };
}
wrapper.arity = 8;
wrapper.func = fun;
return wrapper;
}
function F9(fun)
{
function wrapper(a) { return function(b) { return function(c) {
return function(d) { return function(e) { return function(f) {
return function(g) { return function(h) { return function(i) {
return fun(a, b, c, d, e, f, g, h, i); }; }; }; }; }; }; }; };
}
wrapper.arity = 9;
wrapper.func = fun;
return wrapper;
}
function A2(fun, a, b)
{
return fun.arity === 2
? fun.func(a, b)
: fun(a)(b);
}
function A3(fun, a, b, c)
{
return fun.arity === 3
? fun.func(a, b, c)
: fun(a)(b)(c);
}
function A4(fun, a, b, c, d)
{
return fun.arity === 4
? fun.func(a, b, c, d)
: fun(a)(b)(c)(d);
}
function A5(fun, a, b, c, d, e)
{
return fun.arity === 5
? fun.func(a, b, c, d, e)
: fun(a)(b)(c)(d)(e);
}
function A6(fun, a, b, c, d, e, f)
{
return fun.arity === 6
? fun.func(a, b, c, d, e, f)
: fun(a)(b)(c)(d)(e)(f);
}
function A7(fun, a, b, c, d, e, f, g)
{
return fun.arity === 7
? fun.func(a, b, c, d, e, f, g)
: fun(a)(b)(c)(d)(e)(f)(g);
}
function A8(fun, a, b, c, d, e, f, g, h)
{
return fun.arity === 8
? fun.func(a, b, c, d, e, f, g, h)
: fun(a)(b)(c)(d)(e)(f)(g)(h);
}
function A9(fun, a, b, c, d, e, f, g, h, i)
{
return fun.arity === 9
? fun.func(a, b, c, d, e, f, g, h, i)
: fun(a)(b)(c)(d)(e)(f)(g)(h)(i);
}
//import Native.List //
var _elm_lang$core$Native_Array = function() {
// A RRB-Tree has two distinct data types.
// Leaf -> "height" is always 0
// "table" is an array of elements
// Node -> "height" is always greater than 0
// "table" is an array of child nodes
// "lengths" is an array of accumulated lengths of the child nodes
// M is the maximal table size. 32 seems fast. E is the allowed increase
// of search steps when concatting to find an index. Lower values will
// decrease balancing, but will increase search steps.
var M = 32;
var E = 2;
// An empty array.
var empty = {
ctor: '_Array',
height: 0,
table: []
};
function get(i, array)
{
if (i < 0 || i >= length(array))
{
throw new Error(
'Index ' + i + ' is out of range. Check the length of ' +
'your array first or use getMaybe or getWithDefault.');
}
return unsafeGet(i, array);
}
function unsafeGet(i, array)
{
for (var x = array.height; x > 0; x--)
{
var slot = i >> (x * 5);
while (array.lengths[slot] <= i)
{
slot++;
}
if (slot > 0)
{
i -= array.lengths[slot - 1];
}
array = array.table[slot];
}
return array.table[i];
}
// Sets the value at the index i. Only the nodes leading to i will get
// copied and updated.
function set(i, item, array)
{
if (i < 0 || length(array) <= i)
{
return array;
}
return unsafeSet(i, item, array);
}
function unsafeSet(i, item, array)
{
array = nodeCopy(array);
if (array.height === 0)
{
array.table[i] = item;
}
else
{
var slot = getSlot(i, array);
if (slot > 0)
{
i -= array.lengths[slot - 1];
}
array.table[slot] = unsafeSet(i, item, array.table[slot]);
}
return array;
}
function initialize(len, f)
{
if (len <= 0)
{
return empty;
}
var h = Math.floor( Math.log(len) / Math.log(M) );
return initialize_(f, h, 0, len);
}
function initialize_(f, h, from, to)
{
if (h === 0)
{
var table = new Array((to - from) % (M + 1));
for (var i = 0; i < table.length; i++)
{
table[i] = f(from + i);
}
return {
ctor: '_Array',
height: 0,
table: table
};
}
var step = Math.pow(M, h);
var table = new Array(Math.ceil((to - from) / step));
var lengths = new Array(table.length);
for (var i = 0; i < table.length; i++)
{
table[i] = initialize_(f, h - 1, from + (i * step), Math.min(from + ((i + 1) * step), to));
lengths[i] = length(table[i]) + (i > 0 ? lengths[i-1] : 0);
}
return {
ctor: '_Array',
height: h,
table: table,
lengths: lengths
};
}
function fromList(list)
{
if (list.ctor === '[]')
{
return empty;
}
// Allocate M sized blocks (table) and write list elements to it.
var table = new Array(M);
var nodes = [];
var i = 0;
while (list.ctor !== '[]')
{
table[i] = list._0;
list = list._1;
i++;
// table is full, so we can push a leaf containing it into the
// next node.
if (i === M)
{
var leaf = {
ctor: '_Array',
height: 0,
table: table
};
fromListPush(leaf, nodes);
table = new Array(M);
i = 0;
}
}
// Maybe there is something left on the table.
if (i > 0)
{
var leaf = {
ctor: '_Array',
height: 0,
table: table.splice(0, i)
};
fromListPush(leaf, nodes);
}
// Go through all of the nodes and eventually push them into higher nodes.
for (var h = 0; h < nodes.length - 1; h++)
{
if (nodes[h].table.length > 0)
{
fromListPush(nodes[h], nodes);
}
}
var head = nodes[nodes.length - 1];
if (head.height > 0 && head.table.length === 1)
{
return head.table[0];
}
else
{
return head;
}
}
// Push a node into a higher node as a child.
function fromListPush(toPush, nodes)
{
var h = toPush.height;
// Maybe the node on this height does not exist.
if (nodes.length === h)
{
var node = {
ctor: '_Array',
height: h + 1,
table: [],
lengths: []
};
nodes.push(node);
}
nodes[h].table.push(toPush);
var len = length(toPush);
if (nodes[h].lengths.length > 0)
{
len += nodes[h].lengths[nodes[h].lengths.length - 1];
}
nodes[h].lengths.push(len);
if (nodes[h].table.length === M)
{
fromListPush(nodes[h], nodes);
nodes[h] = {
ctor: '_Array',
height: h + 1,
table: [],
lengths: []
};
}
}
// Pushes an item via push_ to the bottom right of a tree.
function push(item, a)
{
var pushed = push_(item, a);
if (pushed !== null)
{
return pushed;
}
var newTree = create(item, a.height);
return siblise(a, newTree);
}
// Recursively tries to push an item to the bottom-right most
// tree possible. If there is no space left for the item,
// null will be returned.
function push_(item, a)
{
// Handle resursion stop at leaf level.
if (a.height === 0)
{
if (a.table.length < M)
{
var newA = {
ctor: '_Array',
height: 0,
table: a.table.slice()
};
newA.table.push(item);
return newA;
}
else
{
return null;
}
}
// Recursively push
var pushed = push_(item, botRight(a));
// There was space in the bottom right tree, so the slot will
// be updated.
if (pushed !== null)
{
var newA = nodeCopy(a);
newA.table[newA.table.length - 1] = pushed;
newA.lengths[newA.lengths.length - 1]++;
return newA;
}
// When there was no space left, check if there is space left
// for a new slot with a tree which contains only the item
// at the bottom.
if (a.table.length < M)
{
var newSlot = create(item, a.height - 1);
var newA = nodeCopy(a);
newA.table.push(newSlot);
newA.lengths.push(newA.lengths[newA.lengths.length - 1] + length(newSlot));
return newA;
}
else
{
return null;
}
}
// Converts an array into a list of elements.
function toList(a)
{
return toList_(_elm_lang$core$Native_List.Nil, a);
}
function toList_(list, a)
{
for (var i = a.table.length - 1; i >= 0; i--)
{
list =
a.height === 0
? _elm_lang$core$Native_List.Cons(a.table[i], list)
: toList_(list, a.table[i]);
}
return list;
}
// Maps a function over the elements of an array.
function map(f, a)
{
var newA = {
ctor: '_Array',
height: a.height,
table: new Array(a.table.length)
};
if (a.height > 0)
{
newA.lengths = a.lengths;
}
for (var i = 0; i < a.table.length; i++)
{
newA.table[i] =
a.height === 0
? f(a.table[i])
: map(f, a.table[i]);
}
return newA;
}
// Maps a function over the elements with their index as first argument.
function indexedMap(f, a)
{
return indexedMap_(f, a, 0);
}
function indexedMap_(f, a, from)
{
var newA = {
ctor: '_Array',
height: a.height,
table: new Array(a.table.length)
};
if (a.height > 0)
{
newA.lengths = a.lengths;
}
for (var i = 0; i < a.table.length; i++)
{
newA.table[i] =
a.height === 0
? A2(f, from + i, a.table[i])
: indexedMap_(f, a.table[i], i == 0 ? from : from + a.lengths[i - 1]);
}
return newA;
}
function foldl(f, b, a)
{
if (a.height === 0)
{
for (var i = 0; i < a.table.length; i++)
{
b = A2(f, a.table[i], b);
}
}
else
{
for (var i = 0; i < a.table.length; i++)
{
b = foldl(f, b, a.table[i]);
}
}
return b;
}
function foldr(f, b, a)
{
if (a.height === 0)
{
for (var i = a.table.length; i--; )
{
b = A2(f, a.table[i], b);
}
}
else
{
for (var i = a.table.length; i--; )
{
b = foldr(f, b, a.table[i]);
}
}
return b;
}
// TODO: currently, it slices the right, then the left. This can be
// optimized.
function slice(from, to, a)
{
if (from < 0)
{
from += length(a);
}
if (to < 0)
{
to += length(a);
}
return sliceLeft(from, sliceRight(to, a));
}
function sliceRight(to, a)
{
if (to === length(a))
{
return a;
}
// Handle leaf level.
if (a.height === 0)
{
var newA = { ctor:'_Array', height:0 };
newA.table = a.table.slice(0, to);
return newA;
}
// Slice the right recursively.
var right = getSlot(to, a);
var sliced = sliceRight(to - (right > 0 ? a.lengths[right - 1] : 0), a.table[right]);
// Maybe the a node is not even needed, as sliced contains the whole slice.
if (right === 0)
{
return sliced;
}
// Create new node.
var newA = {
ctor: '_Array',
height: a.height,
table: a.table.slice(0, right),
lengths: a.lengths.slice(0, right)
};
if (sliced.table.length > 0)
{
newA.table[right] = sliced;
newA.lengths[right] = length(sliced) + (right > 0 ? newA.lengths[right - 1] : 0);
}
return newA;
}
function sliceLeft(from, a)
{
if (from === 0)
{
return a;
}
// Handle leaf level.
if (a.height === 0)
{
var newA = { ctor:'_Array', height:0 };
newA.table = a.table.slice(from, a.table.length + 1);
return newA;
}
// Slice the left recursively.
var left = getSlot(from, a);
var sliced = sliceLeft(from - (left > 0 ? a.lengths[left - 1] : 0), a.table[left]);
// Maybe the a node is not even needed, as sliced contains the whole slice.
if (left === a.table.length - 1)
{
return sliced;
}
// Create new node.
var newA = {
ctor: '_Array',
height: a.height,
table: a.table.slice(left, a.table.length + 1),
lengths: new Array(a.table.length - left)
};
newA.table[0] = sliced;
var len = 0;
for (var i = 0; i < newA.table.length; i++)
{
len += length(newA.table[i]);
newA.lengths[i] = len;
}
return newA;
}
// Appends two trees.
function append(a,b)
{
if (a.table.length === 0)
{
return b;
}
if (b.table.length === 0)
{
return a;
}
var c = append_(a, b);
// Check if both nodes can be crunshed together.
if (c[0].table.length + c[1].table.length <= M)
{
if (c[0].table.length === 0)
{
return c[1];
}
if (c[1].table.length === 0)
{
return c[0];
}
// Adjust .table and .lengths
c[0].table = c[0].table.concat(c[1].table);
if (c[0].height > 0)
{
var len = length(c[0]);
for (var i = 0; i < c[1].lengths.length; i++)
{
c[1].lengths[i] += len;
}
c[0].lengths = c[0].lengths.concat(c[1].lengths);
}
return c[0];
}
if (c[0].height > 0)
{
var toRemove = calcToRemove(a, b);
if (toRemove > E)
{
c = shuffle(c[0], c[1], toRemove);
}
}
return siblise(c[0], c[1]);
}
// Returns an array of two nodes; right and left. One node _may_ be empty.
function append_(a, b)
{
if (a.height === 0 && b.height === 0)
{
return [a, b];
}
if (a.height !== 1 || b.height !== 1)
{
if (a.height === b.height)
{
a = nodeCopy(a);
b = nodeCopy(b);
var appended = append_(botRight(a), botLeft(b));
insertRight(a, appended[1]);
insertLeft(b, appended[0]);
}
else if (a.height > b.height)
{
a = nodeCopy(a);
var appended = append_(botRight(a), b);
insertRight(a, appended[0]);
b = parentise(appended[1], appended[1].height + 1);
}
else
{
b = nodeCopy(b);
var appended = append_(a, botLeft(b));
var left = appended[0].table.length === 0 ? 0 : 1;
var right = left === 0 ? 1 : 0;
insertLeft(b, appended[left]);
a = parentise(appended[right], appended[right].height + 1);
}
}
// Check if balancing is needed and return based on that.
if (a.table.length === 0 || b.table.length === 0)
{
return [a, b];
}
var toRemove = calcToRemove(a, b);
if (toRemove <= E)
{
return [a, b];
}
return shuffle(a, b, toRemove);
}
// Helperfunctions for append_. Replaces a child node at the side of the parent.
function insertRight(parent, node)
{
var index = parent.table.length - 1;
parent.table[index] = node;
parent.lengths[index] = length(node);
parent.lengths[index] += index > 0 ? parent.lengths[index - 1] : 0;
}
function insertLeft(parent, node)
{
if (node.table.length > 0)
{
parent.table[0] = node;
parent.lengths[0] = length(node);
var len = length(parent.table[0]);
for (var i = 1; i < parent.lengths.length; i++)
{
len += length(parent.table[i]);
parent.lengths[i] = len;
}
}
else
{
parent.table.shift();
for (var i = 1; i < parent.lengths.length; i++)
{
parent.lengths[i] = parent.lengths[i] - parent.lengths[0];
}
parent.lengths.shift();
}
}
// Returns the extra search steps for E. Refer to the paper.
function calcToRemove(a, b)
{
var subLengths = 0;
for (var i = 0; i < a.table.length; i++)
{
subLengths += a.table[i].table.length;
}
for (var i = 0; i < b.table.length; i++)
{
subLengths += b.table[i].table.length;
}
var toRemove = a.table.length + b.table.length;
return toRemove - (Math.floor((subLengths - 1) / M) + 1);
}
// get2, set2 and saveSlot are helpers for accessing elements over two arrays.
function get2(a, b, index)
{
return index < a.length
? a[index]
: b[index - a.length];
}
function set2(a, b, index, value)
{
if (index < a.length)
{
a[index] = value;
}
else
{
b[index - a.length] = value;
}
}
function saveSlot(a, b, index, slot)
{
set2(a.table, b.table, index, slot);
var l = (index === 0 || index === a.lengths.length)
? 0
: get2(a.lengths, a.lengths, index - 1);
set2(a.lengths, b.lengths, index, l + length(slot));
}
// Creates a node or leaf with a given length at their arrays for perfomance.
// Is only used by shuffle.
function createNode(h, length)
{
if (length < 0)
{
length = 0;
}
var a = {
ctor: '_Array',
height: h,
table: new Array(length)
};
if (h > 0)
{
a.lengths = new Array(length);
}
return a;
}
// Returns an array of two balanced nodes.
function shuffle(a, b, toRemove)
{
var newA = createNode(a.height, Math.min(M, a.table.length + b.table.length - toRemove));
var newB = createNode(a.height, newA.table.length - (a.table.length + b.table.length - toRemove));
// Skip the slots with size M. More precise: copy the slot references
// to the new node
var read = 0;
while (get2(a.table, b.table, read).table.length % M === 0)
{
set2(newA.table, newB.table, read, get2(a.table, b.table, read));
set2(newA.lengths, newB.lengths, read, get2(a.lengths, b.lengths, read));
read++;
}
// Pulling items from left to right, caching in a slot before writing
// it into the new nodes.
var write = read;
var slot = new createNode(a.height - 1, 0);
var from = 0;
// If the current slot is still containing data, then there will be at
// least one more write, so we do not break this loop yet.
while (read - write - (slot.table.length > 0 ? 1 : 0) < toRemove)
{
// Find out the max possible items for copying.
var source = get2(a.table, b.table, read);
var to = Math.min(M - slot.table.length, source.table.length);
// Copy and adjust size table.
slot.table = slot.table.concat(source.table.slice(from, to));
if (slot.height > 0)
{
var len = slot.lengths.length;
for (var i = len; i < len + to - from; i++)
{
slot.lengths[i] = length(slot.table[i]);
slot.lengths[i] += (i > 0 ? slot.lengths[i - 1] : 0);
}
}
from += to;
// Only proceed to next slots[i] if the current one was
// fully copied.
if (source.table.length <= to)
{
read++; from = 0;
}
// Only create a new slot if the current one is filled up.
if (slot.table.length === M)
{
saveSlot(newA, newB, write, slot);
slot = createNode(a.height - 1, 0);
write++;
}
}
// Cleanup after the loop. Copy the last slot into the new nodes.
if (slot.table.length > 0)
{
saveSlot(newA, newB, write, slot);
write++;
}
// Shift the untouched slots to the left
while (read < a.table.length + b.table.length )
{
saveSlot(newA, newB, write, get2(a.table, b.table, read));
read++;
write++;
}
return [newA, newB];
}
// Navigation functions
function botRight(a)
{
return a.table[a.table.length - 1];
}
function botLeft(a)
{
return a.table[0];
}
// Copies a node for updating. Note that you should not use this if
// only updating only one of "table" or "lengths" for performance reasons.
function nodeCopy(a)
{
var newA = {
ctor: '_Array',
height: a.height,
table: a.table.slice()
};
if (a.height > 0)
{
newA.lengths = a.lengths.slice();
}
return newA;
}
// Returns how many items are in the tree.
function length(array)
{
if (array.height === 0)
{
return array.table.length;
}
else
{
return array.lengths[array.lengths.length - 1];
}
}
// Calculates in which slot of "table" the item probably is, then
// find the exact slot via forward searching in "lengths". Returns the index.
function getSlot(i, a)
{
var slot = i >> (5 * a.height);
while (a.lengths[slot] <= i)
{
slot++;
}
return slot;
}
// Recursively creates a tree with a given height containing
// only the given item.
function create(item, h)
{
if (h === 0)
{
return {
ctor: '_Array',
height: 0,
table: [item]
};
}
return {
ctor: '_Array',
height: h,
table: [create(item, h - 1)],
lengths: [1]
};
}
// Recursively creates a tree that contains the given tree.
function parentise(tree, h)
{
if (h === tree.height)