numo-narray 0.9.1.4 → 0.9.1.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +12 -6
  3. data/Rakefile +4 -63
  4. data/ext/numo/narray/array.c +35 -32
  5. data/ext/numo/narray/data.c +40 -43
  6. data/ext/numo/narray/depend.erb +6 -10
  7. data/ext/numo/narray/extconf.rb +24 -21
  8. data/ext/numo/narray/gen/cogen.rb +2 -2
  9. data/ext/numo/narray/gen/narray_def.rb +5 -1
  10. data/ext/numo/narray/gen/spec.rb +17 -7
  11. data/ext/numo/narray/gen/tmpl/accum.c +2 -2
  12. data/ext/numo/narray/gen/tmpl/accum_arg.c +88 -0
  13. data/ext/numo/narray/gen/tmpl/accum_binary.c +1 -1
  14. data/ext/numo/narray/gen/tmpl/accum_index.c +25 -14
  15. data/ext/numo/narray/gen/tmpl/alloc_func.c +4 -1
  16. data/ext/numo/narray/gen/tmpl/allocate.c +1 -0
  17. data/ext/numo/narray/gen/tmpl/aref.c +5 -35
  18. data/ext/numo/narray/gen/tmpl/aset.c +7 -37
  19. data/ext/numo/narray/gen/tmpl/bincount.c +7 -7
  20. data/ext/numo/narray/gen/tmpl/cast.c +7 -0
  21. data/ext/numo/narray/gen/tmpl/clip.c +11 -15
  22. data/ext/numo/narray/gen/tmpl/cum.c +1 -1
  23. data/ext/numo/narray/gen/tmpl/each.c +4 -2
  24. data/ext/numo/narray/gen/tmpl/each_with_index.c +5 -2
  25. data/ext/numo/narray/gen/tmpl/lib.c +2 -2
  26. data/ext/numo/narray/gen/tmpl/logseq.c +6 -5
  27. data/ext/numo/narray/gen/tmpl/map_with_index.c +5 -6
  28. data/ext/numo/narray/gen/tmpl/median.c +2 -2
  29. data/ext/numo/narray/gen/tmpl/minmax.c +1 -1
  30. data/ext/numo/narray/gen/tmpl/poly.c +4 -4
  31. data/ext/numo/narray/gen/tmpl/qsort.c +1 -1
  32. data/ext/numo/narray/gen/tmpl/rand.c +8 -6
  33. data/ext/numo/narray/gen/tmpl/rand_norm.c +18 -16
  34. data/ext/numo/narray/gen/tmpl/seq.c +5 -4
  35. data/ext/numo/narray/gen/tmpl/sort.c +3 -3
  36. data/ext/numo/narray/gen/tmpl/sort_index.c +2 -2
  37. data/ext/numo/narray/gen/tmpl/store_array.c +14 -2
  38. data/ext/numo/narray/gen/tmpl/unary_s.c +55 -31
  39. data/ext/numo/narray/gen/tmpl_bit/allocate.c +1 -0
  40. data/ext/numo/narray/gen/tmpl_bit/aref.c +22 -30
  41. data/ext/numo/narray/gen/tmpl_bit/aset.c +20 -34
  42. data/ext/numo/narray/gen/tmpl_bit/binary.c +48 -14
  43. data/ext/numo/narray/gen/tmpl_bit/bit_count.c +5 -0
  44. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +5 -0
  45. data/ext/numo/narray/gen/tmpl_bit/store_array.c +14 -2
  46. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +27 -9
  47. data/ext/numo/narray/gen/tmpl_bit/unary.c +25 -7
  48. data/ext/numo/narray/index.c +363 -59
  49. data/ext/numo/narray/math.c +2 -2
  50. data/ext/numo/narray/narray.c +151 -36
  51. data/ext/numo/narray/ndloop.c +4 -4
  52. data/ext/numo/narray/numo/intern.h +3 -2
  53. data/ext/numo/narray/numo/narray.h +37 -14
  54. data/ext/numo/narray/numo/ndloop.h +2 -2
  55. data/ext/numo/narray/numo/template.h +11 -15
  56. data/ext/numo/narray/numo/types/complex.h +2 -2
  57. data/ext/numo/narray/numo/types/int16.h +5 -2
  58. data/ext/numo/narray/numo/types/int32.h +0 -1
  59. data/ext/numo/narray/numo/types/int64.h +0 -1
  60. data/ext/numo/narray/numo/types/int8.h +1 -2
  61. data/ext/numo/narray/numo/types/uint16.h +5 -2
  62. data/ext/numo/narray/numo/types/uint32.h +0 -1
  63. data/ext/numo/narray/numo/types/uint64.h +0 -1
  64. data/ext/numo/narray/numo/types/uint8.h +1 -2
  65. data/ext/numo/narray/numo/types/xint_macro.h +2 -0
  66. data/ext/numo/narray/step.c +58 -252
  67. data/ext/numo/narray/struct.c +3 -3
  68. data/lib/numo/narray.rb +1 -7
  69. data/lib/numo/narray/extra.rb +214 -213
  70. data/numo-narray.gemspec +7 -8
  71. metadata +17 -36
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  ndloop.h
3
- Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2017 by Masahiro TANAKA
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
5
  */
