All files / src/Ice HashMap.js

83.14% Statements 296/356
87.14% Branches 61/70
76% Functions 19/25
83.14% Lines 296/356

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 35741x 41x 41x 41x 41x 41x 1234x 1234x 1234x 1234x 1234x 88x 7x 7x 7x 7x 7x 7x 88x 1227x 1227x 1227x 1227x 1227x 1227x 1234x 41x 4820x 4820x 1775x 1775x 4820x     3045x 4820x 41x       41x 41x 41x 440x 440x 440x 440x 436x 440x 4x 4x 4x 440x 440x 440x 440x 440x 440x 440x 440x 440x 440x 4x 4x 4x 128x 128x 4x 4x 8x 8x 440x 436x 436x 13952x 13952x 436x 440x 41x 41x 1234x 1234x 1234x 1234x 1234x 1234x 41x 41x 5581x 5581x 5581x 5581x 41x 41x 2698x 2698x 2698x 41x 41x 1051x 1051x 1051x 1051x 1051x 1051x 1051x 1051x 1051x 1061x 1049x 1049x 1049x 1049x 1049x 1049x 1049x 1049x 1049x 8x 1049x 1041x 1041x 1049x 1049x 1049x 1049x 1049x 385x 385x 1049x 470x 470x 1049x 1049x 664x 664x 1049x 1049x 1049x 12x 12x 12x 2x 2x 1051x 41x 41x 235x 7520x 7520x 235x 235x 235x 41x 41x 4x 4x 12x 12x 4x 41x 41x 46x 106x 106x 46x 41x 41x         41x 41x 280x 282x 282x 280x 41x 41x 1227x 1227x 1227x 1227x 1227x 1227x 1227x   1227x 1227x 1227x 1227x 1227x   1227x 1227x 1227x 1227x 1227x   1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 1227x 715x 715x 1227x 1227x 1227x 1227x     1227x 41x 41x                             41x 41x 8279x 8279x 8279x 8279x 8279x 3907x 3716x 3716x 3907x 4563x 4563x 8279x 41x 41x 10564x 10564x 41x 41x 10564x     10564x 10564x             10564x 10564x     10564x 10564x 10564x 10564x     10564x                                         10564x 41x 41x 4820x 4820x 41x 41x 163x 163x 41x 41x 41x 41x 41x 41x 41x 41x 41x 542x 542x 542x 542x 5x 5x 47x 542x 542x 542x 542x 542x 542x  
// Copyright (c) ZeroC, Inc.
 
import { StringUtil } from "./StringUtil.js";
import { generateUUID } from "./UUID.js";
import { StreamHelpers } from "./StreamHelpers.js";
 
function setInternal(map, key, value, hash, index) {
    //
    // Search for an entry with the same key.
    //
    for (let e = map._table[index]; e !== null; e = e._nextInBucket) {
        if (e._hash === hash && map.keysEqual(key, e._key)) {
            //
            // Found a match, update the value.
            //
            e._value = value;
            return undefined;
        }
    }
 
    //
    // No match found, add a new entry.
    //
    map.add(key, value, hash, index);
    return undefined;
}
 
function compareEquals(v1, v2) {
    if (v1 === v2) {
        return true;
    }
    if (v1 === undefined || v1 === null || v2 === undefined || v2 === null) {
        return false;
    }
    return v1.equals(v2);
}
 
function compareIdentity(v1, v2) {
    return v1 === v2;
}
 
export class HashMap {
    constructor(arg1) {
        // The first argument can be a HashMap or the keyComparator.
        let h, keyComparator;
 
        if (typeof arg1 == "function") {
            keyComparator = arg1;
        } else if (arg1 instanceof HashMap) {
            h = arg1;
            keyComparator = h._keyComparator;
        }
 
        this._size = 0;
        this._head = null;
        this._initialCapacity = 32;
        this._loadFactor = 0.75;
        this._table = [];
 
        this._keyComparator = typeof keyComparator == "function" ? keyComparator : compareIdentity;
 
        if (h instanceof HashMap && h._size > 0) {
            this._threshold = h._threshold;
            this._table.length = h._table.length;
            for (let i = 0; i < h._table.length; i++) {
                this._table[i] = null;
            }
 
            for (const [k, v] of h) {
                this.set(k, v);
            }
        } else {
            this._threshold = this._initialCapacity * this._loadFactor;
            for (let i = 0; i < this._initialCapacity; i++) {
                this._table[i] = null;
            }
        }
    }
 
    set(key, value) {
        const r = this.computeHash(key); // Returns an object with key,hash members.
 
        const index = this.hashIndex(r.hash, this._table.length);
 
        return setInternal(this, r.key, value, r.hash, index);
    }
 
    get(key) {
        const r = this.computeHash(key); // Returns an object with key,hash members.
        const e = this.findEntry(r.key, r.hash);
        return e !== undefined ? e._value : undefined;
    }
 
    has(key) {
        const r = this.computeHash(key); // Returns an object with key,hash members.
        return this.findEntry(r.key, r.hash) !== undefined;
    }
 
