MCPcopy Index your code
hub / github.com/immutable-js/immutable-js / hashCollection

Function hashCollection

src/utils/hasCollection.ts:7–37  ·  view source on GitHub ↗
(collection: Collection<K, V>)

Source from the content-addressed store, hash-verified

5import { isOrdered } from '../predicates/isOrdered';
6
7export 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
39function murmurHashOfSize(size: number, h: number): number {
40 h = imul(h, 0xcc9e2d51);

Callers 1

hashCodeFunction · 0.90

Calls 6

isOrderedFunction · 0.90
isKeyedFunction · 0.90
hashFunction · 0.90
hashMergeFunction · 0.85
murmurHashOfSizeFunction · 0.85
__iterateMethod · 0.45

Tested by

no test coverage detected