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