diff --git a/Makefile b/Makefile index ff5f95a..0cdb278 100644 --- a/Makefile +++ b/Makefile @@ -2,21 +2,15 @@ all: dist/rbtree.min.js dist/bintree.min.js dist/rbtree.js: lib/rbtree.js lib/treebase.js - ./node_modules/.bin/reunion --ns RBTree $< > $@ + ./node_modules/.bin/browserify -s RBTree $< > $@ dist/bintree.js: lib/bintree.js lib/treebase.js - ./node_modules/.bin/reunion --ns BinTree $< > $@ + ./node_modules/.bin/browserify -s 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 \ - > $@ + ./node_modules/.bin/uglifyjs $< -c -m -o $@ 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 \ - > $@ + ./node_modules/.bin/uglifyjs $< -c -m -o $@ diff --git a/dist/bintree.js b/dist/bintree.js index 7cbdf0b..e32fcbf 100644 --- a/dist/bintree.js +++ b/dist/bintree.js @@ -1,18 +1,113 @@ -BinTree = (function(window) { -var global = window; -var require = function(name) { - var fn = require.m[name]; - if (fn.mod) { - return fn.mod.exports; +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.BinTree = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 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; + + +},{"./treebase":2}],2:[function(require,module,exports){ function TreeBase() {} @@ -138,7 +233,9 @@ TreeBase.prototype.iterator = function() { TreeBase.prototype.each = function(cb) { var it=this.iterator(), data; while((data = it.next()) !== null) { - cb(data); + if(cb(data) === false) { + return; + } } }; @@ -146,7 +243,9 @@ TreeBase.prototype.each = function(cb) { TreeBase.prototype.reach = function(cb) { var it=this.iterator(), data; while((data = it.prev()) !== null) { - cb(data); + if(cb(data) === false) { + return; + } } }; @@ -244,116 +343,6 @@ Iterator.prototype._maxNode = function(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); +},{}]},{},[1])(1) +}); \ No newline at end of file diff --git a/dist/bintree.min.js b/dist/bintree.min.js index 6179405..7fda722 100644 --- a/dist/bintree.min.js +++ b/dist/bintree.min.js @@ -1,8 +1 @@ -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(0d(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=00,0===u&&(n=o)}return null!==n?(n.data=o.data,i.set_child(i.right===o,o.get_child(null===o.left)),this._root=r.right,this.size--,!0):!1},r.exports=i},{"./treebase":2}],2:[function(t,r,o){function e(){}function i(t){this._tree=t,this._ancestors=[],this._cursor=null}e.prototype.clear=function(){this._root=null,this.size=0},e.prototype.find=function(t){for(var r=this._root;null!==r;){var o=this._comparator(t,r.data);if(0===o)return r.data;r=r.get_child(o>0)}return null},e.prototype.findIter=function(t){for(var r=this._root,o=this.iterator();null!==r;){var e=this._comparator(t,r.data);if(0===e)return o._cursor=r,o;o._ancestors.push(r),r=r.get_child(e>0)}return null},e.prototype.lowerBound=function(t){for(var r=this._root,o=this.iterator(),e=this._comparator;null!==r;){var i=e(t,r.data);if(0===i)return o._cursor=r,o;o._ancestors.push(r),r=r.get_child(i>0)}for(var n=o._ancestors.length-1;n>=0;--n)if(r=o._ancestors[n],e(t,r.data)<0)return o._cursor=r,o._ancestors.length=n,o;return o._ancestors.length=0,o},e.prototype.upperBound=function(t){for(var r=this.lowerBound(t),o=this._comparator;null!==r.data()&&0===o(r.data(),t);)r.next();return r},e.prototype.min=function(){var t=this._root;if(null===t)return null;for(;null!==t.left;)t=t.left;return t.data},e.prototype.max=function(){var t=this._root;if(null===t)return null;for(;null!==t.right;)t=t.right;return t.data},e.prototype.iterator=function(){return new i(this)},e.prototype.each=function(t){for(var r,o=this.iterator();null!==(r=o.next());)if(t(r)===!1)return},e.prototype.reach=function(t){for(var r,o=this.iterator();null!==(r=o.prev());)if(t(r)===!1)return},i.prototype.data=function(){return null!==this._cursor?this._cursor.data:null},i.prototype.next=function(){if(null===this._cursor){var t=this._tree._root;null!==t&&this._minNode(t)}else if(null===this._cursor.right){var r;do{if(r=this._cursor,!this._ancestors.length){this._cursor=null;break}this._cursor=this._ancestors.pop()}while(this._cursor.right===r)}else this._ancestors.push(this._cursor),this._minNode(this._cursor.right);return null!==this._cursor?this._cursor.data:null},i.prototype.prev=function(){if(null===this._cursor){var t=this._tree._root;null!==t&&this._maxNode(t)}else if(null===this._cursor.left){var r;do{if(r=this._cursor,!this._ancestors.length){this._cursor=null;break}this._cursor=this._ancestors.pop()}while(this._cursor.left===r)}else this._ancestors.push(this._cursor),this._maxNode(this._cursor.left);return null!==this._cursor?this._cursor.data:null},i.prototype._minNode=function(t){for(;null!==t.left;)this._ancestors.push(t),t=t.left;this._cursor=t},i.prototype._maxNode=function(t){for(;null!==t.right;)this._ancestors.push(t),t=t.right;this._cursor=t},r.exports=e},{}]},{},[1])(1)}); \ No newline at end of file diff --git a/dist/rbtree.js b/dist/rbtree.js index 182dbe2..cedcbfa 100644 --- a/dist/rbtree.js +++ b/dist/rbtree.js @@ -1,18 +1,224 @@ -RBTree = (function(window) { -var global = window; -var require = function(name) { - var fn = require.m[name]; - if (fn.mod) { - return fn.mod.exports; +(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.RBTree = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o 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; }; -require.m = {}; -require.m['./treebase'] = function(module, exports) { +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; + +},{"./treebase":2}],2:[function(require,module,exports){ function TreeBase() {} @@ -138,7 +344,9 @@ TreeBase.prototype.iterator = function() { TreeBase.prototype.each = function(cb) { var it=this.iterator(), data; while((data = it.next()) !== null) { - cb(data); + if(cb(data) === false) { + return; + } } }; @@ -146,7 +354,9 @@ TreeBase.prototype.each = function(cb) { TreeBase.prototype.reach = function(cb) { var it=this.iterator(), data; while((data = it.prev()) !== null) { - cb(data); + if(cb(data) === false) { + return; + } } }; @@ -244,226 +454,6 @@ Iterator.prototype._maxNode = function(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); +},{}]},{},[1])(1) +}); \ No newline at end of file diff --git a/dist/rbtree.min.js b/dist/rbtree.min.js index 5084629..48ee639 100644 --- a/dist/rbtree.min.js +++ b/dist/rbtree.min.js @@ -1,9 +1 @@ -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(0d(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=0d,null!==h&&(c=h),h=a,a=f,f=f.get_child(o)}this._root=e.right}return this._root.red=!1,r},o.prototype.remove=function(t){if(null===this._root)return!1;var r=new i(void 0),e=r;e.right=this._root;for(var o=null,u=null,h=null,c=1;null!==e.get_child(c);){var a=c;u=o,o=e,e=e.get_child(c);var f=this._comparator(t,e.data);if(c=f>0,0===f&&(h=e),!n(e)&&!n(e.get_child(c)))if(n(e.get_child(!c))){var _=s(e,c);o.set_child(a,_),o=_}else if(!n(e.get_child(!c))){var d=o.get_child(!a);if(null!==d)if(n(d.get_child(!a))||n(d.get_child(a))){var p=u.right===o;n(d.get_child(a))?u.set_child(p,l(o,a)):n(d.get_child(!a))&&u.set_child(p,s(o,a));var g=u.get_child(p);g.red=!0,e.red=!0,g.left.red=!1,g.right.red=!1}else o.red=!1,d.red=!0,e.red=!0}}return null!==h&&(h.data=e.data,o.set_child(o.right===e,e.get_child(null===e.left)),this.size--),this._root=r.right,null!==this._root&&(this._root.red=!1),null!==h},r.exports=o},{"./treebase":2}],2:[function(t,r,e){function i(){}function o(t){this._tree=t,this._ancestors=[],this._cursor=null}i.prototype.clear=function(){this._root=null,this.size=0},i.prototype.find=function(t){for(var r=this._root;null!==r;){var e=this._comparator(t,r.data);if(0===e)return r.data;r=r.get_child(e>0)}return null},i.prototype.findIter=function(t){for(var r=this._root,e=this.iterator();null!==r;){var i=this._comparator(t,r.data);if(0===i)return e._cursor=r,e;e._ancestors.push(r),r=r.get_child(i>0)}return null},i.prototype.lowerBound=function(t){for(var r=this._root,e=this.iterator(),i=this._comparator;null!==r;){var o=i(t,r.data);if(0===o)return e._cursor=r,e;e._ancestors.push(r),r=r.get_child(o>0)}for(var n=e._ancestors.length-1;n>=0;--n)if(r=e._ancestors[n],i(t,r.data)<0)return e._cursor=r,e._ancestors.length=n,e;return e._ancestors.length=0,e},i.prototype.upperBound=function(t){for(var r=this.lowerBound(t),e=this._comparator;null!==r.data()&&0===e(r.data(),t);)r.next();return r},i.prototype.min=function(){var t=this._root;if(null===t)return null;for(;null!==t.left;)t=t.left;return t.data},i.prototype.max=function(){var t=this._root;if(null===t)return null;for(;null!==t.right;)t=t.right;return t.data},i.prototype.iterator=function(){return new o(this)},i.prototype.each=function(t){for(var r,e=this.iterator();null!==(r=e.next());)if(t(r)===!1)return},i.prototype.reach=function(t){for(var r,e=this.iterator();null!==(r=e.prev());)if(t(r)===!1)return},o.prototype.data=function(){return null!==this._cursor?this._cursor.data:null},o.prototype.next=function(){if(null===this._cursor){var t=this._tree._root;null!==t&&this._minNode(t)}else if(null===this._cursor.right){var r;do{if(r=this._cursor,!this._ancestors.length){this._cursor=null;break}this._cursor=this._ancestors.pop()}while(this._cursor.right===r)}else this._ancestors.push(this._cursor),this._minNode(this._cursor.right);return null!==this._cursor?this._cursor.data:null},o.prototype.prev=function(){if(null===this._cursor){var t=this._tree._root;null!==t&&this._maxNode(t)}else if(null===this._cursor.left){var r;do{if(r=this._cursor,!this._ancestors.length){this._cursor=null;break}this._cursor=this._ancestors.pop()}while(this._cursor.left===r)}else this._ancestors.push(this._cursor),this._maxNode(this._cursor.left);return null!==this._cursor?this._cursor.data:null},o.prototype._minNode=function(t){for(;null!==t.left;)this._ancestors.push(t),t=t.left;this._cursor=t},o.prototype._maxNode=function(t){for(;null!==t.right;)this._ancestors.push(t),t=t.right;this._cursor=t},r.exports=i},{}]},{},[1])(1)}); \ No newline at end of file diff --git a/lib/bintree.js b/lib/bintree.js index 0f043dd..5b08c84 100644 --- a/lib/bintree.js +++ b/lib/bintree.js @@ -49,7 +49,6 @@ BinTree.prototype.insert = function(data) { // insert new node at the bottom node = new Node(data); p.set_child(dir, node); - ret = true; this.size++; return true; } diff --git a/package.json b/package.json index d8f5c8a..f223fb6 100644 --- a/package.json +++ b/package.json @@ -18,14 +18,23 @@ }, "main": "./index.js", "scripts": { - "test": "nodeunit ./test/test_*.js && jshint lib/*.js index.js" - }, - "dependencies": { + "pretest": "eslint lib/*.js index.js test/*.js", + "test": "nodeunit ./test/test_*.js" }, + "dependencies": {}, "devDependencies": { + "browserify": "^13.0.1", + "eslint": "^2.13.0", "nodeunit": "0.9.1", - "jshint": "0.5.9", - "underscore": "1.3.1", - "reunion": "0.0.0" + "uglify-js": "^2.6.2" + }, + "eslintConfig": { + "extends": "eslint:recommended", + "env": { + "node": true + }, + "rules": { + "no-constant-condition": 0 + } } } diff --git a/test/loader.js b/test/loader.js index d366c53..0d8fed1 100644 --- a/test/loader.js +++ b/test/loader.js @@ -1,5 +1,4 @@ var fs = require('fs'); -var _ = require('underscore'); function load(filename) { var ret = []; @@ -15,11 +14,11 @@ function load(filename) { } function get_inserts(tests) { - return _.select(tests, function(n) { return n > 0; }); + return tests.filter(function(n) { return n > 0; }); } function get_removes(tests) { - return _.select(tests, function(n) { return n < 0; }); + return tests.filter(function(n) { return n < 0; }); } function new_tree(tree_type) { diff --git a/test/perf_test.js b/test/perf_test.js index 05819e9..62dbcad 100644 --- a/test/perf_test.js +++ b/test/perf_test.js @@ -6,6 +6,8 @@ var NUM_TIMES = 10; var BASE_DIR = __dirname + '/perf'; var TREES = ['../test/arrtree', 'rbtree', 'bintree']; +/* eslint no-console: 0 */ + function mean(arr) { var sum = 0; arr.forEach(function(n) { diff --git a/test/test_api.js b/test/test_api.js index 6ea5960..2a4c07b 100644 --- a/test/test_api.js +++ b/test/test_api.js @@ -1,5 +1,3 @@ -var _ = require('underscore'); - var loader = require('./loader'); var SAMPLE_FILE = __dirname + '/samples/10k'; @@ -44,8 +42,8 @@ function minmax(assert, tree_class) { 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)); + assert.equal(tree.min(), Math.min.apply(Math, inserts)); + assert.equal(tree.max(), Math.max.apply(Math, inserts)); } function forward_it(assert, tree_class) { @@ -161,6 +159,7 @@ function switch_it(assert, tree_class) { items.push(it.next()); } + var data; while((data = it.prev()) !== null) { items.push(data); } @@ -191,13 +190,14 @@ function empty_it(assert, tree_class) { function lower_bound(assert, tree_class) { var inserts = loader.get_inserts(loader.load(SAMPLE_FILE)); var tree = loader.build_tree(tree_class, inserts); + var iter; inserts.sort(function(a,b) { return a - b; }); for(var i=1; i