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,299 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_obj_reduce.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 "carray.h"
14
+
15
+ static int8_t CA_OBJ_REDUCE;
16
+
17
+ static VALUE rb_cCAReduce;
18
+
19
+ /* rdoc:
20
+ class CAReduce < CAVirtual # :nodoc:
21
+ end
22
+ */
23
+
24
+ /* ------------------------------------------------------------------- */
25
+
26
+ int
27
+ ca_reduce_setup (CAReduce *ca, CArray *parent, int32_t count, int32_t offset)
28
+ {
29
+ int32_t elements;
30
+
31
+ /* check arguments */
32
+
33
+ if ( ! ca_is_boolean_type(parent) ) {
34
+ rb_raise(rb_eRuntimeError,
35
+ "[BUG] CAReduce can't inherit other than boolean array");
36
+ }
37
+
38
+ elements = parent->elements / count;
39
+
40
+ ca->obj_type = CA_OBJ_REDUCE;
41
+ ca->data_type = CA_BOOLEAN; /* data type is fixed to boolean */
42
+ ca->flags = 0;
43
+ ca->rank = 1;
44
+ ca->bytes = ca_sizeof[CA_BOOLEAN];
45
+ ca->elements = elements;
46
+ ca->ptr = NULL;
47
+ ca->mask = NULL;
48
+ ca->dim = &ca->elements;
49
+
50
+ ca->parent = parent;
51
+ ca->attach = 0;
52
+ ca->nosync = 0;
53
+
54
+ ca->count = count;
55
+ ca->offset = offset;
56
+
57
+ if ( ca_has_mask(parent) ) {
58
+ ca_create_mask(ca);
59
+ }
60
+
61
+ if ( ca_is_scalar(parent) ) {
62
+ ca_set_flag(ca, CA_FLAG_SCALAR);
63
+ }
64
+
65
+ return 0;
66
+ }
67
+
68
+ CAReduce *
69
+ ca_reduce_new (CArray *parent, int32_t count, int32_t offset)
70
+ {
71
+ CAReduce *ca = ALLOC(CAReduce);
72
+ ca_reduce_setup(ca, parent, count, offset);
73
+ return ca;
74
+ }
75
+
76
+ static void
77
+ free_ca_reduce (void *ap)
78
+ {
79
+ CAReduce *ca = (CAReduce *) ap;
80
+ if ( ca != NULL ) {
81
+ ca_free(ca->mask);
82
+ /* free(ca->dim); */
83
+ xfree(ca);
84
+ }
85
+ }
86
+
87
+ /* ------------------------------------------------------------------- */
88
+
89
+ static void *
90
+ ca_reduce_func_clone (void *ap)
91
+ {
92
+ CAReduce *ca = (CAReduce *) ap;
93
+ return ca_reduce_new(ca->parent, ca->count, ca->offset);
94
+ }
95
+
96
+ static char *
97
+ ca_reduce_func_ptr_at_addr (void *ap, int32_t addr)
98
+ {
99
+ CAReduce *ca = (CAReduce *) ap;
100
+ return ca->ptr + addr;
101
+ }
102
+
103
+ static char *
104
+ ca_reduce_func_ptr_at_index (void *ap, int32_t *idx)
105
+ {
106
+ CAReduce *ca = (CAReduce *) ap;
107
+ return ca_func[CA_OBJ_ARRAY].ptr_at_index(ca, idx);
108
+ }
109
+
110
+ static void
111
+ ca_reduce_func_fetch_addr (void *ap, int32_t addr, void *ptr)
112
+ {
113
+ CAReduce *ca = (CAReduce *) ap;
114
+ char q;
115
+ int i;
116
+ for (i=0; i<ca->count; i++) {
117
+ ca_fetch_addr(ca->parent, addr*ca->count+ca->offset, &q);
118
+ if ( q ) {
119
+ *(char*)ptr = (char) 1;
120
+ return;
121
+ }
122
+ }
123
+ *(char*)ptr = (char) 0;
124
+ }
125
+
126
+ static void
127
+ ca_reduce_func_store_addr (void *ap, int32_t addr, void *ptr)
128
+ {
129
+ CAReduce *ca = (CAReduce *) ap;
130
+ int32_t i;
131
+ for (i=0; i<ca->count; i++) {
132
+ ca_store_addr(ca->parent, addr*ca->count+i+ca->offset, ptr);
133
+ }
134
+ }
135
+
136
+ static void
137
+ ca_reduce_func_allocate (void *ap)
138
+ {
139
+ CAReduce *ca = (CAReduce *) ap;
140
+ ca_attach(ca->parent);
141
+ /* ca->ptr = ALLOC_N(char, ca->elements); */
142
+ ca->ptr = malloc_with_check(ca_length(ca));
143
+ }
144
+
145
+ static void
146
+ ca_reduce_func_attach (void *ap)
147
+ {
148
+ CAReduce *ca = (CAReduce *) ap;
149
+ char *p;
150
+ int32_t i;
151
+ ca_attach(ca->parent);
152
+ /* ca->ptr = ALLOC_N(char, ca->elements); */
153
+ ca->ptr = malloc_with_check(ca_length(ca));
154
+ p = ca->ptr;
155
+ for (i=0; i<ca->elements; i++) {
156
+ ca_reduce_func_fetch_addr(ca, i, p);
157
+ p++;
158
+ }
159
+ }
160
+
161
+ static void
162
+ ca_reduce_func_sync (void *ap)
163
+ {
164
+ CAReduce *ca = (CAReduce *) ap;
165
+ char *p;
166
+ int32_t i;
167
+ p = ca->ptr;
168
+ ca_attach(ca->parent);
169
+ for (i=0; i<ca->elements; i++) {
170
+ ca_reduce_func_store_addr(ca, i, p);
171
+ p++;
172
+ }
173
+ ca_sync(ca->parent);
174
+ ca_detach(ca->parent);
175
+ }
176
+
177
+ static void
178
+ ca_reduce_func_detach (void *ap)
179
+ {
180
+ CAReduce *ca = (CAReduce *) ap;
181
+ free(ca->ptr);
182
+ ca->ptr = NULL;
183
+ ca_detach(ca->parent);
184
+ }
185
+
186
+ static void
187
+ ca_reduce_func_copy_data (void *ap, void *ptr)
188
+ {
189
+ CAReduce *ca = (CAReduce *) ap;
190
+ int32_t i;
191
+ char *p;
192
+ ca_attach(ca->parent);
193
+ p = ptr;
194
+ for (i=0; i<ca->elements; i++) {
195
+ ca_reduce_func_fetch_addr(ca, i, p);
196
+ p++;
197
+ }
198
+ ca_detach(ca->parent);
199
+ }
200
+
201
+ static void
202
+ ca_reduce_func_sync_data (void *ap, void *ptr)
203
+ {
204
+ CAReduce *ca = (CAReduce *) ap;
205
+ char *p;
206
+ int32_t i;
207
+ ca_attach(ca->parent);
208
+ p = ptr;
209
+ for (i=0; i<ca->elements; i++) {
210
+ ca_reduce_func_store_addr(ca, i, p);
211
+ p++;
212
+ }
213
+ ca_sync(ca->parent);
214
+ ca_detach(ca->parent);
215
+ }
216
+
217
+ static void
218
+ ca_reduce_func_fill_data (void *ap, void *ptr)
219
+ {
220
+ CAReduce *ca = (CAReduce *) ap;
221
+ int32_t i;
222
+ ca_attach(ca->parent);
223
+ for (i=0; i<ca->elements; i++) {
224
+ ca_reduce_func_store_addr(ca, i, ptr);
225
+ }
226
+ ca_sync(ca->parent);
227
+ ca_detach(ca->parent);
228
+ }
229
+
230
+ static void
231
+ ca_reduce_func_create_mask (void *ap)
232
+ {
233
+ CAReduce *ca = (CAReduce *) ap;
234
+
235
+ ca_update_mask(ca->parent);
236
+ if ( ! ca->parent->mask ) {
237
+ ca_create_mask(ca->parent);
238
+ }
239
+
240
+ ca->mask = (CArray *) ca_reduce_new(ca->parent->mask, ca->count, ca->offset);
241
+ }
242
+
243
+ ca_operation_function_t ca_reduce_func = {
244
+ -1, /* CA_OBJ_REDUCE */
245
+ CA_VIRTUAL_ARRAY,
246
+ free_ca_reduce,
247
+ ca_reduce_func_clone,
248
+ ca_reduce_func_ptr_at_addr,
249
+ ca_reduce_func_ptr_at_index,
250
+ ca_reduce_func_fetch_addr,
251
+ NULL,
252
+ ca_reduce_func_store_addr,
253
+ NULL,
254
+ ca_reduce_func_allocate,
255
+ ca_reduce_func_attach,
256
+ ca_reduce_func_sync,
257
+ ca_reduce_func_detach,
258
+ ca_reduce_func_copy_data,
259
+ ca_reduce_func_sync_data,
260
+ ca_reduce_func_fill_data,
261
+ ca_reduce_func_create_mask,
262
+ };
263
+
264
+ /* ------------------------------------------------------------------- */
265
+
266
+ static VALUE
267
+ rb_ca_reduce_s_allocate (VALUE klass)
268
+ {
269
+ CAReduce *ca;
270
+ return Data_Make_Struct(klass, CAReduce, ca_mark, ca_free, ca);
271
+ }
272
+
273
+ static VALUE
274
+ rb_ca_reduce_initialize_copy (VALUE self, VALUE other)
275
+ {
276
+ CAReduce *ca, *cs;
277
+
278
+ Data_Get_Struct(self, CAReduce, ca);
279
+ Data_Get_Struct(other, CAReduce, cs);
280
+
281
+ ca_reduce_setup(ca, cs->parent, cs->count, cs->offset);
282
+
283
+ return self;
284
+ }
285
+
286
+ void
287
+ Init_ca_obj_reduce ()
288
+ {
289
+ rb_cCAReduce = rb_define_class("CAReduce", rb_cCAVirtual);
290
+
291
+ CA_OBJ_REDUCE = ca_install_obj_type(rb_cCAReduce, ca_reduce_func);
292
+ rb_define_const(rb_cObject, "CA_OBJ_REDUCE", INT2NUM(CA_OBJ_REDUCE));
293
+
294
+ rb_define_alloc_func(rb_cCAReduce, rb_ca_reduce_s_allocate);
295
+ rb_define_method(rb_cCAReduce, "initialize_copy",
296
+ rb_ca_reduce_initialize_copy, 1);
297
+ }
298
+
299
+
@@ -0,0 +1,627 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_obj_refer.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 "carray.h"
14
+
15
+ VALUE rb_cCARefer;
16
+
17
+ /* rdoc:
18
+ class CARefer < CAVirtual # :nodoc:
19
+ end
20
+ */
21
+
22
+ static int
23
+ ca_refer_setup (CARefer *ca, CArray *parent,
24
+ int8_t data_type, int8_t rank, int32_t *dim, int32_t bytes,
25
+ int32_t offset)
26
+ {
27
+ int32_t elements, ratio;
28
+ int32_t i;
29
+ int is_deformed;
30
+
31
+ CA_CHECK_DATA_TYPE(data_type);
32
+ CA_CHECK_RANK(rank);
33
+ CA_CHECK_DIM(rank, dim);
34
+ CA_CHECK_BYTES(data_type, bytes);
35
+
36
+ if ( ca_is_object_type(parent) && data_type != CA_OBJECT ) {
37
+ rb_raise(rb_eRuntimeError, "object carray can't be referred by other type");
38
+ }
39
+
40
+ if ( parent->elements && bytes > parent->bytes * parent->elements ) {
41
+ rb_raise(rb_eRuntimeError, "byte size mismatch");
42
+ }
43
+
44
+ /* calc datanum and check deformation */
45
+ is_deformed = ( rank == parent->rank ) ? 0 : 1;
46
+ ratio = 1;
47
+ elements = 1;
48
+ for (i=0; i<rank; i++) {
49
+ elements *= dim[i];
50
+ if ( dim[i] != parent->dim[i] ) {
51
+ is_deformed |= 1;
52
+ }
53
+ }
54
+ if ( bytes < parent->bytes ) {
55
+ if ( parent->bytes % bytes != 0 ) {
56
+ rb_raise(rb_eArgError, "invalid bytes");
57
+ }
58
+ is_deformed = -2;
59
+ ratio = parent->bytes / bytes;
60
+ }
61
+ else if ( bytes > parent->bytes ) {
62
+ if ( bytes % parent->bytes != 0 ) {
63
+ rb_raise(rb_eArgError, "invalid bytes");
64
+ }
65
+ is_deformed = 2;
66
+ ratio = bytes / parent->bytes;
67
+ }
68
+
69
+ if ( offset < 0 ) {
70
+ rb_raise(rb_eRuntimeError,
71
+ "negative offset for CARefer");
72
+ }
73
+
74
+ if ( ( bytes * elements + parent->bytes * offset ) >
75
+ ( parent->bytes * parent->elements ) ) {
76
+ rb_raise(rb_eRuntimeError, "data size too large for CARefer");
77
+ }
78
+
79
+ ca->obj_type = CA_OBJ_REFER;
80
+ ca->data_type = data_type;
81
+ ca->flags = 0;
82
+ ca->rank = rank;
83
+ ca->bytes = bytes;
84
+ ca->elements = elements;
85
+ ca->dim = ALLOC_N(int32_t, rank);
86
+ ca->ptr = NULL;
87
+ ca->mask = NULL;
88
+ ca->mask0 = NULL;
89
+
90
+ ca->parent = parent;
91
+ ca->attach = 0;
92
+ ca->nosync = 0;
93
+
94
+ ca->is_deformed = (int8_t) is_deformed;
95
+ ca->ratio = ratio;
96
+ ca->offset = offset;
97
+
98
+ if ( ca->offset > 0 && ca->is_deformed == 0 ) {
99
+ ca->is_deformed = 1;
100
+ }
101
+
102
+ memcpy(ca->dim, dim, rank * sizeof(int32_t));
103
+
104
+ if ( ca_is_scalar(parent) ) {
105
+ ca_set_flag(ca, CA_FLAG_SCALAR);
106
+ }
107
+
108
+ return 0;
109
+ }
110
+
111
+ CARefer *
112
+ ca_refer_new (CArray *parent,
113
+ int8_t data_type, int8_t rank, int32_t *dim, int32_t bytes,
114
+ int32_t offset)
115
+ {
116
+ CARefer *ca = ALLOC(CARefer);
117
+ ca_refer_setup(ca, parent, data_type, rank, dim, bytes, offset);
118
+ return ca;
119
+ }
120
+
121
+ static void
122
+ free_ca_refer (void *ap)
123
+ {
124
+ CARefer *ca = (CARefer *) ap;
125
+ if ( ca != NULL ) {
126
+ xfree(ca->dim);
127
+ ca_free(ca->mask);
128
+ ca_free(ca->mask0);
129
+ xfree(ca);
130
+ }
131
+ }
132
+
133
+ /* ------------------------------------------------------------------- */
134
+
135
+ static void *
136
+ ca_refer_func_clone (void *ap)
137
+ {
138
+ CARefer *ca = (CARefer *) ap;
139
+ return ca_refer_new(ca->parent,
140
+ ca->data_type, ca->rank, ca->dim, ca->bytes, ca->offset);
141
+ }
142
+
143
+ static char *
144
+ ca_refer_func_ptr_at_addr (void *ap, int32_t addr)
145
+ {
146
+ CARefer *ca = (CARefer *) ap;
147
+ int major, minor;
148
+ switch ( ca->is_deformed ) {
149
+ case 0:
150
+ case 1:
151
+ return ca->ptr + ca->bytes * addr;
152
+ case -2:
153
+ major = (addr * ca->bytes) / ca->parent->bytes;
154
+ minor = (addr * ca->bytes) % ca->parent->bytes;
155
+ return ca->ptr + ca->bytes * addr + minor;
156
+ case 2:
157
+ return ca->ptr + ca->bytes * (addr * ca->ratio);
158
+ default:
159
+ rb_raise(rb_eRuntimeError, "[BUG]");
160
+ }
161
+ }
162
+
163
+ static char *
164
+ ca_refer_func_ptr_at_index (void *ap, int32_t *idx)
165
+ {
166
+ CARefer *ca = (CARefer*) ap;
167
+ int32_t *dim = ca->dim;
168
+ int32_t n, i;
169
+ n = idx[0]; /* n = idx[0]*dim[1]*dim[2]*...*dim[rank-1] */
170
+ for (i=1; i<ca->rank; i++) { /* + idx[1]*dim[1]*dim[2]*...*dim[rank-1] */
171
+ n = dim[i]*n+idx[i]; /* ... + idx[rank-2]*dim[1] + idx[rank-1] */
172
+ }
173
+ return ca->ptr + ca->bytes * n;
174
+ }
175
+
176
+ static void
177
+ ca_refer_func_fetch_addr (void *ap, int32_t addr, void *ptr)
178
+ {
179
+ CARefer *ca = (CARefer *) ap;
180
+ int major, minor;
181
+ switch ( ca->is_deformed ) {
182
+ case 0:
183
+ ca_fetch_addr(ca->parent, addr, ptr);
184
+ case 1:
185
+ ca_fetch_addr(ca->parent, addr + ca->offset, ptr);
186
+ break;
187
+ case -2: {
188
+ major = (addr * ca->bytes) / ca->parent->bytes;
189
+ minor = (addr * ca->bytes) % ca->parent->bytes;
190
+ if ( ca->parent->bytes <= 256 ) {
191
+ char val[256];
192
+ ca_fetch_addr(ca->parent, major + ca->offset, val);
193
+ memcpy(ptr, val+minor, ca->bytes);
194
+ }
195
+ else {
196
+ char *val = malloc_with_check(ca->parent->bytes);
197
+ ca_fetch_addr(ca->parent, major + ca->offset, val);
198
+ memcpy(ptr, val+minor, ca->bytes);
199
+ free(val);
200
+ }
201
+ break;
202
+ }
203
+ case 2: {
204
+ int i;
205
+ for (i=0; i<ca->ratio; i++) {
206
+ ca_fetch_addr(ca->parent,
207
+ addr * ca->ratio + i + ca->offset,
208
+ ptr + i * ca->parent->bytes);
209
+ }
210
+ break;
211
+ }
212
+ }
213
+ }
214
+
215
+ static void
216
+ ca_refer_func_fetch_index (void *ap, int32_t *idx, void *ptr)
217
+ {
218
+ CARefer *ca = (CARefer *) ap;
219
+ if ( ca->is_deformed ) {
220
+ int32_t *dim = ca->dim;
221
+ int32_t n, i;
222
+ n = idx[0];
223
+ for (i=1; i<ca->rank; i++) {
224
+ n = dim[i]*n+idx[i];
225
+ }
226
+ ca_refer_func_fetch_addr(ca, n, ptr);
227
+ }
228
+ else {
229
+ ca_fetch_index(ca->parent, idx, ptr);
230
+ }
231
+ }
232
+
233
+ static void
234
+ ca_refer_func_store_addr (void *ap, int32_t addr, void *ptr)
235
+ {
236
+ CARefer *ca = (CARefer *) ap;
237
+ int major, minor;
238
+ switch ( ca->is_deformed ) {
239
+ case 0:
240
+ ca_store_addr(ca->parent, addr, ptr);
241
+ break;
242
+ case 1:
243
+ ca_store_addr(ca->parent, addr + ca->offset, ptr);
244
+ break;
245
+ case -2: {
246
+ major = (addr * ca->bytes) / ca->parent->bytes;
247
+ minor = (addr * ca->bytes) % ca->parent->bytes;
248
+ if ( ca->parent->bytes <= 256 ) {
249
+ char val[256];
250
+ ca_fetch_addr(ca->parent, major + ca->offset, val);
251
+ memcpy(val+minor, ptr, ca->bytes);
252
+ ca_store_addr(ca->parent, major + ca->offset, val);
253
+ }
254
+ else {
255
+ char *val = malloc_with_check(ca->parent->bytes);
256
+ ca_fetch_addr(ca->parent, major + ca->offset, val);
257
+ memcpy(val+minor, ptr, ca->bytes);
258
+ ca_store_addr(ca->parent, major + ca->offset, val);
259
+ free(val);
260
+ }
261
+ break;
262
+ }
263
+ case 2: {
264
+ int i;
265
+ for (i=0; i<ca->ratio; i++) {
266
+ ca_store_addr(ca->parent,
267
+ addr * ca->ratio + i + ca->offset,
268
+ ptr + i * ca->parent->bytes);
269
+ }
270
+ break;
271
+ }
272
+ }
273
+ }
274
+
275
+ static void
276
+ ca_refer_func_store_index (void *ap, int32_t *idx, void *ptr)
277
+ {
278
+ CARefer *ca = (CARefer *) ap;
279
+ if ( ca->is_deformed ) {
280
+ int32_t *dim = ca->dim;
281
+ int32_t n, i;
282
+ n = idx[0];
283
+ for (i=1; i<ca->rank; i++) {
284
+ n = dim[i]*n+idx[i];
285
+ }
286
+ ca_refer_func_store_addr(ca, n, ptr);
287
+ }
288
+ else {
289
+ ca_store_index(ca->parent, idx, ptr);
290
+ }
291
+ }
292
+
293
+ static void
294
+ ca_refer_func_allocate (void *ap)
295
+ {
296
+ CARefer *ca = (CARefer *) ap;
297
+ ca_allocate(ca->parent);
298
+ ca->ptr = ca->parent->ptr + ca->parent->bytes * ca->offset;
299
+ return;
300
+ }
301
+
302
+ static void
303
+ ca_refer_func_attach (void *ap)
304
+ {
305
+ CARefer *ca = (CARefer *) ap;
306
+ ca_attach(ca->parent);
307
+ ca->ptr = ca->parent->ptr + ca->parent->bytes * ca->offset;
308
+ return;
309
+ }
310
+
311
+ static void
312
+ ca_refer_func_sync (void *ap)
313
+ {
314
+ CARefer *ca = (CARefer *) ap;
315
+ ca_sync(ca->parent);
316
+ return;
317
+ }
318
+
319
+ static void
320
+ ca_refer_func_detach (void *ap)
321
+ {
322
+ CARefer *ca = (CARefer *) ap;
323
+ ca->ptr = NULL;
324
+ ca_detach(ca->parent);
325
+ return;
326
+ }
327
+
328
+ static void
329
+ ca_refer_func_copy_data (void *ap, void *ptr)
330
+ {
331
+ CARefer *ca = (CARefer *) ap;
332
+ if ( ptr != ca->ptr ) {
333
+ ca_attach(ca->parent);
334
+ memmove(ptr,
335
+ ca->parent->ptr + ca->parent->bytes * ca->offset,
336
+ ca_length(ca));
337
+ ca_detach(ca->parent);
338
+ }
339
+ }
340
+
341
+ static void
342
+ ca_refer_func_sync_data (void *ap, void *ptr)
343
+ {
344
+ CARefer *ca = (CARefer *) ap;
345
+ if ( ptr != ca->ptr ) {
346
+ ca_allocate(ca->parent);
347
+ memmove(ca->parent->ptr + ca->parent->bytes * ca->offset,
348
+ ptr,
349
+ ca_length(ca));
350
+ ca_sync(ca->parent);
351
+ ca_detach(ca->parent);
352
+ }
353
+ }
354
+
355
+ #define proc_fill_bang_fixlen() \
356
+ { \
357
+ int32_t i; \
358
+ int32_t bytes = ca->bytes; \
359
+ char *p = ca->parent->ptr + ca->parent->bytes * ca->offset; \
360
+ for (i=ca->elements; i; i--, p+=bytes) { \
361
+ memcpy(p, val, bytes); \
362
+ } \
363
+ }
364
+
365
+ #define proc_fill_bang(type) \
366
+ { \
367
+ int32_t i; \
368
+ type *p = (type *)(ca->parent->ptr + ca->parent->bytes * ca->offset); \
369
+ type v = *(type *)val; \
370
+ for (i=ca->elements; i; i--, p++) { \
371
+ *p = v; \
372
+ } \
373
+ }
374
+
375
+ static void
376
+ ca_refer_func_fill_data (void *ap, void *val)
377
+ {
378
+ CARefer *ca = (CARefer *) ap;
379
+
380
+ ca_allocate(ca->parent);
381
+
382
+ switch ( ca->data_type ) {
383
+ case CA_FIXLEN: proc_fill_bang_fixlen(); break;
384
+ case CA_BOOLEAN:
385
+ case CA_INT8:
386
+ case CA_UINT8: proc_fill_bang(int8_t); break;
387
+ case CA_INT16:
388
+ case CA_UINT16: proc_fill_bang(int16_t); break;
389
+ case CA_INT32:
390
+ case CA_UINT32:
391
+ case CA_FLOAT32: proc_fill_bang(int32_t); break;
392
+ case CA_INT64:
393
+ case CA_UINT64:
394
+ case CA_FLOAT64: proc_fill_bang(float64_t); break;
395
+ case CA_FLOAT128: proc_fill_bang(float128_t); break;
396
+ #ifdef HAVE_COMPLEX_H
397
+ case CA_CMPLX64: proc_fill_bang(cmplx64_t); break;
398
+ case CA_CMPLX128: proc_fill_bang(cmplx128_t); break;
399
+ case CA_CMPLX256: proc_fill_bang(cmplx256_t); break;
400
+ #endif
401
+ case CA_OBJECT: proc_fill_bang(VALUE); break;
402
+ default: rb_bug("array has an unknown data type");
403
+ }
404
+ ca_sync(ca->parent);
405
+ ca_detach(ca->parent);
406
+ }
407
+
408
+ static void
409
+ ca_refer_func_create_mask (void *ap)
410
+ {
411
+ CARefer *ca = (CARefer *) ap;
412
+
413
+ ca_update_mask(ca->parent);
414
+ if ( ! ca->parent->mask ) {
415
+ ca_create_mask(ca->parent);
416
+ }
417
+
418
+ if ( ca->bytes == ca->parent->bytes ) {
419
+ ca->mask =
420
+ (CArray *) ca_refer_new(ca->parent->mask,
421
+ CA_BOOLEAN, ca->rank, ca->dim, 0, ca->offset);
422
+ }
423
+ else if ( ca->is_deformed == -2 ) {
424
+ int32_t count[CA_RANK_MAX];
425
+ int i;
426
+ for (i=0; i<ca->parent->rank; i++) {
427
+ count[i] = 0;
428
+ }
429
+ count[ca->parent->rank] = ca->ratio;
430
+ ca->mask0 =
431
+ (CArray *) ca_repeat_new(ca->parent->mask, ca->parent->rank+1, count);
432
+ ca_unset_flag(ca->mask0, CA_FLAG_READ_ONLY);
433
+
434
+ ca->mask =
435
+ (CArray *) ca_refer_new(ca->mask0,
436
+ CA_BOOLEAN, ca->rank, ca->dim, 0, ca->offset);
437
+ }
438
+ else if ( ca->is_deformed == 2 ) {
439
+ /* TODO */
440
+ ca->mask0 =
441
+ (CArray *) ca_reduce_new(ca->parent->mask, ca->ratio, ca->offset);
442
+ ca->mask =
443
+ (CArray *) ca_refer_new(ca->mask0, CA_BOOLEAN, ca->rank, ca->dim, 0, 0);
444
+ }
445
+ }
446
+
447
+ ca_operation_function_t ca_refer_func = {
448
+ CA_OBJ_REFER,
449
+ CA_VIRTUAL_ARRAY,
450
+ free_ca_refer,
451
+ ca_refer_func_clone,
452
+ ca_refer_func_ptr_at_addr,
453
+ ca_refer_func_ptr_at_index,
454
+ ca_refer_func_fetch_addr,
455
+ ca_refer_func_fetch_index,
456
+ ca_refer_func_store_addr,
457
+ ca_refer_func_store_index,
458
+ ca_refer_func_allocate,
459
+ ca_refer_func_attach,
460
+ ca_refer_func_sync,
461
+ ca_refer_func_detach,
462
+ ca_refer_func_copy_data,
463
+ ca_refer_func_sync_data,
464
+ ca_refer_func_fill_data,
465
+ ca_refer_func_create_mask,
466
+ };
467
+
468
+ /* ------------------------------------------------------------------- */
469
+
470
+ static VALUE
471
+ rb_ca_refer_s_allocate (VALUE klass)
472
+ {
473
+ CARefer *ca;
474
+ return Data_Make_Struct(klass, CARefer, ca_mark, ca_free, ca);
475
+ }
476
+
477
+ static VALUE
478
+ rb_ca_refer_initialize_copy (VALUE self, VALUE other)
479
+ {
480
+ CARefer *ca, *cs;
481
+
482
+ Data_Get_Struct(self, CARefer, ca);
483
+ Data_Get_Struct(other, CARefer, cs);
484
+
485
+ ca_refer_setup(ca, cs->parent, cs->data_type, cs->rank, cs->dim,
486
+ cs->bytes, cs->offset);
487
+
488
+ return self;
489
+ }
490
+
491
+ /* rdoc:
492
+ class CArray
493
+ # call-seq:
494
+ # CArray.refer()
495
+ # CArray.refer(data_type, dim[, :bytes=>bytes, :offset=>offset])
496
+ # CArray.refer(data_class, dim)
497
+ #
498
+ # Returns CARefer object which refers self.
499
+ # In second form, `data_type` can be different data_type of self,
500
+ # as long as the total byte length of new array is smaller than
501
+ # that of self.
502
+ def refer (*argv)
503
+ end
504
+ end
505
+ */
506
+
507
+ static VALUE
508
+ rb_ca_refer (int argc, VALUE *argv, VALUE self)
509
+ {
510
+ volatile VALUE obj = Qnil;
511
+ CArray *ca;
512
+ CARefer *cr;
513
+ int8_t data_type;
514
+ int8_t rank;
515
+ int32_t dim[CA_RANK_MAX];
516
+ int32_t bytes, offset = 0;
517
+ int32_t i;
518
+
519
+ Data_Get_Struct(self, CArray, ca);
520
+
521
+ if ( argc == 0 ) { /* CArray#refer() */
522
+ data_type = ca->data_type;
523
+ bytes = ca->bytes;
524
+ rank = ca->rank;
525
+ for (i=0; i<rank; i++) {
526
+ dim[i] = ca->dim[i];
527
+ }
528
+ cr = ca_refer_new((CArray*)ca, data_type, rank, dim, bytes, offset);
529
+ obj = ca_wrap_struct(cr);
530
+ rb_ca_set_parent(obj, self);
531
+ rb_ca_data_type_inherit(obj, self);
532
+ }
533
+ else {
534
+ volatile VALUE rtype, rdim, ropt, rbytes = Qnil, roffset = Qnil;
535
+ int32_t elements;
536
+
537
+ ropt = rb_pop_options(&argc, &argv);
538
+ rb_scan_args(argc, argv, "11", &rtype, &rdim);
539
+ rb_scan_options(ropt, "bytes,offset", &rbytes, &roffset);
540
+
541
+ if ( NIL_P(rbytes) ) {
542
+ rbytes = rb_ca_bytes(self);
543
+ }
544
+
545
+ rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
546
+
547
+ if ( NIL_P(rdim) ) {
548
+ if ( ca->bytes != bytes ) {
549
+ rb_raise(rb_eRuntimeError,
550
+ "specify dimension shape for different byte size");
551
+ }
552
+ else {
553
+ rdim = rb_ca_dim(self);
554
+ }
555
+ }
556
+
557
+ Check_Type(rdim, T_ARRAY);
558
+ rank = RARRAY_LEN(rdim);
559
+
560
+ elements = 1;
561
+ for (i=0; i<rank; i++) {
562
+ dim[i] = NUM2INT(rb_ary_entry(rdim, i));
563
+ elements *= dim[i];
564
+ }
565
+
566
+ if ( ! NIL_P(roffset) ) {
567
+ offset = NUM2LONG(roffset);
568
+ }
569
+
570
+ cr = ca_refer_new((CArray*)ca, data_type, rank, dim, bytes, offset);
571
+ obj = ca_wrap_struct(cr);
572
+ rb_ca_set_parent(obj, self);
573
+ rb_ca_data_type_import(obj, rtype);
574
+ }
575
+
576
+ return obj;
577
+ }
578
+
579
+ /* api: rb_ca_refer_new
580
+ */
581
+
582
+ VALUE
583
+ rb_ca_refer_new (VALUE self,
584
+ int8_t data_type, int8_t rank, int32_t *dim, int32_t bytes,
585
+ int32_t offset)
586
+ {
587
+ volatile VALUE list, rdim, ropt;
588
+ CArray *ca;
589
+ int32_t i;
590
+
591
+ Data_Get_Struct(self, CArray, ca);
592
+
593
+ rdim = rb_ary_new2(rank);
594
+ for (i=0; i<rank; i++) {
595
+ rb_ary_store(rdim, i, INT2NUM(dim[i]));
596
+ }
597
+
598
+ list = rb_ary_new2(3);
599
+ if ( data_type == CA_FIXLEN && rb_ca_has_data_class(self) ) {
600
+ rb_ary_store(list, 0, rb_ca_data_class(self));
601
+ }
602
+ else {
603
+ rb_ary_store(list, 0, INT2NUM(data_type));
604
+ }
605
+ rb_ary_store(list, 1, rdim);
606
+ ropt = rb_hash_new();
607
+ rb_set_options(ropt, "bytes,offset", INT2NUM(bytes), INT2NUM(offset));
608
+ rb_ary_store(list, 2, ropt);
609
+
610
+ return rb_ca_refer(3, RARRAY_PTR(list), self);
611
+ }
612
+
613
+
614
+ void
615
+ Init_ca_obj_refer ()
616
+ {
617
+ /* rb_cCARefer, CA_OBJ_REFER are defined in ruby_carray.c */
618
+
619
+ rb_define_const(rb_cObject, "CA_OBJ_REFER", INT2NUM(CA_OBJ_REFER));
620
+
621
+ rb_define_method(rb_cCArray, "refer", rb_ca_refer, -1);
622
+
623
+ rb_define_alloc_func(rb_cCARefer, rb_ca_refer_s_allocate);
624
+ rb_define_method(rb_cCARefer, "initialize_copy",
625
+ rb_ca_refer_initialize_copy, 1);
626
+ }
627
+