-
Notifications
You must be signed in to change notification settings - Fork 1
/
power_set.ts
60 lines (59 loc) · 1.44 KB
/
power_set.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// This module is browser compatible.
/**
* The set of all subsets of the given `iterable`. Equivalent to running
* `combinations` with `0 <= r <= iterable.length` and flattening the results. The
* first subset is the empty set given when `r = 0`.
*
* ```ts
* import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
* import { powerSet } from "https://deno.land/x/combinatorics/mod.ts";
*
* const sequences = [...powerSet([1, 2, 3])];
*
* assertEquals(sequences, [
* [],
* [1],
* [2],
* [3],
* [1, 2],
* [1, 3],
* [2, 3],
* [1, 2, 3],
* ]);
* ```
*/
export function* powerSet<T>(iterable: Iterable<T>): Generator<T[]> {
const pool = [...iterable];
const n = pool.length;
const indices = new Uint32Array(n);
for (let r = 0; r <= n; r++) {
const result: T[] = Array(r);
for (let i = 0; i < r; i++) {
indices[i] = i;
result[i] = pool[i];
}
yield result;
while (true) {
let i: number;
loop: {
for (i = r - 1; i >= 0; i--) {
if (indices[i] !== i + n - r) {
break loop;
}
}
break;
}
const result: T[] = Array(r);
for (let j = 0; j < i; j++) {
result[j] = pool[indices[j]];
}
let index = indices[i] += 1;
result[i] = pool[index];
for (let j = i + 1; j < r; j++) {
indices[j] = index += 1;
result[j] = pool[index];
}
yield result;
}
}
}