/* * Copyright (C) 2013 Andrea Mazzoleni * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef __COMBO_H #define __COMBO_H #include /** * Get the first combination 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 at 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) { c[i] = c[i-1] + 1; /* each position start at the next value of the previous one */ ++i; } return 1; } #endif