numo-narray 0.9.1.4 → 0.9.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +10 -4
  3. data/ext/numo/narray/array.c +17 -7
  4. data/ext/numo/narray/data.c +39 -36
  5. data/ext/numo/narray/extconf.rb +1 -0
  6. data/ext/numo/narray/gen/narray_def.rb +4 -0
  7. data/ext/numo/narray/gen/spec.rb +5 -1
  8. data/ext/numo/narray/gen/tmpl/accum.c +2 -2
  9. data/ext/numo/narray/gen/tmpl/accum_arg.c +88 -0
  10. data/ext/numo/narray/gen/tmpl/accum_binary.c +1 -1
  11. data/ext/numo/narray/gen/tmpl/accum_index.c +25 -14
  12. data/ext/numo/narray/gen/tmpl/aref.c +5 -35
  13. data/ext/numo/narray/gen/tmpl/aset.c +7 -37
  14. data/ext/numo/narray/gen/tmpl/bincount.c +7 -7
  15. data/ext/numo/narray/gen/tmpl/clip.c +11 -15
  16. data/ext/numo/narray/gen/tmpl/cum.c +1 -1
  17. data/ext/numo/narray/gen/tmpl/each.c +4 -2
  18. data/ext/numo/narray/gen/tmpl/each_with_index.c +5 -2
  19. data/ext/numo/narray/gen/tmpl/lib.c +2 -2
  20. data/ext/numo/narray/gen/tmpl/logseq.c +6 -5
  21. data/ext/numo/narray/gen/tmpl/map_with_index.c +5 -6
  22. data/ext/numo/narray/gen/tmpl/median.c +2 -2
  23. data/ext/numo/narray/gen/tmpl/minmax.c +1 -1
  24. data/ext/numo/narray/gen/tmpl/poly.c +4 -4
  25. data/ext/numo/narray/gen/tmpl/qsort.c +1 -1
  26. data/ext/numo/narray/gen/tmpl/rand.c +8 -6
  27. data/ext/numo/narray/gen/tmpl/rand_norm.c +18 -16
  28. data/ext/numo/narray/gen/tmpl/seq.c +5 -4
  29. data/ext/numo/narray/gen/tmpl/sort.c +3 -3
  30. data/ext/numo/narray/gen/tmpl/sort_index.c +2 -2
  31. data/ext/numo/narray/gen/tmpl/store_array.c +14 -2
  32. data/ext/numo/narray/gen/tmpl/unary_s.c +55 -31
  33. data/ext/numo/narray/gen/tmpl_bit/aref.c +22 -30
  34. data/ext/numo/narray/gen/tmpl_bit/aset.c +20 -34
  35. data/ext/numo/narray/gen/tmpl_bit/binary.c +42 -14
  36. data/ext/numo/narray/gen/tmpl_bit/bit_count.c +5 -0
  37. data/ext/numo/narray/gen/tmpl_bit/bit_reduce.c +5 -0
  38. data/ext/numo/narray/gen/tmpl_bit/store_array.c +14 -2
  39. data/ext/numo/narray/gen/tmpl_bit/store_bit.c +21 -7
  40. data/ext/numo/narray/gen/tmpl_bit/unary.c +21 -7
  41. data/ext/numo/narray/index.c +369 -59
  42. data/ext/numo/narray/math.c +2 -2
  43. data/ext/numo/narray/narray.c +45 -27
  44. data/ext/numo/narray/ndloop.c +2 -2
  45. data/ext/numo/narray/numo/intern.h +3 -2
  46. data/ext/numo/narray/numo/narray.h +24 -5
  47. data/ext/numo/narray/numo/ndloop.h +2 -2
  48. data/ext/numo/narray/numo/template.h +4 -6
  49. data/ext/numo/narray/numo/types/complex.h +2 -2
  50. data/ext/numo/narray/step.c +58 -252
  51. data/ext/numo/narray/struct.c +2 -2
  52. data/lib/numo/narray/extra.rb +172 -212
  53. data/numo-narray.gemspec +9 -5
  54. metadata +18 -17
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  math.c
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-2019 Masahiro TANAKA
5
5
  */
6
6
  #include <ruby.h>
7
7
  #include "numo/narray.h"
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  narray.c
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-2019 Masahiro TANAKA
5
5
  */
