-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFastList.min.js
1 lines (1 loc) · 3.01 KB
/
FastList.min.js
1
!function(t){"use strict";function e(t,e){return null===t&&(t="null"),null===e&&(e="null"),t.toString()<e.toString()?-1:1}function n(t){this.value=t.value,this.next=t.next||null,this.parent=t.parent||null,this.__key__=t.key}function i(){var t=(new Date).getTime(),e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var n=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"==e?n:3&n|8).toString(16)});return e}var r=function(){this.head=null,this.tail=null,this.length=0,this.listIndex={}};r.prototype._mustFind=function(t){var e=this.listIndex[t];if(e)return e;throw new Error(t+" does not reference an existing node. Create a node with this key first using an add method")},r.prototype._mustNotFind=function(t){if(this.listIndex[t])throw new Error(t+" already refers to an existing node. Use 'set' to update an existing node or make sure to pass in a unique key.")},r.prototype._add=function(t,e){e=e||i();try{this._mustNotFind(e);var r=new n({value:t,key:e});return this.listIndex[e]=r,this.length++,r}catch(a){throw a}},r.prototype._addFirst=function(t,e){if(this.length)throw new Error("List already has elements. Use add before/after instead");return this.head=this.tail=this._add(t,e),this.head.__key__},r.prototype._placeIn=function(t,e,n){try{var i=this._mustFind(e),r=n?"parent":"next",a=n?"next":"parent";i[r]&&(t[r]=i[r],i[r][a]=t),t[a]=i,i[r]=t,n&&i===this.head?this.head=t:n||i!==this.tail||(this.tail=t)}catch(o){throw o}},r.prototype.addBefore=function(t,e,n){try{var i=this._add(e,n);return this._placeIn(i,t,!0),i.__key__}catch(r){throw r}},r.prototype.addAfter=function(t,e,n){try{var i=this._add(e,n);return this._placeIn(i,t,!1),i.__key__}catch(r){throw r}},r.prototype.addToTail=function(t,e){return this.head?this.addAfter(this.tail.__key__,t,e):this._addFirst(t,e)},r.prototype.addToHead=function(t,e){return this.head?this.addBefore(this.head.__key__,t,e):this._addFirst(t,e)},r.prototype.removeAt=function(t){var e=this.listIndex[t];return e?(e.parent?e.parent.next=e.next:this.head===e&&(this.head=e.next),e.next?e.next.parent=e.parent:this.tail===e&&(this.tail=e.parent),delete this.listIndex[t],this.length--,e.value):void 0},r.prototype.removeFromHead=function(){return this.head?this.removeAt(this.head.__key__):null},r.prototype.removeFromTail=function(){return this.tail?this.removeAt(this.tail.__key__):null},r.prototype.get=function(t){var e=this.listIndex[t];return e?e.value:void 0},r.prototype.first=function(){return this.length?this.get(this.head.__key__):void 0},r.prototype.last=function(){return this.length?this.get(this.tail.__key__):void 0},r.prototype.set=function(t,e){try{var n=this._mustFind(t);n.value=e}catch(i){throw i}},r.prototype.forEach=r.prototype.each=function(t){for(var e=this.head;e;e=e.next)t(e.value,e.__key__,this)},r.prototype.sort=function(t){t=t||e;for(var n=this.head;n;n=n.next)for(var i=n.parent;i&&t(n.value,i.value)<0;)i.next=n.next,n.parent=i.parent,i===this.head?this.head=n:i.parent.next=n,n===this.tail?this.tail=i:n.next.parent=i,i.parent=n,n.next=i,i=n.parent},t.FastList=r}(window);