Update test framework: fix run_tests.py to support all test files, add auto-import-check for test files

This commit is contained in:
qiaoxinjiu
2026-05-09 15:11:30 +08:00
parent eb053a347f
commit eaba8328da
21739 changed files with 2236758 additions and 719 deletions

1
node_modules/bintrees/.npmignore generated vendored Normal file
View File

@@ -0,0 +1 @@
node_modules

4
node_modules/bintrees/.travis.yml generated vendored Normal file
View File

@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.10"
- "0.12"

19
node_modules/bintrees/LICENSE generated vendored Normal file
View File

@@ -0,0 +1,19 @@
Copyright (C) 2011 by Vadim Graboys
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

22
node_modules/bintrees/Makefile generated vendored Normal file
View File

@@ -0,0 +1,22 @@
all: dist/rbtree.min.js dist/bintree.min.js
dist/rbtree.js: lib/rbtree.js lib/treebase.js
./node_modules/.bin/reunion --ns RBTree $< > $@
dist/bintree.js: lib/bintree.js lib/treebase.js
./node_modules/.bin/reunion --ns BinTree $< > $@
dist/bintree.min.js: dist/bintree.js
curl --data-urlencode "js_code@$<" \
-d "output_info=compiled_code&compilation_level=SIMPLE_OPTIMIZATIONS" \
http://closure-compiler.appspot.com/compile \
> $@
dist/rbtree.min.js: dist/rbtree.js
curl --data-urlencode "js_code@$<" \
-d "output_info=compiled_code&compilation_level=SIMPLE_OPTIMIZATIONS" \
http://closure-compiler.appspot.com/compile \
> $@

130
node_modules/bintrees/README.md generated vendored Normal file
View File

@@ -0,0 +1,130 @@
Binary Trees [![Build Status](https://secure.travis-ci.org/vadimg/js_bintrees.png?branch=master)](http://travis-ci.org/vadimg/js_bintrees)
============
This package provides Binary and Red-Black Search Trees written in Javascript. It is released under the MIT License.
Binary Search Trees are a good way to store data in sorted order. A Red-Black tree is a variation of a Binary Tree that balances itself.
Algorithms were taken from Julienne Walker: http://eternallyconfuzzled.com/jsw_home.aspx
Trees
------------
* BinTree - Binary Search Tree
* RBTree - Red-Black Tree
Quickstart
------------
node.js:
```
npm install bintrees
```
```javascript
var RBTree = require('bintrees').RBTree;
var tree = new RBTree(function(a, b) { return a - b; });
tree.insert(2);
tree.insert(-3);
```
see examples/node.js for more info
In the browser:
```html
<script src="/path/to/rbtree.js"></script>
<script>
var tree = new RBTree(function(a, b) { return a - b; });
tree.insert(0);
tree.insert(1);
</script>
```
see examples/client.html for more info
Constructor
------------
Requires 1 argument: a comparator function f(a,b) which returns:
* 0 if a == b
* >0 if a > b
* <0 if a < b
Methods
------------
### insert(item)
> Inserts the item into the tree. Returns true if inserted, false if duplicate.
### remove(item)
> Removes the item from the tree. Returns true if removed, false if not found.
### size
> Number of nodes in the tree.
### clear()
> Removes all nodes from the tree.
### find(item)
> Returns node data if found, null otherwise.
### findIter(item)
> Returns an iterator to the node if found, null otherwise.
### lowerBound(item)
> Returns an iterator to the tree node at or immediately after the item. Returns null-iterator if tree is empty.
>> __NOTE: Changed in version 1.0.0 to match C++ lower_bound__
### upperBound(item)
> Returns an iterator to the tree node immediately after the item. Returns null-iterator if tree is empty.
>> __NOTE: Changed in version 1.0.0 to match C++ upper_bound__
### min()
> Returns the min node data in the tree, or null if the tree is empty.
### max()
> Returns the max node data in the tree, or null if the tree is empty.
### each(f)
> Calls f on each node's data, in order.
### reach(f)
> Calls f on each node's data, in reverse order.
### iterator()
> Returns a null-iterator. See __Iterators__ section below.
Iterators
------------
tree.iterator() will return a null-iterator. On a null iterator,
* next() will return the first element in the tree
* prev() will return the last element in the tree
Otherwise,
* next() will return the next element
* prev() will return the previous element
* data() will return the node the iterator is pointing to
When iteration reaches the end, the iterator becomes a null-iterator again.
Forward iteration example:
```javascript
var it=tree.iterator(), item;
while((item = it.next()) !== null) {
// do stuff with item
}
```
If you are iterating forward through the tree, you can always call prev() to go back, and vice versa.
__NOTE:__ iterators become invalid when you add or remove elements from the tree.
## Production Usage
* [Coinbase Exchange](https://exchange.coinbase.com/), since Jan 26, 2015.
* If you are using this in production, please let me know! (add your company to this README in a pull request)

359
node_modules/bintrees/dist/bintree.js generated vendored Normal file
View File

@@ -0,0 +1,359 @@
BinTree = (function(window) {
var global = window;
var require = function(name) {
var fn = require.m[name];
if (fn.mod) {
return fn.mod.exports;
}
var mod = fn.mod = { exports: {} };
fn(mod, mod.exports);
return mod.exports;
};
require.m = {};
require.m['./treebase'] = function(module, exports) {
function TreeBase() {}
// removes all nodes from the tree
TreeBase.prototype.clear = function() {
this._root = null;
this.size = 0;
};
// returns node data if found, null otherwise
TreeBase.prototype.find = function(data) {
var res = this._root;
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
return res.data;
}
else {
res = res.get_child(c > 0);
}
}
return null;
};
// returns iterator to node if found, null otherwise
TreeBase.prototype.findIter = function(data) {
var res = this._root;
var iter = this.iterator();
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
iter._cursor = res;
return iter;
}
else {
iter._ancestors.push(res);
res = res.get_child(c > 0);
}
}
return null;
};
// Returns an iterator to the tree node at or immediately after the item
TreeBase.prototype.lowerBound = function(item) {
var cur = this._root;
var iter = this.iterator();
var cmp = this._comparator;
while(cur !== null) {
var c = cmp(item, cur.data);
if(c === 0) {
iter._cursor = cur;
return iter;
}
iter._ancestors.push(cur);
cur = cur.get_child(c > 0);
}
for(var i=iter._ancestors.length - 1; i >= 0; --i) {
cur = iter._ancestors[i];
if(cmp(item, cur.data) < 0) {
iter._cursor = cur;
iter._ancestors.length = i;
return iter;
}
}
iter._ancestors.length = 0;
return iter;
};
// Returns an iterator to the tree node immediately after the item
TreeBase.prototype.upperBound = function(item) {
var iter = this.lowerBound(item);
var cmp = this._comparator;
while(iter.data() !== null && cmp(iter.data(), item) === 0) {
iter.next();
}
return iter;
};
// returns null if tree is empty
TreeBase.prototype.min = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.left !== null) {
res = res.left;
}
return res.data;
};
// returns null if tree is empty
TreeBase.prototype.max = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.right !== null) {
res = res.right;
}
return res.data;
};
// returns a null iterator
// call next() or prev() to point to an element
TreeBase.prototype.iterator = function() {
return new Iterator(this);
};
// calls cb on each node's data, in order
TreeBase.prototype.each = function(cb) {
var it=this.iterator(), data;
while((data = it.next()) !== null) {
cb(data);
}
};
// calls cb on each node's data, in reverse order
TreeBase.prototype.reach = function(cb) {
var it=this.iterator(), data;
while((data = it.prev()) !== null) {
cb(data);
}
};
function Iterator(tree) {
this._tree = tree;
this._ancestors = [];
this._cursor = null;
}
Iterator.prototype.data = function() {
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns first node
// otherwise, returns next node
Iterator.prototype.next = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._minNode(root);
}
}
else {
if(this._cursor.right === null) {
// no greater node in subtree, go up to parent
// if coming from a right child, continue up the stack
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.right === save);
}
else {
// get the next node from the subtree
this._ancestors.push(this._cursor);
this._minNode(this._cursor.right);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns last node
// otherwise, returns previous node
Iterator.prototype.prev = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._maxNode(root);
}
}
else {
if(this._cursor.left === null) {
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.left === save);
}
else {
this._ancestors.push(this._cursor);
this._maxNode(this._cursor.left);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
Iterator.prototype._minNode = function(start) {
while(start.left !== null) {
this._ancestors.push(start);
start = start.left;
}
this._cursor = start;
};
Iterator.prototype._maxNode = function(start) {
while(start.right !== null) {
this._ancestors.push(start);
start = start.right;
}
this._cursor = start;
};
module.exports = TreeBase;
};
require.m['__main__'] = function(module, exports) {
var TreeBase = require('./treebase');
function Node(data) {
this.data = data;
this.left = null;
this.right = null;
}
Node.prototype.get_child = function(dir) {
return dir ? this.right : this.left;
};
Node.prototype.set_child = function(dir, val) {
if(dir) {
this.right = val;
}
else {
this.left = val;
}
};
function BinTree(comparator) {
this._root = null;
this._comparator = comparator;
this.size = 0;
}
BinTree.prototype = new TreeBase();
// returns true if inserted, false if duplicate
BinTree.prototype.insert = function(data) {
if(this._root === null) {
// empty tree
this._root = new Node(data);
this.size++;
return true;
}
var dir = 0;
// setup
var p = null; // parent
var node = this._root;
// search down
while(true) {
if(node === null) {
// insert new node at the bottom
node = new Node(data);
p.set_child(dir, node);
ret = true;
this.size++;
return true;
}
// stop if found
if(this._comparator(node.data, data) === 0) {
return false;
}
dir = this._comparator(node.data, data) < 0;
// update helpers
p = node;
node = node.get_child(dir);
}
};
// returns true if removed, false if not found
BinTree.prototype.remove = function(data) {
if(this._root === null) {
return false;
}
var head = new Node(undefined); // fake tree root
var node = head;
node.right = this._root;
var p = null; // parent
var found = null; // found item
var dir = 1;
while(node.get_child(dir) !== null) {
p = node;
node = node.get_child(dir);
var cmp = this._comparator(data, node.data);
dir = cmp > 0;
if(cmp === 0) {
found = node;
}
}
if(found !== null) {
found.data = node.data;
p.set_child(p.right === node, node.get_child(node.left === null));
this._root = head.right;
this.size--;
return true;
}
else {
return false;
}
};
module.exports = BinTree;
};
return require('__main__');
})(window);

