mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-20 10:16:29 +10:00
1 line
7.1 KiB
JavaScript
1 line
7.1 KiB
JavaScript
function Map(e){"use strict";return e&&e.constructor===Map?e:e&&0!==e.length?Map.empty().merge(e):Map.empty()}function OwnerID(){"use strict"}function BitmapIndexedNode(e,t,r,n){"use strict";this.ownerID=e,this.bitmap=t,this.keys=r,this.values=n}function HashCollisionNode(e,t,r,n){"use strict";this.ownerID=e,this.collisionHash=t,this.keys=r,this.values=n}function makeNode(e,t,r,n,s){var i=r>>>t&MASK,u=[],o=[];return o[i]=s,null!=n&&(u[i]=n),new BitmapIndexedNode(e,1<<i,u,o)}function deepMerger(e){return function(t,r){return t&&t.mergeDeepWith?t.mergeDeepWith(e,r):e?e(t,r):r}}function mergeIntoMapWith(e,t,r){return 0===r.length?e:e.withMutations(function(e){for(var n=0;n<r.length;n++){var s=r[n];s&&(s=s.forEach?s:Sequence(s),s.forEach(t?function(r,n){var s=e.get(n,__SENTINEL);e.set(n,s===__SENTINEL?r:t(s,r))}:function(t,r){e.set(r,t)}))}})}function updateInDeepMap(e,t,r,n){var s=t[n],i=e.get?e.get(s,__SENTINEL):__SENTINEL;return i===__SENTINEL?e:e.set?e.set(s,n===t.length-1?r(i):updateInDeepMap(i,t,r,n+1)):e}function BoolRef(e){return __BOOL_REF.value=e,__BOOL_REF}function hashValue(e){if(!e)return 0;if(e===!0)return 1;if("function"==typeof e.hashCode)return e.hashCode();var t=typeof e;if("number"===t)return Math.floor(e)%2147483647;if("string"===t)return hashString(e);throw new Error("Unable to hash")}function hashString(e){var t=STRING_HASH_CACHE[e];if(null==t){t=0;for(var r=0;r<e.length;r++)t=(31*t+e.charCodeAt(r))%STRING_HASH_MAX_VAL;STRING_HASH_CACHE_SIZE===STRING_HASH_CACHE_MAX_SIZE&&(STRING_HASH_CACHE_SIZE=0,STRING_HASH_CACHE={}),STRING_HASH_CACHE_SIZE++,STRING_HASH_CACHE[e]=t}return t}var Sequence=require("./Sequence").Sequence;for(var Sequence____Key in Sequence)Sequence.hasOwnProperty(Sequence____Key)&&(Map[Sequence____Key]=Sequence[Sequence____Key]);var ____SuperProtoOfSequence=null===Sequence?null:Sequence.prototype;Map.prototype=Object.create(____SuperProtoOfSequence),Map.prototype.constructor=Map,Map.__superConstructor__=Sequence,Map.empty=function(){"use strict";return __EMPTY_MAP||(__EMPTY_MAP=Map.$Map_make(0))},Map.prototype.toString=function(){"use strict";return this.__toString("Map {","}")},Map.prototype.get=function(e,t){"use strict";return null==e||null==this.$Map_root?t:this.$Map_root.get(0,hashValue(e),e,t)},Map.prototype.set=function(e,t){"use strict";if(null==e)return this;var r,n=this.length;if(this.$Map_root){var s=BoolRef();r=this.$Map_root.set(this.__ownerID,0,hashValue(e),e,t,s),s.value&&n++}else n++,r=makeNode(this.__ownerID,0,hashValue(e),e,t);return this.__ownerID?(this.length=n,this.$Map_root=r,this):r===this.$Map_root?this:Map.$Map_make(n,r)},Map.prototype.delete=function(e){"use strict";if(null==e||null==this.$Map_root)return this;if(this.__ownerID){var t=BoolRef();return this.$Map_root=this.$Map_root.delete(this.__ownerID,0,hashValue(e),e,t),t.value&&this.length--,this}var r=this.$Map_root.delete(this.__ownerID,0,hashValue(e),e);return r?r===this.$Map_root?this:Map.$Map_make(this.length-1,r):Map.empty()},Map.prototype.clear=function(){"use strict";return this.__ownerID?(this.length=0,this.$Map_root=null,this):Map.empty()},Map.prototype.merge=function(){"use strict";return mergeIntoMapWith(this,null,arguments)},Map.prototype.mergeWith=function(e){"use strict";var t=Array.prototype.slice.call(arguments,1);return mergeIntoMapWith(this,e,t)},Map.prototype.mergeDeep=function(){"use strict";return mergeIntoMapWith(this,deepMerger(null),arguments)},Map.prototype.mergeDeepWith=function(e){"use strict";var t=Array.prototype.slice.call(arguments,1);return mergeIntoMapWith(this,deepMerger(e),t)},Map.prototype.updateIn=function(e,t){"use strict";return updateInDeepMap(this,e,t,0)},Map.prototype.withMutations=function(e){"use strict";var t=this.__ensureOwner(this.__ownerID||new OwnerID);return e(t),t.__ensureOwner(this.__ownerID)},Map.prototype.__ensureOwner=function(e){"use strict";return e===this.__ownerID?this:e?Map.$Map_make(this.length,this.$Map_root,e):(this.__ownerID=e,this)},Map.prototype.__deepEqual=function(e){"use strict";var t=require("./Immutable").is,r=this;return e.every(function(e,n){return t(r.get(n,__SENTINEL),e)})},Map.prototype.__iterate=function(e,t){"use strict";return this.$Map_root?this.$Map_root.iterate(this,e,t):0},Map.$Map_make=function(e,t,r){"use strict";var n=Object.create(Map.prototype);return n.length=e,n.$Map_root=t,n.__ownerID=r,n},Map.from=Map,BitmapIndexedNode.prototype.get=function(e,t,r,n){"use strict";var s=t>>>e&MASK;if(0===(this.bitmap&1<<s))return n;var i=this.keys[s],u=this.values[s];return null==i?u.get(e+SHIFT,t,r,n):r===i?u:n},BitmapIndexedNode.prototype.set=function(e,t,r,n,s,i){"use strict";var u,o=r>>>t&MASK,a=1<<o;if(0===(this.bitmap&a))return i&&(i.value=!0),u=this.ensureOwner(e),u.keys[o]=n,u.values[o]=s,u.bitmap|=a,u;var p,h=this.keys[o],_=this.values[o];if(null==h)return p=_.set(e,t+SHIFT,r,n,s,i),p===_?this:(u=this.ensureOwner(e),u.values[o]=p,u);if(n===h)return s===_?this:(u=this.ensureOwner(e),u.values[o]=s,u);var l=hashValue(h);return p=r===l?new HashCollisionNode(e,r,[h,n],[_,s]):makeNode(e,t+SHIFT,l,h,_).set(e,t+SHIFT,r,n,s),i&&(i.value=!0),u=this.ensureOwner(e),delete u.keys[o],u.values[o]=p,u},BitmapIndexedNode.prototype.delete=function(e,t,r,n,s){"use strict";var i,u=r>>>t&MASK,o=1<<u,a=this.keys[u];if(0===(this.bitmap&o)||null!=a&&n!==a)return this;if(null==a){var p=this.values[u],h=p.delete(e,t+SHIFT,r,n,s);if(h===p)return this;if(h)return i=this.ensureOwner(e),i.values[u]=h,i}else s&&(s.value=!0);return this.bitmap===o?null:(i=this.ensureOwner(e),delete i.keys[u],delete i.values[u],i.bitmap^=o,i)},BitmapIndexedNode.prototype.ensureOwner=function(e){"use strict";return e&&e===this.ownerID?this:new BitmapIndexedNode(e,this.bitmap,this.keys.slice(),this.values.slice())},BitmapIndexedNode.prototype.iterate=function(e,t,r){"use strict";for(var n=this.values,s=this.keys,i=n.length,u=0;i>=u;u++){var o=r?i-u:u,a=s[o],p=n[o];if(null!=a){if(t(p,a,e)===!1)return!1}else if(p&&!p.iterate(e,t,r))return!1}return!0},HashCollisionNode.prototype.get=function(e,t,r,n){"use strict";var s=Sequence(this.keys).indexOf(r);return-1===s?n:this.values[s]},HashCollisionNode.prototype.set=function(e,t,r,n,s,i){"use strict";if(r!==this.collisionHash)return i&&(i.value=!0),makeNode(e,t,r,null,this).set(e,t,r,n,s);var u=Sequence(this.keys).indexOf(n);if(u>=0&&this.values[u]===s)return this;var o=this.ensureOwner(e);return-1===u?(o.keys.push(n),o.values.push(s),i&&(i.value=!0)):o.values[u]=s,o},HashCollisionNode.prototype.delete=function(e,t,r,n,s){"use strict";var i=this.keys.indexOf(n);if(-1===i)return this;if(s&&(s.value=!0),this.values.length>1){var u=this.ensureOwner(e);return u.keys[i]=u.keys.pop(),u.values[i]=u.values.pop(),u}},HashCollisionNode.prototype.ensureOwner=function(e){"use strict";return e&&e===this.ownerID?this:new HashCollisionNode(e,this.collisionHash,this.keys.slice(),this.values.slice())},HashCollisionNode.prototype.iterate=function(e,t,r){"use strict";for(var n=this.values,s=this.keys,i=n.length-1,u=0;i>=u;u++){var o=r?i-u:u;if(t(n[o],s[o],e)===!1)return!1}return!0};var __BOOL_REF={value:!1},STRING_HASH_MAX_VAL=4294967296,STRING_HASH_CACHE_MAX_SIZE=255,STRING_HASH_CACHE_SIZE=0,STRING_HASH_CACHE={},SHIFT=5,SIZE=1<<SHIFT,MASK=SIZE-1,__SENTINEL={},__EMPTY_MAP;module.exports=Map; |