Skip to content

Commit 256dcc0

Browse files
committed
Merge branch 'main' into 6.x
2 parents 13fc231 + 4cde1da commit 256dcc0

File tree

4 files changed

+117
-42
lines changed

4 files changed

+117
-42
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: [jdeniau, Methuselah96]
1+
github: [jdeniau]

src/CollectionHelperMethods.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import type { Collection } from '../type-definitions/immutable';
2+
import assertNotInfinite from './utils/assertNotInfinite';
3+
4+
export function reduce(
5+
collection: Collection<unknown, unknown>,
6+
reducer: (...args: unknown[]) => unknown,
7+
reduction: unknown,
8+
context: unknown,
9+
useFirst: boolean,
10+
reverse: boolean
11+
) {
12+
// @ts-expect-error Migrate to CollectionImpl in v6
13+
assertNotInfinite(collection.size);
14+
// @ts-expect-error Migrate to CollectionImpl in v6
15+
collection.__iterate((v, k, c) => {
16+
if (useFirst) {
17+
useFirst = false;
18+
reduction = v;
19+
} else {
20+
reduction = reducer.call(context, reduction, v, k, c);
21+
}
22+
}, reverse);
23+
return reduction;
24+
}
25+
26+
export function keyMapper<K, V>(v: V, k: K): K {
27+
return k;
28+
}
29+
30+
export function entryMapper<K, V>(v: V, k: K): [K, V] {
31+
return [k, v];
32+
}
33+
34+
export function not(predicate: (...args: unknown[]) => boolean) {
35+
return function (this: unknown, ...args: unknown[]): boolean {
36+
return !predicate.apply(this, args);
37+
};
38+
}
39+
40+
export function neg(predicate: (...args: unknown[]) => number) {
41+
return function (this: unknown, ...args: unknown[]): number {
42+
return -predicate.apply(this, args);
43+
};
44+
}
45+
46+
export function defaultNegComparator(
47+
a: number | string,
48+
b: number | string
49+
): number {
50+
return a < b ? 1 : a > b ? -1 : 0;
51+
}

src/CollectionImpl.js

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ import {
55
KeyedCollectionImpl,
66
SetCollectionImpl,
77
} from './Collection';
8+
import {
9+
defaultNegComparator,
10+
entryMapper,
11+
keyMapper,
12+
neg,
13+
not,
14+
reduce,
15+
} from './CollectionHelperMethods';
816
import { ITERATE_KEYS, ITERATE_VALUES, Iterator } from './Iterator';
917
import { List } from './List';
1018
import { Map } from './Map';
@@ -56,10 +64,11 @@ import { getIn } from './methods/getIn';
5664
import { hasIn } from './methods/hasIn';
5765
import { toObject } from './methods/toObject';
5866
import { IS_COLLECTION_SYMBOL } from './predicates/isCollection';
59-
import { IS_INDEXED_SYMBOL, isIndexed } from './predicates/isIndexed';
60-
import { IS_KEYED_SYMBOL, isKeyed } from './predicates/isKeyed';
67+
import { isIndexed, IS_INDEXED_SYMBOL } from './predicates/isIndexed';
68+
import { isKeyed, IS_KEYED_SYMBOL } from './predicates/isKeyed';
6169
import { IS_ORDERED_SYMBOL } from './predicates/isOrdered';
6270
import { toJS } from './toJS';
71+
import arrCopy from './utils/arrCopy';
6372
import assertNotInfinite from './utils/assertNotInfinite';
6473
import mixin from './utils/mixin';
6574
import quoteString from './utils/quoteString';
@@ -691,43 +700,6 @@ mixin(SetSeqImpl, SetCollectionPrototype);
691700

692701
// #pragma Helper functions
693702

694-
function reduce(collection, reducer, reduction, context, useFirst, reverse) {
695-
assertNotInfinite(collection.size);
696-
collection.__iterate((v, k, c) => {
697-
if (useFirst) {
698-
useFirst = false;
699-
reduction = v;
700-
} else {
701-
reduction = reducer.call(context, reduction, v, k, c);
702-
}
703-
}, reverse);
704-
return reduction;
705-
}
706-
707-
function keyMapper(v, k) {
708-
return k;
709-
}
710-
711-
function entryMapper(v, k) {
712-
return [k, v];
713-
}
714-
715-
function not(predicate) {
716-
return function () {
717-
return !predicate.apply(this, arguments);
718-
};
719-
}
720-
721-
function neg(predicate) {
722-
return function () {
723-
return -predicate.apply(this, arguments);
724-
};
725-
}
726-
727-
function defaultZipper(...values) {
728-
return values;
729-
}
730-
731-
function defaultNegComparator(a, b) {
732-
return a < b ? 1 : a > b ? -1 : 0;
703+
function defaultZipper() {
704+
return arrCopy(arguments);
733705
}

src/utils/hasCollection.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import type { Collection } from '../../type-definitions/immutable';
2+
import { hash } from '../Hash';
3+
import { imul, smi } from '../Math';
4+
import { isKeyed } from '../predicates/isKeyed';
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);
41+
h = imul((h << 15) | (h >>> -15), 0x1b873593);
42+
h = imul((h << 13) | (h >>> -13), 5);
43+
h = ((h + 0xe6546b64) | 0) ^ size;
44+
h = imul(h ^ (h >>> 16), 0x85ebca6b);
45+
h = imul(h ^ (h >>> 13), 0xc2b2ae35);
46+
h = smi(h ^ (h >>> 16));
47+
return h;
48+
}
49+
50+
function hashMerge(a: number, b: number): number {
51+
return (a ^ (b + 0x9e3779b9 + (a << 6) + (a >> 2))) | 0; // int
52+
}

0 commit comments

Comments
 (0)