New upstream release
[sgt-puzzles/ydirson.git] / combi.c
blob4c5d1077aa07ae6b83e5a550b4cb6649abe54597
1 #include <assert.h>
2 #include <string.h>
4 #include "puzzles.h"
6 /* horrific and doesn't check overflow. */
7 static long factx(long x, long y)
9 long acc = 1, i;
11 for (i = y; i <= x; i++)
12 acc *= i;
13 return acc;
16 void reset_combi(combi_ctx *combi)
18 int i;
19 combi->nleft = combi->total;
20 for (i = 0; i < combi->r; i++)
21 combi->a[i] = i;
24 combi_ctx *new_combi(int r, int n)
26 long nfr, nrf;
27 combi_ctx *combi;
29 assert(r <= n);
30 assert(n >= 1);
32 combi = snew(combi_ctx);
33 memset(combi, 0, sizeof(combi_ctx));
34 combi->r = r;
35 combi->n = n;
37 combi->a = snewn(r, int);
38 memset(combi->a, 0, r * sizeof(int));
40 nfr = factx(n, r+1);
41 nrf = factx(n-r, 1);
42 combi->total = (int)(nfr / nrf);
44 reset_combi(combi);
45 return combi;
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)
54 goto done;
55 else if (combi->nleft <= 0)
56 return NULL;
58 while (combi->a[i] == combi->n - combi->r + i)
59 i--;
60 combi->a[i] += 1;
61 for (j = i+1; j < combi->r; j++)
62 combi->a[j] = combi->a[i] + j - i;
64 done:
65 combi->nleft--;
66 return combi;
69 void free_combi(combi_ctx *combi)
71 sfree(combi->a);
72 sfree(combi);
75 /* compile this with:
76 * gcc -o combi.exe -DSTANDALONE_COMBI_TEST combi.c malloc.c
78 #ifdef STANDALONE_COMBI_TEST
80 #include <stdio.h>
82 void fatal(char *fmt, ...)
84 abort();
87 int main(int argc, char *argv[])
89 combi_ctx *c;
90 int i, r, n;
92 if (argc < 3) {
93 fprintf(stderr, "Usage: combi R N\n");
94 exit(1);
97 r = atoi(argv[1]); n = atoi(argv[2]);
98 c = new_combi(r, n);
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]);
105 printf("\n");
107 free_combi(c);
110 #endif