6 /* horrific and doesn't check overflow. */
7 static long factx(long x, long y)
11 for (i = y; i <= x; i++)
16 void reset_combi(combi_ctx *combi)
19 combi->nleft = combi->total;
20 for (i = 0; i < combi->r; i++)
24 combi_ctx *new_combi(int r, int n)
32 combi = snew(combi_ctx);
33 memset(combi, 0, sizeof(combi_ctx));
37 combi->a = snewn(r, int);
38 memset(combi->a, 0, r * sizeof(int));
42 combi->total = (int)(nfr / nrf);
48 /* returns NULL when we're done otherwise returns input. */
49 combi_ctx *next_combi(combi_ctx *combi)
51 int i = combi->r - 1, j;
53 if (combi->nleft == combi->total)
55 else if (combi->nleft <= 0)
58 while (combi->a[i] == combi->n - combi->r + i)
61 for (j = i+1; j < combi->r; j++)
62 combi->a[j] = combi->a[i] + j - i;
69 void free_combi(combi_ctx *combi)
76 * gcc -o combi.exe -DSTANDALONE_COMBI_TEST combi.c malloc.c
78 #ifdef STANDALONE_COMBI_TEST
82 void fatal(char *fmt, ...)
87 int main(int argc, char *argv[])
93 fprintf(stderr, "Usage: combi R N\n");
97 r = atoi(argv[1]); n = atoi(argv[2]);
99 printf("combi %d of %d, %d elements.\n", c->r, c->n, c->total);
101 while (next_combi(c)) {
102 for (i = 0; i < c->r; i++) {
103 printf("%d ", c->a[i]);