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 • 2.97 kB
JavaScript
'use strict';var c=Object.defineProperty;var u=(l,t)=>c(l,"name",{value:t,configurable:true});var o=class{constructor(t,e=null,i=null){this.val=t;this.prev=e;this.next=i;}static{u(this,"ListNode");}cleanup(){this.prev=null,this.next=null,this.val=null;}};var a=class l{static{u(this,"Deque");}#t;#e;#i;#n;constructor({initValues:t,factory:e}={}){this.#t=this.#e=null,this.#i=0,this.#n=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 o(t);if(this.isEmpty()){this.#t=this.#e=e,this.#i++;return}this.#t.prev=e,e.next=this.#t,this.#t=e,this.#i++;}pushBack(t){let e=new o(t);if(this.isEmpty()){this.#t=this.#e=e,this.#i++;return}e.prev=this.#e,this.#e.next=e,this.#e=e,this.#i++;}popFront(){if(this.isEmpty())return;let t=this.#t,e=t.val;return this.#i===1?(this.#t=this.#e=null,t.cleanup(),this.#i--,e):(this.#t=this.#t.next,this.#t.prev=null,t.cleanup(),this.#i--,e)}popBack(){if(this.isEmpty())return;let t=this.#e,e=t.val;if(this.#i===1)return this.#t=this.#e=null,t.cleanup(),this.#i--,e;let i=this.#e.prev;return i.next=null,this.#e=i,t.cleanup(),this.#i--,e}isEmpty(){return this.#i===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.#i=0;}emplaceFront(...t){if(typeof this.#n=="function"){this.pushFront(this.#n(...t));return}this.pushFront(t[0]);}emplaceBack(...t){if(typeof this.#n=="function"){this.pushBack(this.#n(...t));return}this.pushBack(t[0]);}static swap(t,e){if(!(t instanceof l&&e instanceof l))throw new TypeError("Both arguments must be instances of Deque");let i=t.#t,n=t.#e,r=t.#i;t.#t=e.#t,t.#e=e.#e,t.#i=e.#i,e.#t=i,e.#e=n,e.#i=r;}get length(){return this.#i}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.#i)return;let e=this.#t,i=0;for(;e!==null;){if(t===i)return e.val;e=e.next,i++;}}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 h=r;h<s;h++)this.pushBack(n[h]);return}if(typeof t=="number"&&arguments.length===2){let n=t,r=e;if(n<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let s=0;s<n;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 i=this.#t,n=0;for(;i!==null&&t.call(e,i.val,n,this)!==false;)i=i.next,n++;}};exports.Deque=a;