@calcit/ternary-tree 0.0.19 → 0.0.21

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/hash.d.mts CHANGED
@@ -1,3 +1,3 @@
1
- export declare type Hash = number;
1
+ export type Hash = number;
2
2
  export declare let startHash: (h: Hash, val: number) => Hash;
3
3
  export declare let finishHash: (h: Hash) => number;
package/lib/list.mjs CHANGED
@@ -47,6 +47,7 @@ export function makeTernaryTreeList(size, offset, xs) {
47
47
  right: emptyBranch,
48
48
  depth: decideParentDepth(left, middle),
49
49
  };
50
+ checkListStructure(result);
50
51
  return result;
51
52
  }
52
53
  case 3: {
@@ -61,6 +62,7 @@ export function makeTernaryTreeList(size, offset, xs) {
61
62
  right: right,
62
63
  depth: decideParentDepth(left, middle, right),
63
64
  };
65
+ checkListStructure(result);
64
66
  return result;
65
67
  }
66
68
  default: {
@@ -76,6 +78,7 @@ export function makeTernaryTreeList(size, offset, xs) {
76
78
  middle: middle,
77
79
  right: right,
78
80
  };
81
+ checkListStructure(result);
79
82
  return result;
80
83
  }
81
84
  }
@@ -91,10 +94,10 @@ export function initTernaryTreeList(xs) {
91
94
  }
92
95
  // from a slice of an existed array
93
96
  export function initTernaryTreeListFromRange(xs, from, to) {
94
- let ys = new Array(xs.length);
97
+ let ys = new Array(to - from);
95
98
  for (let idx = from; idx < to; idx++) {
96
99
  let x = xs[idx];
97
- ys[idx] = { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: x };
100
+ ys[idx - from] = { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: x };
98
101
  }
99
102
  return makeTernaryTreeList(xs.length, 0, ys);
100
103
  }
@@ -348,6 +351,7 @@ export function assocList(tree, idx, item) {
348
351
  middle: tree.middle,
349
352
  right: tree.right,
350
353
  };
354
+ checkListStructure(result);
351
355
  return result;
352
356
  }
353
357
  else if (idx <= leftSize + middleSize - 1) {
@@ -360,6 +364,7 @@ export function assocList(tree, idx, item) {
360
364
  middle: changedBranch,
361
365
  right: tree.right,
362
366
  };
367
+ checkListStructure(result);
363
368
  return result;
364
369
  }
365
370
  else {
@@ -372,6 +377,7 @@ export function assocList(tree, idx, item) {
372
377
  middle: tree.middle,
373
378
  right: changedBranch,
374
379
  };
380
+ checkListStructure(result);
375
381
  return result;
376
382
  }
377
383
  }
@@ -464,6 +470,7 @@ export function dissocList(tree, idx) {
464
470
  if (result.middle == null) {
465
471
  return result.left;
466
472
  }
473
+ checkListStructure(result);
467
474
  return result;
468
475
  }
469
476
  export function rest(tree) {
@@ -501,6 +508,7 @@ export function insert(tree, idx, item, after = false) {
501
508
  middle: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
502
509
  right: emptyBranch,
503
510
  };
511
+ checkListStructure(result);
504
512
  return result;
505
513
  }
506
514
  else {
@@ -512,9 +520,11 @@ export function insert(tree, idx, item, after = false) {
512
520
  middle: tree,
513
521
  right: emptyBranch,
514
522
  };
523
+ checkListStructure(result);
515
524
  return result;
516
525
  }
517
526
  }
527
+ checkListStructure(tree);
518
528
  if (listLen(tree) === 1) {
519
529
  if (after) {
520
530
  // in compact mode, values placed at left
@@ -526,6 +536,7 @@ export function insert(tree, idx, item, after = false) {
526
536
  middle: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
527
537
  right: emptyBranch,
528
538
  };
539
+ checkListStructure(result);
529
540
  return result;
530
541
  }
531
542
  else {
@@ -537,6 +548,7 @@ export function insert(tree, idx, item, after = false) {
537
548
  middle: tree.left,
538
549
  right: emptyBranch,
539
550
  };
551
+ checkListStructure(result);
540
552
  return result;
541
553
  }
542
554
  }
