typescript-dotnet-core
Version:
A the core classes and utilities of a JavaScript-Friendly .NET Based TypeScript Library.
8 lines (7 loc) • 3.55 kB
JavaScript
"use strict";
/*!
* @author electricessence / https://github.com/electricessence/
* Original: http://linqjs.codeplex.com/
* Licensing: MIT https://github.com/electricessence/TypeScript.NET-Core/blob/master/LICENSE.md
*/Object.defineProperty(exports,"__esModule",{value:!0});var linkedListPool,tslib_1=require("tslib"),Compare_1=require("../../Compare"),Types_1=require("../../Types"),EnumeratorBase_1=require("../Enumeration/EnumeratorBase"),LinkedNodeList_1=require("../LinkedNodeList"),ObjectPool_1=require("../../Disposable/ObjectPool"),getIdentifier_1=require("./getIdentifier"),DictionaryBase_1=require("./DictionaryBase"),VOID0=void 0,HashEntry=function(e,t,r,n){this.key=e,this.value=t,this.previous=r,this.next=n};function linkedNodeList(e){if(linkedListPool=linkedListPool||ObjectPool_1.default.createAutoRecycled(function(){return new LinkedNodeList_1.default},20),!e)return linkedListPool.take();linkedListPool.give(e)}var Dictionary=function(r){function e(e){var t=r.call(this)||this;return t._keyGenerator=e,t._entries=linkedNodeList(),t._buckets={},t}return tslib_1.__extends(e,r),e.prototype._onDispose=function(){r.prototype._onDispose.call(this);var e=this;e._entries=null,e._buckets=null,e._hashGenerator=null},e.prototype.getCount=function(){return this._entries&&this._entries.unsafeCount||0},e.prototype._getBucket=function(e,t){if(null==e||!t&&!this.getCount())return VOID0;Types_1.default.isPrimitiveOrSymbol(e)||console.warn("Key type not indexable and could cause Dictionary to be extremely slow.");var r=this._buckets,n=r[e];return t&&!n&&(r[e]=n=linkedNodeList()),n},e.prototype._getBucketEntry=function(e,t,r){if(null==e||!this.getCount())return VOID0;var n=this._keyGenerator,i=n?n(e):e;return(r=r||this._getBucket(t||getIdentifier_1.default(i)))&&(n?r.find(function(e){return n(e.key)===i}):r.find(function(e){return e.key===i}))},e.prototype._getEntry=function(e){var t=this._getBucketEntry(e);return t&&t.value},e.prototype.getValue=function(e){var t=this._getEntry(e);return t?t.value:VOID0},e.prototype._setValueInternal=function(e,t){var r=this,n=r._buckets,i=r._entries,o=r._keyGenerator?r._keyGenerator(e):e,u=getIdentifier_1.default(o),s=r._getBucket(u),a=s&&r._getBucketEntry(e,u,s);if(a){var l=s;if(t!==VOID0){var d=a.value.value;return a.value.value=t,!Compare_1.areEqual(t,d)}var c=l.removeNode(a),f=i.removeNode(a.value);if(c&&!l.count&&(delete n[u],linkedNodeList(l)),c!==f)throw"Entries and buckets are out of sync.";if(c)return!0}else if(t!==VOID0){if(!(s=s||r._getBucket(u,!0)))throw new Error('"'+u.toString()+'" cannot be added to lookup table.');var _=new HashEntry(e,t);return i.addNode(_),s.addNode(new HashEntry(e,_)),!0}return!1},e.prototype._clearInternal=function(){var e=this._buckets;for(var t in e)if(e.hasOwnProperty(t)){var r=e[t];delete e[t],linkedNodeList(r)}return this._entries.clear()},e.prototype.getEnumerator=function(){var r,n,i=this;return i.throwIfDisposed(),new EnumeratorBase_1.FiniteEnumeratorBase(function(){i.throwIfDisposed(),r=i._version,n=i._entries.first},function(e){if(n){i.throwIfDisposed(),i.assertVersion(r);var t={key:n.key,value:n.value};return n=n.next,e.yieldReturn(t)}return e.yieldBreak()})},e.prototype.getKeys=function(){for(var e=[],t=this._entries&&this._entries.first;t;)e.push(t.key),t=t.next;return e},e.prototype.getValues=function(){for(var e=[],t=this._entries&&this._entries.first;t;)e.push(t.value),t=t.next;return e},e}(DictionaryBase_1.default);exports.Dictionary=Dictionary,exports.default=Dictionary;
//# sourceMappingURL=Dictionary.js.map