@bablr/btree 0.1.0
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/LICENSE +21 -0
- package/README.md +13 -0
- package/lib/enhanceable.js +399 -0
- package/lib/index.js +24 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Conrad Buck
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @bablr/btree
|
|
2
|
+
|
|
3
|
+
Functional utilities for working with btrees such as those used in agAST. These trees could also correctly be termed sum trees, and are represented as:
|
|
4
|
+
|
|
5
|
+
```js
|
|
6
|
+
let leafNode = [...data];
|
|
7
|
+
|
|
8
|
+
let branchNode = [sum, [...nodes]];
|
|
9
|
+
|
|
10
|
+
let tree = [3, [[node1, node2], [node3]]];
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You can differentiate non-leaf nodes because they have a number as their first element. This is possible the data stored in this tree will always be object-typed.
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import emptyStack from '@iter-tools/imm-stack';
|
|
2
|
+
|
|
3
|
+
const { isArray } = Array;
|
|
4
|
+
const { freeze: freezeObject, isFrozen } = Object;
|
|
5
|
+
const { isFinite } = Number;
|
|
6
|
+
|
|
7
|
+
export const buildModule = (NODE_SIZE = 8) => {
|
|
8
|
+
const sumNodes = (nodes) => {
|
|
9
|
+
return nodes.map(getSum).reduce((a, b) => a + b, 0);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const treeFrom = (...trees) => [sumNodes(trees), trees];
|
|
13
|
+
|
|
14
|
+
const findBalancePoint = (tree) => {
|
|
15
|
+
const values = isFinite(tree[0]) ? tree[1] : tree;
|
|
16
|
+
let leftSum = 0;
|
|
17
|
+
let rightSum = getSum(tree);
|
|
18
|
+
let balance = leftSum / rightSum;
|
|
19
|
+
|
|
20
|
+
if (!values.length) return null;
|
|
21
|
+
if (values.length === 1) return 1;
|
|
22
|
+
|
|
23
|
+
for (let i = 1; i < values.length; i++) {
|
|
24
|
+
const sum = getSum(values[i - 1]);
|
|
25
|
+
const lastBalance = balance;
|
|
26
|
+
leftSum += sum;
|
|
27
|
+
rightSum -= sum;
|
|
28
|
+
|
|
29
|
+
balance = leftSum / rightSum;
|
|
30
|
+
|
|
31
|
+
if (lastBalance < 1 && balance >= 1) {
|
|
32
|
+
return i;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return values.length - 1;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const split = (tree) => {
|
|
39
|
+
const values = isFinite(tree[0]) ? tree[1] : tree;
|
|
40
|
+
const isLeaf = isLeafNode(tree);
|
|
41
|
+
|
|
42
|
+
let midIndex;
|
|
43
|
+
|
|
44
|
+
if (isLeaf) {
|
|
45
|
+
midIndex = Math.floor(values.length / 2 + 0.01);
|
|
46
|
+
} else {
|
|
47
|
+
midIndex = findBalancePoint(tree);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
let leftValues = values.slice(0, midIndex);
|
|
51
|
+
let rightValues = values.slice(midIndex);
|
|
52
|
+
|
|
53
|
+
let left = isLeaf ? leftValues : [sumNodes(leftValues), leftValues];
|
|
54
|
+
let right = isLeaf ? rightValues : [sumNodes(rightValues), rightValues];
|
|
55
|
+
|
|
56
|
+
return { left, right };
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const setValuesAt = (idx, node, value) => {
|
|
60
|
+
const isLeaf = isLeafNode(node);
|
|
61
|
+
const values = isLeaf ? node : node[1];
|
|
62
|
+
|
|
63
|
+
if (!isLeaf && !isArray(value)) {
|
|
64
|
+
throw new Error();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const oldSize = isLeaf ? 1 : values[idx] ? getSum(values[idx]) : 0;
|
|
68
|
+
const newSize = isLeaf ? 1 : getSum(value);
|
|
69
|
+
|
|
70
|
+
// TODO mutable sets?
|
|
71
|
+
|
|
72
|
+
const newValues = [...values];
|
|
73
|
+
newValues[idx] = value;
|
|
74
|
+
freezeObject(newValues);
|
|
75
|
+
return isLeaf ? newValues : freezeObject([node[0] + newSize - oldSize, newValues]);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const addAt = (idx, tree, value) => {
|
|
79
|
+
if (idx < 0) throw new Error('invalid argument');
|
|
80
|
+
|
|
81
|
+
let path = findPath(idx, tree);
|
|
82
|
+
|
|
83
|
+
let { node, index } = path.value;
|
|
84
|
+
|
|
85
|
+
// left pushout vs right pushout?
|
|
86
|
+
let pushout = value;
|
|
87
|
+
|
|
88
|
+
let values = [...getValues(node)];
|
|
89
|
+
|
|
90
|
+
for (;;) {
|
|
91
|
+
if (pushout) {
|
|
92
|
+
values = [...values];
|
|
93
|
+
if (values.length + getValues(pushout).length > NODE_SIZE) {
|
|
94
|
+
values.splice(index, 0, pushout);
|
|
95
|
+
node = setValues(node, values);
|
|
96
|
+
const { left, right } = split(node);
|
|
97
|
+
|
|
98
|
+
pushout = left;
|
|
99
|
+
node = right;
|
|
100
|
+
} else {
|
|
101
|
+
values.splice(index, 0, pushout);
|
|
102
|
+
node = setValues(node, values);
|
|
103
|
+
pushout = null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (path.size === 1) {
|
|
108
|
+
if (pushout) {
|
|
109
|
+
return treeFrom(pushout, node);
|
|
110
|
+
} else {
|
|
111
|
+
return node;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const poppedNode = node;
|
|
116
|
+
path = path.pop();
|
|
117
|
+
({ node, index } = path.value);
|
|
118
|
+
|
|
119
|
+
node = setValuesAt(index, node, poppedNode);
|
|
120
|
+
values = getValues(node);
|
|
121
|
+
path = path.replace({ node, index });
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
const push = (tree, value) => {
|
|
126
|
+
return addAt(getSum(tree), tree, value);
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
const collapses = (size) => {
|
|
130
|
+
return size > NODE_SIZE / 2 + 0.01;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const nodeCollapses = (node) => {
|
|
134
|
+
return collapses(getValues(node).length);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const nodeCanDonate = (node) => {
|
|
138
|
+
return collapses(getValues(node).length - 1);
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const pop = (tree) => {
|
|
142
|
+
let path = findPath(-1, tree);
|
|
143
|
+
|
|
144
|
+
let { node, index } = path.value;
|
|
145
|
+
|
|
146
|
+
const initialValues = [...getValues(node)].slice(0, -1);
|
|
147
|
+
let returnValue = isLeafNode(node) ? initialValues : [sumNodes(initialValues), initialValues];
|
|
148
|
+
|
|
149
|
+
for (;;) {
|
|
150
|
+
let values = getValues(returnValue);
|
|
151
|
+
let adjustSibling = null;
|
|
152
|
+
|
|
153
|
+
if (path.size > 1 && nodeCollapses(returnValue)) {
|
|
154
|
+
let { node: parentNode, index: parentIndex } = path.prev.value;
|
|
155
|
+
const prevSibling = getValues(parentNode)[parentIndex - 1];
|
|
156
|
+
const nextSibling = getValues(parentNode)[parentIndex + 1];
|
|
157
|
+
let targetSibling = nodeCanDonate(prevSibling)
|
|
158
|
+
? prevSibling
|
|
159
|
+
: nodeCanDonate(nextSibling)
|
|
160
|
+
? nextSibling
|
|
161
|
+
: null;
|
|
162
|
+
let targetSiblingIndex = targetSibling && (prevSibling ? parentIndex - 1 : parentIndex + 1);
|
|
163
|
+
|
|
164
|
+
if (targetSibling) {
|
|
165
|
+
let targetValues = [...getValues(targetSibling)];
|
|
166
|
+
|
|
167
|
+
const donationIdx = targetSibling === prevSibling ? targetValues.length - 1 : 0;
|
|
168
|
+
const donated = targetValues[donationIdx];
|
|
169
|
+
targetValues.splice(donationIdx, 1);
|
|
170
|
+
|
|
171
|
+
adjustSibling = {
|
|
172
|
+
node: setValues(targetSibling, targetValues),
|
|
173
|
+
index: targetSiblingIndex,
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
values = [...values];
|
|
177
|
+
|
|
178
|
+
values.splice(targetSibling === prevSibling ? values.length : 0, 0, donated);
|
|
179
|
+
|
|
180
|
+
returnValue = setValues(returnValue, values);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (path.size === 1) {
|
|
185
|
+
if (isFinite(returnValue[0]) && getSum(returnValue) <= NODE_SIZE) {
|
|
186
|
+
returnValue = returnValue[1].flat();
|
|
187
|
+
}
|
|
188
|
+
return returnValue;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
path = path.pop();
|
|
192
|
+
({ node, index } = path.value);
|
|
193
|
+
|
|
194
|
+
values = [...getValues(node)];
|
|
195
|
+
|
|
196
|
+
values.splice(index, 1, returnValue);
|
|
197
|
+
|
|
198
|
+
if (adjustSibling) {
|
|
199
|
+
const { index, node } = adjustSibling;
|
|
200
|
+
values.splice(index, 1, ...(node ? [node] : []));
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
returnValue = node = setValues(node, values);
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const isValidNode = (node) => {
|
|
208
|
+
if (!isArray(node)) return false;
|
|
209
|
+
const values = isFinite(node[0]) ? node[1] : node;
|
|
210
|
+
return isArray(values); // && values.length <= NODE_SIZE;
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const assertValidNode = (node) => {
|
|
214
|
+
if (!isValidNode(node)) throw new Error();
|
|
215
|
+
return true;
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const getValues = (node) => {
|
|
219
|
+
return node ? (isArray(node) ? (isFinite(node[0]) ? node[1] : node) : [node]) : [];
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
const setValues = (node, values) => {
|
|
223
|
+
return isFinite(node[0]) ? treeFrom(...values) : values;
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const isLeafNode = (node) => {
|
|
227
|
+
return isArray(node) && !isFinite(node[0]);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
function* traverse(tree) {
|
|
231
|
+
let states = emptyStack.push({ node: tree, i: 0 });
|
|
232
|
+
|
|
233
|
+
assertValidNode(tree);
|
|
234
|
+
|
|
235
|
+
stack: while (states.size) {
|
|
236
|
+
const s = states.value;
|
|
237
|
+
const { node } = s;
|
|
238
|
+
const isLeaf = isLeafNode(node);
|
|
239
|
+
|
|
240
|
+
const values = isLeaf ? node : node[1];
|
|
241
|
+
|
|
242
|
+
if (isLeaf) {
|
|
243
|
+
for (let i = 0; i < values.length; i++) {
|
|
244
|
+
yield values[i];
|
|
245
|
+
}
|
|
246
|
+
} else {
|
|
247
|
+
for (let { i } = s; s.i < values.length; ) {
|
|
248
|
+
const node = values[i];
|
|
249
|
+
assertValidNode(node);
|
|
250
|
+
states = states.push({ node, i: 0 });
|
|
251
|
+
i = ++s.i;
|
|
252
|
+
continue stack;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
states = states.pop();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const getSum = (tree) => {
|
|
260
|
+
if (!isArray(tree)) {
|
|
261
|
+
return 1;
|
|
262
|
+
} else if (isFinite(tree[0])) {
|
|
263
|
+
return tree[0];
|
|
264
|
+
} else {
|
|
265
|
+
return tree.length;
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
function* indexes(count, backwards = false) {
|
|
270
|
+
const increment = backwards ? -1 : 1;
|
|
271
|
+
for (let i = backwards ? count - 1 : 0; backwards ? i >= 0 : i < count; i += increment) {
|
|
272
|
+
yield i;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const findPath = (idx, tree) => {
|
|
277
|
+
let path = emptyStack;
|
|
278
|
+
|
|
279
|
+
let treeSum = getSum(tree);
|
|
280
|
+
let currentIdx = idx < 0 ? treeSum : 0;
|
|
281
|
+
let direction = idx < 0 ? -1 : 1;
|
|
282
|
+
let targetIdx = idx < 0 ? treeSum + idx : idx;
|
|
283
|
+
|
|
284
|
+
let node = tree;
|
|
285
|
+
stack: while (node) {
|
|
286
|
+
assertValidNode(node);
|
|
287
|
+
|
|
288
|
+
if (isLeafNode(node)) {
|
|
289
|
+
const startIdx = idx < 0 ? currentIdx - getSum(node) : currentIdx;
|
|
290
|
+
let index = isFinite(currentIdx) ? targetIdx - startIdx : currentIdx;
|
|
291
|
+
if (index < 0) {
|
|
292
|
+
index = -Infinity;
|
|
293
|
+
} else if (index >= node.length) {
|
|
294
|
+
index = Infinity;
|
|
295
|
+
}
|
|
296
|
+
return path.push({ index, node });
|
|
297
|
+
} else {
|
|
298
|
+
const values = node[1];
|
|
299
|
+
let candidateNode;
|
|
300
|
+
let i;
|
|
301
|
+
|
|
302
|
+
for (i of indexes(values.length, idx < 0)) {
|
|
303
|
+
candidateNode = values[i];
|
|
304
|
+
const sum = getSum(candidateNode);
|
|
305
|
+
const nextCount = currentIdx + sum * direction;
|
|
306
|
+
if (idx < 0 ? nextCount <= targetIdx : nextCount > targetIdx || nextCount >= treeSum) {
|
|
307
|
+
path = path.push({ index: i, node });
|
|
308
|
+
node = candidateNode;
|
|
309
|
+
continue stack;
|
|
310
|
+
} else {
|
|
311
|
+
currentIdx += sum * direction;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
return null;
|
|
318
|
+
};
|
|
319
|
+
|
|
320
|
+
const getAt = (idx, tree) => {
|
|
321
|
+
const v = findPath(idx, tree)?.value;
|
|
322
|
+
return v && v.node[v.index];
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const replaceAt = (idx, tree, value) => {
|
|
326
|
+
let path = findPath(idx, tree);
|
|
327
|
+
|
|
328
|
+
let { node, index } = path.value;
|
|
329
|
+
|
|
330
|
+
let returnValue = setValuesAt(index, node, value);
|
|
331
|
+
|
|
332
|
+
for (;;) {
|
|
333
|
+
({ node, index } = path.value);
|
|
334
|
+
|
|
335
|
+
if (path.size > 1) {
|
|
336
|
+
path = path.pop();
|
|
337
|
+
({ node, index } = path.value);
|
|
338
|
+
|
|
339
|
+
returnValue = setValuesAt(index, node, returnValue);
|
|
340
|
+
} else {
|
|
341
|
+
return returnValue;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
};
|
|
345
|
+
|
|
346
|
+
const freeze = (tree) => {
|
|
347
|
+
let states = emptyStack.push({ node: tree, i: 0 });
|
|
348
|
+
|
|
349
|
+
stack: while (states.size) {
|
|
350
|
+
const s = states.value;
|
|
351
|
+
const { node } = s;
|
|
352
|
+
const isLeaf = isLeafNode(node);
|
|
353
|
+
|
|
354
|
+
assertValidNode(node);
|
|
355
|
+
|
|
356
|
+
freezeObject(node);
|
|
357
|
+
|
|
358
|
+
const values = isLeaf ? node : node[1];
|
|
359
|
+
|
|
360
|
+
if (!isLeaf) {
|
|
361
|
+
freezeObject(values);
|
|
362
|
+
|
|
363
|
+
for (let { i } = s; s.i < values.length; ) {
|
|
364
|
+
const node = values[i];
|
|
365
|
+
assertValidNode(node);
|
|
366
|
+
states = states.push({ node, i: 0 });
|
|
367
|
+
i = ++s.i;
|
|
368
|
+
continue stack;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
states = states.pop();
|
|
372
|
+
}
|
|
373
|
+
return tree;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
return {
|
|
377
|
+
buildModule,
|
|
378
|
+
treeFrom,
|
|
379
|
+
findBalancePoint,
|
|
380
|
+
split,
|
|
381
|
+
collapses,
|
|
382
|
+
nodeCollapses,
|
|
383
|
+
nodeCanDonate,
|
|
384
|
+
pop,
|
|
385
|
+
push,
|
|
386
|
+
addAt,
|
|
387
|
+
isValidNode,
|
|
388
|
+
assertValidNode,
|
|
389
|
+
getValues,
|
|
390
|
+
setValues,
|
|
391
|
+
isLeafNode,
|
|
392
|
+
traverse,
|
|
393
|
+
getSum,
|
|
394
|
+
findPath,
|
|
395
|
+
getAt,
|
|
396
|
+
replaceAt,
|
|
397
|
+
freeze,
|
|
398
|
+
};
|
|
399
|
+
};
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { buildModule } from './enhanceable.js';
|
|
2
|
+
|
|
3
|
+
export const {
|
|
4
|
+
treeFrom,
|
|
5
|
+
findBalancePoint,
|
|
6
|
+
split,
|
|
7
|
+
collapses,
|
|
8
|
+
nodeCollapses,
|
|
9
|
+
nodeCanDonate,
|
|
10
|
+
pop,
|
|
11
|
+
push,
|
|
12
|
+
addAt,
|
|
13
|
+
isValidNode,
|
|
14
|
+
assertValidNode,
|
|
15
|
+
getValues,
|
|
16
|
+
setValues,
|
|
17
|
+
isLeafNode,
|
|
18
|
+
traverse,
|
|
19
|
+
getSum,
|
|
20
|
+
findPath,
|
|
21
|
+
getAt,
|
|
22
|
+
replaceAt,
|
|
23
|
+
freeze,
|
|
24
|
+
} = buildModule();
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bablr/btree",
|
|
3
|
+
"description": "Functional utilities for working with btrees such as those used in agAST",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"author": "Conrad Buck<conartist6@gmail.com>",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": "./lib/index.js",
|
|
12
|
+
"./enhanceable": "./lib/enhanceable.js"
|
|
13
|
+
},
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@iter-tools/imm-stack": "1.1.0"
|
|
17
|
+
},
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#49f5952efed27f94ee9b94340eb1563c440bf64e",
|
|
20
|
+
"enhanced-resolve": "^5.12.0",
|
|
21
|
+
"eslint": "^8.32.0",
|
|
22
|
+
"eslint-import-resolver-enhanced-resolve": "^1.0.5",
|
|
23
|
+
"eslint-plugin-import": "^2.27.5",
|
|
24
|
+
"expect": "29.7.0",
|
|
25
|
+
"iter-tools-es": "^7.3.1",
|
|
26
|
+
"mocha": "10.7.3",
|
|
27
|
+
"prettier": "^2.6.2"
|
|
28
|
+
},
|
|
29
|
+
"repository": "github:bablr-lang/btree",
|
|
30
|
+
"homepage": "https://github.com/bablr-lang/btree",
|
|
31
|
+
"license": "MIT"
|
|
32
|
+
}
|