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.14 kB
import {a as a$1}from'./chunk-HAYEU4HA.js';import {a}from'./chunk-EI7MMDWY.js';var c=class o{static{a(this,"Queue");}#t;#e;#n;#r;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#n=0,this.#r=e,this.#i(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]()}#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 e of t)this.push(e);}}push(t){let e=new a$1(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.#r!="function")throw new TypeError("Please provide a factory function to use emplace.");this.push(this.#r(...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,i){if(Array.isArray(t)){let n=t,r=typeof e=="number"?e:0,s=typeof i=="number"?i:n.length;if(r<0||s>n.length||r>s)throw new RangeError("Invalid array slice range");this.clear();for(let l=r;l<s;l++)this.push(n[l]);return}if(typeof t=="number"&&arguments.length===2){let n=t,r=e;if(r===void 0)throw new TypeError("Value must be provided when assigning repeated elements.");if(n<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<n;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 i=this.#t,n=0;for(;i!==null&&t.call(e,i.val,n,this)!==false;)i=i.next,n++;}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.#r});for(let i of this)e.push(t(i));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,i=(n,r)=>n===r){if(!(t instanceof o&&e instanceof o))throw new TypeError("Input value must be an instance of queue");if(t.#r!==e.#r||t.#n!==e.#n)return false;function n(r,s){return r===null||s===null?r===s:i(r.val,s.val)?n(r.next,s.next):false}return a(n,"backtrack"),n(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.#r!==e.#r)throw new TypeError("Both stacks must have the same factory function");let i=t.#t,n=t.#e,r=t.#n;t.#t=e.#t,t.#e=e.#e,t.#n=e.#n,e.#t=i,e.#e=n,e.#n=r;}};export{c as a};