/
githubmirror
/
immutable-js
Обзор
Документация
Войти
/
githubmirror
/
immutable-js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/utils/hasCollection.ts
52 строки
2 KB
Julien Deniau
Extract CollectionHelperMethods
01 июл 2025, 01:40
01 июл 2025, 01:40
d613de2
Код
Авторство
О чём код?
import type { Collection } from '../../type-definitions/immutable'; import { hash } from '../Hash'; import { imul, smi } from '../Math'; import { isKeyed } from '../predicates/isKeyed'; import { isOrdered } from '../predicates/isOrdered'; export function hashCollection<K, V>(collection: Collection<K, V>): number { // @ts-expect-error Migrate to CollectionImpl in v6 if (collection.size === Infinity) { return 0; } const ordered = isOrdered(collection); const keyed = isKeyed(collection); let h: number = ordered ? 1 : 0; // @ts-expect-error Migrate to CollectionImpl in v6 collection.__iterate( keyed ? ordered ? (v: V, k: K): void => { h = (31 * h + hashMerge(hash(v), hash(k))) | 0; } : (v: V, k: K): void => { h = (h + hashMerge(hash(v), hash(k))) | 0; } : ordered ? (v: V): void => { h = (31 * h + hash(v)) | 0; } : (v: V): void => { h = (h + hash(v)) | 0; } ); // @ts-expect-error Migrate to CollectionImpl in v6 return murmurHashOfSize(collection.size, h); } function murmurHashOfSize(size: number, h: number): number { h = imul(h, 0xcc9e2d51); h = imul((h << 15) | (h >>> -15), 0x1b873593); h = imul((h << 13) | (h >>> -13), 5); h = ((h + 0xe6546b64) | 0) ^ size; h = imul(h ^ (h >>> 16), 0x85ebca6b); h = imul(h ^ (h >>> 13), 0xc2b2ae35); h = smi(h ^ (h >>> 16)); return h; } function hashMerge(a: number, b: number): number { return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; // int }