data-collection.js
Version:
Simple collection classes that can be used in JS/Node.
9 lines • 13.2 kB
JavaScript
!function(t){var e={};function r(n){if(e[n])return e[n].exports;var s=e[n]={i:n,l:!1,exports:{}};return t[n].call(s.exports,s,s.exports,r),s.l=!0,s.exports}r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)r.d(n,s,function(e){return t[e]}.bind(null,s));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=5)}([function(t,e){t.exports={Node:class{constructor(t){this.data=t}equals(t){let e=t.data;return null===e&&null===this.data||("object"==typeof e&&"object"==typeof this.data?JSON.stringify(e)===JSON.stringify(this.data):"function"==typeof e&&"function"==typeof this.data?e.toString()===this.data.toString():e===this.data)}}}},function(t,e){t.exports={NodedCollection:class{constructor(t=null){this.root=t,this[Symbol.iterator]=function*(){let t=[];this.__traverse(e=>{t.push(e.data)});for(let e=0;e<t.length;e++)yield t[e]}}__traverse(t,e){throw new Error("Collection needs to implement __traverse. Please raise an issue at https://github.com/Varun2604/JSCollection/issues")}}}},function(t,e,r){const{TreeNode:n}=r(3),{NodedCollection:s}=r(1);class o extends s{constructor(t=o.comparator,e=null){super(e),this.comparator=t,this.size=e?1:0}push(t){t instanceof n||(t=new n(t));let e=!1;return this.root?e=this.__insert(t,this.root):(this.root=t,e=!0),e&&this.size++,e}remove(t){return t instanceof n||(t=new n(t)),this.root?(this.size--,this.__delete(t,this.root),t.data):null}pop(){let t=o.leftMost(this.root);return this.__delete(new n(t),this.root),this.size--,t}shift(){let t=this.root.data;return this.root=this.__delete(this.root,this.root),this.size--,t}get(t){let e=void 0;if(t<this.size){let r=0;this.__traverseAndBreak(n=>r++!==t||(e=n.data,!1))}return e}contains(t){return t instanceof n||(t=new n(t)),this.indexOf(t)>-1}indexOf(t){t instanceof n||(t=new n(t));let e=-1,r=!1;return this.__traverseAndBreak(n=>(++e,o.comparator(t,n)<0||0===o.comparator(t,n)&&(r=!0,!1))),r?e:-1}lastIndexOf(t){t instanceof n||(t=new n(t));let e=-1;return this.__traverse(r=>{this.comparator(r,t)<=0||e++}),++e}forEach(t){this.__traverse(t)}join(t=","){let e="";return this.__traverse(r=>{e+=r.data+t}),e.substr(0,e.length-t.length)}filter(t){let e=[];return this.__traverse(r=>{t(r.data)&&e.push(r.data)}),e}map(t){let e=[];return this.__traverse(r=>{e.push(t(r.data))}),e}toArray(){let t=[];return this.__traverse(e=>{t.push(e.data)}),t}reduce(t,e){e||(e=0);let r=e,n=0;return this.__traverse(e=>{r=t(r,e.data,++n,this)}),r}every(t){let e=!0;return this.__traverseAndBreak(r=>e=t(r.data)),e}some(t){let e=!1;return this.__traverseAndBreak(r=>!(e=t(r.data))),e}find(t){let e=void 0;return this.__traverseAndBreak(r=>!t(r.data)||(e=r.data,!1)),e}concat(t){for(let e of t)this.push(e)}__traverse(t,e=this.root){null!==e&&(e.left&&this.__traverse(t,e.left),t(e),e.right&&this.__traverse(t,e.right))}__traverseAndBreak(t,e=this.root){if(null!==e&&(!e.left||!1!==this.__traverseAndBreak(t,e.left)))return!!t(e)&&void(!e.right||this.__traverseAndBreak(t,e.right))}__delete(t,e){if(0===this.comparator(t,e)){if(null===e)return null;if(null===e.right)return e.left;if(null===e.left)return e.right;e.data=o.leftMost(e.right),e.right=this.__delete(e,e.right)}else this.comparator(e,t)<0?e.left=this.__delete(t,e.left):e.right=this.__delete(t,e.right);return e}__insert(t,e){return this.comparator(e,t)<=0?null===e.left?(e.left=t,!0):this.__insert(t,e.left):null===e.right?(e.right=t,!0):this.__insert(t,e.right)}static comparator(t,e){return t instanceof n&&(t=t.data),e instanceof n&&(e=e.data),t>e?-1:+(t<e)}static leftMost(t){let e=t.data;for(;t.left;)e=t.left.data,t=t.left;return e}}t.exports={TreeList:o}},function(t,e,r){const{Node:n}=r(0);t.exports={TreeNode:class extends n{constructor(t,e=null,r=null){super(t),this.left=e,this.right=r}}}},function(t,e){var r={utf8:{stringToBytes:function(t){return r.bin.stringToBytes(unescape(encodeURIComponent(t)))},bytesToString:function(t){return decodeURIComponent(escape(r.bin.bytesToString(t)))}},bin:{stringToBytes:function(t){for(var e=[],r=0;r<t.length;r++)e.push(255&t.charCodeAt(r));return e},bytesToString:function(t){for(var e=[],r=0;r<t.length;r++)e.push(String.fromCharCode(t[r]));return e.join("")}}};t.exports=r},function(t,e,r){const{LinkedList:n}=r(6),{TreeList:s}=r(2),{TreeSet:o}=r(8),{Graph:i}=r(9);t.exports={LinkedList:n,TreeList:s,TreeSet:o,Graph:i}},function(t,e,r){const{LinkedNode:n}=r(7),{NodedCollection:s}=r(1);t.exports={LinkedList:class extends s{constructor(t=null){super(t),this.size=t?1:0}push(t){t instanceof n||(t=new n(t));let e=!1;return this.root?e=this.__insert(t):(this.root=t,e=!0),e&&this.size++,e}insertAt(t,e){e instanceof n||(e=new n(e));let r=!1;if(this.root)0===t?(e.next=this.root,this.root=e,r=!0):r=this.__insert(e,t);else{if(0!==t)return!1;this.root=e,r=!0}return r&&this.size++,r}get(t){if(null==t)return;let e=0,r=null;return this.__traverseAndBreak(n=>(r=n,e++!==t)),e-1===t&&r?r.data:void 0}contains(t){return t instanceof n||(t=new n(t)),-1!==this.indexOf(t)}indexOf(t){let e=-1;return t instanceof n||(t=new n(t)),this.__traverseAndBreak((r,n)=>(t.equals(r)&&(e=n),-1===e)),e}lastIndexOf(t){let e=-1;return t instanceof n||(t=new n(t)),this.__traverse((r,n)=>(t.equals(r)&&(e=n),-1===e)),e}remove(t){if(t instanceof n||(t=new n(t)),this.root.equals(t)){let t=this.root.data;return this.root=this.root.next,this.size--,t}let e=null,r=null;if(this.__traverseAndBreak((n,s)=>(r=e,!(e=n).equals(t))),null!==r&&null!==e&&e.equals(t)){let t=e.data;return r.next=e.next,this.size--,t}return null}removeAt(t=0){return this.__deleteAt(t)}pop(){return this.__deleteAt(this.size-1)}shift(){return this.__deleteAt(0)}peekFirst(){return this.root.data}peekLast(){let t=null;return this.__traverse(e=>t=e),t.data}concat(t=[]){for(let e of t)e instanceof n||(e=new n(e)),this.push(e)}toArray(){let t=[];return this.__traverse(e=>{t.push(e.data)}),t}__traverse(t,e=this.root){if(!e)return;let r=0;for(;e;)t(e,r++),e=e.next}__traverseAndBreak(t,e=this.root){if(!e)return;let r=0;for(;e&&t(e,r++);)e=e.next}__insert(t,e=this.size){let r=0,n=this.root;for(;n&&++r<e;)n=n.next;return r===e&&(t.next=n.next,n.next=t,!0)}__deleteAt(t){if(0===t){let t=this.root.data;return this.root=this.root.next,this.size--,t}let e=null,r=null,n=!0;if(this.__traverseAndBreak((s,o)=>(e=r,r=s,n=!(t===o))),e&&r&&!n){let t=r.data;return e.next=r.next,this.size--,t}return null}}}},function(t,e,r){const{Node:n}=r(0);t.exports={LinkedNode:class extends n{constructor(t,e=null){super(t),this.next=e}}}},function(t,e,r){const{TreeNode:n}=r(3),{TreeList:s}=r(2);t.exports={TreeSet:class extends s{push(t){t instanceof n||(t=new n(t));let e=!1;return this.root?e=this.__insert(t,this.root):(this.root=t,e=!0),e&&this.size++,e}lastIndexOf(t){return this.indexOf(t)}__insert(t,e){return this.comparator(e,t)<0?null===e.left?(e.left=t,!0):this.__insert(t,e.left):this.comparator(e,t)>0&&(null===e.right?(e.right=t,!0):this.__insert(t,e.right))}}}},function(t,e,r){const{GraphNode:n}=r(10),{NodedCollection:s}=r(1);t.exports={Graph:class extends s{constructor(){super(),this.graph_map={}}createEdge(t,e,r=!0){t instanceof n||(t=new n(t)),this.__createEdge__(t,e),r||(e instanceof n||(e=new n(e)),this.__createEdge__(e,t))}__createEdge__(t,e){this.graph_map.hasOwnProperty(t.hash())||(this.graph_map[t.hash()]=t),this.graph_map[t.hash()].addAdjacent(e)}}}},function(t,e,r){const{Node:n}=r(0),s=r(11);class o extends n{constructor(t,e=null){if(super(t),this.adjacents=[],e)for(let t of e)this.addAdjacent(t)}addAdjacent(t){t instanceof o||(t=new o(t)),this.adjacents.push(t)}hash(){return s(JSON.stringify(this.data))}}t.exports={GraphNode:o}},function(t,e,r){var n,s,o,i,a;n=r(12),s=r(4).utf8,o=r(13),i=r(4).bin,(a=function(t,e){t.constructor==String?t=e&&"binary"===e.encoding?i.stringToBytes(t):s.stringToBytes(t):o(t)?t=Array.prototype.slice.call(t,0):Array.isArray(t)||(t=t.toString());for(var r=n.bytesToWords(t),u=8*t.length,l=1732584193,h=-271733879,f=-1732584194,c=271733878,d=0;d<r.length;d++)r[d]=16711935&(r[d]<<8|r[d]>>>24)|4278255360&(r[d]<<24|r[d]>>>8);r[u>>>5]|=128<<u%32,r[14+(u+64>>>9<<4)]=u;var _=a._ff,p=a._gg,g=a._hh,v=a._ii;for(d=0;d<r.length;d+=16){var x=l,y=h,m=f,b=c;l=_(l,h,f,c,r[d+0],7,-680876936),c=_(c,l,h,f,r[d+1],12,-389564586),f=_(f,c,l,h,r[d+2],17,606105819),h=_(h,f,c,l,r[d+3],22,-1044525330),l=_(l,h,f,c,r[d+4],7,-176418897),c=_(c,l,h,f,r[d+5],12,1200080426),f=_(f,c,l,h,r[d+6],17,-1473231341),h=_(h,f,c,l,r[d+7],22,-45705983),l=_(l,h,f,c,r[d+8],7,1770035416),c=_(c,l,h,f,r[d+9],12,-1958414417),f=_(f,c,l,h,r[d+10],17,-42063),h=_(h,f,c,l,r[d+11],22,-1990404162),l=_(l,h,f,c,r[d+12],7,1804603682),c=_(c,l,h,f,r[d+13],12,-40341101),f=_(f,c,l,h,r[d+14],17,-1502002290),l=p(l,h=_(h,f,c,l,r[d+15],22,1236535329),f,c,r[d+1],5,-165796510),c=p(c,l,h,f,r[d+6],9,-1069501632),f=p(f,c,l,h,r[d+11],14,643717713),h=p(h,f,c,l,r[d+0],20,-373897302),l=p(l,h,f,c,r[d+5],5,-701558691),c=p(c,l,h,f,r[d+10],9,38016083),f=p(f,c,l,h,r[d+15],14,-660478335),h=p(h,f,c,l,r[d+4],20,-405537848),l=p(l,h,f,c,r[d+9],5,568446438),c=p(c,l,h,f,r[d+14],9,-1019803690),f=p(f,c,l,h,r[d+3],14,-187363961),h=p(h,f,c,l,r[d+8],20,1163531501),l=p(l,h,f,c,r[d+13],5,-1444681467),c=p(c,l,h,f,r[d+2],9,-51403784),f=p(f,c,l,h,r[d+7],14,1735328473),l=g(l,h=p(h,f,c,l,r[d+12],20,-1926607734),f,c,r[d+5],4,-378558),c=g(c,l,h,f,r[d+8],11,-2022574463),f=g(f,c,l,h,r[d+11],16,1839030562),h=g(h,f,c,l,r[d+14],23,-35309556),l=g(l,h,f,c,r[d+1],4,-1530992060),c=g(c,l,h,f,r[d+4],11,1272893353),f=g(f,c,l,h,r[d+7],16,-155497632),h=g(h,f,c,l,r[d+10],23,-1094730640),l=g(l,h,f,c,r[d+13],4,681279174),c=g(c,l,h,f,r[d+0],11,-358537222),f=g(f,c,l,h,r[d+3],16,-722521979),h=g(h,f,c,l,r[d+6],23,76029189),l=g(l,h,f,c,r[d+9],4,-640364487),c=g(c,l,h,f,r[d+12],11,-421815835),f=g(f,c,l,h,r[d+15],16,530742520),l=v(l,h=g(h,f,c,l,r[d+2],23,-995338651),f,c,r[d+0],6,-198630844),c=v(c,l,h,f,r[d+7],10,1126891415),f=v(f,c,l,h,r[d+14],15,-1416354905),h=v(h,f,c,l,r[d+5],21,-57434055),l=v(l,h,f,c,r[d+12],6,1700485571),c=v(c,l,h,f,r[d+3],10,-1894986606),f=v(f,c,l,h,r[d+10],15,-1051523),h=v(h,f,c,l,r[d+1],21,-2054922799),l=v(l,h,f,c,r[d+8],6,1873313359),c=v(c,l,h,f,r[d+15],10,-30611744),f=v(f,c,l,h,r[d+6],15,-1560198380),h=v(h,f,c,l,r[d+13],21,1309151649),l=v(l,h,f,c,r[d+4],6,-145523070),c=v(c,l,h,f,r[d+11],10,-1120210379),f=v(f,c,l,h,r[d+2],15,718787259),h=v(h,f,c,l,r[d+9],21,-343485551),l=l+x>>>0,h=h+y>>>0,f=f+m>>>0,c=c+b>>>0}return n.endian([l,h,f,c])})._ff=function(t,e,r,n,s,o,i){var a=t+(e&r|~e&n)+(s>>>0)+i;return(a<<o|a>>>32-o)+e},a._gg=function(t,e,r,n,s,o,i){var a=t+(e&n|r&~n)+(s>>>0)+i;return(a<<o|a>>>32-o)+e},a._hh=function(t,e,r,n,s,o,i){var a=t+(e^r^n)+(s>>>0)+i;return(a<<o|a>>>32-o)+e},a._ii=function(t,e,r,n,s,o,i){var a=t+(r^(e|~n))+(s>>>0)+i;return(a<<o|a>>>32-o)+e},a._blocksize=16,a._digestsize=16,t.exports=function(t,e){if(null==t)throw new Error("Illegal argument "+t);var r=n.wordsToBytes(a(t,e));return e&&e.asBytes?r:e&&e.asString?i.bytesToString(r):n.bytesToHex(r)}},function(t,e){var r,n;r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",n={rotl:function(t,e){return t<<e|t>>>32-e},rotr:function(t,e){return t<<32-e|t>>>e},endian:function(t){if(t.constructor==Number)return 16711935&n.rotl(t,8)|4278255360&n.rotl(t,24);for(var e=0;e<t.length;e++)t[e]=n.endian(t[e]);return t},randomBytes:function(t){for(var e=[];t>0;t--)e.push(Math.floor(256*Math.random()));return e},bytesToWords:function(t){for(var e=[],r=0,n=0;r<t.length;r++,n+=8)e[n>>>5]|=t[r]<<24-n%32;return e},wordsToBytes:function(t){for(var e=[],r=0;r<32*t.length;r+=8)e.push(t[r>>>5]>>>24-r%32&255);return e},bytesToHex:function(t){for(var e=[],r=0;r<t.length;r++)e.push((t[r]>>>4).toString(16)),e.push((15&t[r]).toString(16));return e.join("")},hexToBytes:function(t){for(var e=[],r=0;r<t.length;r+=2)e.push(parseInt(t.substr(r,2),16));return e},bytesToBase64:function(t){for(var e=[],n=0;n<t.length;n+=3)for(var s=t[n]<<16|t[n+1]<<8|t[n+2],o=0;o<4;o++)8*n+6*o<=8*t.length?e.push(r.charAt(s>>>6*(3-o)&63)):e.push("=");return e.join("")},base64ToBytes:function(t){t=t.replace(/[^A-Z0-9+\/]/gi,"");for(var e=[],n=0,s=0;n<t.length;s=++n%4)0!=s&&e.push((r.indexOf(t.charAt(n-1))&Math.pow(2,-2*s+8)-1)<<2*s|r.indexOf(t.charAt(n))>>>6-2*s);return e}},t.exports=n},function(t,e){function r(t){return!!t.constructor&&"function"==typeof t.constructor.isBuffer&&t.constructor.isBuffer(t)}
/*!
* Determine if an object is a Buffer
*
* @author Feross Aboukhadijeh <https://feross.org>
* @license MIT
*/
t.exports=function(t){return null!=t&&(r(t)||function(t){return"function"==typeof t.readFloatLE&&"function"==typeof t.slice&&r(t.slice(0,0))}(t)||!!t._isBuffer)}}]);
//# sourceMappingURL=data-collection.bundle.js.map