Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add link to Gosper's hack
  • Loading branch information
RobinMalfait committed Oct 20, 2025
commit 8f8463355875dca86e449d8a76f528014e438070
12 changes: 8 additions & 4 deletions packages/tailwindcss/src/canonicalize-candidates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1812,21 +1812,25 @@ function optimizeModifier(candidate: Candidate, options: InternalCanonicalizeOpt
// 2. Sets of size 1 and 0 are not yielded
function* combinations<T>(arr: T[]): Generator<T[]> {
let n = arr.length
let limit = 1n << BigInt(n)

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

while (mask < limit) {
let out = new Array<T>(k)
let p = 0
let out = []
for (let i = 0; i < n; i++) {
if ((mask >> BigInt(i)) & 1n) {
out[p++] = arr[i]
out.push(arr[i])
}
}
yield out

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