UNPKG

@data-structure/deque

Version:
3 lines (2 loc) 5.97 kB
import{NotImplementedError as t,IndexError as i,ValueError as n,TypeError as s}from"@failure-abstraction/error";function h(){}function r(){}function o(t,i){this.M=i,this.c=new Array(i),this.x=0,this.length=0,null!==t&&this.extend(t)}function u(t){null!==t&&this.extend(t)}function e(t){this.e=!0,this.v=0,null!==t&&this.extend(t)}function f(t){this.G=2,this.m=10,this.s=this.m,this.c=new Array(this.s),this.x=0,this.length=0,null!==t&&this.extend(t)}function c(t,i,h,r){return(o=null,u=null)=>{if(null===u)return new t(o);if(!Number.isInteger(u))throw new s(u);if(0===u)return new r(o);if(1===u)return new h(o);if(u>0)return new i(o,u);throw new n(u)}}h.prototype.len=function(){throw new t("len")},h.prototype.capacity=function(){throw new t("capacity")},h.prototype.empty=function(){return 0===this.len()},h.prototype[Symbol.iterator]=function(){return this.values()},h.prototype.values=function(){throw new t("values")},h.prototype.append=function(i){throw new t("append")},h.prototype.appendleft=function(i){throw new t("appendleft")},h.prototype.clear=function(){throw new t("clear")},h.prototype.copy=function(){throw new t("copy")},h.prototype.count=function(t){let i=0;for(const n of this)n===t&&++i;return i},h.prototype.extend=function(t){for(const i of t)this.append(i);return this},h.prototype.extendleft=function(t){for(const i of t)this.appendleft(i);return this},h.prototype.b=function(t){if(t<0||t>=this.len())throw new i(t)},h.prototype.w=function(i){throw new t("_where")},h.prototype.get=function(t){const[i,n]=this.w(t);return i[n]},h.prototype.set=function(t,i){const[n,s]=this.w(t);return n[s]=i,this},h.prototype.r=function*(t,i){for(let n=t;n<i;++n)yield[n,this.get(n)]},h.prototype.index=function(t,i=0,s=this.len()){for(const[n,h]of this.r(i,s))if(h===t)return n;throw new n("not found")},h.prototype.pop=function(){throw new t("pop")},h.prototype.popleft=function(){throw new t("popleft")},h.prototype.insert=function(t,i){this.b(t),this.append(i);let n=this.len()-1;for(;t<n;--n){const t=this.get(n);this.set(n,this.get(n-1)),this.set(n-1,t)}return this},h.prototype.delete=function(t){this.b(t);const i=this.len()-1;for(;t<i;++t)this.set(t,this.get(t+1));return this.pop(),this},h.prototype.remove=function(t){const i=this.index(t);return this.delete(i),this},h.prototype.reverse=function(){for(let t=0,i=this.len();t<--i;++t){const n=this.get(t),s=this.get(i);this.set(t,s),this.set(i,n)}return this},h.prototype.rotate=function(t){if(t>0)for(;t-- >0;)this.appendleft(this.pop());else if(t<0)for(;t++<0;)this.append(this.popleft());return this},(r.prototype=new h).values=function*(){let t=this.x;const i=t+this.length,n=Math.min(this.capacity(),i);for(;t<n;++t)yield this.c[t];const s=i%this.capacity();if(s<i)for(t=0;t<s;++t)yield this.c[t]},r.prototype.p=function(i,n){throw new t("_popindex")},r.prototype.pop=function(){const[t,i]=this.w(this.length-1);return this.p(t,i)},r.prototype.popleft=function(){const[t,i]=this.w(0);return++this.x,this.x%=this.capacity(),this.p(t,i)},(o.prototype=new r).len=function(){return this.length},o.prototype.capacity=function(){return this.M},o.prototype.append=function(t){return this.length===this.M?(this.c[this.x]=t,++this.x,this.x%=this.M):(this.c[(this.x+this.length)%this.M]=t,++this.length),this},o.prototype.appendleft=function(t){return--this.x,this.x+=this.M,this.x%=this.M,this.c[this.x]=t,this.length<this.M&&++this.length,this},o.prototype.clear=function(){return this.x=0,this.length=0,this.c=new Array(this.M),this},o.prototype.copy=function(){return new o(this,this.M)},o.prototype.w=function(t){return this.b(t),[this.c,(this.x+t)%this.M]},o.prototype.p=function(t,i){const n=t[i];return t[i]=0,--this.length,n},(u.prototype=new h).len=function(){return 0},u.prototype.capacity=function(){return 0},u.prototype.values=function(){return{next:()=>({done:!0})}},u.prototype.append=function(t){return this},u.prototype.appendleft=function(t){return this},u.prototype.clear=function(){return this},u.prototype.copy=function(){return new u(this)},u.prototype.w=function(t){throw new i(t)},u.prototype.pop=u.prototype.popleft=function(){throw new i("pop / popleft")},(e.prototype=new h).len=function(){return this.e?0:1},e.prototype.capacity=function(){return 1},e.prototype.values=function*(){this.e||(yield this.v)},e.prototype.append=e.prototype.appendleft=function(t){return this.e=!1,this.v=t,this},e.prototype.clear=function(){return this.e=!0,this.v=0,this},e.prototype.copy=function(){return new e(this)},e.prototype.pop=e.prototype.popleft=function(){if(this.e)throw new i("pop / popleft");const t=this.v;return this.e=!0,this.v=0,t},e.prototype.get=function(t){if(this.e||0!==t)throw new i(t);return this.v},e.prototype.set=function(t,n){if(this.e||0!==t)throw new i(t);return this.v=n,this},(f.prototype=new r).y=function(t){const i=this.length;for(let n=0;n<i;++n)t[n]=this.get(n)},f.prototype.a=function(t){const i=new Array(t);this.y(i),this.c=i,this.x=0,this.s=t},f.prototype.z=function(){const t=Math.max(this.m,this.length*this.G);t*this.G>=this.s||this.a(t)},f.prototype.g=function(t){t<=this.s||this.a(t*this.G)},f.prototype.len=function(){return this.length},f.prototype.capacity=function(){return this.s},f.prototype.append=function(t){return this.g(this.length+1),this.c[(this.x+this.length)%this.s]=t,++this.length,this},f.prototype.appendleft=function(t){return this.g(this.length+1),--this.x,this.x+=this.s,this.x%=this.s,this.c[this.x]=t,++this.length,this},f.prototype.clear=function(){return this.s=this.m,this.c=new Array(this.s),this.x=0,this.length=0,this},f.prototype.copy=function(){return new f(this)},f.prototype.w=function(t){return this.b(t),[this.c,(this.x+t)%this.s]},f.prototype.p=function(t,i){const n=t[i];return t[i]=0,--this.length,this.z(),n};const w=c(f,o,e,u);export{r as ArbitrarySizeDeque,o as BoundedDeque,h as Deque,u as EmptyDeque,e as SingleElementDeque,f as UnboundedDeque,c as _deque,w as deque}; //# sourceMappingURL=index.modern.js.map