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.23 kB
JavaScript
import {a}from'./chunk-EI7MMDWY.js';var h=class a$1 extends Array{static{a(this,"Vector");}#t;constructor({initValues:t=[],factory:e}={}){super(...t),Object.setPrototypeOf(this,a$1.prototype),this.#t=e;}*rbegin(){for(let t=this.length-1;t>=0;t--)yield this[t];}begin(){return this[Symbol.iterator]()}isEmpty(){return this.length===0}clear(){this.length=0;}insertAt(t,e){if(arguments.length<2)return this.length;if(typeof t!="number"||t<0||t>this.length)throw new RangeError("Index out of range");return this.splice(t,0,e),this.length}eraseAt(t){if(typeof t!="number"||t<0||t>=this.length)throw new RangeError("Index out of range");let[e]=this.splice(t,1);return e}resize(t){if(!(t>=0&&t<2**32))throw new RangeError("Failed to set the size on 'Vector': Invalid vector length");this.length=t;}pushFront(t){return arguments.length===0?this.length:this.unshift(t)}pushBack(t){return arguments.length===0?this.length:this.push(t)}popFront(){return this.shift()}popBack(){return this.pop()}emplaceAt(t,...e){return arguments.length<2?this.length:typeof this.#t=="function"?this.insertAt(t,this.#t(...e)):this.insertAt(t,e[0])}emplaceFront(...t){return arguments.length<1?this.length:typeof this.#t=="function"?this.pushFront(this.#t(...t)):this.pushFront(t[0])}emplaceBack(...t){return arguments.length<1?this.length:typeof this.#t=="function"?this.pushBack(this.#t(...t)):this.pushBack(t[0])}assign(t,e,r){if(Array.isArray(t)){let n=t,s=typeof e=="number"?e:0,i=typeof r=="number"?r:n.length;if(s<0||i>n.length||s>i)throw new RangeError("Invalid array slice range");this.clear();for(let u=s;u<i;u++)this.pushBack(n[u]);return}if(typeof t=="number"&&arguments.length===2){let n=t,s=e;if(n<0)throw new RangeError("Count must be a non-negative integer");this.clear();for(let i=0;i<n;i++)this.pushBack(s);return}throw new TypeError("Invalid arguments passed to assign()")}equals(t,e=(r,n)=>r===n){return this.length!==t.length?false:this.every((r,n)=>e(r,t[n]))}get front(){return this[0]}set front(t){if(this.isEmpty())throw new RangeError("Cannot set front on empty vector");this[0]=t;}get back(){return this.at(-1)}set back(t){if(this.isEmpty())throw new RangeError("Cannot set back on empty vector");this[this.length-1]=t;}};export{h as a};