UNPKG

data-structures

Version:

JavaScript data structures written in CoffeeScript.

1 lines 13.4 kB
require=function(t,e,i){function r(i,o){if(!e[i]){if(!t[i]){var s="function"==typeof require&&require;if(!o&&s)return s(i,!0);if(n)return n(i,!0);throw Error("Cannot find module '"+i+"'")}var u=e[i]={exports:{}};t[i][0](function(e){var n=t[i][1][e];return r(n?n:e)},u,u.exports)}return e[i].exports}for(var n="function"==typeof require&&require,o=0;i.length>o;o++)r(i[o]);return r}({"data-structures":[function(t,e){e.exports=t("EerzU6")},{}],EerzU6:[function(t,e){(function(){e.exports={Graph:t("./Graph"),Heap:t("./Heap"),LinkedList:t("./LinkedList"),Map:t("./Map"),Queue:t("./Queue"),RedBlackTree:t("./RedBlackTree"),Trie:t("./Trie")}}).call(this)},{"./Graph":1,"./Heap":2,"./LinkedList":3,"./Map":4,"./Queue":5,"./RedBlackTree":6,"./Trie":7}],1:[function(t,e){(function(){var t;t=function(){function t(){this._nodes={},this.nodeSize=0,this.edgeSize=0}return t.prototype.addNode=function(t){return this._nodes[t]?void 0:(this.nodeSize++,this._nodes[t]={_id:t,_outEdges:{},_inEdges:{},_edgeCount:0})},t.prototype.getNode=function(t){return this._nodes[t]},t.prototype.removeNode=function(t){var e;return e=this._nodes[t],e?(this.edgeSize-=this._nodes[t]._edgeCount,this.nodeSize--,delete this._nodes[t],e):void 0},t.prototype.addEdge=function(t,e,i){var r,n,o;return null==i&&(i=1),!this.getEdge(t,e)&&(n=this._nodes[t],o=this._nodes[e],n&&o)?(r={_fromId:t,_toId:e,weight:i},n._outEdges[e]=r,o._inEdges[t]=r,n._edgeCount++,this.edgeSize++,n!==o&&o._edgeCount++,r):void 0},t.prototype.getEdge=function(t,e){var i,r;if(i=this._nodes[t],r=this._nodes[e],i||r)if(i)if(r)if(!i._outEdges[e]&&r._inEdges[t])delete r._inEdges[t];else{if(r._inEdges[t]||!i._outEdges[e])return i._outEdges[e];delete i._outEdges[e]}else i._outEdges[e]&&delete i._outEdges[e];else r._inEdges[t]&&delete r._inEdges[t];else;},t.prototype.removeEdge=function(t,e){var i,r,n;return r=this._nodes[t],n=this._nodes[e],(i=this.getEdge(t,e))?(delete r._outEdges[e],delete n._inEdges[t],this.edgeSize--,i):void 0},t.prototype.getInEdgesOf=function(t){var e,i,r,n;if(n=this._nodes[t],!n)return[];r=[];for(i in n._inEdges)e=this.getEdge(i,t),e&&r.push(e);return r},t.prototype.getOutEdgesOf=function(t){var e,i,r,n;if(i=this._nodes[t],!i)return[];r=[];for(n in i._outEdges)e=this.getEdge(t,n),e&&r.push(e);return r},t.prototype.getAllEdgesOf=function(t){var e,i,r,n,o,s,u;if(i=this.getInEdgesOf(t),r=this.getOutEdgesOf(t),0===i.length)return r;if(n=this.getEdge(t,t))for(e=o=0,s=i.length;s>=0?s>o:o>s;e=s>=0?++o:--o)if(i[e]===n){u=[i[i.length-1],i[e]],i[e]=u[0],i[i.length-1]=u[1],i.pop();break}return i.concat(r)},t.prototype.forEachNode=function(t){var e,i,r;r=this._nodes;for(e in r)i=r[e],t(i)},t.prototype.forEachEdge=function(t){var e,i,r,n,o,s;o=this._nodes;for(i in o){r=o[i],s=r._outEdges;for(n in s)e=s[n],t(e)}},t}(),e.exports=t}).call(this)},{}],2:[function(t,e){(function(){var t,i,r,n;t=function(){function t(t){var e,i,r,n,o,s;for(null==t&&(t=[]),this._data=[void 0],r=0,o=t.length;o>r;r++)i=t[r],null!=i&&this._data.push(i);if(this._data.length>1)for(e=n=2,s=this._data.length;s>=2?s>n:n>s;e=s>=2?++n:--n)this._upHeap(e);this.size=this._data.length-1}return t.prototype.add=function(t){return null!=t?(this._data.push(t),this._upHeap(this._data.length-1),this.size++,t):void 0},t.prototype.removeMin=function(){var t;if(1!==this._data.length)return this.size--,2===this._data.length?this._data.pop():(t=this._data[1],this._data[1]=this._data.pop(),this._downHeap(),t)},t.prototype.peekMin=function(){return this._data[1]},t.prototype._upHeap=function(t){var e,i;for(e=this._data[t];this._data[t]<this._data[r(t)]&&t>1;)i=[this._data[r(t)],this._data[t]],this._data[t]=i[0],this._data[r(t)]=i[1],t=r(t)},t.prototype._downHeap=function(){var t,e,r;for(t=1;i(this._data.length>t)&&(e=i(t),this._data.length-1>e&&this._data[n(t)]<this._data[e]&&(e=n(t)),this._data[e]<this._data[t]);)r=[this._data[t],this._data[e]],this._data[e]=r[0],this._data[t]=r[1],t=e},t}(),r=function(t){return t>>1},i=function(t){return t<<1},n=function(t){return(t<<1)+1},e.exports=t}).call(this)},{}],3:[function(t,e){(function(){var t;t=function(){function t(t){var e,i,r;for(null==t&&(t=[]),this.head={prev:void 0,value:void 0,next:void 0},this.tail={prev:void 0,value:void 0,next:void 0},this.size=0,i=0,r=t.length;r>i;i++)e=t[i],this.add(e)}return t.prototype.at=function(t){var e,i,r,n,o;if(t>=-this.size&&this.size>t){if(t=this._adjust(t),this.size>2*t)for(e=this.head,i=r=1;t>=r;i=r+=1)e=e.next;else for(e=this.tail,i=n=1,o=this.size-t-1;o>=n;i=n+=1)e=e.prev;return e}},t.prototype.add=function(t,e){var i,r,n,o,s;return null==e&&(e=this.size),e>=-this.size&&this.size>=e?(r={value:t},e=this._adjust(e),0===this.size?this.head=r:0===e?(n=[r,this.head,r],this.head.prev=n[0],r.next=n[1],this.head=n[2]):(i=this.at(e-1),o=[i.next,r,r,i],r.next=o[0],null!=(s=i.next)?s.prev=o[1]:void 0,i.next=o[2],r.prev=o[3]),e===this.size&&(this.tail=r),this.size++,t):void 0},t.prototype.removeAt=function(t){var e,i,r;return null==t&&(t=this.size-1),t>=-this.size&&this.size>t&&0!==this.size?(t=this._adjust(t),1===this.size?(i=this.head.value,this.head.value=this.tail.value=void 0):0===t?(i=this.head.value,this.head=this.head.next,this.head.prev=void 0):(e=this.at(t),i=e.value,e.prev.next=e.next,null!=(r=e.next)&&(r.prev=e.prev),t===this.size-1&&(this.tail=e.prev)),this.size--,i):void 0},t.prototype.remove=function(t){var e;if(null!=t){for(e=this.head;e&&e.value!==t;)e=e.next;if(e)return 1===this.size?this.head.value=this.tail.value=void 0:e===this.head?(this.head=this.head.next,this.head.prev=void 0):e===this.tail?(this.tail=this.tail.prev,this.tail.next=void 0):(e.prev.next=e.next,e.next.prev=e.prev),this.size--,t}},t.prototype.indexOf=function(t,e){var i,r;if(null==e&&(e=0),null==this.head.value&&!this.head.next||e>=this.size)return-1;for(e=Math.max(0,this._adjust(e)),i=this.at(e),r=e;i&&i.value!==t;)i=i.next,r++;return r===this.size?-1:r},t.prototype._adjust=function(t){return 0>t?this.size+t:t},t}(),e.exports=t}).call(this)},{}],4:[function(t,e){(function(){var t,i,r,n,o={}.hasOwnProperty;i="_mapId_",t=function(){function t(e){var i,r;this._content={},this._itemId=0,this._id=t._newMapId(),this.size=0;for(i in e)o.call(e,i)&&(r=e[i],this.set(i,r))}return t._mapIdTracker=0,t._newMapId=function(){return this._mapIdTracker++},t.prototype.hash=function(t,e){var o,s;return null==e&&(e=!1),s=r(t),n(t)?(o=i+this._id,e&&!t[o]&&(t[o]=this._itemId++),o+"_"+t[o]):s+"_"+t},t.prototype.set=function(t,e){return this.has(t)||this.size++,this._content[this.hash(t,!0)]=[e,t],e},t.prototype.get=function(t){var e;return null!=(e=this._content[this.hash(t)])?e[0]:void 0},t.prototype.has=function(t){return this.hash(t)in this._content},t.prototype["delete"]=function(t){var e;return e=this.hash(t),e in this._content?(delete this._content[e],n(t)&&delete t[i+this._id],this.size--,!0):!1},t.prototype.forEach=function(t){var e,i,r;r=this._content;for(e in r)i=r[e],t(i[1],i[0])},t}(),n=function(t){var e,i,n,o,s;for(e=["Boolean","Number","String","Undefined","Null","RegExp","Function"],n=r(t),o=0,s=e.length;s>o;o++)if(i=e[o],n===i)return!1;return!0},r=function(t){return Object.prototype.toString.apply(t).match(/\[object (.+)\]/)[1]},e.exports=t}).call(this)},{}],5:[function(t,e){(function(){var t;t=function(){function t(t){null==t&&(t=[]),this._content=t,this._dequeueIndex=0,this.size=this._content.length}return t.prototype.enqueue=function(t){return this.size++,this._content.push(t),t},t.prototype.dequeue=function(){var t;if(0!==this.size)return this.size--,t=this._content[this._dequeueIndex],this._dequeueIndex++,2*this._dequeueIndex>this._content.length&&(this._content=this._content.slice(this._dequeueIndex),this._dequeueIndex=0),t},t.prototype.peek=function(){return this._content[this._dequeueIndex]},t}(),e.exports=t}).call(this)},{}],6:[function(t,e){(function(){var t,i,r,n,o,s,u,h,a,_,l,d,f,p,c;i=0,r=1,n=2,u=3,o=1,t=2,s=function(){function e(t){var e,i,r;for(null==t&&(t=[]),this._root,this.size=0,i=0,r=t.length;r>i;i++)e=t[i],null!=e&&this.add(e)}return e.prototype.add=function(e){var s,l,d,f;if(null!=e){if(this.size++,d={value:e,_color:o},this._root){if(l=h(this._root,function(t){return e===t.value?i:t.value>e?t._left?r:(d._parent=t,t._left=d,u):t._right?n:(d._parent=t,t._right=d,u)}),null!=l)return}else this._root=d;for(s=d;;){if(s===this._root){s._color=t;break}if(s._parent._color===t)break;{if((null!=(f=c(s))?f._color:void 0)!==o){!_(s)&&_(s._parent)?(this._rotateLeft(s._parent),s=s._left):_(s)&&!_(s._parent)&&(this._rotateRight(s._parent),s=s._right),s._parent._color=t,a(s)._color=o,_(s)?this._rotateRight(a(s)):this._rotateLeft(a(s));break}s._parent._color=t,c(s)._color=t,a(s)._color=o,s=a(s)}}return e}},e.prototype.has=function(t){var e;return e=h(this._root,function(e){return t===e.value?i:e.value>t?r:n}),e?!0:!1},e.prototype.peekMin=function(){var t;return null!=(t=f(this._root))?t.value:void 0},e.prototype.peekMax=function(){var t;return null!=(t=d(this._root))?t.value:void 0},e.prototype.remove=function(t){var e;return(e=h(this._root,function(e){return t===e.value?i:e.value>t?r:n}))?(this._removeNode(this._root,e),this.size--,t):void 0},e.prototype.removeMin=function(){var t,e;return(t=f(this._root))?(e=t.value,this._removeNode(this._root,t),e):void 0},e.prototype.removeMax=function(){var t,e;return(t=d(this._root))?(e=t.value,this._removeNode(this._root,t),e):void 0},e.prototype._removeNode=function(e,i){var r,n,s,u,h,a,d,c,v,g;if(i._left&&i._right&&(n=f(i._right),i.value=n.value,i=n),n=i._left||i._right,n||(n={color:t,_right:void 0,_left:void 0,isLeaf:!0}),n._parent=i._parent,null!=(s=i._parent)&&(s[l(i)]=n),i._color===t)if(n._color===o)n._color=t,n._parent||(this._root=n);else for(;;){if(!n._parent){this._root=n.isLeaf?void 0:n;break}if(r=p(n),(null!=r?r._color:void 0)===o&&(n._parent._color=o,r._color=t,_(n)?this._rotateLeft(n._parent):this._rotateRight(n._parent)),r=p(n),n._parent._color!==t||r&&(r._color!==t||r._left&&r._left._color!==t||r._right&&r._right._color!==t)){if(!(n._parent._color!==o||r&&(r._color!==t||r._left&&(null!=(u=r._left)?u._color:void 0)!==t||r._right&&(null!=(h=r._right)?h._color:void 0)!==t))){null!=r&&(r._color=o),n._parent._color=t;break}if((null!=r?r._color:void 0)===t){!_(n)||r._right&&r._right._color!==t||(null!=(a=r._left)?a._color:void 0)!==o?_(n)||r._left&&r._left._color!==t||(null!=(c=r._right)?c._color:void 0)!==o||(r._color=o,null!=(v=r._right)&&(v._color=t),this._rotateLeft(r)):(r._color=o,null!=(d=r._left)&&(d._color=t),this._rotateRight(r));break}r=p(n),r._color=n._parent._color,_(n)?(r._right._color=t,this._rotateRight(n._parent)):(r._left._color=t,this._rotateLeft(n._parent))}else null!=r&&(r._color=o),n.isLeaf&&(n._parent[l(n)]=void 0),n=n._parent}return n.isLeaf?null!=(g=n._parent)?g[l(n)]=void 0:void 0:void 0},e.prototype._rotateLeft=function(t){var e,i;return null!=(e=t._parent)&&(e[l(t)]=t._right),t._right._parent=t._parent,t._parent=t._right,t._right=t._right._left,t._parent._left=t,null!=(i=t._right)&&(i._parent=t),null==t._parent._parent?this._root=t._parent:void 0},e.prototype._rotateRight=function(t){var e,i;return null!=(e=t._parent)&&(e[l(t)]=t._left),t._left._parent=t._parent,t._parent=t._left,t._left=t._left._right,t._parent._right=t,null!=(i=t._left)&&(i._parent=t),null==t._parent._parent?this._root=t._parent:void 0},e}(),_=function(t){return t===t._parent._left},l=function(t){return _(t)?"_left":"_right"},h=function(t,e){var o,s,h;for(s=t,h=void 0;s;){if(o=e(s),o===i){h=s;break}if(o===r)s=s._left;else if(o===n)s=s._right;else if(o===u)break}return h},f=function(t){return h(t,function(t){return t._left?r:i})},d=function(t){return h(t,function(t){return t._right?n:i})},a=function(t){var e;return null!=(e=t._parent)?e._parent:void 0},c=function(t){return a(t)?_(t._parent)?a(t)._right:a(t)._left:void 0},p=function(t){return _(t)?t._parent._right:t._parent._left},e.exports=s}).call(this)},{}],7:[function(t,e){(function(){var i,r,n,o;i=t("./Queue"),n="end",r=function(){function t(t){var e,i,r;for(null==t&&(t=[]),this._root={},this.size=0,i=0,r=t.length;r>i;i++)e=t[i],this.add(e)}return t.prototype.add=function(t){var e,i,r,o;if(null!=t){for(this.size++,e=this._root,r=0,o=t.length;o>r;r++)i=t[r],null==e[i]&&(e[i]={}),e=e[i];return e[n]=!0,t}},t.prototype.has=function(t){var e,i,r,o;if(null==t)return!1;for(e=this._root,r=0,o=t.length;o>r;r++){if(i=t[r],null==e[i])return!1;e=e[i]}return e[n]?!0:!1},t.prototype.longestPrefixOf=function(t){var e,i,r,n,o;if(null==t)return"";for(e=this._root,r="",n=0,o=t.length;o>n&&(i=t[n],null!=e[i]);n++)r+=i,e=e[i];return r},t.prototype.wordsWithPrefix=function(t){var e,r,o,s,u,h,a,_,l,d;if(null==t)return[];for(null!=t||(t=""),a=[],r=this._root,_=0,l=t.length;l>_;_++)if(o=t[_],r=r[o],null==r)return[];for(u=new i,u.enqueue([r,""]);0!==u.size;){d=u.dequeue(),s=d[0],e=d[1],s[n]&&a.push(t+e);for(o in s)h=s[o],u.enqueue([h,e+o])}return a},t.prototype.remove=function(t){var e,i,r,s,u,h,a,_;if(null!=t){for(e=this._root,s=[],u=0,a=t.length;a>u;u++){if(r=t[u],null==e[r])return;e=e[r],s.push([r,e])}if(e[n]){if(this.size--,delete e[n],o(e,1))return t;for(i=h=_=s.length-1;(1>=_?1>=h:h>=1)&&!o(s[i][1],1);i=1>=_?++h:--h)delete s[i-1][1][s[i][0]];return o(this._root[s[0][0]],1)||delete this._root[s[0][0]],t}}},t}(),o=function(t,e){var i,r;if(0===e)return!0;r=0;for(i in t)if(r++,r>=e)return!0;return!1},e.exports=r}).call(this)},{"./Queue":5}]},{},["EerzU6"]);