From 240db63ea29f10810c4b55b1a9674e4aa8283d05 Mon Sep 17 00:00:00 2001 From: Julian Schurhammer Date: Thu, 11 Aug 2022 23:57:14 +1200 Subject: implement hashCode for maps in javascript --- src/persistent-hash-map.mjs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'src') 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() { -- cgit v1.2.3