@calcit/ternary-tree 0.0.22 → 0.0.24

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/list.d.mts CHANGED
@@ -26,8 +26,12 @@ export declare function forceListInplaceBalancing<T>(tree: TernaryTreeList<T>):
26
26
  export declare function prepend<T>(tree: TernaryTreeList<T>, item: T, disableBalancing?: boolean): TernaryTreeList<T>;
27
27
  export declare function append<T>(tree: TernaryTreeList<T>, item: T, disableBalancing?: boolean): TernaryTreeList<T>;
28
28
  export declare function concat<T>(...xsGroups: Array<TernaryTreeList<T>>): TernaryTreeList<T>;
29
+ export declare function concat2<T>(left: TernaryTreeList<T>, middle: TernaryTreeList<T>): TernaryTreeList<T>;
30
+ export declare function concat3<T>(left: TernaryTreeList<T>, middle: TernaryTreeList<T>, right: TernaryTreeList<T>): TernaryTreeList<T>;
29
31
  export declare function sameListShape<T>(xs: TernaryTreeList<T>, ys: TernaryTreeList<T>): boolean;
30
32
  export declare function listEqual<T>(xs: TernaryTreeList<T>, ys: TernaryTreeList<T>): boolean;
33
+ /** in some cases we disable for performance */
34
+ export declare let disableListStructureCheck: () => void;
31
35
  export declare function checkListStructure<T>(tree: TernaryTreeList<T>): boolean;
32
36
  export declare function slice<T>(tree: TernaryTreeList<T>, startIdx: number, endIdx: number): TernaryTreeList<T>;
33
37
  export declare function reverse<T>(tree: TernaryTreeList<T>): TernaryTreeList<T>;