6
6
  #ifndef NDLOOP_H
7
7
  #define NDLOOP_H
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  template.h
3
- Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2017 by Masahiro TANAKA
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
5
  */
6
6
  #ifndef TEMPLATE_H
7
7
  #define TEMPLATE_H
@@ -36,19 +36,17 @@
36
36
 
37
37
  #define INIT_PTR_BIT( lp, i, ad, ps, st ) \
38
38
  { \
39
- ps = ((lp)->args[i].iter[0]).pos; \
40
- ad = (BIT_DIGIT*)(((lp)->args[i]).ptr) + ps/NB; \
41
- ps %= NB; \
42
- st = ((lp)->args[i].iter[0]).step; \
39
+ ps = ((lp)->args[i].iter[0]).pos; \
40
+ ad = (BIT_DIGIT*)(((lp)->args[i]).ptr); \
41
+ st = ((lp)->args[i].iter[0]).step; \
43
42
  }
44
43
 
45
44
  #define INIT_PTR_BIT_IDX( lp, i, ad, ps, st, id ) \
46
45
  { \
47
- ps = ((lp)->args[i].iter[0]).pos; \
48
- ad = (BIT_DIGIT*)(((lp)->args[i]).ptr) + ps/NB; \
49
- ps %= NB; \
50
- st = ((lp)->args[i].iter[0]).step; \
51
- id = ((lp)->args[i].iter[0]).idx; \
46
+ ps = ((lp)->args[i].iter[0]).pos; \
47
+ ad = (BIT_DIGIT*)(((lp)->args[i]).ptr); \
48
+ st = ((lp)->args[i].iter[0]).step; \
49
+ id = ((lp)->args[i].iter[0]).idx; \
52
50
  }
53
51
 
54
52
  #define GET_DATA( ptr, type, val ) \
@@ -112,9 +110,8 @@
112
110
  size_t dig = (pos) / NB; \
113
111
  int bit = (pos) % NB; \
114
112
  ((BIT_DIGIT*)(adr))[dig] = \
115
- (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
113
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | (((val)&1u)<<(bit)); \
116
114
  }
117
- // val -> val&1 ??
118
115
 
119
116
  #define STORE_BIT_STEP( adr, pos, step, idx, val )\
120
117
  { \
@@ -129,9 +126,8 @@
129
126
  pos += step; \
130
127
  } \
131
128
  ((BIT_DIGIT*)(adr))[dig] = \
132
- (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
129
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | (((val)&1u)<<(bit)); \
133
130
  }
134
- // val -> val&1 ??
135
131
 
136
132
  static inline int
137
133
  is_aligned(const void *ptr, const size_t alignment)
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  complex.h
3
- Numerical Array Extension for Ruby
4
- (C) Copyright 1999-2017 by Masahiro TANAKA
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 1999-2020 Masahiro TANAKA
5
5
  */
6
6
 
7
7
 
