Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add asyncForEach #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Or use the compiled version 'dist/avl.js'.
* `tree.at(index:Number):Node|Null` - Return node by its index in sorted order of keys
* `tree.contains(key):Boolean` - Whether a node with the given key is in the tree
* `tree.forEach(function(node) {...}):Tree` In-order traversal
* `tree.asyncForEach(async function(node) {...}):Tree` In-order traversal for async callbacks
* `tree.range(lo, high, function(node) {} [, context]):Tree` - Walks the range of keys in order. Stops, if the visitor function returns a non-zero value.
* `tree.keys():Array<key>` - Returns the array of keys in order
* `tree.values():Array<*>` - Returns the array of data fields in order
Expand Down
13 changes: 13 additions & 0 deletions bench/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ new Benchmark.Suite(`Bulk-load (x${M})`, options)
t.load(data, []);
})
.run();

new Benchmark.Suite(`Traverse (x${N})`, options)
.add('AVL (forEach)', () => {
let count = 0;
prefilledAVL.forEach(node => count += node.key);
})
.add('AVL (asyncForEach)', (deferred) => {
let count = 0;
const promise = prefilledAVL.asyncForEach(node => count += node.key)
promise.then(() => deferred.resolve());
}, {'defer': true})
.run();

47 changes: 47 additions & 0 deletions dist/avl.es6.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/avl.es6.js.map

Large diffs are not rendered by default.

47 changes: 47 additions & 0 deletions dist/avl.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/avl.js.map

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { print, isBalanced, loadRecursive, markBalance, sort } from './utils';
import { print, isBalanced, loadRecursive, markBalance, sort, whilst } from './utils';


// function createNode (parent, left, right, height, key, data) {
Expand Down Expand Up @@ -270,6 +270,17 @@ export default class AVLTree {
return this;
}

/**
* @param {forEachCallback} callback
* @return {Promise}
*/
asyncForEach (callback) {
const minNode = this.minNode();
let node;
const next = this.next.bind(this);
const iterate = () => { node = node ? next(node) : minNode; return node; };
return whilst(iterate, callback);
}

/**
* Walk key range from `low` to `high`. Stops if `fn` returns a value.
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,12 @@ export function sort(keys, values, left, right, compare) {
sort(keys, values, left, j, compare);
sort(keys, values, j + 1, right, compare);
}

export async function whilst (next, iteratee) {
let i = 0, n;
// eslint-disable-next-line no-cond-assign
while ((n = next()) !== undefined) {
// eslint-disable-next-line no-await-in-loop
await iteratee(n, i++);
}
}
16 changes: 16 additions & 0 deletions tests/async.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { describe, it } from 'mocha';
import { assert } from 'chai';

import Tree from '../src/index';

describe('async check', () => {
it ('should traverse the tree in order', async () => {
const tree = new Tree();
tree.insert(2);
tree.insert(0);
tree.insert(3);
tree.insert(1);

await tree.asyncForEach(async (n, i) => assert.equal(n.key, i));
});
});