all files / TypeScript.NET/source/System/Collections/Dictionaries/ Dictionary.js

76.22% Statements 109/143
63.75% Branches 51/80
92% Functions 23/25
77.37% Lines 106/137
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184                        132× 132× 132× 132×     132× 51× 132× 89× 43× 43×   302×   259×   259×   259×     259×       23× 23× 23× 23× 23×   519×   325× 23× 302× 302× 302× 66×     302×   260× 67× 193× 193× 193× 193× 66×   260× 260×   114× 114×   66× 66×                                     66× 66× 66× 66× 66× 66× 66×       14× 14× 43× 43× 43× 43×     14×     20× 15× 15× 15× 15×                                            
/*!
 * @author electricessence / https://github.com/electricessence/
 * Original: http://linqjs.codeplex.com/
 * Licensing: MIT https://github.com/electricessence/TypeScript.NET/blob/master/LICENSE.md
 */
var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
(function (factory) {
    Eif (typeof module === 'object' && typeof module.exports === 'object') {
        var v = factory(require, exports); Iif (v !== undefined) module.exports = v;
    }
    else if (typeof define === 'function' && define.amd) {
        define(["require", "exports", "../../Compare", "../../Types", "../../Functions", "../Enumeration/EnumeratorBase", "../LinkedNodeList", "../../Disposable/ObjectPool", "./DictionaryBase"], factory);
    }
})(function (require, exports) {
    "use strict";
    var Compare_1 = require("../../Compare");
    var Types_1 = require("../../Types");
    var Functions_1 = require("../../Functions");
    var EnumeratorBase_1 = require("../Enumeration/EnumeratorBase");
    var LinkedNodeList_1 = require("../LinkedNodeList");
    var ObjectPool_1 = require("../../Disposable/ObjectPool");
    var DictionaryBase_1 = require("./DictionaryBase");
    var VOID0 = void 0;
    var HashEntry = (function () {
        function HashEntry(key, value, previous, next) {
            this.key = key;
            this.value = value;
            this.previous = previous;
            this.next = next;
        }
        return HashEntry;
    }());
    var linkedListPool;
    function linkedNodeList(recycle) {
        if (!linkedListPool)
            linkedListPool
                = new ObjectPool_1.ObjectPool(20, function () { return new LinkedNodeList_1.LinkedNodeList(); });
        if (!recycle)
            return linkedListPool.take();
        recycle.clear();
        linkedListPool.add(recycle);
    }
    function callHasOwnProperty(target, key) {
        return Object.prototype.hasOwnProperty.call(target, key);
    }
    var NULL = "null", GET_HASH_CODE = "getHashCode";
    function getHashString(obj) {
        Iif (obj === null)
            return NULL;
        Iif (obj === VOID0)
            return Types_1.Type.UNDEFINED;
        Iif (Types_1.Type.hasMemberOfType(obj, GET_HASH_CODE, Types_1.Type.FUNCTION)) {
            return obj.getHashCode();
        }
        return (typeof obj.toString == Types_1.Type.FUNCTION)
            ? obj.toString()
            : Object.prototype.toString.call(obj);
    }
    var Dictionary = (function (_super) {
        __extends(Dictionary, _super);
        function Dictionary(_keyComparer) {
            if (_keyComparer === void 0) { _keyComparer = Functions_1.Functions.Identity; }
            _super.call(this);
            this._keyComparer = _keyComparer;
            this._entries = linkedNodeList();
            this._buckets = {};
        }
        Dictionary.prototype.getCount = function () {
            return this._entries.unsafeCount;
        };
        Dictionary.prototype._getBucket = function (hash, createIfMissing) {
            if (hash === null || hash === VOID0 || !createIfMissing && !this.getCount())
                return null;
            var buckets = this._buckets;
            var bucket = callHasOwnProperty(buckets, hash) ? buckets[hash] : VOID0;
            if (createIfMissing && !bucket)
                buckets[hash]
                    = bucket
                        = linkedNodeList();
            return bucket;
        };
        Dictionary.prototype._getBucketEntry = function (key, hash, bucket) {
            if (key === null || key === VOID0 || !this.getCount())
                return null;
            var _ = this, comparer = _._keyComparer, compareKey = comparer(key);
            Eif (!bucket)
                bucket = _._getBucket(hash || getHashString(compareKey));
            return bucket && bucket
                .find(function (e) { return comparer(e.key) === compareKey; });
        };
        Dictionary.prototype._getEntry = function (key) {
            var e = this._getBucketEntry(key);
            return e && e.value;
        };
        Dictionary.prototype.getValue = function (key) {
            var e = this._getEntry(key);
            return e ? e.value : VOID0;
        };
        Dictionary.prototype._setValueInternal = function (key, value) {
            var _ = this, buckets = _._buckets, entries = _._entries, comparer = _._keyComparer, compareKey = comparer(key), hash = getHashString(compareKey), bucket = _._getBucket(hash), bucketEntry = bucket && _._getBucketEntry(key, hash, bucket);
            Iif (bucketEntry) {
                if (value === VOID0) {
                    var x = bucket.removeNode(bucketEntry), y = entries.removeNode(bucketEntry.value);
                    if (x && !bucket.count) {
                        delete buckets[hash];
                        linkedNodeList(bucket);
                        bucket = null;
                    }
                    if (x !== y)
                        throw "Entries and buckets are out of sync.";
                    if (x)
                        return true;
                }
                else {
                    var old = bucketEntry.value.value;
                    bucketEntry.value.value = value;
                    return !Compare_1.areEqual(value, old);
                }
            }
            else Eif (value !== VOID0) {
                Eif (!bucket)
                    bucket = _._getBucket(hash, true);
                var entry = new HashEntry(key, value);
                entries.addNode(entry);
                bucket.addNode(new HashEntry(key, entry));
                return true;
            }
            return false;
        };
        Dictionary.prototype._clearInternal = function () {
            var _ = this, buckets = _._buckets;
            for (var key in buckets) {
                Eif (buckets.hasOwnProperty(key)) {
                    var bucket = buckets[key];
                    delete buckets[key];
                    linkedNodeList(bucket);
                }
            }
            return _._entries.clear();
        };
        Dictionary.prototype.getEnumerator = function () {
            var _ = this, ver, currentEntry;
            return new EnumeratorBase_1.EnumeratorBase(function () {
                ver = _._version;
                currentEntry = _._entries.first;
            }, function (yielder) {
                if (currentEntry != null) {
                    _.assertVersion(ver);
                    var result = { key: currentEntry.key, value: currentEntry.value };
                    currentEntry = currentEntry.next;
                    return yielder.yieldReturn(result);
                }
                return yielder.yieldBreak();
            });
        };
        Dictionary.prototype.getKeys = function () {
            var _ = this, result = [];
            var e = _._entries.first;
            while (e) {
                result.push(e.key);
                e = e.next;
            }
            return result;
        };
        Dictionary.prototype.getValues = function () {
            var _ = this, result = [];
            var e = _._entries.first;
            while (e) {
                result.push(e.value);
                e = e.next;
            }
            return result;
        };
        return Dictionary;
    }(DictionaryBase_1.default));
    exports.Dictionary = Dictionary;
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.default = Dictionary;
});
//# sourceMappingURL=Dictionary.js.map