6
6
  #define NARRAY_C
7
7
  #include <ruby.h>
@@ -42,17 +42,21 @@ VALUE sym_option;
42
42
  VALUE sym_loop_opt;
43
43
  VALUE sym_init;
44
44
 
45
- VALUE na_cStep;
46
45
  #ifndef HAVE_RB_CCOMPLEX
47
46
  VALUE rb_cComplex;
48
47
  #endif
48
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
49
+ VALUE rb_cArithSeq;
50
+ #endif
49
51
 
50
52
  int numo_na_inspect_rows=20;
51
53
  int numo_na_inspect_cols=80;
52
54
 
53
55
  void Init_nary_data();
54
56
  void Init_nary_ndloop();
57
+ #ifndef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
55
58
  void Init_nary_step();
59
+ #endif
56
60
  void Init_nary_index();
57
61
  void Init_numo_bit();
58
62
  void Init_numo_int8();
@@ -348,19 +352,19 @@ na_setup(VALUE self, int ndim, size_t *shape)
348
352
 
349
353
  @example
350
354
  i = Numo::Int64.new([2,4,3])
351
- #=> Numo::Int64#shape=[2,4,3](empty)
355
+ # => Numo::Int64#shape=[2,4,3](empty)
352
356
 
353
357
  f = Numo::DFloat.new(3,4)
354
- #=> Numo::DFloat#shape=[3,4](empty)
358
+ # => Numo::DFloat#shape=[3,4](empty)
355
359
 
356
360
  f.fill(2)
357
- #=> Numo::DFloat#shape=[3,4]
361
+ # => Numo::DFloat#shape=[3,4]
358
362
  # [[2, 2, 2, 2],
359
363
  # [2, 2, 2, 2],
360
364
  # [2, 2, 2, 2]]
361
365
 
362
366
  x = Numo::NArray.new(5)
363
- #=> in `new': allocator undefined for Numo::NArray (TypeError)
367
+ # => in `new': allocator undefined for Numo::NArray (TypeError)
364
368
  # from t.rb:9:in `<main>'
365
369
 
366
370
  */
@@ -444,10 +448,10 @@ na_initialize_copy(VALUE self, VALUE orig)
444
448
  * but for typed NArray subclasses, e.g., DFloat, Int64.
445
449
  * @example
446
450
  * a = Numo::DFloat.zeros(3,5)
447
- * => Numo::DFloat#shape=[3,5]
448
- * [[0, 0, 0, 0, 0],
449
- * [0, 0, 0, 0, 0],
450
- * [0, 0, 0, 0, 0]]
451
+ * # => Numo::DFloat#shape=[3,5]
452
+ * # [[0, 0, 0, 0, 0],
453
+ * # [0, 0, 0, 0, 0],
454
+ * # [0, 0, 0, 0, 0]]
451
455
  */
452
456
  static VALUE
453
457
  na_s_zeros(int argc, VALUE *argv, VALUE klass)
@@ -468,10 +472,10 @@ na_s_zeros(int argc, VALUE *argv, VALUE klass)
468
472
  * but for typed NArray subclasses, e.g., DFloat, Int64.
469
473
  * @example
470
474
  * a = Numo::DFloat.ones(3,5)
471
- * => Numo::DFloat#shape=[3,5]
472
- * [[1, 1, 1, 1, 1],
473
- * [1, 1, 1, 1, 1],
474
- * [1, 1, 1, 1, 1]]
475
+ * # => Numo::DFloat#shape=[3,5]
476
+ * # [[1, 1, 1, 1, 1],
477
+ * # [1, 1, 1, 1, 1],
478
+ * # [1, 1, 1, 1, 1]]
475
479
  */
476
480
  static VALUE
477
481
  na_s_ones(int argc, VALUE *argv, VALUE klass)
@@ -495,8 +499,8 @@ na_s_ones(int argc, VALUE *argv, VALUE klass)
495
499
 
496
500
  @example
497
501
  a = Numo::DFloat.linspace(-5,5,7)
498
- => Numo::DFloat#shape=[7]
499
- [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5]
502
+ # => Numo::DFloat#shape=[7]
503
+ # [-5, -3.33333, -1.66667, 0, 1.66667, 3.33333, 5]
500
504
  */