@@ -4,8 +4,11 @@ typedef int16_t rtype;
4
4
  #define cRT cT
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2INT(x))
7
- #define m_data_to_num(x) INT2NUM((int)(x))
8
- #define m_extract(x) INT2NUM((int)*(dtype*)(x))
7
+ #if SIZEOF_INT > 2
8
+ #define m_data_to_num(x) INT2FIX(x)
9
+ #else
10
+ #define m_data_to_num(x) INT2NUM(x)
11
+ #endif
9
12
  #define m_sprintf(s,x) sprintf(s,"%d",(int)(x))
10
13
 
11
14
  #ifndef INT16_MIN
@@ -5,7 +5,6 @@ typedef int32_t rtype;
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2INT32(x))
7
7
  #define m_data_to_num(x) INT322NUM((int32_t)(x))
8
- #define m_extract(x) INT322NUM((int32_t)*(dtype*)(x))
9
8
  #define m_sprintf(s,x) sprintf(s,"%"PRId32,(int32_t)(x))
10
9
 
11
10
  #ifndef INT32_MIN
@@ -5,7 +5,6 @@ typedef int64_t rtype;
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2INT64(x))
7
7
  #define m_data_to_num(x) INT642NUM((int64_t)(x))
8
- #define m_extract(x) INT642NUM((int64_t)*(dtype*)(x))
9
8
  #define m_sprintf(s,x) sprintf(s,"%"PRId64,(int64_t)(x))
10
9
 
11
10
  #ifndef INT64_MIN
@@ -4,8 +4,7 @@ typedef int8_t rtype;
4
4
  #define cRT cT
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2INT(x))
7
- #define m_data_to_num(x) INT2NUM((int)(x))
8
- #define m_extract(x) INT2NUM((int)*(dtype*)(x))
7
+ #define m_data_to_num(x) INT2FIX(x)
9
8
  #define m_sprintf(s,x) sprintf(s,"%d",(int)(x))
10
9
 
11
10
  #ifndef INT8_MIN
@@ -4,8 +4,11 @@ typedef u_int16_t rtype;
4
4
  #define cRT cT
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
- #define m_data_to_num(x) UINT2NUM((unsigned int)(x))
8
- #define m_extract(x) UINT2NUM((unsigned int)*(dtype*)(x))
7
+ #if SIZEOF_INT > 2
8
+ #define m_data_to_num(x) INT2FIX(x)
9
+ #else
10
+ #define m_data_to_num(x) UINT2NUM(x)
11
+ #endif
9
12
  #define m_sprintf(s,x) sprintf(s,"%u",(unsigned int)(x))
10
13
 
11
14
  #ifndef UINT16_MAX
@@ -5,7 +5,6 @@ typedef u_int32_t rtype;
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2UINT32(x))
7
7
  #define m_data_to_num(x) UINT322NUM((u_int32_t)(x))
8
- #define m_extract(x) UINT322NUM((u_int32_t)*(dtype*)(x))
9
8
  #define m_sprintf(s,x) sprintf(s,"%"PRIu32,(u_int32_t)(x))
10
9
 
11
10
  #ifndef UINT32_MAX
@@ -5,7 +5,6 @@ typedef u_int64_t rtype;
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2UINT64(x))
7
7
  #define m_data_to_num(x) UINT642NUM((u_int64_t)(x))
8
- #define m_extract(x) UINT642NUM((u_int64_t)*(dtype*)(x))
9
8
  #define m_sprintf(s,x) sprintf(s,"%"PRIu64,(u_int64_t)(x))
10
9
 
11
10
  #ifndef UINT64_MAX
@@ -4,8 +4,7 @@ typedef u_int8_t rtype;
4
4
  #define cRT cT
5
5
 
6
6
  #define m_num_to_data(x) ((dtype)NUM2UINT(x))
7
- #define m_data_to_num(x) UINT2NUM((unsigned int)(x))
8
- #define m_extract(x) UINT2NUM((unsigned int)*(dtype*)(x))
7
+ #define m_data_to_num(x) INT2FIX(x)
9
8
  #define m_sprintf(s,x) sprintf(s,"%u",(unsigned int)(x))
10
9
 
11
10
  #ifndef UINT8_MAX
@@ -1,6 +1,8 @@
1
1
  #define m_zero 0
2
2
  #define m_one 1
3
3
 
4
+ #define m_extract(x) m_data_to_num(*(dtype*)(x))
5
+
4
6
  #define m_from_double(x) (x)
