Last active
June 29, 2021 23:42
-
-
Save AndreVallestero/98a64e4f2a96a8c3544c608a04c3b7b1 to your computer and use it in GitHub Desktop.
Chunk generate combinations for multi threaded combination generation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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