501
505
  static VALUE
502
506
  na_s_linspace(int argc, VALUE *argv, VALUE klass)
@@ -534,11 +538,12 @@ na_s_linspace(int argc, VALUE *argv, VALUE klass)
534
538
 
535
539
  @example
536
540
  Numo::DFloat.logspace(4,0,5,2)
537
- => Numo::DFloat#shape=[5]
538
- [16, 8, 4, 2, 1]
541
+ # => Numo::DFloat#shape=[5]
542
+ # [16, 8, 4, 2, 1]
543
+
539
544
  Numo::DComplex.logspace(0,1i*Math::PI,5,Math::E)
540
- => Numo::DComplex#shape=[5]
541
- [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...]
545
+ # => Numo::DComplex#shape=[5]
546
+ # [1+4.44659e-323i, 0.707107+0.707107i, 6.12323e-17+1i, -0.707107+0.707107i, ...]
542
547
  */
543
548
  static VALUE
544
549
  na_s_logspace(int argc, VALUE *argv, VALUE klass)
@@ -572,10 +577,10 @@ na_s_logspace(int argc, VALUE *argv, VALUE klass)
572
577
  @return [Numo::NArray] created NArray.
573
578
  @example
574
579
  a = Numo::DFloat.eye(3)
575
- => Numo::DFloat#shape=[3,3]
576
- [[1, 0, 0],
577
- [0, 1, 0],
578
- [0, 0, 1]]
580
+ # => Numo::DFloat#shape=[3,3]
581
+ # [[1, 0, 0],
582
+ # [0, 1, 0],
583
+ # [0, 0, 1]]
579
584
  */
580
585
  static VALUE
581
586
  na_s_eye(int argc, VALUE *argv, VALUE klass)
@@ -1583,8 +1588,13 @@ na_get_reduce_flag_from_axes(VALUE na_obj, VALUE axes)
1583
1588
  len = 1;
1584
1589
  step = 0;
1585
1590
  //printf("beg=%d step=%d len=%d\n",beg,step,len);
1586
- } else if (rb_obj_is_kind_of(v,rb_cRange) ||
1587
- rb_obj_is_kind_of(v,na_cStep)) {
1591
+ } else if (rb_obj_is_kind_of(v,rb_cRange)
1592
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
1593
+ || rb_obj_is_kind_of(v,rb_cArithSeq)
1594
+ #else
1595
+ || rb_obj_is_kind_of(v,rb_cEnumerator)
1596
+ #endif
1597
+ ) {
1588
1598
  nary_step_array_index( v, ndim, &len, &beg, &step );
1589
1599
  } else {
1590
1600
  rb_raise(nary_eDimensionError, "invalid dimension argument %s",
@@ -1870,6 +1880,9 @@ na_equal(VALUE self, volatile VALUE other)
1870
1880
  return Qfalse;
1871
1881
  }
1872
1882
  }
1883
+ if (na1->size == 0) {
1884
+ return Qtrue;
1885
+ }
1873
1886
  vbool = rb_funcall(self, id_eq, 1, other);
1874
1887
  return (rb_funcall(vbool, id_count_false, 0)==INT2FIX(0)) ? Qtrue : Qfalse;
1875
1888
  }
@@ -1896,6 +1909,9 @@ Init_narray()
1896
1909
  rb_require("complex");
1897
1910
  rb_cComplex = rb_const_get(rb_cObject, rb_intern("Complex"));
1898
1911
  #endif
1912
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
1913
+ rb_cArithSeq = rb_path2class("Enumerator::ArithmeticSequence");
1914
+ #endif
1899
1915
 
1900
1916
  rb_define_const(cNArray, "VERSION", rb_str_new2(NARRAY_VERSION));
1901
1917
 
@@ -1999,7 +2015,9 @@ Init_narray()
1999
2015
  sym_loop_opt = ID2SYM(rb_intern("loop_opt"));
2000
2016
  sym_init = ID2SYM(rb_intern("init"));
2001
2017
 
2018
+ #ifndef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
2002
2019
  Init_nary_step();
2020
+ #endif
2003
2021
  Init_nary_index();
2004
2022
 
2005
2023
  Init_nary_data();
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  ndloop.c
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-2019 Masahiro TANAKA
5
5
  */
