@bablr/btree 0.3.0 → 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/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. These trees could also correctly be termed sum trees, and are represented as:
3
+ Functional utilities for working with btrees such as those used in agAST.
4
4
 
5
5
  ```js
6
- let leafNode = [...data];
6
+ expect(push(['a', 'b'], 'c')).toEqual([
7
+ 3,
8
+ [['a'], ['b', 'c']],
9
+ ]);
7
10
 
8
- let branchNode = [sum, [...nodes]];
9
-
10
- let tree = [3, [[node1, node2], [node3]]];
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.
@@ -94,7 +94,7 @@ export const buildModule = (
94
94
  const isLeaf = isLeafNode(node);
95
95
  const values = getValues(node);
96
96
 
97
- if (isArray(value)) Error('cannot set arrays as btree values');
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 = [...values];
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 = [...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 = [...getValues(targetSibling)];
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 = [...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 = [...getValues(node)];
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
- values.splice(index, 1, ...(node ? [node] : []));
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
- for (i of indexes(values.length, idx < 0)) {
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 (idx < 0 ? nextCount <= targetIdx : nextCount > targetIdx || nextCount >= treeSum) {
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
- treeFromValues,
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
@@ -1,8 +1,12 @@
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
- treeFromValues,
6
+ btreeFrom,
7
+ from,
8
+ btreeFromValues,
9
+ fromValues,
6
10
  findBalancePoint,
7
11
  splitValues,
8
12
  collapses,
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.3.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#49f5952efed27f94ee9b94340eb1563c440bf64e",
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",