rimbu-org/rimbu

Bug: `getAtIndex` causes TypeError: Cannot read properties of undefined (reading 'size')

Closed this issue · 2 comments

Environment

  • OS: mac
  • runtime: Node v18.18.0

Reproduction

https://codesandbox.io/p/sandbox/pensive-cohen-g36fkc?file=%2Fsrc%2Findex.ts%3A5%2C1-6%2C1

Steps to reproduce

Expected Behavior

The expected output is

9
19
29
39
49
59

Actual Behavior

The outputs til 49 is correct,

9
19
29
39
49

but at the end it emits error instead of printing 59

TypeError
Cannot read properties of undefined (reading 'size')
    at innerGetSubIndex (https://g36fkc.csb.app/node_modules/
rimbu/sorted/dist/module/common/base.js:232:21
    at innerGetAtIndex (https://g36fkc.csb.app/node_modules/
rimbu/sorted/dist/module/common/base.js:241:20
    at innerGetAtIndex (https://g36fkc.csb.app/node_modules/
rimbu/sorted/dist/module/common/base.js:240:25
    at SortedSetInner.getAtIndex (https://g36fkc.csb.app/node_modules/
rimbu/sorted/dist/module/set-custom/implementation/immutable.js:367:46
$csb$eval
/src/index.ts:7:19
  4 | for (const i of Stream.range({ start: 0, end: 5 })) {
  5 |   // add [0 .. 10], [10 .. 20], [20 .. 30] ...
  6 |   set = set.addAll(Stream.range({ start: i * 10, amount: 10 }));
> 7 |   console.info(set.getAtIndex(-1));
    |                   ^
  8 | }
  9 | 

I think the addAll has the problem. When I added values with addAll, some values were missing. The tree got somewhat broken by addAll.
Switching addAll to repeating add solved the problem.

vitoke commented

Ah this is not good indeed. Glad that you found a workaround for now. The addAll method converts the collection to a Builder and adds the elements to it, and then converts it back into an immutable instance. This has some performance benefits. So my guess is that the problem is in the Builder. Added to the list of things to fix.

Thanks by the way, really happy that people like yourself are finding these issues, since I did write a lot of tests, but only through extensive usage can the library really become rock solid.