Skip to content

Instantly share code, notes, and snippets.

@AndreVallestero
Last active June 29, 2021 23:42
Show Gist options
  • Save AndreVallestero/98a64e4f2a96a8c3544c608a04c3b7b1 to your computer and use it in GitHub Desktop.
Save AndreVallestero/98a64e4f2a96a8c3544c608a04c3b7b1 to your computer and use it in GitHub Desktop.
Chunk generate combinations for multi threaded combination generation
/*
* n Number of elements in the pool
* k Number of elements to choose for each combination, k <= n
* init_index The starting index, init_index + k <= n
*/
fn chunk_generate_combinations(n: usize, k: usize, init_index: usize) {
let mut indices: Vec<usize> = (init_index..init_index + k).collect();
let k_sub_1 = k - 1;
let n_sub_k = n - k;
while indices[0] == init_index && indices[k_sub_1] < n {
dbg!(&indices);
// generate next combination in lexicographic order
let mut t = k_sub_1;
while t != 0 && indices[t] == n_sub_k + t {
t -= 1;
}
indices[t] += 1;
for j in t + 1..k {
indices[j] = indices[j - 1] + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment