diff options
author | Louis Pilfold <louis@lpil.uk> | 2024-11-27 23:01:05 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2024-11-27 23:01:05 +0000 |
commit | b8785ed40b0e97ba0ce304fb728695ac50655d37 (patch) | |
tree | c57caf63d1b363a0c4ebfacac8dcff512e4c8c4b /src/dict.mjs | |
parent | b0afb8f5522ab96b54580dfe94ec045da0f758aa (diff) | |
download | gleam_stdlib-b8785ed40b0e97ba0ce304fb728695ac50655d37.tar.gz gleam_stdlib-b8785ed40b0e97ba0ce304fb728695ac50655d37.zip |
Format
Diffstat (limited to 'src/dict.mjs')
-rw-r--r-- | src/dict.mjs | 28 |
1 files changed, 24 insertions, 4 deletions
diff --git a/src/dict.mjs b/src/dict.mjs index a8309e0..950037e 100644 --- a/src/dict.mjs +++ b/src/dict.mjs @@ -25,6 +25,7 @@ function hashByReference(o) { referenceMap.set(o, hash); return hash; } + /** * merge two hashes in an order sensitive way * @param {number} a @@ -34,6 +35,7 @@ function hashByReference(o) { function hashMerge(a, b) { return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; } + /** * standard string hash popularised by java * @param {string} s @@ -47,6 +49,7 @@ function hashString(s) { } return hash; } + /** * hash a number by converting to two integers and do some jumbling * @param {number} n @@ -58,6 +61,7 @@ function hashNumber(n) { const j = tempDataView.getInt32(4); return Math.imul(0x45d9f3b, (i >> 16) ^ i) ^ j; } + /** * hash a BigInt by converting it to a string and hashing that * @param {BigInt} n @@ -66,6 +70,7 @@ function hashNumber(n) { function hashBigInt(n) { return hashString(n.toString()); } + /** * hash any js object * @param {any} o @@ -113,6 +118,7 @@ function hashObject(o) { } return h; } + /** * hash any js value * @param {any} u @@ -140,6 +146,7 @@ export function getHash(u) { return 0; // should be unreachable } } + /** * @template K,V * @typedef {ArrayNode<K,V> | IndexNode<K,V> | CollisionNode<K,V>} Node @@ -172,6 +179,7 @@ const ENTRY = 0; const ARRAY_NODE = 1; const INDEX_NODE = 2; const COLLISION_NODE = 3; + /** @type {IndexNode<any,any>} */ const EMPTY = { type: INDEX_NODE, @@ -187,6 +195,7 @@ const EMPTY = { function mask(hash, shift) { return (hash >>> shift) & MASK; } + /** * Set only the Nth bit where N is the masked hash * @param {number} hash @@ -196,6 +205,7 @@ function mask(hash, shift) { function bitpos(hash, shift) { return 1 << mask(hash, shift); } + /** * Count the number of 1 bits in a number * @param {number} x @@ -209,6 +219,7 @@ function bitcount(x) { x += x >> 16; return x & 0x7f; } + /** * Calculate the array index of an item in a bitmap index node * @param {number} bitmap @@ -218,6 +229,7 @@ function bitcount(x) { function index(bitmap, bit) { return bitcount(bitmap & (bit - 1)); } + /** * Efficiently copy an array and set one value at an index * @template T @@ -235,6 +247,7 @@ function cloneAndSet(arr, at, val) { out[at] = val; return out; } + /** * Efficiently copy an array and insert one value at an index * @template T @@ -257,6 +270,7 @@ function spliceIn(arr, at, val) { } return out; } + /** * Efficiently copy an array and remove one value at an index * @template T @@ -278,6 +292,7 @@ function spliceOut(arr, at) { } return out; } + /** * Create a new node containing two entries * @template K,V @@ -308,9 +323,10 @@ function createNode(shift, key1, val1, key2hash, key2, val2) { key2hash, key2, val2, - addedLeaf + addedLeaf, ); } + /** * @template T,K,V * @callback AssocFunction @@ -377,7 +393,7 @@ function assocArray(root, shift, hash, key, val, addedLeaf) { array: cloneAndSet( root.array, idx, - createNode(shift + SHIFT, node.k, node.v, hash, key, val) + createNode(shift + SHIFT, node.k, node.v, hash, key, val), ), }; } @@ -441,7 +457,7 @@ function assocIndex(root, shift, hash, key, val, addedLeaf) { array: cloneAndSet( root.array, idx, - createNode(shift + SHIFT, nodeKey, node.v, hash, key, val) + createNode(shift + SHIFT, nodeKey, node.v, hash, key, val), ), }; } else { @@ -528,7 +544,7 @@ function assocCollision(root, shift, hash, key, val, addedLeaf) { hash, key, val, - addedLeaf + addedLeaf, ); } /** @@ -813,6 +829,7 @@ function forEach(root, fn) { forEach(item, fn); } } + /** * Extra wrapper to keep track of Dict size and clean up the API * @template K,V @@ -833,6 +850,7 @@ export default class Dict { } return m; } + /** * @template K,V * @param {Map<K,V>} o @@ -846,9 +864,11 @@ export default class Dict { }); return m; } + static new() { return new Dict(undefined, 0); } + /** * @param {undefined | Node<K,V>} root * @param {number} size |