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,405 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_obj_fake.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
+ typedef struct {
16
+ int16_t obj_type;
17
+ int8_t data_type;
18
+ int8_t rank;
19
+ int32_t flags;
20
+ int32_t bytes;
21
+ int32_t elements;
22
+ int32_t *dim;
23
+ char *ptr;
24
+ CArray *mask;
25
+ CArray *parent;
26
+ uint32_t attach;
27
+ uint8_t nosync;
28
+ } CAFake;
29
+
30
+ static int8_t CA_OBJ_FAKE;
31
+
32
+ static VALUE rb_cCAFake;
33
+
34
+ /* rdoc:
35
+ class CAFake < CAVirtual # :nodoc:
36
+ end
37
+ */
38
+
39
+ /* ------------------------------------------------------------------- */
40
+
41
+ int
42
+ ca_fake_setup (CAFake *ca, CArray *parent, int8_t data_type, int32_t bytes)
43
+ {
44
+ int8_t rank;
45
+ int32_t *dim, elements;
46
+
47
+ /* check arguments */
48
+
49
+ CA_CHECK_DATA_TYPE(data_type);
50
+ CA_CHECK_BYTES(data_type, bytes);
51
+
52
+ rank = parent->rank;
53
+ dim = parent->dim;
54
+ elements = parent->elements;
55
+
56
+ ca->obj_type = CA_OBJ_FAKE;
57
+ ca->data_type = data_type;
58
+ ca->flags = 0;
59
+ ca->rank = rank;
60
+ ca->bytes = bytes;
61
+ ca->elements = elements;
62
+ ca->ptr = NULL;
63
+ ca->mask = NULL;
64
+ ca->dim = ALLOC_N(int32_t, rank);
65
+
66
+ ca->parent = parent;
67
+ ca->attach = 0;
68
+ ca->nosync = 0;
69
+
70
+ memcpy(ca->dim, dim, rank * sizeof(int32_t));
71
+
72
+ if ( ca_has_mask(parent) ) {
73
+ ca_create_mask(ca);
74
+ }
75
+
76
+ if ( ca_is_scalar(parent) ) {
77
+ ca_set_flag(ca, CA_FLAG_SCALAR);
78
+ }
79
+
80
+ return 0;
81
+ }
82
+
83
+ CAFake *
84
+ ca_fake_new (CArray *parent, int8_t data_type, int32_t bytes)
85
+ {
86
+ CAFake *ca = ALLOC(CAFake);
87
+ ca_fake_setup(ca, parent, data_type, bytes);
88
+ return ca;
89
+ }
90
+
91
+ static void
92
+ free_ca_fake (void *ap)
93
+ {
94
+ CAFake *ca = (CAFake *) ap;
95
+ if ( ca != NULL ) {
96
+ ca_free(ca->mask);
97
+ xfree(ca->dim);
98
+ xfree(ca);
99
+ }
100
+ }
101
+
102
+
103
+ /* ------------------------------------------------------------------- */
104
+
105
+ static void *
106
+ ca_fake_func_clone (void *ap)
107
+ {
108
+ CAFake *ca = (CAFake *) ap;
109
+ return ca_fake_new(ca->parent, ca->data_type, ca->bytes);
110
+ }
111
+
112
+ static char *
113
+ ca_fake_func_ptr_at_addr (void *ap, int32_t addr)
114
+ {
115
+ CAFake *ca = (CAFake *) ap;
116
+ return ca->ptr + ca->bytes * addr;
117
+ }
118
+
119
+ static char *
120
+ ca_fake_func_ptr_at_index (void *ap, int32_t *idx)
121
+ {
122
+ CAFake *ca = (CAFake *) ap;
123
+ return ca_func[CA_OBJ_ARRAY].ptr_at_index(ca, idx);
124
+ }
125
+
126
+ static void
127
+ ca_fake_func_fetch_index (void *ap, int32_t *idx, void *ptr)
128
+ {
129
+ CAFake *ca = (CAFake *) ap;
130
+ if ( ca->parent->bytes <= 32 ) {
131
+ char v[32];
132
+ ca_fetch_index(ca->parent, idx, v);
133
+ ca_ptr2ptr(ca->parent, v, ca, ptr);
134
+ }
135
+ else {
136
+ char *v = malloc_with_check(ca->parent->bytes);
137
+ ca_fetch_index(ca->parent, idx, v);
138
+ ca_ptr2ptr(ca->parent, v, ca, ptr);
139
+ free(v);
140
+ }
141
+ }
142
+
143
+ static void
144
+ ca_fake_func_store_index (void *ap, int32_t *idx, void *ptr)
145
+ {
146
+ CAFake *ca = (CAFake *) ap;
147
+ if ( ca->parent->bytes <= 32 ) {
148
+ char v[32];
149
+ ca_ptr2ptr(ca, ptr, ca->parent, v);
150
+ ca_store_index(ca->parent, idx, v);
151
+ }
152
+ else {
153
+ char *v = malloc_with_check(ca->parent->bytes);
154
+ ca_ptr2ptr(ca, ptr, ca->parent, v);
155
+ ca_store_index(ca->parent, idx, v);
156
+ free(v);
157
+ }
158
+ }
159
+
160
+ static void
161
+ ca_fake_func_allocate (void *ap)
162
+ {
163
+ CAFake *ca = (CAFake *) ap;
164
+ ca_attach(ca->parent);
165
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
166
+ ca->ptr = malloc_with_check(ca_length(ca));
167
+
168
+ /* initialize elements with 0 for CA_OBJECT data_type */
169
+ if ( ca->data_type == CA_OBJECT ) {
170
+ VALUE *p = (VALUE *) ca->ptr;
171
+ VALUE zero = INT2FIX(0);
172
+ int32_t i;
173
+ for (i=0; i<ca->elements; i++) {
174
+ *p++ = zero;
175
+ }
176
+ }
177
+ }
178
+
179
+ static void
180
+ ca_fake_func_attach (void *ap)
181
+ {
182
+ void ca_fake_attach (CAFake *cb);
183
+
184
+ CAFake *ca = (CAFake *) ap;
185
+ ca_attach(ca->parent);
186
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
187
+ ca->ptr = malloc_with_check(ca_length(ca));
188
+
189
+ /* initialize elements with 0 for CA_OBJECT data_type */
190
+ if ( ca->data_type == CA_OBJECT ) {
191
+ VALUE *p = (VALUE *) ca->ptr;
192
+ VALUE zero = INT2FIX(0);
193
+ int32_t i;
194
+ for (i=0; i<ca->elements; i++) {
195
+ *p++ = zero;
196
+ }
197
+ }
198
+
199
+ if ( ca->parent->mask ) {
200
+ ca_cast_block_with_mask(ca->elements, ca->parent, ca->parent->ptr,
201
+ ca, ca->ptr,
202
+ (boolean8_t*)ca->parent->mask->ptr);
203
+ }
204
+ else {
205
+ ca_cast_block(ca->elements, ca->parent, ca->parent->ptr, ca, ca->ptr);
206
+ }
207
+ }
208
+
209
+ static void
210
+ ca_fake_func_sync (void *ap)
211
+ {
212
+ CAFake *ca = (CAFake *) ap;
213
+ ca_update_mask(ca);
214
+ if ( ca->mask ) {
215
+ ca_cast_block_with_mask(ca->elements, ca, ca->ptr, ca->parent, ca->parent->ptr,
216
+ (boolean8_t *)ca->parent->mask->ptr);
217
+ }
218
+ else {
219
+ ca_cast_block(ca->elements, ca, ca->ptr, ca->parent, ca->parent->ptr);
220
+ }
221
+ ca_sync(ca->parent);
222
+ }
223
+
224
+ static void
225
+ ca_fake_func_detach (void *ap)
226
+ {
227
+ CAFake *ca = (CAFake *) ap;
228
+ free(ca->ptr);
229
+ ca->ptr = NULL;
230
+ ca_detach(ca->parent);
231
+ }
232
+
233
+ static void
234
+ ca_fake_func_copy_data (void *ap, void *ptr)
235
+ {
236
+ CAFake *ca = (CAFake *) ap;
237
+ ca_attach(ca->parent);
238
+ ca_update_mask(ca);
239
+ if ( ca->parent->mask ) {
240
+ ca_cast_block_with_mask(ca->elements, ca->parent, ca->parent->ptr, ca, ptr,
241
+ (boolean8_t*)ca->parent->mask->ptr);
242
+ }
243
+ else {
244
+ ca_cast_block(ca->elements, ca->parent, ca->parent->ptr, ca, ptr);
245
+ }
246
+ ca_detach(ca->parent);
247
+ }
248
+
249
+ static void
250
+ ca_fake_func_sync_data (void *ap, void *ptr)
251
+ {
252
+ CAFake *ca = (CAFake *) ap;
253
+ ca_attach(ca->parent);
254
+ if ( ca->parent->mask ) {
255
+ ca_cast_block_with_mask(ca->elements, ca, ptr, ca->parent, ca->parent->ptr,
256
+ (boolean8_t*)ca->parent->mask->ptr);
257
+ }
258
+ else {
259
+ ca_cast_block(ca->elements, ca, ptr, ca->parent, ca->parent->ptr);
260
+ }
261
+ ca_detach(ca->parent);
262
+ }
263
+
264
+ static void
265
+ ca_fake_func_fill_data (void *ap, void *ptr)
266
+ {
267
+ CAFake *ca = (CAFake *) ap;
268
+ if ( ca->parent->bytes <= 32 ) {
269
+ char v[32];
270
+ ca_ptr2ptr(ca, ptr, ca->parent, v);
271
+ ca_fill(ca->parent, v);
272
+ }
273
+ else {
274
+ char *v = malloc_with_check(ca->parent->bytes);
275
+ ca_ptr2ptr(ca, ptr, ca->parent, v);
276
+ ca_fill(ca->parent, v);
277
+ free(v);
278
+ }
279
+ }
280
+
281
+ static void
282
+ ca_fake_func_create_mask (void *ap)
283
+ {
284
+ CAFake *ca = (CAFake *) ap;
285
+ ca_update_mask(ca->parent);
286
+ if ( ! ca->parent->mask ) {
287
+ ca_create_mask(ca->parent);
288
+ }
289
+ ca->mask =
290
+ (CArray *) ca_refer_new(ca->parent->mask,
291
+ CA_BOOLEAN, ca->rank, ca->dim, 0, 0);
292
+ }
293
+
294
+ ca_operation_function_t ca_fake_func = {
295
+ -1, /* CA_OBJ_FAKE */
296
+ CA_VIRTUAL_ARRAY,
297
+ free_ca_fake,
298
+ ca_fake_func_clone,
299
+ ca_fake_func_ptr_at_addr,
300
+ ca_fake_func_ptr_at_index,
301
+ NULL,
302
+ ca_fake_func_fetch_index,
303
+ NULL,
304
+ ca_fake_func_store_index,
305
+ ca_fake_func_allocate,
306
+ ca_fake_func_attach,
307
+ ca_fake_func_sync,
308
+ ca_fake_func_detach,
309
+ ca_fake_func_copy_data,
310
+ ca_fake_func_sync_data,
311
+ ca_fake_func_fill_data,
312
+ ca_fake_func_create_mask,
313
+ };
314
+
315
+ /* ------------------------------------------------------------------- */
316
+
317
+ VALUE
318
+ rb_ca_fake_new (VALUE cary, int8_t data_type, int32_t bytes)
319
+ {
320
+ volatile VALUE obj;
321
+ CArray *parent;
322
+ CAFake *ca;
323
+ rb_check_carray_object(cary);
324
+ Data_Get_Struct(cary, CArray, parent);
325
+ ca = ca_fake_new(parent, data_type, bytes);
326
+ obj = ca_wrap_struct(ca);
327
+ rb_ca_set_parent(obj, cary);
328
+ return obj;
329
+ }
330
+
331
+ /* rdoc:
332
+ class CArray
333
+ def fake (data_type, options={:bytes=>0})
334
+ end
335
+ end
336
+ */
337
+
338
+ VALUE
339
+ rb_ca_fake (int argc, VALUE *argv, VALUE self)
340
+ {
341
+ volatile VALUE obj, rtype, ropt, rbytes = Qnil;
342
+ CArray *ca;
343
+ int8_t data_type;
344
+ int32_t bytes;
345
+
346
+ Data_Get_Struct(self, CArray, ca);
347
+
348
+ rb_scan_args(argc, argv, "11", &rtype, &ropt);
349
+ rb_scan_options(ropt, "bytes", &rbytes);
350
+
351
+ rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
352
+ obj = rb_ca_fake_new(self, data_type, bytes);
353
+ rb_ca_data_type_import(obj, rtype);
354
+
355
+ return obj;
356
+ }
357
+
358
+ VALUE
359
+ rb_ca_fake_type (VALUE self, VALUE rtype, VALUE rbytes)
360
+ {
361
+ volatile VALUE obj;
362
+ int8_t data_type;
363
+ int32_t bytes;
364
+ rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
365
+ obj = rb_ca_fake_new(self, data_type, bytes);
366
+ rb_ca_data_type_import(obj, rtype);
367
+ return obj;
368
+ }
369
+
370
+ static VALUE
371
+ rb_ca_fake_s_allocate (VALUE klass)
372
+ {
373
+ CAFake *ca;
374
+ return Data_Make_Struct(klass, CAFake, ca_mark, ca_free, ca);
375
+ }
376
+
377
+ static VALUE
378
+ rb_ca_fake_initialize_copy (VALUE self, VALUE other)
379
+ {
380
+ CAFake *ca, *cs;
381
+
382
+ Data_Get_Struct(self, CAFake, ca);
383
+ Data_Get_Struct(other, CAFake, cs);
384
+
385
+ ca_fake_setup(ca, cs->parent, cs->data_type, cs->bytes);
386
+
387
+ return self;
388
+ }
389
+
390
+ void
391
+ Init_ca_obj_fake ()
392
+ {
393
+ rb_cCAFake = rb_define_class("CAFake", rb_cCAVirtual);
394
+
395
+ CA_OBJ_FAKE = ca_install_obj_type(rb_cCAFake, ca_fake_func);
396
+ rb_define_const(rb_cObject, "CA_OBJ_FAKE", INT2NUM(CA_OBJ_FAKE));
397
+
398
+ rb_define_method(rb_cCArray, "fake", rb_ca_fake, -1);
399
+
400
+ rb_define_alloc_func(rb_cCAFake, rb_ca_fake_s_allocate);
401
+ rb_define_method(rb_cCAFake, "initialize_copy",
402
+ rb_ca_fake_initialize_copy, 1);
403
+ }
404
+
405
+
@@ -0,0 +1,482 @@
1
+ /* ---------------------------------------------------------------------------
2
+
3
+ ca_obj_farray.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
+ typedef struct {
16
+ int16_t obj_type;
17
+ int8_t data_type;
18
+ int8_t rank;
19
+ int32_t flags;
20
+ int32_t bytes;
21
+ int32_t elements;
22
+ int32_t *dim;
23
+ char *ptr;
24
+ CArray *mask;
25
+ CArray *parent;
26
+ uint32_t attach;
27
+ uint8_t nosync;
28
+ /* --------------- */
29
+ int32_t step;
30
+ } CAFarray;
31
+
32
+ static int8_t CA_OBJ_FARRAY;
33
+
34
+ static VALUE rb_cCAFarray;
35
+
36
+ /* rdoc:
37
+ class CAFArray < CAVirtual # :nodoc:
38
+ end
39
+ */
40
+
41
+ /* ------------------------------------------------------------------- */
42
+
43
+ int
44
+ ca_farray_setup (CAFarray *ca, CArray *parent)
45
+ {
46
+ int8_t rank, data_type;
47
+ int32_t *dim, elements, bytes;
48
+ int i;
49
+
50
+ /* check arguments */
51
+
52
+ data_type = parent->data_type;
53
+ rank = parent->rank;
54
+ dim = parent->dim;
55
+ bytes = parent->bytes;
56
+ elements = parent->elements;
57
+
58
+ ca->obj_type = CA_OBJ_FARRAY;
59
+ ca->data_type = data_type;
60
+ ca->flags = 0;
61
+ ca->rank = rank;
62
+ ca->bytes = bytes;
63
+ ca->elements = elements;
64
+ ca->ptr = NULL;
65
+ ca->mask = NULL;
66
+ ca->dim = ALLOC_N(int32_t, rank);
67
+
68
+ ca->parent = parent;
69
+ ca->attach = 0;
70
+ ca->nosync = 0;
71
+
72
+ for (i=0; i<rank; i++) {
73
+ ca->dim[i] = dim[rank-1-i];
74
+ }
75
+
76
+ ca->step = 1;
77
+ for (i=1; i<rank; i++) {
78
+ ca->step *= dim[i];
79
+ }
80
+
81
+ if ( ca_has_mask(parent) ) {
82
+ ca_create_mask(ca);
83
+ }
84
+
85
+ if ( ca_is_scalar(parent) ) {
86
+ ca_set_flag(ca, CA_FLAG_SCALAR);
87
+ }
88
+
89
+ return 0;
90
+ }
91
+
92
+ CAFarray *
93
+ ca_farray_new (CArray *parent)
94
+ {
95
+ CAFarray *ca = ALLOC(CAFarray);
96
+ ca_farray_setup(ca, parent);
97
+ return ca;
98
+ }
99
+
100
+ static void
101
+ free_ca_farray (void *ap)
102
+ {
103
+ CAFarray *ca = (CAFarray *) ap;
104
+ if ( ca != NULL ) {
105
+ ca_free(ca->mask);
106
+ xfree(ca->dim);
107
+ xfree(ca);
108
+ }
109
+ }
110
+
111
+ /* ------------------------------------------------------------------- */
112
+
113
+ static void *
114
+ ca_farray_func_clone (void *ap)
115
+ {
116
+ CAFarray *ca = (CAFarray *) ap;
117
+ return ca_farray_new(ca->parent);
118
+ }
119
+
120
+ #define ca_farray_func_ptr_at_addr ca_array_func_ptr_at_addr
121
+ #define ca_farray_func_ptr_at_index ca_array_func_ptr_at_index
122
+
123
+ static void
124
+ ca_farray_func_fetch_index (void *ap, int32_t *idx, void *ptr)
125
+ {
126
+ CAFarray *ca = (CAFarray *) ap;
127
+ int8_t rank = ca->rank;
128
+ int32_t idx0[CA_RANK_MAX];
129
+ int i;
130
+ for (i=0; i<rank; i++) {
131
+ idx0[i] = idx[rank-1-i];
132
+ }
133
+ ca_fetch_index(ca->parent, idx0, ptr);
134
+ }
135
+
136
+ static void
137
+ ca_farray_func_store_index (void *ap, int32_t *idx, void *ptr)
138
+ {
139
+ CAFarray *ca = (CAFarray *) ap;
140
+ int8_t rank = ca->rank;
141
+ int32_t idx0[CA_RANK_MAX];
142
+ int i;
143
+ for (i=0; i<rank; i++) {
144
+ idx0[i] = idx[rank-1-i];
145
+ }
146
+ ca_store_index(ca->parent, idx0, ptr);
147
+ }
148
+
149
+ static void
150
+ ca_farray_func_allocate (void *ap)
151
+ {
152
+ CAFarray *ca = (CAFarray *) ap;
153
+ ca_attach(ca->parent);
154
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
155
+ ca->ptr = malloc_with_check(ca_length(ca));
156
+ }
157
+
158
+ static void ca_fa_attach (CAFarray *ca);
159
+ static void ca_fa_sync (CAFarray *ca);
160
+
161
+ static void
162
+ ca_farray_func_attach (void *ap)
163
+ {
164
+ CAFarray *ca = (CAFarray *) ap;
165
+ ca_attach(ca->parent);
166
+ /* ca->ptr = ALLOC_N(char, ca_length(ca)); */
167
+ ca->ptr = malloc_with_check(ca_length(ca));
168
+ ca_fa_attach(ca);
169
+ }
170
+
171
+ static void
172
+ ca_farray_func_sync (void *ap)
173
+ {
174
+ CAFarray *ca = (CAFarray *) ap;
175
+ ca_fa_sync(ca);
176
+ ca_sync(ca->parent);
177
+ }
178
+
179
+ static void
180
+ ca_farray_func_detach (void *ap)
181
+ {
182
+ CAFarray *ca = (CAFarray *) ap;
183
+ free(ca->ptr);
184
+ ca->ptr = NULL;
185
+ ca_detach(ca->parent);
186
+ }
187
+
188
+ static void
189
+ ca_farray_func_copy_data (void *ap, void *ptr)
190
+ {
191
+ CAFarray *ca = (CAFarray *) ap;
192
+ char *ptr0 = ca->ptr;
193
+ ca_attach(ca->parent);
194
+ ca->ptr = ptr;
195
+ ca_fa_attach(ca);
196
+ ca->ptr = ptr0;
197
+ ca_detach(ca->parent);
198
+ }
199
+
200
+ static void
201
+ ca_farray_func_sync_data (void *ap, void *ptr)
202
+ {
203
+ CAFarray *ca = (CAFarray *) ap;
204
+ char *ptr0 = ca->ptr;
205
+ ca_attach(ca->parent);
206
+ ca->ptr = ptr;
207
+ ca_fa_sync(ca);
208
+ ca->ptr = ptr0;
209
+ ca_sync(ca->parent);
210
+ ca_detach(ca->parent);
211
+ }
212
+
213
+ static void
214
+ ca_farray_func_fill_data (void *ap, void *ptr)
215
+ {
216
+ CAFarray *ca = (CAFarray *) ap;
217
+ ca_fill(ca->parent, ptr);
218
+ }
219
+
220
+ static void
221
+ ca_farray_func_create_mask (void *ap)
222
+ {
223
+ CAFarray *ca = (CAFarray *) ap;
224
+ ca_update_mask(ca->parent);
225
+ if ( ! ca->parent->mask ) {
226
+ ca_create_mask(ca->parent);
227
+ }
228
+ ca->mask = (CArray *) ca_farray_new(ca->parent->mask);
229
+ }
230
+
231
+ ca_operation_function_t ca_farray_func = {
232
+ -1, /* CA_OBJ_FARRAY */
233
+ CA_VIRTUAL_ARRAY,
234
+ free_ca_farray,
235
+ ca_farray_func_clone,
236
+ ca_farray_func_ptr_at_addr,
237
+ ca_farray_func_ptr_at_index,
238
+ NULL,
239
+ ca_farray_func_fetch_index,
240
+ NULL,
241
+ ca_farray_func_store_index,
242
+ ca_farray_func_allocate,
243
+ ca_farray_func_attach,
244
+ ca_farray_func_sync,
245
+ ca_farray_func_detach,
246
+ ca_farray_func_copy_data,
247
+ ca_farray_func_sync_data,
248
+ ca_farray_func_fill_data,
249
+ ca_farray_func_create_mask,
250
+ };
251
+
252
+ /* ------------------------------------------------------------------- */
253
+
254
+
255
+ static void
256
+ ca_fa_attach_loop (CAFarray *ca, int8_t level, int32_t *idx, int32_t *idx0)
257
+ {
258
+ int32_t step = ca->step;
259
+ int32_t dim = ca->dim[level];
260
+ int32_t i;
261
+ if ( level == ca->rank - 1 ) {
262
+ idx[level] = 0;
263
+ idx0[0] = 0;
264
+ switch ( ca->bytes ) {
265
+ case 1:
266
+ {
267
+ int8_t *p = ca_ptr_at_index(ca, idx);
268
+ int8_t *q = ca_ptr_at_index(ca->parent, idx0);
269
+ for (i=0; i<dim; i++) {
270
+ *p++ = *q;
271
+ q += step;
272
+ }
273
+ }
274
+ break;
275
+ case 2:
276
+ {
277
+ int16_t *p = ca_ptr_at_index(ca, idx);
278
+ int16_t *q = ca_ptr_at_index(ca->parent, idx0);
279
+ for (i=0; i<dim; i++) {
280
+ *p++ = *q;
281
+ q += step;
282
+ }
283
+ }
284
+ break;
285
+ case 4:
286
+ {
287
+ int32_t *p = ca_ptr_at_index(ca, idx);
288
+ int32_t *q = ca_ptr_at_index(ca->parent, idx0);
289
+ for (i=0; i<dim; i++) {
290
+ *p++ = *q;
291
+ q += step;
292
+ }
293
+ }
294
+ break;
295
+ case 8:
296
+ {
297
+ float64_t *p = ca_ptr_at_index(ca, idx);
298
+ float64_t *q = ca_ptr_at_index(ca->parent, idx0);
299
+ for (i=0; i<dim; i++) {
300
+ *p++ = *q;
301
+ q += step;
302
+ }
303
+ }
304
+ break;
305
+ default:
306
+ {
307
+ char *p = ca_ptr_at_index(ca, idx);
308
+ char *q = ca_ptr_at_index(ca->parent, idx0);
309
+ for (i=0; i<dim; i++) {
310
+ memcpy(p, q, ca->bytes);
311
+ p += ca->bytes;
312
+ q += step * ca->bytes;
313
+ }
314
+ }
315
+ }
316
+ }
317
+ else {
318
+ int level0 = ca->rank - 1 - level;
319
+ for (i=0; i<dim; i++) {
320
+ idx[level] = i;
321
+ idx0[level0] = i;
322
+ ca_fa_attach_loop(ca, level+1, idx, idx0);
323
+ }
324
+ }
325
+ }
326
+
327
+ static void
328
+ ca_fa_attach (CAFarray *ca)
329
+ {
330
+ int32_t idx[CA_RANK_MAX];
331
+ int32_t idx0[CA_RANK_MAX];
332
+ ca_fa_attach_loop(ca, 0, idx, idx0);
333
+ }
334
+
335
+ static void
336
+ ca_fa_sync_loop (CAFarray *ca, int8_t level, int32_t *idx, int32_t *idx0)
337
+ {
338
+ int32_t step = ca->step;
339
+ int32_t dim = ca->dim[level];
340
+ int32_t i;
341
+
342
+ if ( level == ca->rank - 1 ) {
343
+ idx[level] = 0;
344
+ idx0[0] = 0;
345
+ switch ( ca->bytes ) {
346
+ case 1:
347
+ {
348
+ int8_t *p = ca_ptr_at_index(ca, idx);
349
+ int8_t *q = ca_ptr_at_index(ca->parent, idx0);
350
+ for (i=0; i<dim; i++) {
351
+ *q = *p++;
352
+ q += step;
353
+ }
354
+ }
355
+ break;
356
+ case 2:
357
+ {
358
+ int16_t *p = ca_ptr_at_index(ca, idx);
359
+ int16_t *q = ca_ptr_at_index(ca->parent, idx0);
360
+ for (i=0; i<dim; i++) {
361
+ *q = *p++;
362
+ q += step;
363
+ }
364
+ }
365
+ break;
366
+ case 4:
367
+ {
368
+ int32_t *p = ca_ptr_at_index(ca, idx);
369
+ int32_t *q = ca_ptr_at_index(ca->parent, idx0);
370
+ for (i=0; i<dim; i++) {
371
+ *q = *p++;
372
+ q += step;
373
+ }
374
+ }
375
+ break;
376
+ case 8:
377
+ {
378
+ float64_t *p = ca_ptr_at_index(ca, idx);
379
+ float64_t *q = ca_ptr_at_index(ca->parent, idx0);
380
+ for (i=0; i<dim; i++) {
381
+ *q = *p++;
382
+ q += step;
383
+ }
384
+ }
385
+ break;
386
+ default:
387
+ {
388
+ char *p = ca_ptr_at_index(ca, idx);
389
+ char *q = ca_ptr_at_index(ca->parent, idx0);
390
+ for (i=0; i<dim; i++) {
391
+ memcpy(q, p, ca->bytes);
392
+ p += ca->bytes;
393
+ q += step * ca->bytes;
394
+ }
395
+ }
396
+ }
397
+ }
398
+ else {
399
+ for (i=0; i<dim; i++) {
400
+ idx[level] = i;
401
+ idx0[ca->rank - 1 - level] = i;
402
+ ca_fa_sync_loop(ca, level+1, idx, idx0);
403
+ }
404
+ }
405
+ }
406
+
407
+ static void
408
+ ca_fa_sync (CAFarray *ca)
409
+ {
410
+ int32_t idx[CA_RANK_MAX];
411
+ int32_t idx0[CA_RANK_MAX];
412
+ ca_fa_sync_loop(ca, 0, idx, idx0);
413
+ }
414
+
415
+ /* ------------------------------------------------------------------- */
416
+
417
+ VALUE
418
+ rb_ca_farray_new (VALUE cary)
419
+ {
420
+ volatile VALUE obj;
421
+ CArray *parent;
422
+ CAFarray *ca;
423
+ rb_check_carray_object(cary);
424
+ Data_Get_Struct(cary, CArray, parent);
425
+ ca = ca_farray_new(parent);
426
+ obj = ca_wrap_struct(ca);
427
+ rb_ca_set_parent(obj, cary);
428
+ rb_ca_data_type_inherit(obj, cary);
429
+ return obj;
430
+ }
431
+
432
+ /* rdoc:
433
+ class CArray
434
+ # create the virtual transposed array which dimension order is reversed.
435
+ def t
436
+ end
437
+ end
438
+ */
439
+
440
+ VALUE
441
+ rb_ca_farray (VALUE self)
442
+ {
443
+ return rb_ca_farray_new(self);
444
+ }
445
+
446
+ static VALUE
447
+ rb_ca_farray_s_allocate (VALUE klass)
448
+ {
449
+ CAFarray *ca;
450
+ return Data_Make_Struct(klass, CAFarray, ca_mark, ca_free, ca);
451
+ }
452
+
453
+ static VALUE
454
+ rb_ca_farray_initialize_copy (VALUE self, VALUE other)
455
+ {
456
+ CAFarray *ca, *cs;
457
+
458
+ Data_Get_Struct(self, CAFarray, ca);
459
+ Data_Get_Struct(other, CAFarray, cs);
460
+
461
+ ca_farray_setup(ca, cs->parent);
462
+
463
+ return self;
464
+ }
465
+
466
+ void
467
+ Init_ca_obj_farray ()
468
+ {
469
+ rb_cCAFarray = rb_define_class("CAFarray", rb_cCAVirtual);
470
+
471
+ CA_OBJ_FARRAY = ca_install_obj_type(rb_cCAFarray, ca_farray_func);
472
+ rb_define_const(rb_cObject, "CA_OBJ_FARRAY", INT2NUM(CA_OBJ_FARRAY));
473
+
474
+ rb_define_method(rb_cCArray, "farray", rb_ca_farray, 0);
475
+ rb_define_method(rb_cCArray, "t", rb_ca_farray, 0);
476
+
477
+ rb_define_alloc_func(rb_cCAFarray, rb_ca_farray_s_allocate);
478
+ rb_define_method(rb_cCAFarray, "initialize_copy",
479
+ rb_ca_farray_initialize_copy, 1);
480
+ }
481
+
482
+