Skip to content

Commit 8f84633

Browse files
committed
add link to Gosper's hack
1 parent 8da361e commit 8f84633

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

packages/tailwindcss/src/canonicalize-candidates.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,21 +1812,25 @@ function optimizeModifier(candidate: Candidate, options: InternalCanonicalizeOpt
18121812
// 2. Sets of size 1 and 0 are not yielded
18131813
function* combinations<T>(arr: T[]): Generator<T[]> {
18141814
let n = arr.length
1815+
let limit = 1n << BigInt(n)
18151816

18161817
for (let k = n; k >= 2; k--) {
18171818
let mask = (1n << BigInt(k)) - 1n
1818-
let limit = 1n << BigInt(n)
18191819

18201820
while (mask < limit) {
1821-
let out = new Array<T>(k)
1822-
let p = 0
1821+
let out = []
18231822
for (let i = 0; i < n; i++) {
18241823
if ((mask >> BigInt(i)) & 1n) {
1825-
out[p++] = arr[i]
1824+
out.push(arr[i])
18261825
}
18271826
}
18281827
yield out
18291828

1829+
// Gosper's hack:
1830+
// - https://programmingforinsomniacs.blogspot.com/2018/03/gospers-hack-explained.html
1831+
// - https://rosettacode.org/wiki/Gosper%27s_hack
1832+
//
1833+
// We need to generate the next mask in lexicographical order.
18301834
let carry = mask & -mask
18311835
let ripple = mask + carry
18321836
mask = (((ripple ^ mask) >> 2n) / carry) | ripple

0 commit comments

Comments
 (0)