@calcit/ternary-tree 0.0.17 → 0.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/bundle.js DELETED
@@ -1,2988 +0,0 @@
1
- /******/ (() => { // webpackBootstrap
2
- /******/ "use strict";
3
- /******/ var __webpack_modules__ = ({
4
-
5
- /***/ "./lib/list.js":
6
- /*!*********************!*\
7
- !*** ./lib/list.js ***!
8
- \*********************/
9
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
10
-
11
- __webpack_require__.r(__webpack_exports__);
12
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
13
- /* harmony export */ "getDepth": () => (/* binding */ getDepth),
14
- /* harmony export */ "makeTernaryTreeList": () => (/* binding */ makeTernaryTreeList),
15
- /* harmony export */ "initTernaryTreeList": () => (/* binding */ initTernaryTreeList),
16
- /* harmony export */ "initTernaryTreeListFromRange": () => (/* binding */ initTernaryTreeListFromRange),
17
- /* harmony export */ "initEmptyTernaryTreeList": () => (/* binding */ initEmptyTernaryTreeList),
18
- /* harmony export */ "listToString": () => (/* binding */ listToString),
19
- /* harmony export */ "listLen": () => (/* binding */ listLen),
20
- /* harmony export */ "formatListInline": () => (/* binding */ formatListInline),
21
- /* harmony export */ "listToItems": () => (/* binding */ listToItems),
22
- /* harmony export */ "findIndex": () => (/* binding */ findIndex),
23
- /* harmony export */ "indexOf": () => (/* binding */ indexOf),
24
- /* harmony export */ "indexToItems": () => (/* binding */ indexToItems),
25
- /* harmony export */ "listToPairs": () => (/* binding */ listToPairs),
26
- /* harmony export */ "listGet": () => (/* binding */ listGet),
27
- /* harmony export */ "first": () => (/* binding */ first),
28
- /* harmony export */ "last": () => (/* binding */ last),
29
- /* harmony export */ "assocList": () => (/* binding */ assocList),
30
- /* harmony export */ "dissocList": () => (/* binding */ dissocList),
31
- /* harmony export */ "rest": () => (/* binding */ rest),
32
- /* harmony export */ "butlast": () => (/* binding */ butlast),
33
- /* harmony export */ "insert": () => (/* binding */ insert),
34
- /* harmony export */ "assocBefore": () => (/* binding */ assocBefore),
35
- /* harmony export */ "assocAfter": () => (/* binding */ assocAfter),
36
- /* harmony export */ "forceListInplaceBalancing": () => (/* binding */ forceListInplaceBalancing),
37
- /* harmony export */ "prepend": () => (/* binding */ prepend),
38
- /* harmony export */ "append": () => (/* binding */ append),
39
- /* harmony export */ "concat": () => (/* binding */ concat),
40
- /* harmony export */ "sameListShape": () => (/* binding */ sameListShape),
41
- /* harmony export */ "listEqual": () => (/* binding */ listEqual),
42
- /* harmony export */ "checkListStructure": () => (/* binding */ checkListStructure),
43
- /* harmony export */ "slice": () => (/* binding */ slice),
44
- /* harmony export */ "reverse": () => (/* binding */ reverse),
45
- /* harmony export */ "listMapValues": () => (/* binding */ listMapValues)
46
- /* harmony export */ });
47
- /* harmony import */ var _types__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./types */ "./lib/types.js");
48
- /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils */ "./lib/utils.js");
49
-
50
-
51
- // just get, will not compute recursively
52
- function getDepth(tree) {
53
- if (tree == null)
54
- return 0;
55
- switch (tree.kind) {
56
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
57
- return 1;
58
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
59
- return tree.depth;
60
- }
61
- }
62
- let emptyBranch = null;
63
- let isEmptyBranch = (x) => {
64
- if (x == null) {
65
- return true;
66
- }
67
- return x.size == 0;
68
- };
69
- function decideParentDepth(...xs) {
70
- let depth = 0;
71
- for (let i = 0; i < xs.length; i++) {
72
- let x = xs[i];
73
- let y = getDepth(x);
74
- if (y > depth) {
75
- depth = y;
76
- }
77
- }
78
- return depth + 1;
79
- }
80
- function makeTernaryTreeList(size, offset, xs) {
81
- switch (size) {
82
- case 0: {
83
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch, size: 0, depth: 1, left: emptyBranch, middle: emptyBranch, right: emptyBranch };
84
- }
85
- case 1:
86
- return xs[offset];
87
- case 2: {
88
- let left = xs[offset];
89
- let middle = xs[offset + 1];
90
- let result = {
91
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
92
- size: listLen(left) + listLen(middle),
93
- left: left,
94
- middle: middle,
95
- right: emptyBranch,
96
- depth: decideParentDepth(left, middle),
97
- };
98
- return result;
99
- }
100
- case 3: {
101
- let left = xs[offset];
102
- let middle = xs[offset + 1];
103
- let right = xs[offset + 2];
104
- let result = {
105
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
106
- size: listLen(left) + listLen(middle) + listLen(right),
107
- left: left,
108
- middle: middle,
109
- right: right,
110
- depth: decideParentDepth(left, middle, right),
111
- };
112
- return result;
113
- }
114
- default: {
115
- let divided = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.divideTernarySizes)(size);
116
- let left = makeTernaryTreeList(divided.left, offset, xs);
117
- let middle = makeTernaryTreeList(divided.middle, offset + divided.left, xs);
118
- let right = makeTernaryTreeList(divided.right, offset + divided.left + divided.middle, xs);
119
- let result = {
120
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
121
- size: listLen(left) + listLen(middle) + listLen(right),
122
- depth: decideParentDepth(left, middle, right),
123
- left: left,
124
- middle: middle,
125
- right: right,
126
- };
127
- return result;
128
- }
129
- }
130
- }
131
- function initTernaryTreeList(xs) {
132
- let ys = new Array(xs.length);
133
- let size = xs.length;
134
- for (let idx = 0; idx < size; idx++) {
135
- let x = xs[idx];
136
- ys[idx] = { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: x };
137
- }
138
- return makeTernaryTreeList(xs.length, 0, ys);
139
- }
140
- // from a slice of an existed array
141
- function initTernaryTreeListFromRange(xs, from, to) {
142
- let ys = new Array(xs.length);
143
- for (let idx = from; idx < to; idx++) {
144
- let x = xs[idx];
145
- ys[idx] = { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: x };
146
- }
147
- return makeTernaryTreeList(xs.length, 0, ys);
148
- }
149
- function initEmptyTernaryTreeList() {
150
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch, size: 0, depth: 1, middle: emptyBranch, left: emptyBranch, right: emptyBranch };
151
- }
152
- function listToString(tree) {
153
- return `TernaryTreeList[${tree.size}, ...]`;
154
- }
155
- function listLen(tree) {
156
- if (tree == null) {
157
- return 0;
158
- }
159
- else {
160
- return tree.size;
161
- }
162
- }
163
- function isLeaf(tree) {
164
- return tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf;
165
- }
166
- function isBranch(tree) {
167
- return tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch;
168
- }
169
- function formatListInline(tree) {
170
- if (tree == null) {
171
- return "_";
172
- }
173
- switch (tree.kind) {
174
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
175
- return `${tree.value}`;
176
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
177
- return "(" + formatListInline(tree.left) + " " + formatListInline(tree.middle) + " " + formatListInline(tree.right) + ")";
178
- // "(" & tree.left.formatListInline & " " & tree.middle.formatListInline & " " & tree.right.formatListInline & ")@{tree.depth} " & "{tree.left.getDepth} {tree.middle.getDepth} {tree.right.getDepth}..."
179
- }
180
- }
181
- function* listToItems(tree) {
182
- if (tree != null) {
183
- switch (tree.kind) {
184
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
185
- yield tree.value;
186
- break;
187
- }
188
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
189
- if (tree.left != null) {
190
- for (let x of listToItems(tree.left)) {
191
- yield x;
192
- }
193
- }
194
- if (tree.middle != null) {
195
- for (let x of listToItems(tree.middle)) {
196
- yield x;
197
- }
198
- }
199
- if (tree.right != null) {
200
- for (let x of listToItems(tree.right)) {
201
- yield x;
202
- }
203
- }
204
- break;
205
- }
206
- }
207
- }
208
- }
209
- // returns -1 if (not foun)
210
- function findIndex(tree, f) {
211
- if (tree == null) {
212
- return -1;
213
- }
214
- switch (tree.kind) {
215
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
216
- if (f(tree.value)) {
217
- return 0;
218
- }
219
- else {
220
- return -1;
221
- }
222
- }
223
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
224
- let tryLeft = findIndex(tree.left, f);
225
- if (tryLeft >= 0) {
226
- return tryLeft;
227
- }
228
- let tryMiddle = findIndex(tree.middle, f);
229
- if (tryMiddle >= 0) {
230
- return tryMiddle + listLen(tree.left);
231
- }
232
- let tryRight = findIndex(tree.right, f);
233
- if (tryRight >= 0) {
234
- return tryRight + listLen(tree.left) + listLen(tree.middle);
235
- }
236
- return -1;
237
- }
238
- }
239
- }
240
- // returns -1 if (not foun)
241
- function indexOf(tree, item) {
242
- if (tree == null) {
243
- return -1;
244
- }
245
- switch (tree.kind) {
246
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
247
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(item, tree.value)) {
248
- return 0;
249
- }
250
- default:
251
- return -1;
252
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
253
- let tryLeft = indexOf(tree.left, item);
254
- if (tryLeft >= 0) {
255
- return tryLeft;
256
- }
257
- let tryMiddle = indexOf(tree.middle, item);
258
- if (tryMiddle >= 0) {
259
- return tryMiddle + listLen(tree.left);
260
- }
261
- let tryRight = indexOf(tree.right, item);
262
- if (tryRight >= 0) {
263
- return tryRight + listLen(tree.left) + listLen(tree.middle);
264
- }
265
- return -1;
266
- }
267
- }
268
- function writeLeavesArray(tree, acc, idx) {
269
- if (tree == null) {
270
- //
271
- }
272
- else {
273
- switch (tree.kind) {
274
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
275
- acc[idx.value] = tree;
276
- idx.value = idx.value + 1;
277
- break;
278
- }
279
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
280
- if (tree.left != null) {
281
- writeLeavesArray(tree.left, acc, idx);
282
- }
283
- if (tree.middle != null) {
284
- writeLeavesArray(tree.middle, acc, idx);
285
- }
286
- if (tree.right != null) {
287
- writeLeavesArray(tree.right, acc, idx);
288
- }
289
- break;
290
- }
291
- default: {
292
- throw new Error("Unknown");
293
- }
294
- }
295
- }
296
- }
297
- function toLeavesArray(tree) {
298
- let acc = new Array(listLen(tree));
299
- let counter = { value: 0 };
300
- writeLeavesArray(tree, acc, counter);
301
- return acc;
302
- }
303
- function* indexToItems(tree) {
304
- for (let idx = 0; idx < listLen(tree); idx++) {
305
- yield listGet(tree, idx);
306
- }
307
- }
308
- function* listToPairs(tree) {
309
- let idx = 0;
310
- for (let x of listToItems(tree)) {
311
- yield [idx, x];
312
- idx = idx + 1;
313
- }
314
- }
315
- function listGet(originalTree, originalIdx) {
316
- let tree = originalTree;
317
- let idx = originalIdx;
318
- while (tree != null) {
319
- if (idx < 0) {
320
- throw new Error("Cannot index negative number");
321
- }
322
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
323
- if (idx === 0) {
324
- return tree.value;
325
- }
326
- else {
327
- throw new Error(`Cannot get from leaf with index ${idx}`);
328
- }
329
- }
330
- if (idx > tree.size - 1) {
331
- throw new Error("Index too large");
332
- }
333
- let leftSize = tree.left == null ? 0 : tree.left.size;
334
- let middleSize = tree.middle == null ? 0 : tree.middle.size;
335
- let rightSize = tree.right == null ? 0 : tree.right.size;
336
- if (leftSize + middleSize + rightSize !== tree.size) {
337
- throw new Error("tree.size does not match sum case branch sizes");
338
- }
339
- if (idx <= leftSize - 1) {
340
- tree = tree.left;
341
- }
342
- else if (idx <= leftSize + middleSize - 1) {
343
- tree = tree.middle;
344
- idx = idx - leftSize;
345
- }
346
- else {
347
- tree = tree.right;
348
- idx = idx - leftSize - middleSize;
349
- }
350
- }
351
- throw new Error(`Failed to get ${idx}`);
352
- }
353
- function first(tree) {
354
- if (listLen(tree) > 0) {
355
- return listGet(tree, 0);
356
- }
357
- else {
358
- throw new Error("Cannot get from empty list");
359
- }
360
- }
361
- function last(tree) {
362
- if (listLen(tree) > 0) {
363
- return listGet(tree, listLen(tree) - 1);
364
- }
365
- else {
366
- throw new Error("Cannot get from empty list");
367
- }
368
- }
369
- function assocList(tree, idx, item) {
370
- if (idx < 0) {
371
- throw new Error("Cannot index negative number");
372
- }
373
- if (idx > tree.size - 1) {
374
- throw new Error("Index too large");
375
- }
376
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
377
- if (idx === 0) {
378
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item };
379
- }
380
- else {
381
- throw new Error(`Cannot get from leaf with index ${idx}`);
382
- }
383
- }
384
- let leftSize = listLen(tree.left);
385
- let middleSize = listLen(tree.middle);
386
- let rightSize = listLen(tree.right);
387
- if (leftSize + middleSize + rightSize !== tree.size)
388
- throw new Error("tree.size does not match sum case branch sizes");
389
- if (idx <= leftSize - 1) {
390
- let changedBranch = assocList(tree.left, idx, item);
391
- let result = {
392
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
393
- size: tree.size,
394
- depth: decideParentDepth(changedBranch, tree.middle, tree.right),
395
- left: changedBranch,
396
- middle: tree.middle,
397
- right: tree.right,
398
- };
399
- return result;
400
- }
401
- else if (idx <= leftSize + middleSize - 1) {
402
- let changedBranch = assocList(tree.middle, idx - leftSize, item);
403
- let result = {
404
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
405
- size: tree.size,
406
- depth: decideParentDepth(tree.left, changedBranch, tree.right),
407
- left: tree.left,
408
- middle: changedBranch,
409
- right: tree.right,
410
- };
411
- return result;
412
- }
413
- else {
414
- let changedBranch = assocList(tree.right, idx - leftSize - middleSize, item);
415
- let result = {
416
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
417
- size: tree.size,
418
- depth: decideParentDepth(tree.left, tree.middle, changedBranch),
419
- left: tree.left,
420
- middle: tree.middle,
421
- right: changedBranch,
422
- };
423
- return result;
424
- }
425
- }
426
- function dissocList(tree, idx) {
427
- if (tree == null) {
428
- throw new Error("dissoc does not work on null");
429
- }
430
- if (idx < 0) {
431
- throw new Error(`Index is negative ${idx}`);
432
- }
433
- if (listLen(tree) === 0) {
434
- throw new Error("Cannot remove from empty list");
435
- }
436
- if (idx > listLen(tree) - 1) {
437
- throw new Error(`Index too large ${idx}`);
438
- }
439
- if (listLen(tree) === 1) {
440
- return emptyBranch;
441
- }
442
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
443
- throw new Error("dissoc should be handled at branches");
444
- }
445
- let leftSize = listLen(tree.left);
446
- let middleSize = listLen(tree.middle);
447
- let rightSize = listLen(tree.right);
448
- if (leftSize + middleSize + rightSize !== tree.size) {
449
- throw new Error("tree.size does not match sum from branch sizes");
450
- }
451
- let result = emptyBranch;
452
- if (idx <= leftSize - 1) {
453
- let changedBranch = dissocList(tree.left, idx);
454
- if (changedBranch == null || changedBranch.size === 0) {
455
- result = {
456
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
457
- size: tree.size - 1,
458
- depth: decideParentDepth(tree.middle, tree.right),
459
- left: tree.middle,
460
- middle: tree.right,
461
- right: emptyBranch,
462
- };
463
- }
464
- else {
465
- result = {
466
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
467
- size: tree.size - 1,
468
- depth: decideParentDepth(changedBranch, tree.middle, tree.right),
469
- left: changedBranch,
470
- middle: tree.middle,
471
- right: tree.right,
472
- };
473
- }
474
- }
475
- else if (idx <= leftSize + middleSize - 1) {
476
- let changedBranch = dissocList(tree.middle, idx - leftSize);
477
- if (changedBranch == null || changedBranch.size === 0) {
478
- result = {
479
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
480
- size: tree.size - 1,
481
- depth: decideParentDepth(tree.left, changedBranch, tree.right),
482
- left: tree.left,
483
- middle: tree.right,
484
- right: emptyBranch,
485
- };
486
- }
487
- else {
488
- result = {
489
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
490
- size: tree.size - 1,
491
- depth: decideParentDepth(tree.left, changedBranch, tree.right),
492
- left: tree.left,
493
- middle: changedBranch,
494
- right: tree.right,
495
- };
496
- }
497
- }
498
- else {
499
- let changedBranch = dissocList(tree.right, idx - leftSize - middleSize);
500
- if (changedBranch == null || changedBranch.size === 0) {
501
- changedBranch = emptyBranch;
502
- }
503
- result = {
504
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
505
- size: tree.size - 1,
506
- depth: decideParentDepth(tree.left, tree.middle, changedBranch),
507
- left: tree.left,
508
- middle: tree.middle,
509
- right: changedBranch,
510
- };
511
- }
512
- if (result.middle == null) {
513
- return result.left;
514
- }
515
- return result;
516
- }
517
- function rest(tree) {
518
- if (tree == null) {
519
- throw new Error("Cannot call rest on null");
520
- }
521
- if (listLen(tree) < 1) {
522
- throw new Error("Cannot call rest on empty list");
523
- }
524
- return dissocList(tree, 0);
525
- }
526
- function butlast(tree) {
527
- if (tree == null) {
528
- throw new Error("Cannot call butlast on null");
529
- }
530
- if (listLen(tree) < 1) {
531
- throw new Error("Cannot call butlast on empty list");
532
- }
533
- return dissocList(tree, listLen(tree) - 1);
534
- }
535
- function insert(tree, idx, item, after = false) {
536
- if (tree == null) {
537
- throw new Error("Cannot insert into null");
538
- }
539
- if (listLen(tree) === 0) {
540
- throw new Error("Empty node is not a correct position for inserting");
541
- }
542
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
543
- if (after) {
544
- let result = {
545
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
546
- depth: getDepth(tree) + 1,
547
- size: 2,
548
- left: tree,
549
- middle: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
550
- right: emptyBranch,
551
- };
552
- return result;
553
- }
554
- else {
555
- let result = {
556
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
557
- depth: getDepth(tree) + 1,
558
- size: 2,
559
- left: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
560
- middle: tree,
561
- right: emptyBranch,
562
- };
563
- return result;
564
- }
565
- }
566
- if (listLen(tree) === 1) {
567
- if (after) {
568
- // in compact mode, values placed at left
569
- let result = {
570
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
571
- size: 2,
572
- depth: 2,
573
- left: tree.left,
574
- middle: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
575
- right: emptyBranch,
576
- };
577
- return result;
578
- }
579
- else {
580
- let result = {
581
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
582
- size: 2,
583
- depth: getDepth(tree.middle) + 1,
584
- left: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
585
- middle: tree.left,
586
- right: emptyBranch,
587
- };
588
- return result;
589
- }
590
- }
591
- if (listLen(tree) === 2 && tree.middle != null) {
592
- if (after) {
593
- if (idx === 0) {
594
- let result = {
595
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
596
- size: 3,
597
- depth: 2,
598
- left: tree.left,
599
- middle: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
600
- right: tree.middle,
601
- };
602
- return result;
603
- }
604
- if (idx === 1) {
605
- let result = {
606
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
607
- size: 3,
608
- depth: 2,
609
- left: tree.left,
610
- middle: tree.middle,
611
- right: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
612
- };
613
- return result;
614
- }
615
- else {
616
- throw new Error("cannot insert after position 2 since only 2 elements here");
617
- }
618
- }
619
- else {
620
- if (idx === 0) {
621
- let result = {
622
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
623
- size: 3,
624
- depth: 2,
625
- left: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
626
- middle: tree.left,
627
- right: tree.middle,
628
- };
629
- return result;
630
- }
631
- else if (idx === 1) {
632
- let result = {
633
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
634
- size: 3,
635
- depth: 2,
636
- left: tree.left,
637
- middle: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
638
- right: tree.middle,
639
- };
640
- return result;
641
- }
642
- else {
643
- throw new Error("cannot insert before position 2 since only 2 elements here");
644
- }
645
- }
646
- }
647
- let leftSize = listLen(tree.left);
648
- let middleSize = listLen(tree.middle);
649
- let rightSize = listLen(tree.right);
650
- if (leftSize + middleSize + rightSize !== tree.size) {
651
- throw new Error("tree.size does not match sum case branch sizes");
652
- }
653
- // echo "picking: ", idx, " ", leftSize, " ", middleSize, " ", rightSize
654
- if (idx === 0 && !after) {
655
- if (listLen(tree.left) >= listLen(tree.middle) && listLen(tree.left) >= listLen(tree.right)) {
656
- let result = {
657
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
658
- size: tree.size + 1,
659
- depth: tree.depth + 1,
660
- left: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
661
- middle: tree,
662
- right: emptyBranch,
663
- };
664
- return result;
665
- }
666
- }
667
- if (idx === listLen(tree) - 1 && after) {
668
- if (listLen(tree.right) >= listLen(tree.middle) && listLen(tree.right) >= listLen(tree.left)) {
669
- let result = {
670
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
671
- size: tree.size + 1,
672
- depth: tree.depth + 1,
673
- left: tree,
674
- middle: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
675
- right: emptyBranch,
676
- };
677
- return result;
678
- }
679
- }
680
- if (after && idx === listLen(tree) - 1 && rightSize === 0 && middleSize >= leftSize) {
681
- let result = {
682
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
683
- size: tree.size + 1,
684
- depth: tree.depth,
685
- left: tree.left,
686
- middle: tree.middle,
687
- right: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
688
- };
689
- return result;
690
- }
691
- if (!after && idx === 0 && rightSize === 0 && middleSize >= rightSize) {
692
- let result = {
693
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
694
- size: tree.size + 1,
695
- depth: tree.depth,
696
- left: { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
697
- middle: tree.left,
698
- right: tree.middle,
699
- };
700
- return result;
701
- }
702
- if (idx <= leftSize - 1) {
703
- let changedBranch = insert(tree.left, idx, item, after);
704
- let result = {
705
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
706
- size: tree.size + 1,
707
- depth: decideParentDepth(changedBranch, tree.middle, tree.right),
708
- left: changedBranch,
709
- middle: tree.middle,
710
- right: tree.right,
711
- };
712
- return result;
713
- }
714
- else if (idx <= leftSize + middleSize - 1) {
715
- let changedBranch = insert(tree.middle, idx - leftSize, item, after);
716
- let result = {
717
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
718
- size: tree.size + 1,
719
- depth: decideParentDepth(tree.left, changedBranch, tree.right),
720
- left: tree.left,
721
- middle: changedBranch,
722
- right: tree.right,
723
- };
724
- return result;
725
- }
726
- else {
727
- let changedBranch = insert(tree.right, idx - leftSize - middleSize, item, after);
728
- let result = {
729
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
730
- size: tree.size + 1,
731
- depth: decideParentDepth(tree.left, tree.middle, changedBranch),
732
- left: tree.left,
733
- middle: tree.middle,
734
- right: changedBranch,
735
- };
736
- return result;
737
- }
738
- }
739
- function assocBefore(tree, idx, item, after = false) {
740
- return insert(tree, idx, item, false);
741
- }
742
- function assocAfter(tree, idx, item, after = false) {
743
- return insert(tree, idx, item, true);
744
- }
745
- // this function mutates original tree to make it more balanced
746
- function forceListInplaceBalancing(tree) {
747
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch) {
748
- // echo "Force inplace balancing case list: ", tree.size
749
- let ys = toLeavesArray(tree);
750
- let newTree = makeTernaryTreeList(ys.length, 0, ys);
751
- // let newTree = initTernaryTreeList(ys)
752
- tree.left = newTree.left;
753
- tree.middle = newTree.middle;
754
- tree.right = newTree.right;
755
- tree.depth = decideParentDepth(tree.left, tree.middle, tree.right);
756
- }
757
- else {
758
- //
759
- }
760
- }
761
- // TODO, need better strategy for detecting
762
- function maybeReblance(tree) {
763
- let currentDepth = getDepth(tree);
764
- if (currentDepth > 50) {
765
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.roughIntPow)(3, currentDepth - 50) > tree.size) {
766
- forceListInplaceBalancing(tree);
767
- }
768
- }
769
- }
770
- function prepend(tree, item, disableBalancing = false) {
771
- if (tree == null || listLen(tree) === 0) {
772
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item };
773
- }
774
- let result = insert(tree, 0, item, false);
775
- if (!disableBalancing) {
776
- maybeReblance(result);
777
- }
778
- return result;
779
- }
780
- function append(tree, item, disableBalancing = false) {
781
- if (tree == null || listLen(tree) === 0) {
782
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item };
783
- }
784
- let result = insert(tree, listLen(tree) - 1, item, true);
785
- if (!disableBalancing) {
786
- maybeReblance(result);
787
- }
788
- return result;
789
- }
790
- function concat(...xsGroups) {
791
- xsGroups = xsGroups.filter((xs) => listLen(xs) > 0);
792
- let result = makeTernaryTreeList(xsGroups.length, 0, xsGroups);
793
- maybeReblance(result);
794
- return result;
795
- }
796
- function sameListShape(xs, ys) {
797
- if (xs == null) {
798
- if (ys == null) {
799
- return true;
800
- }
801
- else {
802
- return false;
803
- }
804
- }
805
- if (ys == null) {
806
- return false;
807
- }
808
- if (listLen(xs) !== listLen(ys)) {
809
- return false;
810
- }
811
- if (xs.kind !== ys.kind) {
812
- return false;
813
- }
814
- if (xs.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf && ys.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
815
- if (!(0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(xs.value, ys.value)) {
816
- return false;
817
- }
818
- else {
819
- return true;
820
- }
821
- }
822
- if (xs.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch && ys.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch) {
823
- if (!sameListShape(xs.left, ys.left))
824
- return false;
825
- if (!sameListShape(xs.middle, ys.middle))
826
- return false;
827
- if (!sameListShape(xs.right, ys.right))
828
- return false;
829
- return true;
830
- }
831
- return false;
832
- }
833
- function listEqual(xs, ys) {
834
- if (xs === ys) {
835
- return true;
836
- }
837
- if (listLen(xs) !== listLen(ys)) {
838
- return false;
839
- }
840
- for (let idx = 0; idx < listLen(xs); idx++) {
841
- if (!(0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(listGet(xs, idx), listGet(ys, idx))) {
842
- return false;
843
- }
844
- }
845
- return true;
846
- }
847
- function checkListStructure(tree) {
848
- if (tree == null) {
849
- return true;
850
- }
851
- else {
852
- switch (tree.kind) {
853
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
854
- if (tree.size !== 1) {
855
- throw new Error(`Bad size at node ${formatListInline(tree)}`);
856
- }
857
- break;
858
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
859
- if (tree.size !== listLen(tree.left) + listLen(tree.middle) + listLen(tree.right)) {
860
- throw new Error(`Bad size at branch ${formatListInline(tree)}`);
861
- }
862
- if (tree.depth !== decideParentDepth(tree.left, tree.middle, tree.right)) {
863
- let x = decideParentDepth(tree.left, tree.middle, tree.right);
864
- throw new Error(`Bad depth at branch ${formatListInline(tree)}`);
865
- }
866
- checkListStructure(tree.left);
867
- checkListStructure(tree.middle);
868
- checkListStructure(tree.right);
869
- break;
870
- }
871
- }
872
- return true;
873
- }
874
- }
875
- // excludes value at endIdx, kept aligned with JS & Clojure
876
- function slice(tree, startIdx, endIdx) {
877
- // echo "slice {tree.formatListInline}: {startIdx}..{endIdx}"
878
- if (endIdx > listLen(tree)) {
879
- throw new Error("Slice range too large {endIdx} for {tree}");
880
- }
881
- if (startIdx < 0) {
882
- throw new Error("Slice range too small {startIdx} for {tree}");
883
- }
884
- if (startIdx > endIdx) {
885
- throw new Error("Invalid slice range {startIdx}..{endIdx} for {tree}");
886
- }
887
- if (startIdx === endIdx) {
888
- return { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch, size: 0, depth: 0 };
889
- }
890
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf)
891
- if (startIdx === 0 && endIdx === 1) {
892
- return tree;
893
- }
894
- else {
895
- throw new Error(`Invalid slice range for a leaf: ${startIdx} ${endIdx}`);
896
- }
897
- if (startIdx === 0 && endIdx === listLen(tree)) {
898
- return tree;
899
- }
900
- let leftSize = listLen(tree.left);
901
- let middleSize = listLen(tree.middle);
902
- let rightSize = listLen(tree.right);
903
- // echo "sizes: {leftSize} {middleSize} {rightSize}"
904
- if (startIdx >= leftSize + middleSize) {
905
- return slice(tree.right, startIdx - leftSize - middleSize, endIdx - leftSize - middleSize);
906
- }
907
- if (startIdx >= leftSize)
908
- if (endIdx <= leftSize + middleSize) {
909
- return slice(tree.middle, startIdx - leftSize, endIdx - leftSize);
910
- }
911
- else {
912
- let middleCut = slice(tree.middle, startIdx - leftSize, middleSize);
913
- let rightCut = slice(tree.right, 0, endIdx - leftSize - middleSize);
914
- return concat(middleCut, rightCut);
915
- }
916
- if (endIdx <= leftSize) {
917
- return slice(tree.left, startIdx, endIdx);
918
- }
919
- if (endIdx <= leftSize + middleSize) {
920
- let leftCut = slice(tree.left, startIdx, leftSize);
921
- let middleCut = slice(tree.middle, 0, endIdx - leftSize);
922
- return concat(leftCut, middleCut);
923
- }
924
- if (endIdx <= leftSize + middleSize + rightSize) {
925
- let leftCut = slice(tree.left, startIdx, leftSize);
926
- let rightCut = slice(tree.right, 0, endIdx - leftSize - middleSize);
927
- return concat(concat(leftCut, tree.middle), rightCut);
928
- }
929
- throw new Error("Unknown");
930
- }
931
- function reverse(tree) {
932
- if (tree == null) {
933
- return tree;
934
- }
935
- switch (tree.kind) {
936
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
937
- return tree;
938
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
939
- let result = {
940
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
941
- size: tree.size,
942
- depth: tree.depth,
943
- left: reverse(tree.right),
944
- middle: reverse(tree.middle),
945
- right: reverse(tree.left),
946
- };
947
- return result;
948
- }
949
- }
950
- }
951
- function listMapValues(tree, f) {
952
- if (tree == null) {
953
- return tree;
954
- }
955
- switch (tree.kind) {
956
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
957
- let result = {
958
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
959
- size: tree.size,
960
- value: f(tree.value),
961
- };
962
- return result;
963
- }
964
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
965
- let result = {
966
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
967
- size: tree.size,
968
- depth: tree.depth,
969
- left: tree.left == null ? emptyBranch : listMapValues(tree.left, f),
970
- middle: tree.middle == null ? emptyBranch : listMapValues(tree.middle, f),
971
- right: tree.right == null ? emptyBranch : listMapValues(tree.right, f),
972
- };
973
- return result;
974
- }
975
- }
976
- }
977
-
978
-
979
- /***/ }),
980
-
981
- /***/ "./lib/map.js":
982
- /*!********************!*\
983
- !*** ./lib/map.js ***!
984
- \********************/
985
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
986
-
987
- __webpack_require__.r(__webpack_exports__);
988
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
989
- /* harmony export */ "getMapDepth": () => (/* binding */ getMapDepth),
990
- /* harmony export */ "initTernaryTreeMapFromHashEntries": () => (/* binding */ initTernaryTreeMapFromHashEntries),
991
- /* harmony export */ "initTernaryTreeMap": () => (/* binding */ initTernaryTreeMap),
992
- /* harmony export */ "initEmptyTernaryTreeMap": () => (/* binding */ initEmptyTernaryTreeMap),
993
- /* harmony export */ "mapToString": () => (/* binding */ mapToString),
994
- /* harmony export */ "mapLen": () => (/* binding */ mapLen),
995
- /* harmony export */ "mapLenBound": () => (/* binding */ mapLenBound),
996
- /* harmony export */ "formatMapInline": () => (/* binding */ formatMapInline),
997
- /* harmony export */ "isMapEmpty": () => (/* binding */ isMapEmpty),
998
- /* harmony export */ "isMapOfOne": () => (/* binding */ isMapOfOne),
999
- /* harmony export */ "toHashSortedPairs": () => (/* binding */ toHashSortedPairs),
1000
- /* harmony export */ "contains": () => (/* binding */ contains),
1001
- /* harmony export */ "mapGetDefault": () => (/* binding */ mapGetDefault),
1002
- /* harmony export */ "checkMapStructure": () => (/* binding */ checkMapStructure),
1003
- /* harmony export */ "assocMap": () => (/* binding */ assocMap),
1004
- /* harmony export */ "dissocMap": () => (/* binding */ dissocMap),
1005
- /* harmony export */ "toPairsArray": () => (/* binding */ toPairsArray),
1006
- /* harmony export */ "toPairs": () => (/* binding */ toPairs),
1007
- /* harmony export */ "toKeys": () => (/* binding */ toKeys),
1008
- /* harmony export */ "toValues": () => (/* binding */ toValues),
1009
- /* harmony export */ "mapEqual": () => (/* binding */ mapEqual),
1010
- /* harmony export */ "merge": () => (/* binding */ merge),
1011
- /* harmony export */ "mergeSkip": () => (/* binding */ mergeSkip),
1012
- /* harmony export */ "forceMapInplaceBalancing": () => (/* binding */ forceMapInplaceBalancing),
1013
- /* harmony export */ "sameMapShape": () => (/* binding */ sameMapShape),
1014
- /* harmony export */ "mapMapValues": () => (/* binding */ mapMapValues)
1015
- /* harmony export */ });
1016
- /* harmony import */ var _types__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./types */ "./lib/types.js");
1017
- /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils */ "./lib/utils.js");
1018
-
1019
-
1020
- let emptyBranch = null;
1021
- let nilResult = null;
1022
- function getMax(tree) {
1023
- if (tree == null) {
1024
- throw new Error("Cannot find max hash of nil");
1025
- }
1026
- switch (tree.kind) {
1027
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1028
- return tree.hash;
1029
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1030
- return tree.maxHash;
1031
- default:
1032
- throw new Error("Unknown");
1033
- }
1034
- }
1035
- function getMin(tree) {
1036
- if (tree == null) {
1037
- throw new Error("Cannot find min hash of nil");
1038
- }
1039
- switch (tree.kind) {
1040
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1041
- return tree.hash;
1042
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1043
- return tree.minHash;
1044
- default:
1045
- throw new Error("Unknown");
1046
- }
1047
- }
1048
- function getMapDepth(tree) {
1049
- // console.log( "calling...", tree)
1050
- if (tree == null) {
1051
- return 0;
1052
- }
1053
- switch (tree.kind) {
1054
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1055
- return 1;
1056
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1057
- return Math.max(getMapDepth(tree.left), getMapDepth(tree.middle), getMapDepth(tree.right)) + 1;
1058
- default:
1059
- throw new Error("Unknown");
1060
- }
1061
- }
1062
- function createLeaf(k, v) {
1063
- let result = {
1064
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1065
- hash: (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(k),
1066
- elements: [[k, v]],
1067
- };
1068
- return result;
1069
- }
1070
- function createLeafFromHashEntry(item) {
1071
- let result = {
1072
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1073
- hash: item.hash,
1074
- elements: item.pairs,
1075
- };
1076
- return result;
1077
- }
1078
- // this proc is not exported, pick up next proc as the entry.
1079
- // pairs must be sorted before passing to proc.
1080
- function makeTernaryTreeMap(size, offset, xs) {
1081
- switch (size) {
1082
- case 0: {
1083
- let result = emptyBranch;
1084
- return result;
1085
- }
1086
- case 1: {
1087
- let leftPair = xs[offset];
1088
- let result = createLeafFromHashEntry(leftPair);
1089
- return result;
1090
- }
1091
- case 2: {
1092
- let leftPair = xs[offset];
1093
- let middlePair = xs[offset + 1];
1094
- let result = {
1095
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1096
- maxHash: middlePair.hash,
1097
- minHash: leftPair.hash,
1098
- left: createLeafFromHashEntry(leftPair),
1099
- middle: createLeafFromHashEntry(middlePair),
1100
- right: emptyBranch,
1101
- depth: 1,
1102
- };
1103
- return result;
1104
- }
1105
- case 3: {
1106
- let leftPair = xs[offset];
1107
- let middlePair = xs[offset + 1];
1108
- let rightPair = xs[offset + 2];
1109
- let result = {
1110
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1111
- maxHash: rightPair.hash,
1112
- minHash: leftPair.hash,
1113
- left: createLeafFromHashEntry(leftPair),
1114
- middle: createLeafFromHashEntry(middlePair),
1115
- right: createLeafFromHashEntry(rightPair),
1116
- depth: 1,
1117
- };
1118
- return result;
1119
- }
1120
- default: {
1121
- let divided = (0,_utils__WEBPACK_IMPORTED_MODULE_1__.divideTernarySizes)(size);
1122
- let left = makeTernaryTreeMap(divided.left, offset, xs);
1123
- let middle = makeTernaryTreeMap(divided.middle, offset + divided.left, xs);
1124
- let right = makeTernaryTreeMap(divided.right, offset + divided.left + divided.middle, xs);
1125
- let result = {
1126
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1127
- maxHash: getMax(right),
1128
- minHash: getMin(left),
1129
- left: left,
1130
- middle: middle,
1131
- right: right,
1132
- depth: Math.max(getMapDepth(left), getMapDepth(middle), getMapDepth(right)) + 1,
1133
- };
1134
- return result;
1135
- }
1136
- }
1137
- }
1138
- function initTernaryTreeMapFromHashEntries(xs) {
1139
- return makeTernaryTreeMap(xs.length, 0, xs);
1140
- }
1141
- function initTernaryTreeMap(t) {
1142
- let groupBuffers = new Map();
1143
- for (let [k, v] of t) {
1144
- let h = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(k);
1145
- if (groupBuffers.has(h)) {
1146
- let branch = groupBuffers.get(h);
1147
- if (branch != null) {
1148
- branch.push([k, v]);
1149
- }
1150
- else {
1151
- throw new Error("Expected referece to pairs");
1152
- }
1153
- }
1154
- else {
1155
- groupBuffers.set(h, [[k, v]]);
1156
- }
1157
- }
1158
- let xs = [];
1159
- for (let [k, v] of groupBuffers) {
1160
- if (v != null) {
1161
- xs.push({
1162
- hash: k,
1163
- pairs: v,
1164
- });
1165
- }
1166
- else {
1167
- throw new Error("Expected reference to paris");
1168
- }
1169
- }
1170
- // MUTABLE in-place sort
1171
- xs.sort((a, b) => (0,_utils__WEBPACK_IMPORTED_MODULE_1__.cmp)(a.hash, b.hash));
1172
- let result = initTernaryTreeMapFromHashEntries(xs);
1173
- // checkMapStructure(result);
1174
- return result;
1175
- }
1176
- // for empty map
1177
- function initEmptyTernaryTreeMap() {
1178
- let result = emptyBranch;
1179
- return result;
1180
- }
1181
- function mapToString(tree) {
1182
- return `TernaryTreeMap[${mapLen(tree)}, ...]`;
1183
- }
1184
- function mapLen(tree) {
1185
- if (tree == null) {
1186
- return 0;
1187
- }
1188
- switch (tree.kind) {
1189
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1190
- return tree.elements.length;
1191
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1192
- return mapLen(tree.left) + mapLen(tree.middle) + mapLen(tree.right); // TODO
1193
- default:
1194
- throw new Error("Unknown");
1195
- }
1196
- }
1197
- // when size succeeds bound, no longer counting, faster than traversing whole tree
1198
- function mapLenBound(tree, bound) {
1199
- if (tree == null) {
1200
- return 0;
1201
- }
1202
- switch (tree.kind) {
1203
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1204
- return tree.elements.length;
1205
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1206
- let ret = mapLenBound(tree.left, bound);
1207
- if (ret > bound) {
1208
- return ret;
1209
- }
1210
- ret = ret + mapLenBound(tree.middle, bound);
1211
- if (ret > bound) {
1212
- return ret;
1213
- }
1214
- ret = ret + mapLenBound(tree.right, bound);
1215
- return ret;
1216
- default:
1217
- throw new Error("Unknown");
1218
- }
1219
- }
1220
- function formatMapInline(tree, withHash = false) {
1221
- if (tree == null) {
1222
- return "_";
1223
- }
1224
- switch (tree.kind) {
1225
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1226
- if (withHash) {
1227
- return `${tree.hash}->${tree.elements[0][0]}:${tree.elements[0][1]}`; // TODO show whole list
1228
- }
1229
- else {
1230
- return `${tree.elements[0][0]}:${tree.elements[0][1]}`;
1231
- }
1232
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
1233
- return "(" + formatMapInline(tree.left, withHash) + " " + formatMapInline(tree.middle, withHash) + " " + formatMapInline(tree.right, withHash) + ")";
1234
- }
1235
- default:
1236
- throw new Error("Unknown");
1237
- }
1238
- }
1239
- function isMapEmpty(tree) {
1240
- if (tree == null) {
1241
- return true;
1242
- }
1243
- switch (tree.kind) {
1244
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1245
- return false;
1246
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1247
- return tree.left == null && tree.middle == null && tree.right == null;
1248
- default:
1249
- throw new Error("Unknown");
1250
- }
1251
- }
1252
- function isMapOfOne(tree, counted = 0) {
1253
- if (tree == null) {
1254
- return true;
1255
- }
1256
- switch (tree.kind) {
1257
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf:
1258
- return false;
1259
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch:
1260
- return tree.left == null && tree.middle == null && tree.right == null;
1261
- default:
1262
- throw new Error("Unknown");
1263
- }
1264
- }
1265
- function collectHashSortedArray(tree, acc, idx) {
1266
- if (tree == null || isMapEmpty(tree)) {
1267
- // discard
1268
- }
1269
- else {
1270
- switch (tree.kind) {
1271
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
1272
- for (let i = 0; i < tree.elements.length; i++) {
1273
- let item = tree.elements[i];
1274
- acc[idx.value] = item;
1275
- idx.value = idx.value + 1;
1276
- }
1277
- break;
1278
- }
1279
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
1280
- collectHashSortedArray(tree.left, acc, idx);
1281
- collectHashSortedArray(tree.middle, acc, idx);
1282
- collectHashSortedArray(tree.right, acc, idx);
1283
- break;
1284
- }
1285
- default:
1286
- throw new Error("Unknown");
1287
- }
1288
- }
1289
- }
1290
- // sorted by hash(tree.key)
1291
- function toHashSortedPairs(tree) {
1292
- let acc = new Array(mapLen(tree));
1293
- let idx = { value: 0 };
1294
- collectHashSortedArray(tree, acc, idx);
1295
- return acc;
1296
- }
1297
- function collectOrderedHashEntries(tree, acc, idx) {
1298
- if (tree == null || isMapEmpty(tree)) {
1299
- // discard
1300
- }
1301
- else {
1302
- switch (tree.kind) {
1303
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
1304
- acc[idx.value] = { hash: tree.hash, pairs: tree.elements };
1305
- idx.value = idx.value + 1;
1306
- break;
1307
- }
1308
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
1309
- collectOrderedHashEntries(tree.left, acc, idx);
1310
- collectOrderedHashEntries(tree.middle, acc, idx);
1311
- collectOrderedHashEntries(tree.right, acc, idx);
1312
- break;
1313
- }
1314
- default: {
1315
- throw new Error("Unknown");
1316
- }
1317
- }
1318
- }
1319
- }
1320
- // for reusing leaves during rebalancing
1321
- function toOrderedHashEntries(tree) {
1322
- let acc = new Array(mapLen(tree));
1323
- let idx = { value: 0 };
1324
- collectOrderedHashEntries(tree, acc, idx);
1325
- return acc;
1326
- }
1327
- function contains(originalTree, item) {
1328
- if (originalTree == null) {
1329
- return false;
1330
- }
1331
- // TODO
1332
- // reduce redundant computation by reusing hash result
1333
- let hx = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(item);
1334
- let tree = originalTree;
1335
- whileLoop: while (tree != null) {
1336
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1337
- if (hx === tree.hash) {
1338
- let size = tree.elements.length;
1339
- for (let idx = 0; idx < size; idx++) {
1340
- let pair = tree.elements[idx];
1341
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(pair[0], item)) {
1342
- return true;
1343
- }
1344
- }
1345
- }
1346
- return false;
1347
- }
1348
- // echo "looking for: ", hx, " ", item, " in ", tree.formatInline(true)
1349
- if (tree.left == null) {
1350
- return false;
1351
- }
1352
- if (tree.left.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1353
- if (hx < tree.left.hash) {
1354
- return false;
1355
- }
1356
- if (tree.left.hash === hx) {
1357
- tree = tree.left;
1358
- continue whileLoop; // notice, it jumps to while loop
1359
- }
1360
- }
1361
- else {
1362
- if (hx < tree.left.minHash) {
1363
- return false;
1364
- }
1365
- if (hx <= tree.left.maxHash) {
1366
- tree = tree.left;
1367
- continue whileLoop; // notice, it jumps to while loop
1368
- }
1369
- }
1370
- if (tree.middle == null) {
1371
- return false;
1372
- }
1373
- if (tree.middle.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1374
- if (hx < tree.middle.hash) {
1375
- return false;
1376
- }
1377
- if (tree.middle.hash === hx) {
1378
- tree = tree.middle;
1379
- continue whileLoop; // notice, it jumps to while loop
1380
- }
1381
- }
1382
- else {
1383
- if (hx < tree.middle.minHash) {
1384
- return false;
1385
- }
1386
- if (hx <= tree.middle.maxHash) {
1387
- tree = tree.middle;
1388
- continue whileLoop; // notice, it jumps to while loop
1389
- }
1390
- }
1391
- if (tree.right == null) {
1392
- return false;
1393
- }
1394
- if (tree.right.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1395
- if (hx < tree.right.hash) {
1396
- return false;
1397
- }
1398
- if (tree.right.hash === hx) {
1399
- tree = tree.right;
1400
- continue whileLoop; // notice, it jumps to while loop
1401
- }
1402
- }
1403
- else {
1404
- if (hx < tree.right.minHash) {
1405
- return false;
1406
- }
1407
- if (hx <= tree.right.maxHash) {
1408
- tree = tree.right;
1409
- continue whileLoop; // notice, it jumps to while loop
1410
- }
1411
- }
1412
- return false;
1413
- }
1414
- return false;
1415
- }
1416
- function mapGetDefault(originalTree, item, v0) {
1417
- let hx = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(item);
1418
- let tree = originalTree;
1419
- whileLoop: while (tree != null) {
1420
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1421
- let size = tree.elements.length;
1422
- for (let i = 0; i < size; i++) {
1423
- let pair = tree.elements[i];
1424
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(pair[0], item)) {
1425
- return pair[1];
1426
- }
1427
- }
1428
- return v0;
1429
- }
1430
- // echo "looking for: ", hx, " ", item, " in ", tree.formatInline
1431
- if (tree.left == null) {
1432
- return v0;
1433
- }
1434
- if (tree.left.kind == _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1435
- if (hx < tree.left.hash) {
1436
- return v0;
1437
- }
1438
- if (tree.left.hash === hx) {
1439
- tree = tree.left;
1440
- continue whileLoop; // notice, it jumps to while loop
1441
- }
1442
- }
1443
- else {
1444
- if (hx < tree.left.minHash) {
1445
- return v0;
1446
- }
1447
- if (hx <= tree.left.maxHash) {
1448
- tree = tree.left;
1449
- continue whileLoop; // notice, it jumps to while loop
1450
- }
1451
- }
1452
- if (tree.middle == null) {
1453
- return v0;
1454
- }
1455
- if (tree.middle.kind == _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1456
- if (hx < tree.middle.hash) {
1457
- return v0;
1458
- }
1459
- if (tree.middle.hash === hx) {
1460
- tree = tree.middle;
1461
- continue whileLoop; // notice, it jumps to while loop
1462
- }
1463
- }
1464
- else {
1465
- if (hx < tree.middle.minHash) {
1466
- return v0;
1467
- }
1468
- if (hx <= tree.middle.maxHash) {
1469
- tree = tree.middle;
1470
- continue whileLoop; // notice, it jumps to while loop
1471
- }
1472
- }
1473
- if (tree.right == null) {
1474
- return v0;
1475
- }
1476
- if (tree.right.kind == _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1477
- if (hx < tree.right.hash) {
1478
- return v0;
1479
- }
1480
- if (tree.right.hash === hx) {
1481
- tree = tree.right;
1482
- continue whileLoop; // notice, it jumps to while loop
1483
- }
1484
- }
1485
- else {
1486
- if (hx < tree.right.minHash) {
1487
- return v0;
1488
- }
1489
- if (hx <= tree.right.maxHash) {
1490
- tree = tree.right;
1491
- continue whileLoop; // notice, it jumps to while loop
1492
- }
1493
- }
1494
- return v0;
1495
- }
1496
- return v0;
1497
- }
1498
- // leaves on the left has smaller hashes
1499
- // TODO check sizes, hashes
1500
- function checkMapStructure(tree) {
1501
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1502
- for (let i = 0; i < tree.elements.length; i++) {
1503
- let pair = tree.elements[i];
1504
- if (pair.length !== 2) {
1505
- throw new Error("Expected pair to br [k,v] :" + pair);
1506
- }
1507
- if (tree.hash !== (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(pair[0])) {
1508
- throw new Error(`Bad hash at leaf node ${tree}`);
1509
- }
1510
- }
1511
- if (mapLenBound(tree, 2) !== 1) {
1512
- throw new Error(`Bad len at leaf node ${tree}`);
1513
- }
1514
- }
1515
- else {
1516
- if (tree.left == null) {
1517
- if (tree.middle != null) {
1518
- throw new Error("Layout is not compact");
1519
- }
1520
- if (tree.middle != null) {
1521
- throw new Error("Layout is not compact");
1522
- }
1523
- }
1524
- if (tree.middle == null) {
1525
- if (tree.right != null) {
1526
- throw new Error("Layout is not compact");
1527
- }
1528
- }
1529
- if (tree.left != null && tree.middle != null) {
1530
- if (getMax(tree.left) >= getMin(tree.middle)) {
1531
- throw new Error(`Wrong hash order at left/middle branches ${formatMapInline(tree, true)}`);
1532
- }
1533
- }
1534
- if (tree.left != null && tree.right != null) {
1535
- if (getMax(tree.left) >= getMin(tree.right)) {
1536
- console.log(getMax(tree.left), getMin(tree.right));
1537
- throw new Error(`Wrong hash order at left/right branches ${formatMapInline(tree, true)}`);
1538
- }
1539
- }
1540
- if (tree.middle != null && tree.right != null) {
1541
- if (getMax(tree.middle) >= getMin(tree.right)) {
1542
- throw new Error(`Wrong hash order at middle/right branches ${formatMapInline(tree, true)}`);
1543
- }
1544
- }
1545
- if (tree.left != null) {
1546
- checkMapStructure(tree.left);
1547
- }
1548
- if (tree.middle != null) {
1549
- checkMapStructure(tree.middle);
1550
- }
1551
- if (tree.right != null) {
1552
- checkMapStructure(tree.right);
1553
- }
1554
- }
1555
- return true;
1556
- }
1557
- function rangeContainsHash(tree, thisHash) {
1558
- if (tree == null) {
1559
- return false;
1560
- }
1561
- else if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1562
- return tree.hash === thisHash;
1563
- }
1564
- else {
1565
- return thisHash >= tree.minHash && thisHash <= tree.maxHash;
1566
- }
1567
- }
1568
- function assocExisted(tree, key, item, thisHash = null) {
1569
- if (tree == null || isMapEmpty(tree)) {
1570
- throw new Error("Cannot call assoc on nil");
1571
- }
1572
- thisHash = thisHash !== null && thisHash !== void 0 ? thisHash : (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(key);
1573
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1574
- if (tree.hash !== thisHash) {
1575
- throw new Error("Expected hashes to be identical, otherwise element is missing");
1576
- }
1577
- let newPairs = new Array(tree.elements.length);
1578
- let replaced = false;
1579
- let size = tree.elements.length;
1580
- for (let idx = 0; idx < size; idx++) {
1581
- let pair = tree.elements[idx];
1582
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(pair[0], key)) {
1583
- newPairs[idx] = [key, item];
1584
- replaced = true;
1585
- }
1586
- else {
1587
- newPairs[idx] = pair;
1588
- }
1589
- }
1590
- if (replaced) {
1591
- let result = { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, hash: thisHash, elements: newPairs };
1592
- return result;
1593
- }
1594
- else {
1595
- throw new Error("Unexpected missing hash in assoc, invalid branch");
1596
- }
1597
- }
1598
- if (thisHash < tree.minHash)
1599
- throw new Error("Unexpected missing hash in assoc, hash too small");
1600
- else if (thisHash > tree.maxHash)
1601
- throw new Error("Unexpected missing hash in assoc, hash too large");
1602
- if (tree.left == null) {
1603
- throw new Error("Unexpected missing hash in assoc, found not branch");
1604
- }
1605
- if (rangeContainsHash(tree.left, thisHash)) {
1606
- let result = {
1607
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1608
- maxHash: tree.maxHash,
1609
- minHash: tree.minHash,
1610
- left: assocExisted(tree.left, key, item, thisHash),
1611
- middle: tree.middle,
1612
- right: tree.right,
1613
- depth: 0, // TODO
1614
- };
1615
- return result;
1616
- }
1617
- if (tree.middle == null) {
1618
- throw new Error("Unexpected missing hash in assoc, found not branch");
1619
- }
1620
- if (rangeContainsHash(tree.middle, thisHash)) {
1621
- let result = {
1622
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1623
- maxHash: tree.maxHash,
1624
- minHash: tree.minHash,
1625
- left: tree.left,
1626
- middle: assocExisted(tree.middle, key, item, thisHash),
1627
- right: tree.right,
1628
- depth: 0, // TODO
1629
- };
1630
- return result;
1631
- }
1632
- if (tree.right == null) {
1633
- throw new Error("Unexpected missing hash in assoc, found not branch");
1634
- }
1635
- if (rangeContainsHash(tree.right, thisHash)) {
1636
- let result = {
1637
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1638
- maxHash: tree.maxHash,
1639
- minHash: tree.minHash,
1640
- left: tree.left,
1641
- middle: tree.middle,
1642
- right: assocExisted(tree.right, key, item, thisHash),
1643
- depth: 0, // TODO
1644
- };
1645
- return result;
1646
- }
1647
- throw new Error("Unexpected missing hash in assoc, found not branch");
1648
- }
1649
- function assocNew(tree, key, item, thisHash = null) {
1650
- // echo fmt"assoc new: {key} to {tree.formatInline}"
1651
- if (tree == null || isMapEmpty(tree)) {
1652
- return createLeaf(key, item);
1653
- }
1654
- thisHash = thisHash !== null && thisHash !== void 0 ? thisHash : (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(key);
1655
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1656
- if (thisHash > tree.hash) {
1657
- let childBranch = {
1658
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1659
- hash: thisHash,
1660
- elements: [[key, item]],
1661
- };
1662
- let result = {
1663
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1664
- maxHash: thisHash,
1665
- minHash: tree.hash,
1666
- left: tree,
1667
- middle: childBranch,
1668
- right: emptyBranch,
1669
- depth: 0, // TODO
1670
- };
1671
- return result;
1672
- }
1673
- else if (thisHash < tree.hash) {
1674
- let childBranch = {
1675
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1676
- hash: thisHash,
1677
- elements: [[key, item]],
1678
- };
1679
- let result = {
1680
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1681
- maxHash: tree.hash,
1682
- minHash: thisHash,
1683
- left: childBranch,
1684
- middle: tree,
1685
- right: emptyBranch,
1686
- depth: 0, // TODO
1687
- };
1688
- return result;
1689
- }
1690
- else {
1691
- let size = tree.elements.length;
1692
- for (let i = 0; i < size; i++) {
1693
- let pair = tree.elements[i];
1694
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(pair[0], key)) {
1695
- throw new Error("Unexpected existed key in assoc");
1696
- }
1697
- }
1698
- let newPairs = new Array(tree.elements.length + 1);
1699
- for (let idx = 0; idx < size; idx++) {
1700
- let pair = tree.elements[idx];
1701
- newPairs[idx] = pair;
1702
- }
1703
- newPairs[tree.elements.length] = [key, item];
1704
- let result = {
1705
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1706
- hash: tree.hash,
1707
- elements: newPairs,
1708
- };
1709
- return result;
1710
- }
1711
- }
1712
- else {
1713
- if (thisHash < tree.minHash) {
1714
- if (tree.right == null) {
1715
- let childBranch = {
1716
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1717
- hash: thisHash,
1718
- elements: [[key, item]],
1719
- };
1720
- let result = {
1721
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1722
- maxHash: tree.maxHash,
1723
- minHash: thisHash,
1724
- left: childBranch,
1725
- middle: tree.left,
1726
- right: tree.middle,
1727
- depth: 0, // TODO
1728
- };
1729
- return result;
1730
- }
1731
- else {
1732
- let childBranch = {
1733
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1734
- hash: thisHash,
1735
- elements: [[key, item]],
1736
- };
1737
- let result = {
1738
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1739
- maxHash: tree.maxHash,
1740
- minHash: thisHash,
1741
- left: childBranch,
1742
- middle: tree,
1743
- right: emptyBranch,
1744
- depth: 0, // TODO
1745
- };
1746
- return result;
1747
- }
1748
- }
1749
- if (thisHash > tree.maxHash) {
1750
- // in compact layout, left arm must be existed
1751
- if (tree.middle == null) {
1752
- let childBranch = {
1753
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1754
- hash: thisHash,
1755
- elements: [[key, item]],
1756
- };
1757
- let result = {
1758
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1759
- maxHash: thisHash,
1760
- minHash: tree.minHash,
1761
- left: tree.left,
1762
- middle: childBranch,
1763
- right: emptyBranch,
1764
- depth: 0, // TODO
1765
- };
1766
- return result;
1767
- }
1768
- else if (tree.right == null) {
1769
- let childBranch = {
1770
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1771
- hash: thisHash,
1772
- elements: [[key, item]],
1773
- };
1774
- let result = {
1775
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1776
- maxHash: thisHash,
1777
- minHash: tree.minHash,
1778
- left: tree.left,
1779
- middle: tree.middle,
1780
- right: childBranch,
1781
- depth: 0, // TODO
1782
- };
1783
- return result;
1784
- }
1785
- else {
1786
- let childBranch = {
1787
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
1788
- hash: thisHash,
1789
- elements: [[key, item]],
1790
- };
1791
- let result = {
1792
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1793
- maxHash: thisHash,
1794
- minHash: tree.minHash,
1795
- left: tree,
1796
- middle: childBranch,
1797
- right: emptyBranch,
1798
- depth: 0, // TODO
1799
- };
1800
- return result;
1801
- }
1802
- }
1803
- if (rangeContainsHash(tree.left, thisHash)) {
1804
- let result = {
1805
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1806
- maxHash: tree.maxHash,
1807
- minHash: tree.minHash,
1808
- left: assocNew(tree.left, key, item, thisHash),
1809
- middle: tree.middle,
1810
- right: tree.right,
1811
- depth: 0, // TODO
1812
- };
1813
- return result;
1814
- }
1815
- if (rangeContainsHash(tree.middle, thisHash)) {
1816
- let result = {
1817
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1818
- maxHash: tree.maxHash,
1819
- minHash: tree.minHash,
1820
- left: tree.left,
1821
- middle: assocNew(tree.middle, key, item, thisHash),
1822
- right: tree.right,
1823
- depth: 0, // TODO
1824
- };
1825
- return result;
1826
- }
1827
- if (rangeContainsHash(tree.right, thisHash)) {
1828
- let result = {
1829
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1830
- maxHash: tree.maxHash,
1831
- minHash: tree.minHash,
1832
- left: tree.left,
1833
- middle: tree.middle,
1834
- right: assocNew(tree.right, key, item, thisHash),
1835
- depth: 0, // TODO
1836
- };
1837
- return result;
1838
- }
1839
- if (tree.middle == null) {
1840
- throw new Error("unreachable. if inside range, then middle should be here");
1841
- }
1842
- if (thisHash < getMin(tree.middle)) {
1843
- let result = {
1844
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1845
- maxHash: tree.maxHash,
1846
- minHash: tree.minHash,
1847
- left: assocNew(tree.left, key, item, thisHash),
1848
- middle: tree.middle,
1849
- right: tree.right,
1850
- depth: 0, // TODO
1851
- };
1852
- return result;
1853
- }
1854
- else {
1855
- let result = {
1856
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1857
- maxHash: tree.maxHash,
1858
- minHash: tree.minHash,
1859
- left: tree.left,
1860
- middle: tree.middle,
1861
- right: assocNew(tree.right, key, item, thisHash),
1862
- depth: 0, // TODO
1863
- };
1864
- return result;
1865
- }
1866
- }
1867
- }
1868
- function assocMap(tree, key, item, disableBalancing = false) {
1869
- if (tree == null || isMapEmpty(tree)) {
1870
- return createLeaf(key, item);
1871
- }
1872
- if (contains(tree, key)) {
1873
- return assocExisted(tree, key, item);
1874
- }
1875
- else {
1876
- return assocNew(tree, key, item);
1877
- }
1878
- }
1879
- function dissocExisted(tree, key) {
1880
- if (tree == null) {
1881
- throw new Error("Unexpected missing key in dissoc");
1882
- }
1883
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
1884
- if (tree.hash === (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(key)) {
1885
- let size = tree.elements.length;
1886
- if (size === 1 && (0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(key, tree.elements[0][0])) {
1887
- return emptyBranch;
1888
- }
1889
- else {
1890
- let newPairs = [];
1891
- for (let i = 0; i < size; i++) {
1892
- let pair = tree.elements[i];
1893
- if (!(0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(pair[0], key)) {
1894
- newPairs.push(pair);
1895
- }
1896
- }
1897
- let result = { kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf, hash: tree.hash, elements: newPairs };
1898
- return result;
1899
- }
1900
- }
1901
- else {
1902
- throw new Error("Unexpected missing key in dissoc on leaf");
1903
- }
1904
- }
1905
- if (mapLenBound(tree, 2) === 1) {
1906
- if (!contains(tree, key)) {
1907
- throw new Error("Unexpected missing key in dissoc single branch");
1908
- }
1909
- return emptyBranch;
1910
- }
1911
- let thisHash = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(key);
1912
- if (rangeContainsHash(tree.left, thisHash)) {
1913
- let changedBranch = dissocExisted(tree.left, key);
1914
- if (isMapEmpty(changedBranch)) {
1915
- if (isMapEmpty(tree.right)) {
1916
- return tree.middle;
1917
- }
1918
- let result = {
1919
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1920
- maxHash: tree.maxHash,
1921
- minHash: getMin(tree.middle),
1922
- left: tree.middle,
1923
- middle: tree.right,
1924
- right: emptyBranch,
1925
- depth: 0, // TODO
1926
- };
1927
- return result;
1928
- }
1929
- else {
1930
- let result = {
1931
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1932
- maxHash: tree.maxHash,
1933
- minHash: getMin(changedBranch),
1934
- left: changedBranch,
1935
- middle: tree.middle,
1936
- right: tree.right,
1937
- depth: 0, // TODO
1938
- };
1939
- return result;
1940
- }
1941
- }
1942
- if (rangeContainsHash(tree.middle, thisHash)) {
1943
- let changedBranch = dissocExisted(tree.middle, key);
1944
- if (isMapEmpty(changedBranch)) {
1945
- if (isMapEmpty(tree.right)) {
1946
- return tree.left;
1947
- }
1948
- let result = {
1949
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1950
- maxHash: tree.maxHash,
1951
- minHash: tree.minHash,
1952
- left: tree.left,
1953
- middle: tree.right,
1954
- right: emptyBranch,
1955
- depth: 0, // TODO
1956
- };
1957
- return result;
1958
- }
1959
- else {
1960
- let result = {
1961
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1962
- maxHash: tree.maxHash,
1963
- minHash: tree.minHash,
1964
- left: tree.left,
1965
- middle: changedBranch,
1966
- right: tree.right,
1967
- depth: 0, // TODO
1968
- };
1969
- return result;
1970
- }
1971
- }
1972
- if (rangeContainsHash(tree.right, thisHash)) {
1973
- let changedBranch = dissocExisted(tree.right, key);
1974
- if (changedBranch == null) {
1975
- let result = {
1976
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1977
- maxHash: getMax(tree.middle),
1978
- minHash: tree.minHash,
1979
- left: tree.left,
1980
- middle: tree.middle,
1981
- right: emptyBranch,
1982
- depth: 0, // TODO
1983
- };
1984
- return result;
1985
- }
1986
- else {
1987
- let result = {
1988
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
1989
- maxHash: getMax(changedBranch),
1990
- minHash: tree.minHash,
1991
- left: tree.left,
1992
- middle: tree.middle,
1993
- right: changedBranch,
1994
- depth: 0, // TODO
1995
- };
1996
- return result;
1997
- }
1998
- }
1999
- throw new Error("Cannot find branch in dissoc");
2000
- }
2001
- function dissocMap(tree, key) {
2002
- if (contains(tree, key)) {
2003
- return dissocExisted(tree, key);
2004
- }
2005
- else {
2006
- return tree;
2007
- }
2008
- }
2009
- function collectToPairsArray(acc, tree) {
2010
- if (tree != null) {
2011
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
2012
- for (let i = 0; i < tree.elements.length; i++) {
2013
- let pair = tree.elements[i];
2014
- acc.push(pair);
2015
- }
2016
- }
2017
- else {
2018
- if (tree.left != null) {
2019
- collectToPairsArray(acc, tree.left);
2020
- }
2021
- if (tree.middle != null) {
2022
- collectToPairsArray(acc, tree.middle);
2023
- }
2024
- if (tree.right != null) {
2025
- collectToPairsArray(acc, tree.right);
2026
- }
2027
- }
2028
- }
2029
- }
2030
- /** similar to `toPairs`, but using Array.push directly */
2031
- function toPairsArray(tree) {
2032
- let result = [];
2033
- collectToPairsArray(result, tree);
2034
- return result;
2035
- }
2036
- function* toPairs(tree) {
2037
- if (tree != null) {
2038
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
2039
- for (let pair of tree.elements) {
2040
- yield pair;
2041
- }
2042
- }
2043
- else {
2044
- if (tree.left != null) {
2045
- for (let item of toPairs(tree.left)) {
2046
- yield item;
2047
- }
2048
- }
2049
- if (tree.middle != null) {
2050
- for (let item of toPairs(tree.middle)) {
2051
- yield item;
2052
- }
2053
- }
2054
- if (tree.right != null) {
2055
- for (let item of toPairs(tree.right)) {
2056
- yield item;
2057
- }
2058
- }
2059
- }
2060
- }
2061
- }
2062
- function* toKeys(tree) {
2063
- if (tree != null) {
2064
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
2065
- for (let i = 0; i < tree.elements.length; i++) {
2066
- let pair = tree.elements[i];
2067
- yield pair[0];
2068
- }
2069
- }
2070
- else {
2071
- if (tree.left != null) {
2072
- for (let item of toKeys(tree.left)) {
2073
- yield item;
2074
- }
2075
- }
2076
- if (tree.middle != null) {
2077
- for (let item of toKeys(tree.middle)) {
2078
- yield item;
2079
- }
2080
- }
2081
- if (tree.right != null) {
2082
- for (let item of toKeys(tree.right)) {
2083
- yield item;
2084
- }
2085
- }
2086
- }
2087
- }
2088
- }
2089
- function* toValues(tree) {
2090
- if (tree != null) {
2091
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
2092
- for (let i = 0; i < tree.elements.length; i++) {
2093
- let pair = tree.elements[i];
2094
- yield pair[1];
2095
- }
2096
- }
2097
- else {
2098
- if (tree.left != null) {
2099
- for (let item of toValues(tree.left)) {
2100
- yield item;
2101
- }
2102
- }
2103
- if (tree.middle != null) {
2104
- for (let item of toValues(tree.middle)) {
2105
- yield item;
2106
- }
2107
- }
2108
- if (tree.right != null) {
2109
- for (let item of toValues(tree.right)) {
2110
- yield item;
2111
- }
2112
- }
2113
- }
2114
- }
2115
- }
2116
- function mapEqual(xs, ys) {
2117
- if (xs === ys) {
2118
- return true;
2119
- }
2120
- if (mapLen(xs) !== mapLen(ys)) {
2121
- return false;
2122
- }
2123
- if (isMapEmpty(xs)) {
2124
- return true;
2125
- }
2126
- for (let pair of toPairsArray(xs)) {
2127
- let key = pair[0];
2128
- let vx = pair[1];
2129
- if (!contains(ys, key)) {
2130
- return false;
2131
- }
2132
- let vy = mapGetDefault(ys, key, null);
2133
- // TODO compare deep structures
2134
- if (!(0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(vx, vy)) {
2135
- return false;
2136
- }
2137
- }
2138
- return true;
2139
- }
2140
- function merge(xs, ys) {
2141
- let ret = xs;
2142
- let counted = 0;
2143
- for (let [key, item] of toPairs(ys)) {
2144
- ret = assocMap(ret, key, item);
2145
- // # TODO pickd loop by experience
2146
- if (counted > 700) {
2147
- forceMapInplaceBalancing(ret);
2148
- counted = 0;
2149
- }
2150
- else {
2151
- counted = counted + 1;
2152
- }
2153
- }
2154
- return ret;
2155
- }
2156
- // # skip a value, mostly for nil
2157
- function mergeSkip(xs, ys, skipped) {
2158
- let ret = xs;
2159
- let counted = 0;
2160
- for (let [key, item] of toPairs(ys)) {
2161
- if ((0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(item, skipped)) {
2162
- continue;
2163
- }
2164
- ret = assocMap(ret, key, item);
2165
- // # TODO pickd loop by experience
2166
- if (counted > 700) {
2167
- forceMapInplaceBalancing(ret);
2168
- counted = 0;
2169
- }
2170
- else {
2171
- counted = counted + 1;
2172
- }
2173
- }
2174
- return ret;
2175
- }
2176
- // this function mutates original tree to make it more balanced
2177
- function forceMapInplaceBalancing(tree) {
2178
- // echo "Force inplace balancing of list"
2179
- if (tree.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch) {
2180
- let xs = toOrderedHashEntries(tree);
2181
- let newTree = makeTernaryTreeMap(xs.length, 0, xs);
2182
- tree.left = newTree.left;
2183
- tree.middle = newTree.middle;
2184
- tree.right = newTree.right;
2185
- }
2186
- else {
2187
- // discard
2188
- }
2189
- }
2190
- function sameMapShape(xs, ys) {
2191
- if (xs == null) {
2192
- if (ys == null) {
2193
- return true;
2194
- }
2195
- else {
2196
- return false;
2197
- }
2198
- }
2199
- if (ys == null) {
2200
- return false;
2201
- }
2202
- if (mapLen(xs) !== mapLen(ys)) {
2203
- return false;
2204
- }
2205
- if (xs.kind !== ys.kind) {
2206
- return false;
2207
- }
2208
- if (xs.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf && ys.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf) {
2209
- if (xs.elements.length !== ys.elements.length) {
2210
- return false;
2211
- }
2212
- for (let idx = 0; idx < xs.elements.length; idx++) {
2213
- if (!(0,_utils__WEBPACK_IMPORTED_MODULE_1__.dataEqual)(xs.elements[idx], ys.elements[idx])) {
2214
- return false;
2215
- }
2216
- }
2217
- return true;
2218
- }
2219
- else if (xs.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch && ys.kind === _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch) {
2220
- if (!sameMapShape(xs.left, ys.left)) {
2221
- return false;
2222
- }
2223
- if (!sameMapShape(xs.middle, ys.middle)) {
2224
- return false;
2225
- }
2226
- if (!sameMapShape(xs.right, ys.right)) {
2227
- return false;
2228
- }
2229
- return true;
2230
- }
2231
- else {
2232
- throw new Error("Unknown");
2233
- }
2234
- }
2235
- function mapMapValues(tree, f) {
2236
- if (tree == null) {
2237
- return tree;
2238
- }
2239
- switch (tree.kind) {
2240
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf: {
2241
- let newElements = new Array(tree.elements.length);
2242
- let size = tree.elements.length;
2243
- for (let idx = 0; idx < size; idx++) {
2244
- newElements[idx] = [tree.elements[idx][0], f(tree.elements[idx][1])];
2245
- }
2246
- let result = {
2247
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeLeaf,
2248
- hash: tree.hash,
2249
- elements: newElements,
2250
- };
2251
- return result;
2252
- }
2253
- case _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch: {
2254
- let result = {
2255
- kind: _types__WEBPACK_IMPORTED_MODULE_0__.TernaryTreeKind.ternaryTreeBranch,
2256
- depth: tree.depth,
2257
- minHash: tree.minHash,
2258
- maxHash: tree.maxHash,
2259
- left: mapMapValues(tree.left, f),
2260
- middle: mapMapValues(tree.middle, f),
2261
- right: mapMapValues(tree.right, f),
2262
- };
2263
- return result;
2264
- }
2265
- }
2266
- }
2267
-
2268
-
2269
- /***/ }),
2270
-
2271
- /***/ "./lib/test-list.js":
2272
- /*!**************************!*\
2273
- !*** ./lib/test-list.js ***!
2274
- \**************************/
2275
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2276
-
2277
- __webpack_require__.r(__webpack_exports__);
2278
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2279
- /* harmony export */ "runListTests": () => (/* binding */ runListTests)
2280
- /* harmony export */ });
2281
- /* harmony import */ var _list__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./list */ "./lib/list.js");
2282
- /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils */ "./lib/utils.js");
2283
-
2284
-
2285
- let runListTests = () => {
2286
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("init list", () => {
2287
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listToString)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4])) === "TernaryTreeList[4, ...]");
2288
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
2289
- let data11 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin11);
2290
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)(data11));
2291
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data11), "((1 (2 3 _) 4) (5 6 7) (8 (9 10 _) 11))");
2292
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.arrayEqual)(origin11, [...(0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(data11)]));
2293
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.arrayEqual)([...(0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(data11)], [...(0,_list__WEBPACK_IMPORTED_MODULE_0__.indexToItems)(data11)]));
2294
- let emptyXs = new Array(0);
2295
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initEmptyTernaryTreeList)(), (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(emptyXs)));
2296
- });
2297
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("list operations", () => {
2298
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
2299
- let data11 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin11);
2300
- // get
2301
- for (let idx = 0; idx < origin11.length; idx++) {
2302
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(origin11[idx] === (0,_list__WEBPACK_IMPORTED_MODULE_0__.listGet)(data11, idx));
2303
- }
2304
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.first)(data11) === 1);
2305
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.last)(data11) === 11);
2306
- // assoc
2307
- let origin5 = [1, 2, 3, 4, 5];
2308
- let data5 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin5);
2309
- let updated = (0,_list__WEBPACK_IMPORTED_MODULE_0__.assocList)(data5, 3, 10);
2310
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listGet)(updated, 3) === 10);
2311
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listGet)(data5, 3) === 4);
2312
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)(updated) === (0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)(data5));
2313
- for (let idx = 0; idx < (0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)(data5); idx++) {
2314
- // echo data5.dissoc(idx).formatInline
2315
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, idx)) === (0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)(data5) - 1);
2316
- }
2317
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data5), "((1 2 _) 3 (4 5 _))");
2318
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, 0)), "(2 3 (4 5 _))");
2319
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, 1)), "(1 3 (4 5 _))");
2320
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, 2)), "((1 2 _) (4 5 _) _)");
2321
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, 3)), "((1 2 _) 3 5)");
2322
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.dissocList)(data5, 4)), "((1 2 _) 3 4)");
2323
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.rest)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1]))), "_");
2324
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.rest)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2]))), "2");
2325
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.rest)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3]))), "(2 3 _)");
2326
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.rest)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4]))), "((2 3 _) 4 _)");
2327
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.rest)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4, 5]))), "(2 3 (4 5 _))");
2328
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.butlast)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1]))), "_");
2329
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.butlast)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2]))), "1");
2330
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.butlast)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3]))), "(1 2 _)");
2331
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.butlast)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4]))), "(1 (2 3 _) _)");
2332
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.butlast)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4, 5]))), "((1 2 _) 3 4)");
2333
- });
2334
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("list insertions", () => {
2335
- let origin5 = [1, 2, 3, 4, 5];
2336
- let data5 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin5);
2337
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data5), "((1 2 _) 3 (4 5 _))");
2338
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 0, 10, false)), "(10 ((1 2 _) 3 (4 5 _)) _)");
2339
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 0, 10, true)), "((1 10 2) 3 (4 5 _))");
2340
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 1, 10, false)), "((1 10 2) 3 (4 5 _))");
2341
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 1, 10, true)), "((1 2 10) 3 (4 5 _))");
2342
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 2, 10, false)), "((1 2 _) (10 3 _) (4 5 _))");
2343
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 2, 10, true)), "((1 2 _) (3 10 _) (4 5 _))");
2344
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 3, 10, false)), "((1 2 _) 3 (10 4 5))");
2345
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 3, 10, true)), "((1 2 _) 3 (4 10 5))");
2346
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 4, 10, false)), "((1 2 _) 3 (4 10 5))");
2347
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.insert)(data5, 4, 10, true)), "(((1 2 _) 3 (4 5 _)) 10 _)");
2348
- let origin4 = [1, 2, 3, 4];
2349
- let data4 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin4);
2350
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.assocBefore)(data4, 3, 10)), "(1 (2 3 _) (10 4 _))");
2351
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.assocAfter)(data4, 3, 10)), "(1 (2 3 _) (4 10 _))");
2352
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.prepend)(data4, 10)), "((10 1 _) (2 3 _) 4)");
2353
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.checkEqual)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.append)(data4, 10)), "(1 (2 3 _) (4 10 _))");
2354
- });
2355
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("concat", () => {
2356
- let data1 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2]);
2357
- let data2 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([3, 4]);
2358
- let data3 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([5, 6]);
2359
- let data4 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([7, 8]);
2360
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2)) === "((1 2 _) (3 4 _) _)");
2361
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([]), data1)) === "(1 2 _)");
2362
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2, data3)) === "((1 2 _) (3 4 _) (5 6 _))");
2363
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2, data3, data4)) === "((1 2 _) ((3 4 _) (5 6 _) _) (7 8 _))");
2364
- (0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2));
2365
- (0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2, data3));
2366
- (0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2, data3, data4));
2367
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listLen)((0,_list__WEBPACK_IMPORTED_MODULE_0__.concat)(data1, data2, data3, data4)) === 8);
2368
- });
2369
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("check(equality", () => {
2370
- let origin4 = [1, 2, 3, 4];
2371
- let data4 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin4);
2372
- let data4n = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin4);
2373
- let data4Made = (0,_list__WEBPACK_IMPORTED_MODULE_0__.prepend)((0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([2, 3, 4]), 1);
2374
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.sameListShape)(data4, data4) === true);
2375
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.sameListShape)(data4, data4n) === true);
2376
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.sameListShape)(data4, data4Made) === false);
2377
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)(data4, data4n));
2378
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)(data4, data4Made));
2379
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)(data4n, data4Made));
2380
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(data4 !== data4Made); // identical false
2381
- });
2382
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("force balancing", () => {
2383
- var data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([]);
2384
- for (let idx = 0; idx < 20; idx++) {
2385
- data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.append)(data, idx, true);
2386
- }
2387
- // echo data.formatInline
2388
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data) === "(((0 1 2) (3 4 5) (6 7 8)) ((9 10 11) (12 13 14) (15 16 17)) (18 19 _))");
2389
- (0,_list__WEBPACK_IMPORTED_MODULE_0__.forceListInplaceBalancing)(data);
2390
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data) === "(((0 1 _) (2 3 4) (5 6 _)) ((7 8 _) (9 10 _) (11 12 _)) ((13 14 _) (15 16 17) (18 19 _)))");
2391
- // echo data.formatInline
2392
- });
2393
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("iterator", () => {
2394
- let origin4 = [1, 2, 3, 4];
2395
- let data4 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin4);
2396
- var i = 0;
2397
- for (let item of (0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(data4)) {
2398
- i = i + 1;
2399
- }
2400
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 4);
2401
- i = 0;
2402
- for (let [idx, item] of (0,_list__WEBPACK_IMPORTED_MODULE_0__.listToPairs)(data4)) {
2403
- i = i + idx;
2404
- }
2405
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 6);
2406
- });
2407
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("check structure", () => {
2408
- var data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([]);
2409
- for (let idx = 0; idx < 20; idx++) {
2410
- data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.append)(data, idx, true);
2411
- }
2412
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)(data));
2413
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
2414
- let data11 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)(origin11);
2415
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)(data11));
2416
- });
2417
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("slices", () => {
2418
- var data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([]);
2419
- for (let idx = 0; idx < 40; idx++) {
2420
- data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.append)(data, idx, true);
2421
- }
2422
- var list40 = [];
2423
- for (let idx = 0; idx < 40; idx++) {
2424
- list40.push(idx);
2425
- }
2426
- for (let i = 0; i < 40; i++) {
2427
- for (let j = i; j < 40; j++) {
2428
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.arrayEqual)([...(0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)((0,_list__WEBPACK_IMPORTED_MODULE_0__.slice)(data, i, j))], list40.slice(i, j)));
2429
- }
2430
- }
2431
- });
2432
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("reverse", () => {
2433
- let data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
2434
- let reversedData = (0,_list__WEBPACK_IMPORTED_MODULE_0__.reverse)(data);
2435
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.arrayEqual)([...(0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(data)].reverse(), [...(0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(reversedData)]));
2436
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)(reversedData));
2437
- });
2438
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("list traverse", () => {
2439
- var i = 0;
2440
- let data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
2441
- for (let x of (0,_list__WEBPACK_IMPORTED_MODULE_0__.listToItems)(data)) {
2442
- i = i + 1;
2443
- }
2444
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 10);
2445
- });
2446
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("index of", () => {
2447
- let data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4, 5, 6, 7, 8]);
2448
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.indexOf)(data, 2) === 1);
2449
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.findIndex)(data, (x) => x === 2) === 1);
2450
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.indexOf)(data, 9) === -1);
2451
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.findIndex)(data, (x) => x === 9) === -1);
2452
- });
2453
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("map values", () => {
2454
- let data = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 2, 3, 4]);
2455
- let data2 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([1, 4, 9, 16]);
2456
- let data3 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.listMapValues)(data, (x) => x * x);
2457
- (0,_list__WEBPACK_IMPORTED_MODULE_0__.checkListStructure)(data3);
2458
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)(data2, data3));
2459
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data2) === (0,_list__WEBPACK_IMPORTED_MODULE_0__.formatListInline)(data3));
2460
- });
2461
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("concat", () => {
2462
- let data1 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeList)([3, 4]);
2463
- let data2 = (0,_list__WEBPACK_IMPORTED_MODULE_0__.initTernaryTreeListFromRange)([1, 2, 3, 4, 5, 6], 2, 4);
2464
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_list__WEBPACK_IMPORTED_MODULE_0__.listEqual)(data1, data2));
2465
- });
2466
- };
2467
-
2468
-
2469
- /***/ }),
2470
-
2471
- /***/ "./lib/test-map.js":
2472
- /*!*************************!*\
2473
- !*** ./lib/test-map.js ***!
2474
- \*************************/
2475
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2476
-
2477
- __webpack_require__.r(__webpack_exports__);
2478
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2479
- /* harmony export */ "runMapTests": () => (/* binding */ runMapTests)
2480
- /* harmony export */ });
2481
- /* harmony import */ var _types__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./types */ "./lib/types.js");
2482
- /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./utils */ "./lib/utils.js");
2483
- /* harmony import */ var _map__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./map */ "./lib/map.js");
2484
-
2485
-
2486
-
2487
- let runMapTests = () => {
2488
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("init map", () => {
2489
- var dict = new Map();
2490
- var inList = [];
2491
- for (let idx = 0; idx < 10; idx++) {
2492
- dict.set(`${idx}`, idx + 10);
2493
- inList.push([`${idx}`, idx + 10]);
2494
- }
2495
- // TODO
2496
- inList.sort((x, y) => {
2497
- let hx = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(x[0]);
2498
- let hy = (0,_types__WEBPACK_IMPORTED_MODULE_0__.hashGenerator)(y[0]);
2499
- return (0,_utils__WEBPACK_IMPORTED_MODULE_1__.cmp)(hx, hy);
2500
- });
2501
- let data10 = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2502
- let data11 = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(inList);
2503
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data10);
2504
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data11);
2505
- // echo data10
2506
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.justDisplay)((0,_map__WEBPACK_IMPORTED_MODULE_2__.formatMapInline)(data10, true), " ((0:10 1:11 2:12) (3:13 (4:14 5:15 _) 6:16) (7:17 8:18 9:19))");
2507
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.toHashSortedPairs)(data10), inList));
2508
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.toHashSortedPairs)(data11), inList));
2509
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data10, "1") === true);
2510
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data10, "11") === false);
2511
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(data10, "1", null), 11));
2512
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(data10, "111", 0), 0));
2513
- // check(deepEqual(mapGetDefault(data10, "11", {} as any), null)); // should throws error
2514
- let emptyData = new Map();
2515
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.initEmptyTernaryTreeMap)(), (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(emptyData)));
2516
- });
2517
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("assoc and contains", () => {
2518
- var dict = new Map();
2519
- for (let idx = 0; idx < 100; idx++) {
2520
- dict.set(`${idx * 2}`, idx);
2521
- }
2522
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2523
- for (let idx = 0; idx < 100; idx++) {
2524
- dict.set(`${idx * 2 + 1}`, idx);
2525
- let data2 = (0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(data, `${idx * 2 + 1}`, idx);
2526
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data2, `${idx * 2 + 1}`));
2527
- }
2528
- var dict = new Map();
2529
- data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2530
- for (let idx = 0; idx < 1000; idx++) {
2531
- let p = 100 - idx / 10;
2532
- data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(data, `${p}`, idx);
2533
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data, `${p}`));
2534
- }
2535
- });
2536
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("check structure", () => {
2537
- var dict = new Map();
2538
- for (let idx = 0; idx < 100; idx++) {
2539
- dict.set(`${idx}`, idx + 10);
2540
- }
2541
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2542
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data));
2543
- });
2544
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("assoc map", () => {
2545
- var dict = new Map();
2546
- for (let idx = 0; idx < 10; idx++) {
2547
- dict.set(`${idx}`, idx + 10);
2548
- }
2549
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2550
- // echo data.formatInline
2551
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data, "1") === true);
2552
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data, "12") === false);
2553
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2554
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.justDisplay)((0,_map__WEBPACK_IMPORTED_MODULE_2__.formatMapInline)((0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(data, "1", 2222), true), "((0:10 1:2222 2:12) (3:13 (4:14 5:15 _) 6:16) (7:17 8:18 9:19))");
2555
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.justDisplay)((0,_map__WEBPACK_IMPORTED_MODULE_2__.formatMapInline)((0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(data, "23", 2222), true), "(((0:10 1:11 2:12) (3:13 (4:14 5:15 _) 6:16) (7:17 8:18 9:19)) 23:2222 _)");
2556
- });
2557
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("dissoc", () => {
2558
- var dict = new Map();
2559
- for (let idx = 0; idx < 10; idx++) {
2560
- dict.set(`${idx}`, idx + 10);
2561
- }
2562
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2563
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2564
- // echo data.formatInline
2565
- for (let idx = 0; idx < 10; idx++) {
2566
- let v = (0,_map__WEBPACK_IMPORTED_MODULE_2__.dissocMap)(data, `${idx}`);
2567
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(v, `${idx}`) === false);
2568
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(data, `${idx}`) === true);
2569
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapLen)(v) === (0,_map__WEBPACK_IMPORTED_MODULE_2__.mapLen)(data) - 1);
2570
- }
2571
- for (let idx = 10; idx < 12; idx++) {
2572
- let v = (0,_map__WEBPACK_IMPORTED_MODULE_2__.dissocMap)(data, `${idx}`);
2573
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.contains)(v, `${idx}`) === false);
2574
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapLen)(v) === (0,_map__WEBPACK_IMPORTED_MODULE_2__.mapLen)(data));
2575
- }
2576
- });
2577
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("to array", () => {
2578
- var dict = new Map();
2579
- for (let idx = 0; idx < 10; idx++) {
2580
- dict.set(`${idx}`, idx + 10);
2581
- }
2582
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2583
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2584
- // TODO
2585
- // justDisplay((mapToString(toPairs(data))) , "@[2:12, 3:13, 7:17, 9:19, 6:16, 5:15, 1:11, 8:18, 0:10, 4:14]")
2586
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.justDisplay)([...(0,_map__WEBPACK_IMPORTED_MODULE_2__.toKeys)(data)], ["2", "3", "7", "9", "6", "5", "1", "8", "0", "4"]);
2587
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.toPairsArray)(data), [...(0,_map__WEBPACK_IMPORTED_MODULE_2__.toPairs)(data)]));
2588
- });
2589
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("Equality", () => {
2590
- var dict = new Map();
2591
- for (let idx = 0; idx < 10; idx++) {
2592
- dict.set(`${idx}`, idx + 10);
2593
- }
2594
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2595
- let b = (0,_map__WEBPACK_IMPORTED_MODULE_2__.dissocMap)(data, "3");
2596
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2597
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(b);
2598
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)(data, data));
2599
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(!(0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)(data, b));
2600
- let c = (0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(data, "3", 15);
2601
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.sameMapShape)(data, data));
2602
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.sameMapShape)(data, b) === false);
2603
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.sameMapShape)(data, c) === false);
2604
- let d = (0,_map__WEBPACK_IMPORTED_MODULE_2__.assocMap)(c, "3", 13);
2605
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)(data, d));
2606
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(data !== d); // not identical
2607
- });
2608
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("Merge", () => {
2609
- var dict = new Map();
2610
- var dictBoth = new Map();
2611
- for (let idx = 0; idx < 4; idx++) {
2612
- dict.set(`${idx}`, idx + 10);
2613
- dictBoth.set(`${idx}`, idx + 10);
2614
- }
2615
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2616
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2617
- var dictB = new Map();
2618
- for (let idx = 10; idx < 14; idx++) {
2619
- dictB.set(`${idx}`, idx + 23);
2620
- dictBoth.set(`${idx}`, idx + 23);
2621
- }
2622
- let b = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dictB);
2623
- let merged = (0,_map__WEBPACK_IMPORTED_MODULE_2__.merge)(data, b);
2624
- let both = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dictBoth);
2625
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)(merged, both));
2626
- });
2627
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("Merge skip", () => {
2628
- var dict = new Map();
2629
- for (let idx = 0; idx < 4; idx++) {
2630
- dict.set(`${idx}`, idx + 10);
2631
- }
2632
- let a = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2633
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(a);
2634
- var dict2 = new Map();
2635
- for (let idx = 0; idx < 4; idx++) {
2636
- dict2.set(`${idx}`, idx + 11);
2637
- }
2638
- let b = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict2);
2639
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(b);
2640
- let c = (0,_map__WEBPACK_IMPORTED_MODULE_2__.mergeSkip)(a, b, 11);
2641
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(c, "0", null), 10));
2642
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(c, "1", null), 12));
2643
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(c, "2", null), 13));
2644
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_utils__WEBPACK_IMPORTED_MODULE_1__.deepEqual)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapGetDefault)(c, "3", null), 14));
2645
- });
2646
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("iterator", () => {
2647
- var dict = new Map();
2648
- var dictBoth = new Map();
2649
- for (let idx = 0; idx < 4; idx++) {
2650
- dict.set(`${idx}`, idx + 10);
2651
- dictBoth.set(`${idx}`, idx + 10);
2652
- }
2653
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2654
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2655
- var i = 0;
2656
- for (let [k, v] of (0,_map__WEBPACK_IMPORTED_MODULE_2__.toPairs)(data)) {
2657
- i = i + 1;
2658
- }
2659
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 4);
2660
- i = 0;
2661
- for (let key of (0,_map__WEBPACK_IMPORTED_MODULE_2__.toPairs)(data)) {
2662
- i = i + 1;
2663
- }
2664
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 4);
2665
- });
2666
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("each map", () => {
2667
- var dict = new Map();
2668
- for (let idx = 0; idx < 100; idx++) {
2669
- dict.set(`${idx}`, idx + 10);
2670
- }
2671
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2672
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data);
2673
- var i = 0;
2674
- for (let [k, v] of (0,_map__WEBPACK_IMPORTED_MODULE_2__.toPairs)(data)) {
2675
- // echo "..{k}-{v}.."
2676
- i = i + 1;
2677
- }
2678
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)(i === 100);
2679
- });
2680
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.test)("map values", () => {
2681
- var dict = new Map();
2682
- for (let idx = 0; idx < 4; idx++) {
2683
- dict.set(`${idx}`, idx + 10);
2684
- }
2685
- let data = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict);
2686
- var dict2 = new Map();
2687
- for (let idx = 0; idx < 4; idx++) {
2688
- dict2.set(`${idx}`, idx + 20);
2689
- }
2690
- let data2 = (0,_map__WEBPACK_IMPORTED_MODULE_2__.initTernaryTreeMap)(dict2);
2691
- let data3 = (0,_map__WEBPACK_IMPORTED_MODULE_2__.mapMapValues)(data, (x) => x + 10);
2692
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data3);
2693
- (0,_map__WEBPACK_IMPORTED_MODULE_2__.checkMapStructure)(data3);
2694
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.mapEqual)(data2, data3));
2695
- (0,_utils__WEBPACK_IMPORTED_MODULE_1__.check)((0,_map__WEBPACK_IMPORTED_MODULE_2__.formatMapInline)(data2) === (0,_map__WEBPACK_IMPORTED_MODULE_2__.formatMapInline)(data3));
2696
- });
2697
- };
2698
-
2699
-
2700
- /***/ }),
2701
-
2702
- /***/ "./lib/types.js":
2703
- /*!**********************!*\
2704
- !*** ./lib/types.js ***!
2705
- \**********************/
2706
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2707
-
2708
- __webpack_require__.r(__webpack_exports__);
2709
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2710
- /* harmony export */ "TernaryTreeKind": () => (/* binding */ TernaryTreeKind),
2711
- /* harmony export */ "valueHash": () => (/* binding */ valueHash),
2712
- /* harmony export */ "hashGenerator": () => (/* binding */ hashGenerator),
2713
- /* harmony export */ "overwriteHashGenerator": () => (/* binding */ overwriteHashGenerator),
2714
- /* harmony export */ "mergeValueHash": () => (/* binding */ mergeValueHash)
2715
- /* harmony export */ });
2716
- var TernaryTreeKind;
2717
- (function (TernaryTreeKind) {
2718
- TernaryTreeKind[TernaryTreeKind["ternaryTreeBranch"] = 0] = "ternaryTreeBranch";
2719
- TernaryTreeKind[TernaryTreeKind["ternaryTreeLeaf"] = 1] = "ternaryTreeLeaf";
2720
- })(TernaryTreeKind || (TernaryTreeKind = {}));
2721
- let valueHash = (x) => {
2722
- if (typeof x === "number") {
2723
- // console.log("hash for x:", x, "\t", result);
2724
- return x;
2725
- }
2726
- else if (typeof x === "string") {
2727
- let h = 0;
2728
- // https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0#gistcomment-2775538
2729
- for (var i = 0; i < x.length; i++) {
2730
- h = Math.imul(31, h) + (x[i].charCodeAt(0) | 0);
2731
- }
2732
- // console.log("hash for x:", x, "\t", result);
2733
- return h;
2734
- }
2735
- throw new Error("Hash solution not provided for this type(other than number and string)");
2736
- };
2737
- /** default hash function only handles number and string, need customization */
2738
- let hashGenerator = valueHash;
2739
- /** allow customizing hash function from outside */
2740
- let overwriteHashGenerator = (f) => {
2741
- hashGenerator = f;
2742
- };
2743
- let mergeValueHash = (base, x) => {
2744
- if (typeof x === "number") {
2745
- return Math.imul(31, base) + x;
2746
- }
2747
- else if (typeof x === "string") {
2748
- let h = base;
2749
- // https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0#gistcomment-2775538
2750
- for (var i = 0; i < x.length; i++) {
2751
- h = Math.imul(31, h) + (x[i].charCodeAt(0) | 0);
2752
- }
2753
- return h;
2754
- }
2755
- throw new Error("Hash solution not provided for this type(other than number and string)");
2756
- };
2757
-
2758
-
2759
- /***/ }),
2760
-
2761
- /***/ "./lib/utils.js":
2762
- /*!**********************!*\
2763
- !*** ./lib/utils.js ***!
2764
- \**********************/
2765
- /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
2766
-
2767
- __webpack_require__.r(__webpack_exports__);
2768
- /* harmony export */ __webpack_require__.d(__webpack_exports__, {
2769
- /* harmony export */ "dataEqual": () => (/* binding */ dataEqual),
2770
- /* harmony export */ "overwriteComparator": () => (/* binding */ overwriteComparator),
2771
- /* harmony export */ "roughIntPow": () => (/* binding */ roughIntPow),
2772
- /* harmony export */ "divideTernarySizes": () => (/* binding */ divideTernarySizes),
2773
- /* harmony export */ "shallowCloneArray": () => (/* binding */ shallowCloneArray),
2774
- /* harmony export */ "test": () => (/* binding */ test),
2775
- /* harmony export */ "check": () => (/* binding */ check),
2776
- /* harmony export */ "checkEqual": () => (/* binding */ checkEqual),
2777
- /* harmony export */ "justDisplay": () => (/* binding */ justDisplay),
2778
- /* harmony export */ "cmp": () => (/* binding */ cmp),
2779
- /* harmony export */ "deepEqual": () => (/* binding */ deepEqual),
2780
- /* harmony export */ "arrayEqual": () => (/* binding */ arrayEqual)
2781
- /* harmony export */ });
2782
- /** by default, it compares by reference
2783
- * supposed to be overwritten by user
2784
- */
2785
- let dataEqual = (x, y) => {
2786
- return x === y;
2787
- };
2788
- let overwriteComparator = (f) => {
2789
- dataEqual = f;
2790
- };
2791
- let roughIntPow = (x, times) => {
2792
- if (times < 1) {
2793
- return x;
2794
- }
2795
- let result = 1;
2796
- for (let idx = 0; idx < times; idx++) {
2797
- result = result * x;
2798
- }
2799
- return result;
2800
- };
2801
- let divideTernarySizes = (size) => {
2802
- if (size < 0) {
2803
- throw new Error("Unexpected negative size");
2804
- }
2805
- let extra = size % 3;
2806
- let groupSize = Math.floor(size / 3);
2807
- var leftSize = groupSize;
2808
- var middleSize = groupSize;
2809
- var rightSize = groupSize;
2810
- switch (extra) {
2811
- case 0:
2812
- break;
2813
- case 1:
2814
- middleSize = middleSize + 1;
2815
- break;
2816
- case 2:
2817
- leftSize = leftSize + 1;
2818
- rightSize = rightSize + 1;
2819
- break;
2820
- default:
2821
- throw new Error(`Unexpected mod result ${extra}`);
2822
- }
2823
- return { left: leftSize, middle: middleSize, right: rightSize };
2824
- };
2825
- function shallowCloneArray(xs) {
2826
- let ys = new Array(xs.length);
2827
- for (let i = 0; i < xs.length; i++) {
2828
- ys[i] = xs[i];
2829
- }
2830
- return ys;
2831
- }
2832
- let test = (name, cb) => {
2833
- console.log("Test:", name);
2834
- cb();
2835
- };
2836
- let check = (x) => {
2837
- if (!x) {
2838
- throw new Error("Test failed");
2839
- }
2840
- };
2841
- /** compare by reference */
2842
- let checkEqual = (x, y) => {
2843
- if (x !== y) {
2844
- console.log("Left: ", x);
2845
- console.log("Right: ", y);
2846
- throw new Error("Test failed");
2847
- }
2848
- };
2849
- let justDisplay = (x, y) => {
2850
- console.group("Compare:");
2851
- console.log(x);
2852
- console.log(y);
2853
- console.groupEnd();
2854
- };
2855
- let cmp = (x, y) => {
2856
- if (x < y) {
2857
- return -1;
2858
- }
2859
- if (x > y) {
2860
- return 1;
2861
- }
2862
- return 0;
2863
- };
2864
- // https://stackoverflow.com/a/25456134/883571
2865
- let deepEqual = function (x, y) {
2866
- if (x === y) {
2867
- return true;
2868
- }
2869
- else if (typeof x === "object" && x != null && typeof y === "object" && y != null) {
2870
- if (Object.keys(x).length != Object.keys(y).length)
2871
- return false;
2872
- for (var prop in x) {
2873
- if (y.hasOwnProperty(prop)) {
2874
- if (!deepEqual(x[prop], y[prop]))
2875
- return false;
2876
- }
2877
- else
2878
- return false;
2879
- }
2880
- return true;
2881
- }
2882
- else
2883
- return false;
2884
- };
2885
- let arrayEqual = (xs, ys) => {
2886
- if (xs.length != ys.length) {
2887
- return false;
2888
- }
2889
- for (let idx = 0; idx < xs.length; idx++) {
2890
- if (xs[idx] !== ys[idx]) {
2891
- return false;
2892
- }
2893
- }
2894
- return true;
2895
- };
2896
-
2897
-
2898
- /***/ })
2899
-
2900
- /******/ });
2901
- /************************************************************************/
2902
- /******/ // The module cache
2903
- /******/ var __webpack_module_cache__ = {};
2904
- /******/
2905
- /******/ // The require function
2906
- /******/ function __webpack_require__(moduleId) {
2907
- /******/ // Check if module is in cache
2908
- /******/ var cachedModule = __webpack_module_cache__[moduleId];
2909
- /******/ if (cachedModule !== undefined) {
2910
- /******/ return cachedModule.exports;
2911
- /******/ }
2912
- /******/ // Create a new module (and put it into the cache)
2913
- /******/ var module = __webpack_module_cache__[moduleId] = {
2914
- /******/ // no module.id needed
2915
- /******/ // no module.loaded needed
2916
- /******/ exports: {}
2917
- /******/ };
2918
- /******/
2919
- /******/ // Execute the module function
2920
- /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
2921
- /******/
2922
- /******/ // Return the exports of the module
2923
- /******/ return module.exports;
2924
- /******/ }
2925
- /******/
2926
- /************************************************************************/
2927
- /******/ /* webpack/runtime/define property getters */
2928
- /******/ (() => {
2929
- /******/ // define getter functions for harmony exports
2930
- /******/ __webpack_require__.d = (exports, definition) => {
2931
- /******/ for(var key in definition) {
2932
- /******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
2933
- /******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
2934
- /******/ }
2935
- /******/ }
2936
- /******/ };
2937
- /******/ })();
2938
- /******/
2939
- /******/ /* webpack/runtime/hasOwnProperty shorthand */
2940
- /******/ (() => {
2941
- /******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
2942
- /******/ })();
2943
- /******/
2944
- /******/ /* webpack/runtime/make namespace object */
2945
- /******/ (() => {
2946
- /******/ // define __esModule on exports
2947
- /******/ __webpack_require__.r = (exports) => {
2948
- /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
2949
- /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
2950
- /******/ }
2951
- /******/ Object.defineProperty(exports, '__esModule', { value: true });
2952
- /******/ };
2953
- /******/ })();
2954
- /******/
2955
- /************************************************************************/
2956
- var __webpack_exports__ = {};
2957
- // This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
2958
- (() => {
2959
- /*!*********************!*\
2960
- !*** ./lib/main.js ***!
2961
- \*********************/
2962
- __webpack_require__.r(__webpack_exports__);
2963
- /* harmony import */ var _utils__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./utils */ "./lib/utils.js");
2964
- /* harmony import */ var _test_list__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./test-list */ "./lib/test-list.js");
2965
- /* harmony import */ var _test_map__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./test-map */ "./lib/test-map.js");
2966
- /* harmony import */ var _types__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./types */ "./lib/types.js");
2967
-
2968
-
2969
-
2970
-
2971
-
2972
-
2973
- (0,_utils__WEBPACK_IMPORTED_MODULE_0__.overwriteComparator)((x, y) => {
2974
- // console.log("comparing", x, y);
2975
- return (0,_utils__WEBPACK_IMPORTED_MODULE_0__.deepEqual)(x, y);
2976
- });
2977
- (0,_types__WEBPACK_IMPORTED_MODULE_3__.overwriteHashGenerator)((x) => {
2978
- let ret = (0,_types__WEBPACK_IMPORTED_MODULE_3__.mergeValueHash)(10, (0,_types__WEBPACK_IMPORTED_MODULE_3__.valueHash)(x));
2979
- // console.log("hashing", x, ret);
2980
- return ret;
2981
- });
2982
- (0,_test_list__WEBPACK_IMPORTED_MODULE_1__.runListTests)();
2983
- (0,_test_map__WEBPACK_IMPORTED_MODULE_2__.runMapTests)();
2984
-
2985
- })();
2986
-
2987
- /******/ })()
2988
- ;