UNPKG

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 3.32 kB
'use strict';var p=Object.defineProperty;var l=(o,t)=>p(o,"name",{value:t,configurable:true});var u=class{constructor(t,n=null,i=null){this.val=t;this.prev=n;this.next=i;}static{l(this,"ListNode");}cleanup(){this.prev=null,this.next=null,this.val=null;}};var c=class o{static{l(this,"Queue");}#t;#n;#e;#r;constructor({initValues:t,factory:n}={}){this.#t=this.#n=null,this.#e=0,this.#r=n,this.#i(t);}*[Symbol.iterator](){let t=this.#t;for(;t!==null;)yield t.val,t=t.next;}*rbegin(){let t=this.#n;for(;t!==null;)yield t.val,t=t.prev;}begin(){return this[Symbol.iterator]()}#i(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 n of t)this.push(n);}}push(t){let n=new u(t);if(this.isEmpty()){this.#t=this.#n=n,this.#e++;return}n.prev=this.#n,this.#n.next=n,this.#n=n,this.#e++;}pop(){if(this.isEmpty())return;let t=this.#t,n=t.val;return this.#e===1?(this.#t=this.#n=null,t.cleanup(),this.#e--,n):(this.#t=this.#t.next,this.#t.prev=null,t.cleanup(),this.#e--,n)}emplace(...t){if(typeof this.#r!="function")throw new TypeError("Please provide a factory function to use emplace.");this.push(this.#r(...t));}isEmpty(){return this.#e===0}clear(){let t=this.#t;for(;t!==null;){let n=t.next;t.cleanup(),t=n;}this.#t=this.#n=null,this.#e=0;}toArray(){return [...this]}assign(t,n,i){if(Array.isArray(t)){let e=t,r=typeof n=="number"?n:0,s=typeof i=="number"?i:e.length;if(r<0||s>e.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let a=r;a<s;a++)this.push(e[a]);return}if(typeof t=="number"&&arguments.length===2){let e=t,r=n;if(r===void 0)throw new TypeError("Value must be provided when assigning repeated elements.");if(e<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<e;s++)this.push(r);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(t,n){if(typeof t!="function")throw new TypeError("Callback must be a function");let i=this.#t,e=0;for(;i!==null&&t.call(n,i.val,e,this)!==false;)i=i.next,e++;}peekFirst(){if(!this.isEmpty())return this.#t.val}peekLast(){if(!this.isEmpty())return this.#n.val}clone(t=n=>{if(typeof structuredClone=="function")return structuredClone(n);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 n=new o({factory:this.#r});for(let i of this)n.push(t(i));return n}get length(){return this.#e}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.#n.val}set back(t){this.isEmpty()||(this.#n.val=t);}static equals(t,n,i=(e,r)=>e===r){if(!(t instanceof o&&n instanceof o))throw new TypeError("Input value must be an instance of queue");if(t.#r!==n.#r||t.#e!==n.#e)return false;function e(r,s){return r===null||s===null?r===s:i(r.val,s.val)?e(r.next,s.next):false}return l(e,"backtrack"),e(t.#t,n.#t)}static swap(t,n){if(!(t instanceof o&&n instanceof o))throw new TypeError("Input value must be an instance of queue");if(t.#r!==n.#r)throw new TypeError("Both stacks must have the same factory function");let i=t.#t,e=t.#n,r=t.#e;t.#t=n.#t,t.#n=n.#n,t.#e=n.#e,n.#t=i,n.#n=e,n.#e=r;}};exports.Queue=c;