File: combination.c

package info (click to toggle)
ruby-gsl 2.1.0.3%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,604 kB
  • sloc: ansic: 62,050; ruby: 15,845; sh: 19; makefile: 10
file content (275 lines) | stat: -rw-r--r-- 8,568 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
  combination.c
  Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
    (C) Copyright 2004 by Yoshiki Tsunesada

  Ruby/GSL is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License.
  This library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.
*/

#include "include/rb_gsl_common.h"
#include "include/rb_gsl_array.h"

static VALUE cgsl_combination_data;

static VALUE rb_gsl_combination_new(VALUE klass, VALUE n, VALUE k)
{
  gsl_combination *c = NULL;
  CHECK_FIXNUM(n); CHECK_FIXNUM(k);
  c = gsl_combination_alloc(FIX2INT(n), FIX2INT(k));
  return Data_Wrap_Struct(klass, 0, gsl_combination_free, c);
}

static VALUE rb_gsl_combination_calloc(VALUE klass, VALUE n, VALUE k)
{
  gsl_combination *c = NULL;
  CHECK_FIXNUM(n); CHECK_FIXNUM(k);
  c = gsl_combination_calloc(FIX2INT(n), FIX2INT(k));
  return Data_Wrap_Struct(klass, 0, gsl_combination_free, c);
}

static VALUE rb_gsl_combination_init_first(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  gsl_combination_init_first(c);
  return obj;
}

static VALUE rb_gsl_combination_init_last(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  gsl_combination_init_last(c);
  return obj;
}

/* singleton */
static VALUE rb_gsl_combination_memcpy(VALUE klass, VALUE dst, VALUE src)
{
  gsl_combination *c, *c2;
  if (!rb_obj_is_kind_of(dst, klass))
    rb_raise(rb_eTypeError, "wrong argument type %s (Combination expected)",
             rb_class2name(CLASS_OF(dst)));
  if (!rb_obj_is_kind_of(src, klass))
    rb_raise(rb_eTypeError, "wrong argument type %s (Combination expected)",
             rb_class2name(CLASS_OF(src)));
  Data_Get_Struct(dst, gsl_combination, c2);
  Data_Get_Struct(src, gsl_combination, c);
  gsl_combination_memcpy(c2, c);
  return dst;
}

static VALUE rb_gsl_combination_clone(VALUE obj)
{
  gsl_combination *c, *c2;
  Data_Get_Struct(obj, gsl_combination, c);
  c2 = gsl_combination_alloc(c->n, c->k);
  gsl_combination_memcpy(c2, c);
  return Data_Wrap_Struct(CLASS_OF(obj), 0, gsl_combination_free, c2);
}

static VALUE rb_gsl_combination_get(VALUE obj, VALUE ii)
{
  gsl_combination *c = NULL;
  size_t i;
  CHECK_FIXNUM(ii);
  Data_Get_Struct(obj, gsl_combination, c);
  i = FIX2INT(ii);
  if (i > c->n) rb_raise(rb_eIndexError, "index out of range");
  return INT2FIX(gsl_combination_get(c, i));
}

static VALUE rb_gsl_combination_set(VALUE obj, VALUE ii, VALUE val)
{
  gsl_combination *c = NULL;
  size_t i;
  CHECK_FIXNUM(ii);
  CHECK_FIXNUM(val);
  Data_Get_Struct(obj, gsl_combination, c);
  i = FIX2INT(ii);
  c->data[i] = FIX2INT(val);
  return obj;
}

static VALUE rb_gsl_combination_n(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  return INT2FIX(gsl_combination_n(c));

}
static VALUE rb_gsl_combination_k(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  return INT2FIX(gsl_combination_k(c));
}

static VALUE rb_gsl_combination_data(VALUE obj)
{
  gsl_combination *c = NULL;
  gsl_permutation *p = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  p = ALLOC(gsl_permutation);
  p->size = c->k;
  p->data = c->data;
  return Data_Wrap_Struct(cgsl_combination_data, 0, free, p);
}

static VALUE rb_gsl_combination_valid(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  return INT2FIX(gsl_combination_valid(c));
}

static VALUE rb_gsl_combination_valid2(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  if(gsl_combination_valid(c)) return Qtrue;
  else return Qfalse;
}

static VALUE rb_gsl_combination_next(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  return INT2FIX(gsl_combination_next(c));
}
static VALUE rb_gsl_combination_prev(VALUE obj)
{
  gsl_combination *c = NULL;
  Data_Get_Struct(obj, gsl_combination, c);
  return INT2FIX(gsl_combination_prev(c));
}