@@ -551,6 +563,7 @@ export function insert(tree, idx, item, after = false) {
551
563
  middle: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
552
564
  right: tree.middle,
553
565
  };
566
+ checkListStructure(result);
554
567
  return result;
555
568
  }
556
569
  if (idx === 1) {
@@ -562,6 +575,7 @@ export function insert(tree, idx, item, after = false) {
562
575
  middle: tree.middle,
563
576
  right: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
564
577
  };
578
+ checkListStructure(result);
565
579
  return result;
566
580
  }
567
581
  else {
@@ -578,6 +592,7 @@ export function insert(tree, idx, item, after = false) {
578
592
  middle: tree.left,
579
593
  right: tree.middle,
580
594
  };
595
+ checkListStructure(result);
581
596
  return result;
582
597
  }
583
598
  else if (idx === 1) {
@@ -589,6 +604,7 @@ export function insert(tree, idx, item, after = false) {
589
604
  middle: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
590
605
  right: tree.middle,
591
606
  };
607
+ checkListStructure(result);
592
608
  return result;
593
609
  }
594
610
  else {
@@ -613,6 +629,7 @@ export function insert(tree, idx, item, after = false) {
613
629
  middle: tree,
614
630
  right: emptyBranch,
615
631
  };
632
+ checkListStructure(result);
616
633
  return result;
617
634
  }
618
635
  }
@@ -626,6 +643,7 @@ export function insert(tree, idx, item, after = false) {
626
643
  middle: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
627
644
  right: emptyBranch,
628
645
  };
646
+ checkListStructure(result);
629
647
  return result;
630
648
  }
631
649
  }
@@ -638,6 +656,7 @@ export function insert(tree, idx, item, after = false) {
638
656
  middle: tree.middle,
639
657
  right: { kind: TernaryTreeKind.ternaryTreeLeaf, size: 1, value: item },
640
658
  };
659
+ checkListStructure(result);
641
660
  return result;
642
661
  }
643
662
  if (!after && idx === 0 && rightSize === 0 && middleSize >= rightSize) {
@@ -649,6 +668,7 @@ export function insert(tree, idx, item, after = false) {
649
668
  middle: tree.left,
650
669
  right: tree.middle,
651
670
  };
671
+ checkListStructure(result);
652
672
  return result;
653
673
  }
654
674
  if (idx <= leftSize - 1) {
@@ -661,6 +681,7 @@ export function insert(tree, idx, item, after = false) {
661
681
  middle: tree.middle,
662
682
  right: tree.right,
663
683
  };
684
+ checkListStructure(result);
664
685
  return result;
665
686
  }
666
687
  else if (idx <= leftSize + middleSize - 1) {
@@ -673,6 +694,7 @@ export function insert(tree, idx, item, after = false) {
673
694
  middle: changedBranch,
674
695
  right: tree.right,
675
696
  };
697
+ checkListStructure(result);
676
698
  return result;
677
699
  }
678
700
  else {
@@ -685,6 +707,7 @@ export function insert(tree, idx, item, after = false) {
685
707
  middle: tree.middle,
686
708
  right: changedBranch,
687
709
  };
710
+ checkListStructure(result);
688
711
  return result;
689
712
  }
690
713
  }
@@ -743,6 +766,7 @@ export function concat(...xsGroups) {
743
766
  xsGroups = xsGroups.filter((xs) => listLen(xs) > 0);
744
767
  let result = makeTernaryTreeList(xsGroups.length, 0, xsGroups);
745
768
  maybeReblance(result);
769
+ checkListStructure(result);
746
770
  return result;
747
771
  }