8
node_modules/bintrees/dist/bintree.min.js generated vendored Normal file
View File

@@ -0,0 +1,8 @@
BinTree=function(m){var k=function(h){h=k.m[h];if(h.mod)return h.mod.exports;var l=h.mod={exports:{}};h(l,l.exports);return l.exports};k.m={};k.m["./treebase"]=function(h,k){function f(){}function g(a){this._tree=a;this._ancestors=[];this._cursor=null}f.prototype.clear=function(){this._root=null;this.size=0};f.prototype.find=function(a){for(var c=this._root;null!==c;){var b=this._comparator(a,c.data);if(0===b)return c.data;c=c.get_child(0<b)}return null};f.prototype.findIter=function(a){for(var c=
this._root,b=this.iterator();null!==c;){var d=this._comparator(a,c.data);if(0===d)return b._cursor=c,b;b._ancestors.push(c);c=c.get_child(0<d)}return null};f.prototype.lowerBound=function(a){for(var c=this._root,b=this.iterator(),d=this._comparator;null!==c;){var e=d(a,c.data);if(0===e)return b._cursor=c,b;b._ancestors.push(c);c=c.get_child(0<e)}for(e=b._ancestors.length-1;0<=e;--e)if(c=b._ancestors[e],0>d(a,c.data))return b._cursor=c,b._ancestors.length=e,b;b._ancestors.length=0;return b};f.prototype.upperBound=
function(a){for(var c=this.lowerBound(a),b=this._comparator;null!==c.data()&&0===b(c.data(),a);)c.next();return c};f.prototype.min=function(){var a=this._root;if(null===a)return null;for(;null!==a.left;)a=a.left;return a.data};f.prototype.max=function(){var a=this._root;if(null===a)return null;for(;null!==a.right;)a=a.right;return a.data};f.prototype.iterator=function(){return new g(this)};f.prototype.each=function(a){for(var c=this.iterator(),b;null!==(b=c.next());)a(b)};f.prototype.reach=function(a){for(var c=
this.iterator(),b;null!==(b=c.prev());)a(b)};g.prototype.data=function(){return null!==this._cursor?this._cursor.data:null};g.prototype.next=function(){if(null===this._cursor){var a=this._tree._root;null!==a&&this._minNode(a)}else if(null===this._cursor.right){do if(a=this._cursor,this._ancestors.length)this._cursor=this._ancestors.pop();else{this._cursor=null;break}while(this._cursor.right===a)}else this._ancestors.push(this._cursor),this._minNode(this._cursor.right);return null!==this._cursor?this._cursor.data:
null};g.prototype.prev=function(){if(null===this._cursor){var a=this._tree._root;null!==a&&this._maxNode(a)}else if(null===this._cursor.left){do if(a=this._cursor,this._ancestors.length)this._cursor=this._ancestors.pop();else{this._cursor=null;break}while(this._cursor.left===a)}else this._ancestors.push(this._cursor),this._maxNode(this._cursor.left);return null!==this._cursor?this._cursor.data:null};g.prototype._minNode=function(a){for(;null!==a.left;)this._ancestors.push(a),a=a.left;this._cursor=
a};g.prototype._maxNode=function(a){for(;null!==a.right;)this._ancestors.push(a),a=a.right;this._cursor=a};h.exports=f};k.m.__main__=function(h,l){function f(a){this.data=a;this.right=this.left=null}function g(a){this._root=null;this._comparator=a;this.size=0}var a=k("./treebase");f.prototype.get_child=function(a){return a?this.right:this.left};f.prototype.set_child=function(a,b){a?this.right=b:this.left=b};g.prototype=new a;g.prototype.insert=function(a){if(null===this._root)return this._root=new f(a),
this.size++,!0;for(var b=0,d=null,e=this._root;;){if(null===e)return e=new f(a),d.set_child(b,e),ret=!0,this.size++,!0;if(0===this._comparator(e.data,a))return!1;b=0>this._comparator(e.data,a);d=e;e=e.get_child(b)}};g.prototype.remove=function(a){if(null===this._root)return!1;var b=new f(void 0),d=b;d.right=this._root;for(var e=null,g=null,h=1;null!==d.get_child(h);){var e=d,d=d.get_child(h),k=this._comparator(a,d.data),h=0<k;0===k&&(g=d)}return null!==g?(g.data=d.data,e.set_child(e.right===d,d.get_child(null===
d.left)),this._root=b.right,this.size--,!0):!1};h.exports=g};return k("__main__")}(window);

