@stdlib/esm
Version:
ES module distribution for Stdlib, a standard library for JavaScript and Node.js.
3 lines • 3.7 kB
JavaScript
// This file is a part of stdlib. License is Apache-2.0: http://www.apache.org/licenses/LICENSE-2.0
import t from"./define-nonenumerable-read-only-property.js";import e from"./define-nonenumerable-read-only-accessor.js";import r from"./../symbol/iterator.js";import i from"./define-property.js";var n=i;function s(t){return n(this,"next",{configurable:!1,enumerable:!0,get:function(){return this._next}}),n(this,"prev",{configurable:!1,enumerable:!0,get:function(){return this._prev}}),this.value=t,n(this,"_next",{configurable:!1,enumerable:!1,writable:!0,value:null}),n(this,"_prev",{configurable:!1,enumerable:!1,writable:!0,value:null}),this}var h=t,o=e,l=r,a=s;function u(){return this instanceof u?(this._length=0,this._first=null,this._last=null,this):new u}h(u.prototype,"clear",(function(){return this._length=0,this._first=null,this._last=null,this})),h(u.prototype,"first",(function(){if(this._length)return this._first})),h(u.prototype,"insert",(function(t,e,r){var i,n;if(arguments.length>2){if("before"!==(i=r)&&"after"!==i)throw new Error("invalid argument. Third argument must be a recognized location. Value: `"+i+"`.")}else i="after";if("after"===i&&t===this._last)return this.push(e);if("before"===i&&t===this._first)return this.unshift(e);for(n=this._first;n!==this._last&&n!==t;)n=n._next;if(n===this._last&&n!==t)throw new Error("invalid argument. The list does not contain the provided list node.");return n=new a(e),"after"===i?(t._next._prev=n,n._next=t._next,t._next=n,n._prev=t):(t._prev._next=n,n._prev=t._prev,t._prev=n,n._next=t),this._length+=1,this})),h(u.prototype,"iterator",(function(t){var e,r,i,n,s,o,a;if(arguments.length){if("forward"!==(s=t)&&"reverse"!==s)throw new Error("invalid argument. Must provide a recognized iteration direction. Value: `"+s+"`.")}else s="forward";return i=this,"forward"===s?(a=-1,o=1):(a=this._length,o=-1),e=this.toArray(),h(r={},"next",u),h(r,"return",_),l&&h(r,l,f),r;function u(){return a+=o,n||a<0||a>=e.length?{done:!0}:{value:e[a],done:!1}}function _(t){return n=!0,arguments.length?{value:t,done:!0}:{done:!0}}function f(){return i.iterator()}})),h(u.prototype,"last",(function(){if(this._length)return this._last})),o(u.prototype,"length",(function(){return this._length})),h(u.prototype,"pop",(function(){var t;return this._length&&(t=this._last.value,this._last._prev?(this._last=this._last._prev,this._last._next=null):(this._first=null,this._last=null),this._length-=1),t})),h(u.prototype,"push",(function(t){var e;return e=new a(t),0===this._length?(this._first=e,this._last=e):(e._prev=this._last,this._last._next=e,this._last=e),this._length+=1,this})),h(u.prototype,"remove",(function(t){var e,r;if(t===this._first)return this.shift();if(t===this._last)return this.pop();for(e=t.value,r=this._first;r!==this._last&&r!==t;)r=r._next;if(r===this._last)throw new Error("invalid argument. The list does not contain the provided list node.");return t._prev._next=t._next,t._next._prev=t._prev,this._length-=1,e})),h(u.prototype,"shift",(function(){var t;return this._length&&(t=this._first.value,this._first._next?(this._first=this._first._next,this._first._prev=null):(this._first=null,this._last=null),this._length-=1),t})),h(u.prototype,"toArray",(function(){var t,e,r;for(e=[],t=this._first,r=0;r<this._length;r++)e.push(t.value),t=t.next;return e})),h(u.prototype,"toJSON",(function(){var t={type:"doubly-linked-list"};return t.data=this.toArray(),t})),h(u.prototype,"unshift",(function(t){var e;return e=new a(t),0===this._length?(this._first=e,this._last=e):(e._next=this._first,this._first._prev=e,this._first=e),this._length+=1,this}));var _=u;export default _;
//# sourceMappingURL=doubly-linked-list.js.map