748
772
  export function sameListShape(xs, ys) {
@@ -808,9 +832,18 @@ export function checkListStructure(tree) {
808
832
  }
809
833
  break;
810
834
  case TernaryTreeKind.ternaryTreeBranch: {
835
+ if (tree.size >= 6 && tree.depth >= tree.size) {
836
+ throw new Error(`Bad depth at branch ${formatListInline(tree)}`);
837
+ }
811
838
  if (tree.size !== listLen(tree.left) + listLen(tree.middle) + listLen(tree.right)) {
812
839
  throw new Error(`Bad size at branch ${formatListInline(tree)}`);
813
840
  }
841
+ if (tree.left == null && tree.middle != null) {
842
+ throw new Error("morformed tree");
843
+ }
844
+ if (tree.middle == null && tree.right != null) {
845
+ throw new Error("morformed tree");
846
+ }
814
847
  if (tree.depth !== decideParentDepth(tree.left, tree.middle, tree.right)) {
815
848
  let x = decideParentDepth(tree.left, tree.middle, tree.right);
816
849
  throw new Error(`Bad depth at branch ${formatListInline(tree)}`);
@@ -896,6 +929,11 @@ export function reverse(tree) {
896
929
  middle: reverse(tree.middle),
897
930
  right: reverse(tree.left),
898
931
  };
932
+ if (result.left == null) {
933
+ result.left = result.middle;
934
+ result.middle = result.right;
935
+ result.right = undefined;
936
+ }
899
937
  return result;
900
938
  }
901
939
  }
package/lib/types.d.mts CHANGED
@@ -2,7 +2,7 @@ export declare enum TernaryTreeKind {
2
2
  ternaryTreeBranch = 0,
3
3
  ternaryTreeLeaf = 1
4
4
  }
5
- export declare type TernaryTreeListTheBranch<T> = {
5
+ export type TernaryTreeListTheBranch<T> = {
6
6
  size: number;
7
7
  kind: TernaryTreeKind.ternaryTreeBranch;
8
8
  depth: number;
@@ -10,17 +10,17 @@ export declare type TernaryTreeListTheBranch<T> = {
10
10
  middle: TernaryTreeList<T>;
11
11
  right: TernaryTreeList<T>;
12
12
  };
13
- export declare type TernaryTreeListTheLeaf<T> = {
13
+ export type TernaryTreeListTheLeaf<T> = {
14
14
  size: number;
15
15
  kind: TernaryTreeKind.ternaryTreeLeaf;
16
16
  value: T;
17
17
  };
18
- export declare type TernaryTreeList<T> = TernaryTreeListTheBranch<T> | TernaryTreeListTheLeaf<T>;
19
- export declare type TernaryTreeMapHashEntry<K, V> = {
18
+ export type TernaryTreeList<T> = TernaryTreeListTheBranch<T> | TernaryTreeListTheLeaf<T>;
19
+ export type TernaryTreeMapHashEntry<K, V> = {
20
20
  hash: Hash;
21
21
  pairs: Array<[K, V]>;
22
22
  };
23
- export declare type TernaryTreeMapTheBranch<K, T> = {
23
+ export type TernaryTreeMapTheBranch<K, T> = {
24
24
  kind: TernaryTreeKind.ternaryTreeBranch;
25
25
  depth: number;
26
26
  maxHash: number;
@@ -29,16 +29,16 @@ export declare type TernaryTreeMapTheBranch<K, T> = {
29
29
  middle: TernaryTreeMap<K, T>;
30
30
  right: TernaryTreeMap<K, T>;
31
31
  };
32
- export declare type TernaryTreeMapTheLeaf<K, T> = {
32
+ export type TernaryTreeMapTheLeaf<K, T> = {
33
33
  kind: TernaryTreeKind.ternaryTreeLeaf;
34
34
  hash: number;
35
35
  elements: Array<[K, T]>;
36
36
  };
37
- export declare type TernaryTreeMap<K, T> = TernaryTreeMapTheBranch<K, T> | TernaryTreeMapTheLeaf<K, T>;
38
- export declare type RefInt = {
37
+ export type TernaryTreeMap<K, T> = TernaryTreeMapTheBranch<K, T> | TernaryTreeMapTheLeaf<K, T>;
38
+ export type RefInt = {
39
39
  value: number;
40
40
  };
41
- export declare type Hash = number;
41
+ export type Hash = number;
42
42
  export declare let valueHash: (x: any) => Hash;
43
43
  /** default hash function only handles number and string, need customization */
44
44
  export declare let hashGenerator: typeof valueHash;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@calcit/ternary-tree",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "main": "./lib/index.mjs",
5
5
  "scripts": {},
6
6
  "devDependencies": {
7
- "prettier": "^2.6.0",
8
- "typescript": "^4.6.2"
7
+ "prettier": "^3.0.0",
8
+ "typescript": "^5.1.6"
9
9
  }
10
10
  }
package/lib/index.html DELETED
@@ -1,2 +0,0 @@
1
-
2
- <script type="module" src="main.mjs" ></script>