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,681 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ carray_generate.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
+ #include "carray.h"
15
+
16
+ /* ----------------------------------------------------------------- */
17
+
18
+ /* rdoc:
19
+ class CArray
20
+ # Sets true at the given index for the boolean array and returns self.
21
+ # It accept the arguments same as for CArray#[].
22
+ def set (*argv)
23
+ end
24
+ # Sets false at the given index for the boolean array and returns self.
25
+ # It accept the arguments same as for CArray#[].
26
+ def unset (*argv)
27
+ end
28
+ end
29
+ */
30
+
31
+ static VALUE
32
+ rb_ca_boolean_set (int argc, VALUE *argv, VALUE self)
33
+ {
34
+ VALUE one = INT2FIX(1);
35
+ rb_ca_modify(self);
36
+ if ( ! rb_ca_is_boolean_type(self) ) {
37
+ rb_raise(rb_eCADataTypeError, "reciever should be a boolean array");
38
+ }
39
+ rb_ca_store2(self, argc, argv, one);
40
+ return self;
41
+ }
42
+
43
+ static VALUE
44
+ rb_ca_boolean_unset (int argc, VALUE *argv, VALUE self)
45
+ {
46
+ VALUE zero = INT2FIX(0);
47
+ rb_ca_modify(self);
48
+ if ( ! rb_ca_is_boolean_type(self) ) {
49
+ rb_raise(rb_eCADataTypeError, "reciever should be a boolean array");
50
+ }
51
+ rb_ca_store2(self, argc, argv, zero);
52
+ return self;
53
+ }
54
+
55
+ /* ----------------------------------------------------------------- */
56
+
57
+ /* rdoc:
58
+ class CArray
59
+ # Returns the 1d index array for non-zero elements of self
60
+ def where
61
+ end
62
+ end
63
+ */
64
+
65
+ VALUE
66
+ rb_ca_where (VALUE self)
67
+ {
68
+ volatile VALUE bool, obj;
69
+ CArray *ca, *co;
70
+ boolean8_t *p, *m;
71
+ int32_t *q;
72
+ int32_t i, count;
73
+
74
+ bool = ( ! rb_ca_is_boolean_type(self) ) ? rb_ca_to_boolean(self) : self;
75
+
76
+ Data_Get_Struct(bool, CArray, ca);
77
+
78
+ ca_attach(ca);
79
+
80
+ /* calculate elements of output array */
81
+ p = (boolean8_t *) ca->ptr;
82
+ m = ca_mask_ptr(ca);
83
+ count = 0;
84
+ if ( m ) {
85
+ for (i=0; i<ca->elements; i++) {
86
+ if ( ( ! *m ) && ( *p ) ) { count++; } /* not-masked && true */
87
+ m++; p++;
88
+ }
89
+ }
90
+ else {
91
+ for (i=0; i<ca->elements; i++) {
92
+ if ( *p ) { count++; } /* true */
93
+ p++;
94
+ }
95
+ }
96
+
97
+ /* create output array */
98
+ obj = rb_carray_new(CA_INT32, 1, &count, 0, NULL);
99
+ Data_Get_Struct(obj, CArray, co);
100
+
101
+ /* store address which elements is true to output array */
102
+ p = (boolean8_t *) ca->ptr;
103
+ q = (int32_t *) co->ptr;
104
+ m = ca_mask_ptr(ca);
105
+ if ( m ) {
106
+ for (i=0; i<ca->elements; i++) { /* not-masked && true */
107
+ if ( ( ! *m ) && ( *p ) ) { *q = i; q++; }
108
+ m++; p++;
109
+ }
110
+ }
111
+ else { /* true */
112
+ for (i=0; i<ca->elements; i++) {
113
+ if ( *p ) { *q = i; q++; }
114
+ p++;
115
+ }
116
+ }
117
+
118
+ ca_detach(ca);
119
+
120
+ return obj;
121
+ }
122
+
123
+ /* ----------------------------------------------------------------- */
124
+
125
+ #define proc_seq_bang(type, from, to) \
126
+ { \
127
+ type *p = (type *)ca->ptr; \
128
+ int32_t i; \
129
+ if ( NIL_P(roffset) && NIL_P(rstep) ) { \
130
+ for (i=0; i<ca->elements; i++) { \
131
+ *p++ = to(i); \
132
+ } \
133
+ } \
134
+ else if ( rb_obj_is_kind_of(rstep, rb_cFloat) || \
135
+ rb_obj_is_kind_of(roffset, rb_cFloat) ) { \
136
+ type offset = (NIL_P(roffset)) ? (type) 0 : from(roffset); \
137
+ double step = (NIL_P(rstep)) ? 1 : NUM2DBL(rstep); \
138
+ for (i=0; i<ca->elements; i++) { \
139
+ *p++ = to(step*i+offset); \
140
+ } \
141
+ } \
142
+ else { \
143
+ type offset = (NIL_P(roffset)) ? (type) 0 : from(roffset); \
144
+ type step = (NIL_P(rstep)) ? (type) 1 : from(rstep); \
145
+ for (i=0; i<ca->elements; i++) { \
146
+ *p++ = to(step*i+offset); \
147
+ } \
148
+ } \
149
+ }
150
+
151
+ #define proc_seq_bang_with_block(type, from, to) \
152
+ { \
153
+ type *p = (type *)ca->ptr; \
154
+ int32_t i; \
155
+ if ( NIL_P(roffset) && NIL_P(rstep) ) { \
156
+ for (i=0; i<ca->elements; i++) { \
157
+ *p++ = from(rb_yield(INT2NUM(i))); \
158
+ } \
159
+ } \
160
+ else if ( rb_obj_is_kind_of(rstep, rb_cFloat) || \
161
+ rb_obj_is_kind_of(roffset, rb_cFloat)) { \
162
+ type offset = (NIL_P(roffset)) ? (type) 0 : from(roffset); \
163
+ double step = (NIL_P(rstep)) ? 1 : NUM2DBL(rstep); \
164
+ for (i=0; i<ca->elements; i++) { \
165
+ *p++ = from(rb_yield(rb_float_new(step*i+offset))); \
166
+ } \
167
+ } \
168
+ else { \
169
+ type offset = (NIL_P(roffset)) ? (type) 0 : from(roffset); \
170
+ type step = (NIL_P(rstep)) ? (type) 1 : from(rstep); \
171
+ for (i=0; i<ca->elements; i++) { \
172
+ *p++ = from(rb_yield(INT2NUM(step*i+offset))); \
173
+ } \
174
+ } \
175
+ }
176
+
177
+ static VALUE
178
+ rb_ca_seq_bang_object (int argc, VALUE *argv, VALUE self)
179
+ {
180
+ volatile VALUE roffset, rstep, rval, rmethod = Qnil;
181
+ CArray *ca;
182
+ VALUE *p;
183
+ int32_t i;
184
+
185
+ Data_Get_Struct(self, CArray, ca);
186
+
187
+ rb_scan_args((argc>2) ? 2 : argc, argv, "02", &roffset, &rstep);
188
+
189
+ if ( TYPE(rstep) == T_SYMBOL ) { /* e.g. a.seq("a", :succ) */
190
+ rmethod = rstep;
191
+ rstep = Qnil;
192
+ }
193
+
194
+ ca_allocate(ca);
195
+
196
+ if ( ca_has_mask(ca) ) {
197
+ ca_clear_mask(ca); /* clear all mask */
198
+ }
199
+
200
+ p = (VALUE *)ca->ptr;
201
+ if ( rb_obj_is_kind_of(roffset, rb_cFloat) ||
202
+ rb_obj_is_kind_of(rstep, rb_cFloat) ) { /* a.seq(0.0, 1.0) */
203
+ double offset = ( NIL_P(roffset) ) ? 0 : NUM2DBL(roffset);
204
+ double step = ( NIL_P(rstep) ) ? 1 : NUM2DBL(rstep);
205
+ for (i=0; i<ca->elements; i++) {
206
+ *p++ = rb_float_new(step*i+offset);
207
+ }
208
+ }
209
+ else if ( NIL_P(roffset) ) {
210
+ if ( ! NIL_P(rstep) ) { /* a.seq(nil, 1) */
211
+ rb_raise(rb_eArgError,
212
+ "nil is invalid as offset for seq([offset[,step])");
213
+ }
214
+ for (i=0; i<ca->elements; i++) { /* a.seq() */
215
+ *p++ = INT2NUM(i);
216
+ }
217
+ }
218
+ else if ( ! NIL_P(rmethod) ) { /* a.seq(obj, :method) */
219
+ ID id_method = SYM2ID(rmethod);
220
+ *p++ = rval = roffset;
221
+ for (i=1; i<ca->elements; i++) {
222
+ *p++ = rval = rb_funcall2(rval, id_method, argc-2, argv+2);
223
+ }
224
+ }
225
+ else { /* a.seq(obj, step) */
226
+ ID id_plus = rb_intern("+");
227
+ rstep = ( NIL_P(rstep) ) ? INT2NUM(1) : rstep;
228
+ *p++ = rval = roffset;
229
+ for (i=1; i<ca->elements; i++) {
230
+ *p++ = rval = rb_funcall(rval, id_plus, 1, rstep);
231
+ }
232
+ }
233
+
234
+ if ( rb_block_given_p() ) {
235
+ p = (VALUE *)ca->ptr;
236
+ for(i=0; i<ca->elements; i++) {
237
+ *p = rb_yield(*p);
238
+ p++;
239
+ }
240
+ }
241
+
242
+ ca_sync(ca);
243
+ ca_detach(ca);
244
+
245
+ return self;
246
+ }
247
+
248
+ /* rdoc:
249
+ class CArray
250
+ # call-seq:
251
+ # seq (init_val=0, step=1)
252
+ # seq (init_val=0, step=1) {|x| ... }
253
+ # seq (init_val=0, step=A_symbol) ### for object array
254
+ # seq (init_val=0, step=A_symbol) {|x| ...} ### for object array
255
+ #
256
+ # Generates sequential data with initial value `init_val`
257
+ # and step value `step`. For object array, if the second argument
258
+ # is Symbol object, it will be interpreted as stepping method and
259
+ # it is called for the last element in each step.
260
+ #
261
+ def seq (init_val=0, step=1)
262
+ end
263
+ #
264
+ def seq! (init_val=0, step=1)
265
+ end
266
+ end
267
+ */
268
+
269
+ static VALUE
270
+ rb_ca_seq_bang_method (int argc, VALUE *argv, VALUE self)
271
+ {
272
+ volatile VALUE roffset, rstep;
273
+ CArray *ca;
274
+
275
+ rb_ca_modify(self);
276
+ Data_Get_Struct(self, CArray, ca);
277
+
278
+ /* delegate to rb_ca_seq_bang_object if data_type is object */
279
+ if ( ca_is_object_type(ca) ) {
280
+ return rb_ca_seq_bang_object(argc, argv, self);
281
+ }
282
+
283
+ rb_scan_args(argc, argv, "02", &roffset, &rstep);
284
+
285
+ ca_allocate(ca);
286
+
287
+ if ( ca_has_mask(ca) ) {
288
+ ca_clear_mask(ca); /* clear all mask */
289
+ }
290
+
291
+ if ( rb_block_given_p() ) { /* with block */
292
+ switch ( ca->data_type ) {
293
+ case CA_INT8: proc_seq_bang_with_block(int8_t, NUM2LONG, ); break;
294
+ case CA_UINT8: proc_seq_bang_with_block(uint8_t, NUM2ULONG, ); break;
295
+ case CA_INT16: proc_seq_bang_with_block(int16_t, NUM2LONG, ) ; break;
296
+ case CA_UINT16: proc_seq_bang_with_block(uint16_t, NUM2ULONG, ); break;
297
+ case CA_INT32: proc_seq_bang_with_block(int32_t, NUM2LONG, ); break;
298
+ case CA_UINT32: proc_seq_bang_with_block(uint32_t, NUM2ULONG, ); break;
299
+ case CA_INT64: proc_seq_bang_with_block(int64_t, NUM2LL, ); break;
300
+ case CA_UINT64: proc_seq_bang_with_block(uint64_t, rb_num2ull, ); break;
301
+ case CA_FLOAT32: proc_seq_bang_with_block(float32_t, NUM2DBL, ); break;
302
+ case CA_FLOAT64: proc_seq_bang_with_block(float64_t, NUM2DBL, ); break;
303
+ case CA_FLOAT128: proc_seq_bang_with_block(float128_t, NUM2DBL, ); break;
304
+ #ifdef HAVE_COMPLEX_H
305
+ case CA_CMPLX64: proc_seq_bang_with_block(cmplx64_t, (cmplx64_t) NUM2CC,); break;
306
+ case CA_CMPLX128: proc_seq_bang_with_block(cmplx128_t, NUM2CC, ); break;
307
+ case CA_CMPLX256: proc_seq_bang_with_block(cmplx256_t, (cmplx256_t) NUM2CC, ); break;
308
+ #endif
309
+ default: rb_raise(rb_eCADataTypeError,
310
+ "invalid data type of receiver");
311
+ }
312
+ }
313
+ else { /* without block */
314
+ switch ( ca->data_type ) {
315
+ case CA_INT8: proc_seq_bang(int8_t, NUM2LONG, ); break;
316
+ case CA_UINT8: proc_seq_bang(uint8_t, NUM2ULONG, ); break;
317
+ case CA_INT16: proc_seq_bang(int16_t, NUM2LONG, ) ; break;
318
+ case CA_UINT16: proc_seq_bang(uint16_t, NUM2ULONG, ); break;
319
+ case CA_INT32: proc_seq_bang(int32_t, NUM2LONG, ); break;
320
+ case CA_UINT32: proc_seq_bang(uint32_t, NUM2ULONG, ); break;
321
+ case CA_INT64: proc_seq_bang(int64_t, NUM2LL, ); break;
322
+ case CA_UINT64: proc_seq_bang(uint64_t, rb_num2ull, ); break;
323
+ case CA_FLOAT32: proc_seq_bang(float32_t, NUM2DBL, ); break;
324
+ case CA_FLOAT64: proc_seq_bang(float64_t, NUM2DBL, ); break;
325
+ case CA_FLOAT128: proc_seq_bang(float128_t, NUM2DBL, ); break;
326
+ #ifdef HAVE_COMPLEX_H
327
+ case CA_CMPLX64: proc_seq_bang(cmplx64_t, (cmplx64_t) NUM2CC, ); break;
328
+ case CA_CMPLX128: proc_seq_bang(cmplx128_t, NUM2CC, ); break;
329
+ case CA_CMPLX256: proc_seq_bang(cmplx256_t, (cmplx256_t) NUM2CC, ); break;
330
+ #endif
331
+ default: rb_raise(rb_eCADataTypeError,
332
+ "invalid data type of reciever");
333
+ }
334
+ }
335
+
336
+ ca_sync(ca);
337
+ ca_detach(ca);
338
+
339
+ return self;
340
+ }
341
+
342
+ static VALUE
343
+ rb_ca_seq_method (int argc, VALUE *argv, VALUE self)
344
+ {
345
+ volatile VALUE out = rb_ca_template(self);
346
+ return rb_ca_seq_bang_method(argc, argv, out);
347
+ }
348
+
349
+ VALUE
350
+ rb_ca_seq_bang (VALUE self, VALUE offset, VALUE step)
351
+ {
352
+ VALUE args[2] = { offset, step };
353
+ return rb_ca_seq_bang_method(2, args, self);
354
+ }
355
+
356
+ VALUE
357
+ rb_ca_seq_bang2 (VALUE self, int n, VALUE *args)
358
+ {
359
+ return rb_ca_seq_bang_method(n, args, self);
360
+ }
361
+
362
+ VALUE
363
+ rb_ca_seq (VALUE self, VALUE offset, VALUE step)
364
+ {
365
+ VALUE args[2] = { offset, step };
366
+ return rb_ca_seq_method(2, args, self);
367
+ }
368
+
369
+ VALUE
370
+ rb_ca_seq2 (VALUE self, int n, VALUE *args)
371
+ {
372
+ return rb_ca_seq_method(n, args, self);
373
+ }
374
+
375
+ /* ----------------------------------------------------------------- */
376
+
377
+
378
+ void
379
+ ca_swap_bytes (char *ptr, int32_t bytes, int32_t elements)
380
+ {
381
+ char *p;
382
+ char val;
383
+ int32_t i;
384
+
385
+ #define SWAP_BYTE(a, b) (val = (a), (a) = (b), (b) = val)
386
+
387
+ switch ( bytes ) {
388
+ case 1:
389
+ break;
390
+ case 2:
391
+ #ifdef _OPENMP
392
+ #pragma omp parallel for private(p)
393
+ #endif
394
+ for (i=0; i<elements; i++) {
395
+ p = ptr + 2*i;
396
+ SWAP_BYTE(p[0], p[1]);
397
+ }
398
+ break;
399
+ case 4:
400
+ #ifdef _OPENMP
401
+ #pragma omp parallel for private(p)
402
+ #endif
403
+ for (i=0; i<elements; i++) {
404
+ p = ptr + 4*i;
405
+ SWAP_BYTE(p[0], p[3]);
406
+ SWAP_BYTE(p[1], p[2]);
407
+ }
408
+ break;
409
+ case 8:
410
+ #ifdef _OPENMP
411
+ #pragma omp parallel for private(p)
412
+ #endif
413
+ for (i=0; i<elements; i++) {
414
+ p = ptr + 8*i;
415
+ SWAP_BYTE(p[0], p[7]);
416
+ SWAP_BYTE(p[1], p[6]);
417
+ SWAP_BYTE(p[2], p[5]);
418
+ SWAP_BYTE(p[3], p[4]);
419
+ }
420
+ break;
421
+ case 16:
422
+ #ifdef _OPENMP
423
+ #pragma omp parallel for private(p)
424
+ #endif
425
+ for (i=0; i<elements; i++) {
426
+ p = ptr + 16*i;
427
+ SWAP_BYTE(p[0], p[15]);
428
+ SWAP_BYTE(p[1], p[14]);
429
+ SWAP_BYTE(p[2], p[13]);
430
+ SWAP_BYTE(p[3], p[12]);
431
+ SWAP_BYTE(p[4], p[11]);
432
+ SWAP_BYTE(p[5], p[10]);
433
+ SWAP_BYTE(p[6], p[9]);
434
+ SWAP_BYTE(p[7], p[8]);
435
+ }
436
+ break;
437
+ default: {
438
+ char *p1, *p2;
439
+ #ifdef _OPENMP
440
+ #pragma omp parallel for private(p,p1,p2)
441
+ #endif
442
+ for (i=0; i<elements; i++) {
443
+ p = ptr + i*bytes;
444
+ p1 = p;
445
+ p2 = p+bytes-1;
446
+ while (p1<p2) {
447
+ SWAP_BYTE(*p1, *p2);
448
+ p1++; p2--;
449
+ }
450
+ }
451
+ break;
452
+ }
453
+ }
454
+
455
+ #undef SWAP_BYTE
456
+
457
+ }
458
+
459
+ /* rdoc:
460
+ class CArray
461
+ # Swaps the byte order of each element.
462
+ def swap_bytes
463
+ end
464
+ #
465
+ def swap_bytes!
466
+ end
467
+ end
468
+ */
469
+
470
+ VALUE
471
+ rb_ca_swap_bytes_bang (VALUE self)
472
+ {
473
+ CArray *ca;
474
+ int i;
475
+
476
+ rb_ca_modify(self);
477
+
478
+ if ( rb_ca_is_object_type(self) ) {
479
+ rb_raise(rb_eCADataTypeError, "object array can't swap bytes");
480
+ }
481
+
482
+ if ( rb_ca_is_fixlen_type(self) ) {
483
+ if ( rb_ca_has_data_class(self) ) {
484
+ volatile VALUE members = rb_ca_fields(self);
485
+ Check_Type(members, T_ARRAY);
486
+ for (i=0; i<RARRAY_LEN(members); i++) {
487
+ volatile VALUE obj = rb_ary_entry(members, i);
488
+ rb_ca_swap_bytes_bang(obj);
489
+ }
490
+ }
491
+ else {
492
+ Data_Get_Struct(self, CArray, ca);
493
+ ca_attach(ca);
494
+ ca_swap_bytes(ca->ptr, ca->bytes, ca->elements);
495
+ ca_sync(ca);
496
+ ca_detach(ca);
497
+ }
498
+ return self;
499
+ }
500
+
501
+ Data_Get_Struct(self, CArray, ca);
502
+
503
+ switch ( ca->data_type ) {
504
+ case CA_INT16:
505
+ case CA_UINT16:
506
+ ca_attach(ca);
507
+ ca_swap_bytes(ca->ptr, 2, ca->elements);
508
+ ca_sync(ca);
509
+ ca_detach(ca);
510
+ break;
511
+ case CA_INT32:
512
+ case CA_UINT32:
513
+ case CA_FLOAT32:
514
+ ca_attach(ca);
515
+ ca_swap_bytes(ca->ptr, 4, ca->elements);
516
+ ca_sync(ca);
517
+ ca_detach(ca);
518
+ break;
519
+ case CA_INT64:
520
+ case CA_UINT64:
521
+ case CA_FLOAT64:
522
+ ca_attach(ca);
523
+ ca_swap_bytes(ca->ptr, 8, ca->elements);
524
+ ca_sync(ca);
525
+ ca_detach(ca);
526
+ break;
527
+ case CA_FLOAT128:
528
+ ca_attach(ca);
529
+ ca_swap_bytes(ca->ptr, 16, ca->elements);
530
+ ca_sync(ca);
531
+ ca_detach(ca);
532
+ break;
533
+ case CA_CMPLX64:
534
+ ca_attach(ca);
535
+ ca_swap_bytes(ca->ptr, 4, 2 * ca->elements);
536
+ ca_sync(ca);
537
+ ca_detach(ca);
538
+ break;
539
+ case CA_CMPLX128:
540
+ ca_attach(ca);
541
+ ca_swap_bytes(ca->ptr, 8, 2 * ca->elements);
542
+ ca_sync(ca);
543
+ ca_detach(ca);
544
+ break;
545
+ case CA_CMPLX256:
546
+ ca_attach(ca);
547
+ ca_swap_bytes(ca->ptr, 16, 2 * ca->elements);
548
+ ca_sync(ca);
549
+ ca_detach(ca);
550
+ break;
551
+ }
552
+
553
+ return self;
554
+ }
555
+
556
+ VALUE
557
+ rb_ca_swap_bytes (VALUE self)
558
+ {
559
+ volatile VALUE out = rb_ca_copy(self);
560
+ return rb_ca_swap_bytes_bang(out);
561
+ }
562
+
563
+ /* ----------------------------------------------------------------- */
564
+
565
+ #define proc_trim_bang(type, from) \
566
+ { \
567
+ type *ptr = (type *) ca->ptr; \
568
+ boolean8_t *m = (ca->mask) ? (boolean8_t*) ca->mask->ptr : NULL; \
569
+ type min = (type) from(rmin); \
570
+ type max = (type) from(rmax); \
571
+ int32_t i; \
572
+ if ( m && rfval == CA_UNDEF) { \
573
+ for (i=ca->elements; i; i--, ptr++, m++) { \
574
+ if ( ! *m ) { \
575
+ if ( *ptr < min || *ptr >= max ) \
576
+ *m = 1; \
577
+ } \
578
+ } \
579
+ } \
580
+ else { \
581
+ int has_fill = ! ( NIL_P(rfval) ); \
582
+ type fill = (has_fill) ? from(rfval) : (type) 0; \
583
+ if ( m ) { \
584
+ for (i=ca->elements; i; i--, ptr++) { \
585
+ if ( ! *m++ ) { \
586
+ if ( *ptr >= max ) \
587
+ *ptr = (has_fill) ? fill : max; \
588
+ else if ( *ptr < min ) \
589
+ *ptr = (has_fill) ? fill : min; \
590
+ } \
591
+ } \
592
+ } \
593
+ else { \
594
+ for (i=ca->elements; i; i--, ptr++) { \
595
+ if ( *ptr >= max ) \
596
+ *ptr = (has_fill) ? fill : max; \
597
+ else if ( *ptr < min ) \
598
+ *ptr = (has_fill) ? fill : min; \
599
+ } \
600
+ } \
601
+ } \
602
+ }
603
+
604
+ /* rdoc:
605
+ class CArray
606
+ # trims the data into the range between min and max. If `fill_value`
607
+ # is given, the element out of the range between min and max is filled
608
+ # by `fill_value`
609
+ def trim (min, max, fill_value=nil)
610
+ end
611
+ #
612
+ def trim! (min, max, fill_value=nil)
613
+ end
614
+ end
615
+ */
616
+
617
+ static VALUE
618
+ rb_ca_trim_bang (int argc, VALUE *argv, VALUE self)
619
+ {
620
+ volatile VALUE rmin, rmax, rfval;
621
+ CArray *ca;
622
+
623
+ rb_ca_modify(self);
624
+
625
+ Data_Get_Struct(self, CArray, ca);
626
+
627
+ rb_scan_args(argc, argv, "21", &rmin, &rmax, &rfval);
628
+
629
+ if ( rfval == CA_UNDEF ) {
630
+ ca_create_mask(ca);
631
+ }
632
+
633
+ ca_attach(ca);
634
+
635
+ switch ( ca->data_type ) {
636
+ case CA_INT8: proc_trim_bang(int8_t, NUM2LONG); break;
637
+ case CA_UINT8: proc_trim_bang(uint8_t, NUM2ULONG); break;
638
+ case CA_INT16: proc_trim_bang(int16_t, NUM2LONG); break;
639
+ case CA_UINT16: proc_trim_bang(uint16_t, NUM2LONG); break;
640
+ case CA_INT32: proc_trim_bang(int32_t, NUM2LONG); break;
641
+ case CA_UINT32: proc_trim_bang(uint32_t, NUM2LONG); break;
642
+ case CA_INT64: proc_trim_bang(int64_t, NUM2LONG); break;
643
+ case CA_UINT64: proc_trim_bang(uint64_t, NUM2LONG); break;
644
+ case CA_FLOAT32: proc_trim_bang(float32_t, NUM2DBL); break;
645
+ case CA_FLOAT64: proc_trim_bang(float64_t, NUM2DBL); break;
646
+ case CA_FLOAT128: proc_trim_bang(float128_t, NUM2DBL); break;
647
+ default:
648
+ rb_raise(rb_eCADataTypeError,
649
+ "can not trim for non-numeric or complex data type");
650
+ }
651
+
652
+ ca_detach(ca);
653
+
654
+ return self;
655
+ }
656
+
657
+ static VALUE
658
+ rb_ca_trim (int argc, VALUE *argv, VALUE self)
659
+ {
660
+ volatile VALUE out = rb_ca_copy(self);
661
+ return rb_ca_trim_bang(argc, argv, out);
662
+ }
663
+
664
+
665
+ void
666
+ Init_carray_generate ()
667
+ {
668
+ rb_define_method(rb_cCArray, "set", rb_ca_boolean_set, -1);
669
+ rb_define_method(rb_cCArray, "unset", rb_ca_boolean_unset, -1);
670
+
671
+ rb_define_method(rb_cCArray, "where", rb_ca_where, 0);
672
+ rb_define_method(rb_cCArray, "seq!", rb_ca_seq_bang_method, -1);
673
+ rb_define_method(rb_cCArray, "seq", rb_ca_seq_method, -1);
674
+ rb_define_method(rb_cCArray, "swap_bytes!", rb_ca_swap_bytes_bang, 0);
675
+ rb_define_method(rb_cCArray, "swap_bytes", rb_ca_swap_bytes, 0);
676
+ rb_define_method(rb_cCArray, "trim!", rb_ca_trim_bang, -1);
677
+ rb_define_method(rb_cCArray, "trim", rb_ca_trim, -1);
678
+ }
679
+
680
+
681
+