Skip to content

Commit

Permalink
Refactor BTree range method to improve readability and performance
Browse files Browse the repository at this point in the history
  • Loading branch information
n8allan committed Mar 5, 2024
1 parent 7eba53b commit 11fb10f
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/b-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,23 @@ export class BTree<TKey, TEntry> {
/** Iterates based on the given range
* WARNING: mutation during iteration will result in an exception
*/
*range(range: KeyRange<TKey>): Generator<Path<TKey, TEntry>, void, void> {
*range(range: KeyRange<TKey>): IterableIterator<Path<TKey, TEntry>> {
const startPath = range.first
? this.findFirst(range)
: (range.isAscending ? this.first() : this.last());
const endPath = range.last
? this.findLast(range)
: (range.isAscending ? this.last() : this.first());
const endKey = this.keyFromEntry(endPath.leafNode.entries[endPath.leafIndex]);
const iterable = range.isAscending
? this.internalAscending(startPath)
: this.internalDescending(startPath);
const iter = iterable[Symbol.iterator]();
for (let path of iter) {
const ascendingFactor = range.isAscending ? 1 : -1;
for (let path of iterable) {
if (!path.on || !endPath.on || this.compareKeys(
this.keyFromEntry(path.leafNode.entries[path.leafIndex]),
this.keyFromEntry(endPath.leafNode.entries[endPath.leafIndex])
) * (range.isAscending ? 1 : -1) > 0) {
this.keyFromEntry(path.leafNode.entries[path.leafIndex]),
endKey
) * ascendingFactor > 0) {
break;
}
yield path;
Expand Down

0 comments on commit 11fb10f

Please sign in to comment.