Files

1 line
11 KiB
JavaScript

function Vector(){"use strict";var e=Array.prototype.slice.call(arguments,0);return Vector.from(e)}function OwnerID(){"use strict"}function VNode(e,t){"use strict";this.array=e,this.ownerID=t}function VectorIterator(e,t,r,o,i,n){"use strict";var s=getTailOffset(r);this.$VectorIterator_stack={node:i.array,level:o,offset:-t,max:s-t,__prev:{node:n.array,level:0,offset:s-t,max:r-t}}}function vectorWithLengthOfLongestSeq(e,t){var r=Math.max.apply(null,t.map(function(e){return e.length||0}));return r>e.length?e.setLength(r):e}function rawIndex(e,t){if(0>e)throw new Error("Index out of bounds");return e+t}function getTailOffset(e){return SIZE>e?0:e-1>>>SHIFT<<SHIFT}var SequenceModule=require("./Sequence"),Sequence=SequenceModule.Sequence,IndexedSequence=SequenceModule.IndexedSequence,ImmutableMap=require("./Map");for(var IndexedSequence____Key in IndexedSequence)IndexedSequence.hasOwnProperty(IndexedSequence____Key)&&(Vector[IndexedSequence____Key]=IndexedSequence[IndexedSequence____Key]);var ____SuperProtoOfIndexedSequence=null===IndexedSequence?null:IndexedSequence.prototype;Vector.prototype=Object.create(____SuperProtoOfIndexedSequence),Vector.prototype.constructor=Vector,Vector.__superConstructor__=IndexedSequence,Vector.empty=function(){"use strict";return __EMPTY_VECT||(__EMPTY_VECT=Vector.$Vector_make(0,0,SHIFT,__EMPTY_VNODE,__EMPTY_VNODE))},Vector.from=function(e){"use strict";if(e&&e.constructor===Vector)return e;if(!e||0===e.length)return Vector.empty();var t=Array.isArray(e);return e.length>0&&e.length<SIZE?Vector.$Vector_make(0,e.length,SHIFT,__EMPTY_VNODE,new VNode(t?e.slice():Sequence(e).toArray())):(t||(e=Sequence(e),e instanceof IndexedSequence||(e=e.values())),Vector.empty().merge(e))},Vector.prototype.toString=function(){"use strict";return this.__toString("Vector [","]")},Vector.prototype.get=function(e,t){"use strict";if(e=rawIndex(e,this.$Vector_origin),e>=this.$Vector_size)return t;var r=this.$Vector_nodeFor(e),o=e&MASK;return r&&(void 0===t||r.array.hasOwnProperty(o))?r.array[o]:t},Vector.prototype.first=function(){"use strict";return this.get(0)},Vector.prototype.last=function(){"use strict";return this.get(this.length?this.length-1:0)},Vector.prototype.set=function(e,t){"use strict";var r=getTailOffset(this.$Vector_size);if(e>=this.length)return this.withMutations(function(r){return r.$Vector_setBounds(0,e+1).set(e,t)});if(this.get(e,__SENTINEL)===t)return this;if(e=rawIndex(e,this.$Vector_origin),e>=r){var o=this.$Vector_tail.ensureOwner(this.__ownerID);o.array[e&MASK]=t;var i=e>=this.$Vector_size?e+1:this.$Vector_size;return this.__ownerID?(this.length=i-this.$Vector_origin,this.$Vector_size=i,this.$Vector_tail=o,this):Vector.$Vector_make(this.$Vector_origin,i,this.$Vector_level,this.$Vector_root,o)}for(var n=this.$Vector_root.ensureOwner(this.__ownerID),s=n,c=this.$Vector_level;c>0;c-=SHIFT){var a=e>>>c&MASK;s=s.array[a]=s.array[a]?s.array[a].ensureOwner(this.__ownerID):new VNode([],this.__ownerID)}return s.array[e&MASK]=t,this.__ownerID?(this.$Vector_root=n,this):Vector.$Vector_make(this.$Vector_origin,this.$Vector_size,this.$Vector_level,n,this.$Vector_tail)},Vector.prototype.delete=function(e){"use strict";if(!this.has(e))return this;var t=getTailOffset(this.$Vector_size);if(e=rawIndex(e,this.$Vector_origin),e>=t){var r=this.$Vector_tail.ensureOwner(this.__ownerID);return delete r.array[e&MASK],this.__ownerID?(this.$Vector_tail=r,this):Vector.$Vector_make(this.$Vector_origin,this.$Vector_size,this.$Vector_level,this.$Vector_root,r)}for(var o=this.$Vector_root.ensureOwner(this.__ownerID),i=o,n=this.$Vector_level;n>0;n-=SHIFT){var s=e>>>n&MASK;i=i.array[s]=i.array[s].ensureOwner(this.__ownerID)}return delete i.array[e&MASK],this.__ownerID?(this.$Vector_root=o,this):Vector.$Vector_make(this.$Vector_origin,this.$Vector_size,this.$Vector_level,o,this.$Vector_tail)},Vector.prototype.clear=function(){"use strict";return this.__ownerID?(this.length=this.$Vector_origin=this.$Vector_size=0,this.$Vector_level=SHIFT,this.$Vector_root=this.$Vector_tail=__EMPTY_VNODE,this):Vector.empty()},Vector.prototype.push=function(){"use strict";var e=arguments,t=this.length;return this.withMutations(function(r){r.$Vector_setBounds(0,t+e.length);for(var o=0;o<e.length;o++)r.set(t+o,e[o])})},Vector.prototype.pop=function(){"use strict";return this.$Vector_setBounds(0,-1)},Vector.prototype.unshift=function(){"use strict";var e=arguments;return this.withMutations(function(t){t.$Vector_setBounds(-e.length);for(var r=0;r<e.length;r++)t.set(r,e[r])})},Vector.prototype.shift=function(){"use strict";return this.$Vector_setBounds(1)},Vector.prototype.merge=function(){"use strict";var e=Array.prototype.slice.call(arguments,0);return ImmutableMap.prototype.merge.apply(vectorWithLengthOfLongestSeq(this,e),arguments)},Vector.prototype.mergeWith=function(){"use strict";var e=Array.prototype.slice.call(arguments,1);return ImmutableMap.prototype.mergeWith.apply(vectorWithLengthOfLongestSeq(this,e),arguments)},Vector.prototype.mergeDeep=function(){"use strict";var e=Array.prototype.slice.call(arguments,0);return ImmutableMap.prototype.mergeDeep.apply(vectorWithLengthOfLongestSeq(this,e),arguments)},Vector.prototype.mergeDeepWith=function(){"use strict";var e=Array.prototype.slice.call(arguments,1);return ImmutableMap.prototype.mergeDeepWith.apply(vectorWithLengthOfLongestSeq(this,e),arguments)},Vector.prototype.setLength=function(e){"use strict";return this.$Vector_setBounds(0,e)},Vector.prototype.$Vector_setBounds=function(e,t){"use strict";var r=this.__ownerID||new OwnerID,o=this.$Vector_origin,i=this.$Vector_size,n=o+e,s=null==t?i:0>t?i+t:o+t;if(n===o&&s===i)return this;if(n>=s)return this.clear();for(var c=this.$Vector_level,a=this.$Vector_root,h=0;0>n+h;)a=new VNode(a.array.length?[,a]:[],r),h+=1<<c,c+=SHIFT;h&&(n+=h,o+=h,s+=h,i+=h);for(var u=getTailOffset(i),_=getTailOffset(s);_>=1<<c+SHIFT;)a=new VNode(a.array.length?[a]:[],r),c+=SHIFT;var V=this.$Vector_tail,l=u>_?this.$Vector_nodeFor(s):_>u?new VNode([],r):V;if(_>u&&i>n&&V.array.length){a=a.ensureOwner(r);for(var f=a,p=c;p>SHIFT;p-=SHIFT){var y=u>>>p&MASK;f=f.array[y]=f.array[y]?f.array[y].ensureOwner(r):new VNode([],r)}f.array[u>>>SHIFT&MASK]=V}if(i>s&&(l=l.removeAfter(r,0,s)),n>=_)n-=_,s-=_,c=SHIFT,a=__EMPTY_VNODE,l=l.removeBefore(r,0,n);else if(n>o||u>_){var v,$;h=0;do v=n>>>c&MASK,$=_-1>>>c&MASK,v===$&&(v&&(h+=(1<<c)*v),c-=SHIFT,a=a&&a.array[v]);while(a&&v===$);a&&n>o&&(a=a.removeBefore(r,c,n-h)),a&&u>_&&(a=a.removeAfter(r,c,_-h)),h&&(n-=h,s-=h),a=a||__EMPTY_VNODE}return this.__ownerID?(this.length=s-n,this.$Vector_origin=n,this.$Vector_size=s,this.$Vector_level=c,this.$Vector_root=a,this.$Vector_tail=l,this):Vector.$Vector_make(n,s,c,a,l)},Vector.prototype.__ensureOwner=function(e){"use strict";return e===this.__ownerID?this:e?Vector.$Vector_make(this.$Vector_origin,this.$Vector_size,this.$Vector_level,this.$Vector_root,this.$Vector_tail,e):(this.__ownerID=e,this)},Vector.prototype.slice=function(e,t,r){"use strict";var o=____SuperProtoOfIndexedSequence.slice.call(this,e,t,r);if(!r&&o!==this){var i=this,n=i.length;o.toVector=function(){return i.$Vector_setBounds(0>e?Math.max(0,n+e):n?Math.min(n,e):e,null==t?n:0>t?Math.max(0,n+t):n?Math.min(n,t):t)}}return o},Vector.prototype.__deepEquals=function(e){"use strict";var t=require("./Immutable").is,r=this.__iterator__();return e.every(function(e,o){var i=r.next();return o===i[0]&&t(e,i[1])})},Vector.prototype.__iterator__=function(){"use strict";return new VectorIterator(this,this.$Vector_origin,this.$Vector_size,this.$Vector_level,this.$Vector_root,this.$Vector_tail)},Vector.prototype.__iterate=function(e,t,r){"use strict";var o=this,i=0,n=o.length-1;r^=t;var s,c=function(t,s){return e(t,r?n-s:s,o)===!1?!1:(i=s,!0)},a=getTailOffset(this.$Vector_size);return s=t?this.$Vector_tail.iterate(0,a-this.$Vector_origin,this.$Vector_size-this.$Vector_origin,c,t)&&this.$Vector_root.iterate(this.$Vector_level,-this.$Vector_origin,a-this.$Vector_origin,c,t):this.$Vector_root.iterate(this.$Vector_level,-this.$Vector_origin,a-this.$Vector_origin,c,t)&&this.$Vector_tail.iterate(0,a-this.$Vector_origin,this.$Vector_size-this.$Vector_origin,c,t),(s?n:t?n-i:i)+1},Vector.$Vector_make=function(e,t,r,o,i,n){"use strict";var s=Object.create(Vector.prototype);return s.length=t-e,s.$Vector_origin=e,s.$Vector_size=t,s.$Vector_level=r,s.$Vector_root=o,s.$Vector_tail=i,s.__ownerID=n,s},Vector.prototype.$Vector_nodeFor=function(e){"use strict";if(e>=getTailOffset(this.$Vector_size))return this.$Vector_tail;if(e<1<<this.$Vector_level+SHIFT){for(var t=this.$Vector_root,r=this.$Vector_level;t&&r>0;)t=t.array[e>>>r&MASK],r-=SHIFT;return t}},Vector.prototype.withMutations=ImmutableMap.prototype.withMutations,Vector.prototype.updateIn=ImmutableMap.prototype.updateIn,VNode.prototype.ensureOwner=function(e){"use strict";return e&&e===this.ownerID?this:new VNode(this.array.slice(),e)},VNode.prototype.removeBefore=function(e,t,r){"use strict";if(r===1<<t||0===this.array.length)return this;var o=r>>>t&MASK;if(o>=this.array.length)return new VNode([],e);var i,n=0===o;if(t>0){var s=this.array[o];if(i=s&&s.removeBefore(e,t-SHIFT,r),i===s&&n)return this}if(n&&!i)return this;var c=this.ensureOwner();if(!n)for(var a=0;o>a;a++)delete c.array[a];return i&&(c.array[o]=i),c},VNode.prototype.removeAfter=function(e,t,r){"use strict";if(r===1<<t||0===this.array.length)return this;var o=r-1>>>t&MASK;if(o>=this.array.length)return this;var i,n=o===this.array.length-1;if(t>0){var s=this.array[o];if(i=s&&s.removeAfter(e,t-SHIFT,r),i===s&&n)return this}if(n&&!i)return this;var c=this.ensureOwner();return n||(c.array.length=o+1),i&&(c.array[o]=i),c},VNode.prototype.iterate=function(e,t,r,o,i){"use strict";if(0===e){if(i){for(var n=this.array.length-1;n>=0;n--)if(this.array.hasOwnProperty(n)){var s=n+t;if(s>=0&&r>s&&o(this.array[n],s)===!1)return!1}return!0}return this.array.every(function(e,i){var n=i+t;return 0>n||n>=r||o(e,n)!==!1})}var c=1<<e,a=e-SHIFT;if(i){for(var h=this.array.length-1;h>=0;h--){var u=t+h*c;if(r>u&&u+c>0&&this.array.hasOwnProperty(h)&&!this.array[h].iterate(a,u,r,o,i))return!1}return!0}return this.array.every(function(e,n){var s=t+n*c;return s>=r||0>=s+c||e.iterate(a,s,r,o,i)})},VectorIterator.prototype.next=function(){"use strict";var e=this.$VectorIterator_stack;e:for(;e;){if(0===e.level)for(e.rawIndex||(e.rawIndex=0);e.rawIndex<e.node.length;){var t=e.rawIndex+e.offset;if(t>=0&&t<e.max&&e.node.hasOwnProperty(e.rawIndex)){var r=e.node[e.rawIndex];return e.rawIndex++,[t,r]}e.rawIndex++}else{var o=1<<e.level;for(e.levelIndex||(e.levelIndex=0);e.levelIndex<e.node.length;){var i=e.offset+e.levelIndex*o;if(i+o>0&&i<e.max&&e.node.hasOwnProperty(e.levelIndex)){var n=e.node[e.levelIndex].array;e.levelIndex++,e=this.$VectorIterator_stack={node:n,level:e.level-SHIFT,offset:i,max:e.max,__prev:e};continue e}e.levelIndex++}}e=this.$VectorIterator_stack=this.$VectorIterator_stack.__prev}if(global.StopIteration)throw global.StopIteration};var SHIFT=5,SIZE=1<<SHIFT,MASK=SIZE-1,__SENTINEL={},__EMPTY_VECT,__EMPTY_VNODE=new VNode([]);module.exports=Vector;