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.03 kB
import {a as a$1}from'./chunk-HAYEU4HA.js';import {a}from'./chunk-EI7MMDWY.js';var p=class s{static{a(this,"Stack");}#n;#e;#t;#r;constructor({initValues:e,factory:t}={}){this.#n=this.#e=null,this.#t=0,this.#r=t,this.#i(e);}*[Symbol.iterator](){let e=this.#n;for(;e!==null;)yield e.val,e=e.next;}*rbegin(){let e=this.#e;for(;e!==null;)yield e.val,e=e.prev;}begin(){return this[Symbol.iterator]()}#i(e){if(e!==void 0){if(!Array.isArray(e))throw new TypeError("Expected an array to initialize the stack.");if(e.length!==0)for(let t of e)this.push(t);}}push(e){let t=new a$1(e);if(this.isEmpty()){this.#n=this.#e=t,this.#t++;return}t.prev=this.#e,this.#e.next=t,this.#e=t,this.#t++;}pop(){if(this.isEmpty())return;let e=this.#e,t=e.val;if(this.#t===1)return this.#n=this.#e=null,e.cleanup(),this.#t--,t;let n=this.#e.prev;return n.next=null,this.#e=n,e.cleanup(),this.#t--,t}emplace(...e){if(typeof this.#r!="function")throw new TypeError("Please provide a factory function to use emplace.");this.push(this.#r(...e));}isEmpty(){return this.#t===0}clear(){let e=this.#n;for(;e!==null;){let t=e.next;e.cleanup(),e=t;}this.#n=this.#e=null,this.#t=0;}toArray(){return [...this]}assign(e,t,n){if(Array.isArray(e)){let r=e,i=typeof t=="number"?t:0,o=typeof n=="number"?n:r.length;if(i<0||o>r.length||i>o)throw new RangeError("Invalid array slice range");this.clear();for(let u=i;u<o;u++)this.push(r[u]);return}if(typeof e=="number"&&arguments.length===2){let r=e,i=t;if(i===void 0)throw new TypeError("Value must be provided when assigning repeated elements.");if(r<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let o=0;o<r;o++)this.push(i);return}throw new TypeError("Invalid arguments passed to assign()")}forEach(e,t){if(typeof e!="function")throw new TypeError("Callback must be a function");let n=this.#n,r=0;for(;n!==null&&e.call(t,n.val,r,this)!==false;)n=n.next,r++;}peek(){return this.top}clone(e=t=>{if(typeof structuredClone=="function")return structuredClone(t);throw new Error("structuredClone is not supported in this environment. Please provide a custom deepCloneFn.")}){if(typeof e!="function")throw new TypeError("deepCloneFn must be a function");let t=new s({factory:this.#r});for(let n of this)t.push(e(n));return t}get length(){return this.#t}get top(){if(!this.isEmpty())return this.#e.val}set top(e){if(this.isEmpty())throw new Error("Cannot set top of an empty stack.");this.#e.val=e;}static equals(e,t,n=(r,i)=>r===i){if(!(e instanceof s&&t instanceof s))throw new TypeError("Input value must be an instance of stack");if(e.#r!==t.#r||e.#t!==t.#t)return false;function r(i,o){return i===null||o===null?i===o:n(i.val,o.val)?r(i.next,o.next):false}return a(r,"backtrack"),r(e.#n,t.#n)}static swap(e,t){if(!(e instanceof s&&t instanceof s))throw new TypeError("Input value must be an instance of stack");if(e.#r!==t.#r)throw new TypeError("Both stacks must have the same factory function");let n=e.#n,r=e.#e,i=e.#t;e.#n=t.#n,e.#e=t.#e,e.#t=t.#t,t.#n=n,t.#e=r,t.#t=i;}};export{p as a};