@data-structure/deque
Version:
Python's deque data structure for JavaScript
3 lines (2 loc) • 8.1 kB
JavaScript
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports,require("@failure-abstraction/error")):"function"==typeof define&&define.amd?define(["exports","@failure-abstraction/error"],n):n((t||self).deque={},t.error)}(this,function(t,n){function i(t,n){(null==n||n>t.length)&&(n=t.length);for(var i=0,r=new Array(n);i<n;i++)r[i]=t[i];return r}function r(t,n){var r="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(r)return(r=r.call(t)).next.bind(r);if(Array.isArray(t)||(r=function(t,n){if(t){if("string"==typeof t)return i(t,n);var r=Object.prototype.toString.call(t).slice(8,-1);return"Object"===r&&t.constructor&&(r=t.constructor.name),"Map"===r||"Set"===r?Array.from(t):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?i(t,n):void 0}}(t))||n&&t&&"number"==typeof t.length){r&&(t=r);var e=0;return function(){return e>=t.length?{done:!0}:{done:!1,value:t[e++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function e(){}function s(){}function h(t,n){this.M=n,this.c=new Array(n),this.x=0,this.length=0,null!==t&&this.extend(t)}function u(t){null!==t&&this.extend(t)}function o(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,r,e){return function(s,h){if(void 0===s&&(s=null),void 0===h&&(h=null),null===h)return new t(s);if(!Number.isInteger(h))throw new n.TypeError(h);if(0===h)return new e(s);if(1===h)return new r(s);if(h>0)return new i(s,h);throw new n.ValueError(h)}}e.prototype.len=function(){throw new n.NotImplementedError("len")},e.prototype.capacity=function(){throw new n.NotImplementedError("capacity")},e.prototype.empty=function(){return 0===this.len()},e.prototype[Symbol.iterator]=function(){return this.values()},e.prototype.values=function(){throw new n.NotImplementedError("values")},e.prototype.append=function(t){throw new n.NotImplementedError("append")},e.prototype.appendleft=function(t){throw new n.NotImplementedError("appendleft")},e.prototype.clear=function(){throw new n.NotImplementedError("clear")},e.prototype.copy=function(){throw new n.NotImplementedError("copy")},e.prototype.count=function(t){for(var n,i=0,e=r(this);!(n=e()).done;)n.value===t&&++i;return i},e.prototype.extend=function(t){for(var n,i=r(t);!(n=i()).done;)this.append(n.value);return this},e.prototype.extendleft=function(t){for(var n,i=r(t);!(n=i()).done;)this.appendleft(n.value);return this},e.prototype.b=function(t){if(t<0||t>=this.len())throw new n.IndexError(t)},e.prototype.w=function(t){throw new n.NotImplementedError("_where")},e.prototype.get=function(t){var n=this.w(t);return n[0][n[1]]},e.prototype.set=function(t,n){var i=this.w(t);return i[0][i[1]]=n,this},e.prototype.r=regeneratorRuntime.mark(function t(n,i){var r;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:r=n;case 1:if(!(r<i)){t.next=7;break}return t.next=4,[r,this.get(r)];case 4:++r,t.next=1;break;case 7:case"end":return t.stop()}},t,this)}),e.prototype.index=function(t,i,e){void 0===i&&(i=0),void 0===e&&(e=this.len());for(var s,h=r(this.r(i,e));!(s=h()).done;){var u=s.value;if(u[1]===t)return u[0]}throw new n.ValueError("not found")},e.prototype.pop=function(){throw new n.NotImplementedError("pop")},e.prototype.popleft=function(){throw new n.NotImplementedError("popleft")},e.prototype.insert=function(t,n){this.b(t),this.append(n);for(var i=this.len()-1;t<i;--i){var r=this.get(i);this.set(i,this.get(i-1)),this.set(i-1,r)}return this},e.prototype.delete=function(t){this.b(t);for(var n=this.len()-1;t<n;++t)this.set(t,this.get(t+1));return this.pop(),this},e.prototype.remove=function(t){var n=this.index(t);return this.delete(n),this},e.prototype.reverse=function(){for(var t=0,n=this.len();t<--n;++t){var i=this.get(t),r=this.get(n);this.set(t,r),this.set(n,i)}return this},e.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},(s.prototype=new e).values=regeneratorRuntime.mark(function t(){var n,i,r,e;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:i=(n=this.x)+this.length,r=Math.min(this.capacity(),i);case 3:if(!(n<r)){t.next=9;break}return t.next=6,this.c[n];case 6:++n,t.next=3;break;case 9:if(!((e=i%this.capacity())<i)){t.next=18;break}n=0;case 12:if(!(n<e)){t.next=18;break}return t.next=15,this.c[n];case 15:++n,t.next=12;break;case 18:case"end":return t.stop()}},t,this)}),s.prototype.p=function(t,i){throw new n.NotImplementedError("_popindex")},s.prototype.pop=function(){var t=this.w(this.length-1);return this.p(t[0],t[1])},s.prototype.popleft=function(){var t=this.w(0),n=t[0],i=t[1];return++this.x,this.x%=this.capacity(),this.p(n,i)},(h.prototype=new s).len=function(){return this.length},h.prototype.capacity=function(){return this.M},h.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},h.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},h.prototype.clear=function(){return this.x=0,this.length=0,this.c=new Array(this.M),this},h.prototype.copy=function(){return new h(this,this.M)},h.prototype.w=function(t){return this.b(t),[this.c,(this.x+t)%this.M]},h.prototype.p=function(t,n){var i=t[n];return t[n]=0,--this.length,i},(u.prototype=new e).len=function(){return 0},u.prototype.capacity=function(){return 0},u.prototype.values=function(){return{next:function(){return{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 n.IndexError(t)},u.prototype.pop=u.prototype.popleft=function(){throw new n.IndexError("pop / popleft")},(o.prototype=new e).len=function(){return this.e?0:1},o.prototype.capacity=function(){return 1},o.prototype.values=regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(!this.e){t.next=2;break}return t.abrupt("return");case 2:return t.next=4,this.v;case 4:case"end":return t.stop()}},t,this)}),o.prototype.append=o.prototype.appendleft=function(t){return this.e=!1,this.v=t,this},o.prototype.clear=function(){return this.e=!0,this.v=0,this},o.prototype.copy=function(){return new o(this)},o.prototype.pop=o.prototype.popleft=function(){if(this.e)throw new n.IndexError("pop / popleft");var t=this.v;return this.e=!0,this.v=0,t},o.prototype.get=function(t){if(this.e||0!==t)throw new n.IndexError(t);return this.v},o.prototype.set=function(t,i){if(this.e||0!==t)throw new n.IndexError(t);return this.v=i,this},(f.prototype=new s).y=function(t){for(var n=this.length,i=0;i<n;++i)t[i]=this.get(i)},f.prototype.a=function(t){var n=new Array(t);this.y(n),this.c=n,this.x=0,this.s=t},f.prototype.z=function(){var 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,n){var i=t[n];return t[n]=0,--this.length,this.z(),i};var a=c(f,h,o,u);t.ArbitrarySizeDeque=s,t.BoundedDeque=h,t.Deque=e,t.EmptyDeque=u,t.SingleElementDeque=o,t.UnboundedDeque=f,t.t=c,t.deque=a});
//# sourceMappingURL=index.umd.js.map