gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 8.04 kB
JavaScript
/*! Copyright 2023-2024 the gnablib contributors MPL-1.1 */
import{NotEnoughSpaceError as e}from"../error/NotEnoughSpaceError.js";import{nextPow2 as t}from"./xtBit.js";import{parseDec as r}from"./number/xtUint.js";const n=Symbol.for("nodejs.util.inspect.custom"),s=1048576,i=new ArrayBuffer(0);function o(e){return e instanceof SharedArrayBuffer||e instanceof ArrayBuffer?e:"object"==typeof e&&null!=e&&"buffer"in e&&e.buffer instanceof ArrayBuffer?e.buffer:void 0}function a(e,t){return e?(e<0&&(e+=t),e):0}function d(e,t,r,n){if(r=a(r,e.length),n=null!=n?n:e.length,r<0||n<=0)return;const s=r+n;e.fill(t,r,s)}class l{constructor(e){this._view=e}get byteLength(){return this._view.byteLength}get byteOffset(){return this._view.byteOffset}get BYTES_PER_ELEMENT(){return this._view.BYTES_PER_ELEMENT}get length(){return this._view.length}get capacity(){return this._view.length}at(e){if(e<0&&(e+=this._view.length),!(e<0||e>=this._view.length))return this._view[e]}clone(e,t){return this._view.slice(e,t)}entries(){return this._view.entries()}every(e,t){const r=e.bind(t);return this._view.every((function(e,t){return r(e,t)}))}filter(e,t){const r=e.bind(t);return this._view.filter((function(e,t){return r(e,t)}))}find(e,t){const r=e.bind(t);return this._view.find((function(e,t){return r(e,t)}))}findIndex(e,t){const r=e.bind(t);return this._view.findIndex((function(e,t){return r(e,t)}))}forEach(e,t){const r=e.bind(t);this._view.forEach((function(e,t){r(e,t)}))}includes(e,t){return this._view.includes(e,t)}indexOf(e,t){return this._view.indexOf(e,t)}join(e){return this._view.join(null!=e?e:",")}keys(){return this._view.keys()}lastIndexOf(e,t){return this._view.lastIndexOf(e,null!=t?t:-1)}map(e,t){const r=e.bind(t);return this._view.map((function(e,t){return r(e,t)}))}some(e,t){const r=e.bind(t);return this._view.some((function(e,t){return r(e,t)}))}values(){return this._view.values()}toString(){return this._view.join(",")}toJSON(){return Array.from(this._view.values())}[n](){return this._view}[Symbol.iterator](){return this._view[Symbol.iterator]()}}export class ReadonlyTyped extends l{constructor(e,t,r,n){let s;if(s=t instanceof e?t.buffer:o(t),null==s)throw new TypeError("Expecting ArrayBufferLike|IBufferer");return super(new e(s,r=(null!=r?r:0)*e.BYTES_PER_ELEMENT,n=null!=n?n:(s.byteLength-r)/e.BYTES_PER_ELEMENT)),this.base=e,new Proxy(this,{get:(e,t)=>t in e?e[t]:e.at(t),set:()=>!1})}readonlySpan(e,t){return new ReadonlyTyped(this.base,this._view.buffer,e,t)}get[Symbol.toStringTag](){return"Readonly<"+this._view.constructor.name+">"}}export class FixedTyped extends l{constructor(e,t,n,s){let i;if(i=t instanceof e?t.buffer:o(t),null==i)throw new TypeError("Expecting ArrayBufferLike|IBufferer");return super(new e(i,n=(null!=n?n:0)*e.BYTES_PER_ELEMENT,s=null!=s?s:(i.byteLength-n)/e.BYTES_PER_ELEMENT)),this.base=e,new Proxy(this,{get:(e,t)=>t in e?e[t]:e.at(t),set(e,t,n){var s;const i=r(null!==(s=t)&&void 0!==s?s:"");return null!=i&&(e.setEl(i,n),!0)}})}setEl(e,t){this._view[e]=t}fill(e,t,r){return d(this._view,e,t,r),this}readonlySpan(e,t){return new ReadonlyTyped(this.base,this._view.buffer,e,t)}reverse(){return this._view.reverse(),this}set(e,t){this._view.set(e,t)}sort(e){return this._view.sort(e),this}span(e,t){return new FixedTyped(this.base,this._view.buffer,e,t)}get[Symbol.toStringTag](){return"Fixed<"+this._view.constructor.name+">"}}export class ScalingTyped extends l{constructor(n,o,a){let d;if(0===(o=null!=o?o:0))d=i;else{if((a=null!=a?a:0)>0){if(a<o)throw new e("Capacity",o,a);a*=n.BYTES_PER_ELEMENT}else a=(l=o*n.BYTES_PER_ELEMENT)<=4?4:l<s?0===l?4:t(l):(l+s)/s;d=new ArrayBuffer(a)}var l;return super(new n(d,0,o)),this.base=n,this._sizeLocked=!1,this._data=d,new Proxy(this,{get:(e,t)=>t in e?e[t]:e.at(t),set(e,t,n){var s;if(t in e)return e[t]=n,!0;const i=r(null!==(s=t)&&void 0!==s?s:"");return null!=i&&(e.setEl(i,n),!0)}})}_resize(e,t){if(this._sizeLocked)throw new Error("This array is locked");if(e<this._data.byteLength)this._data=this._data.slice(0,e),this._view=new this.base(this._data,0,t);else{const r=new ArrayBuffer(e),n=new this.base(r,0,t);n.set(this._view),this._data=r,this._view=n}}assertSpace(e){if(this._view.length>e)return;const t=e*this._view.BYTES_PER_ELEMENT;this._data.byteLength<=t?this._resize(t,e):this._view=new this.base(this._data,0,e)}get capacity(){return this._data.byteLength/this.base.BYTES_PER_ELEMENT}set capacity(e){const t=e*this.base.BYTES_PER_ELEMENT;t!==this._data.byteLength&&this._resize(t,this._view.length<e?this._view.length:e)}fill(e,t,r){if((t=a(t,this._view.length))<0||t>=this._view.length)return this;const n=t+(r=null!=r?r:this._view.length-t);return this.assertSpace(n),d(this._view,e,t,r),this}readonlySpan(e,t){this._sizeLocked=!0;return new ReadonlyTyped(this.base,this._view.buffer,e,t)}reverse(){return this._view.reverse(),this}set(e,t){(t=null!=t?t:0)<0||(this.assertSpace(t+e.length),this._view.set(e,t))}setEl(e,t){this.assertSpace(e+1),this._view[e]=t}sort(e){return this._view.sort(e),this}span(e,t){this._sizeLocked=!0;return new FixedTyped(this.base,this._data,e,t)}get[Symbol.toStringTag](){return"Scaling<"+this._view.constructor.name+">"}}export class ReadonlyUint8Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Uint8Array,e,t,r)}}export class ReadonlyUint16Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Uint16Array,e,t,r)}}export class ReadonlyUint32Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Uint32Array,e,t,r)}}export class ReadonlyInt8Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Int8Array,e,t,r)}}export class ReadonlyInt16Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Int16Array,e,t,r)}}export class ReadonlyInt32Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Int32Array,e,t,r)}}export class ReadonlyFloat32Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Float32Array,e,t,r)}}export class ReadonlyFloat64Array extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Float64Array,e,t,r)}}export class ReadonlyUint8ClampedArray extends ReadonlyTyped{constructor(e,t=void 0,r=void 0){super(Uint8ClampedArray,e,t,r)}}export class FixedUint8Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Uint8Array,e,t,r)}}export class FixedUint16Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Uint16Array,e,t,r)}}export class FixedUint32Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Uint32Array,e,t,r)}}export class FixedInt8Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Int8Array,e,t,r)}}export class FixedInt16Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Int16Array,e,t,r)}}export class FixedInt32Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Int32Array,e,t,r)}}export class FixedFloat32Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Float32Array,e,t,r)}}export class FixedFloat64Array extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Float64Array,e,t,r)}}export class FixedUint8ClampedArray extends FixedTyped{constructor(e,t=void 0,r=void 0){super(Uint8ClampedArray,e,t,r)}}export class ScalingUint8Array extends ScalingTyped{constructor(e,t){super(Uint8Array,e,t)}}export class ScalingUint16Array extends ScalingTyped{constructor(e,t){super(Uint16Array,e,t)}}export class ScalingUint32Array extends ScalingTyped{constructor(e,t){super(Uint32Array,e,t)}}export class ScalingInt8Array extends ScalingTyped{constructor(e,t){super(Int8Array,e,t)}}export class ScalingInt16Array extends ScalingTyped{constructor(e,t){super(Int16Array,e,t)}}export class ScalingInt32Array extends ScalingTyped{constructor(e,t){super(Int32Array,e,t)}}export class ScalingFloat32Array extends ScalingTyped{constructor(e,t){super(Float32Array,e,t)}}export class ScalingFloat64Array extends ScalingTyped{constructor(e,t){super(Float64Array,e,t)}}export class ScalingUint8ClampedArray extends ScalingTyped{constructor(e,t){super(Uint8ClampedArray,e,t)}}