diff options
author | Julian Schurhammer <julian.schurhammer@gmail.com> | 2022-08-11 23:57:14 +1200 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2023-03-13 10:48:28 +0000 |
commit | 240db63ea29f10810c4b55b1a9674e4aa8283d05 (patch) | |
tree | 6884fc2522769130db9375a76be2382193e4506a /src | |
parent | 92cbe1595c1359ab770e4ed3407dd7557e0f5f08 (diff) | |
download | gleam_stdlib-240db63ea29f10810c4b55b1a9674e4aa8283d05.tar.gz gleam_stdlib-240db63ea29f10810c4b55b1a9674e4aa8283d05.zip |
implement hashCode for maps in javascript
Diffstat (limited to 'src')
-rw-r--r-- | src/persistent-hash-map.mjs | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/persistent-hash-map.mjs b/src/persistent-hash-map.mjs index 8cdc023..c2a792c 100644 --- a/src/persistent-hash-map.mjs +++ b/src/persistent-hash-map.mjs @@ -568,7 +568,7 @@ function withoutCollision(root, _shift, _hash, key) { array: spliceOut(root.array, idx), }; } -function toArray(root, result) { +function forEach(root, fn) { if (root === undefined) { return; } @@ -580,10 +580,10 @@ function toArray(root, result) { continue; } if (item.type === ENTRY) { - result.push([item.k, item.v]); + fn(item.v, item.k); continue; } - toArray(item, result); + forEach(item, fn); } } /** Extra wrapper to keep track of map size */ @@ -592,6 +592,13 @@ export class PMap { this.root = root; this.size = size; } + hashCode() { + let h = 0; + forEach(this, (v, k) => { + h = (h + hashMerge(getHash(v), getHash(k))) | 0; + }); + return h; + } } export function create() { return new PMap(undefined, 0); @@ -645,7 +652,7 @@ export function entries(map) { return []; } const result = []; - toArray(map.root, result); + forEach(map.root, (v, k) => result.push([k, v])); return result; } export function __include_me() { |