Skip to content

Commit

Permalink
Format
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed Nov 27, 2024
1 parent b0afb8f commit b8785ed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
28 changes: 24 additions & 4 deletions src/dict.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function hashByReference(o) {
referenceMap.set(o, hash);
return hash;
}

/**
* merge two hashes in an order sensitive way
* @param {number} a
Expand All @@ -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
Expand All @@ -47,6 +49,7 @@ function hashString(s) {
}
return hash;
}

/**
* hash a number by converting to two integers and do some jumbling
* @param {number} n
Expand All @@ -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
Expand All @@ -66,6 +70,7 @@ function hashNumber(n) {
function hashBigInt(n) {
return hashString(n.toString());
}

/**
* hash any js object
* @param {any} o
Expand Down Expand Up @@ -113,6 +118,7 @@ function hashObject(o) {
}
return h;
}

/**
* hash any js value
* @param {any} u
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -257,6 +270,7 @@ function spliceIn(arr, at, val) {
}
return out;
}

/**
* Efficiently copy an array and remove one value at an index
* @template T
Expand All @@ -278,6 +292,7 @@ function spliceOut(arr, at) {
}
return out;
}

/**
* Create a new node containing two entries
* @template K,V
Expand Down Expand Up @@ -308,9 +323,10 @@ function createNode(shift, key1, val1, key2hash, key2, val2) {
key2hash,
key2,
val2,
addedLeaf
addedLeaf,
);
}

/**
* @template T,K,V
* @callback AssocFunction
Expand Down Expand Up @@ -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),
),
};
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -528,7 +544,7 @@ function assocCollision(root, shift, hash, key, val, addedLeaf) {
hash,
key,
val,
addedLeaf
addedLeaf,
);
}
/**
Expand Down Expand Up @@ -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
Expand All @@ -833,6 +850,7 @@ export default class Dict {
}
return m;
}

/**
* @template K,V
* @param {Map<K,V>} o
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -1014,4 +1017,3 @@ export function bit_array_starts_with(bits, prefix) {

return true;
}

0 comments on commit b8785ed

Please sign in to comment.