carray 1.1.4 → 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +4 -4
  2. data/COPYING +56 -0
  3. data/GPL +340 -0
  4. data/Gemfile +9 -0
  5. data/Gemfile.lock +33 -0
  6. data/LEGAL +50 -0
  7. data/NOTE +73 -0
  8. data/Rakefile +20 -0
  9. data/TODO +5 -0
  10. data/ca_iter_block.c +242 -0
  11. data/ca_iter_dimension.c +287 -0
  12. data/ca_iter_window.c +202 -0
  13. data/ca_obj_array.c +1189 -0
  14. data/ca_obj_bitarray.c +523 -0
  15. data/ca_obj_bitfield.c +636 -0
  16. data/ca_obj_block.c +885 -0
  17. data/ca_obj_fake.c +405 -0
  18. data/ca_obj_farray.c +482 -0
  19. data/ca_obj_field.c +625 -0
  20. data/ca_obj_grid.c +738 -0
  21. data/ca_obj_mapping.c +614 -0
  22. data/ca_obj_object.c +777 -0
  23. data/ca_obj_reduce.c +299 -0
  24. data/ca_obj_refer.c +627 -0
  25. data/ca_obj_repeat.c +640 -0
  26. data/ca_obj_select.c +558 -0
  27. data/ca_obj_shift.c +952 -0
  28. data/ca_obj_transpose.c +582 -0
  29. data/ca_obj_unbound_repeat.c +557 -0
  30. data/ca_obj_window.c +1023 -0
  31. data/carray.h +1381 -0
  32. data/carray_access.c +1798 -0
  33. data/carray_attribute.c +903 -0
  34. data/carray_call_cfunc.c +1107 -0
  35. data/carray_cast.c +1155 -0
  36. data/carray_cast_func.rb +498 -0
  37. data/carray_class.c +132 -0
  38. data/carray_conversion.c +518 -0
  39. data/carray_copy.c +453 -0
  40. data/carray_core.c +1307 -0
  41. data/carray_element.c +572 -0
  42. data/carray_generate.c +681 -0
  43. data/carray_iterator.c +630 -0
  44. data/carray_loop.c +462 -0
  45. data/carray_mask.c +1174 -0
  46. data/carray_math.rb +834 -0
  47. data/carray_numeric.c +257 -0
  48. data/carray_operator.c +582 -0
  49. data/carray_order.c +1040 -0
  50. data/carray_random.c +529 -0
  51. data/carray_sort_addr.c +261 -0
  52. data/carray_stat.c +2102 -0
  53. data/carray_stat_proc.rb +1990 -0
  54. data/carray_test.c +602 -0
  55. data/carray_undef.c +69 -0
  56. data/carray_utils.c +740 -0
  57. data/ext/calculus/carray_calculus.c +792 -0
  58. data/ext/calculus/carray_interp.c +355 -0
  59. data/ext/calculus/extconf.rb +12 -0
  60. data/ext/calculus/lib/autoload/autoload_math_calculus.rb +2 -0
  61. data/ext/calculus/lib/math/calculus.rb +119 -0
  62. data/ext/calculus/lib/math/interp/adapter_interp1d.rb +31 -0
  63. data/ext/dataframe/API.txt +11 -0
  64. data/ext/dataframe/extconf.rb +3 -0
  65. data/ext/dataframe/lib/carray/autoload/autoload_dataframe_dataframe.rb +14 -0
  66. data/ext/dataframe/lib/carray/dataframe/dataframe.rb +1104 -0
  67. data/ext/dataframe/sample/test_uniq_sort.rb +5 -0
  68. data/ext/fortio/extconf.rb +3 -0
  69. data/ext/fortio/lib/carray/autoload/autoload_fortran_format.rb +5 -0
  70. data/ext/fortio/lib/carray/io/fortran_format.rb +43 -0
  71. data/ext/fortio/lib/fortio.rb +3 -0
  72. data/ext/fortio/lib/fortio/fortran_format.rb +603 -0
  73. data/ext/fortio/lib/fortio/fortran_format.tab.rb +536 -0
  74. data/ext/fortio/lib/fortio/fortran_format.y +215 -0
  75. data/ext/fortio/lib/fortio/fortran_namelist.rb +151 -0
  76. data/ext/fortio/lib/fortio/fortran_namelist.tab.rb +470 -0
  77. data/ext/fortio/lib/fortio/fortran_namelist.y +213 -0
  78. data/ext/fortio/lib/fortio/fortran_sequential.rb +345 -0
  79. data/ext/fortio/ruby_fortio.c +182 -0
  80. data/ext/fortio/test/test_H.rb +5 -0
  81. data/ext/fortio/test/test_T.rb +7 -0
  82. data/ext/fortio/test/test_fortran_format.rb +86 -0
  83. data/ext/fortio/test/test_namelist.rb +25 -0
  84. data/ext/fortio/test/test_sequential.rb +13 -0
  85. data/ext/fortio/test/test_sequential2.rb +13 -0
  86. data/ext/fortio/work/test.rb +10 -0
  87. data/ext/fortio/work/test_e.rb +19 -0
  88. data/ext/fortio/work/test_ep.rb +10 -0
  89. data/ext/fortio/work/test_parse.rb +12 -0
  90. data/ext/imagemap/carray_imagemap.c +495 -0
  91. data/ext/imagemap/doc/call_graph.dot +28 -0
  92. data/ext/imagemap/draw.c +567 -0
  93. data/ext/imagemap/extconf.rb +13 -0
  94. data/ext/imagemap/lib/autoload/autoload_graphics_imagemap.rb +1 -0
  95. data/ext/imagemap/lib/graphics/imagemap.rb +273 -0
  96. data/ext/imagemap/lib/image_map.rb +4 -0
  97. data/ext/imagemap/test/swath_index.rb +83 -0
  98. data/ext/imagemap/test/swath_warp.rb +99 -0
  99. data/ext/imagemap/test/test.rb +23 -0
  100. data/ext/imagemap/test/test_image.rb +42 -0
  101. data/ext/imagemap/test/test_line.rb +14 -0
  102. data/ext/imagemap/test/test_rotate.rb +17 -0
  103. data/ext/imagemap/test/test_triangle.rb +20 -0
  104. data/ext/imagemap/test/test_warp.rb +26 -0
  105. data/ext/mathfunc/carray_mathfunc.c +321 -0
  106. data/ext/mathfunc/extconf.rb +18 -0
  107. data/ext/mathfunc/lib/autoload/autoload_math_mathfunc.rb +1 -0
  108. data/ext/mathfunc/lib/math/mathfunc.rb +15 -0
  109. data/ext/mathfunc/test/test_hypot.rb +5 -0
  110. data/ext/mathfunc/test/test_j0.rb +22 -0
  111. data/ext/mathfunc/test/test_jn.rb +8 -0
  112. data/ext/mathfunc/test/test_sph.rb +9 -0
  113. data/ext/narray/README +22 -0
  114. data/ext/narray/ca_wrap_narray.c +491 -0
  115. data/ext/narray/carray_narray.c +21 -0
  116. data/ext/narray/extconf.rb +57 -0
  117. data/ext/narray/lib/autoload/autoload_math_narray.rb +1 -0
  118. data/ext/narray/lib/autoload/autoload_math_narray_miss.rb +11 -0
  119. data/ext/narray/lib/math/narray.rb +17 -0
  120. data/ext/narray/lib/math/narray_miss.rb +45 -0
  121. data/extconf.rb +3 -25
  122. data/lib/carray.rb +28 -0
  123. data/lib/carray/autoload/autoload_base.rb +23 -0
  124. data/lib/carray/autoload/autoload_graphics_gnuplot.rb +2 -0
  125. data/lib/carray/autoload/autoload_io_csv.rb +14 -0
  126. data/lib/carray/autoload/autoload_io_excel.rb +5 -0
  127. data/lib/carray/autoload/autoload_io_imagemagick.rb +6 -0
  128. data/lib/carray/autoload/autoload_io_pg.rb +6 -0
  129. data/lib/carray/autoload/autoload_io_sqlite3.rb +12 -0
  130. data/lib/carray/autoload/autoload_io_table.rb +1 -0
  131. data/lib/carray/autoload/autoload_math_histogram.rb +5 -0
  132. data/lib/carray/autoload/autoload_math_interp.rb +4 -0
  133. data/lib/carray/autoload/autoload_math_recurrence.rb +6 -0
  134. data/lib/carray/autoload/autoload_object_iterator.rb +1 -0
  135. data/lib/carray/autoload/autoload_object_link.rb +1 -0
  136. data/lib/carray/autoload/autoload_object_pack.rb +2 -0
  137. data/lib/carray/base/autoload.rb +94 -0
  138. data/lib/carray/base/basic.rb +1051 -0
  139. data/lib/carray/base/inspect.rb +252 -0
  140. data/lib/carray/base/iterator.rb +367 -0
  141. data/lib/carray/base/math.rb +403 -0
  142. data/lib/carray/base/obsolete.rb +93 -0
  143. data/lib/carray/base/serialize.rb +260 -0
  144. data/lib/carray/base/struct.rb +634 -0
  145. data/lib/carray/graphics/gnuplot.rb +2116 -0
  146. data/lib/carray/info.rb +112 -0
  147. data/lib/carray/io/csv.rb +560 -0
  148. data/lib/carray/io/excel.rb +26 -0
  149. data/lib/carray/io/imagemagick.rb +231 -0
  150. data/lib/carray/io/pg.rb +101 -0
  151. data/lib/carray/io/sqlite3.rb +202 -0
  152. data/lib/carray/io/table.rb +77 -0
  153. data/lib/carray/math/histogram.rb +179 -0
  154. data/lib/carray/math/interp.rb +57 -0
  155. data/lib/carray/math/interp/adapter_gsl_spline.rb +47 -0
  156. data/lib/carray/math/recurrence.rb +95 -0
  157. data/lib/carray/mkmf.rb +145 -0
  158. data/lib/carray/object/ca_obj_iterator.rb +52 -0
  159. data/lib/carray/object/ca_obj_link.rb +52 -0
  160. data/lib/carray/object/ca_obj_pack.rb +101 -0
  161. data/mkmath.rb +731 -0
  162. data/mt19937ar.c +182 -0
  163. data/mt19937ar.h +86 -0
  164. data/rdoc_main.rb +27 -0
  165. data/rdoc_math.rb +5 -0
  166. data/rdoc_stat.rb +31 -0
  167. data/ruby_carray.c +242 -0
  168. data/ruby_ccomplex.c +497 -0
  169. data/ruby_float_func.c +83 -0
  170. data/spec/CABlockIterator/CABlockIterator_spec.rb +113 -0
  171. data/spec/CArray/bug/store_spec.rb +27 -0
  172. data/spec/CArray/index/repeat_spec.rb +10 -0
  173. data/spec/CArray/method/eq_spec.rb +80 -0
  174. data/spec/CArray/method/is_nan_spec.rb +12 -0
  175. data/spec/CArray/method/ne_spec.rb +18 -0
  176. data/spec/CArray/method/round_spec.rb +11 -0
  177. data/spec/CArray/object/_attribute_spec.rb +32 -0
  178. data/spec/CArray/object/s_new_spec.rb +31 -0
  179. data/spec/CArray/serialize/Serialization_spec.rb +89 -0
  180. data/spec/spec_all.rb +11 -0
  181. data/test/test_ALL.rb +50 -0
  182. data/test/test_CABitfield.rb +59 -0
  183. data/test/test_CABlock.rb +208 -0
  184. data/test/test_CAField.rb +40 -0
  185. data/test/test_CAGrid.rb +76 -0
  186. data/test/test_CAMapping.rb +106 -0
  187. data/test/test_CAMmap.rb +11 -0
  188. data/test/test_CARefer.rb +94 -0
  189. data/test/test_CARepeat.rb +66 -0
  190. data/test/test_CASelect.rb +23 -0
  191. data/test/test_CAShift.rb +17 -0
  192. data/test/test_CATranspose.rb +61 -0
  193. data/test/test_CAVirtual.rb +214 -0
  194. data/test/test_CAWindow.rb +55 -0
  195. data/test/test_CAWrap.rb +9 -0
  196. data/test/test_CArray.rb +228 -0
  197. data/test/test_CComplex.rb +83 -0
  198. data/test/test_CScalar.rb +91 -0
  199. data/test/test_attribute.rb +281 -0
  200. data/test/test_block_iterator.rb +17 -0
  201. data/test/test_boolean.rb +99 -0
  202. data/test/test_cast.rb +33 -0
  203. data/test/test_class.rb +85 -0
  204. data/test/test_complex.rb +43 -0
  205. data/test/test_composite.rb +125 -0
  206. data/test/test_convert.rb +79 -0
  207. data/test/test_copy.rb +141 -0
  208. data/test/test_creation.rb +85 -0
  209. data/test/test_element.rb +146 -0
  210. data/test/test_extream.rb +55 -0
  211. data/test/test_generate.rb +75 -0
  212. data/test/test_index.rb +71 -0
  213. data/test/test_mask.rb +578 -0
  214. data/test/test_math.rb +98 -0
  215. data/test/test_narray.rb +64 -0
  216. data/test/test_order.rb +147 -0
  217. data/test/test_random.rb +15 -0
  218. data/test/test_ref_store.rb +211 -0
  219. data/test/test_stat.rb +414 -0
  220. data/test/test_struct.rb +72 -0
  221. data/test/test_virtual.rb +49 -0
  222. data/utils/ca_ase.rb +21 -0
  223. data/utils/ca_methods.rb +15 -0
  224. data/utils/cast_checker.rb +30 -0
  225. data/utils/create_rdoc.sh +9 -0
  226. data/utils/diff_method.rb +52 -0
  227. data/utils/extract_rdoc.rb +27 -0
  228. data/utils/make_tgz.sh +3 -0
  229. data/utils/remove_resource_fork.sh +5 -0
  230. data/version.h +3 -3
  231. metadata +266 -1
