stl-kit
Version:
A modern JavaScript & TypeScript standard template library (STL) for data structures and algorithms. Includes high-performance implementations of Stack, Queue, Deque, Linked List, Vector, Set, Map, Tree, Heap, and Graph — inspired by C++ STL. Designed for
1 lines • 15.9 kB
JavaScript
'use strict';var d=Object.defineProperty;var u=(o,t)=>d(o,"name",{value:t,configurable:true});var h=class{constructor(t,e=null,n=null){this.val=t;this.prev=e;this.next=n;}static{u(this,"ListNode");}cleanup(){this.prev=null,this.next=null,this.val=null;}};var a=class o{static{u(this,"LinkedList");}#t;#e;#n;#i;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#n=0,this.#i=e,this.#r(t);}*[Symbol.iterator](){let t=this.#t;for(;t!==null;)yield t.val,t=t.next;}*rbegin(){let t=this.#e;for(;t!==null;)yield t.val,t=t.prev;}begin(){return this[Symbol.iterator]()}#r(t){if(t!==void 0){if(!Array.isArray(t))throw new TypeError("Expected an array to initialize the list.");if(t.length!==0)for(let e of t)this.pushBack(e);}}#s(t){let{prev:e,next:n,cleanup:i,val:r}=t;return e!==null?e.next=n:this.#t=n,n!==null?n.prev=e:this.#e=e,i(),this.#n--,r}pushFront(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}this.#t.prev=e,e.next=this.#t,this.#t=e,this.#n++;}pushBack(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}e.prev=this.#e,this.#e.next=e,this.#e=e,this.#n++;}popFront(){if(this.isEmpty())return;let t=this.#t,e=t.val;return this.#n===1?(this.#t=this.#e=null,t.cleanup(),this.#n--,e):(this.#t=this.#t.next,this.#t.prev=null,t.cleanup(),this.#n--,e)}popBack(){if(this.isEmpty())return;let t=this.#e,e=t.val;if(this.#n===1)return this.#t=this.#e=null,t.cleanup(),this.#n--,e;let n=this.#e.prev;return n.next=null,this.#e=n,t.cleanup(),this.#n--,e}insertAt(t,e){if(e<0||e>this.#n)throw new RangeError("Invalid index to insert");if(e===0){this.pushFront(t);return}if(e===this.#n){this.pushBack(t);return}let n=this.#t,i=0;for(;i<e-1&&n!=null;)n=n.next,i++;let r=new h(t),s=n.next;r.prev=n,r.next=s,n.next=r,s.prev=r,this.#n++;}eraseAt(t){if(this.isEmpty())return;if(t<0||t>=this.#n)throw new RangeError("Invalid index to erase");if(t===0)return this.popFront();if(t===this.#n-1)return this.popBack();let e=this.#t,n=0;for(;e!=null&&n<t;)e=e.next,n++;if(e===null)throw new Error("Index out of bounds after traversal");return this.#s(e)}emplaceFront(...t){if(typeof this.#i=="function"){this.pushFront(this.#i(...t));return}this.pushFront(t[0]);}emplaceBack(...t){if(typeof this.#i=="function"){this.pushBack(this.#i(...t));return}this.pushBack(t[0]);}emplaceAt(t,...e){if(typeof this.#i=="function"){this.insertAt(this.#i(...e),t);return}this.insertAt(e[0],t);}isEmpty(){return this.#n===0}clear(){if(this.isEmpty())return;let t=this.#t;for(;t!=null;){let e=t.next;t.cleanup(),t=e;}this.#t=this.#e=null,this.#n=0;}toArray(){return [...this]}assign(t,e,n){if(Array.isArray(t)){let i=t,r=typeof e=="number"?e:0,s=typeof n=="number"?n:i.length;if(r<0||s>i.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.pushBack(i[l]);return}if(typeof t=="number"&&arguments.length===2){let i=t,r=e;if(i<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<i;s++)this.pushBack(r);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(t,e){if(typeof t!="function")throw new TypeError("Callback must be a function");let n=this.#t,i=0;for(;n!==null&&t.call(e,n.val,i,this)!==false;)n=n.next,i++;}reverse(){if(this.length<=1)return;let t=null,e=this.#t,n=null;for(;e!=null;)n=e.next,e.next=t,e.prev=n,t=e,e=n;this.#e=this.#t,this.#t=t;}remove(t,e=(n,i)=>n===i){if(this.isEmpty())return 0;if(typeof e!="function")throw new TypeError("compareFn must be a function");let n=0,i=this.#t;for(;i!==null;){let r=i.next;if(e(i.val,t)){let s=i.prev;s!==null&&r!==null?(s.next=r,r.prev=s):s!==null?(s.next=null,this.#e=s):r!==null?(r.prev=null,this.#t=r):this.#t=this.#e=null,i.cleanup(),n++;}i=r;}return this.#n-=n,n}get length(){return this.#n}get front(){if(!this.isEmpty())return this.#t.val}set front(t){this.isEmpty()||(this.#t.val=t);}get back(){if(!this.isEmpty())return this.#e.val}set back(t){this.isEmpty()||(this.#e.val=t);}static swap(t,e){if(!(t instanceof o&&e instanceof o))throw new TypeError("Both arguments must be instances of LinkedList");let n=t.#t,i=t.#e,r=t.#n;t.#t=e.#t,t.#e=e.#e,t.#n=e.#n,e.#t=n,e.#e=i,e.#n=r;}static merge(t,e,n=(i,r)=>i<r?-1:i>r?1:0){if(!(t instanceof o&&e instanceof o))throw new TypeError("Both arguments must be instances of LinkedList");if(typeof n!="function")throw new TypeError("compareFn must be a function");if(t===e)throw new Error("Cannot merge a list with itself");let i=t.#t,r=e.#t;for(;i!==null&&r!==null;)if(n(r.val,i.val)<0){let s=r.next;s&&(s.prev=null),e.#t=s,e.#t||(e.#e=null),e.#n--;let l=i.prev;r.prev=l,r.next=i,i.prev=r,l?l.next=r:t.#t=r,t.#n++,r=s;}else i=i.next;r!==null&&(t.#e?(t.#e.next=r,r.prev=t.#e):t.#t=r,t.#e=e.#e,t.#n+=e.#n,e.#t=e.#e=null,e.#n=0);}static buildNodes(t){if(!Array.isArray(t))throw new TypeError("Expected an array of values");let e=null,n=null;for(let i of t){let r=new h(i);e===null?e=n=r:(n.next=r,r.prev=n,n=r);}return {head:e,tail:n}}};var c=class o{static{u(this,"Stack");}#t;#e;#n;#i;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#n=0,this.#i=e,this.#r(t);}*[Symbol.iterator](){let t=this.#t;for(;t!==null;)yield t.val,t=t.next;}*rbegin(){let t=this.#e;for(;t!==null;)yield t.val,t=t.prev;}begin(){return this[Symbol.iterator]()}#r(t){if(t!==void 0){if(!Array.isArray(t))throw new TypeError("Expected an array to initialize the stack.");if(t.length!==0)for(let e of t)this.push(e);}}push(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}e.prev=this.#e,this.#e.next=e,this.#e=e,this.#n++;}pop(){if(this.isEmpty())return;let t=this.#e,e=t.val;if(this.#n===1)return this.#t=this.#e=null,t.cleanup(),this.#n--,e;let n=this.#e.prev;return n.next=null,this.#e=n,t.cleanup(),this.#n--,e}emplace(...t){if(typeof this.#i!="function")throw new TypeError("Please provide a factory function to use emplace.");this.push(this.#i(...t));}isEmpty(){return this.#n===0}clear(){let t=this.#t;for(;t!==null;){let e=t.next;t.cleanup(),t=e;}this.#t=this.#e=null,this.#n=0;}toArray(){return [...this]}assign(t,e,n){if(Array.isArray(t)){let i=t,r=typeof e=="number"?e:0,s=typeof n=="number"?n:i.length;if(r<0||s>i.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.push(i[l]);return}if(typeof t=="number"&&arguments.length===2){let i=t,r=e;if(r===void 0)throw new TypeError("Value must be provided when assigning repeated elements.");if(i<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<i;s++)this.push(r);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(t,e){if(typeof t!="function")throw new TypeError("Callback must be a function");let n=this.#t,i=0;for(;n!==null&&t.call(e,n.val,i,this)!==false;)n=n.next,i++;}peek(){return this.top}clone(t=e=>{if(typeof structuredClone=="function")return structuredClone(e);throw new Error("structuredClone is not supported in this environment. Please provide a custom deepCloneFn.")}){if(typeof t!="function")throw new TypeError("deepCloneFn must be a function");let e=new o({factory:this.#i});for(let n of this)e.push(t(n));return e}get length(){return this.#n}get top(){if(!this.isEmpty())return this.#e.val}set top(t){if(this.isEmpty())throw new Error("Cannot set top of an empty stack.");this.#e.val=t;}static equals(t,e,n=(i,r)=>i===r){if(!(t instanceof o&&e instanceof o))throw new TypeError("Input value must be an instance of stack");if(t.#i!==e.#i||t.#n!==e.#n)return false;function i(r,s){return r===null||s===null?r===s:n(r.val,s.val)?i(r.next,s.next):false}return u(i,"backtrack"),i(t.#t,e.#t)}static swap(t,e){if(!(t instanceof o&&e instanceof o))throw new TypeError("Input value must be an instance of stack");if(t.#i!==e.#i)throw new TypeError("Both stacks must have the same factory function");let n=t.#t,i=t.#e,r=t.#n;t.#t=e.#t,t.#e=e.#e,t.#n=e.#n,e.#t=n,e.#e=i,e.#n=r;}};var p=class o{static{u(this,"Queue");}#t;#e;#n;#i;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#n=0,this.#i=e,this.#r(t);}*[Symbol.iterator](){let t=this.#t;for(;t!==null;)yield t.val,t=t.next;}*rbegin(){let t=this.#e;for(;t!==null;)yield t.val,t=t.prev;}begin(){return this[Symbol.iterator]()}#r(t){if(t!==void 0){if(!Array.isArray(t))throw new TypeError("Expected an array to initialize the stack.");if(t.length!==0)for(let e of t)this.push(e);}}push(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}e.prev=this.#e,this.#e.next=e,this.#e=e,this.#n++;}pop(){if(this.isEmpty())return;let t=this.#t,e=t.val;return this.#n===1?(this.#t=this.#e=null,t.cleanup(),this.#n--,e):(this.#t=this.#t.next,this.#t.prev=null,t.cleanup(),this.#n--,e)}emplace(...t){if(typeof this.#i!="function")throw new TypeError("Please provide a factory function to use emplace.");this.push(this.#i(...t));}isEmpty(){return this.#n===0}clear(){let t=this.#t;for(;t!==null;){let e=t.next;t.cleanup(),t=e;}this.#t=this.#e=null,this.#n=0;}toArray(){return [...this]}assign(t,e,n){if(Array.isArray(t)){let i=t,r=typeof e=="number"?e:0,s=typeof n=="number"?n:i.length;if(r<0||s>i.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.push(i[l]);return}if(typeof t=="number"&&arguments.length===2){let i=t,r=e;if(r===void 0)throw new TypeError("Value must be provided when assigning repeated elements.");if(i<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<i;s++)this.push(r);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(t,e){if(typeof t!="function")throw new TypeError("Callback must be a function");let n=this.#t,i=0;for(;n!==null&&t.call(e,n.val,i,this)!==false;)n=n.next,i++;}peekFirst(){if(!this.isEmpty())return this.#t.val}peekLast(){if(!this.isEmpty())return this.#e.val}clone(t=e=>{if(typeof structuredClone=="function")return structuredClone(e);throw new Error("structuredClone is not supported in this environment. Please provide a custom deepCloneFn.")}){if(typeof t!="function")throw new TypeError("deepCloneFn must be a function");let e=new o({factory:this.#i});for(let n of this)e.push(t(n));return e}get length(){return this.#n}get front(){if(!this.isEmpty())return this.#t.val}set front(t){this.isEmpty()||(this.#t.val=t);}get back(){if(!this.isEmpty())return this.#e.val}set back(t){this.isEmpty()||(this.#e.val=t);}static equals(t,e,n=(i,r)=>i===r){if(!(t instanceof o&&e instanceof o))throw new TypeError("Input value must be an instance of queue");if(t.#i!==e.#i||t.#n!==e.#n)return false;function i(r,s){return r===null||s===null?r===s:n(r.val,s.val)?i(r.next,s.next):false}return u(i,"backtrack"),i(t.#t,e.#t)}static swap(t,e){if(!(t instanceof o&&e instanceof o))throw new TypeError("Input value must be an instance of queue");if(t.#i!==e.#i)throw new TypeError("Both stacks must have the same factory function");let n=t.#t,i=t.#e,r=t.#n;t.#t=e.#t,t.#e=e.#e,t.#n=e.#n,e.#t=n,e.#e=i,e.#n=r;}};var f=class o{static{u(this,"Deque");}#t;#e;#n;#i;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#n=0,this.#i=e,this.#r(t);}*[Symbol.iterator](){let t=this.#t;for(;t!==null;)yield t.val,t=t.next;}*rbegin(){let t=this.#e;for(;t!==null;)yield t.val,t=t.prev;}begin(){return this[Symbol.iterator]()}#r(t){if(t!==void 0){if(!Array.isArray(t))throw new TypeError("Expected an array to initialize the deque.");if(t.length!==0)for(let e of t)this.pushBack(e);}}pushFront(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}this.#t.prev=e,e.next=this.#t,this.#t=e,this.#n++;}pushBack(t){let e=new h(t);if(this.isEmpty()){this.#t=this.#e=e,this.#n++;return}e.prev=this.#e,this.#e.next=e,this.#e=e,this.#n++;}popFront(){if(this.isEmpty())return;let t=this.#t,e=t.val;return this.#n===1?(this.#t=this.#e=null,t.cleanup(),this.#n--,e):(this.#t=this.#t.next,this.#t.prev=null,t.cleanup(),this.#n--,e)}popBack(){if(this.isEmpty())return;let t=this.#e,e=t.val;if(this.#n===1)return this.#t=this.#e=null,t.cleanup(),this.#n--,e;let n=this.#e.prev;return n.next=null,this.#e=n,t.cleanup(),this.#n--,e}isEmpty(){return this.#n===0}clear(){if(this.isEmpty())return;let t=this.#t;for(;t!=null;){let e=t.next;t.cleanup(),t=e;}this.#t=this.#e=null,this.#n=0;}emplaceFront(...t){if(typeof this.#i=="function"){this.pushFront(this.#i(...t));return}this.pushFront(t[0]);}emplaceBack(...t){if(typeof this.#i=="function"){this.pushBack(this.#i(...t));return}this.pushBack(t[0]);}static swap(t,e){if(!(t instanceof o&&e instanceof o))throw new TypeError("Both arguments must be instances of Deque");let n=t.#t,i=t.#e,r=t.#n;t.#t=e.#t,t.#e=e.#e,t.#n=e.#n,e.#t=n,e.#e=i,e.#n=r;}get length(){return this.#n}get front(){if(!this.isEmpty())return this.#t.val}set front(t){this.isEmpty()||(this.#t.val=t);}get back(){if(!this.isEmpty())return this.#e.val}set back(t){this.isEmpty()||(this.#e.val=t);}at(t){if(this.isEmpty()||t<0||t>=this.#n)return;let e=this.#t,n=0;for(;e!==null;){if(t===n)return e.val;e=e.next,n++;}}toArray(){return [...this]}assign(t,e,n){if(Array.isArray(t)){let i=t,r=typeof e=="number"?e:0,s=typeof n=="number"?n:i.length;if(r<0||s>i.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.pushBack(i[l]);return}if(typeof t=="number"&&arguments.length===2){let i=t,r=e;if(i<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<i;s++)this.pushBack(r);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(t,e){if(typeof t!="function")throw new TypeError("Callback must be a function");let n=this.#t,i=0;for(;n!==null&&t.call(e,n.val,i,this)!==false;)n=n.next,i++;}};var b=class o extends Array{static{u(this,"Vector");}#t;constructor({initValues:t=[],factory:e}={}){super(...t),Object.setPrototypeOf(this,o.prototype),this.#t=e;}*rbegin(){for(let t=this.length-1;t>=0;t--)yield this[t];}begin(){return this[Symbol.iterator]()}isEmpty(){return this.length===0}clear(){this.length=0;}insertAt(t,e){if(arguments.length<2)return this.length;if(typeof t!="number"||t<0||t>this.length)throw new RangeError("Index out of range");return this.splice(t,0,e),this.length}eraseAt(t){if(typeof t!="number"||t<0||t>=this.length)throw new RangeError("Index out of range");let[e]=this.splice(t,1);return e}resize(t){if(!(t>=0&&t<2**32))throw new RangeError("Failed to set the size on 'Vector': Invalid vector length");this.length=t;}pushFront(t){return arguments.length===0?this.length:this.unshift(t)}pushBack(t){return arguments.length===0?this.length:this.push(t)}popFront(){return this.shift()}popBack(){return this.pop()}emplaceAt(t,...e){return arguments.length<2?this.length:typeof this.#t=="function"?this.insertAt(t,this.#t(...e)):this.insertAt(t,e[0])}emplaceFront(...t){return arguments.length<1?this.length:typeof this.#t=="function"?this.pushFront(this.#t(...t)):this.pushFront(t[0])}emplaceBack(...t){return arguments.length<1?this.length:typeof this.#t=="function"?this.pushBack(this.#t(...t)):this.pushBack(t[0])}assign(t,e,n){if(Array.isArray(t)){let i=t,r=typeof e=="number"?e:0,s=typeof n=="number"?n:i.length;if(r<0||s>i.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.pushBack(i[l]);return}if(typeof t=="number"&&arguments.length===2){let i=t,r=e;if(i<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<i;s++)this.pushBack(r);return}throw new TypeError("Invalid arguments passed to assign()")}equals(t,e=(n,i)=>n===i){return this.length!==t.length?false:this.every((n,i)=>e(n,t[i]))}get front(){return this[0]}set front(t){if(this.isEmpty())throw new RangeError("Cannot set front on empty vector");this[0]=t;}get back(){return this.at(-1)}set back(t){if(this.isEmpty())throw new RangeError("Cannot set back on empty vector");this[this.length-1]=t;}};exports.Deque=f;exports.LinkedList=a;exports.ListNode=h;exports.Queue=p;exports.Stack=c;exports.Vector=b;