@calcit/ternary-tree 0.0.16 → 0.0.19-a1

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/src/shape.nim DELETED
@@ -1,62 +0,0 @@
1
- # TODO get a ts version
2
-
3
- import random
4
- import tables
5
- import strformat
6
-
7
- import ternary_tree
8
-
9
- randomize()
10
-
11
- proc tryListShape(): void =
12
- var source: seq[int] = @[]
13
- var data = initTernaryTreeList[int](@[0])
14
-
15
- for i in 0..<18:
16
- source.add i
17
-
18
- data = initTernaryTreeList(source)
19
-
20
- for i in 0..<18:
21
- # echo data.formatInline
22
- let newData = data.assocAfter(i, 888)
23
- echo newData.getDepth, " : ", newData.formatInline
24
-
25
- # data.forceInplaceBalancing
26
- # echo data.getDepth, " : ", data.formatInline
27
-
28
-
29
- proc tryMapShape(): void =
30
- var dict: Table[string, int]
31
- var data = initTernaryTreeMap(dict)
32
-
33
- for idx in 0..<20:
34
- data = data.assoc(fmt"x{idx}", idx + 10)
35
- echo data.formatInline()
36
- # echo "checked: ", data.checkStructure()
37
-
38
- proc tryConcatList(): void =
39
- let a = initTernaryTreeList(@[1,2,3,4])
40
- let b = initTernaryTreeList(@[5,6,7,8])
41
- let c = initTernaryTreeList(@[9,10,11,12])
42
-
43
- echo a.formatInline
44
- echo b.formatInline
45
- echo c.formatInline
46
- echo a.concat(b).formatInline
47
- echo a.concat(b).concat(c).formatInline
48
-
49
- let d = a.concat(b)
50
- d.forceInplaceBalancing
51
-
52
- echo d.formatInline
53
-
54
- for i in 0..<8:
55
- for j in i..<9:
56
- echo fmt"{i}-{j} ", d.slice(i, j).formatInline
57
-
58
- # tryListShape()
59
-
60
- # tryMapShape()
61
-
62
- tryConcatList()
package/src/test-list.ts DELETED
@@ -1,264 +0,0 @@
1
- import {
2
- listToString,
3
- initTernaryTreeList,
4
- indexOf,
5
- findIndex,
6
- reverse,
7
- checkListStructure,
8
- slice,
9
- listToPairs,
10
- listToItems,
11
- formatListInline,
12
- sameListShape,
13
- assocBefore,
14
- concat,
15
- assocAfter,
16
- prepend,
17
- append,
18
- rest,
19
- butlast,
20
- first,
21
- assocList,
22
- dissocList,
23
- listGet,
24
- insert,
25
- initEmptyTernaryTreeList,
26
- last,
27
- listLen,
28
- forceListInplaceBalancing,
29
- listEqual,
30
- indexToItems,
31
- listMapValues,
32
- } from "./list";
33
-
34
- import { test, check, arrayEqual, checkEqual } from "./utils";
35
-
36
- export let runListTests = () => {
37
- test("init list", () => {
38
- check(
39
- listToString(
40
- initTernaryTreeList<number>([1, 2, 3, 4])
41
- ) === "TernaryTreeList[4, ...]"
42
- );
43
-
44
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
45
- let data11 = initTernaryTreeList<number>(origin11);
46
-
47
- check(checkListStructure(data11));
48
-
49
- checkEqual(formatListInline(data11), "((1 (2 3 _) 4) (5 6 7) (8 (9 10 _) 11))");
50
- check(
51
- arrayEqual<number>(origin11, [...listToItems(data11)])
52
- );
53
-
54
- check(arrayEqual<number>([...listToItems(data11)], [...indexToItems(data11)]));
55
-
56
- let emptyXs = new Array<number>(0);
57
- check(listEqual(initEmptyTernaryTreeList<number>(), initTernaryTreeList(emptyXs)));
58
- });
59
-
60
- test("list operations", () => {
61
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
62
- let data11 = initTernaryTreeList<number>(origin11);
63
-
64
- // get
65
- for (let idx = 0; idx < origin11.length; idx++) {
66
- check(origin11[idx] === listGet(data11, idx));
67
- }
68
-
69
- check(first(data11) === 1);
70
- check(last(data11) === 11);
71
-
72
- // assoc
73
- let origin5 = [1, 2, 3, 4, 5];
74
- let data5 = initTernaryTreeList(origin5);
75
- let updated = assocList(data5, 3, 10);
76
- check(listGet(updated, 3) === 10);
77
- check(listGet(data5, 3) === 4);
78
- check(listLen(updated) === listLen(data5));
79
-
80
- for (let idx = 0; idx < listLen(data5); idx++) {
81
- // echo data5.dissoc(idx).formatInline
82
- check(listLen(dissocList(data5, idx)) === listLen(data5) - 1);
83
- }
84
-
85
- checkEqual(formatListInline(data5), "((1 2 _) 3 (4 5 _))");
86
- checkEqual(formatListInline(dissocList(data5, 0)), "(2 3 (4 5 _))");
87
- checkEqual(formatListInline(dissocList(data5, 1)), "(1 3 (4 5 _))");
88
- checkEqual(formatListInline(dissocList(data5, 2)), "((1 2 _) (4 5 _) _)");
89
- checkEqual(formatListInline(dissocList(data5, 3)), "((1 2 _) 3 5)");
90
- checkEqual(formatListInline(dissocList(data5, 4)), "((1 2 _) 3 4)");
91
-
92
- checkEqual(formatListInline(rest(initTernaryTreeList([1]))), "_");
93
- checkEqual(formatListInline(rest(initTernaryTreeList([1, 2]))), "2");
94
- checkEqual(formatListInline(rest(initTernaryTreeList([1, 2, 3]))), "(2 3 _)");
95
- checkEqual(formatListInline(rest(initTernaryTreeList([1, 2, 3, 4]))), "((2 3 _) 4 _)");
96
- checkEqual(formatListInline(rest(initTernaryTreeList([1, 2, 3, 4, 5]))), "(2 3 (4 5 _))");
97
-
98
- checkEqual(formatListInline(butlast(initTernaryTreeList([1]))), "_");
99
- checkEqual(formatListInline(butlast(initTernaryTreeList([1, 2]))), "1");
100
- checkEqual(formatListInline(butlast(initTernaryTreeList([1, 2, 3]))), "(1 2 _)");
101
- checkEqual(formatListInline(butlast(initTernaryTreeList([1, 2, 3, 4]))), "(1 (2 3 _) _)");
102
- checkEqual(formatListInline(butlast(initTernaryTreeList([1, 2, 3, 4, 5]))), "((1 2 _) 3 4)");
103
- });
104
-
105
- test("list insertions", () => {
106
- let origin5 = [1, 2, 3, 4, 5];
107
- let data5 = initTernaryTreeList(origin5);
108
-
109
- checkEqual(formatListInline(data5), "((1 2 _) 3 (4 5 _))");
110
-
111
- checkEqual(formatListInline(insert(data5, 0, 10, false)), "(10 ((1 2 _) 3 (4 5 _)) _)");
112
- checkEqual(formatListInline(insert(data5, 0, 10, true)), "((1 10 2) 3 (4 5 _))");
113
- checkEqual(formatListInline(insert(data5, 1, 10, false)), "((1 10 2) 3 (4 5 _))");
114
- checkEqual(formatListInline(insert(data5, 1, 10, true)), "((1 2 10) 3 (4 5 _))");
115
- checkEqual(formatListInline(insert(data5, 2, 10, false)), "((1 2 _) (10 3 _) (4 5 _))");
116
- checkEqual(formatListInline(insert(data5, 2, 10, true)), "((1 2 _) (3 10 _) (4 5 _))");
117
- checkEqual(formatListInline(insert(data5, 3, 10, false)), "((1 2 _) 3 (10 4 5))");
118
- checkEqual(formatListInline(insert(data5, 3, 10, true)), "((1 2 _) 3 (4 10 5))");
119
- checkEqual(formatListInline(insert(data5, 4, 10, false)), "((1 2 _) 3 (4 10 5))");
120
- checkEqual(formatListInline(insert(data5, 4, 10, true)), "(((1 2 _) 3 (4 5 _)) 10 _)");
121
-
122
- let origin4 = [1, 2, 3, 4];
123
- let data4 = initTernaryTreeList(origin4);
124
-
125
- checkEqual(formatListInline(assocBefore(data4, 3, 10)), "(1 (2 3 _) (10 4 _))");
126
- checkEqual(formatListInline(assocAfter(data4, 3, 10)), "(1 (2 3 _) (4 10 _))");
127
-
128
- checkEqual(formatListInline(prepend(data4, 10)), "((10 1 _) (2 3 _) 4)");
129
- checkEqual(formatListInline(append(data4, 10)), "(1 (2 3 _) (4 10 _))");
130
- });
131
-
132
- test("concat", () => {
133
- let data1 = initTernaryTreeList([1, 2]);
134
- let data2 = initTernaryTreeList([3, 4]);
135
-
136
- let data3 = initTernaryTreeList([5, 6]);
137
- let data4 = initTernaryTreeList([7, 8]);
138
-
139
- check(formatListInline(concat(data1, data2)) === "((1 2 _) (3 4 _) _)");
140
- check(formatListInline(concat(initTernaryTreeList<number>([]), data1)) === "(1 2 _)");
141
- check(formatListInline(concat(data1, data2, data3)) === "((1 2 _) (3 4 _) (5 6 _))");
142
- check(formatListInline(concat(data1, data2, data3, data4)) === "((1 2 _) ((3 4 _) (5 6 _) _) (7 8 _))");
143
-
144
- checkListStructure(concat(data1, data2));
145
- checkListStructure(concat(data1, data2, data3));
146
- checkListStructure(concat(data1, data2, data3, data4));
147
-
148
- check(listLen(concat(data1, data2, data3, data4)) === 8);
149
- });
150
-
151
- test("check(equality", () => {
152
- let origin4 = [1, 2, 3, 4];
153
- let data4 = initTernaryTreeList(origin4);
154
- let data4n = initTernaryTreeList(origin4);
155
- let data4Made = prepend(initTernaryTreeList([2, 3, 4]), 1);
156
-
157
- check(sameListShape(data4, data4) === true);
158
- check(sameListShape(data4, data4n) === true);
159
- check(sameListShape(data4, data4Made) === false);
160
-
161
- check(listEqual(data4, data4n));
162
- check(listEqual(data4, data4Made));
163
- check(listEqual(data4n, data4Made));
164
- check(data4 !== data4Made); // identical false
165
- });
166
-
167
- test("force balancing", () => {
168
- var data = initTernaryTreeList<number>([]);
169
- for (let idx = 0; idx < 20; idx++) {
170
- data = append(data, idx, true);
171
- }
172
- // echo data.formatInline
173
- check(formatListInline(data) === "(((0 1 2) (3 4 5) (6 7 8)) ((9 10 11) (12 13 14) (15 16 17)) (18 19 _))");
174
- forceListInplaceBalancing(data);
175
- check(formatListInline(data) === "(((0 1 _) (2 3 4) (5 6 _)) ((7 8 _) (9 10 _) (11 12 _)) ((13 14 _) (15 16 17) (18 19 _)))");
176
- // echo data.formatInline
177
- });
178
-
179
- test("iterator", () => {
180
- let origin4 = [1, 2, 3, 4];
181
- let data4 = initTernaryTreeList(origin4);
182
-
183
- var i = 0;
184
- for (let item of listToItems(data4)) {
185
- i = i + 1;
186
- }
187
-
188
- check(i === 4);
189
-
190
- i = 0;
191
- for (let [idx, item] of listToPairs(data4)) {
192
- i = i + idx;
193
- }
194
-
195
- check(i === 6);
196
- });
197
-
198
- test("check structure", () => {
199
- var data = initTernaryTreeList<number>([]);
200
- for (let idx = 0; idx < 20; idx++) {
201
- data = append(data, idx, true);
202
- }
203
-
204
- check(checkListStructure(data));
205
-
206
- let origin11 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
207
- let data11 = initTernaryTreeList<number>(origin11);
208
-
209
- check(checkListStructure(data11));
210
- });
211
-
212
- test("slices", () => {
213
- var data = initTernaryTreeList<number>([]);
214
- for (let idx = 0; idx < 40; idx++) {
215
- data = append(data, idx, true);
216
- }
217
-
218
- var list40: Array<number> = [];
219
- for (let idx = 0; idx < 40; idx++) {
220
- list40.push(idx);
221
- }
222
-
223
- for (let i = 0; i < 40; i++) {
224
- for (let j = i; j < 40; j++) {
225
- check(arrayEqual<number>([...listToItems(slice(data, i, j))], list40.slice(i, j)));
226
- }
227
- }
228
- });
229
-
230
- test("reverse", () => {
231
- let data = initTernaryTreeList([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
232
- let reversedData = reverse(data);
233
- check(arrayEqual([...listToItems(data)].reverse(), [...listToItems(reversedData)]));
234
- check(checkListStructure(reversedData));
235
- });
236
-
237
- test("list traverse", () => {
238
- var i = 0;
239
- let data = initTernaryTreeList<number>([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
240
- for (let x of listToItems(data)) {
241
- i = i + 1;
242
- }
243
-
244
- check(i === 10);
245
- });
246
-
247
- test("index of", () => {
248
- let data = initTernaryTreeList<number>([1, 2, 3, 4, 5, 6, 7, 8]);
249
- check(indexOf(data, 2) === 1);
250
- check(findIndex(data, (x: number): boolean => x === 2) === 1);
251
- check(indexOf(data, 9) === -1);
252
- check(findIndex(data, (x: number): boolean => x === 9) === -1);
253
- });
254
-
255
- test("map values", () => {
256
- let data = initTernaryTreeList<number>([1, 2, 3, 4]);
257
- let data2 = initTernaryTreeList<number>([1, 4, 9, 16]);
258
- let data3 = listMapValues(data, (x) => x * x);
259
-
260
- checkListStructure(data3);
261
- check(listEqual(data2, data3));
262
- check(formatListInline(data2) == formatListInline(data3));
263
- });
264
- };
package/src/test-map.ts DELETED
@@ -1,287 +0,0 @@
1
- import { hashGenerator } from "./types";
2
- import { test, check, cmp, deepEqual, justDisplay } from "./utils";
3
- import {
4
- initTernaryTreeMap,
5
- toHashSortedPairs,
6
- merge,
7
- mergeSkip,
8
- forceMapInplaceBalancing,
9
- formatMapInline,
10
- assocMap,
11
- dissocMap,
12
- contains,
13
- toPairs,
14
- initEmptyTernaryTreeMap,
15
- sameMapShape,
16
- checkMapStructure,
17
- mapLen,
18
- mapToString,
19
- mapEqual,
20
- toKeys,
21
- toPairsArray,
22
- mapMapValues,
23
- mapGetDefault,
24
- } from "./map";
25
-
26
- export let runMapTests = () => {
27
- test("init map", () => {
28
- var dict: Map<string, number> = new Map();
29
- var inList: Array<[string, number]> = [];
30
- for (let idx = 0; idx < 10; idx++) {
31
- dict.set(`${idx}`, idx + 10);
32
- inList.push([`${idx}`, idx + 10]);
33
- }
34
-
35
- // TODO
36
- inList.sort((x, y: [string, number]): number => {
37
- let hx = hashGenerator(x[0]);
38
- let hy = hashGenerator(y[0]);
39
- return cmp(hx, hy);
40
- });
41
-
42
- let data10 = initTernaryTreeMap<string, number>(dict);
43
- let data11 = initTernaryTreeMap<string, number>(inList);
44
- checkMapStructure(data10);
45
- checkMapStructure(data11);
46
-
47
- // echo data10
48
- justDisplay(formatMapInline(data10, true), " ((0:10 1:11 2:12) (3:13 (4:14 5:15 _) 6:16) (7:17 8:18 9:19))");
49
-
50
- check(deepEqual(toHashSortedPairs(data10), inList));
51
- check(deepEqual(toHashSortedPairs(data11), inList));
52
-
53
- check(contains(data10, "1") == true);
54
- check(contains(data10, "11") == false);
55
-
56
- check(deepEqual(mapGetDefault(data10, "1", null), 11));
57
- check(deepEqual(mapGetDefault(data10, "111", 0), 0));
58
- // check(deepEqual(mapGetDefault(data10, "11", {} as any), null)); // should throws error
59
-
60
- let emptyData: Map<string, number> = new Map();
61
- check(mapEqual(initEmptyTernaryTreeMap<string, number>(), initTernaryTreeMap(emptyData)));
62
- });
63
-
64
- test("assoc and contains", () => {
65
- var dict: Map<string, number> = new Map();
66
- for (let idx = 0; idx < 100; idx++) {
67
- dict.set(`${idx * 2}`, idx);
68
- }
69
- let data = initTernaryTreeMap(dict);
70
- for (let idx = 0; idx < 100; idx++) {
71
- dict.set(`${idx * 2 + 1}`, idx);
72
- let data2 = assocMap(data, `${idx * 2 + 1}`, idx);
73
- check(contains(data2, `${idx * 2 + 1}`));
74
- }
75
-
76
- var dict: Map<string, number> = new Map();
77
- data = initTernaryTreeMap(dict);
78
- for (let idx = 0; idx < 1000; idx++) {
79
- let p = 100 - idx / 10;
80
- data = assocMap(data, `${p}`, idx);
81
- check(contains(data, `${p}`));
82
- }
83
- });
84
-
85
- test("check structure", () => {
86
- var dict: Map<string, number> = new Map();
87
- for (let idx = 0; idx < 100; idx++) {
88
- dict.set(`${idx}`, idx + 10);
89
- }
90
-
91
- let data = initTernaryTreeMap(dict);
92
-
93
- check(checkMapStructure(data));
94
- });
95
-
96
- test("assoc map", () => {
97
- var dict: Map<string, number> = new Map();
98
- for (let idx = 0; idx < 10; idx++) {
99
- dict.set(`${idx}`, idx + 10);
100
- }
101
-
102
- let data = initTernaryTreeMap(dict);
103
-
104
- // echo data.formatInline
105
-
106
- check(contains(data, "1") == true);
107
- check(contains(data, "12") == false);
108
- checkMapStructure(data);
109
-
110
- justDisplay(formatMapInline(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))");
111
- justDisplay(formatMapInline(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 _)");
112
- });
113
-
114
- test("dissoc", () => {
115
- var dict: Map<string, number> = new Map();
116
- for (let idx = 0; idx < 10; idx++) {
117
- dict.set(`${idx}`, idx + 10);
118
- }
119
-
120
- let data = initTernaryTreeMap(dict);
121
- checkMapStructure(data);
122
-
123
- // echo data.formatInline
124
-
125
- for (let idx = 0; idx < 10; idx++) {
126
- let v = dissocMap(data, `${idx}`);
127
- check(contains(v, `${idx}`) == false);
128
- check(contains(data, `${idx}`) == true);
129
- check(mapLen(v) == mapLen(data) - 1);
130
- }
131
-
132
- for (let idx = 10; idx < 12; idx++) {
133
- let v = dissocMap(data, `${idx}`);
134
- check(contains(v, `${idx}`) == false);
135
- check(mapLen(v) == mapLen(data));
136
- }
137
- });
138
-
139
- test("to array", () => {
140
- var dict: Map<string, number> = new Map();
141
- for (let idx = 0; idx < 10; idx++) {
142
- dict.set(`${idx}`, idx + 10);
143
- }
144
-
145
- let data = initTernaryTreeMap(dict);
146
- checkMapStructure(data);
147
-
148
- // TODO
149
- // justDisplay((mapToString(toPairs(data))) , "@[2:12, 3:13, 7:17, 9:19, 6:16, 5:15, 1:11, 8:18, 0:10, 4:14]")
150
- justDisplay([...toKeys(data)], ["2", "3", "7", "9", "6", "5", "1", "8", "0", "4"]);
151
-
152
- check(deepEqual(toPairsArray(data), [...toPairs(data)]));
153
- });
154
-
155
- test("Equality", () => {
156
- var dict: Map<string, number> = new Map();
157
- for (let idx = 0; idx < 10; idx++) {
158
- dict.set(`${idx}`, idx + 10);
159
- }
160
-
161
- let data = initTernaryTreeMap(dict);
162
- let b = dissocMap(data, "3");
163
- checkMapStructure(data);
164
- checkMapStructure(b);
165
-
166
- check(mapEqual(data, data));
167
- check(!mapEqual(data, b));
168
-
169
- let c = assocMap(data, "3", 15);
170
- check(sameMapShape(data, data));
171
- check(sameMapShape(data, b) == false);
172
- check(sameMapShape(data, c) == false);
173
-
174
- let d = assocMap(c, "3", 13);
175
- check(mapEqual(data, d));
176
- check(data !== d); // not identical
177
- });
178
-
179
- test("Merge", () => {
180
- var dict: Map<string, number> = new Map();
181
- var dictBoth: Map<string, number> = new Map();
182
- for (let idx = 0; idx < 4; idx++) {
183
- dict.set(`${idx}`, idx + 10);
184
- dictBoth.set(`${idx}`, idx + 10);
185
- }
186
-
187
- let data = initTernaryTreeMap(dict);
188
- checkMapStructure(data);
189
-
190
- var dictB: Map<string, number> = new Map();
191
- for (let idx = 10; idx < 14; idx++) {
192
- dictB.set(`${idx}`, idx + 23);
193
- dictBoth.set(`${idx}`, idx + 23);
194
- }
195
- let b = initTernaryTreeMap(dictB);
196
-
197
- let merged = merge(data, b);
198
- let both = initTernaryTreeMap(dictBoth);
199
-
200
- check(mapEqual(merged, both));
201
- });
202
-
203
- test("Merge skip", () => {
204
- var dict: Map<string, number> = new Map();
205
- for (let idx = 0; idx < 4; idx++) {
206
- dict.set(`${idx}`, idx + 10);
207
- }
208
- let a = initTernaryTreeMap(dict);
209
- checkMapStructure(a);
210
-
211
- var dict2: Map<string, number> = new Map();
212
- for (let idx = 0; idx < 4; idx++) {
213
- dict2.set(`${idx}`, idx + 11);
214
- }
215
- let b = initTernaryTreeMap(dict2);
216
- checkMapStructure(b);
217
-
218
- let c = mergeSkip(a, b, 11);
219
- check(deepEqual(mapGetDefault(c, "0", null), 10));
220
- check(deepEqual(mapGetDefault(c, "1", null), 12));
221
- check(deepEqual(mapGetDefault(c, "2", null), 13));
222
- check(deepEqual(mapGetDefault(c, "3", null), 14));
223
- });
224
-
225
- test("iterator", () => {
226
- var dict: Map<string, number> = new Map();
227
- var dictBoth: Map<string, number> = new Map();
228
- for (let idx = 0; idx < 4; idx++) {
229
- dict.set(`${idx}`, idx + 10);
230
- dictBoth.set(`${idx}`, idx + 10);
231
- }
232
-
233
- let data = initTernaryTreeMap(dict);
234
- checkMapStructure(data);
235
-
236
- var i = 0;
237
- for (let [k, v] of toPairs(data)) {
238
- i = i + 1;
239
- }
240
-
241
- check(i == 4);
242
-
243
- i = 0;
244
- for (let key of toPairs(data)) {
245
- i = i + 1;
246
- }
247
- check(i == 4);
248
- });
249
-
250
- test("each map", () => {
251
- var dict: Map<string, number> = new Map();
252
- for (let idx = 0; idx < 100; idx++) {
253
- dict.set(`${idx}`, idx + 10);
254
- }
255
-
256
- let data = initTernaryTreeMap(dict);
257
- checkMapStructure(data);
258
-
259
- var i = 0;
260
- for (let [k, v] of toPairs(data)) {
261
- // echo "..{k}-{v}.."
262
- i = i + 1;
263
- }
264
- check(i == 100);
265
- });
266
-
267
- test("map values", () => {
268
- var dict: Map<string, number> = new Map();
269
- for (let idx = 0; idx < 4; idx++) {
270
- dict.set(`${idx}`, idx + 10);
271
- }
272
- let data = initTernaryTreeMap(dict);
273
-
274
- var dict2: Map<string, number> = new Map();
275
- for (let idx = 0; idx < 4; idx++) {
276
- dict2.set(`${idx}`, idx + 20);
277
- }
278
- let data2 = initTernaryTreeMap(dict2);
279
-
280
- let data3 = mapMapValues(data, (x) => x + 10);
281
- checkMapStructure(data3);
282
-
283
- checkMapStructure(data3);
284
- check(mapEqual(data2, data3));
285
- check(formatMapInline(data2) === formatMapInline(data3));
286
- });
287
- };
package/src/types.ts DELETED
@@ -1,87 +0,0 @@
1
- export enum TernaryTreeKind {
2
- ternaryTreeBranch,
3
- ternaryTreeLeaf,
4
- }
5
-
6
- export type TernaryTreeListTheBranch<T> = {
7
- size: number;
8
- kind: TernaryTreeKind.ternaryTreeBranch;
9
- depth: number;
10
- left: TernaryTreeList<T>;
11
- middle: TernaryTreeList<T>;
12
- right: TernaryTreeList<T>;
13
- };
14
-
15
- export type TernaryTreeListTheLeaf<T> = {
16
- size: number;
17
- kind: TernaryTreeKind.ternaryTreeLeaf;
18
- value: T;
19
- };
20
-
21
- export type TernaryTreeList<T> = TernaryTreeListTheBranch<T> | TernaryTreeListTheLeaf<T>;
22
-
23
- export type TernaryTreeMapHashEntry<K, V> = {
24
- hash: Hash;
25
- pairs: Array<[K, V]>;
26
- };
27
-
28
- export type TernaryTreeMapTheBranch<K, T> = {
29
- kind: TernaryTreeKind.ternaryTreeBranch;
30
- depth: number;
31
- maxHash: number;
32
- minHash: number;
33
- left: TernaryTreeMap<K, T>;
34
- middle: TernaryTreeMap<K, T>;
35
- right: TernaryTreeMap<K, T>;
36
- };
37
- export type TernaryTreeMapTheLeaf<K, T> = {
38
- kind: TernaryTreeKind.ternaryTreeLeaf;
39
- hash: number;
40
- elements: Array<[K, T]>; // handle hash collapsing
41
- };
42
-
43
- export type TernaryTreeMap<K, T> = TernaryTreeMapTheBranch<K, T> | TernaryTreeMapTheLeaf<K, T>;
44
-
45
- export type RefInt = {
46
- value: number;
47
- };
48
-
49
- export type Hash = number; // TODO
50
-
51
- export let valueHash = (x: any): Hash => {
52
- if (typeof x === "number") {
53
- // console.log("hash for x:", x, "\t", result);
54
- return x;
55
- } else if (typeof x === "string") {
56
- let h = 0;
57
- // https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0#gistcomment-2775538
58
- for (var i = 0; i < x.length; i++) {
59
- h = Math.imul(31, h) + (x[i].charCodeAt(0) | 0);
60
- }
61
- // console.log("hash for x:", x, "\t", result);
62
- return h;
63
- }
64
- throw new Error("Hash solution not provided for this type(other than number and string)");
65
- };
66
-
67
- /** default hash function only handles number and string, need customization */
68
- export let hashGenerator: typeof valueHash = valueHash;
69
-
70
- /** allow customizing hash function from outside */
71
- export let overwriteHashGenerator = (f: typeof hashGenerator) => {
72
- hashGenerator = f;
73
- };
74
-
75
- export let mergeValueHash = (base: Hash, x: number | string): Hash => {
76
- if (typeof x === "number") {
77
- return Math.imul(31, base) + x;
78
- } else if (typeof x === "string") {
79
- let h = base;
80
- // https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0#gistcomment-2775538
81
- for (var i = 0; i < x.length; i++) {
82
- h = Math.imul(31, h) + (x[i].charCodeAt(0) | 0);
83
- }
84
- return h;
85
- }
86
- throw new Error("Hash solution not provided for this type(other than number and string)");
87
- };