5
7
  #define m_from_real(x) (x)
6
8
  #define m_from_sint(x) (x)
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  step.c
3
- Numerical Array Extension for Ruby
4
- (C) Copyright 2007,2013 by Masahiro TANAKA
3
+ Ruby/Numo::NArray - Numerical Array class for Ruby
4
+ Copyright (C) 2007-2020 Masahiro TANAKA
5
5
  */
6
6
  #include <ruby.h>
7
7
  #include <math.h>
@@ -24,164 +24,10 @@
24
24
  #define DBL_EPSILON 2.2204460492503131e-16
25
25
  #endif
26
26
 
27
- static ID id_beg, id_end, id_len, id_step, id_excl;
27
+ static ID id_beg, id_end, id_len, id_step;
28
28
 
29
- //#define EXCL(r) RTEST(rb_ivar_get((r), id_excl))
30
- #define EXCL(r) RTEST(rb_funcall((r), rb_intern("exclude_end?"), 0))
31
-
32
- #define SET_EXCL(r,v) rb_ivar_set((r), id_excl, (v) ? Qtrue : Qfalse)
33
-
34
- static void
35
- step_init(
36
- VALUE self,
37
- VALUE beg,
38
- VALUE end,
39
- VALUE step,
40
- VALUE len,
41
- VALUE excl
42
- )
43
- {
44
- if (RTEST(len)) {
45
- if (!(FIXNUM_P(len) || TYPE(len)==T_BIGNUM)) {
46
- rb_raise(rb_eArgError, "length must be Integer");
47
- }
48
- if (RTEST(rb_funcall(len,rb_intern("<"),1,INT2FIX(0)))) {
49
- rb_raise(rb_eRangeError,"length must be non negative");
50
- }
51
- }
52
- rb_ivar_set(self, id_beg, beg);
53
- rb_ivar_set(self, id_end, end);
54
- rb_ivar_set(self, id_len, len);
55
- rb_ivar_set(self, id_step, step);
56
- SET_EXCL(self, excl);
57
- }
58
-
59
- static VALUE
60
- nary_step_new2(
61
- VALUE range,
62
- VALUE step,
63
- VALUE len
64
- )
65
- {
66
- VALUE beg, end, excl;
67
- VALUE self = rb_obj_alloc(na_cStep);
68
-
69
- //beg = rb_ivar_get(range, id_beg);
70
- beg = rb_funcall(range, id_beg, 0);
71
- //end = rb_ivar_get(range, id_end);
72
- end = rb_funcall(range, id_end, 0);
73
- excl = rb_funcall(range, rb_intern("exclude_end?"), 0);
74
-
75
- step_init(self, beg, end, step, len, excl);
76
- return self;
77
- }
78
-
79
-
80
- /*
81
- * call-seq:
82
- * Step.new(start, end, step=nil, length=nil) => step
83
- * Step.new(range, step=nil, length=nil) => step
84
- *
85
- * Constructs a step using three parameters among <i>start</i>,
86
- * <i>end</i>, <i>step</i> and <i>length</i>. <i>start</i>,
87
- * <i>end</i> parameters can be replaced with <i>range</i>. If the
88
- * <i>step</i> is omitted (or supplied with nil), then calculated
89
- * from <i>length</i> or definded as 1.
90
- */
91
-
92
- static VALUE
93
- step_initialize( int argc, VALUE *argv, VALUE self )
94
- {
95
- VALUE a, b=Qnil, c=Qnil, d=Qnil, e=Qnil;
96
-
97
- rb_scan_args(argc, argv, "13", &a, &b, &c, &d);
98
- /* Selfs are immutable, so that they should be initialized only once. */
99
- if (rb_ivar_defined(self, id_beg)) {
100
- rb_name_error(rb_intern("initialize"), "`initialize' called twice");
101
- }
102
- if (rb_obj_is_kind_of(a,rb_cRange)) {
103
- if (argc>3) {
104
- rb_raise(rb_eArgError, "extra argument");
105
- }
106
- d = c;
107
- c = b;
108
- e = rb_funcall(a, rb_intern("exclude_end?"), 0);
109
- //b = rb_ivar_get(a, id_end);
110
- b = rb_funcall(a, id_end, 0);
111
- //a = rb_ivar_get(a, id_beg);
112
- a = rb_funcall(a, id_beg, 0);
113
- }
114
- step_init(self, a, b, c, d, e);
115
- return Qnil;
116
- }
117
-
118
- /*
119
- * call-seq:
120
- * step.begin => obj
121
- * step.first => obj
122
- *
123
- * Returns the start of <i>step</i>.
124
- */
125
-
126
- static VALUE
127
- step_first( VALUE self )
128
- {
129
- return rb_ivar_get(self, id_beg);
130
- }
131
-
132
- /*
133
- * call-seq:
134
- * step.end => obj
135
- * step.last => obj
136
- *
137
- * Returns the object that defines the end of <i>step</i>.
138
- */
139
-
140
- static VALUE
141
- step_last( VALUE self )
142
- {
143
- return rb_ivar_get(self, id_end);
144
- }
145
-
146
- /*
147
- * call-seq:
148
- * step.length => obj
149
- * step.size => obj
150
- *
151
- * Returns the length of <i>step</i>.
152
- */
153
-
154
- static VALUE
155
- step_length( VALUE self )
156
- {
157
- return rb_ivar_get(self, id_len);
158
- }
159
-
160
- /*
161
- * call-seq:
162
- * step.step => obj
163
- *
164
- * Returns the step of <i>step</i>.
165
- */
166
-
167
- static VALUE
168
- step_step( VALUE self )
169
- {
170
- return rb_ivar_get(self, id_step);
171
- }
172
-
173
- /*
174
- * call-seq:
175
- * step.exclude_end? => true or false
176
- *
177
- * Returns <code>true</code> if <i>step</i> excludes its end value.
178
- */
179
- static VALUE
180
- step_exclude_end_p(VALUE self)
181
- {
182
- return RTEST(rb_ivar_get(self, id_excl)) ? Qtrue : Qfalse;
183
- }
184
29
 