@@ -0,0 +1,69 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ carray_undef.c
4
+
5
+ This file is part of Ruby/CArray extension library.
6
+ You can redistribute it and/or modify it under the terms of
7
+ the Ruby Licence.
8
+
9
+ Copyright (C) 2005 Hiroki Motoyoshi
10
+
11
+ ---------------------------------------------------------------------------- */
12
+
13
+ #include "ruby.h"
14
+
15
+ static VALUE rb_cUNDEF;
16
+
17
+ VALUE CA_UNDEF;
18
+
19
+ static VALUE rb_ud_inspect (VALUE self)
20
+ {
21
+ return rb_str_new2("UNDEF");
22
+ }
23
+
24
+ static VALUE rb_ud_to_s (VALUE self)
25
+ {
26
+ /* rb_raise(rb_eTypeError, "can't coerce UNDEF into String"); */
27
+ return rb_str_new2("UNDEF");
28
+ }
29
+
30
+ static VALUE rb_ud_to_f (VALUE self)
31
+ {
32
+ rb_raise(rb_eTypeError, "can't coerce UNDEF into Float");
33
+ /* return rb_float_new(0.0/0.0); */
34
+ }
35
+
36
+ static VALUE rb_ud_to_i (VALUE self)
37
+ {
38
+ rb_raise(rb_eTypeError, "can't coerce UNDEF into Integer");
39
+ }
40
+
41
+ static VALUE rb_ud_equal (VALUE self, VALUE other)
42
+ {
43
+ return ( self == other ) ? Qtrue : Qfalse;
44
+ }
45
+
46
+
47
+ static VALUE rb_obj_is_undef (VALUE self)
48
+ {
49
+ return ( self == CA_UNDEF ) ? Qtrue : Qfalse;
50
+ }
51
+
52
+ void
53
+ Init_carray_undef ()
54
+ {
55
+ rb_cUNDEF = rb_define_class("UndefClass", rb_cObject);
56
+ rb_define_method(rb_cUNDEF, "inspect", rb_ud_inspect, 0);
57
+ rb_define_method(rb_cUNDEF, "to_s", rb_ud_to_s, 0);
58
+ rb_define_method(rb_cUNDEF, "to_f", rb_ud_to_f, 0);
59
+ rb_define_method(rb_cUNDEF, "to_i", rb_ud_to_i, 0);
60
+ rb_define_method(rb_cUNDEF, "to_int", rb_ud_to_i, 0);
61
+ rb_define_method(rb_cUNDEF, "==", rb_ud_equal, 1);
62
+
63
+ CA_UNDEF = rb_funcall(rb_cUNDEF, rb_intern("new"), 0);
64
+ rb_undef_method(CLASS_OF(rb_cUNDEF), "new");
65
+ rb_const_set(rb_cObject, rb_intern("UNDEF"), CA_UNDEF);
66
+
67
+ rb_define_method(rb_cObject, "undef?", rb_obj_is_undef, 0);
68
+ }
69
+
@@ -0,0 +1,740 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ carray_utils.c
4
+
5
+ This file is part of Ruby/CArray extension library.
6
+ You can redistribute it and/or modify it under the terms of
7
+ the Ruby Licence.
8
+
9
+ Copyright (C) 2005 Hiroki Motoyoshi
10
+
11
+ ---------------------------------------------------------------------------- */
12
+
13
+ #include <stdarg.h>
14
+ #include "ruby.h"
15
+ #include "carray.h"
16
+
17
+ #if RUBY_VERSION_CODE >= 190
18
+ #include "ruby/st.h"
19
+ #else
20
+ #include "st.h"
21
+ #endif
22
+
23
+ #if RUBY_VERSION_CODE >= 190
24
+ #define RANGE_BEG(r) (RSTRUCT(r)->as.ary[0])
25
+ #define RANGE_END(r) (RSTRUCT(r)->as.ary[1])
26
+ #define RANGE_EXCL(r) (RSTRUCT(r)->as.ary[2])
27
+ #else
28
+ static ID id_beg, id_end, id_excl;
29
+ #define RANGE_BEG(r) (rb_ivar_get(r, id_beg))
30
+ #define RANGE_END(r) (rb_ivar_get(r, id_end))
31
+ #define RANGE_EXCL(r) (rb_ivar_get(r, id_excl))
32
+ #endif
33
+
34
+
35
+ /* ------------------------------------------------------------------- */
36
+
37
+ void *
38
+ malloc_with_check (size_t size)
39
+ {
40
+ void *ptr;
41
+ ptr = xmalloc(size);
42
+ if ( !ptr ) {
43
+ rb_memerror();
44
+ }
45
+ return ptr;
46
+ }
47
+
48
+ /* ------------------------------------------------------------------- */
49
+
50
+ void
51
+ ca_debug () {}
52
+
53
+ /* ------------------------------------------------------------------- */
54
+
55
+ int32_t
56
+ ca_set_iterator (int n, ...)
57
+ {
58
+ CArray *ca;
59
+ char **p;
60
+ int *s;
61
+ int32_t max = -1;
62
+ int all_scalar = 1;
63
+ va_list args;
64
+ va_start(args, n);
65
+ while ( n-- ) {
66
+ ca = va_arg(args, CArray *);
67
+ p = va_arg(args, char **);
68
+ s = va_arg(args, int *);
69
+ *p = ca->ptr;
70
+ if ( ca_is_scalar(ca) ) {
71
+ *s = 0;
72
+ all_scalar &= 1;
73
+ }
74
+ else {
75
+ *s = 1;
76
+ all_scalar = 0;
77
+ if ( max < 0 ) {
78
+ max = ca->elements;
79
+ }
80
+ else if ( max != ca->elements ) {
81
+ rb_raise(rb_eRuntimeError, "data size mismatch in operation");
82
+ }
83
+ }
84
+ }
85
+ va_end(args);
86
+
87
+ if ( all_scalar && max < 0 ) {
88
+ max = 1;
89
+ }
90
+
91
+ return max;
92
+ }
93
+
94
+ /* ------------------------------------------------------------------- */
95
+
96
+ int32_t
97
+ ca_get_loop_count (int n, ...)
98
+ {
99
+ CArray *ca;
100
+ int32_t elements = -1;
101
+ int32_t is_scalar = 1;
102
+ va_list args;
103
+ va_start(args, n);
104
+ while ( n-- ) {
105
+ ca = va_arg(args, CArray*);
106
+ if ( ca_is_scalar(ca) ) {
107
+ continue;
108
+ }
109
+ is_scalar = 0;
110
+ if ( elements == -1 || ca->elements < elements ) {
111
+ elements = ca->elements;
112
+ }
113
+ }
114
+ va_end(args);
115
+ if ( elements == -1 && is_scalar ) {
116
+ elements = 1;
117
+ }
118
+
119
+ if ( elements == -1 ) {
120
+ rb_raise(rb_eRuntimeError, "no data to process");
121
+ }
122
+ return elements;
123
+ }
124
+
125
+ /*
126
+ ca_parse_range and ca_parse_range_without_check parse the following
127
+ types of range specifications
128
+
129
+ i
130
+
131
+ nil
132
+ i..j
133
+
134
+ [nil]
135
+ [i..j]
136
+
137
+ [nil, k]
138
+ [i..j, k]
139
+
140
+ [i]
141
+ [i,n]
142
+ [i,n,k]
143
+ */
144
+
145
+ void
146
+ ca_parse_range (VALUE arg, int size, int *poffset, int *pcount, int *pstep)
147
+ {
148
+ int32_t first, start, last, count, step, bound, excl;
149
+
150
+ retry:
151
+
152
+ if ( NIL_P(arg) ) { /* nil */
153
+ *poffset = 0;
154
+ *pcount = size;
155
+ *pstep = 1;
156
+ }
157
+ else if ( rb_obj_is_kind_of(arg, rb_cInteger) ) {
158
+ /* i */
159
+ start = FIX2INT(arg);
160
+ CA_CHECK_INDEX(start, size);
161
+ *poffset = start;
162
+ *pcount = 1;
163
+ *pstep = 1;
164
+ }
165
+ else if ( rb_obj_is_kind_of(arg, rb_cRange) ) {
166
+ /* i..j */
167
+ first = NUM2INT(RANGE_BEG(arg));
168
+ last = NUM2INT(RANGE_END(arg));
169
+ excl = RTEST(RANGE_EXCL(arg));
170
+ CA_CHECK_INDEX(first, size);
171
+ if ( last < 0 ) {
172
+ last += size;
173
+ }
174
+ if ( excl ) {
175
+ last += ( (last>=first) ? -1 : 1 );
176
+ }
177
+ if ( last < 0 || last >= size ) {
178
+ rb_raise(rb_eIndexError,
179
+ "invalid index range");
180
+ }
181
+ *poffset = first;
182
+ *pcount = abs(last - first) + 1;
183
+ *pstep = 1;
184
+ }
185
+ else if ( TYPE(arg) == T_ARRAY ) {
186
+ if ( RARRAY_LEN(arg) == 1 ) { /* [nil] or [i..j] or [i] */
187
+ arg = rb_ary_entry(arg, 0);
188
+ goto retry;
189
+ }
190
+ else if ( RARRAY_LEN(arg) == 2 ) {
191
+ VALUE arg0 = rb_ary_entry(arg, 0);
192
+ VALUE arg1 = rb_ary_entry(arg, 1);
193
+ if ( NIL_P(arg0) ) { /* [nil,k] */
194
+ step = NUM2INT(arg1);
195
+ if ( step == 0 ) {
196
+ rb_raise(rb_eRuntimeError, "step should not be 0");
197
+ }
198
+ start = 0;
199
+ count = (size-1)/abs(step) + 1;
200
+ bound = start + (count - 1)*step;
201
+ CA_CHECK_INDEX(start, size);
202
+ CA_CHECK_INDEX(bound, size);
203
+ *poffset = start;
204
+ *pcount = count;
205
+ *pstep = step;
206
+ }
207
+ else if ( rb_obj_is_kind_of(arg0, rb_cRange) ) { /* [i..j,k] */
208
+ start = NUM2INT(RANGE_BEG(arg0));
209
+ last = NUM2INT(RANGE_END(arg0));
210
+ excl = RTEST(RANGE_EXCL(arg0));
211
+ step = NUM2INT(arg1);
212
+ if ( step == 0 ) {
213
+ rb_raise(rb_eRuntimeError, "step should not be 0");
214
+ }
215
+ if ( last < 0 ) {
216
+ last += size;
217
+ }
218
+ if ( excl ) {
219
+ last += ( (last>=start) ? -1 : 1 );
220
+ }
221
+ if ( last < 0 || last >= size ) {
222
+ rb_raise(rb_eIndexError, "index out of range");
223
+ }
224
+ CA_CHECK_INDEX(start, size);
225
+ if ( (last - start) * step < 0 ) {
226
+ count = 1;
227
+ }
228
+ else {
229
+ count = abs(last - start)/abs(step) + 1;
230
+ }
231
+ bound = start + (count - 1)*step;
232
+ CA_CHECK_INDEX(bound, size);
233
+ *poffset = start;
234
+ *pcount = count;
235
+ *pstep = step;
236
+ }
237
+ else { /* [i,n] */
238
+ start = NUM2INT(arg0);
239
+ count = NUM2INT(arg1);
240
+ bound = start + (count - 1);
241
+ CA_CHECK_INDEX(start, size);
242
+ CA_CHECK_INDEX(bound, size);
243
+ *poffset = start;
244
+ *pcount = count;
245
+ *pstep = 1;
246
+ }
247
+ }
248
+ else if ( RARRAY_LEN(arg) == 3 ) { /* [i,n,k] */
249
+ start = NUM2INT(rb_ary_entry(arg, 0));
250
+ count = NUM2INT(rb_ary_entry(arg, 1));
251
+ step = NUM2INT(rb_ary_entry(arg, 2));
252
+ if ( step == 0 ) {
253
+ rb_raise(rb_eRuntimeError, "step should not be 0");
254
+ }
255
+ bound = start + (count - 1)*step;
256
+ CA_CHECK_INDEX(start, size);
257
+ CA_CHECK_INDEX(bound, size);
258
+ *poffset = start;
259
+ *pcount = count;
260
+ *pstep = step;
261
+ }
262
+ else {
263
+ rb_raise(rb_eRuntimeError, "unknown range specification");
264
+ }
265
+ }
266
+ else {
267
+ rb_raise(rb_eRuntimeError, "unknown range specification");
268
+ }
269
+ }
270
+
271
+ void
272
+ ca_parse_range_without_check (VALUE arg, int size,
273
+ int *poffset, int *pcount, int *pstep)
274
+ {
275
+ int32_t first, start, last, count, step, bound, excl;
276
+
277
+ retry:
278
+
279
+ if ( NIL_P(arg) ) { /* nil */
280
+ *poffset = 0;
281
+ *pcount = size;
282
+ *pstep = 1;
283
+ }
284
+ else if ( rb_obj_is_kind_of(arg, rb_cInteger) ) {
285
+ /* i */
286
+ start = FIX2INT(arg);
287
+ *poffset = start;
288
+ *pcount = 1;
289
+ *pstep = 1;
290
+ }
291
+ else if ( rb_obj_is_kind_of(arg, rb_cRange) ) {
292
+ /* i..j */
293
+ first = NUM2INT(RANGE_BEG(arg));
294
+ last = NUM2INT(RANGE_END(arg));
295
+ excl = RTEST(RANGE_EXCL(arg));
296
+ if ( excl ) {
297
+ last += ( (last>=first) ? -1 : 1 );
298
+ }
299
+ *poffset = first;
300
+ *pcount = last - first + 1;
301
+ *pstep = 1;
302
+ }
303
+ else if ( TYPE(arg) == T_ARRAY ) {
304
+ if ( RARRAY_LEN(arg) == 1 ) { /* [nil] or [i..j] or [i] */
305
+ arg = rb_ary_entry(arg, 0);
306
+ goto retry;
307
+ }
308
+ else if ( RARRAY_LEN(arg) == 2 ) {
309
+ VALUE arg0 = rb_ary_entry(arg, 0);
310
+ VALUE arg1 = rb_ary_entry(arg, 1);
311
+ if ( NIL_P(arg0) ) { /* [nil,k] */
312
+ start = 0;
313
+ step = NUM2INT(arg1);
314
+ count = (size-1)/abs(step) + 1;
315
+ bound = start + (count - 1)*step;
316
+ *poffset = start;
317
+ *pcount = count;
318
+ *pstep = step;
319
+ }
320
+ else if ( rb_obj_is_kind_of(arg0, rb_cRange) ) { /* [i..j,k] */
321
+ start = NUM2INT(RANGE_BEG(arg0));
322
+ last = NUM2INT(RANGE_END(arg0));
323
+ excl = RTEST(RANGE_EXCL(arg0));
324
+ step = NUM2INT(arg1);
325
+ if ( excl ) {
326
+ last += ( (last>=start) ? -1 : 1 );
327
+ }
328
+ count = (last - start)/abs(step) + 1;
329
+ bound = start + (count - 1)*step;
330
+ *poffset = start;
331
+ *pcount = count;
332
+ *pstep = step;
333
+ }
334
+ else { /* [i,n] */
335
+ start = NUM2INT(arg0);
336
+ count = NUM2INT(arg1);
337
+ bound = start + (count - 1);
338
+ *poffset = start;
339
+ *pcount = count;
340
+ *pstep = 1;
341
+ }
342
+ }
343
+ else if ( RARRAY_LEN(arg) == 3 ) { /* [i,n,k] */
344
+ start = NUM2INT(rb_ary_entry(arg, 0));
345
+ count = NUM2INT(rb_ary_entry(arg, 1));
346
+ step = NUM2INT(rb_ary_entry(arg, 2));
347
+ bound = start + (count - 1)*step;
348
+ *poffset = start;
349
+ *pcount = count;
350
+ *pstep = step;
351
+ }
352
+ else {
353
+ rb_raise(rb_eRuntimeError, "unknown range specification");
354
+ }
355
+ }
356
+ else {
357
+ rb_raise(rb_eRuntimeError, "unknown range specification");
358
+ }
359
+ }
360
+
361
+ int32_t
362
+ ca_bounds_normalize_index (int8_t bounds, int32_t size0, int32_t k)
363
+ {
364
+ switch ( bounds ) {
365
+ case CA_BOUNDS_MASK:
366
+ case CA_BOUNDS_FILL:
367
+ return k;
368
+ case CA_BOUNDS_PERIODIC:
369
+ if ( k >= 0 ) {
370
+ return k % size0;
371
+ }
372
+ else {
373
+ k = (-k) % size0;
374
+ return ( ! k ) ? 0 : size0 - k;
375
+ }
376
+ case CA_BOUNDS_RUBY:
377
+ if ( k < 0 ) {
378
+ k += size0;
379
+ }
380
+ if ( k < 0 || k >= size0 ) {
381
+ rb_raise(rb_eRuntimeError,
382
+ "window index out of range");
383
+ }
384
+ return k;
385
+ case CA_BOUNDS_STRICT:
386
+ if ( k < 0 || k >= size0 ) {
387
+ rb_raise(rb_eRuntimeError,
388
+ "window index out of range");
389
+ }
390
+ return k;
391
+ case CA_BOUNDS_NEAREST:
392
+ return ( k < 0 ) ? 0 : ( k >= size0 ) ? size0 - 1 : k;
393
+ case CA_BOUNDS_REFLECT:
394
+ if ( k < 0 ) {
395
+ k = -k - 1;
396
+ }
397
+ k = k % (2*size0);
398
+ return ( k < size0 ) ? k : 2*size0-1-k;
399
+ default:
400
+ rb_raise(rb_eRuntimeError,
401
+ "unknown window boundary specified (%i)", bounds);
402
+ }
403
+ }
404
+
405
+ static VALUE
406
+ rb_ca_s_scan_float (int argc, VALUE *argv, VALUE self)
407
+ {
408
+ volatile VALUE rstr, rfval;
409
+ double value;
410
+ int count;
411
+
412
+ rb_scan_args(argc, argv, "11", &rstr, &rfval);
413
+
414
+ if ( NIL_P(rstr) ) {
415
+ return ( NIL_P(rfval) ) ? rb_float_new(0.0/0.0) : rfval;
416
+ }
417
+
418
+ Check_Type(rstr, T_STRING);
419
+
420
+ count = sscanf(StringValuePtr(rstr), "%lf", &value);
421
+
422
+ if ( count == 1 ) {
423
+ return rb_float_new(value);
424
+ }
425
+ else {
426
+ return ( NIL_P(rfval) ) ? rb_float_new(0.0/0.0) : rfval;
427
+ }
428
+ }
429
+
430
+ static VALUE
431
+ rb_ca_s_scan_int (int argc, VALUE *argv, VALUE self)
432
+ {
433
+ volatile VALUE rstr, rfval;
434
+ long value;
435
+ int count;
436
+
437
+ rb_scan_args(argc, argv, "11", &rstr, &rfval);
438
+
439
+ if ( NIL_P(rstr) ) {
440
+ return ( NIL_P(rfval) ) ? INT2FIX(0) : rfval;
441
+ }
442
+
443
+ Check_Type(rstr, T_STRING);
444
+
445
+ count = sscanf(StringValuePtr(rstr), "%li", &value);
446
+
447
+ if ( count == 1 ) {
448
+ return LONG2NUM(value);
449
+ }
450
+ else {
451
+ return ( NIL_P(rfval) ) ? INT2FIX(0) : rfval;
452
+ }
453
+ }
454
+
455
+ static const struct {
456
+ char *name;
457
+ int data_type;
458
+ } ca_name_to_type[] = {
459
+ { "fixlen", CA_FIXLEN },
460
+ { "boolean", CA_BOOLEAN },
461
+ { "int8", CA_INT8 },
462
+ { "uint8", CA_UINT8 },
463
+ { "int16", CA_INT16 },
464
+ { "uint16", CA_UINT16 },
465
+ { "int32", CA_INT32 },
466
+ { "uint32", CA_UINT32 },
467
+ { "int64", CA_INT64 },
468
+ { "uint64", CA_UINT64 },
469
+ { "float32", CA_FLOAT32 },
470
+ { "float64", CA_FLOAT64 },
471
+ { "float128", CA_FLOAT128 },
472
+ { "cmplx64", CA_CMPLX64 },
473
+ { "cmplx128", CA_CMPLX128 },
474
+ { "cmplx256", CA_CMPLX256 },
475
+ { "object", CA_OBJECT },
476
+ { "byte", CA_UINT8 },
477
+ { "short", CA_INT16 },
478
+ { "int", CA_INT32 },
479
+ { "float", CA_FLOAT32 },
480
+ { "double", CA_FLOAT64 },
481
+ { "complex", CA_CMPLX64 },
482
+ { "dcomplex", CA_CMPLX128 },
483
+ { NULL, CA_NONE },
484
+ };
485
+
486
+
487
+
488
+ int8_t
489
+ rb_ca_guess_type (VALUE obj)
490
+ {
491
+ VALUE inspect;
492
+
493
+ if ( TYPE(obj) == T_FIXNUM ) {
494
+ return NUM2INT(obj);
495
+ }
496
+ else if ( TYPE(obj) == T_STRING ) {
497
+ char *name0, *name = StringValuePtr(obj);
498
+ int i;
499
+ i = 0;
500
+ while ( ( name0 = ca_name_to_type[i].name ) ) {
501
+ name0 = ca_name_to_type[i].name;
502
+ if ( ! strncmp(name, name0, strlen(name0)) ) {
503
+ return ca_name_to_type[i].data_type;
504
+ }
505
+ i++;
506
+ }
507
+ }
508
+ else if ( TYPE(obj) == T_SYMBOL ) {
509
+ return rb_ca_guess_type(rb_str_new2(rb_id2name(SYM2ID(obj))));
510
+ }
511
+ else if ( TYPE(obj) == T_CLASS ) {
512
+ ca_check_data_class(obj);
513
+ return CA_FIXLEN;
514
+ }
515
+
516
+ inspect = rb_inspect(obj);
517
+ rb_raise(rb_eRuntimeError,
518
+ "<%s> is unknown data_type representation", StringValuePtr(inspect));
519
+ }
520
+
521
+ void
522
+ rb_ca_guess_type_and_bytes (VALUE rtype, VALUE rbytes,
523
+ int8_t *data_type, int32_t *bytes)
524
+ {
525
+ *data_type = rb_ca_guess_type(rtype);
526
+
527
+ if ( *data_type == CA_FIXLEN ) {
528
+ if ( TYPE(rtype) == T_CLASS ) {
529
+ *bytes = NUM2INT(rb_const_get(rtype, rb_intern("DATA_SIZE")));
530
+ }
531
+ else {
532
+ if ( NIL_P(rbytes) ) {
533
+ *bytes = 0;
534
+ }
535
+ else {
536
+ *bytes = NUM2INT(rbytes);
537
+ }
538
+ }
539
+ }
540
+ else {
541
+ CA_CHECK_DATA_TYPE(*data_type);
542
+ *bytes = ca_sizeof[*data_type];
543
+ }
544
+ }
545
+
546
+ /* rdoc:
547
+ def CArray.guess_type_and_bytes (type, bytes=0)
548
+ end
549
+ */
550
+
551
+ static VALUE
552
+ rb_ca_s_guess_type_and_bytes (int argc, VALUE *argv, VALUE klass)
553
+ {
554
+ VALUE rtype, rbytes;
555
+ int8_t data_type;
556
+ int32_t bytes;
557
+ rb_scan_args(argc, argv, "11", &rtype, &rbytes);
558
+ rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
559
+ return rb_assoc_new(INT2FIX(data_type), LONG2NUM(bytes));
560
+ }
561
+
562
+ VALUE
563
+ rb_pop_options (int *argc, VALUE **argv)
564
+ {
565
+ VALUE ropt;
566
+ if ( ( *argc > 0 ) && ( TYPE( (*argv)[*argc-1] ) == T_HASH ) ) {
567
+ ropt = (*argv)[*argc-1];
568
+ (*argc) -= 1;
569
+ }
570
+ else {
571
+ ropt = Qnil;
572
+ }
573
+ return ropt;
574
+ }
575
+
576
+
577
+ char *
578
+ strsep1(char **sp, const char sep)
579
+ {
580
+ char *p = *sp;
581
+ char *p0 = *sp;
582
+
583
+ if ( p == NULL ) {
584
+ return NULL;
585
+ }
586
+
587
+ while ( *p != '\0' ) {
588
+ if ( *p == sep ) {
589
+ *p = '\0';
590
+ *sp = p+1;
591
+ return p0;
592
+ }
593
+ else {
594
+ p++;
595
+ }
596
+ }
597
+
598
+ *sp = NULL;
599
+ return p0;
600
+ }
601
+
602
+ #if RUBY_VERSION_CODE >= 220
603
+ static VALUE
604
+ rb_hash_has_key(VALUE hash, VALUE key)
605
+ {
606
+ if ( RHASH_EMPTY_P(hash) ) {
607
+ return Qfalse;
608
+ }
609
+ if (st_lookup(RHASH_TBL(hash), key, 0)) {
610
+ return Qtrue;
611
+ }
612
+ return Qfalse;
613
+ }
614
+ #else
615
+ #if RUBY_VERSION_CODE >= 190
616
+ static VALUE
617
+ rb_hash_has_key(VALUE hash, VALUE key)
618
+ {
619
+ if (!RHASH(hash)->ntbl) {
620
+ return Qfalse;
621
+ }
622
+ if (st_lookup(RHASH(hash)->ntbl, key, 0)) {
623
+ return Qtrue;
624
+ }
625
+ return Qfalse;
626
+ }
627
+ #else
628
+ static VALUE
629
+ rb_hash_has_key (VALUE hash, VALUE key)
630
+ {
631
+ if (st_lookup(RHASH(hash)->tbl, key, 0) ) {
632
+ return Qtrue;
633
+ }
634
+ return Qfalse;
635
+ }
636
+ #endif
637
+ #endif
638
+
639
+ void
640
+ rb_scan_options (VALUE ropt, const char *spec_in, ...)
641
+ {
642
+ VALUE *vp;
643
+ char *sp, *tok, *spec;
644
+ int has_option = 0;
645
+ va_list vals;
646
+
647
+ if ( TYPE(ropt) == T_HASH ) {
648
+ has_option = 1;
649
+ }
650
+ else if ( ! NIL_P(ropt) ) {
651
+ VALUE inspect = rb_inspect(ropt);
652
+ rb_raise(rb_eArgError,
653
+ "<%s> is invalid option specifier",
654
+ StringValuePtr(inspect));
655
+ }
656
+
657
+ va_start(vals, spec_in);
658
+ sp = spec = strdup(spec_in);
659
+ tok = strsep1(&sp, ',');
660
+ while ( tok != NULL ) {
661
+ vp = va_arg(vals, VALUE*);
662
+ if ( has_option ) {
663
+ VALUE key = ID2SYM(rb_intern(tok));
664
+ if ( rb_hash_has_key(ropt, key) ) {
665
+ *vp = rb_hash_aref(ropt, key);
666
+ }
667
+ else {
668
+ if ( *vp != CA_NIL ) {
669
+ *vp = Qnil;
670
+ }
671
+ }
672
+ }
673
+ else {
674
+ if ( *vp != CA_NIL ) {
675
+ *vp = Qnil;
676
+ }
677
+ }
678
+ tok = strsep1(&sp, ',');
679
+ }
680
+ va_end(vals);
681
+ free(spec);
682
+
683
+ return;
684
+ }
685
+
686
+ void
687
+ rb_set_options (VALUE ropt, const char *spec_in, ...)
688
+ {
689
+ VALUE rval;
690
+ char *sp, *tok, *spec;
691
+ int has_option = 0;
692
+ va_list vals;
693
+
694
+ if ( TYPE(ropt) == T_HASH ) {
695
+ has_option = 1;
696
+ }
697
+ else if ( ! NIL_P(ropt) ) {
698
+ VALUE inspect = rb_inspect(ropt);
699
+ rb_raise(rb_eArgError,
700
+ "<%s> is invalid option specifier",
701
+ StringValuePtr(inspect));
702
+ }
703
+
704
+ va_start(vals, spec_in);
705
+ sp = spec = strdup(spec_in);
706
+ tok = strsep1(&sp, ',');
707
+ while ( tok != NULL ) {
708
+ rval = va_arg(vals, VALUE);
709
+ if ( has_option ) {
710
+ rb_hash_aset(ropt, ID2SYM(rb_intern(tok)), rval);
711
+ }
712
+ tok = strsep1(&sp, ',');
713
+ }
714
+ va_end(vals);
715
+ free(spec);
716
+
717
+ return;
718
+ }
719
+
720
+
721
+ void
722
+ Init_carray_utils ()
723
+ {
724
+ #if RUBY_VERSION_CODE >= 190
725
+ #else
726
+ id_beg = rb_intern("begin");
727
+ id_end = rb_intern("end");
728
+ id_excl = rb_intern("excl");
729
+ #endif
730
+
731
+ rb_define_singleton_method(rb_cCArray, "_scan_float",
732
+ rb_ca_s_scan_float, -1);
733
+ rb_define_singleton_method(rb_cCArray, "_scan_int",
734
+ rb_ca_s_scan_int, -1);
735
+
736
+ rb_define_singleton_method(rb_cCArray, "guess_type_and_bytes",
737
+ rb_ca_s_guess_type_and_bytes, -1);
738
+
739
+ }
740
+