469
node_modules/bintrees/dist/rbtree.js generated vendored Normal file
View File

@@ -0,0 +1,469 @@
RBTree = (function(window) {
var global = window;
var require = function(name) {
var fn = require.m[name];
if (fn.mod) {
return fn.mod.exports;
}
var mod = fn.mod = { exports: {} };
fn(mod, mod.exports);
return mod.exports;
};
require.m = {};
require.m['./treebase'] = function(module, exports) {
function TreeBase() {}
// removes all nodes from the tree
TreeBase.prototype.clear = function() {
this._root = null;
this.size = 0;
};
// returns node data if found, null otherwise
TreeBase.prototype.find = function(data) {
var res = this._root;
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
return res.data;
}
else {
res = res.get_child(c > 0);
}
}
return null;
};
// returns iterator to node if found, null otherwise
TreeBase.prototype.findIter = function(data) {
var res = this._root;
var iter = this.iterator();
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
iter._cursor = res;
return iter;
}
else {
iter._ancestors.push(res);
res = res.get_child(c > 0);
}
}
return null;
};
// Returns an iterator to the tree node at or immediately after the item
TreeBase.prototype.lowerBound = function(item) {
var cur = this._root;
var iter = this.iterator();
var cmp = this._comparator;
while(cur !== null) {
var c = cmp(item, cur.data);
if(c === 0) {
iter._cursor = cur;
return iter;
}
iter._ancestors.push(cur);
cur = cur.get_child(c > 0);
}
for(var i=iter._ancestors.length - 1; i >= 0; --i) {
cur = iter._ancestors[i];
if(cmp(item, cur.data) < 0) {
iter._cursor = cur;
iter._ancestors.length = i;
return iter;
}
}
iter._ancestors.length = 0;
return iter;
};
// Returns an iterator to the tree node immediately after the item
TreeBase.prototype.upperBound = function(item) {
var iter = this.lowerBound(item);
var cmp = this._comparator;
while(iter.data() !== null && cmp(iter.data(), item) === 0) {
iter.next();
}
return iter;
};
// returns null if tree is empty
TreeBase.prototype.min = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.left !== null) {
res = res.left;
}
return res.data;
};
// returns null if tree is empty
TreeBase.prototype.max = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.right !== null) {
res = res.right;
}
return res.data;
};
// returns a null iterator
// call next() or prev() to point to an element
TreeBase.prototype.iterator = function() {
return new Iterator(this);
};
// calls cb on each node's data, in order
TreeBase.prototype.each = function(cb) {
var it=this.iterator(), data;
while((data = it.next()) !== null) {
cb(data);
}
};
// calls cb on each node's data, in reverse order
TreeBase.prototype.reach = function(cb) {
var it=this.iterator(), data;
while((data = it.prev()) !== null) {
cb(data);
}
};
function Iterator(tree) {
this._tree = tree;
this._ancestors = [];
this._cursor = null;
}
Iterator.prototype.data = function() {
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns first node
// otherwise, returns next node
Iterator.prototype.next = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._minNode(root);
}
}
else {
if(this._cursor.right === null) {
// no greater node in subtree, go up to parent
// if coming from a right child, continue up the stack
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.right === save);
}
else {
// get the next node from the subtree
this._ancestors.push(this._cursor);
this._minNode(this._cursor.right);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns last node
// otherwise, returns previous node
Iterator.prototype.prev = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._maxNode(root);
}
}
else {
if(this._cursor.left === null) {
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.left === save);
}
else {
this._ancestors.push(this._cursor);
this._maxNode(this._cursor.left);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
Iterator.prototype._minNode = function(start) {
while(start.left !== null) {
this._ancestors.push(start);
start = start.left;
}
this._cursor = start;
};
Iterator.prototype._maxNode = function(start) {
while(start.right !== null) {
this._ancestors.push(start);
start = start.right;
}
this._cursor = start;
};
module.exports = TreeBase;
};
require.m['__main__'] = function(module, exports) {
var TreeBase = require('./treebase');
function Node(data) {
this.data = data;
this.left = null;
this.right = null;
this.red = true;
}
Node.prototype.get_child = function(dir) {
return dir ? this.right : this.left;
};
Node.prototype.set_child = function(dir, val) {
if(dir) {
this.right = val;
}
else {
this.left = val;
}
};
function RBTree(comparator) {
this._root = null;
this._comparator = comparator;
this.size = 0;
}
RBTree.prototype = new TreeBase();
// returns true if inserted, false if duplicate
RBTree.prototype.insert = function(data) {
var ret = false;
if(this._root === null) {
// empty tree
this._root = new Node(data);
ret = true;
this.size++;
}
else {
var head = new Node(undefined); // fake tree root
var dir = 0;
var last = 0;
// setup
var gp = null; // grandparent
var ggp = head; // grand-grand-parent
var p = null; // parent
var node = this._root;
ggp.right = this._root;
// search down
while(true) {
if(node === null) {
// insert new node at the bottom
node = new Node(data);
p.set_child(dir, node);
ret = true;
this.size++;
}
else if(is_red(node.left) && is_red(node.right)) {
// color flip
node.red = true;
node.left.red = false;
node.right.red = false;
}
// fix red violation
if(is_red(node) && is_red(p)) {
var dir2 = ggp.right === gp;
if(node === p.get_child(last)) {
ggp.set_child(dir2, single_rotate(gp, !last));
}
else {
ggp.set_child(dir2, double_rotate(gp, !last));
}
}
var cmp = this._comparator(node.data, data);
// stop if found
if(cmp === 0) {
break;
}
last = dir;
dir = cmp < 0;
// update helpers
if(gp !== null) {
ggp = gp;
}
gp = p;
p = node;
node = node.get_child(dir);
}
// update root
this._root = head.right;
}
// make root black
this._root.red = false;
return ret;
};
// returns true if removed, false if not found
RBTree.prototype.remove = function(data) {
if(this._root === null) {
return false;
}
var head = new Node(undefined); // fake tree root
var node = head;
node.right = this._root;
var p = null; // parent
var gp = null; // grand parent
var found = null; // found item
var dir = 1;
while(node.get_child(dir) !== null) {
var last = dir;
// update helpers
gp = p;
p = node;
node = node.get_child(dir);
var cmp = this._comparator(data, node.data);
dir = cmp > 0;
// save found node
if(cmp === 0) {
found = node;
}
// push the red node down
if(!is_red(node) && !is_red(node.get_child(dir))) {
if(is_red(node.get_child(!dir))) {
var sr = single_rotate(node, dir);
p.set_child(last, sr);
p = sr;
}
else if(!is_red(node.get_child(!dir))) {
var sibling = p.get_child(!last);
if(sibling !== null) {
if(!is_red(sibling.get_child(!last)) && !is_red(sibling.get_child(last))) {
// color flip
p.red = false;
sibling.red = true;
node.red = true;
}
else {
var dir2 = gp.right === p;
if(is_red(sibling.get_child(last))) {
gp.set_child(dir2, double_rotate(p, last));
}
else if(is_red(sibling.get_child(!last))) {
gp.set_child(dir2, single_rotate(p, last));
}
// ensure correct coloring
var gpc = gp.get_child(dir2);
gpc.red = true;
node.red = true;
gpc.left.red = false;
gpc.right.red = false;
}
}
}
}
}
// replace and remove if found
if(found !== null) {
found.data = node.data;
p.set_child(p.right === node, node.get_child(node.left === null));
this.size--;
}
// update root and make it black
this._root = head.right;
if(this._root !== null) {
this._root.red = false;
}
return found !== null;
};
function is_red(node) {
return node !== null && node.red;
}
function single_rotate(root, dir) {
var save = root.get_child(!dir);
root.set_child(!dir, save.get_child(dir));
save.set_child(dir, root);
root.red = true;
save.red = false;
return save;
}
function double_rotate(root, dir) {
root.set_child(!dir, single_rotate(root.get_child(!dir), !dir));
return single_rotate(root, dir);
}
module.exports = RBTree;
};
return require('__main__');
})(window);