6
6
 
7
7
  #include <ruby.h>
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  intern.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-2019 Masahiro TANAKA
5
5
  */
6
6
  #ifndef INTERN_H
7
7
  #define INTERN_H
@@ -99,6 +99,7 @@ bool nary_test_reduce(VALUE reduce, int dim);
99
99
 
100
100
  void nary_step_array_index(VALUE self, size_t ary_size, size_t *plen, ssize_t *pbeg, ssize_t *pstep);
101
101
  void nary_step_sequence(VALUE self, size_t *plen, double *pbeg, double *pstep);
102
+ void na_parse_enumerator_step(VALUE enum_obj, VALUE *pstep );
102
103
 
103
104
  // used in aref, aset
104
105
  #define na_get_result_dimension nary_get_result_dimension
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  narray.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-2019 Masahiro TANAKA
5
5
  */
6
6
  #ifndef NARRAY_H
7
7
  #define NARRAY_H
@@ -13,8 +13,8 @@ extern "C" {
13
13
  #endif
14
14
  #endif
15
15
 
16
- #define NARRAY_VERSION "0.9.1.4"
17
- #define NARRAY_VERSION_CODE 914
16
+ #define NARRAY_VERSION "0.9.1.5"
17
+ #define NARRAY_VERSION_CODE 915
18
18
 
19
19
  #include <math.h>
20
20
  #include "numo/compat.h"
@@ -165,10 +165,12 @@ extern VALUE numo_cUInt32;
165
165
  extern VALUE numo_cUInt16;
166
166
  extern VALUE numo_cUInt8;
167
167
  extern VALUE numo_cRObject;
168
- extern VALUE na_cStep;
169
168
  #ifndef HAVE_RB_CCOMPLEX
170
169
  extern VALUE rb_cComplex;
171
170
  #endif
171
+ #ifdef HAVE_RB_ARITHMETIC_SEQUENCE_EXTRACT
172
+ extern VALUE rb_cArithSeq;
173
+ #endif
172
174
 
173
175
  extern VALUE sym_reduce;
174
176
  extern VALUE sym_option;
@@ -234,6 +236,23 @@ typedef struct {
234
236
  unsigned int element_stride;
235
237
  } narray_type_info_t;
236
238
 
239
+ // from ruby/enumerator.c
240
+ struct enumerator {
241
+ VALUE obj;
242
+ ID meth;
243
+ VALUE args;
244
+ // use only above in this source
245
+ VALUE fib;
246
+ VALUE dst;
247
+ VALUE lookahead;
248
+ VALUE feedvalue;
249
+ VALUE stop_exc;
250
+ VALUE size;
251
+ // incompatible below depending on ruby version
252
+ //VALUE procs; // ruby 2.4
253
+ //rb_enumerator_size_func *size_fn; // ruby 2.1-2.4
254
+ //VALUE (*size_fn)(ANYARGS); // ruby 2.0
255
+ };
237
256
 
238
257
  static inline narray_t *
239
258
  na_get_narray_t(VALUE obj)
@@ -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-2019 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-2019 Masahiro TANAKA
5
5
  */
6
6
  #ifndef TEMPLATE_H
7
7
  #define TEMPLATE_H
@@ -112,9 +112,8 @@
112
112
  size_t dig = (pos) / NB; \
113
113
  int bit = (pos) % NB; \
114
114
  ((BIT_DIGIT*)(adr))[dig] = \
115
- (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
115
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | (((val)&1u)<<(bit)); \
116
116
  }
117
- // val -> val&1 ??
118
117
 
119
118
  #define STORE_BIT_STEP( adr, pos, step, idx, val )\
120
119
  { \
@@ -129,9 +128,8 @@
129
128
  pos += step; \
130
129
  } \
131
130
  ((BIT_DIGIT*)(adr))[dig] = \
132
- (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | ((val)<<(bit)); \
131
+ (((BIT_DIGIT*)(adr))[dig] & ~(1u<<(bit))) | (((val)&1u)<<(bit)); \
133
132
  }
134
- // val -> val&1 ??
135
133
 
136
134
  static inline int
137
135
  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-2019 Masahiro TANAKA
5
5
  */
6
6
 
7
7
 
@@ -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-2019 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
  }