@bablr/btree 0.3.1 → 0.4.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/lib/enhanceable.js +24 -22
- package/lib/index.js +4 -1
- package/package.json +2 -2
package/lib/enhanceable.js
CHANGED
|
@@ -94,7 +94,7 @@ export const buildModule = (
|
|
|
94
94
|
const isLeaf = isLeafNode(node);
|
|
95
95
|
const values = getValues(node);
|
|
96
96
|
|
|
97
|
-
if (
|
|
97
|
+
if (!Number.isFinite(idx)) throw new Error();
|
|
98
98
|
|
|
99
99
|
if (!isLeaf && !isArray(value)) {
|
|
100
100
|
throw new Error();
|
|
@@ -102,17 +102,14 @@ export const buildModule = (
|
|
|
102
102
|
|
|
103
103
|
if (isLeaf && isArray(value) && node.length) throw new Error();
|
|
104
104
|
|
|
105
|
-
const newValues =
|
|
105
|
+
const newValues = values.slice();
|
|
106
106
|
newValues[idx] = value;
|
|
107
107
|
return treeFromValues(newValues);
|
|
108
108
|
};
|
|
109
109
|
|
|
110
110
|
const addAt = (idx, tree, value) => {
|
|
111
|
-
if (idx < 0) throw new Error('invalid argument');
|
|
111
|
+
if (idx < 0 || !Number.isFinite(idx)) throw new Error('invalid argument');
|
|
112
112
|
if (!isArray(tree)) throw new Error();
|
|
113
|
-
|
|
114
|
-
let isLeaf = isLeafNode(tree);
|
|
115
|
-
|
|
116
113
|
if (isArray(value)) throw new Error('cannot add arrays to btrees');
|
|
117
114
|
|
|
118
115
|
let path = findPath(idx, tree);
|
|
@@ -126,7 +123,7 @@ export const buildModule = (
|
|
|
126
123
|
|
|
127
124
|
for (;;) {
|
|
128
125
|
if (pushout) {
|
|
129
|
-
values =
|
|
126
|
+
values = values.slice();
|
|
130
127
|
|
|
131
128
|
let finiteIndex = index === Infinity ? values.length : index;
|
|
132
129
|
|
|
@@ -204,7 +201,7 @@ export const buildModule = (
|
|
|
204
201
|
let targetSiblingIndex = targetSibling && (prevSibling ? parentIndex - 1 : parentIndex + 1);
|
|
205
202
|
|
|
206
203
|
if (targetSibling) {
|
|
207
|
-
let targetValues =
|
|
204
|
+
let targetValues = getValues(targetSibling).slice();
|
|
208
205
|
|
|
209
206
|
const donationIdx = targetSibling === prevSibling ? targetValues.length - 1 : 0;
|
|
210
207
|
const donated = targetValues[donationIdx];
|
|
@@ -215,7 +212,7 @@ export const buildModule = (
|
|
|
215
212
|
index: targetSiblingIndex,
|
|
216
213
|
};
|
|
217
214
|
|
|
218
|
-
values =
|
|
215
|
+
values = values.slice();
|
|
219
216
|
|
|
220
217
|
values.splice(targetSibling === prevSibling ? values.length : 0, 0, donated);
|
|
221
218
|
|
|
@@ -233,13 +230,17 @@ export const buildModule = (
|
|
|
233
230
|
path = path.pop();
|
|
234
231
|
({ node, index } = path.value);
|
|
235
232
|
|
|
236
|
-
values =
|
|
233
|
+
values = getValues(node).slice();
|
|
237
234
|
|
|
238
235
|
values.splice(index, 1, returnValue);
|
|
239
236
|
|
|
240
237
|
if (adjustSibling) {
|
|
241
238
|
const { index, node } = adjustSibling;
|
|
242
|
-
|
|
239
|
+
if (node) {
|
|
240
|
+
values.splice(index, 1, node);
|
|
241
|
+
} else {
|
|
242
|
+
values.splice(index, 1);
|
|
243
|
+
}
|
|
243
244
|
}
|
|
244
245
|
|
|
245
246
|
returnValue = node = setValues(node, values);
|
|
@@ -317,13 +318,6 @@ export const buildModule = (
|
|
|
317
318
|
}
|
|
318
319
|
};
|
|
319
320
|
|
|
320
|
-
function* indexes(count, backwards = false) {
|
|
321
|
-
const increment = backwards ? -1 : 1;
|
|
322
|
-
for (let i = backwards ? count - 1 : 0; backwards ? i >= 0 : i < count; i += increment) {
|
|
323
|
-
yield i;
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
|
|
327
321
|
const findPath = (idx, tree) => {
|
|
328
322
|
if (idx == null) throw new Error();
|
|
329
323
|
|
|
@@ -352,11 +346,17 @@ export const buildModule = (
|
|
|
352
346
|
let candidateNode;
|
|
353
347
|
let i;
|
|
354
348
|
|
|
355
|
-
|
|
349
|
+
let backwards = idx < 0;
|
|
350
|
+
const increment = backwards ? -1 : 1;
|
|
351
|
+
for (
|
|
352
|
+
let i = backwards ? values.length - 1 : 0;
|
|
353
|
+
backwards ? i >= 0 : i < values.length;
|
|
354
|
+
i += increment
|
|
355
|
+
) {
|
|
356
356
|
candidateNode = values[i];
|
|
357
357
|
const sum = getSize(candidateNode);
|
|
358
358
|
const nextCount = currentIdx + sum * direction;
|
|
359
|
-
if (
|
|
359
|
+
if (backwards ? nextCount <= targetIdx : nextCount > targetIdx || nextCount >= treeSum) {
|
|
360
360
|
path = path.push({ index: i, node });
|
|
361
361
|
node = candidateNode;
|
|
362
362
|
continue stack;
|
|
@@ -404,8 +404,10 @@ export const buildModule = (
|
|
|
404
404
|
|
|
405
405
|
return {
|
|
406
406
|
buildModule,
|
|
407
|
-
treeFrom,
|
|
408
|
-
|
|
407
|
+
btreeFrom: treeFrom,
|
|
408
|
+
from: treeFrom,
|
|
409
|
+
btreeFromValues: treeFromValues,
|
|
410
|
+
fromValues: treeFromValues,
|
|
409
411
|
findBalancePoint,
|
|
410
412
|
splitValues,
|
|
411
413
|
collapses,
|
package/lib/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bablr/btree",
|
|
3
3
|
"description": "Functional utilities for working with btrees such as those used in agAST",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"author": "Conrad Buck<conartist6@gmail.com>",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"files": [
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"@iter-tools/imm-stack": "1.1.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
|
-
"@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#
|
|
22
|
+
"@bablr/eslint-config-base": "github:bablr-lang/eslint-config-base#c97bfa4b3663f8378e9b3e42bb5a41e685406cf9",
|
|
23
23
|
"enhanced-resolve": "^5.12.0",
|
|
24
24
|
"eslint": "^8.32.0",
|
|
25
25
|
"eslint-import-resolver-enhanced-resolve": "^1.0.5",
|