@bablr/btree 0.2.0 → 0.3.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.
@@ -13,7 +13,7 @@ export const buildModule = (
13
13
  finalizeSums,
14
14
  ) => {
15
15
  const sumNodes = (nodes) => {
16
- return nodes.map(getSum).reduce((a, b) => a + b, 0);
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 + getSum(v), 0);
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 = getSum(values[i - 1]);
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(getSum(tree), tree, value);
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]) && getSum(returnValue) <= NODE_SIZE) {
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 getSum = (tree) => {
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 = getSum(tree);
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 - getSum(node) : 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 >= getSum(node)) {
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 = getSum(candidateNode);
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 (getSum(tree) < idx) {
381
+ if (getSize(tree) < idx) {
377
382
  throw new Error('Cannot add past the end of a list');
378
- } else if (getSum(tree) === idx) {
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
- getSum,
424
+ getSize,
419
425
  findPath,
420
426
  getAt,
421
427
  replaceAt,
package/lib/index.js CHANGED
@@ -14,10 +14,11 @@ export const {
14
14
  isValidNode,
15
15
  assertValidNode,
16
16
  getValues,
17
+ getSums,
17
18
  setValues,
18
19
  isLeafNode,
19
20
  traverse,
20
- getSum,
21
+ getSize,
21
22
  findPath,
22
23
  getAt,
23
24
  replaceAt,
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.2.0",
4
+ "version": "0.3.0",
5
5
  "author": "Conrad Buck<conartist6@gmail.com>",
6
6
  "type": "module",
7
7
  "files": [