@bablr/btree 0.2.0 → 0.3.1
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/README.md +12 -7
- package/lib/enhanceable.js +19 -13
- package/lib/index.js +5 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
# @bablr/btree
|
|
2
2
|
|
|
3
|
-
Functional utilities for working with btrees such as those used in agAST.
|
|
3
|
+
Functional utilities for working with btrees such as those used in agAST.
|
|
4
4
|
|
|
5
5
|
```js
|
|
6
|
-
|
|
6
|
+
expect(push(['a', 'b'], 'c')).toEqual([
|
|
7
|
+
3,
|
|
8
|
+
[['a'], ['b', 'c']],
|
|
9
|
+
]);
|
|
7
10
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
expect(addAt(0, [3, [['x'], ['y', 'z']]], 'w')).toEqual([
|
|
12
|
+
4,
|
|
13
|
+
[
|
|
14
|
+
['w', 'x'],
|
|
15
|
+
['y', 'z'],
|
|
16
|
+
],
|
|
17
|
+
]);
|
|
11
18
|
```
|
|
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.
|
package/lib/enhanceable.js
CHANGED
|
@@ -13,7 +13,7 @@ export const buildModule = (
|
|
|
13
13
|
finalizeSums,
|
|
14
14
|
) => {
|
|
15
15
|
const sumNodes = (nodes) => {
|
|
16
|
-
return nodes.map(
|
|
16
|
+
return nodes.map(getSize).reduce((a, b) => a + b, 0);
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
const treeFrom = (...values) => {
|
|
@@ -52,14 +52,14 @@ export const buildModule = (
|
|
|
52
52
|
|
|
53
53
|
const findBalancePoint = (values) => {
|
|
54
54
|
let leftSum = 0;
|
|
55
|
-
let rightSum = values.reduce((sum, v) => sum +
|
|
55
|
+
let rightSum = values.reduce((sum, v) => sum + getSize(v), 0);
|
|
56
56
|
let balance = leftSum / rightSum;
|
|
57
57
|
|
|
58
58
|
if (!values.length) return null;
|
|
59
59
|
if (values.length === 1) return 1;
|
|
60
60
|
|
|
61
61
|
for (let i = 1; i < values.length; i++) {
|
|
62
|
-
const sum =
|
|
62
|
+
const sum = getSize(values[i - 1]);
|
|
63
63
|
const lastBalance = balance;
|
|
64
64
|
leftSum += sum;
|
|
65
65
|
rightSum -= sum;
|
|
@@ -163,7 +163,7 @@ export const buildModule = (
|
|
|
163
163
|
};
|
|
164
164
|
|
|
165
165
|
const push = (tree, value) => {
|
|
166
|
-
return addAt(
|
|
166
|
+
return addAt(getSize(tree), tree, value);
|
|
167
167
|
};
|
|
168
168
|
|
|
169
169
|
const collapses = (size) => {
|
|
@@ -224,7 +224,7 @@ export const buildModule = (
|
|
|
224
224
|
}
|
|
225
225
|
|
|
226
226
|
if (path.size === 1) {
|
|
227
|
-
if (isFinite(returnValue[0]) &&
|
|
227
|
+
if (isFinite(returnValue[0]) && getSize(returnValue) <= NODE_SIZE) {
|
|
228
228
|
returnValue = returnValue[1].flat();
|
|
229
229
|
}
|
|
230
230
|
return returnValue;
|
|
@@ -261,6 +261,11 @@ export const buildModule = (
|
|
|
261
261
|
return node ? (isArray(node) ? (isFinite(node[0]) ? node[1] : node) : [node]) : [];
|
|
262
262
|
};
|
|
263
263
|
|
|
264
|
+
const getSums = (node) => {
|
|
265
|
+
if (!isValidNode(node)) throw new Error();
|
|
266
|
+
return node[2];
|
|
267
|
+
};
|
|
268
|
+
|
|
264
269
|
const setValues = (node, values) => {
|
|
265
270
|
if (values.length > NODE_SIZE) throw new Error();
|
|
266
271
|
|
|
@@ -300,7 +305,7 @@ export const buildModule = (
|
|
|
300
305
|
}
|
|
301
306
|
}
|
|
302
307
|
|
|
303
|
-
const
|
|
308
|
+
const getSize = (tree) => {
|
|
304
309
|
if (tree == null) {
|
|
305
310
|
return 0;
|
|
306
311
|
} else if (!isArray(tree)) {
|
|
@@ -324,7 +329,7 @@ export const buildModule = (
|
|
|
324
329
|
|
|
325
330
|
let path = emptyStack;
|
|
326
331
|
|
|
327
|
-
let treeSum =
|
|
332
|
+
let treeSum = getSize(tree);
|
|
328
333
|
let currentIdx = idx < 0 ? treeSum : 0;
|
|
329
334
|
let direction = idx < 0 ? -1 : 1;
|
|
330
335
|
let targetIdx = idx < 0 ? treeSum + idx : idx;
|
|
@@ -334,11 +339,11 @@ export const buildModule = (
|
|
|
334
339
|
assertValidNode(node);
|
|
335
340
|
|
|
336
341
|
if (isLeafNode(node)) {
|
|
337
|
-
const startIdx = idx < 0 ? currentIdx -
|
|
342
|
+
const startIdx = idx < 0 ? currentIdx - getSize(node) : currentIdx;
|
|
338
343
|
let index = isFinite(currentIdx) ? targetIdx - startIdx : currentIdx;
|
|
339
344
|
if (index < 0) {
|
|
340
345
|
index = -Infinity;
|
|
341
|
-
} else if (index >=
|
|
346
|
+
} else if (index >= getSize(node)) {
|
|
342
347
|
index = Infinity;
|
|
343
348
|
}
|
|
344
349
|
return path.push({ index, node });
|
|
@@ -349,7 +354,7 @@ export const buildModule = (
|
|
|
349
354
|
|
|
350
355
|
for (i of indexes(values.length, idx < 0)) {
|
|
351
356
|
candidateNode = values[i];
|
|
352
|
-
const sum =
|
|
357
|
+
const sum = getSize(candidateNode);
|
|
353
358
|
const nextCount = currentIdx + sum * direction;
|
|
354
359
|
if (idx < 0 ? nextCount <= targetIdx : nextCount > targetIdx || nextCount >= treeSum) {
|
|
355
360
|
path = path.push({ index: i, node });
|
|
@@ -373,9 +378,9 @@ export const buildModule = (
|
|
|
373
378
|
const replaceAt = (idx, tree, value) => {
|
|
374
379
|
let path = findPath(idx, tree);
|
|
375
380
|
|
|
376
|
-
if (
|
|
381
|
+
if (getSize(tree) < idx) {
|
|
377
382
|
throw new Error('Cannot add past the end of a list');
|
|
378
|
-
} else if (
|
|
383
|
+
} else if (getSize(tree) === idx) {
|
|
379
384
|
return addAt(idx, tree, value);
|
|
380
385
|
}
|
|
381
386
|
|
|
@@ -412,10 +417,11 @@ export const buildModule = (
|
|
|
412
417
|
isValidNode,
|
|
413
418
|
assertValidNode,
|
|
414
419
|
getValues,
|
|
420
|
+
getSums,
|
|
415
421
|
setValues,
|
|
416
422
|
isLeafNode,
|
|
417
423
|
traverse,
|
|
418
|
-
|
|
424
|
+
getSize,
|
|
419
425
|
findPath,
|
|
420
426
|
getAt,
|
|
421
427
|
replaceAt,
|
package/lib/index.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { buildModule } from './enhanceable.js';
|
|
1
|
+
import { defaultNodeSize, buildModule } from './enhanceable.js';
|
|
2
|
+
|
|
3
|
+
export { defaultNodeSize };
|
|
2
4
|
|
|
3
5
|
export const {
|
|
4
|
-
defaultNodeSize,
|
|
5
6
|
treeFromValues,
|
|
6
7
|
findBalancePoint,
|
|
7
8
|
splitValues,
|
|
@@ -14,10 +15,11 @@ export const {
|
|
|
14
15
|
isValidNode,
|
|
15
16
|
assertValidNode,
|
|
16
17
|
getValues,
|
|
18
|
+
getSums,
|
|
17
19
|
setValues,
|
|
18
20
|
isLeafNode,
|
|
19
21
|
traverse,
|
|
20
|
-
|
|
22
|
+
getSize,
|
|
21
23
|
findPath,
|
|
22
24
|
getAt,
|
|
23
25
|
replaceAt,
|
package/package.json
CHANGED