| 5 | import { isOrdered } from '../predicates/isOrdered'; |
| 6 | |
| 7 | export function hashCollection<K, V>(collection: Collection<K, V>): number { |
| 8 | // @ts-expect-error Migrate to CollectionImpl in v6 |
| 9 | if (collection.size === Infinity) { |
| 10 | return 0; |
| 11 | } |
| 12 | const ordered = isOrdered(collection); |
| 13 | const keyed = isKeyed(collection); |
| 14 | let h: number = ordered ? 1 : 0; |
| 15 | |
| 16 | // @ts-expect-error Migrate to CollectionImpl in v6 |
| 17 | collection.__iterate( |
| 18 | keyed |
| 19 | ? ordered |
| 20 | ? (v: V, k: K): void => { |
| 21 | h = (31 * h + hashMerge(hash(v), hash(k))) | 0; |
| 22 | } |
| 23 | : (v: V, k: K): void => { |
| 24 | h = (h + hashMerge(hash(v), hash(k))) | 0; |
| 25 | } |
| 26 | : ordered |
| 27 | ? (v: V): void => { |
| 28 | h = (31 * h + hash(v)) | 0; |
| 29 | } |
| 30 | : (v: V): void => { |
| 31 | h = (h + hash(v)) | 0; |
| 32 | } |
| 33 | ); |
| 34 | |
| 35 | // @ts-expect-error Migrate to CollectionImpl in v6 |
| 36 | return murmurHashOfSize(collection.size, h); |
| 37 | } |
| 38 | |
| 39 | function murmurHashOfSize(size: number, h: number): number { |
| 40 | h = imul(h, 0xcc9e2d51); |