    delete(key) {
        const r = this.computeHash(key); // Returns an object with key,hash members.
 
        const index = this.hashIndex(r.hash, this._table.length);
 
        //
        // Search for an entry with the same key.
        //
        let prev = null;
        for (let e = this._table[index]; e !== null; e = e._nextInBucket) {
            if (e._hash === r.hash && this.keysEqual(r.key, e._key)) {
                //
                // Found a match.
                //
                this._size--;
 
                //
                // Remove from bucket.
                //
                if (prev !== null) {
                    prev._nextInBucket = e._nextInBucket;
                } else {
                    this._table[index] = e._nextInBucket;
                }
 
                //
                // Unlink the entry.
                //
                if (e._prev !== null) {
                    e._prev._next = e._next;
                }
                if (e._next !== null) {
                    e._next._prev = e._prev;
                }
 
                if (this._head === e) {
                    this._head = e._next;
                }
 
                return e._value;
            }
 
            prev = e;
        }
 
        return undefined;
    }
 
    clear() {
        for (let i = 0; i < this._table.length; ++i) {
            this._table[i] = null;
        }
        this._head = null;
        this._size = 0;
    }
 
    forEach(fn, obj) {
        obj = obj === undefined ? fn : obj;
        for (let e = this._head; e !== null; e = e._next) {
            fn.call(obj, e._value, e._key);
        }
    }
 
    *entries() {
        for (let e = this._head; e !== null; e = e._next) {
            yield [e._key, e._value];
        }
    }
 
    *keys() {
        for (let e = this._head; e !== null; e = e._next) {
            yield e._key;
        }
    }
 
    *values() {
        for (let e = this._head; e !== null; e = e._next) {
            yield e._value;
        }
    }
 
    add(key, value, hash, index) {
        //
        // Create a new table entry.
        //
        const e = Object.create(null, {
            key: {
                enumerable: true,
                get: function () {
                    return this._key;
                },
            },
            value: {
                enumerable: true,
                get: function () {
                    return this._value;
                },
            },
            next: {
                enumerable: true,
                get: function () {
                    return this._next;
                },
            },
            _key: {
                enumerable: false,
                writable: true,
                value: key,
            },
            _value: {
                enumerable: false,
                writable: true,
                value: value,
            },
            _prev: {
                enumerable: false,
                writable: true,
                value: null,
            },
            _next: {
                enumerable: false,
                writable: true,
                value: null,
            },
            _nextInBucket: {
                enumerable: false,
                writable: true,
                value: null,
            },
            _hash: {
                enumerable: false,
                writable: true,
                value: hash,
            },
        });
        e._nextInBucket = this._table[index];
        this._table[index] = e;
 
        e._next = this._head;
        if (this._head !== null) {
            this._head._prev = e;
        }
        this._head = e;
 
        this._size++;
        if (this._size >= this._threshold) {
            this.resize(this._table.length * 2);
        }
    }
 
    resize(capacity) {
        const newTable = new Array(capacity).fill(null);

        //
        // Re-assign all entries to buckets.
        //
        for (let e = this._head; e !== null; e = e._next) {
            const index = this.hashIndex(e._hash, capacity);
            e._nextInBucket = newTable[index];
            newTable[index] = e;
        }

        this._table = newTable;
        this._threshold = capacity * this._loadFactor;
    }
 
    findEntry(key, hash) {
        const index = this.hashIndex(hash, this._table.length);
        //
        // Search for an entry with the same key.
        //
        for (let e = this._table[index]; e !== null; e = e._nextInBucket) {
            if (e._hash === hash && this.keysEqual(key, e._key)) {
                return e;
            }
        }
 
        return undefined;
    }
 
    hashIndex(hash, len) {
        return hash & (len - 1);
    }
 
    computeHash(v) {
        if (v === 0) {
            return { key: 0, hash: 0 };
        }
 
        if (v === null) {
            if (HashMap._null === null) {
                const uuid = generateUUID();
                HashMap._null = { key: uuid, hash: StringUtil.hashCode(uuid) };
            }
            return HashMap._null;
        }
 
        if (v === undefined) {
            throw new RangeError("cannot compute hash for undefined value");
        }
 
        if (typeof v.hashCode === "function") {
            return { key: v, hash: v.hashCode() };
        }

        const type = typeof v;
        if (type === "string" || v instanceof String) {
            return { key: v, hash: StringUtil.hashCode(v) };
        } else if (type === "number" || v instanceof Number) {
            if (isNaN(v)) {
                if (HashMap._nan === null) {
                    const uuid = generateUUID();
                    HashMap._nan = {
                        key: uuid,
                        hash: StringUtil.hashCode(uuid),
                    };
                }
                return HashMap._nan;
            }
            return { key: v, hash: v.toFixed(0) };
        } else if (type === "boolean" || v instanceof Boolean) {
            return { key: v, hash: v ? 1 : 0 };
        } else if (type === "bigint" || v instanceof BigInt) {
            return { key: v, hash: Number(v).toFixed() };
        }

        throw new RangeError("cannot compute hash for value of type " + type);
    }
 
    keysEqual(k1, k2) {
        return this._keyComparator.call(this._keyComparator, k1, k2);
    }
 
    get size() {
        return this._size;
    }
}
 
HashMap.prototype[Symbol.iterator] = HashMap.prototype.entries;
HashMap.compareEquals = compareEquals;
HashMap.compareIdentity = compareIdentity;
HashMap._null = null;
HashMap._nan = null;
 
export function defineDictionary(keyHelper, valueHelper, fixed, keysEqual, valueType) {
    const dictionaryConstructor =
        keysEqual === undefined
            ? Map
            : // Define a constructor function for a dictionary whose key type requires comparison using an equals() method
              // instead of the native comparison operators.
              function (h) {
                  return new HashMap(h || keysEqual);
              };
    return [
        dictionaryConstructor,
        StreamHelpers.generateDictHelper(keyHelper, valueHelper, fixed, valueType, dictionaryConstructor),
    ];
}