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 | |
parent | b0afb8f5522ab96b54580dfe94ec045da0f758aa (diff) | |
download | gleam_stdlib-b8785ed40b0e97ba0ce304fb728695ac50655d37.tar.gz gleam_stdlib-b8785ed40b0e97ba0ce304fb728695ac50655d37.zip |
Format
-rw-r--r-- | src/dict.mjs | 28 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 16 |
2 files changed, 33 insertions, 11 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 diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 06302bf..dc57c9c 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -47,13 +47,13 @@ export function to_string(term) { } export function float_to_string(float) { - const string = float.toString().replace('+', ''); + const string = float.toString().replace("+", ""); if (string.indexOf(".") >= 0) { return string; } else { const index = string.indexOf("e"); if (index >= 0) { - return string.slice(0, index) + '.0' + string.slice(index); + return string.slice(0, index) + ".0" + string.slice(index); } else { return string + ".0"; } @@ -186,7 +186,7 @@ export function pop_grapheme(string) { } export function pop_codeunit(str) { - return [str.charCodeAt(0)|0, str.slice(1)] + return [str.charCodeAt(0) | 0, str.slice(1)]; } export function lowercase(string) { @@ -256,12 +256,15 @@ export function string_slice(string, idx, len) { return result; } else { - return string.match(/./gsu).slice(idx, idx + len).join(""); + return string + .match(/./gsu) + .slice(idx, idx + len) + .join(""); } } export function string_codeunit_slice(str, from, length) { - return str.slice(from, from + length) + return str.slice(from, from + length); } export function crop_string(string, substring) { return string.substring(string.indexOf(substring)); @@ -305,7 +308,7 @@ const unicode_whitespaces = [ const trim_start_regex = new RegExp(`^[${unicode_whitespaces}]*`); const trim_end_regex = new RegExp(`[${unicode_whitespaces}]*$`); const trim_regex = new RegExp( - `^[${unicode_whitespaces}]*(.*?)[${unicode_whitespaces}]*$` + `^[${unicode_whitespaces}]*(.*?)[${unicode_whitespaces}]*$`, ); export function trim(string) { @@ -1014,4 +1017,3 @@ export function bit_array_starts_with(bits, prefix) { return true; } - |