9
node_modules/bintrees/dist/rbtree.min.js generated vendored Normal file
View File

@@ -0,0 +1,9 @@
RBTree=function(v){var p=function(g){g=p.m[g];if(g.mod)return g.mod.exports;var t=g.mod={exports:{}};g(t,t.exports);return t.exports};p.m={};p.m["./treebase"]=function(g,p){function d(){}function h(a){this._tree=a;this._ancestors=[];this._cursor=null}d.prototype.clear=function(){this._root=null;this.size=0};d.prototype.find=function(a){for(var b=this._root;null!==b;){var c=this._comparator(a,b.data);if(0===c)return b.data;b=b.get_child(0<c)}return null};d.prototype.findIter=function(a){for(var b=
this._root,c=this.iterator();null!==b;){var d=this._comparator(a,b.data);if(0===d)return c._cursor=b,c;c._ancestors.push(b);b=b.get_child(0<d)}return null};d.prototype.lowerBound=function(a){for(var b=this._root,c=this.iterator(),d=this._comparator;null!==b;){var q=d(a,b.data);if(0===q)return c._cursor=b,c;c._ancestors.push(b);b=b.get_child(0<q)}for(q=c._ancestors.length-1;0<=q;--q)if(b=c._ancestors[q],0>d(a,b.data))return c._cursor=b,c._ancestors.length=q,c;c._ancestors.length=0;return c};d.prototype.upperBound=
function(a){for(var b=this.lowerBound(a),c=this._comparator;null!==b.data()&&0===c(b.data(),a);)b.next();return b};d.prototype.min=function(){var a=this._root;if(null===a)return null;for(;null!==a.left;)a=a.left;return a.data};d.prototype.max=function(){var a=this._root;if(null===a)return null;for(;null!==a.right;)a=a.right;return a.data};d.prototype.iterator=function(){return new h(this)};d.prototype.each=function(a){for(var b=this.iterator(),c;null!==(c=b.next());)a(c)};d.prototype.reach=function(a){for(var b=
this.iterator(),c;null!==(c=b.prev());)a(c)};h.prototype.data=function(){return null!==this._cursor?this._cursor.data:null};h.prototype.next=function(){if(null===this._cursor){var a=this._tree._root;null!==a&&this._minNode(a)}else if(null===this._cursor.right){do if(a=this._cursor,this._ancestors.length)this._cursor=this._ancestors.pop();else{this._cursor=null;break}while(this._cursor.right===a)}else this._ancestors.push(this._cursor),this._minNode(this._cursor.right);return null!==this._cursor?this._cursor.data:
null};h.prototype.prev=function(){if(null===this._cursor){var a=this._tree._root;null!==a&&this._maxNode(a)}else if(null===this._cursor.left){do if(a=this._cursor,this._ancestors.length)this._cursor=this._ancestors.pop();else{this._cursor=null;break}while(this._cursor.left===a)}else this._ancestors.push(this._cursor),this._maxNode(this._cursor.left);return null!==this._cursor?this._cursor.data:null};h.prototype._minNode=function(a){for(;null!==a.left;)this._ancestors.push(a),a=a.left;this._cursor=
a};h.prototype._maxNode=function(a){for(;null!==a.right;)this._ancestors.push(a),a=a.right;this._cursor=a};g.exports=d};p.m.__main__=function(g,t){function d(a){this.data=a;this.right=this.left=null;this.red=!0}function h(a){this._root=null;this._comparator=a;this.size=0}function a(a){return null!==a&&a.red}function b(a,b){var c=a.get_child(!b);a.set_child(!b,c.get_child(b));c.set_child(b,a);a.red=!0;c.red=!1;return c}function c(a,c){a.set_child(!c,b(a.get_child(!c),!c));return b(a,c)}var u=p("./treebase");
d.prototype.get_child=function(a){return a?this.right:this.left};d.prototype.set_child=function(a,b){a?this.right=b:this.left=b};h.prototype=new u;h.prototype.insert=function(q){var h=!1;if(null===this._root)this._root=new d(q),h=!0,this.size++;else{var f=new d(void 0),l=0,n=0,r=null,m=f,k=null,e=this._root;for(m.right=this._root;;){null===e?(e=new d(q),k.set_child(l,e),h=!0,this.size++):a(e.left)&&a(e.right)&&(e.red=!0,e.left.red=!1,e.right.red=!1);if(a(e)&&a(k)){var g=m.right===r;e===k.get_child(n)?
m.set_child(g,b(r,!n)):m.set_child(g,c(r,!n))}g=this._comparator(e.data,q);if(0===g)break;n=l;l=0>g;null!==r&&(m=r);r=k;k=e;e=e.get_child(l)}this._root=f.right}this._root.red=!1;return h};h.prototype.remove=function(g){if(null===this._root)return!1;var h=new d(void 0),f=h;f.right=this._root;for(var l=null,n=null,r=null,m=1;null!==f.get_child(m);){var k=m,n=l,l=f,f=f.get_child(m),e=this._comparator(g,f.data),m=0<e;0===e&&(r=f);if(!a(f)&&!a(f.get_child(m)))if(a(f.get_child(!m)))n=b(f,m),l.set_child(k,
n),l=n;else if(!a(f.get_child(!m))&&(e=l.get_child(!k),null!==e))if(a(e.get_child(!k))||a(e.get_child(k))){var p=n.right===l;a(e.get_child(k))?n.set_child(p,c(l,k)):a(e.get_child(!k))&&n.set_child(p,b(l,k));k=n.get_child(p);k.red=!0;f.red=!0;k.left.red=!1;k.right.red=!1}else l.red=!1,e.red=!0,f.red=!0}null!==r&&(r.data=f.data,l.set_child(l.right===f,f.get_child(null===f.left)),this.size--);this._root=h.right;null!==this._root&&(this._root.red=!1);return null!==r};g.exports=h};return p("__main__")}(window);

