2 * based on permutation/init.c by Brian Gough
4 * Copyright (C) 2001 Szymon Jaroszewicz
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <gsl/gsl_errno.h>
24 #include <gsl/gsl_combination.h>
27 gsl_combination_alloc (const size_t n, const size_t k)
33 GSL_ERROR_VAL ("combination parameter n must be positive integer",
38 GSL_ERROR_VAL ("combination length k must be an integer less than or equal to n",
41 c = (gsl_combination *) malloc (sizeof (gsl_combination));
45 GSL_ERROR_VAL ("failed to allocate space for combination struct",
51 c->data = (size_t *) malloc (k * sizeof (size_t));
55 free (c); /* exception in constructor, avoid memory leak */
57 GSL_ERROR_VAL ("failed to allocate space for combination data",
73 gsl_combination_calloc (const size_t n, const size_t k)
77 gsl_combination * c = gsl_combination_alloc (n, k);
82 /* initialize combination to identity */
84 for (i = 0; i < k; i++)
93 gsl_combination_init_first (gsl_combination * c)
95 const size_t k = c->k ;
98 /* initialize combination to identity */
100 for (i = 0; i < k; i++)
107 gsl_combination_init_last (gsl_combination * c)
109 const size_t k = c->k ;
113 /* initialize combination to identity */
115 for (i = 0; i < k; i++)
117 c->data[i] = n - k + i;
122 gsl_combination_free (gsl_combination * c)
124 if (c->k > 0) free (c->data);