-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombinations.js
164 lines (132 loc) · 3.72 KB
/
combinations.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
const GF = require('gf-js');
/**
* Math product
*
* @param {Array<GeneratorFunction|Array>} iterables Array of (Array | GeneratorFunction)
* @param {Number} repeat number of time repeat
*/
GF.iproduct = function* iproduct(iterables, repeat = 1) {
function *__dot(accumulator, iterable) {
for (let arr of accumulator) {
yield* iterable.map(v => [...arr, v]);
}
}
//convert each iterable to array
let arrays = iterables.map(iterable => Array.isArray(iterable) ? iterable : [...iterable]);
//repeat arrays
let pools = [];
while (repeat--) {
pools = [...pools, ...arrays];
}
let accumulator = [[]];
for (let iterable of pools) {
accumulator = __dot(accumulator, iterable)
}
yield* accumulator;
}
/**
*
* @param {*} iterables
* @param {*} repeat
*/
GF.product = (iterables, repeat = 1) => [...GF.iproduct(iterables, repeat)];
/**
*
* @param {Array} iterable
* @param {Number} r
*/
GF.ipermutations = function* (iterable, r = undefined) {
let pool = Array.isArray(iterable) ? iterable : [...iterable];
let n = pool.length;
r = r || n;
if (r > n) return;
let indices = GF.range(n);
let cycles = GF.range(n, n - r, -1);
yield indices.slice(0, r).map(i => pool[i]);
while (n) {
let i;
for (i = r - 1; i >= 0; i--) {
cycles[i] -= 1;
if (cycles[i] == 0) {
indices = [...indices.slice(0, i), ...indices.slice(i + 1), indices[i]];
cycles[i] = n - i;
} else {
let j = cycles[i];
[indices[i], indices[indices.length - j]] = [indices[indices.length - j], indices[i]];
yield indices.slice(0, r).map(i => pool[i]);
break;
}
}
if (i < 0)
break;
}
}
/**
* Permutations
*
* @param {*} iterable
* @param {*} r
*/
GF.permutations = (iterable, r) => [...GF.ipermutations(iterable, r)]
/**
*
* @param {*} r
*/
GF.prototype.ipermutations = function (r) { return GF.ipermutations(this, r); }
/**
*
* @param {*} r
*/
GF.prototype.permutations = function (r) { returnGF.permutations(this, r); }
/**
*
* @param {*} iterable
* @param {*} r
*/
GF.icombinations = function* (iterable, r = undefined) {
let pool = Array.isArray(iterable) ? iterable : [...iterable];
let n = pool.length;
r = r || n;
if (r > n) return;
let indices = Array.range(r)
yield indices.map(i => pool[i]);
while (true) {
let i;
for (i = r - 1; i >= 0; i--) {
if (indices[i] != i + n - r) break;
}
if (i < 0) break;
indices[i] += 1;
for (let j = i + 1; j < r; j++) {
indices[j] = indices[j - 1] + 1;
}
yield indices.map(i => pool[i]);
}
}
/**
*
* @param {*} iterable
* @param {*} r
*/
GF.combinations = function (iterable, r) { return [...GF.icombinations(iterable, r)]; }
/**
*
* @param {*} r
*/
GF.prototype.icombinations = function (r) { return GF.icombinations(this, r); }
/**
*
* @param {*} r
*/
GF.prototype.combinations = function (r) { return GF.combinations(this, r); }
Array.iproduct = GF.iproduct;
Array.product = GF.product;
Array.ipermutations = GF.ipermutations;
Array.permutations = GF.permutations;
Array.prototype.ipermutations = function (r) { return GF.ipermutations(this, r); }
Array.prototype.permutations = function (r) { return GF.permutations(this, r); }
Array.icombinations = GF.icombinations;
Array.combinations = GF.combinations;
Array.prototype.icombinations = function (r) { return GF.icombinations(this, r); }
Array.prototype.combinations = function (r) { return GF.combinations(this, r); }
module.exports = GF;