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
|
# File 'ext/ruby_array_find_consecutive/ruby_array_find_consecutive.c', line 4
static VALUE
rb_arr_find_consecutive ( int argc, VALUE *argv, VALUE self )
{
if ( argc > 1 ) {
rb_raise(rb_eArgError, "wrong number of arguments (%i for 0..1)", argc);
}
double step;
if ( argc == 1 && TYPE(argv[0]) == T_FLOAT ) {
step = NUM2DBL(argv[0]);
} else if ( argc == 1 && TYPE(argv[0]) == T_FIXNUM ) {
step = NUM2INT(argv[0]);
} else {
step = 1;
}
VALUE r_ary = rb_ary_new();
VALUE c_ary = rb_ary_new();
long int max = RARRAY_LEN(self);
double c, n;
int i;
for ( i = 0; i < max; i++ ) {
VALUE ce = rb_ary_entry(self, i);
VALUE ne = rb_ary_entry(self, i + 1);
c = TYPE(ce) == T_FLOAT ? NUM2DBL(ce) : FIX2INT(ce);
if ( TYPE(ne) == T_FLOAT ) {
n = NUM2DBL(ne);
} else if ( TYPE(ne) == T_FIXNUM ) {
n = FIX2INT(ne);
}
if ( RARRAY_LEN(c_ary) == 0 ) {
VALUE c_ary_e = TYPE(ce) == T_FLOAT ? rb_float_new(c) : INT2FIX(c);
rb_ary_push(c_ary, c_ary_e);
}
if ( c + step == n ) {
VALUE c_ary_e = TYPE(ne) == T_FLOAT ? rb_float_new(n) : INT2FIX(n);
rb_ary_push(c_ary, c_ary_e);
} else {
if ( RARRAY_LEN(c_ary) > 1 ) {
rb_ary_push(r_ary, c_ary);
if ( rb_block_given_p() ) rb_yield(c_ary);
}
c_ary = rb_ary_new();
}
}
return r_ary;
}
|