static VALUE rb_gsl_combination_fwrite(VALUE obj, VALUE io)
{
  gsl_combination *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_combination, h);
  f = rb_gsl_open_writefile(io, &flag);
  status = gsl_combination_fwrite(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_combination_fread(VALUE obj, VALUE io)
{
  gsl_combination *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;

  Data_Get_Struct(obj, gsl_combination, h);
  f = rb_gsl_open_readfile(io, &flag);
  status = gsl_combination_fread(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_combination_fprintf(int argc, VALUE *argv, VALUE obj)
{
  gsl_combination *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;

  if (argc != 1 && argc != 2) rb_raise(rb_eArgError,
                                       "wrong number of arguments (%d for 1 or 2)", argc);
  Data_Get_Struct(obj, gsl_combination, h);
  fp = rb_gsl_open_writefile(argv[0], &flag);
  switch (argc) {
  case 1:
    status = gsl_combination_fprintf(fp, h, "%u\n");
    break;
  default:
    Check_Type(argv[1], T_STRING);
    status = gsl_combination_fprintf(fp, h, STR2CSTR(argv[1]));
    break;
  }
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

static VALUE rb_gsl_combination_printf(int argc, VALUE *argv, VALUE obj)
{
  gsl_combination *h = NULL;
  int status;
  Data_Get_Struct(obj, gsl_combination, h);
  switch (argc) {
  case 0:
    status = gsl_combination_fprintf(stdout, h, "%u\n");
    break;
  default:
    Check_Type(argv[0], T_STRING);
    status = gsl_combination_fprintf(stdout, h, STR2CSTR(argv[0]));
    break;
  }
  return INT2FIX(status);
}


static VALUE rb_gsl_combination_fscanf(VALUE obj, VALUE io)
{
  gsl_combination *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_combination, h);
  f = rb_gsl_open_readfile(io, &flag);
  status = gsl_combination_fscanf(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_combination_equal(VALUE obj, VALUE other)
{
  gsl_combination *p1 = NULL, *p2 = NULL;
  size_t i;
  Data_Get_Struct(obj, gsl_combination, p1);
  Data_Get_Struct(other, gsl_combination, p2);
  if (p1->k != p2->k) return Qfalse;
  for (i = 0; i < p1->k; i++)
    if (p1->data[i] != p2->data[i]) return Qfalse;
  return Qtrue;
}

void Init_gsl_combination(VALUE module)
{
  VALUE cgsl_combination;
  cgsl_combination = rb_define_class_under(module, "Combination", cGSL_Object);
  cgsl_combination_data = rb_define_class_under(cgsl_combination, "Data",
                                                cgsl_permutation);
  rb_define_singleton_method(cgsl_combination, "new", rb_gsl_combination_new, 2);
  rb_define_singleton_method(cgsl_combination, "alloc", rb_gsl_combination_new, 2);
  rb_define_singleton_method(cgsl_combination, "calloc", rb_gsl_combination_calloc, 2);
  rb_define_method(cgsl_combination, "init_first", rb_gsl_combination_init_first, 0);
  rb_define_method(cgsl_combination, "init_last", rb_gsl_combination_init_last, 0);
  rb_define_singleton_method(cgsl_combination, "memcpy", rb_gsl_combination_memcpy, 2);
  rb_define_method(cgsl_combination, "clone", rb_gsl_combination_clone, 0);
  rb_define_method(cgsl_combination, "get", rb_gsl_combination_get, 1);
  rb_define_alias(cgsl_combination, "[]", "get");
  rb_define_method(cgsl_combination, "set", rb_gsl_combination_set, 2);
  rb_define_alias(cgsl_combination, "[]=", "set");

  rb_define_method(cgsl_combination, "n", rb_gsl_combination_n, 0);
  rb_define_method(cgsl_combination, "k", rb_gsl_combination_k, 0);
  rb_define_method(cgsl_combination, "data", rb_gsl_combination_data, 0);
  rb_define_method(cgsl_combination, "valid", rb_gsl_combination_valid, 0);
  rb_define_method(cgsl_combination, "valid?", rb_gsl_combination_valid2, 0);
  rb_define_method(cgsl_combination, "next", rb_gsl_combination_next, 0);
  rb_define_method(cgsl_combination, "prev", rb_gsl_combination_prev, 0);

  rb_define_method(cgsl_combination, "fwrite", rb_gsl_combination_fwrite, 1);
  rb_define_method(cgsl_combination, "fread", rb_gsl_combination_fread, 1);
  rb_define_method(cgsl_combination, "fprintf", rb_gsl_combination_fprintf, -1);
  rb_define_method(cgsl_combination, "printf", rb_gsl_combination_printf, -1);
  rb_define_method(cgsl_combination, "fscanf", rb_gsl_combination_fscanf, 1);

  rb_define_method(cgsl_combination, "equal?", rb_gsl_combination_equal, 1);
  rb_define_alias(cgsl_combination, "==", "equal?");
}