Debian Codegraph @

external/gsl/gsl_combination__init.c

raw symbols SCIP
1 /* combination/init.c
2 * based on permutation/init.c by Brian Gough
3 *
4 * Copyright (C) 2001 Szymon Jaroszewicz
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include "gsl__config.h"
22 #include <stdlib.h>
23 #include "gsl_errno.h"
24 #include "gsl_combination.h"
25
26 gsl_combination *
27 gsl_combination_alloc (const size_t n, const size_t k)
28 {
29 gsl_combination * c;
30
31 if (n == 0)
32 {
33 GSL_ERROR_VAL ("combination parameter n must be positive integer",
34 GSL_EDOM, 0);
35 }
36 if (k > n)
37 {
38 GSL_ERROR_VAL ("combination length k must be an integer less than or equal to n",
39 GSL_EDOM, 0);
40 }
41 c = (gsl_combination *) malloc (sizeof (gsl_combination));
42
43 if (c == 0)
44 {
45 GSL_ERROR_VAL ("failed to allocate space for combination struct",
46 GSL_ENOMEM, 0);
47 }
48
49 if (k > 0)
50 {
51 c->data = (size_t *) malloc (k * sizeof (size_t));
52
53 if (c->data == 0)
54 {
55 free (c); /* exception in constructor, avoid memory leak */
56
57 GSL_ERROR_VAL ("failed to allocate space for combination data",
58 GSL_ENOMEM, 0);
59 }
60 }
61 else
62 {
63 c->data = 0;
64 }
65
66 c->n = n;
67 c->k = k;
68
69 return c;
70 }
71
72 gsl_combination *
73 gsl_combination_calloc (const size_t n, const size_t k)
74 {
75 size_t i;
76
77 gsl_combination * c = gsl_combination_alloc (n, k);
78
79 if (c == 0)
80 return 0;
81
82 /* initialize combination to identity */
83
84 for (i = 0; i < k; i++)
85 {
86 c->data[i] = i;
87 }
88
89 return c;
90 }
91
92 void
93 gsl_combination_init_first (gsl_combination * c)
94 {
95 const size_t k = c->k ;
96 size_t i;
97
98 /* initialize combination to identity */
99
100 for (i = 0; i < k; i++)
101 {
102 c->data[i] = i;
103 }
104 }
105
106 void
107 gsl_combination_init_last (gsl_combination * c)
108 {
109 const size_t k = c->k ;
110 size_t i;
111 size_t n = c->n;
112
113 /* initialize combination to identity */
114
115 for (i = 0; i < k; i++)
116 {
117 c->data[i] = n - k + i;
118 }
119 }
120
121 void
122 gsl_combination_free (gsl_combination * c)
123 {
124 if (c->k > 0) free (c->data);
125 free (c);
126 }