// SPDX-License-Identifier: GPL-2.0-or-later // Copyright (C) 2013 Andrea Mazzoleni #ifndef RAID_COMBO_H #define RAID_COMBO_H #include /** * Get the first permutation with repetition of r of n elements. * * Typical use is with permutation_next() in the form: * * int i[R]; * permutation_first(R, N, i); * do { * code using i[0], i[1], ..., i[R-1] * } while (permutation_next(R, N, i)); * * It's equivalent to the code: * * for(i[0]=0;i[0]= n) { /* if we are at the first level, we have finished */ if (i == 0) return 0; /* increase the previous position */ --i; goto recurse; } ++i; /* initialize all the next positions, if any */ while (i < r) { c[i] = 0; ++i; } return 1; } /** * Get the first combination without repetition of r of n elements. * * Typical use is with combination_next() in the form: * * int i[R]; * combination_first(R, N, i); * do { * code using i[0], i[1], ..., i[R-1] * } while (combination_next(R, N, i)); * * It's equivalent to the code: * * for(i[0]=0;i[0]= h) { /* if we are at the first level, we have finished */ if (i == 0) return 0; /* increase the previous position */ --i; --h; goto recurse; } ++i; /* initialize all the next positions, if any */ while (i < r) { /* each position starts at the next value of the previous one */ c[i] = c[i - 1] + 1; ++i; } return 1; } #endif