30
+ #define EXCL(r) RTEST(rb_funcall((r), rb_intern("exclude_end?"), 0))
185
31
 
186
32
  /*
187
33
  * call-seq:
@@ -192,7 +38,7 @@ step_exclude_end_p(VALUE self)
192
38
  */
193
39
 
194
40
  void
195
- nary_step_array_index(VALUE self, size_t ary_size,
41
+ nary_step_array_index(VALUE obj, size_t ary_size,
196
42
  size_t *plen, ssize_t *pbeg, ssize_t *pstep)
197
43
  {
198
44
  size_t len;
@@ -200,14 +46,28 @@ nary_step_array_index(VALUE self, size_t ary_size,
200
46
  VALUE vbeg, vend, vstep, vlen;
201
47
  ssize_t end=ary_size;
202
48
 
203
- //vbeg = rb_ivar_get(self, id_beg);
204
- //vend = rb_ivar_get(self, id_end);
205
- vlen = rb_ivar_get(self, id_len);
206
- vstep = rb_ivar_get(self, id_step);
207
- vbeg = rb_funcall(self, id_beg, 0);
208
- vend = rb_funcall(self, id_end, 0);
209
- //vlen = rb_funcall(self, id_len, 0);
210
- //vstep = rb_funcall(self, id_step, 0);
49
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
50
+ rb_arithmetic_sequence_components_t x;
51
+ rb_arithmetic_sequence_extract(obj, &x);
52
+
53
+ vstep = x.step;
54
+ vbeg = x.begin;
55
+ vend = x.end;
56
+ #else
57
+ struct enumerator *e;
58
+
59
+ if (rb_obj_is_kind_of(obj, rb_cRange)) {
60
+ vstep = rb_ivar_get(obj, id_step);
61
+ } else { // Enumerator
62
+ na_parse_enumerator_step(obj, &vstep);
63
+ e = (struct enumerator *)DATA_PTR(obj);
64
+ obj = e->obj; // Range
65
+ }
66
+
67
+ vbeg = rb_funcall(obj, id_beg, 0);
68
+ vend = rb_funcall(obj, id_end, 0);
69
+ #endif
70
+ vlen = rb_ivar_get(obj, id_len);
211
71
 
212
72
  if (RTEST(vbeg)) {
213
73
  beg = NUM2SSIZET(vbeg);
@@ -237,7 +97,7 @@ nary_step_array_index(VALUE self, size_t ary_size,
237
97
  }
238
98
  } else {
239
99
  if (RTEST(vend)) {
240
- if (EXCL(self)) {
100
+ if (EXCL(obj)) {
241
101
  if (step>0) end--;
242
102
  if (step<0) end++;
243
103
  }
@@ -251,7 +111,7 @@ nary_step_array_index(VALUE self, size_t ary_size,
251
111
  step = 1;
252
112
  if (RTEST(vbeg)) {
253
113
  if (RTEST(vend)) {
254
- if (EXCL(self)) {
114
+ if (EXCL(obj)) {
255
115
  if (beg<end) end--;
256
116
  if (beg>end) end++;
257
117
  }
@@ -262,7 +122,7 @@ nary_step_array_index(VALUE self, size_t ary_size,
262
122
  }
263
123
  } else {
264
124
  if (RTEST(vend)) {
265
- if (EXCL(self)) {
125
+ if (EXCL(obj)) {
266
126
  end--;
267
127
  }
268
128
  beg = end - (len-1);
@@ -286,7 +146,7 @@ nary_step_array_index(VALUE self, size_t ary_size,
286
146
  if (!RTEST(vend)) {
287
147
  end = ary_size-1;
288
148
  }
289
- else if (EXCL(self)) {
149
+ else if (EXCL(obj)) {
290
150
  end--;
291
151
  }
292
152
  if (beg<=end) {
@@ -301,7 +161,7 @@ nary_step_array_index(VALUE self, size_t ary_size,
301
161
  if (!RTEST(vend)) {
302
162
  end = 0;
303
163
  }
304
- else if (EXCL(self)) {
164
+ else if (EXCL(obj)) {
305
165
  end++;
306
166
  }
307
167
  if (beg>=end) {
@@ -327,25 +187,35 @@ nary_step_array_index(VALUE self, size_t ary_size,
327
187
  if (pstep) *pstep = step;
328
188
  }
329
189
 
330
-
331
190
  void
332
- nary_step_sequence( VALUE self, size_t *plen, double *pbeg, double *pstep )
191
+ nary_step_sequence( VALUE obj, size_t *plen, double *pbeg, double *pstep )
333
192
  {
334
- VALUE vbeg, vend, vstep, vlen;
193
+ VALUE vend, vstep, vlen;
335
194
  double dbeg, dend, dstep=1, dsize, err;
336
195
  size_t size, n;
337
196
 
338
- //vbeg = rb_ivar_get(self, id_beg);
339
- vbeg = rb_funcall(self, id_beg, 0);
340
- dbeg = NUM2DBL(vbeg);
341
-
342
- //vend = rb_ivar_get(self, id_end);
343
- vend = rb_funcall(self, id_end, 0);
197
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
198
+ rb_arithmetic_sequence_components_t x;
199
+ rb_arithmetic_sequence_extract(obj, &x);
200
+
201
+ vstep = x.step;
202
+ dbeg = NUM2DBL(x.begin);
203
+ vend = x.end;
204
+ #else
205
+ struct enumerator *e;
206
+
207
+ if (rb_obj_is_kind_of(obj, rb_cRange)) {
208
+ vstep = rb_ivar_get(obj, id_step);
209
+ } else { // Enumerator
210
+ na_parse_enumerator_step(obj, &vstep);
211
+ e = (struct enumerator *)DATA_PTR(obj);
212
+ obj = e->obj; // Range
213
+ }
344
214
 
345
- vlen = rb_ivar_get(self, id_len);
346
- vstep = rb_ivar_get(self, id_step);
347
- //vlen = rb_funcall(self, id_len ,0);
348
- //vstep = rb_funcall(self, id_step,0);
215
+ dbeg = NUM2DBL(rb_funcall(obj, id_beg, 0));
216
+ vend = rb_funcall(obj, id_end, 0);
217
+ #endif
218
+ vlen = rb_ivar_get(obj, id_len);
349
219
 
350
220
  if (RTEST(vlen)) {
351
221
  size = NUM2SIZET(vlen);
@@ -353,7 +223,7 @@ nary_step_sequence( VALUE self, size_t *plen, double *pbeg, double *pstep )
353
223
  if (!RTEST(vstep)) {
354
224
  if (RTEST(vend)) {
355
225
  dend = NUM2DBL(vend);
356
- if (EXCL(self)) {
226
+ if (EXCL(obj)) {
357
227
  n = size;
358
228
  } else {
359
229
  n = size-1;
@@ -378,7 +248,7 @@ nary_step_sequence( VALUE self, size_t *plen, double *pbeg, double *pstep )
378
248
  err = (fabs(dbeg)+fabs(dend)+fabs(dend-dbeg))/fabs(dstep)*DBL_EPSILON;
379
249
  if (err>0.5) err=0.5;
380
250
  dsize = (dend-dbeg)/dstep;
381
- if (EXCL(self))
251
+ if (EXCL(obj))
382
252
  dsize -= err;
383
253
  else
384
254
  dsize += err;
@@ -398,77 +268,13 @@ nary_step_sequence( VALUE self, size_t *plen, double *pbeg, double *pstep )
398
268
  if (pstep) *pstep = dstep;
399
269
  }
400
270
 
401
- /*
402
- static VALUE
403
- step_each( VALUE self )
404
- {
405
- VALUE a;
406
- double beg, step;
407
- size_t i, size;
408
-
409
- a = nary_step_parameters( self, Qnil );
410
- beg = NUM2DBL(RARRAY_PTR(a)[0]);
411
- step = NUM2DBL(RARRAY_PTR(a)[1]);
412
- size = NUM2SIZET(RARRAY_PTR(a)[2]);
413
-
414
- for (i=0; i<size; i++) {
415
- rb_yield(rb_float_new(beg+i*step));
416
- }
417
- return self;
418
- }
419
- */
420
-
421
- static VALUE
422
- range_with_step( VALUE range, VALUE step )
423
- {
424
- return nary_step_new2( range, step, Qnil );
425
- }
426
-
427
- static VALUE
428
- range_with_length( VALUE range, VALUE len )
429
- {
430
- return nary_step_new2( range, Qnil, len );
431
- }
432
-
433
-
434
- static VALUE
435
- nary_s_step( int argc, VALUE *argv, VALUE mod )
436
- {
437
- VALUE self = rb_obj_alloc(na_cStep);
438
- step_initialize(argc, argv, self);
439
- return self;
440
- }
441
-
442
-
443
271
  void
444
272
  Init_nary_step()
445
273
  {
446
- na_cStep = rb_define_class_under(cNArray, "Step", rb_cObject);
447
- rb_include_module(na_cStep, rb_mEnumerable);
448
- rb_define_method(na_cStep, "initialize", step_initialize, -1);
449
-
450
- //rb_define_method(na_cStep, "each", step_each, 0);
451
-
452
- rb_define_method(na_cStep, "first", step_first, 0);
453
- rb_define_method(na_cStep, "last", step_last, 0);
454
- rb_define_method(na_cStep, "begin", step_first, 0);
455
- rb_define_method(na_cStep, "end", step_last, 0);
456
- rb_define_method(na_cStep, "step", step_step, 0);
457
- rb_define_method(na_cStep, "length", step_length, 0);
458
- rb_define_method(na_cStep, "size", step_length, 0);
459
- rb_define_method(na_cStep, "exclude_end?", step_exclude_end_p, 0);
460
- //rb_define_method(na_cStep, "to_s", step_to_s, 0);
461
- //rb_define_method(na_cStep, "inspect", step_inspect, 0);
462
- //rb_define_method(na_cStep, "parameters", nary_step_parameters, 1);
463
-
464
- rb_define_method(rb_cRange, "%", range_with_step, 1);
465
- rb_define_method(rb_cRange, "*", range_with_length, 1);
466
-
467
- rb_define_singleton_method(cNArray, "step", nary_s_step, -1);
274
+ rb_define_alias(rb_cRange, "%", "step");
468
275
 
469
276
  id_beg = rb_intern("begin");
470
277
  id_end = rb_intern("end");
471
278
  id_len = rb_intern("length");
472
279
  id_step = rb_intern("step");
473
- id_excl = rb_intern("excl");
474
280
  }