package/lib/list.mjs CHANGED
@@ -736,8 +736,8 @@ export function forceListInplaceBalancing(tree) {
736
736
  // TODO, need better strategy for detecting
737
737
  function maybeReblance(tree) {
738
738
  let currentDepth = getDepth(tree);
739
- if (currentDepth > 50) {
740
- if (roughIntPow(3, currentDepth - 50) > tree.size) {
739
+ if (currentDepth > 10) {
740
+ if (roughIntPow(3, currentDepth - 10) > tree.size) {
741
741
  forceListInplaceBalancing(tree);
742
742
  }
743
743
  }
@@ -764,11 +764,70 @@ export function append(tree, item, disableBalancing = false) {
764
764
  }
765
765
  export function concat(...xsGroups) {
766
766
  xsGroups = xsGroups.filter((xs) => listLen(xs) > 0);
767
+ if (xsGroups.length === 1) {
768
+ return xsGroups[0];
769
+ }
770
+ if (xsGroups.length === 2) {
771
+ return concat2(xsGroups[0], xsGroups[1]);
772
+ }
773
+ if (xsGroups.length === 3) {
774
+ return concat3(xsGroups[0], xsGroups[1], xsGroups[2]);
775
+ }
767
776
  let result = makeTernaryTreeList(xsGroups.length, 0, xsGroups);
768
777
  maybeReblance(result);
769
778
  checkListStructure(result);
770
779
  return result;
771
780
  }
781
+ export function concat2(left, middle) {
782
+ if (left.kind === TernaryTreeKind.ternaryTreeBranch) {
783
+ if (left.left != null && left.middle != null && left.right == null) {
784
+ let ret = {
785
+ size: left.size + middle.size,
786
+ kind: TernaryTreeKind.ternaryTreeBranch,
787
+ depth: decideParentDepth(left.left, left.middle, middle),
788
+ left: left.left,
789
+ middle: left.middle,
790
+ right: middle,
791
+ };
792
+ return ret;
793
+ }
794
+ }
795
+ if (middle.kind === TernaryTreeKind.ternaryTreeBranch) {
796
+ if (middle.left != null && middle.middle != null && middle.right == null) {
797
+ let ret = {
798
+ size: left.size + middle.size,
799
+ kind: TernaryTreeKind.ternaryTreeBranch,
800
+ depth: decideParentDepth(left, middle.left, middle.middle),
801
+ left: left,
802
+ middle: middle.left,
803
+ right: middle.middle,
804
+ };
805
+ return ret;
806
+ }
807
+ }
808
+ let ret = {
809
+ size: left.size + middle.size,
810
+ kind: TernaryTreeKind.ternaryTreeBranch,
811
+ depth: decideParentDepth(left, middle),
812
+ left: left,
813
+ middle: middle,
814
+ right: emptyBranch,
815
+ };
816
+ checkListStructure(ret);
817
+ return ret;
818
+ }
819
+ export function concat3(left, middle, right) {
820
+ let ret = {
821
+ size: left.size + middle.size + right.size,
822
+ kind: TernaryTreeKind.ternaryTreeBranch,
823
+ depth: decideParentDepth(left, middle, right),
824
+ left,
825
+ middle,
826
+ right,
827
+ };
828
+ checkListStructure(ret);
829
+ return ret;
830
+ }
772
831
  export function sameListShape(xs, ys) {
773
832
  if (xs == null) {
774
833
  if (ys == null) {
@@ -820,7 +879,15 @@ export function listEqual(xs, ys) {
820
879
  }
821
880
  return true;
822
881
  }
882
+ var skipListStructureCheck = false;
883
+ /** in some cases we disable for performance */
884
+ export let disableListStructureCheck = () => {
885
+ skipListStructureCheck = true;
886
+ };
823
887
  export function checkListStructure(tree) {
888
+ if (skipListStructureCheck) {
889
+ return true;
890
+ }
824
891
  if (tree == null || listLen(tree) === 0) {
825
892
  return true;
826
893
  }
package/lib/main.mjs CHANGED
@@ -4,6 +4,8 @@ import "./test-map.mjs";
4
4
  import { runListTests } from "./test-list.mjs";
5
5
  import { runMapTests } from "./test-map.mjs";
6
6
  import { mergeValueHash, overwriteHashGenerator, valueHash } from "./types.mjs";
7
+ // import { disableListStructureCheck } from "./list.mjs";
8
+ // disableListStructureCheck();
7
9
  overwriteComparator((x, y) => {
8
10
  // console.log("comparing", x, y);
9
11
  return deepEqual(x, y);
package/lib/test-list.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { listToString, initTernaryTreeList, initTernaryTreeListFromRange, indexOf, findIndex, reverse, checkListStructure, slice, listToPairs, listToItems, formatListInline, sameListShape, assocBefore, concat, assocAfter, prepend, append, rest, butlast, first, assocList, dissocList, listGet, insert, initEmptyTernaryTreeList, last, listLen, forceListInplaceBalancing, listEqual, indexToItems, listMapValues, } from "./list.mjs";
2
- import { test, check, arrayEqual, checkEqual } from "./utils.mjs";
1
+ import { listToString, initTernaryTreeList, initTernaryTreeListFromRange, indexOf, findIndex, reverse, checkListStructure, slice, listToPairs, listToItems, formatListInline, sameListShape, assocBefore, concat, assocAfter, prepend, append, rest, butlast, first, assocList, dissocList, listGet, insert, initEmptyTernaryTreeList, last, listLen, forceListInplaceBalancing, listEqual, indexToItems, listMapValues, concat2, } from "./list.mjs";
2
+ import { test, check, arrayEqual, checkEqual } from "./test-utils.mjs";
3
3
  export let runListTests = () => {
4
4
  test("init list", () => {
5
5
  check(listToString(initTernaryTreeList([1, 2, 3, 4])) === "TernaryTreeList[4, ...]");
@@ -75,7 +75,7 @@ export let runListTests = () => {
75
75
  let data2 = initTernaryTreeList([3, 4]);
76
76
  let data3 = initTernaryTreeList([5, 6]);
77
77
  let data4 = initTernaryTreeList([7, 8]);
78
- check(formatListInline(concat(data1, data2)) === "((1 2 _) (3 4 _) _)");
78
+ check(formatListInline(concat(data1, data2)) === "(1 2 (3 4 _))");
79
79
  check(formatListInline(concat(initTernaryTreeList([]), data1)) === "(1 2 _)");
80
80
  check(formatListInline(concat(data1, data2, data3)) === "((1 2 _) (3 4 _) (5 6 _))");
81
81
  check(formatListInline(concat(data1, data2, data3, data4)) === "((1 2 _) ((3 4 _) (5 6 _) _) (7 8 _))");
@@ -197,4 +197,12 @@ export let runListTests = () => {
197
197
  checkListStructure(data1);
198
198
  checkListStructure(data2);
199
199
  });
200
+ test("concat loop", () => {
201
+ let data1 = initTernaryTreeList([3, 4]);
202
+ let data2 = initTernaryTreeList([5]);
203
+ for (let i = 0; i < 100; i++) {
204
+ data1 = concat2(data1, data2);
205
+ }
206
+ console.log("concat into depth", formatListInline(data1));
207
+ });
200
208
  };
package/lib/test-map.mjs CHANGED
@@ -1,5 +1,6 @@
1
1
  import { hashGenerator } from "./types.mjs";
2
- import { test, check, cmp, deepEqual, justDisplay } from "./utils.mjs";
2
+ import { cmp, deepEqual } from "./utils.mjs";
3
+ import { test, check, justDisplay } from "./test-utils.mjs";
3
4
  import { initTernaryTreeMap, initTernaryTreeMapFromArray, toHashSortedPairs, merge, mergeSkip, formatMapInline, assocMap, dissocMap, contains, toPairs, initEmptyTernaryTreeMap, sameMapShape, checkMapStructure, mapLen, mapEqual, toKeys, toPairsArray, mapMapValues, mapGetDefault, } from "./map.mjs";
4
5
  export let runMapTests = () => {
5
6
  test("init map", () => {
@@ -0,0 +1,6 @@
1
+ export declare let test: (name: string, cb: () => void) => void;
2
+ export declare let check: (x: boolean) => void;
3
+ /** compare by reference */
4
+ export declare let checkEqual: (x: any, y: any) => void;
5
+ export declare function arrayEqual<T>(xs: Array<T>, ys: Array<T>): boolean;
6
+ export declare let justDisplay: (x: any, y: any) => void;
@@ -0,0 +1,37 @@
1
+ export let test = (name, cb) => {
2
+ let target = process === null || process === void 0 ? void 0 : process.env["target"];
3
+ if (target == null || name.includes(target)) {
4
+ console.log("Test:", name);
5
+ cb();
6
+ }
7
+ };
8
+ export let check = (x) => {
9
+ if (!x) {
10
+ throw new Error("Test failed");
11
+ }
12
+ };
13
+ /** compare by reference */
14
+ export let checkEqual = (x, y) => {
15
+ if (x !== y) {
16
+ console.log("Left: ", x);
17
+ console.log("Right: ", y);
18
+ throw new Error("Test failed");
19
+ }
20
+ };
21
+ export function arrayEqual(xs, ys) {
22
+ if (xs.length != ys.length) {
23
+ return false;
24
+ }
25
+ for (let idx = 0; idx < xs.length; idx++) {
26
+ if (xs[idx] !== ys[idx]) {
27
+ return false;
28
+ }
29
+ }
30
+ return true;
31
+ }
32
+ export let justDisplay = (x, y) => {
33
+ console.group("Compare:");
34
+ console.log(x);
35
+ console.log(y);
36
+ console.groupEnd();
37
+ };
package/lib/utils.d.mts CHANGED
@@ -10,11 +10,5 @@ export declare let divideTernarySizes: (size: number) => {
10
10
  right: number;
11
11
  };
12
12
  export declare function shallowCloneArray<T>(xs: Array<T>): Array<T>;
13
- export declare let test: (name: string, cb: () => void) => void;
14
- export declare let check: (x: boolean) => void;
15
- /** compare by reference */
16
- export declare let checkEqual: (x: any, y: any) => void;
17
- export declare let justDisplay: (x: any, y: any) => void;
18
13
  export declare let cmp: (x: any, y: any) => 0 | 1 | -1;
19
14
  export declare let deepEqual: (x: any, y: any) => boolean;
20
- export declare function arrayEqual<T>(xs: Array<T>, ys: Array<T>): boolean;
package/lib/utils.mjs CHANGED
@@ -48,29 +48,6 @@ export function shallowCloneArray(xs) {
48
48
  }
49
49
  return ys;
50
50
  }
51
- export let test = (name, cb) => {
52
- console.log("Test:", name);
53
- cb();
54
- };
55
- export let check = (x) => {
56
- if (!x) {
57
- throw new Error("Test failed");
58
- }
59
- };
60
- /** compare by reference */
61
- export let checkEqual = (x, y) => {
62
- if (x !== y) {
63
- console.log("Left: ", x);
64
- console.log("Right: ", y);
65
- throw new Error("Test failed");
66
- }
67
- };
68
- export let justDisplay = (x, y) => {
69
- console.group("Compare:");
70
- console.log(x);
71
- console.log(y);
72
- console.groupEnd();
73
- };
74
51
  export let cmp = (x, y) => {
75
52
  if (x < y) {
76
53
  return -1;
@@ -101,14 +78,3 @@ export let deepEqual = function (x, y) {
101
78
  else
102
79
  return false;
103
80
  };
104
- export function arrayEqual(xs, ys) {
105
- if (xs.length != ys.length) {
106
- return false;
107
- }
108
- for (let idx = 0; idx < xs.length; idx++) {
109
- if (xs[idx] !== ys[idx]) {
110
- return false;
111
- }
112
- }
113
- return true;
114
- }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@calcit/ternary-tree",
3
- "version": "0.0.22",
3
+ "version": "0.0.24",
4
4
  "main": "./lib/index.mjs",
5
5
  "scripts": {},
6
6
  "devDependencies": {
7
- "prettier": "^3.0.0",
8
- "typescript": "^5.1.6"
7
+ "@types/node": "^20.12.5",
8
+ "prettier": "^3.2.5",
9
+ "typescript": "^5.4.4"
9
10
  }
10
11
  }