25
node_modules/bintrees/examples/client.html generated vendored Normal file
View File

@@ -0,0 +1,25 @@
<!doctype html>
<html>
<head>
<title>simple browser test</title>
<script src="../dist/rbtree.js"></script>
<script src="../dist/bintree.js"></script>
<script>
function test(tree) {
var tree = new tree(function(a,b) { return a - b; });
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.remove(2);
tree.each(function(d) {
console.log(d);
});
}
test(RBTree);
test(BinTree);
</script>
</head>
<body>
This test just makes sure the script loads and <em>something</em> works. More comprehensive tests are located in the /test directory.
</body>
</html>

13
node_modules/bintrees/examples/node.js generated vendored Normal file
View File

@@ -0,0 +1,13 @@
var Tree = require('..').RBTree;
// create a new tree, pass in the compare function
var tree = new Tree(function(a, b) { return a - b; });
// do some inserts
tree.insert(1);
tree.insert(2);
tree.insert(3);
tree.remove(2);
// get smallest item
tree.min();

4
node_modules/bintrees/index.js generated vendored Normal file
View File

@@ -0,0 +1,4 @@
module.exports = {
RBTree: require('./lib/rbtree'),
BinTree: require('./lib/bintree')
};

108
node_modules/bintrees/lib/bintree.js generated vendored Normal file
View File

@@ -0,0 +1,108 @@
var TreeBase = require('./treebase');
function Node(data) {
this.data = data;
this.left = null;
this.right = null;
}
Node.prototype.get_child = function(dir) {
return dir ? this.right : this.left;
};
Node.prototype.set_child = function(dir, val) {
if(dir) {
this.right = val;
}
else {
this.left = val;
}
};
function BinTree(comparator) {
this._root = null;
this._comparator = comparator;
this.size = 0;
}
BinTree.prototype = new TreeBase();
// returns true if inserted, false if duplicate
BinTree.prototype.insert = function(data) {
if(this._root === null) {
// empty tree
this._root = new Node(data);
this.size++;
return true;
}
var dir = 0;
// setup
var p = null; // parent
var node = this._root;
// search down
while(true) {
if(node === null) {
// insert new node at the bottom
node = new Node(data);
p.set_child(dir, node);
ret = true;
this.size++;
return true;
}
// stop if found
if(this._comparator(node.data, data) === 0) {
return false;
}
dir = this._comparator(node.data, data) < 0;
// update helpers
p = node;
node = node.get_child(dir);
}
};
// returns true if removed, false if not found
BinTree.prototype.remove = function(data) {
if(this._root === null) {
return false;
}
var head = new Node(undefined); // fake tree root
var node = head;
node.right = this._root;
var p = null; // parent
var found = null; // found item
var dir = 1;
while(node.get_child(dir) !== null) {
p = node;
node = node.get_child(dir);
var cmp = this._comparator(data, node.data);
dir = cmp > 0;
if(cmp === 0) {
found = node;
}
}
if(found !== null) {
found.data = node.data;
p.set_child(p.right === node, node.get_child(node.left === null));
this._root = head.right;
this.size--;
return true;
}
else {
return false;
}
};
module.exports = BinTree;

218
node_modules/bintrees/lib/rbtree.js generated vendored Normal file
View File

@@ -0,0 +1,218 @@
var TreeBase = require('./treebase');
function Node(data) {
this.data = data;
this.left = null;
this.right = null;
this.red = true;
}
Node.prototype.get_child = function(dir) {
return dir ? this.right : this.left;
};
Node.prototype.set_child = function(dir, val) {
if(dir) {
this.right = val;
}
else {
this.left = val;
}
};
function RBTree(comparator) {
this._root = null;
this._comparator = comparator;
this.size = 0;
}
RBTree.prototype = new TreeBase();
// returns true if inserted, false if duplicate
RBTree.prototype.insert = function(data) {
var ret = false;
if(this._root === null) {
// empty tree
this._root = new Node(data);
ret = true;
this.size++;
}
else {
var head = new Node(undefined); // fake tree root
var dir = 0;
var last = 0;
// setup
var gp = null; // grandparent
var ggp = head; // grand-grand-parent
var p = null; // parent
var node = this._root;
ggp.right = this._root;
// search down
while(true) {
if(node === null) {
// insert new node at the bottom
node = new Node(data);
p.set_child(dir, node);
ret = true;
this.size++;
}
else if(is_red(node.left) && is_red(node.right)) {
// color flip
node.red = true;
node.left.red = false;
node.right.red = false;
}
// fix red violation
if(is_red(node) && is_red(p)) {
var dir2 = ggp.right === gp;
if(node === p.get_child(last)) {
ggp.set_child(dir2, single_rotate(gp, !last));
}
else {
ggp.set_child(dir2, double_rotate(gp, !last));
}
}
var cmp = this._comparator(node.data, data);
// stop if found
if(cmp === 0) {
break;
}
last = dir;
dir = cmp < 0;
// update helpers
if(gp !== null) {
ggp = gp;
}
gp = p;
p = node;
node = node.get_child(dir);
}
// update root
this._root = head.right;
}
// make root black
this._root.red = false;
return ret;
};
// returns true if removed, false if not found
RBTree.prototype.remove = function(data) {
if(this._root === null) {
return false;
}
var head = new Node(undefined); // fake tree root
var node = head;
node.right = this._root;
var p = null; // parent
var gp = null; // grand parent
var found = null; // found item
var dir = 1;
while(node.get_child(dir) !== null) {
var last = dir;
// update helpers
gp = p;
p = node;
node = node.get_child(dir);
var cmp = this._comparator(data, node.data);
dir = cmp > 0;
// save found node
if(cmp === 0) {
found = node;
}
// push the red node down
if(!is_red(node) && !is_red(node.get_child(dir))) {
if(is_red(node.get_child(!dir))) {
var sr = single_rotate(node, dir);
p.set_child(last, sr);
p = sr;
}
else if(!is_red(node.get_child(!dir))) {
var sibling = p.get_child(!last);
if(sibling !== null) {
if(!is_red(sibling.get_child(!last)) && !is_red(sibling.get_child(last))) {
// color flip
p.red = false;
sibling.red = true;
node.red = true;
}
else {
var dir2 = gp.right === p;
if(is_red(sibling.get_child(last))) {
gp.set_child(dir2, double_rotate(p, last));
}
else if(is_red(sibling.get_child(!last))) {
gp.set_child(dir2, single_rotate(p, last));
}
// ensure correct coloring
var gpc = gp.get_child(dir2);
gpc.red = true;
node.red = true;
gpc.left.red = false;
gpc.right.red = false;
}
}
}
}
}
// replace and remove if found
if(found !== null) {
found.data = node.data;
p.set_child(p.right === node, node.get_child(node.left === null));
this.size--;
}
// update root and make it black
this._root = head.right;
if(this._root !== null) {
this._root.red = false;
}
return found !== null;
};
function is_red(node) {
return node !== null && node.red;
}
function single_rotate(root, dir) {
var save = root.get_child(!dir);
root.set_child(!dir, save.get_child(dir));
save.set_child(dir, root);
root.red = true;
save.red = false;
return save;
}
function double_rotate(root, dir) {
root.set_child(!dir, single_rotate(root.get_child(!dir), !dir));
return single_rotate(root, dir);
}
module.exports = RBTree;

