From 7a7278b5e688415132f968034e55dd9decf2140c Mon Sep 17 00:00:00 2001 From: Nicholas Zuber Date: Sat, 24 Sep 2016 00:43:49 -0400 Subject: [PATCH] xcode license --- bin/needle.min.js | 2 +- examples/scripts/needle.min.js | 2 +- examples/views/index.html | 22 ++++--- gulpfile.js | 6 +- package.json | 2 +- src/BinarySearchTree/binarySearchTree.js | 63 +++++++++++++++++-- src/DoublyLinkedList/doublyLinkedList.js | 2 +- src/KaryTree/karyTree.js | 2 +- .../bidirectional_node.js | 0 .../bidirectional_tree_node.js | 0 .../multidirectional_tree_node.js | 0 .../unidirectional_node.js | 0 src/Queue/queue.js | 2 +- src/SinglyLinkedList/singlyLinkedList.js | 2 +- src/Stack/stack.js | 2 +- 15 files changed, 86 insertions(+), 21 deletions(-) rename src/{__Nodes__ => Nodes}/bidirectional_node.js (100%) rename src/{__Nodes__ => Nodes}/bidirectional_tree_node.js (100%) rename src/{__Nodes__ => Nodes}/multidirectional_tree_node.js (100%) rename src/{__Nodes__ => Nodes}/unidirectional_node.js (100%) diff --git a/bin/needle.min.js b/bin/needle.min.js index b26e905..2072473 100644 --- a/bin/needle.min.js +++ b/bin/needle.min.js @@ -2,4 +2,4 @@ // node-needle v0.1.9 | MIT // Copyright (c) 2016 Nick Zuber */ -!function t(e,i,n){function r(s,h){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!h&&a)return a(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var u=i[s]={exports:{}};e[s][0].call(u.exports,function(t){var i=e[s][1][t];return r(i?i:t)},u,u.exports,t,e,i,n)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s1;)r(this.heap[e],this.heap[Math.floor(e/2)],this.compare)&&this.heap.swap(e,Math.floor(e/2)),e=Math.floor(e/2)},o.prototype["delete"]=function(t){if("number"==typeof t?++t:t=1,"number"!=typeof t)throw new TypeError("Expected a number as argument in BinaryHeap.delete");if(t<0||t>=this.heap.length)throw new Error("Index out of bounds in BinaryHeap.delete");if(this.heap.length<=1)throw new Error("Attempted to remove min element from an empty heap in BinaryHeap.delete");if(2===this.heap.length)return void this.heap.pop();if(this.heap[t]=this.heap.pop(),2===this.heap.length);else if(3===this.heap.length){var t=1;r(this.heap[2*t],this.heap[t],this.compare)&&this.heap.swap(t,2*t)}else for(var e=0,t=1;r(this.heap[2*t],this.heap[t],this.compare)||r(this.heap[2*t+1],this.heap[t],this.compare);){var i;if(i=r(this.heap[2*t],this.heap[2*t+1],this.compare)||"undefined"==typeof this.heap[2*t+1]?2*t:2*t+1,this.heap.swap(t,i),t=i,++e,e>1e3)throw new Error("Timeout error in BinaryHeap.delete. Please report this to https://github.com/nickzuber/needle/issues")}},o.prototype._heapify=function(t){var e=2*t,i=e+1,n=t;this.heap.length;e=1;--e)this._heapify(e);console.log(this.heap)},e.exports=o},{}],2:[function(t,e,i){"use strict";const n=t("./binaryHeap");e.exports=n},{"./binaryHeap":1}],3:[function(t,e,i){"use strict";function n(t,e){return t=this.data.length*r)throw new Error("Index out of bounds in BitArray.set");var i=Math.floor(t++/r);e?this.data[i]|=1<=this.data.length*r)throw new Error("Index out of bounds in BitArray.get");var e=Math.floor(t++/r),i=this.data[e];return i>>r-t&1},o.prototype.size=function(){return this.data.length*r},o.prototype.resize=function(t){if("number"!=typeof bitarray)throw new TypeError("");return this.data.length*r},o.prototype.complement=function(t){"undefined"==typeof t?t=this:t instanceof o||(t=new o(t));var e=new o;return t.data.map(function(t){e.data.push(~t)}),e},o.prototype.union=function(t){if("undefined"==typeof t)throw new Error("Undefined bitarray argument in BitArray.union");if(!(t instanceof o))throw new TypeError("Invalid argument: expected BitArray as an argument in BitArray.union");if(t.size()!==this.size())throw new Error("BitArrays must be of same size to resolve a union in BitArray.union");for(var e=new o,i=0;i>>31),e<<=1;t.split("").reverse().join("")}),t},e.exports=o},{}],6:[function(t,e,i){"use strict";const n=t("./bitArray");e.exports=n},{"./bitArray":5}],7:[function(t,e,i){"use strict";const n=t("../__Nodes__/bidirectional_node.js"),r=function(t){this.head,this.tail,this.size,"undefined"!=typeof t?(this.head=new n(t),this.tail=this.head,this.size=1):(this.head=null,this.tail=null,this.size=0)};r.prototype.insertFront=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertFront");var e=new n(t);if(null===this.head&&null===this.tail)this.head=e,this.tail=this.head;else{if(null===this.head||null===this.tail)throw new Error("Unsynced head and tail in an Doubly Linked List. Please report this to https://github.com/nickzuber/needle/issues");var i=this.head;i.prev=e,this.head=e,this.head.next=i,this.head.prev=null}++this.size},r.prototype.insertNth=function(t,e){if("undefined"==typeof e||"undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.insertNth");if(t<0||t>=this.size&&0!==t)throw new Error("Index out of bounds on DoublyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;if(t===this.size-1)return void this.insertBack(e);for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=this.head,r=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof n.data?JSON.stringify(n.data):n.data,JSON.stringify(this.head.data)===JSON.stringify(t))return this.head=n.next,this.head.prev=null,n=null,--this.size,!0;if(JSON.stringify(this.tail.data)===JSON.stringify(t))return n=this.tail,this.tail=n.prev,this.tail.next=null,n=null,--this.size,!0;for(n=n.next;null!==n;){if(e="object"==typeof n.data?JSON.stringify(n.data):n.data,e===i){r=!0;var o=n.prev;o.next=n.next,n.next.prev=o,n=null,--this.size;break}n=n.next}return r},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on DoublyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,this.tail=null,void(this.size=0);var e=this.head;if(0===t)return this.head=e.next,this.head.prev=null,e=null,void--this.size;if(t===this.size-1)return e=this.tail,this.tail=e.prev,this.tail.next=null,e=null,--this.size,!0;e=e.next;for(var i=1;i0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for DoublyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i0)for(var e=0;e0?parseInt(t):void 0};const s=2147483647,h=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash constructor");if("number"!=typeof t)throw new TypeError("Invalid argument; expected a number in RollingHash constructor");this.BASE=t,this.state=0,this.CACHE=1,this.BUFFER_SIZE=Math.log(t)*Math.LOG10E+1|0,this.INVERSE_BASE=n(this.BASE,s)%s,this.OFFSET_IF_NEGATIVE=s*this.BASE};h.prototype.hash=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash.hash");if(!parseInt(Math.pow(this.BASE,t.length-1)))throw new Error('Integer overflow while trying to hash "'+t+'" in RollingHash.hash\nThis hashing window is too large\nIf this issue is breaking to your program, please report this to https://github.com/nickzuber/needle/issues');var e=0;if("string"==typeof t)for(var i=0;i=this.size&&0!==t)throw new Error("Index out of bounds on SinglyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=null,r=this.head,o=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i)return this.head=r.next,r=null,--this.size,!0;for(n=r,r=n.next;null!==r;){if(e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i){o=!0,n.next=r.next,r=null,--this.size;break}n=r,r=r.next}return o},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for SinglyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on SinglyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,void(this.size=0);var e=null,i=this.head;if(0===t)return this.head=i.next,i=null,void--this.size;e=i,i=i.next;for(var n=1;n0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for SinglyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i1&&!r(t,this.elements[n]);)n=Math.floor(e+(i-e)/2),o(this.elements[n],t,this.compare)?e=n:i=n;o(t,this.elements[n],this.compare)?this.elements.splice(n,0,t):this.elements.splice(n+1,0,t)},s.prototype["delete"]=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.delete");if(0===this.elements.length)return!1;for(var e=0,i=this.elements.length,n=Math.floor(e+(i-e)/2);i>=e;){if(n=Math.floor(e+(i-e)/2),r(t,this.elements[n]))return this.elements.splice(n,1),!0;o(this.elements[n],t,this.compare)?e=n+1:i=n-1}return!1},s.prototype.get=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.get");if(0==typeof t)throw new Error("Invalid argument; expected a number in SortedArray.get");return this.elements[t]},s.prototype.size=function(){return this.elements.length},s.prototype.toString=function(){var t,e="";for(t=0;t0?e.slice(0,-1):e},e.exports=s},{}],21:[function(t,e,i){"use strict";const n=t("./stack");e.exports=n},{"./stack":22}],22:[function(t,e,i){"use strict";const n=t("../__Nodes__/unidirectional_node.js"),r=function(t){this.top,this.size,"undefined"!=typeof t?(this.top=new n(t),this.size=1):(this.top=null,this.size=0)};r.prototype.peek=function(){return this.top},r.prototype.push=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in Stack.push");var e=new n(t),i=this.top;this.top=e,this.top.next=i,++this.size},r.prototype.pop=function(){if(0===this.size)throw new Error("Attempted to remove from empty stack in Stack.pop");var t=this.top.next,e=this.top;return this.top=t,--this.size,e},e.exports=r},{"../__Nodes__/unidirectional_node.js":26}],23:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.next=null,this.prev=null};e.exports=n},{}],24:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.right=null,this.left=null};e.exports=n},{}],25:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.children=[]};n.prototype.appendChild=function(t){this.children.push(new n(t))},e.exports=n},{}],26:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.next=null};e.exports=n},{}],27:[function(t,e,i){"use strict";const n=t("./SinglyLinkedList"),r=t("./Queue"),o=t("./Stack"),s=t("./DoublyLinkedList"),h=t("./BinaryHeap"),a=t("./BinarySearchTree"),u=t("./Hashmap"),f=t("./SortedArray"),p=t("./RollingHash"),d=t("./BitArray"),l=t("./KaryTree"),y={};y.SinglyLinkedList=n,y.Queue=r,y.Stack=o,y.DoublyLinkedList=s,y.BinaryHeap=h,y.BinarySearchTree=a,y.Hashmap=u,y.SortedArray=f,y.RollingHash=p,y.BitArray=d,y.KaryTree=l,i=e.exports=y,"undefined"!=typeof window&&(window.Needle=y)},{"./BinaryHeap":2,"./BinarySearchTree":4,"./BitArray":6,"./DoublyLinkedList":8,"./Hashmap":10,"./KaryTree":12,"./Queue":13,"./RollingHash":15,"./SinglyLinkedList":17,"./SortedArray":19,"./Stack":21}]},{},[27]); \ No newline at end of file +!function t(e,i,n){function r(s,h){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!h&&a)return a(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var u=i[s]={exports:{}};e[s][0].call(u.exports,function(t){var i=e[s][1][t];return r(i?i:t)},u,u.exports,t,e,i,n)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s1;)r(this.heap[e],this.heap[Math.floor(e/2)],this.compare)&&this.heap.swap(e,Math.floor(e/2)),e=Math.floor(e/2)},o.prototype["delete"]=function(t){if("number"==typeof t?++t:t=1,"number"!=typeof t)throw new TypeError("Expected a number as argument in BinaryHeap.delete");if(t<0||t>=this.heap.length)throw new Error("Index out of bounds in BinaryHeap.delete");if(this.heap.length<=1)throw new Error("Attempted to remove min element from an empty heap in BinaryHeap.delete");if(2===this.heap.length)return void this.heap.pop();if(this.heap[t]=this.heap.pop(),2===this.heap.length);else if(3===this.heap.length){var t=1;r(this.heap[2*t],this.heap[t],this.compare)&&this.heap.swap(t,2*t)}else for(var e=0,t=1;r(this.heap[2*t],this.heap[t],this.compare)||r(this.heap[2*t+1],this.heap[t],this.compare);){var i;if(i=r(this.heap[2*t],this.heap[2*t+1],this.compare)||"undefined"==typeof this.heap[2*t+1]?2*t:2*t+1,this.heap.swap(t,i),t=i,++e,e>1e3)throw new Error("Timeout error in BinaryHeap.delete. Please report this to https://github.com/nickzuber/needle/issues")}},o.prototype._heapify=function(t){var e=2*t,i=e+1,n=t;this.heap.length;e=1;--e)this._heapify(e);console.log(this.heap)},e.exports=o},{}],2:[function(t,e,i){"use strict";const n=t("./binaryHeap");e.exports=n},{"./binaryHeap":1}],3:[function(t,e,i){"use strict";function n(t,e){return t=this.data.length*r)throw new Error("Index out of bounds in BitArray.set");var i=Math.floor(t++/r);e?this.data[i]|=1<=this.data.length*r)throw new Error("Index out of bounds in BitArray.get");var e=Math.floor(t++/r),i=this.data[e];return i>>r-t&1},o.prototype.size=function(){return this.data.length*r},o.prototype.resize=function(t){if("number"!=typeof bitarray)throw new TypeError("");return this.data.length*r},o.prototype.complement=function(t){"undefined"==typeof t?t=this:t instanceof o||(t=new o(t));var e=new o;return t.data.map(function(t){e.data.push(~t)}),e},o.prototype.union=function(t){if("undefined"==typeof t)throw new Error("Undefined bitarray argument in BitArray.union");if(!(t instanceof o))throw new TypeError("Invalid argument: expected BitArray as an argument in BitArray.union");if(t.size()!==this.size())throw new Error("BitArrays must be of same size to resolve a union in BitArray.union");for(var e=new o,i=0;i>>31),e<<=1;t.split("").reverse().join("")}),t},e.exports=o},{}],6:[function(t,e,i){"use strict";const n=t("./bitArray");e.exports=n},{"./bitArray":5}],7:[function(t,e,i){"use strict";const n=t("../Nodes/bidirectional_node.js"),r=function(t){this.head,this.tail,this.size,"undefined"!=typeof t?(this.head=new n(t),this.tail=this.head,this.size=1):(this.head=null,this.tail=null,this.size=0)};r.prototype.insertFront=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertFront");var e=new n(t);if(null===this.head&&null===this.tail)this.head=e,this.tail=this.head;else{if(null===this.head||null===this.tail)throw new Error("Unsynced head and tail in an Doubly Linked List. Please report this to https://github.com/nickzuber/needle/issues");var i=this.head;i.prev=e,this.head=e,this.head.next=i,this.head.prev=null}++this.size},r.prototype.insertNth=function(t,e){if("undefined"==typeof e||"undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.insertNth");if(t<0||t>=this.size&&0!==t)throw new Error("Index out of bounds on DoublyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;if(t===this.size-1)return void this.insertBack(e);for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=this.head,r=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof n.data?JSON.stringify(n.data):n.data,JSON.stringify(this.head.data)===JSON.stringify(t))return this.head=n.next,this.head.prev=null,n=null,--this.size,!0;if(JSON.stringify(this.tail.data)===JSON.stringify(t))return n=this.tail,this.tail=n.prev,this.tail.next=null,n=null,--this.size,!0;for(n=n.next;null!==n;){if(e="object"==typeof n.data?JSON.stringify(n.data):n.data,e===i){r=!0;var o=n.prev;o.next=n.next,n.next.prev=o,n=null,--this.size;break}n=n.next}return r},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on DoublyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,this.tail=null,void(this.size=0);var e=this.head;if(0===t)return this.head=e.next,this.head.prev=null,e=null,void--this.size;if(t===this.size-1)return e=this.tail,this.tail=e.prev,this.tail.next=null,e=null,--this.size,!0;e=e.next;for(var i=1;i0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for DoublyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i0)for(var e=0;e0?parseInt(t):void 0};const s=2147483647,h=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash constructor");if("number"!=typeof t)throw new TypeError("Invalid argument; expected a number in RollingHash constructor");this.BASE=t,this.state=0,this.CACHE=1,this.BUFFER_SIZE=Math.log(t)*Math.LOG10E+1|0,this.INVERSE_BASE=n(this.BASE,s)%s,this.OFFSET_IF_NEGATIVE=s*this.BASE};h.prototype.hash=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash.hash");if(!parseInt(Math.pow(this.BASE,t.length-1)))throw new Error('Integer overflow while trying to hash "'+t+'" in RollingHash.hash\nThis hashing window is too large\nIf this issue is breaking to your program, please report this to https://github.com/nickzuber/needle/issues');var e=0;if("string"==typeof t)for(var i=0;i=this.size&&0!==t)throw new Error("Index out of bounds on SinglyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=null,r=this.head,o=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i)return this.head=r.next,r=null,--this.size,!0;for(n=r,r=n.next;null!==r;){if(e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i){o=!0,n.next=r.next,r=null,--this.size;break}n=r,r=r.next}return o},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for SinglyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on SinglyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,void(this.size=0);var e=null,i=this.head;if(0===t)return this.head=i.next,i=null,void--this.size;e=i,i=i.next;for(var n=1;n0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for SinglyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i1&&!r(t,this.elements[n]);)n=Math.floor(e+(i-e)/2),o(this.elements[n],t,this.compare)?e=n:i=n;o(t,this.elements[n],this.compare)?this.elements.splice(n,0,t):this.elements.splice(n+1,0,t)},s.prototype["delete"]=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.delete");if(0===this.elements.length)return!1;for(var e=0,i=this.elements.length,n=Math.floor(e+(i-e)/2);i>=e;){if(n=Math.floor(e+(i-e)/2),r(t,this.elements[n]))return this.elements.splice(n,1),!0;o(this.elements[n],t,this.compare)?e=n+1:i=n-1}return!1},s.prototype.get=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.get");if(0==typeof t)throw new Error("Invalid argument; expected a number in SortedArray.get");return this.elements[t]},s.prototype.size=function(){return this.elements.length},s.prototype.toString=function(){var t,e="";for(t=0;t0?e.slice(0,-1):e},e.exports=s},{}],25:[function(t,e,i){"use strict";const n=t("./stack");e.exports=n},{"./stack":26}],26:[function(t,e,i){"use strict";const n=t("../Nodes/unidirectional_node.js"),r=function(t){this.top,this.size,"undefined"!=typeof t?(this.top=new n(t),this.size=1):(this.top=null,this.size=0)};r.prototype.peek=function(){return this.top},r.prototype.push=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in Stack.push");var e=new n(t),i=this.top;this.top=e,this.top.next=i,++this.size},r.prototype.pop=function(){if(0===this.size)throw new Error("Attempted to remove from empty stack in Stack.pop");var t=this.top.next,e=this.top;return this.top=t,--this.size,e},e.exports=r},{"../Nodes/unidirectional_node.js":16}],27:[function(t,e,i){"use strict";const n=t("./SinglyLinkedList"),r=t("./Queue"),o=t("./Stack"),s=t("./DoublyLinkedList"),h=t("./BinaryHeap"),a=t("./BinarySearchTree"),u=t("./Hashmap"),f=t("./SortedArray"),p=t("./RollingHash"),d=t("./BitArray"),l=t("./KaryTree"),y={};y.SinglyLinkedList=n,y.Queue=r,y.Stack=o,y.DoublyLinkedList=s,y.BinaryHeap=h,y.BinarySearchTree=a,y.Hashmap=u,y.SortedArray=f,y.RollingHash=p,y.BitArray=d,y.KaryTree=l,i=e.exports=y,"undefined"!=typeof window&&(window.Needle=y)},{"./BinaryHeap":2,"./BinarySearchTree":4,"./BitArray":6,"./DoublyLinkedList":8,"./Hashmap":10,"./KaryTree":12,"./Queue":17,"./RollingHash":19,"./SinglyLinkedList":21,"./SortedArray":23,"./Stack":25}]},{},[27]); \ No newline at end of file diff --git a/examples/scripts/needle.min.js b/examples/scripts/needle.min.js index b26e905..2072473 100644 --- a/examples/scripts/needle.min.js +++ b/examples/scripts/needle.min.js @@ -2,4 +2,4 @@ // node-needle v0.1.9 | MIT // Copyright (c) 2016 Nick Zuber */ -!function t(e,i,n){function r(s,h){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!h&&a)return a(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var u=i[s]={exports:{}};e[s][0].call(u.exports,function(t){var i=e[s][1][t];return r(i?i:t)},u,u.exports,t,e,i,n)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s1;)r(this.heap[e],this.heap[Math.floor(e/2)],this.compare)&&this.heap.swap(e,Math.floor(e/2)),e=Math.floor(e/2)},o.prototype["delete"]=function(t){if("number"==typeof t?++t:t=1,"number"!=typeof t)throw new TypeError("Expected a number as argument in BinaryHeap.delete");if(t<0||t>=this.heap.length)throw new Error("Index out of bounds in BinaryHeap.delete");if(this.heap.length<=1)throw new Error("Attempted to remove min element from an empty heap in BinaryHeap.delete");if(2===this.heap.length)return void this.heap.pop();if(this.heap[t]=this.heap.pop(),2===this.heap.length);else if(3===this.heap.length){var t=1;r(this.heap[2*t],this.heap[t],this.compare)&&this.heap.swap(t,2*t)}else for(var e=0,t=1;r(this.heap[2*t],this.heap[t],this.compare)||r(this.heap[2*t+1],this.heap[t],this.compare);){var i;if(i=r(this.heap[2*t],this.heap[2*t+1],this.compare)||"undefined"==typeof this.heap[2*t+1]?2*t:2*t+1,this.heap.swap(t,i),t=i,++e,e>1e3)throw new Error("Timeout error in BinaryHeap.delete. Please report this to https://github.com/nickzuber/needle/issues")}},o.prototype._heapify=function(t){var e=2*t,i=e+1,n=t;this.heap.length;e=1;--e)this._heapify(e);console.log(this.heap)},e.exports=o},{}],2:[function(t,e,i){"use strict";const n=t("./binaryHeap");e.exports=n},{"./binaryHeap":1}],3:[function(t,e,i){"use strict";function n(t,e){return t=this.data.length*r)throw new Error("Index out of bounds in BitArray.set");var i=Math.floor(t++/r);e?this.data[i]|=1<=this.data.length*r)throw new Error("Index out of bounds in BitArray.get");var e=Math.floor(t++/r),i=this.data[e];return i>>r-t&1},o.prototype.size=function(){return this.data.length*r},o.prototype.resize=function(t){if("number"!=typeof bitarray)throw new TypeError("");return this.data.length*r},o.prototype.complement=function(t){"undefined"==typeof t?t=this:t instanceof o||(t=new o(t));var e=new o;return t.data.map(function(t){e.data.push(~t)}),e},o.prototype.union=function(t){if("undefined"==typeof t)throw new Error("Undefined bitarray argument in BitArray.union");if(!(t instanceof o))throw new TypeError("Invalid argument: expected BitArray as an argument in BitArray.union");if(t.size()!==this.size())throw new Error("BitArrays must be of same size to resolve a union in BitArray.union");for(var e=new o,i=0;i>>31),e<<=1;t.split("").reverse().join("")}),t},e.exports=o},{}],6:[function(t,e,i){"use strict";const n=t("./bitArray");e.exports=n},{"./bitArray":5}],7:[function(t,e,i){"use strict";const n=t("../__Nodes__/bidirectional_node.js"),r=function(t){this.head,this.tail,this.size,"undefined"!=typeof t?(this.head=new n(t),this.tail=this.head,this.size=1):(this.head=null,this.tail=null,this.size=0)};r.prototype.insertFront=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertFront");var e=new n(t);if(null===this.head&&null===this.tail)this.head=e,this.tail=this.head;else{if(null===this.head||null===this.tail)throw new Error("Unsynced head and tail in an Doubly Linked List. Please report this to https://github.com/nickzuber/needle/issues");var i=this.head;i.prev=e,this.head=e,this.head.next=i,this.head.prev=null}++this.size},r.prototype.insertNth=function(t,e){if("undefined"==typeof e||"undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.insertNth");if(t<0||t>=this.size&&0!==t)throw new Error("Index out of bounds on DoublyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;if(t===this.size-1)return void this.insertBack(e);for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=this.head,r=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof n.data?JSON.stringify(n.data):n.data,JSON.stringify(this.head.data)===JSON.stringify(t))return this.head=n.next,this.head.prev=null,n=null,--this.size,!0;if(JSON.stringify(this.tail.data)===JSON.stringify(t))return n=this.tail,this.tail=n.prev,this.tail.next=null,n=null,--this.size,!0;for(n=n.next;null!==n;){if(e="object"==typeof n.data?JSON.stringify(n.data):n.data,e===i){r=!0;var o=n.prev;o.next=n.next,n.next.prev=o,n=null,--this.size;break}n=n.next}return r},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on DoublyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,this.tail=null,void(this.size=0);var e=this.head;if(0===t)return this.head=e.next,this.head.prev=null,e=null,void--this.size;if(t===this.size-1)return e=this.tail,this.tail=e.prev,this.tail.next=null,e=null,--this.size,!0;e=e.next;for(var i=1;i0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for DoublyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i0)for(var e=0;e0?parseInt(t):void 0};const s=2147483647,h=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash constructor");if("number"!=typeof t)throw new TypeError("Invalid argument; expected a number in RollingHash constructor");this.BASE=t,this.state=0,this.CACHE=1,this.BUFFER_SIZE=Math.log(t)*Math.LOG10E+1|0,this.INVERSE_BASE=n(this.BASE,s)%s,this.OFFSET_IF_NEGATIVE=s*this.BASE};h.prototype.hash=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash.hash");if(!parseInt(Math.pow(this.BASE,t.length-1)))throw new Error('Integer overflow while trying to hash "'+t+'" in RollingHash.hash\nThis hashing window is too large\nIf this issue is breaking to your program, please report this to https://github.com/nickzuber/needle/issues');var e=0;if("string"==typeof t)for(var i=0;i=this.size&&0!==t)throw new Error("Index out of bounds on SinglyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=null,r=this.head,o=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i)return this.head=r.next,r=null,--this.size,!0;for(n=r,r=n.next;null!==r;){if(e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i){o=!0,n.next=r.next,r=null,--this.size;break}n=r,r=r.next}return o},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for SinglyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on SinglyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,void(this.size=0);var e=null,i=this.head;if(0===t)return this.head=i.next,i=null,void--this.size;e=i,i=i.next;for(var n=1;n0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for SinglyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i1&&!r(t,this.elements[n]);)n=Math.floor(e+(i-e)/2),o(this.elements[n],t,this.compare)?e=n:i=n;o(t,this.elements[n],this.compare)?this.elements.splice(n,0,t):this.elements.splice(n+1,0,t)},s.prototype["delete"]=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.delete");if(0===this.elements.length)return!1;for(var e=0,i=this.elements.length,n=Math.floor(e+(i-e)/2);i>=e;){if(n=Math.floor(e+(i-e)/2),r(t,this.elements[n]))return this.elements.splice(n,1),!0;o(this.elements[n],t,this.compare)?e=n+1:i=n-1}return!1},s.prototype.get=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.get");if(0==typeof t)throw new Error("Invalid argument; expected a number in SortedArray.get");return this.elements[t]},s.prototype.size=function(){return this.elements.length},s.prototype.toString=function(){var t,e="";for(t=0;t0?e.slice(0,-1):e},e.exports=s},{}],21:[function(t,e,i){"use strict";const n=t("./stack");e.exports=n},{"./stack":22}],22:[function(t,e,i){"use strict";const n=t("../__Nodes__/unidirectional_node.js"),r=function(t){this.top,this.size,"undefined"!=typeof t?(this.top=new n(t),this.size=1):(this.top=null,this.size=0)};r.prototype.peek=function(){return this.top},r.prototype.push=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in Stack.push");var e=new n(t),i=this.top;this.top=e,this.top.next=i,++this.size},r.prototype.pop=function(){if(0===this.size)throw new Error("Attempted to remove from empty stack in Stack.pop");var t=this.top.next,e=this.top;return this.top=t,--this.size,e},e.exports=r},{"../__Nodes__/unidirectional_node.js":26}],23:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.next=null,this.prev=null};e.exports=n},{}],24:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.right=null,this.left=null};e.exports=n},{}],25:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.children=[]};n.prototype.appendChild=function(t){this.children.push(new n(t))},e.exports=n},{}],26:[function(t,e,i){"use strict";const n=function(t){this.data=t||void 0,this.next=null};e.exports=n},{}],27:[function(t,e,i){"use strict";const n=t("./SinglyLinkedList"),r=t("./Queue"),o=t("./Stack"),s=t("./DoublyLinkedList"),h=t("./BinaryHeap"),a=t("./BinarySearchTree"),u=t("./Hashmap"),f=t("./SortedArray"),p=t("./RollingHash"),d=t("./BitArray"),l=t("./KaryTree"),y={};y.SinglyLinkedList=n,y.Queue=r,y.Stack=o,y.DoublyLinkedList=s,y.BinaryHeap=h,y.BinarySearchTree=a,y.Hashmap=u,y.SortedArray=f,y.RollingHash=p,y.BitArray=d,y.KaryTree=l,i=e.exports=y,"undefined"!=typeof window&&(window.Needle=y)},{"./BinaryHeap":2,"./BinarySearchTree":4,"./BitArray":6,"./DoublyLinkedList":8,"./Hashmap":10,"./KaryTree":12,"./Queue":13,"./RollingHash":15,"./SinglyLinkedList":17,"./SortedArray":19,"./Stack":21}]},{},[27]); \ No newline at end of file +!function t(e,i,n){function r(s,h){if(!i[s]){if(!e[s]){var a="function"==typeof require&&require;if(!h&&a)return a(s,!0);if(o)return o(s,!0);throw new Error("Cannot find module '"+s+"'")}var u=i[s]={exports:{}};e[s][0].call(u.exports,function(t){var i=e[s][1][t];return r(i?i:t)},u,u.exports,t,e,i,n)}return i[s].exports}for(var o="function"==typeof require&&require,s=0;s1;)r(this.heap[e],this.heap[Math.floor(e/2)],this.compare)&&this.heap.swap(e,Math.floor(e/2)),e=Math.floor(e/2)},o.prototype["delete"]=function(t){if("number"==typeof t?++t:t=1,"number"!=typeof t)throw new TypeError("Expected a number as argument in BinaryHeap.delete");if(t<0||t>=this.heap.length)throw new Error("Index out of bounds in BinaryHeap.delete");if(this.heap.length<=1)throw new Error("Attempted to remove min element from an empty heap in BinaryHeap.delete");if(2===this.heap.length)return void this.heap.pop();if(this.heap[t]=this.heap.pop(),2===this.heap.length);else if(3===this.heap.length){var t=1;r(this.heap[2*t],this.heap[t],this.compare)&&this.heap.swap(t,2*t)}else for(var e=0,t=1;r(this.heap[2*t],this.heap[t],this.compare)||r(this.heap[2*t+1],this.heap[t],this.compare);){var i;if(i=r(this.heap[2*t],this.heap[2*t+1],this.compare)||"undefined"==typeof this.heap[2*t+1]?2*t:2*t+1,this.heap.swap(t,i),t=i,++e,e>1e3)throw new Error("Timeout error in BinaryHeap.delete. Please report this to https://github.com/nickzuber/needle/issues")}},o.prototype._heapify=function(t){var e=2*t,i=e+1,n=t;this.heap.length;e=1;--e)this._heapify(e);console.log(this.heap)},e.exports=o},{}],2:[function(t,e,i){"use strict";const n=t("./binaryHeap");e.exports=n},{"./binaryHeap":1}],3:[function(t,e,i){"use strict";function n(t,e){return t=this.data.length*r)throw new Error("Index out of bounds in BitArray.set");var i=Math.floor(t++/r);e?this.data[i]|=1<=this.data.length*r)throw new Error("Index out of bounds in BitArray.get");var e=Math.floor(t++/r),i=this.data[e];return i>>r-t&1},o.prototype.size=function(){return this.data.length*r},o.prototype.resize=function(t){if("number"!=typeof bitarray)throw new TypeError("");return this.data.length*r},o.prototype.complement=function(t){"undefined"==typeof t?t=this:t instanceof o||(t=new o(t));var e=new o;return t.data.map(function(t){e.data.push(~t)}),e},o.prototype.union=function(t){if("undefined"==typeof t)throw new Error("Undefined bitarray argument in BitArray.union");if(!(t instanceof o))throw new TypeError("Invalid argument: expected BitArray as an argument in BitArray.union");if(t.size()!==this.size())throw new Error("BitArrays must be of same size to resolve a union in BitArray.union");for(var e=new o,i=0;i>>31),e<<=1;t.split("").reverse().join("")}),t},e.exports=o},{}],6:[function(t,e,i){"use strict";const n=t("./bitArray");e.exports=n},{"./bitArray":5}],7:[function(t,e,i){"use strict";const n=t("../Nodes/bidirectional_node.js"),r=function(t){this.head,this.tail,this.size,"undefined"!=typeof t?(this.head=new n(t),this.tail=this.head,this.size=1):(this.head=null,this.tail=null,this.size=0)};r.prototype.insertFront=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertFront");var e=new n(t);if(null===this.head&&null===this.tail)this.head=e,this.tail=this.head;else{if(null===this.head||null===this.tail)throw new Error("Unsynced head and tail in an Doubly Linked List. Please report this to https://github.com/nickzuber/needle/issues");var i=this.head;i.prev=e,this.head=e,this.head.next=i,this.head.prev=null}++this.size},r.prototype.insertNth=function(t,e){if("undefined"==typeof e||"undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.insertNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.insertNth");if(t<0||t>=this.size&&0!==t)throw new Error("Index out of bounds on DoublyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;if(t===this.size-1)return void this.insertBack(e);for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=this.head,r=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof n.data?JSON.stringify(n.data):n.data,JSON.stringify(this.head.data)===JSON.stringify(t))return this.head=n.next,this.head.prev=null,n=null,--this.size,!0;if(JSON.stringify(this.tail.data)===JSON.stringify(t))return n=this.tail,this.tail=n.prev,this.tail.next=null,n=null,--this.size,!0;for(n=n.next;null!==n;){if(e="object"==typeof n.data?JSON.stringify(n.data):n.data,e===i){r=!0;var o=n.prev;o.next=n.next,n.next.prev=o,n=null,--this.size;break}n=n.next}return r},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for DoublyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on DoublyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,this.tail=null,void(this.size=0);var e=this.head;if(0===t)return this.head=e.next,this.head.prev=null,e=null,void--this.size;if(t===this.size-1)return e=this.tail,this.tail=e.prev,this.tail.next=null,e=null,--this.size,!0;e=e.next;for(var i=1;i0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for DoublyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for DoublyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i0)for(var e=0;e0?parseInt(t):void 0};const s=2147483647,h=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash constructor");if("number"!=typeof t)throw new TypeError("Invalid argument; expected a number in RollingHash constructor");this.BASE=t,this.state=0,this.CACHE=1,this.BUFFER_SIZE=Math.log(t)*Math.LOG10E+1|0,this.INVERSE_BASE=n(this.BASE,s)%s,this.OFFSET_IF_NEGATIVE=s*this.BASE};h.prototype.hash=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in RollingHash.hash");if(!parseInt(Math.pow(this.BASE,t.length-1)))throw new Error('Integer overflow while trying to hash "'+t+'" in RollingHash.hash\nThis hashing window is too large\nIf this issue is breaking to your program, please report this to https://github.com/nickzuber/needle/issues');var e=0;if("string"==typeof t)for(var i=0;i=this.size&&0!==t)throw new Error("Index out of bounds on SinglyLinkedList.insertNth");if(0===t)return void this.insertFront(e);if(null===this.head)return!1;for(var i=new n(e),r=this.head,o=1;o0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");var e,i,n=null,r=this.head,o=!1;if(i="object"==typeof t?JSON.stringify(t):t,e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i)return this.head=r.next,r=null,--this.size,!0;for(n=r,r=n.next;null!==r;){if(e="object"==typeof r.data?JSON.stringify(r.data):r.data,e===i){o=!0,n.next=r.next,r=null,--this.size;break}n=r,r=r.next}return o},r.prototype.removeNth=function(t){if("undefined"==typeof t)throw new Error("Too few arguments for SinglyLinkedList.removeNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.removeNth");if(t<0||t>=this.size)throw new Error("Index out of bounds on SinglyLinkedList.removeNth: "+t);if(1===this.size)return this.head=null,void(this.size=0);var e=null,i=this.head;if(0===t)return this.head=i.next,i=null,void--this.size;e=i,i=i.next;for(var n=1;n0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=!1;null!==e;){if(JSON.stringify(e.data)===JSON.stringify(t)){i=!0;break}e=e.next}return!!i&&e},r.prototype.findNth=function(t){if("undefined"==typeof t)throw new TypeError("Too few arguments for SinglyLinkedList.findNth");if("number"!=typeof t)throw new TypeError("Invalid argument for SinglyLinkedList.findNth");if(t<0||t>=this.size)throw new Error("Index out of bounds; attempted to find a node from a SinglyLinkedList that does not exist");if(null===this.head&&this.size>0)throw new Error("Null head in an unemptied list. Please report this to https://github.com/nickzuber/needle/issues");for(var e=this.head,i=0;i1&&!r(t,this.elements[n]);)n=Math.floor(e+(i-e)/2),o(this.elements[n],t,this.compare)?e=n:i=n;o(t,this.elements[n],this.compare)?this.elements.splice(n,0,t):this.elements.splice(n+1,0,t)},s.prototype["delete"]=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.delete");if(0===this.elements.length)return!1;for(var e=0,i=this.elements.length,n=Math.floor(e+(i-e)/2);i>=e;){if(n=Math.floor(e+(i-e)/2),r(t,this.elements[n]))return this.elements.splice(n,1),!0;o(this.elements[n],t,this.compare)?e=n+1:i=n-1}return!1},s.prototype.get=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in SortedArray.get");if(0==typeof t)throw new Error("Invalid argument; expected a number in SortedArray.get");return this.elements[t]},s.prototype.size=function(){return this.elements.length},s.prototype.toString=function(){var t,e="";for(t=0;t0?e.slice(0,-1):e},e.exports=s},{}],25:[function(t,e,i){"use strict";const n=t("./stack");e.exports=n},{"./stack":26}],26:[function(t,e,i){"use strict";const n=t("../Nodes/unidirectional_node.js"),r=function(t){this.top,this.size,"undefined"!=typeof t?(this.top=new n(t),this.size=1):(this.top=null,this.size=0)};r.prototype.peek=function(){return this.top},r.prototype.push=function(t){if("undefined"==typeof t)throw new Error("Too few arguments in Stack.push");var e=new n(t),i=this.top;this.top=e,this.top.next=i,++this.size},r.prototype.pop=function(){if(0===this.size)throw new Error("Attempted to remove from empty stack in Stack.pop");var t=this.top.next,e=this.top;return this.top=t,--this.size,e},e.exports=r},{"../Nodes/unidirectional_node.js":16}],27:[function(t,e,i){"use strict";const n=t("./SinglyLinkedList"),r=t("./Queue"),o=t("./Stack"),s=t("./DoublyLinkedList"),h=t("./BinaryHeap"),a=t("./BinarySearchTree"),u=t("./Hashmap"),f=t("./SortedArray"),p=t("./RollingHash"),d=t("./BitArray"),l=t("./KaryTree"),y={};y.SinglyLinkedList=n,y.Queue=r,y.Stack=o,y.DoublyLinkedList=s,y.BinaryHeap=h,y.BinarySearchTree=a,y.Hashmap=u,y.SortedArray=f,y.RollingHash=p,y.BitArray=d,y.KaryTree=l,i=e.exports=y,"undefined"!=typeof window&&(window.Needle=y)},{"./BinaryHeap":2,"./BinarySearchTree":4,"./BitArray":6,"./DoublyLinkedList":8,"./Hashmap":10,"./KaryTree":12,"./Queue":17,"./RollingHash":19,"./SinglyLinkedList":21,"./SortedArray":23,"./Stack":25}]},{},[27]); \ No newline at end of file diff --git a/examples/views/index.html b/examples/views/index.html index 7e40cfd..6706abb 100644 --- a/examples/views/index.html +++ b/examples/views/index.html @@ -9,14 +9,22 @@ - \ No newline at end of file + diff --git a/gulpfile.js b/gulpfile.js index ebe7aee..070a9d9 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -16,8 +16,10 @@ var banner = ['/*!', // Contacat & compress javascript files gulp.task('dispatch', function(){ - gulp.src(['src/*.js']) - .pipe(browserify({})) + gulp.src(['src/index.js']) + .pipe(browserify({ + entries: ['./index.js'] + })) .pipe(uglify()) .pipe(rename({ basename: 'needle', diff --git a/package.json b/package.json index bcb3e94..f236b49 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "src/needle.js", "scripts": { "build": "gulp", - "dev": "gulp watch", + "dev": "gulp && gulp watch || true", "bench": "node --harmony benchmarks/runTests.js || true", "unit": "node --harmony unit/runTests.js || true", "lint": "./node_modules/.bin/eslint src/*.js || true", diff --git a/src/BinarySearchTree/binarySearchTree.js b/src/BinarySearchTree/binarySearchTree.js index 489c640..54d9947 100644 --- a/src/BinarySearchTree/binarySearchTree.js +++ b/src/BinarySearchTree/binarySearchTree.js @@ -18,14 +18,18 @@ * | search | O(n) | * +------------------------------+ * - * TODO: let user set a custom `equal` function - * */ - 'use strict'; +'use strict'; + +const Node = require('../Nodes/bidirectional_tree_node.js'); -const Node = require('../__Nodes__/bidirectional_tree_node.js'); +const Types = { + LEFT: 'left', + RIGHT: 'right' +} +// @TODO: let user set a custom `compare` function /** @private @default * Compares two elements and returns. * @param {number} first index to compare @@ -206,6 +210,57 @@ BinarySearchTree.prototype.insert = function(data, node){ } } +/** + * Deletes a node from the tree with the value of `data`. + * @param {any} data The data of the node to delete. + * @return {boolean} Returns the success of the deletion success. + */ +BinarySearchTree.prototype.delete = function (data) { + deleteHelper = deleteHelper.bind(this); + return deleteHelper(data, this.root, null, null); +} + +/** + * Deletes a node from the tree with the value of `data`. + * @param {any} data The data of the node to delete. + * @param {Node} node The current node being analyzed. + * @param {Node} nodeType The type of child of the current node being analyzed. + * @param {Node} parentNode The last node that was analyzed. + * @return {boolean} Returns the success of the deletion success. + */ +function deleteHelper (data, node, nodeType, parentNode) { + if (node === null) { + return false; + } + + // @TODO handle object comparisons -- doing a stringify is horrible dont do that + if (data === node.data) { + if (nodeType === Types.RIGHT) { + parentNode.right = null; + } else { + parentNode.left = null; + } + + // Fix tree + if (node.left) { + var nodeRightSubtree = node.right; + parentNode[nodeType] = node.left; + parentNode[nodeType].right = nodeRightSubtree; + } else if (node.right) { + var nodeLeftSubtree = node.left; + parentNode[nodeType] = node.right; + parentNode[nodeType].left = nodeLeftSubtree; + } + return true; + } + + if (safeCompare(data, node.data, this.compare)) { + return deleteHelper(data, node.left, Types.LEFT, node); + } else { + return deleteHelper(data, node.right, Types.RIGHT, node); + } +} + /** * Search for a node in the tree. * @param {*} the data of the node being searched for diff --git a/src/DoublyLinkedList/doublyLinkedList.js b/src/DoublyLinkedList/doublyLinkedList.js index 7a308c9..73814e3 100644 --- a/src/DoublyLinkedList/doublyLinkedList.js +++ b/src/DoublyLinkedList/doublyLinkedList.js @@ -20,7 +20,7 @@ 'use strict'; -const Node = require('../__Nodes__/bidirectional_node.js'); +const Node = require('../Nodes/bidirectional_node.js'); /** * Single argument constructor. diff --git a/src/KaryTree/karyTree.js b/src/KaryTree/karyTree.js index 2b3865b..4bb5315 100644 --- a/src/KaryTree/karyTree.js +++ b/src/KaryTree/karyTree.js @@ -16,7 +16,7 @@ 'use strict'; -const Node = require('../__Nodes__/multidirectional_tree_node.js'); +const Node = require('../Nodes/multidirectional_tree_node.js'); /** * Creates an empty k-ary tree. diff --git a/src/__Nodes__/bidirectional_node.js b/src/Nodes/bidirectional_node.js similarity index 100% rename from src/__Nodes__/bidirectional_node.js rename to src/Nodes/bidirectional_node.js diff --git a/src/__Nodes__/bidirectional_tree_node.js b/src/Nodes/bidirectional_tree_node.js similarity index 100% rename from src/__Nodes__/bidirectional_tree_node.js rename to src/Nodes/bidirectional_tree_node.js diff --git a/src/__Nodes__/multidirectional_tree_node.js b/src/Nodes/multidirectional_tree_node.js similarity index 100% rename from src/__Nodes__/multidirectional_tree_node.js rename to src/Nodes/multidirectional_tree_node.js diff --git a/src/__Nodes__/unidirectional_node.js b/src/Nodes/unidirectional_node.js similarity index 100% rename from src/__Nodes__/unidirectional_node.js rename to src/Nodes/unidirectional_node.js diff --git a/src/Queue/queue.js b/src/Queue/queue.js index fbe05f6..01c826a 100644 --- a/src/Queue/queue.js +++ b/src/Queue/queue.js @@ -12,7 +12,7 @@ * */ -const Node = require('../__Nodes__/unidirectional_node.js'); +const Node = require('../Nodes/unidirectional_node.js'); /** * Single argument constructor. diff --git a/src/SinglyLinkedList/singlyLinkedList.js b/src/SinglyLinkedList/singlyLinkedList.js index 723940b..c96b35e 100644 --- a/src/SinglyLinkedList/singlyLinkedList.js +++ b/src/SinglyLinkedList/singlyLinkedList.js @@ -19,7 +19,7 @@ 'use strict'; -const Node = require('../__Nodes__/unidirectional_node.js'); +const Node = require('../Nodes/unidirectional_node.js'); /** * Single argument constructor. diff --git a/src/Stack/stack.js b/src/Stack/stack.js index 0066365..57003a1 100644 --- a/src/Stack/stack.js +++ b/src/Stack/stack.js @@ -14,7 +14,7 @@ 'use strict'; -const Node = require('../__Nodes__/unidirectional_node.js'); +const Node = require('../Nodes/unidirectional_node.js'); /** * Single argument constructor.