sangja
Version:
JavaScript data structures library
2 lines • 15.7 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("sangja",[],e):"object"==typeof exports?exports.sangja=e():t.sangja=e()}(this,function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=3)}([function(t,e){t.exports={isIterable:function(t){return null!=t&&"function"==typeof t[Symbol.iterator]},defaultKey:t=>t,defaultCompare:(t,e)=>t<e?-1:t>e?1:0,mergeOptions:function(t={},e={}){if(!t)return e;const i={};return Object.keys(e).forEach(r=>{i[r]=t[r]||e[r]}),i}}},function(t,e,i){const r=i(0),s=i(2);class n{constructor(t=[]){if(!r.isIterable(t))throw TypeError();this._linkedList=new s(t)}enqueue(t){this._linkedList.addLast(t)}enqueueAll(t){[...t].forEach(t=>this._linkedList.addLast(t))}dequeue(){if(0!==this._linkedList.size())return this._linkedList.popFirst()}peek(){if(0!==this._linkedList.size())return this._linkedList.getFirst()}size(){return this._linkedList.size()}isEmpty(){return this._linkedList.isEmpty()}clear(){this._linkedList=new s}forEach(t){this._linkedList.forEach(t)}map(t){const e=new n;return e._linkedList=this._linkedList.map(t),e}flatMap(t){const e=new n;return e._linkedList=this._linkedList.flatMap(t),e}filter(t){const e=new n;return e._linkedList=this._linkedList.filter(t),e}reversed(){const t=new n;return t._linkedList=this._linkedList.reversed(),t}some(t){return this._linkedList.some(t)}every(t){return this._linkedList.every(t)}includes(t){return this._linkedList.includes(t)}*[Symbol.iterator](){yield*this._linkedList}}t.exports=n},function(t,e,i){const r=i(0);class s{constructor(t=[]){if(this._linkedList={size:0,front:null,end:null},!r.isIterable(t))throw TypeError();[...t].forEach(t=>this.addLast(t))}_getNode(t){if("number"!=typeof t||t<0||t>=this._linkedList.size)return;if(t<this._linkedList.size/2){let e=this._linkedList.front;for(let i=0;i<t;i+=1)e=e.next;return e}let e=this._linkedList.end;for(let i=this._linkedList.size-1;i>t;i-=1)e=e.prev;return e}add(t,e){if(void 0!==e){if("number"!=typeof t||t<0||t>this._linkedList.size)throw RangeError();if(0===t){const t={prev:null,next:this._linkedList.front,value:e};this._linkedList.front?this._linkedList.front.prev=t:this._linkedList.end=t,this._linkedList.front=t}else if(t===this._linkedList.size){const t={prev:this._linkedList.end,next:null,value:e};this._linkedList.end&&(this._linkedList.end.next=t),this._linkedList.end=t}else{const i=this._getNode(t),r={prev:i.prev,next:i,value:e};r.prev.next=r,r.next.prev=r}this._linkedList.size+=1}else this.addLast(t)}addAll(t,e){if(void 0===e)return void this.addAllLast(t);const i=[...e];for(let e=i.length-1;e>=0;e-=1)this.add(t,i[e])}addFirst(t){this.add(0,t)}addAllFirst(t){const e=[...t];for(let t=e.length-1;t>=0;t-=1)this.add(0,e[t])}addLast(t){this.add(this.size(),t)}addAllLast(t){const e=[...t];for(let t=0;t<e.length;t+=1)this.addLast(e[t])}pop(t){if(void 0===t)return this.popLast();if("number"!=typeof t||t<0||t>=this._linkedList.size)return;const e=this._getNode(t);return e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,e.value}popFirst(){return this.pop(0)}popLast(){return this.pop(this.size()-1)}removeAt(t){if(!("number"!=typeof t||t<0||t>=this._linkedList.size))return this.pop(t)}remove(t){let e=this._linkedList.front;for(;e&&e.value!==t;)e=e.next;return!!e&&(e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,!0)}removeAll(t){let e=this._linkedList.front,i=0;for(;e;)e.value===t&&(e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,i+=1),e=e.next;return i}removeFirst(t){return void 0===t?this.popFirst():this.remove(t)}removeLast(t){let e=this._linkedList.end;for(;e&&e.value!==t;)e=e.prev;return!!e&&(e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,!0)}removeMatch(t){if("function"!=typeof t)throw TypeError();let e=this._linkedList.front;for(;e&&!t(e.value);)e=e.next;if(e)return e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,e.value}removeMatchAll(t){if("function"!=typeof t)throw TypeError();let e=this._linkedList.front;const i=[];for(;e;)t(e.value)&&(e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,i.push(e.value)),e=e.next;return i}removeMatchFirst(t){return this.removeMatch(t)}removeMatchLast(t){if("function"!=typeof t)throw TypeError();let e=this._linkedList.end;for(;e&&!t(e.value);)e=e.prev;if(e)return e.prev?e.prev.next=e.next:this._linkedList.front=e.next,e.next?e.next.prev=e.prev:this._linkedList.end=e.prev,this._linkedList.size-=1,e.value}clear(){this._linkedList={size:0,front:null,end:null}}get(t){if("number"!=typeof t||t<0||t>=this._linkedList.size)return;return this._getNode(t).value}getFirst(){return this.get(0)}getLast(){return this.get(this.size()-1)}set(t,e){if("number"!=typeof t||t<0||t>=this._linkedList.size)throw RangeError();const i=this._getNode(t),r=i.value;return i.value=e,r}find(t){let e=this._linkedList.front;for(;e&&!t(e.value);)e=e.next;if(e)return e.value}size(){return this._linkedList.size}isEmpty(){return 0===this._linkedList.size}forEach(t){let e=this._linkedList.front;for(;e;)t(e.value),e=e.next}map(t){const e=new s;return this.forEach(i=>e.addLast(t(i))),e}flatMap(t){const e=new s;return this.forEach(i=>e.addAllLast([...t(i)])),e}filter(t){const e=new s;return this.forEach(i=>{t(i)&&e.addLast(i)}),e}reversed(){const t=new s;return this.forEach(e=>t.addFirst(e)),t}some(t){if(0===this.size())return!1;let e=this._linkedList.front;for(;e;){if(t(e.value))return!0;e=e.next}return!1}every(t){let e=this._linkedList.front;for(;e;){if(!t(e.value))return!1;e=e.next}return!0}includes(t){let e=this._linkedList.front;for(;e;){if(e.value===t)return!0;e=e.next}return!1}*[Symbol.iterator](){let t=this._linkedList.front;for(;t;)yield t.value,t=t.next}}t.exports=s},function(t,e,i){const r=i(0),s=i(4),n=i(5),o=i(1),l=i(2),h=i(6),u=i(7);t.exports={Optional:s,Stack:n,Queue:o,LinkedList:l,Heap:h,BinarySearchTree:u,isIterable:r.isIterable,defaultKey:r.defaultKey,defaultCompare:r.defaultCompare}},function(t,e){class i{constructor(t){this.value=t}get(){return this.value}getOrElse(t){return void 0===this.value?t:this.value}size(){return void 0===this.value?0:1}isEmpty(){return void 0===this.value}forEach(t){void 0!==this.value&&t(this.value)}map(t){if(void 0===this.value)return new i;try{return new i(t(this.value))}catch(t){return new i}}flatMap(t){if(void 0===this.value)return new i;try{return new i(...t(this.value))}catch(t){return new i}}filter(t){return void 0===this.value?new i:t(this.value)?this:new i}some(t){return void 0!==this.value&&Boolean(t(this.value))}every(t){return void 0===this.value||Boolean(t(this.value))}includes(t){return void 0!==this.value&&this.value===t}*[Symbol.iterator](){void 0!==this.value&&(yield this.value)}}t.exports=i},function(t,e,i){const r=i(0);class s{constructor(t=[]){if(!r.isIterable(t))throw TypeError();this._stack=[...t]}push(t){this._stack.push(t)}pushAll(t){[...t].forEach(t=>this._stack.push(t))}pop(){if(0!==this._stack.length)return this._stack.pop()}top(){if(0!==this._stack.length)return this._stack[this._stack.length-1]}clear(){this._stack=[]}size(){return this._stack.length}isEmpty(){return 0===this._stack.length}forEach(t){for(let e=this._stack.length-1;e>=0;e-=1)t(this._stack[e])}map(t){const e=new s;return e._stack=this._stack.map(t),e}flatMap(t){const e=new s;return this._stack.map(t).forEach(t=>e.pushAll(t)),e}filter(t){const e=new s;return e._stack=this._stack.filter(t),e}reversed(){return new s(this)}some(t){return this._stack.some(t)}every(t){return this._stack.every(t)}includes(t){return this._stack.includes(t)}*[Symbol.iterator](){for(let t=this._stack.length-1;t>=0;t-=1)yield this._stack[t]}}t.exports=s},function(t,e,i){const r=i(0);class s{constructor(t={},e={}){let i=null,s=null,n=null,o=null;r.isIterable(t)?(i=t,({key:s,compare:n,reverse:o}=e)):(i=[],({key:s,compare:n,reverse:o}=t)),this._heap=[0],this._options={key:s||r.defaultKey,compare:n||r.defaultCompare,reverse:o||!1},this._key=this._options.key,this._options.reverse?this._compare=((t,e)=>this._options.compare(this._key(e),this._key(t))):this._compare=((t,e)=>this._options.compare(this._key(t),this._key(e))),[...i].forEach(t=>this.add(t))}add(t){this._heap.push(t);let e=this._heap.length-1,i=Math.floor(e/2);for(;e>1&&this._compare(this._heap[i],this._heap[e])<0;){const t=this._heap[i];this._heap[i]=this._heap[e],this._heap[e]=t,e=i,i=Math.floor(e/2)}}addAll(t){[...t].forEach(t=>this.add(t))}pop(){if(1===this._heap.length)return;if(2===this._heap.length)return this._heap.pop();const t=this._heap[1];this._heap[1]=this._heap.pop();let e=1;for(;e<this._heap.length;){const t=2*e,i=2*e+1;if(i<this._heap.length&&this._compare(this._heap[e],this._heap[i])<0){let r=null;r=this._compare(this._heap[t],this._heap[i])<0?i:t;const s=this._heap[e];this._heap[e]=this._heap[r],this._heap[r]=s,e=r}else{if(!(t<this._heap.length&&this._compare(this._heap[e],this._heap[t])<0))break;{const i=this._heap[e];this._heap[e]=this._heap[t],this._heap[t]=i,e=t}}}return t}peek(){if(1!==this._heap.length)return this._heap[1]}find(t){for(let e=1;e<this._heap.length;e+=1)if(t(this._heap[e]))return this._heap[e]}size(){return this._heap.length-1}isEmpty(){return 1===this._heap.length}clear(){this._heap=[0]}forEach(t){const e=new s(this._heap.slice(1),this._options);for(;!e.isEmpty();)t(e.pop())}map(t,e){const i=new s(r.mergeOptions(e,this._options));return this.forEach(e=>i.add(t(e))),i}flatMap(t,e){const i=new s(r.mergeOptions(e,this._options));return this.forEach(e=>i.addAll([...t(e)])),i}filter(t,e){const i=new s(r.mergeOptions(e,this._options));return this.forEach(e=>{t(e)&&i.add(e)}),i}reversed(){return new s(this,r.mergeOptions({reverse:!this._options.reverse},this._options))}some(t){for(let e=1;e<this._heap.length;e+=1)if(t(this._heap[e]))return!0;return!1}every(t){for(let e=1;e<this._heap.length;e+=1)if(!t(this._heap[e]))return!1;return!0}includes(t){for(let e=1;e<this._heap.length;e+=1)if(this._heap[e]===t)return!0;return!1}*[Symbol.iterator](){const t=new s(this._heap.slice(1),this._options);for(;!t.isEmpty();)yield t.pop()}breadthFirst(t){if(!t){const t=this;return function*(){for(let e=1;e<t._heap.length;e+=1)yield t._heap[e]}()}for(let e=1;e<this._heap.length;e+=1)t(this._heap[e])}}t.exports=s},function(t,e,i){const r=i(0),s=i(1);t.exports=class{constructor(t={},e={}){let i=null,s=null,n=null,o=null;r.isIterable(t)?(i=t,({key:s,compare:n,reverse:o}=e)):(i=[],({key:s,compare:n,reverse:o}=t)),this._root=null,this._options={key:s||r.defaultKey,compare:n||r.defaultCompare,reverse:o||!1},this._key=this._options.key,this._options.reverse?this._compare=((t,e)=>this._options.compare(this._key(e),this._key(t))):this._compare=((t,e)=>this._options.compare(this._key(t),this._key(e))),[...i].forEach(t=>this.add(t))}add(t){if(null==this._root)return void(this._root={left:null,right:null,children:0,value:t});let e=null,i=this._root,r=null;for(;i;)e=i,i.children+=1,this._compare(t,i.value)<=0?(i=i.left,r=0):(i=i.right,r=1);i={left:null,right:null,children:0,value:t},0===r?e.left=i:e.right=i}addAll(t){[...t].forEach(t=>this.add(t))}pop(){if(null===this._root)return;let t=null,e=this._root;for(;e.left;)e.children-=1,t=e,e=e.left;return null!==t?t.left=e.right:this._root=e.right,e.value}remove(t){if(null===this._root||!this.includes(t))return;let e=null,i=this._root;for(;i;){const r=this._compare(this._key(t),this._key(i.value));if(0===r)break;r<0?(i.children-=1,e=i,i=i.left):(i.children-=1,e=i,i=i.right)}if(null!==i){if(i.left&&i.right){let t=i,e=i.left;for(;e.right;)t.children-=1,t=e,e=e.right;t!==i?t.right=e.left:t.left=e.left;const r=i.value;return i.value=e.value,r}return i.left?(i===this._root?this._root=i.left:e.left===i?e.left=i.left:e.right=i.left,i.value):i.right?(i===this._root?this._root=i.right:e.left===i?e.left=i.right:e.right=i.right,i.value):(i===this._root?this._root=null:e.left===i?e.left=null:e.right=null,i.value)}}removeMatch(t){if("function"!=typeof t)throw TypeError();if(this.isEmpty())return;const e=function e(i){return t(i.value)?i.value:i.left&&e(i.left)||i.right&&e(i.right)}(this._root);return this.remove(e),e}peek(){if(null===this._root)return;let t=this._root;for(;t;)t.left&&(t=t.left);return t.value}find(t){const e=[this._root];for(;e.length>0;){const i=e.pop();if(i){if(t(i.value))return i.value;e.push(i.left),e.push(i.right)}}}size(){return null===this._root?0:this._root.children+1}isEmpty(){return null===this._root}clear(){this._root=null}forEach(t){!function e(i){i&&(t(i.value),e(i.left),e(i.right))}(this._root)}map(t,e){const i=new this.constructor(r.mergeOptions(e,this._options));return this.forEach(e=>i.add(t(e))),i}flatMap(t,e){const i=new this.constructor(r.mergeOptions(e,this._options));return this.forEach(e=>i.addAll([...t(e)])),i}filter(t,e){const i=new this.constructor(r.mergeOptions(e,this._options));return this.forEach(e=>{t(e)&&i.add(e)}),i}reversed(){return new this.constructor(this,r.mergeOptions({reverse:!this._options.reverse},this._options))}some(t){return Boolean(this._root)&&function e(i){return t(i.value)||Boolean(i.left&&e(i.left))||Boolean(i.right&&e(i.right))}(this._root)}every(t){return!this._root||function e(i){return t(i.value)&&Boolean(!i.left||e(i.left))&&Boolean(!i.right||e(i.right))}(this._root)}includes(t){let e=this._root;for(;e;){const i=this._compare(this._key(t),this._key(e.value));if(0===i)return!0;e=i<0?e.left:e.right}return!1}*[Symbol.iterator](){yield*this.inorder()}inorder(t){if(!t)return function*t(e){null!==e&&(yield*t(e.left),yield e.value,yield*t(e.right))}(this._root);!function e(i){null!==i&&(e(i.left),t(i.value),e(i.right))}(this._root)}preorder(t){if(!t)return function*t(e){null!==e&&(yield e.value,yield*t(e.left),yield*t(e.right))}(this._root);!function e(i){null!==i&&(t(i.value),e(i.left),e(i.right))}(this._root)}postorder(t){if(!t)return function*t(e){null!==e&&(yield*t(e.left),yield*t(e.right),yield e.value)}(this._root);!function e(i){null!==i&&(e(i.left),e(i.right),t(i.value))}(this._root)}breadthFirst(t){if(!t)return function*(t){const e=new s;for(e.enqueue(t);e.size()>0;){const t=e.dequeue();null!=t&&(yield t.value,e.enqueue(t.left),e.enqueue(t.right))}}(this._root);!function(e){const i=new s;for(i.enqueue(e);i.size()>0;){const e=i.dequeue();null!=e&&(t(e.value),i.enqueue(e.left),i.enqueue(e.right))}}(this._root)}}}])});
//# sourceMappingURL=sangja.min.map