235
node_modules/bintrees/lib/treebase.js generated vendored Normal file
View File

@@ -0,0 +1,235 @@
function TreeBase() {}
// removes all nodes from the tree
TreeBase.prototype.clear = function() {
this._root = null;
this.size = 0;
};
// returns node data if found, null otherwise
TreeBase.prototype.find = function(data) {
var res = this._root;
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
return res.data;
}
else {
res = res.get_child(c > 0);
}
}
return null;
};
// returns iterator to node if found, null otherwise
TreeBase.prototype.findIter = function(data) {
var res = this._root;
var iter = this.iterator();
while(res !== null) {
var c = this._comparator(data, res.data);
if(c === 0) {
iter._cursor = res;
return iter;
}
else {
iter._ancestors.push(res);
res = res.get_child(c > 0);
}
}
return null;
};
// Returns an iterator to the tree node at or immediately after the item
TreeBase.prototype.lowerBound = function(item) {
var cur = this._root;
var iter = this.iterator();
var cmp = this._comparator;
while(cur !== null) {
var c = cmp(item, cur.data);
if(c === 0) {
iter._cursor = cur;
return iter;
}
iter._ancestors.push(cur);
cur = cur.get_child(c > 0);
}
for(var i=iter._ancestors.length - 1; i >= 0; --i) {
cur = iter._ancestors[i];
if(cmp(item, cur.data) < 0) {
iter._cursor = cur;
iter._ancestors.length = i;
return iter;
}
}
iter._ancestors.length = 0;
return iter;
};
// Returns an iterator to the tree node immediately after the item
TreeBase.prototype.upperBound = function(item) {
var iter = this.lowerBound(item);
var cmp = this._comparator;
while(iter.data() !== null && cmp(iter.data(), item) === 0) {
iter.next();
}
return iter;
};
// returns null if tree is empty
TreeBase.prototype.min = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.left !== null) {
res = res.left;
}
return res.data;
};
// returns null if tree is empty
TreeBase.prototype.max = function() {
var res = this._root;
if(res === null) {
return null;
}
while(res.right !== null) {
res = res.right;
}
return res.data;
};
// returns a null iterator
// call next() or prev() to point to an element
TreeBase.prototype.iterator = function() {
return new Iterator(this);
};
// calls cb on each node's data, in order
TreeBase.prototype.each = function(cb) {
var it=this.iterator(), data;
while((data = it.next()) !== null) {
if(cb(data) === false) {
return;
}
}
};
// calls cb on each node's data, in reverse order
TreeBase.prototype.reach = function(cb) {
var it=this.iterator(), data;
while((data = it.prev()) !== null) {
if(cb(data) === false) {
return;
}
}
};
function Iterator(tree) {
this._tree = tree;
this._ancestors = [];
this._cursor = null;
}
Iterator.prototype.data = function() {
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns first node
// otherwise, returns next node
Iterator.prototype.next = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._minNode(root);
}
}
else {
if(this._cursor.right === null) {
// no greater node in subtree, go up to parent
// if coming from a right child, continue up the stack
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.right === save);
}
else {
// get the next node from the subtree
this._ancestors.push(this._cursor);
this._minNode(this._cursor.right);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
// if null-iterator, returns last node
// otherwise, returns previous node
Iterator.prototype.prev = function() {
if(this._cursor === null) {
var root = this._tree._root;
if(root !== null) {
this._maxNode(root);
}
}
else {
if(this._cursor.left === null) {
var save;
do {
save = this._cursor;
if(this._ancestors.length) {
this._cursor = this._ancestors.pop();
}
else {
this._cursor = null;
break;
}
} while(this._cursor.left === save);
}
else {
this._ancestors.push(this._cursor);
this._maxNode(this._cursor.left);
}
}
return this._cursor !== null ? this._cursor.data : null;
};
Iterator.prototype._minNode = function(start) {
while(start.left !== null) {
this._ancestors.push(start);
start = start.left;
}
this._cursor = start;
};
Iterator.prototype._maxNode = function(start) {
while(start.right !== null) {
this._ancestors.push(start);
start = start.right;
}
this._cursor = start;
};
module.exports = TreeBase;

32
node_modules/bintrees/package.json generated vendored Normal file
View File

@@ -0,0 +1,32 @@
{
"author": "Vadim Graboys <dimva13@gmail.com>",
"name": "bintrees",
"description": "Binary Search Trees",
"version": "1.0.2",
"keywords": [
"binary tree",
"red black tree",
"red-black tree",
"redblack tree"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "git://github.com/vadimg/js_bintrees.git"
},
"directories": {
"lib": "lib"
},
"main": "./index.js",
"scripts": {
"test": "nodeunit ./test/test_*.js && jshint lib/*.js index.js"
},
"dependencies": {
},
"devDependencies": {
"nodeunit": "0.9.1",
"jshint": "0.5.9",
"underscore": "1.3.1",
"reunion": "0.0.0"
}
}

68
node_modules/bintrees/test/arrtree.js generated vendored Normal file
View File

@@ -0,0 +1,68 @@
// Not to be used in production!
// Only here to show how much faster bintrees are in the perf benchmarks.
function ArrTree(comparator) {
this._arr = [];
this._comparator = comparator;
}
// returns true if inserted, false if duplicate
ArrTree.prototype.insert = function(data) {
var elem_index = this._find_index(data);
if(elem_index >= 0) {
return false;
}
// recover the index data should have been inserted at and splice data in
this._arr.splice(~elem_index, 0, data);
return true;
};
// returns true if removed, false if not found
ArrTree.prototype.remove = function(data) {
var elem_index = this._find_index(data);
if(elem_index < 0) {
return false;
}
// array remains sorted after element has been removed
this._arr.splice(elem_index, 1);
return true;
};
ArrTree.prototype.find = function(data) {
var elem_index = this._find_index(data);
if(elem_index < 0) {
return null;
}
return this._arr[elem_index];
};
// returns the index if found,
// and the ones-complement of the index it should be inserted at if not
// NOTE: the ones-complement will always be < 0
ArrTree.prototype._find_index = function(data) {
var min_index = 0;
var max_index = this._arr.length - 1;
var current_index;
var current_element;
while(min_index <= max_index) {
current_index = (min_index + max_index) / 2 | 0;
current_element = this._arr[current_index];
if (this._comparator(current_element, data) < 0) {
min_index = current_index + 1;
}
else if(this._comparator(current_element, data) > 0) {
max_index = current_index - 1;
}
else {
return current_index;
}
}
return ~min_index;
}
module.exports = ArrTree;

52
node_modules/bintrees/test/loader.js generated vendored Normal file
View File

@@ -0,0 +1,52 @@
var fs = require('fs');
var _ = require('underscore');
function load(filename) {
var ret = [];
var nums = fs.readFileSync(filename, 'ascii').split('\n');
nums.forEach(function(s) {
if(s.length) {
var n = s*1;
ret.push(n);
}
});
return ret;
}
function get_inserts(tests) {
return _.select(tests, function(n) { return n > 0; });
}
function get_removes(tests) {
return _.select(tests, function(n) { return n < 0; });
}
function new_tree(tree_type) {
return new tree_type(function(a,b) { return a - b });
}
function build_tree(tree_type, inserts) {
var tree = new_tree(tree_type);
inserts.forEach(function(n) {
tree.insert(n);
});
return tree;
}
function load_tree(tree_type, filename) {
var tests = load(filename);
var inserts = get_inserts(tests);
return build_tree(tree_type, inserts);
}
module.exports = {
load: load,
get_inserts: get_inserts,
get_removes: get_removes,
new_tree: new_tree,
build_tree: build_tree,
load_tree: load_tree
};

200000
node_modules/bintrees/test/perf/100k generated vendored Normal file

File diff suppressed because it is too large Load Diff

113
node_modules/bintrees/test/perf_test.js generated vendored Normal file
View File

@@ -0,0 +1,113 @@
var fs = require('fs');
var loader = require('./loader');
var NUM_TIMES = 10;
var BASE_DIR = __dirname + '/perf';
var TREES = ['../test/arrtree', 'rbtree', 'bintree'];
function mean(arr) {
var sum = 0;
arr.forEach(function(n) {
sum += n;
});
return sum/arr.length;
}
function timeit(f) {
var diffs = [];
for(var i=0; i < NUM_TIMES; i++) {
var start = Date.now();
f();
var end = Date.now();
var diff = (end - start)/1000;
diffs.push(diff);
}
return diffs;
}
function print_times(arr) {
console.log('Mean: ', mean(arr));
}
function build(tree_class, test_path) {
var tests = loader.load(test_path);
var inserts = loader.get_inserts(tests);
console.log('build tree...');
print_times(timeit(function(){
loader.build_tree(tree_class, inserts);
}));
}
function build_destroy(tree_class, test_path) {
var tests = loader.load(test_path);
var inserts = loader.get_inserts(tests);
var removes = loader.get_removes(tests);
console.log('build/destroy tree...');
print_times(timeit(function() {
var tree = loader.build_tree(tree_class, inserts);
removes.forEach(function(n) {
tree.remove(n);
});
}));
}
function find(tree_class, test_path) {
var tests = loader.load(test_path);
var inserts = loader.get_inserts(tests);
var tree = loader.build_tree(tree_class, inserts);
console.log('find all nodes...');
print_times(timeit(function() {
inserts.forEach(function(n) {
tree.find(n);
});
}));
}
function interleaved(tree_class, test_path) {
var tests = loader.load(test_path);
console.log('interleaved build/destroy...');
print_times(timeit(function() {
var tree = new tree_class(function(a,b) { return a - b });
tests.forEach(function(n) {
if(n > 0)
tree.insert(n);
else
tree.remove(n);
});
}));
}
var tests = fs.readdirSync(BASE_DIR);
var test_funcs = {};
TREES.forEach(function(tree) {
var tree_class = require('../lib/' + tree);
tests.forEach(function(test) {
var test_path = BASE_DIR + "/" + test;
test_funcs[tree + "_" + test + "_build"] = function(assert) {
build(tree_class, test_path);
assert.done();
};
test_funcs[tree + "_" + test + "_build_destroy"] = function(assert) {
build_destroy(tree_class, test_path);
assert.done();
};
test_funcs[tree + "_" + test + "_find"] = function(assert) {
find(tree_class, test_path);
assert.done();
};
test_funcs[tree + "_" + test + "_interleaved"] = function(assert) {
interleaved(tree_class, test_path);
assert.done();
};
});
});
exports.performance = test_funcs;

20000
node_modules/bintrees/test/samples/10k generated vendored Normal file

File diff suppressed because it is too large Load Diff

38
node_modules/bintrees/test/scripts/gen_test.js generated vendored Normal file
View File

@@ -0,0 +1,38 @@
// generates a test case to STDOUT
var no_dups = false; // set to true if you don't want duplicate inserts
var num_inserts = 100000;
function randInt(start, end) {
return Math.floor(Math.random()*(end-start + 1)) + start;
}
function get_node_to_remove() {
var idx = randInt(0, added.length - 1);
return added.splice(idx, 1)[0];
}
var nums = [];
var added = [];
var ahash = {};
for(var i=0; i < num_inserts; i++) {
do {
var n = randInt(1, 1000000000);
} while(no_dups && ahash[n]);
added.push(n);
nums.push(n);
if(no_dups)
ahash[n] = true;
if(Math.random() < .3) {
// remove a node
nums.push(-get_node_to_remove());
}
}
// remove the rest, randomly
while(added.length > 0)
nums.push(-get_node_to_remove());
console.log(nums.join('\n'));

322
node_modules/bintrees/test/test_api.js generated vendored Normal file
View File

@@ -0,0 +1,322 @@
var _ = require('underscore');
var loader = require('./loader');
var SAMPLE_FILE = __dirname + '/samples/10k';
var TREES = ['rbtree', 'bintree'];
function clear(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
tree.clear();
inserts.forEach(function(data) {
assert.equal(tree.find(data), null);
});
}
function dup(assert, tree_class) {
var tree = loader.new_tree(tree_class);
assert.ok(tree.insert(100));
assert.ok(tree.insert(101));
assert.ok(!tree.insert(101));
assert.ok(!tree.insert(100));
tree.remove(100);
assert.ok(!tree.insert(101));
assert.ok(tree.insert(100));
assert.ok(!tree.insert(100));
}
function nonexist(assert, tree_class) {
var tree = loader.new_tree(tree_class);
assert.ok(!tree.remove(100));
tree.insert(100);
assert.ok(!tree.remove(101));
assert.ok(tree.remove(100));
}
function minmax(assert, tree_class) {
var tree = loader.new_tree(tree_class);
assert.equal(tree.min(), null);
assert.equal(tree.max(), null);
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
tree = loader.build_tree(tree_class, inserts);
assert.equal(tree.min(), _.min(inserts));
assert.equal(tree.max(), _.max(inserts));
}
function forward_it(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var items = [];
var it=tree.iterator(), data;
while((data = it.next()) !== null) {
items.push(data);
}
inserts.sort(function(a,b) { return a - b; });
assert.deepEqual(items, inserts);
items = [];
tree.each(function(data) {
items.push(data);
});
assert.deepEqual(items, inserts);
}
function forward_it_break(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var items = [];
var it=tree.iterator(), data;
while((data = it.next()) !== null) {
items.push(data);
}
inserts.sort(function(a,b) { return a - b; });
assert.deepEqual(items, inserts);
items = [];
var i = 0;
tree.each(function(data) {
items.push(data);
if (i === 3) {
return false;
}
i++;
});
assert.equal(items.length, 4);
}
function reverse_it(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var items = [];
var it=tree.iterator(), data;
while((data = it.prev()) !== null) {
items.push(data);
}
inserts.sort(function(a,b) { return b - a; });
assert.deepEqual(items, inserts);
items = [];
tree.reach(function(data) {
items.push(data);
});
assert.deepEqual(items, inserts);
}
function reverse_it_break(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
var items = [];
var it=tree.iterator(), data;
while((data = it.prev()) !== null) {
items.push(data);
}
inserts.sort(function(a,b) { return b - a; });
assert.deepEqual(items, inserts);
items = [];
var i = 0;
tree.reach(function(data) {
items.push(data);
if (i === 3) {
return false;
}
i++;
});
assert.equal(items.length, 4);
}
function switch_it(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
inserts.sort(function(a,b) { return a - b; });
function do_switch(after) {
var items = [];
var it = tree.iterator();
for(var i = 0; i < after; i++) {
items.push(it.next());
}
while((data = it.prev()) !== null) {
items.push(data);
}
var forward = inserts.slice(0, after);
var reverse = inserts.slice(0, after - 1).reverse();
var all = forward.concat(reverse);
assert.deepEqual(items, all);
}
do_switch(1);
do_switch(10);
do_switch(1000);
do_switch(9000);
}
function empty_it(assert, tree_class) {
var tree = loader.new_tree(tree_class);
var it = tree.iterator();
assert.equal(it.next(), null);
it = tree.iterator();
assert.equal(it.prev(), null);
}
function lower_bound(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
inserts.sort(function(a,b) { return a - b; });
for(var i=1; i<inserts.length-1; ++i) {
var item = inserts[i];
var iter = tree.lowerBound(item);
assert.equal(iter.data(), item);
assert.equal(iter.prev(), inserts[i-1]);
iter.next();
assert.equal(iter.next(), inserts[i+1]);
var prev = tree.lowerBound(item - 0.1);
assert.equal(prev.data(), inserts[i]);
var next = tree.lowerBound(item + 0.1);
assert.equal(next.data(), inserts[i+1]);
}
// test edges
var iter = tree.lowerBound(-1);
assert.equal(iter.data(), inserts[0]);
var last = inserts[inserts.length - 1];
iter = tree.lowerBound(last);
assert.equal(iter.data(), last);
iter = tree.lowerBound(last + 1);
assert.equal(iter.data(), null);
}
function upper_bound(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
inserts.sort(function(a,b) { return a - b; });
for(var i=0; i<inserts.length-2; ++i) {
var item = inserts[i];
var iter = tree.upperBound(item);
assert.equal(iter.data(), inserts[i+1]);
assert.equal(iter.prev(), inserts[i]);
iter.next();
assert.equal(iter.next(), inserts[i+2]);
var prev = tree.upperBound(item - 0.1);
assert.equal(prev.data(), inserts[i]);
var next = tree.upperBound(item + 0.1);
assert.equal(next.data(), inserts[i+1]);
}
// test edges
var iter = tree.upperBound(-1);
assert.equal(iter.data(), inserts[0]);
var last = inserts[inserts.length - 1];
iter = tree.upperBound(last);
assert.equal(iter.data(), null);
iter = tree.upperBound(last + 1);
assert.equal(iter.data(), null);
// test empty
var empty = new tree_class(function(a,b) { return a.val - b.val });
var iter = empty.upperBound({val:0});
assert.equal(iter.data(), null);
}
function find(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
for(var i=1; i<inserts.length-1; ++i) {
var item = inserts[i];
assert.equal(tree.find(item), item);
assert.equal(tree.find(item + 0.1), null);
}
}
function find_iter(assert, tree_class) {
var inserts = loader.get_inserts(loader.load(SAMPLE_FILE));
var tree = loader.build_tree(tree_class, inserts);
inserts.sort(function(a,b) { return a - b; });
for(var i=1; i<inserts.length-1; ++i) {
var item = inserts[i];
var iter = tree.findIter(item);
assert.equal(iter.data(), item);
assert.equal(iter.prev(), inserts[i-1]);
iter.next();
assert.equal(iter.next(), inserts[i+1]);
assert.equal(tree.findIter(item + 0.1), null);
}
}
var TESTS = {
clear: clear,
dup: dup,
nonexist: nonexist,
minmax: minmax,
forward_it: forward_it,
forward_it_break: forward_it_break,
reverse_it: reverse_it,
reverse_it_break: reverse_it_break,
switch_it: switch_it,
empty_it: empty_it,
lower_bound: lower_bound,
upper_bound: upper_bound,
find: find,
find_iter: find_iter
};
var test_funcs = {};
TREES.forEach(function(tree) {
var tree_class = require('../lib/' + tree);
for(var test in TESTS) {
(function(test) {
test_funcs[tree + "_" + test] = function(assert) {
TESTS[test](assert, tree_class);
assert.done();
}
})(test);
}
});
exports.api = test_funcs;

117
node_modules/bintrees/test/test_correctness.js generated vendored Normal file
View File

@@ -0,0 +1,117 @@
var fs = require('fs');
var assert = require('assert');
var loader = require('./loader');
var BASE_DIR = __dirname + '/samples';
var TREES = ['rbtree', 'bintree'];
function bt_assert(root, comparator) {
if(root === null) {
return true;
}
else {
var ln = root.left;
var rn = root.right;
// invalid binary search tree
assert.equal((ln !== null && comparator(ln.data, root.data) >= 0) ||
(rn !== null && comparator(rn.data, root.data) <= 0),
false,
"binary tree violation");
return bt_assert(ln, comparator) && bt_assert(rn, comparator);
}
}
function is_red(node) {
return node !== null && node.red;
}
function rb_assert(root, comparator) {
if(root === null) {
return 1;
}
else {
var ln = root.left;
var rn = root.right;
// red violation
if(is_red(root)) {
assert.equal(is_red(ln) || is_red(rn), false, "red violation");
}
var lh = rb_assert(ln, comparator);
var rh = rb_assert(rn, comparator);
// invalid binary search tree
assert.equal((ln !== null && comparator(ln.data, root.data) >= 0) ||
(rn !== null && comparator(rn.data, root.data) <= 0),
false,
"binary tree violation");
// black height mismatch
assert.equal(lh !== 0 && rh !== 0 && lh !== rh, false, "black violation");
// count black links
if(lh !== 0 && rh !== 0) {
return is_red(root) ? lh : lh + 1;
}
else {
return 0;
}
}
}
var assert_func = {
rbtree: rb_assert,
bintree: bt_assert
};
function tree_assert(tree_name) {
return function(tree) {
return assert_func[tree_name](tree._root, tree._comparator) !== 0;
}
}
function run_test(assert, tree_assert, tree_class, test_path) {
var tree = loader.new_tree(tree_class);
var tests = loader.load(test_path);
var elems = 0;
tests.forEach(function(n) {
if(n > 0) {
// insert
assert.ok(tree.insert(n));
assert.equal(tree.find(n), n);
elems++;
}
else {
// remove
n = -n;
assert.ok(tree.remove(n));
assert.equal(tree.find(n), null);
elems--;
}
assert.equal(tree.size, elems);
assert.ok(tree_assert(tree));
});
}
var tests = fs.readdirSync(BASE_DIR);
var test_funcs = {};
TREES.forEach(function(tree) {
var tree_class = require('../lib/' + tree);
tests.forEach(function(test) {
var test_path = BASE_DIR + "/" + test;
test_funcs[tree + "_" + test] = function(assert) {
run_test(assert, tree_assert(tree), tree_class, test_path);
assert.done();
};
});
});
exports.correctness = test_funcs;