| 630 | // Leaf Node: scanning & deletions ////////////////////////////////////////// |
| 631 | |
| 632 | forRange<R>( |
| 633 | low: K, |
| 634 | high: K, |
| 635 | includeHigh: boolean | undefined, |
| 636 | editMode: boolean, |
| 637 | tree: BTree<K, V>, |
| 638 | count: number, |
| 639 | onFound?: (k: K, v: V, counter: number) => EditRangeResult<V, R> | void, |
| 640 | ): EditRangeResult<V, R> | number { |
| 641 | const cmp = tree._compare |
| 642 | let iLow, iHigh |
| 643 | if (high === low) { |
| 644 | if (!includeHigh) return count |
| 645 | iHigh = (iLow = this.indexOf(low, -1, cmp)) + 1 |
| 646 | if (iLow < 0) return count |
| 647 | } else { |
| 648 | iLow = this.indexOf(low, 0, cmp) |
| 649 | iHigh = this.indexOf(high, -1, cmp) |
| 650 | if (iHigh < 0) iHigh = ~iHigh |
| 651 | else if (includeHigh === true) iHigh++ |
| 652 | } |
| 653 | const keys = this.keys, |
| 654 | values = this.values |
| 655 | if (onFound !== undefined) { |
| 656 | for (let i = iLow; i < iHigh; i++) { |
| 657 | const key = keys[i]! |
| 658 | const result = onFound(key, values[i]!, count++) |
| 659 | if (result !== undefined) { |
| 660 | if (editMode === true) { |
| 661 | if (key !== keys[i] || this.isShared === true) |
| 662 | throw new Error(`BTree illegally changed or cloned in editRange`) |
| 663 | if (result.delete) { |
| 664 | this.keys.splice(i, 1) |
| 665 | if (this.values !== undefVals) this.values.splice(i, 1) |
| 666 | tree._size-- |
| 667 | i-- |
| 668 | iHigh-- |
| 669 | } else if (result.hasOwnProperty(`value`)) { |
| 670 | values[i] = result.value! |
| 671 | } |
| 672 | } |
| 673 | if (result.break !== undefined) return result |
| 674 | } |
| 675 | } |
| 676 | } else count += iHigh - iLow |
| 677 | return count |
| 678 | } |
| 679 | |
| 680 | /** Adds entire contents of right-hand sibling (rhs is left unchanged) */ |
| 681 | mergeSibling(rhs: BNode<K, V>, _: number) { |