carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_access.c CHANGED
@@ -9,7 +9,6 @@
9
9
  ---------------------------------------------------------------------------- */
10
10
 
11
11
  #include "carray.h"
12
- #include "carray_internal.h" /* per-obj_type view constructors */
13
12
  #include "ca_obj_face.h"
14
13
  #include "carray_index_classifier.h" /* rb_ca_scan_index_v2 forward decl */
15
14
 
@@ -23,13 +22,13 @@ static VALUE sym_perc, sym_under, sym_gt, sym_tilde;
23
22
  static VALUE S_CAInfo;
24
23
 
25
24
  VALUE
26
- rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
25
+ rb_ca_store_index (volatile VALUE self, ca_size_t *idx, VALUE rval)
27
26
  {
28
27
  CArray *ca;
29
28
  boolean8_t zero = 0, one = 1;
30
29
 
31
30
  rb_ca_modify(self);
32
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
31
+ GetCArray(self, ca);
33
32
 
34
33
  if ( ca_is_empty(ca) ) {
35
34
  return rval;
@@ -39,15 +38,9 @@ rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
39
38
  ca_create_mask(ca);
40
39
  ca_store_index(ca->mask, idx, &one);
41
40
  }
42
- else { /* unset mask and set value of the element at the index 'idx' */
41
+ else { /* set value of the element at the index 'idx' and unset its mask */
43
42
 
44
- /* unset mask */
45
- ca_update_mask(ca);
46
- if ( ca->mask ) {
47
- ca_store_index(ca->mask, idx, &zero);
48
- }
49
-
50
- /* store value */
43
+ /* A value that can not be stored leaves the cell masked. */
51
44
  {
52
45
  volatile VALUE holder;
53
46
  char *v = ALLOCV_N(char, holder, ca->bytes);
@@ -55,17 +48,22 @@ rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
55
48
  ca_store_index(ca, idx, v);
56
49
  ALLOCV_END(holder);
57
50
  }
51
+
52
+ ca_update_mask(ca);
53
+ if ( ca->mask ) {
54
+ ca_store_index(ca->mask, idx, &zero);
55
+ }
58
56
  }
59
57
 
60
58
  return rval;
61
59
  }
62
60
 
63
61
  VALUE
64
- rb_ca_fetch_index (VALUE self, ca_size_t *idx)
62
+ rb_ca_fetch_index (volatile VALUE self, ca_size_t *idx)
65
63
  {
66
64
  volatile VALUE out;
67
65
  CArray *ca;
68
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
66
+ GetCArray(self, ca);
69
67
 
70
68
  if ( ca_is_empty(ca) ) {
71
69
  return Qnil;
@@ -107,13 +105,13 @@ rb_ca_fetch_index (VALUE self, ca_size_t *idx)
107
105
  }
108
106
 
109
107
  VALUE
110
- rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE rval)
108
+ rb_ca_store_addr (volatile VALUE self, ca_size_t addr, VALUE rval)
111
109
  {
112
110
  CArray *ca;
113
111
  boolean8_t zero = 0, one = 1;
114
112
 
115
113
  rb_ca_modify(self);
116
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
114
+ GetCArray(self, ca);
117
115
 
118
116
  if ( ca_is_empty(ca) ) {
119
117
  return rval;
@@ -123,13 +121,9 @@ rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE rval)
123
121
  ca_create_mask(ca);
124
122
  ca_store_addr(ca->mask, addr, &one);
125
123
  }
126
- else { /* set value at the element */
127
- ca_update_mask(ca);
128
- if ( ca->mask ) {
129
- ca_store_addr(ca->mask, addr, &zero); /* unset mask */
130
- }
124
+ else { /* set value at the element and unset its mask */
131
125
 
132
- /* store value */
126
+ /* A value that can not be stored leaves the cell masked. */
133
127
  {
134
128
  volatile VALUE holder;
135
129
  char *v = ALLOCV_N(char, holder, ca->bytes);
@@ -137,17 +131,22 @@ rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE rval)
137
131
  ca_store_addr(ca, addr, v);
138
132
  ALLOCV_END(holder);
139
133
  }
134
+
135
+ ca_update_mask(ca);
136
+ if ( ca->mask ) {
137
+ ca_store_addr(ca->mask, addr, &zero);
138
+ }
140
139
  }
141
140
 
142
141
  return rval;
143
142
  }
144
143
 
145
144
  VALUE
146
- rb_ca_fetch_addr (VALUE self, ca_size_t addr)
145
+ rb_ca_fetch_addr (volatile VALUE self, ca_size_t addr)
147
146
  {
148
147
  volatile VALUE out;
149
148
  CArray *ca;
150
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
149
+ GetCArray(self, ca);
151
150
 
152
151
  if ( ca_is_empty(ca) ) {
153
152
  return Qnil;
@@ -185,12 +184,12 @@ rb_ca_fetch_addr (VALUE self, ca_size_t addr)
185
184
  }
186
185
 
187
186
  VALUE
188
- rb_ca_fill (VALUE self, VALUE rval)
187
+ rb_ca_fill (volatile VALUE self, VALUE rval)
189
188
  {
190
189
  CArray *ca;
191
190
 
192
191
  rb_ca_modify(self);
193
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
192
+ GetCArray(self, ca);
194
193
 
195
194
  if ( ca_is_empty(ca) ) {
196
195
  /* No cell to fill, but a value no cell could hold is refused, as an
@@ -233,6 +232,7 @@ rb_ca_fill_copy (VALUE self, VALUE rval)
233
232
 
234
233
  /* -------------------------------------------------------------------- */
235
234
 
235
+ /* recursion: one level per dimension; ca_check_rank_fits bounds it by CA_RANK_MAX */
236
236
  static void
237
237
  ary_guess_shape (VALUE ary, int level, int *max_level, ca_size_t *dim)
238
238
  {
@@ -272,13 +272,14 @@ rb_ca_s_guess_array_shape (VALUE self, VALUE ary)
272
272
  int max_level = -1;
273
273
  int i;
274
274
  ary_guess_shape(ary, 0, &max_level, dim);
275
- out = rb_ary_new2(max_level);
275
+ out = rb_ary_new_capa(max_level);
276
276
  for (i=0; i<max_level+1; i++) {
277
277
  rb_ary_store(out, i, SIZE2NUM(dim[i]));
278
278
  }
279
279
  return out;
280
280
  }
281
281
 
282
+ /* recursion: one level per dimension, up to the level ary_guess_shape found */
282
283
  static void
283
284
  ary_flatten_upto_level (VALUE ary, int max_level, int level,
284
285
  VALUE out, int *len)
@@ -333,7 +334,7 @@ rb_ary_flatten_for_elements (VALUE ary, ca_size_t elements, void *ap)
333
334
  rb_raise(rb_eArgError, "invalid shape array for conversion to carray");
334
335
  }
335
336
  else {
336
- volatile VALUE out = rb_ary_new2(0);
337
+ volatile VALUE out = rb_ary_new_capa(0);
337
338
  int len = 0;
338
339
  ary_flatten_upto_level(ary, max_level, 0, out, &len);
339
340
  if ( len != elements ) {
@@ -359,7 +360,7 @@ rb_ary_flatten_for_elements (VALUE ary, ca_size_t elements, void *ap)
359
360
  }
360
361
 
361
362
  if ( same_shape ) {
362
- volatile VALUE out = rb_ary_new2(0);
363
+ volatile VALUE out = rb_ary_new_capa(0);
363
364
  int len = 0;
364
365
  ary_flatten_upto_level(ary, ca->ndim-1, 0, out, &len);
365
366
 
@@ -381,7 +382,7 @@ rb_ary_flatten_for_elements (VALUE ary, ca_size_t elements, void *ap)
381
382
  }
382
383
 
383
384
  if ( level >= 0 ) {
384
- volatile VALUE out = rb_ary_new2(0);
385
+ volatile VALUE out = rb_ary_new_capa(0);
385
386
  int len = 0;
386
387
  ary_flatten_upto_level(ary, level, 0, out, &len);
387
388
  if ( len != elements ) {
@@ -426,25 +427,49 @@ rb_ca_ref_address (VALUE self, CAIndexInfo *info)
426
427
  return rb_ca_fetch_addr(self, addr);
427
428
  }
428
429
 
430
+ /* What a store to one cell takes from an array: its one cell. A cell of an
431
+ object array holds any value, the array itself included, so only a
432
+ CScalar is opened there. */
433
+ static VALUE
434
+ ca_cell_of_store_value (volatile VALUE self, volatile VALUE rval)
435
+ {
436
+ CArray *ca, *cv;
437
+ if ( rb_obj_is_cscalar(rval) ) {
438
+ return rb_ca_fetch_addr(rval, 0);
439
+ }
440
+ if ( ! rb_obj_is_carray(rval) ) {
441
+ return rval;
442
+ }
443
+ GetCArray(self, ca);
444
+ if ( ca->data_type == CA_OBJECT ) {
445
+ return rval;
446
+ }
447
+ GetCArray(rval, cv);
448
+ if ( cv->elements != 1 ) {
449
+ rb_raise(rb_eArgError,
450
+ "%s: shape mismatch writing to one cell (<- %" PRIi64 " cells)",
451
+ ca_calling_method_name(), (int64_t) cv->elements);
452
+ }
453
+ return rb_ca_fetch_addr(rval, 0);
454
+ }
455
+
429
456
  static VALUE
430
457
  rb_ca_store_address (VALUE self, CAIndexInfo *info, volatile VALUE rval)
431
458
  {
432
459
  ca_size_t addr;
433
460
  addr = info->index[0].scalar;
434
- if ( rb_obj_is_cscalar(rval) ) {
435
- rval = rb_ca_fetch_addr(rval, 0);
436
- }
461
+ rval = ca_cell_of_store_value(self, rval);
437
462
  rb_ca_store_addr(self, addr, rval);
438
463
  return rval;
439
464
  }
440
465
 
441
466
  static VALUE
442
- rb_ca_ref_point (VALUE self, CAIndexInfo *info)
467
+ rb_ca_ref_point (volatile VALUE self, CAIndexInfo *info)
443
468
  {
444
469
  CArray *ca;
445
470
  ca_size_t idx[CA_RANK_MAX];
446
471
  int8_t i;
447
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
472
+ GetCArray(self, ca);
448
473
  for (i=0; i<ca->ndim; i++) {
449
474
  idx[i] = info->index[i].scalar;
450
475
  }
@@ -452,18 +477,16 @@ rb_ca_ref_point (VALUE self, CAIndexInfo *info)
452
477
  }
453
478
 
454
479
  static VALUE
455
- rb_ca_store_point (VALUE self, CAIndexInfo *info, volatile VALUE val)
480
+ rb_ca_store_point (volatile VALUE self, CAIndexInfo *info, volatile VALUE val)
456
481
  {
457
482
  CArray *ca;
458
483
  ca_size_t idx[CA_RANK_MAX];
459
484
  int8_t i;
460
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
485
+ GetCArray(self, ca);
461
486
  for (i=0; i<ca->ndim; i++) {
462
487
  idx[i] = info->index[i].scalar;
463
488
  }
464
- if ( rb_obj_is_cscalar(val) ) {
465
- val = rb_ca_fetch_addr(val, 0);
466
- }
489
+ val = ca_cell_of_store_value(self, val);
467
490
  rb_ca_store_index(self, idx, val);
468
491
  return val;
469
492
  }
@@ -482,9 +505,9 @@ ca_id_expression_evaluator (void)
482
505
  return id;
483
506
  }
484
507
 
485
- /* The casting stores below convert into the destination inside a window,
486
- and a conversion from Ruby objects can raise part way. Such a window
487
- attaches rather than allocates: the cells converted before the raise
508
+ /* The casting stores below convert into the destination while it is
509
+ attached, and a conversion from Ruby objects can raise part way. Such a
510
+ store attaches rather than allocates: the cells converted before the raise
488
511
  are then pushed back with the others left as they were -- what an
489
512
  entity destination, which has no separate buffer, shows as well. A
490
513
  cast that cannot raise keeps the cheaper allocate. */
@@ -505,7 +528,7 @@ ca_store_all_mode (CArray *cv)
505
528
  return ( cv->data_type == CA_OBJECT ) ? "w" : "a";
506
529
  }
507
530
 
508
- /* The window has attached ca's mask, and the one created here joins it,
531
+ /* ca_attach_window has attached ca's mask, and the one made here joins it,
509
532
  so the mask is written in its buffer. */
510
533
  static VALUE
511
534
  ca_store_all_cast_body (VALUE arg)
@@ -522,8 +545,8 @@ ca_store_all_cast_body (VALUE arg)
522
545
  memset(ca->mask->ptr, 0, ca->elements);
523
546
  }
524
547
  if ( ca->mask ) {
525
- ca_cast_block_with_mask(ca->elements, c->cv, c->scratch, ca, ca->ptr,
526
- (boolean8_t*)ca->mask->ptr);
548
+ ca_cast_block_skip_masked(ca->elements, c->cv, c->scratch, ca, ca->ptr,
549
+ (boolean8_t*)ca->mask->ptr);
527
550
  }
528
551
  else {
529
552
  ca_cast_block(ca->elements, c->cv, c->scratch, ca, ca->ptr);
@@ -536,7 +559,7 @@ ca_store_all_list_body (VALUE arg)
536
559
  {
537
560
  ca_store_all_ctx_t *c = (ca_store_all_ctx_t *) arg;
538
561
  CArray *ca = c->ca;
539
- VALUE list = c->list;
562
+ volatile VALUE list = c->list;
540
563
  ca_size_t i;
541
564
  /* Face has surface != storage; the cast runs in the storage
542
565
  data_type. Passing ca directly makes the cast table see FIXLEN
@@ -567,9 +590,9 @@ ca_store_all_list_body (VALUE arg)
567
590
  }
568
591
  m++;
569
592
  }
570
- ca_cast_block_with_mask(ca->elements, c->ico, (VALUE *)RARRAY_CONST_PTR(list),
571
- cast_target, ca->ptr,
572
- (boolean8_t*)ca->mask->ptr);
593
+ ca_cast_block_skip_masked(ca->elements, c->ico, (VALUE *)RARRAY_CONST_PTR(list),
594
+ cast_target, ca->ptr,
595
+ (boolean8_t*)ca->mask->ptr);
573
596
  }
574
597
  else {
575
598
  /* Every cell gets a value, so none stays masked. */
@@ -591,7 +614,7 @@ ca_store_all_list_body (VALUE arg)
591
614
  class, or one that reconciles a Face (to_comparable), is left to
592
615
  ca_face_convert_store_rval. */
593
616
  static VALUE
594
- ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
617
+ ca_face_source_for_store (VALUE self, CArray *ca, volatile VALUE rval)
595
618
  {
596
619
  CArray *cr;
597
620
 
@@ -612,17 +635,17 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
612
635
  }
613
636
  if ( cr->obj_type == CA_OBJ_RECORD ) {
614
637
  rb_raise(rb_eTypeError,
615
- "can not store a record array of %"PRIsVALUE
616
- " into a record array of %"PRIsVALUE,
638
+ "%s: can not store a record array of %"PRIsVALUE
639
+ " into a record array of %"PRIsVALUE, ca_calling_method_name(),
617
640
  rb_ca_data_class(rval), rb_ca_data_class(self));
618
641
  }
619
642
  rb_raise(rb_eTypeError,
620
- "can not store a %s array into a record array of %"PRIsVALUE,
621
- rb_obj_classname(rval), rb_ca_data_class(self));
643
+ "%s: can not store a %"PRIsVALUE" array into a record array of %"PRIsVALUE, ca_calling_method_name(),
644
+ rb_obj_class(rval), rb_ca_data_class(self));
622
645
  }
623
646
  if ( ca_is_face(ca) ) {
624
647
  if ( rb_obj_class(self) == rb_obj_class(rval)
625
- || rb_respond_to(self, rb_intern("to_comparable")) ) {
648
+ || rb_obj_respond_to(self, rb_intern("to_comparable"), Qtrue) ) {
626
649
  return rval;
627
650
  }
628
651
  }
@@ -637,21 +660,36 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
637
660
  return rval;
638
661
  }
639
662
  rb_raise(rb_eTypeError,
640
- "can not store a %s array into %s array "
641
- "(to_type(:object) gives its values)",
642
- rb_obj_classname(rval), ca_type_name[ca->data_type]);
663
+ "%s: can not store a %"PRIsVALUE" array into %s array "
664
+ "(to_type(:object) gives its values)", ca_calling_method_name(),
665
+ rb_obj_class(rval), ca_type_name[ca->data_type]);
643
666
  }
644
- return rb_funcall(rval, rb_intern("to_type"), 1, INT2NUM(CA_OBJECT));
667
+ /* coerce: a store into the receiver, an object array */
668
+ return rb_ca_to_type(rval, ID2SYM(ca_data_type_sym[CA_OBJECT]), Qnil);
645
669
  }
646
670
 
647
671
  /* A store into no cells writes nothing, but the value is checked as it
648
672
  would be for any cells: the same statement does not raise or pass
649
673
  depending on how many cells the index picks. */
674
+ /* Writing complex cells into real ones would drop the imaginary parts
675
+ without a word, where a Complex scalar is refused. */
676
+ void
677
+ ca_refuse_complex_into_real (CArray *ca, CArray *cv)
678
+ {
679
+ if ( ca_is_complex_type(cv) && ! ca_is_complex_type(ca)
680
+ && ca->data_type != CA_OBJECT ) {
681
+ rb_raise(rb_eCADataTypeError,
682
+ "%s: cannot store %s cells in %s (#real and #to_type take the part wanted)",
683
+ ca_calling_method_name(), ca_type_name[cv->data_type],
684
+ ca_type_name[ca->data_type]);
685
+ }
686
+ }
687
+
650
688
  static void
651
- ca_store_all_check_empty (VALUE self, VALUE rval)
689
+ ca_store_all_check_empty (volatile VALUE self, volatile VALUE rval)
652
690
  {
653
691
  CArray *ca;
654
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
692
+ GetCArray(self, ca);
655
693
  if ( rb_obj_is_cscalar(rval) ) {
656
694
  rval = rb_ca_fetch_addr(rval, 0);
657
695
  }
@@ -661,7 +699,10 @@ ca_store_all_check_empty (VALUE self, VALUE rval)
661
699
  rval = rb_funcall(rval, id_to_ca, 0);
662
700
  }
663
701
  if ( rb_obj_is_carray(rval) ) {
702
+ CArray *cv;
664
703
  ca_broadcast_to_destination(self, (volatile VALUE *) &rval);
704
+ GetCArray(rval, cv);
705
+ ca_refuse_complex_into_real(ca, cv);
665
706
  }
666
707
  else if ( TYPE(rval) == T_ARRAY ) {
667
708
  if ( NIL_P(rb_ary_flatten_for_elements(rval, ca->elements, ca)) ) {
@@ -677,7 +718,7 @@ ca_store_all_check_empty (VALUE self, VALUE rval)
677
718
  }
678
719
 
679
720
  VALUE
680
- rb_ca_store_all (VALUE self, VALUE rval)
721
+ rb_ca_store_all (volatile VALUE self, volatile VALUE rval)
681
722
  {
682
723
  CArray *ca;
683
724
 
@@ -695,14 +736,14 @@ rb_ca_store_all (VALUE self, VALUE rval)
695
736
  rval = rb_ca_fetch_addr(rval, 0);
696
737
  }
697
738
 
698
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
739
+ GetCArray(self, ca);
699
740
  rval = ca_face_source_for_store(self, ca, rval);
700
741
 
701
742
  retry:
702
743
 
703
744
  if ( rb_obj_is_carray(rval) ) {
704
745
  CArray *cv;
705
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
746
+ GetCArray(rval, cv);
706
747
 
707
748
  /* Where something is registered to compute an expression, and this is
708
749
  one, it is asked before the walk below -- it fills the destination
@@ -732,7 +773,8 @@ rb_ca_store_all (VALUE self, VALUE rval)
732
773
 
733
774
  /* The destination owns the shape; see ca_broadcast_to_destination. */
734
775
  ca_broadcast_to_destination(self, &rval);
735
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
776
+ GetCArray(rval, cv);
777
+ ca_refuse_complex_into_real(ca, cv);
736
778
 
737
779
  /* Fixlen cells of another width are fitted cell by cell, padded with
738
780
  NUL or cut to the destination's width, as a String or an Array of
@@ -742,7 +784,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
742
784
  && ca->bytes != cv->bytes ) {
743
785
  rval = rb_ca_to_type(rb_ca_strip_face_value(rval), INT2NUM(CA_FIXLEN),
744
786
  SIZE2NUM(ca->bytes));
745
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
787
+ GetCArray(rval, cv);
746
788
  }
747
789
 
748
790
  /* Source delivery via ca_xfer_all into a local scratch instead of
@@ -808,6 +850,9 @@ rb_ca_store_all (VALUE self, VALUE rval)
808
850
  volatile VALUE list =
809
851
  rb_ary_flatten_for_elements(rval, ca->elements, ca);
810
852
  ca_size_t i;
853
+ if ( ! NIL_P(list) && ! ca_is_face(ca) ) {
854
+ ca_refuse_non_number_elements(ca->data_type, list);
855
+ }
811
856
  if ( NIL_P(list) ) {
812
857
  rb_raise(rb_eArgError,
813
858
  "failed to guess data size of given array");
@@ -830,7 +875,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
830
875
  old values -- but converting a Ruby object can raise part way, and
831
876
  then the cells already converted are pushed back with the rest
832
877
  left as they were, which needs the old values in the buffer. So
833
- this window attaches (see ca_store_all_mode). */
878
+ this store attaches (see ca_store_all_mode). */
834
879
  {
835
880
  ca_store_all_ctx_t c;
836
881
  c.ca = ca;
@@ -945,7 +990,7 @@ rb_ca_index_restruct_block (int16_t *ndimp, ca_size_t *shrink, ca_size_t *dim,
945
990
  }
946
991
 
947
992
  VALUE
948
- rb_ca_ref_block (VALUE self, CAIndexInfo *info)
993
+ rb_ca_ref_block (volatile VALUE self, CAIndexInfo *info)
949
994
  {
950
995
  volatile VALUE refer;
951
996
  CArray *ca;
@@ -959,7 +1004,7 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
959
1004
  ca_size_t flag = 0;
960
1005
  ca_size_t i;
961
1006
 
962
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1007
+ GetCArray(self, ca);
963
1008
 
964
1009
  ndim = info->ndim;
965
1010
 
@@ -1015,7 +1060,7 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
1015
1060
  casts in the storage type. */
1016
1061
  if ( ca_is_face(ca) ) {
1017
1062
  CArray *blk_ca, *root = ca_strip_face(ca);
1018
- TypedData_Get_Struct(blk, CArray, &carray_data_type, blk_ca);
1063
+ GetCArray(blk, blk_ca);
1019
1064
  if (root && root->data_type != blk_ca->data_type) {
1020
1065
  blk_ca->data_type = root->data_type;
1021
1066
  }
@@ -1025,12 +1070,12 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
1025
1070
  }
1026
1071
 
1027
1072
  static VALUE
1028
- rb_ca_refer_new_flatten (VALUE self)
1073
+ rb_ca_refer_new_flatten (volatile VALUE self)
1029
1074
  {
1030
1075
  CArray *ca;
1031
1076
  ca_size_t dim0;
1032
1077
 
1033
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1078
+ GetCArray(self, ca);
1034
1079
  dim0 = ca->elements;
1035
1080
  return rb_ca_refer_new(self, ca->data_type, 1, &dim0, ca->bytes, 0);
1036
1081
  }
@@ -1052,16 +1097,17 @@ extern VALUE rb_ca_remap_new (VALUE cary, VALUE rmapper);
1052
1097
  A masked mapper cell selects nothing: the cell keeps its position and
1053
1098
  reads as masked (a CAGrid hole); the same-shape fast path is left to a
1054
1099
  mapper with no masked cell. */
1100
+ /* recursion: re-enters once, with the Face taken off */
1055
1101
  static VALUE
1056
- rb_ca_fancy_index_chain (VALUE self, VALUE rmapper)
1102
+ rb_ca_fancy_index_chain (volatile VALUE self, volatile VALUE rmapper)
1057
1103
  {
1058
1104
  CArray *ca, *cm;
1059
1105
  volatile VALUE flat_self, flat_mapper, gridded, result;
1060
1106
  VALUE grid_argv[1];
1061
1107
  int i;
1062
1108
 
1063
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1064
- TypedData_Get_Struct(rmapper, CArray, &carray_data_type, cm);
1109
+ GetCArray(self, ca);
1110
+ GetCArray(rmapper, cm);
1065
1111
 
1066
1112
  /* (a) same-shape fast path: ndim + per-axis dim + CA_SIZE data_type.
1067
1113
  The view reads the index array it is given, so it is given a copy: the
@@ -1075,7 +1121,7 @@ rb_ca_fancy_index_chain (VALUE self, VALUE rmapper)
1075
1121
  volatile VALUE snapshot = rb_ca_copy(rmapper);
1076
1122
  CArray *cs;
1077
1123
  ca_size_t k, *p;
1078
- TypedData_Get_Struct(snapshot, CArray, &carray_data_type, cs);
1124
+ GetCArray(snapshot, cs);
1079
1125
  p = (ca_size_t *) cs->ptr;
1080
1126
  for (k = 0; k < cs->elements; k++) {
1081
1127
  CA_CHECK_INDEX(p[k], ca->elements);
@@ -1120,11 +1166,56 @@ rb_ca_mapping_new (VALUE cary, CArray *mapper)
1120
1166
  return rb_ca_fancy_index_chain(cary, rmapper);
1121
1167
  }
1122
1168
 
1169
+ /* The index a[:m, x] stands for: a.m(x), called as a public method of
1170
+ the array would be (a[:puts] does not run Kernel#puts). The form names
1171
+ a predicate, so the answer must be a boolean array of the array's shape;
1172
+ anything else -- a number, an address list, an array of ranks -- would be
1173
+ read as some other kind of index and pick the wrong cells without a word.
1174
+ `kw` says whether the last argument is keywords for the method. */
1175
+ static VALUE
1176
+ ca_index_method_answer (volatile VALUE self, VALUE sym, int argc, const VALUE *argv,
1177
+ int kw)
1178
+ {
1179
+ volatile VALUE idx;
1180
+ CArray *ca, *ci;
1181
+ int i, same;
1182
+
1183
+ idx = rb_funcallv_public_kw(self, SYM2ID(sym), argc, argv, kw);
1184
+ if ( ! rb_obj_is_carray(idx) ) {
1185
+ rb_raise(rb_eTypeError,
1186
+ "%s: %"PRIsVALUE" returned an object of class %"PRIsVALUE"; a method in an index "
1187
+ "must return a boolean array of the array's shape",
1188
+ ca_calling_method_name(), rb_inspect(sym), rb_obj_class(idx));
1189
+ }
1190
+ GetCArray(self, ca);
1191
+ GetCArray(idx, ci);
1192
+ if ( ci->data_type != CA_BOOLEAN ) {
1193
+ rb_raise(rb_eTypeError,
1194
+ "%s: %"PRIsVALUE" returned a CArray of data type %s; a method in an index "
1195
+ "must return a boolean array of the array's shape",
1196
+ ca_calling_method_name(), rb_inspect(sym),
1197
+ ca_type_name[ci->data_type]);
1198
+ }
1199
+ same = ( ci->ndim == ca->ndim );
1200
+ for (i = 0; same && i < ca->ndim; i++) {
1201
+ same = ( ci->dim[i] == ca->dim[i] );
1202
+ }
1203
+ if ( ! same ) {
1204
+ rb_raise(rb_eArgError,
1205
+ "%s: %"PRIsVALUE" returned a boolean array of shape %"PRIsVALUE
1206
+ "; a method in an index must return one of the array's shape "
1207
+ "%"PRIsVALUE,
1208
+ ca_calling_method_name(), rb_inspect(sym),
1209
+ rb_inspect(rb_ca_dim(idx)), rb_inspect(rb_ca_dim(self)));
1210
+ }
1211
+ return idx;
1212
+ }
1213
+
1123
1214
  VALUE
1124
1215
  rb_ca_mapping (int argc, VALUE *argv, VALUE self)
1125
1216
  {
1126
- volatile VALUE rmapper;
1127
- rb_scan_args(argc, argv, "1", (VALUE *) &rmapper);
1217
+ VALUE rmapper;
1218
+ rb_scan_args(argc, argv, "1", &rmapper);
1128
1219
  rb_check_carray_object(rmapper);
1129
1220
  return rb_ca_fancy_index_chain(self, rmapper);
1130
1221
  }
@@ -1144,7 +1235,7 @@ ca_argv_is_method_call (int argc, VALUE *argv)
1144
1235
  {
1145
1236
  const char *name;
1146
1237
  if ( argc < 1 || ! SYMBOL_P(argv[0]) ) return 0;
1147
- name = rb_id2name(SYM2ID(argv[0]));
1238
+ name = rb_id2name(SYM2ID(argv[0])); /* name: read at once by strlen */
1148
1239
  return ( name != NULL && strlen(name) > 1 );
1149
1240
  }
1150
1241
 
@@ -1174,12 +1265,13 @@ static VALUE rb_ca_slab_iter_new (VALUE self, CAIndexInfo *info);
1174
1265
  static VALUE rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self);
1175
1266
 
1176
1267
  /* axis-group apply (ext/ca_group_iter.c): type gate that routes a
1177
- CACategorical/AxisGroup index to the group path. */
1268
+ CACategorical/CAAxisGroup index to the group path. */
1178
1269
  int ca_argv_has_group (int argc, VALUE *argv);
1179
1270
  VALUE rb_ca_fetch_group (int argc, VALUE *argv, VALUE self);
1180
1271
 
1272
+ /* recursion: re-enters once, on the flattened array */
1181
1273
  static VALUE
1182
- rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1274
+ rb_ca_fetch_method (int argc, VALUE *argv, volatile VALUE self)
1183
1275
  {
1184
1276
  volatile VALUE obj = Qnil;
1185
1277
  CArray *ca;
@@ -1187,12 +1279,14 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1187
1279
 
1188
1280
  retry:
1189
1281
 
1190
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1282
+ GetCArray(self, ca);
1191
1283
 
1192
1284
  if ( ca_argv_is_method_call(argc, argv) ) {
1193
- goto classify;
1285
+ goto classify; /* the keywords go on to the method */
1194
1286
  }
1195
1287
 
1288
+ rb_reject_options(rb_pop_options(&argc, &argv));
1289
+
1196
1290
  /* newaxis (:_) interception (cheap pointer scan; zero overhead beyond
1197
1291
  the loop when no :_ is present). */
1198
1292
  if ( ca_argv_has_newaxis(argc, argv) ) {
@@ -1201,13 +1295,13 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1201
1295
 
1202
1296
  /* slab sigil (:>) interception. Pre-strip :> -> nil and Integer ->
1203
1297
  length-1 Range so mask / fancy outer slots route through the regular
1204
- dispatch (CA_REG_SELECT / CA_REG_GRID / CA_REG_MAPPING) before being
1205
- wrapped in CASlabIterator. */
1298
+ dispatch (CA_REG_SELECT / CA_REG_GRID / CA_REG_ADDRESS_ARRAY) before
1299
+ being wrapped in CASlabIterator. */
1206
1300
  if ( ca_argv_has_slab_sigil(argc, argv) ) {
1207
1301
  return rb_ca_fetch_slab_sigil(argc, argv, self);
1208
1302
  }
1209
1303
 
1210
- /* Axis-group type gate: a CACategorical/AxisGroup index routes to the
1304
+ /* Axis-group type gate: a CACategorical/CAAxisGroup index routes to the
1211
1305
  group apply path (CAGroupIterator); plain int-array indices fall
1212
1306
  through to the regular selection dispatch below. The scan short-circuits
1213
1307
  when the surface classes have not been loaded, so a normal index pays
@@ -1269,17 +1363,17 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1269
1363
  case CA_REG_REPEAT:
1270
1364
  obj = rb_ca_repeat(argc, argv, self);
1271
1365
  break;
1272
- case CA_REG_MAPPING:
1366
+ case CA_REG_ADDRESS_ARRAY:
1273
1367
  obj = rb_ca_fancy_index_chain(self, argv[0]);
1274
1368
  break;
1275
1369
  case CA_REG_GRID:
1276
1370
  obj = rb_ca_grid(argc, argv, self);
1277
1371
  break;
1278
1372
  case CA_REG_METHOD_CALL: {
1279
- /* a[:eq, 0] is a[a.eq(0)]: a public method of the array, as a call
1280
- written out would reach (a[:puts] does not run Kernel#puts). */
1373
+ /* a[:eq, 0] is a[a.eq(0)]; keywords go to the method as keywords */
1281
1374
  volatile VALUE idx;
1282
- idx = rb_funcallv_public(self, SYM2ID(info.symbol), argc-1, argv+1);
1375
+ idx = ca_index_method_answer(self, info.symbol, argc-1, argv+1,
1376
+ rb_keyword_given_p());
1283
1377
  obj = rb_ca_fetch(self, idx);
1284
1378
  break;
1285
1379
  }
@@ -1294,12 +1388,12 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1294
1388
  }
1295
1389
  else {
1296
1390
  rb_raise(rb_eIndexError,
1297
- "can't refer member of carray doesn't have data_class");
1391
+ "%s: a String names a member, and only a struct array has members", ca_calling_method_name());
1298
1392
  }
1299
1393
  break;
1300
1394
  }
1301
1395
  default:
1302
- rb_raise(rb_eIndexError, "invalid index specified");
1396
+ rb_raise(rb_eIndexError, "%s: invalid index specified", ca_calling_method_name());
1303
1397
  }
1304
1398
 
1305
1399
  /* Wrapper lift at the read touch point: if `self` is a Face or a
@@ -1347,11 +1441,11 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1347
1441
  if ( a == sym_gt || a == sym_perc
1348
1442
  || a == Qfalse || a == sym_tilde ) {
1349
1443
  rb_raise(rb_eIndexError,
1350
- "newaxis (:_) cannot be combined with :>, :%%, "
1351
- "or rubber dim (false / :~)");
1444
+ "%s: newaxis (:_) cannot be combined with :>, :%%, "
1445
+ "or rubber dim (false / :~)", ca_calling_method_name());
1352
1446
  }
1353
1447
  if ( nclean >= CA_RANK_MAX ) { /* rank-check: a count of indices */
1354
- rb_raise(rb_eIndexError, "too many indices");
1448
+ rb_raise(rb_eIndexError, "%s: too many indices", ca_calling_method_name());
1355
1449
  }
1356
1450
  clean[nclean++] = a;
1357
1451
  }
@@ -1360,8 +1454,8 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1360
1454
  for partial indices, which is incoherent with axis-position newaxis). */
1361
1455
  if ( nclean != ca->ndim ) {
1362
1456
  rb_raise(rb_eIndexError,
1363
- "newaxis (:_) requires full per-axis indexing "
1364
- "(got %d non-:_ indices for ndim %d)",
1457
+ "%s: newaxis (:_) requires full per-axis indexing "
1458
+ "(got %d non-:_ indices for ndim %d)", ca_calling_method_name(),
1365
1459
  nclean, (int) ca->ndim);
1366
1460
  }
1367
1461
 
@@ -1375,7 +1469,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1375
1469
  VALUE blocks[CA_RANK_MAX];
1376
1470
  ca_check_rank_fits(nunder, NULL); /* the result has the :_ axes only */
1377
1471
  for (i = 0; i < nclean; i++) {
1378
- blocks[i] = rb_assoc_new(clean[i], INT2FIX(1));
1472
+ blocks[i] = rb_range_new(clean[i], clean[i], 0);
1379
1473
  }
1380
1474
  base = rb_ca_fetch_method(nclean, blocks, self);
1381
1475
  for (i = 0; i < nunder; i++) {
@@ -1386,7 +1480,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1386
1480
 
1387
1481
  base = rb_ca_fetch_method(nclean, clean, self);
1388
1482
 
1389
- TypedData_Get_Struct(base, CArray, &carray_data_type, cb);
1483
+ GetCArray(base, cb);
1390
1484
 
1391
1485
  /* The result has the base's axes and one for each :_, before any is
1392
1486
  written into final_dims. */
@@ -1430,7 +1524,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1430
1524
  }
1431
1525
  }
1432
1526
  if ( base_cursor != cb->ndim ) {
1433
- rb_raise(rb_eIndexError, "newaxis: base axis count mismatch");
1527
+ rb_raise(rb_eIndexError, "%s: newaxis: base axis count mismatch", ca_calling_method_name());
1434
1528
  }
1435
1529
 
1436
1530
  for (i = 0; i < nfinal; i++) {
@@ -1473,7 +1567,7 @@ rb_ca_slab_iter_new (VALUE self, CAIndexInfo *info)
1473
1567
  reference (CABlock / CASelectAxis / CAGrid / ...), then
1474
1568
  wrap in CASlabIterator with the recorded slab_axes positions. */
1475
1569
  static VALUE
1476
- rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1570
+ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, volatile VALUE self)
1477
1571
  {
1478
1572
  VALUE expanded[CA_RANK_MAX];
1479
1573
  VALUE clean[CA_RANK_MAX];
@@ -1486,7 +1580,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1486
1580
  VALUE *eargv;
1487
1581
  int rubber_pos = -1;
1488
1582
 
1489
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1583
+ GetCArray(self, ca);
1490
1584
 
1491
1585
  /* Rubber-dim expansion (mirrors ca_classifier_expand_rubber_dim).
1492
1586
  Only the first rubber marker is honoured; later ones are left for
@@ -1505,7 +1599,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1505
1599
  int rndim;
1506
1600
  if ( argc > ca->ndim + 1 ) {
1507
1601
  rb_raise(rb_eIndexError,
1508
- "index specification exceeds the ndim of carray (%i)",
1602
+ "%s: index specification exceeds the ndim of carray (%i)", ca_calling_method_name(),
1509
1603
  ca->ndim);
1510
1604
  }
1511
1605
  rndim = ca->ndim - argc + 1;
@@ -1525,7 +1619,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1525
1619
  }
1526
1620
 
1527
1621
  if ( eargc > CA_RANK_MAX ) { /* rank-check: a count of indices */
1528
- rb_raise(rb_eIndexError, "too many indices");
1622
+ rb_raise(rb_eIndexError, "%s: too many indices", ca_calling_method_name());
1529
1623
  }
1530
1624
 
1531
1625
  for (i = 0; i < eargc; i++) {
@@ -1548,7 +1642,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1548
1642
  base = rb_ca_fetch_method(eargc, clean, self);
1549
1643
  if ( ! rb_obj_is_kind_of(base, rb_cCArray) ) {
1550
1644
  rb_raise(rb_eIndexError,
1551
- "slab sigil (:>) requires the base view to be a CArray");
1645
+ "%s: slab sigil (:>) requires the base view to be a CArray", ca_calling_method_name());
1552
1646
  }
1553
1647
  klass = rb_const_get(rb_cObject, rb_intern("CASlabIterator"));
1554
1648
  return rb_funcall(klass, rb_intern("new"), 2, base, slab_axes);
@@ -1602,15 +1696,17 @@ rb_cs_store_method (int argc, VALUE *argv, VALUE self)
1602
1696
  integer-storage Face takes integer cells only. The scalar path already
1603
1697
  refuses a bare Float there (to_comparable does not recognise it) instead
1604
1698
  of letting the storage cast truncate it, and the bulk path must not be
1605
- the looser door.
1699
+ the looser door. An object array is not storage but values, and goes
1700
+ in as an Array does.
1606
1701
  - an Array is mapped structure-preserving (leaves converted),
1607
1702
  - a scalar goes through the write hook (ca_face_element_to_storage), which
1608
1703
  leaves a bare Integer / String unchanged.
1609
1704
 
1610
1705
  Idempotent: an already-converted Integer passes through, so re-running on
1611
1706
  a `goto retry` iteration is harmless. */
1707
+ /* recursion: re-enters once, with the value converted */
1612
1708
  static VALUE
1613
- ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1709
+ ca_face_convert_store_rval (VALUE self, CArray *ca, volatile VALUE val)
1614
1710
  {
1615
1711
  if ( rb_obj_is_carray(val) ) {
1616
1712
  CArray *cval;
@@ -1639,12 +1735,22 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1639
1735
  return ca_face_convert_store_rval(self, ca,
1640
1736
  rb_funcall(val, rb_intern("to_a"), 0));
1641
1737
  }
1738
+ /* A plain object array holds Ruby values, which are what a Face's cells
1739
+ are written with, not its storage: it goes in as those values, each
1740
+ through the write hook, as a Ruby Array and a single cell do. The
1741
+ values to_type(:object) gives are written back that way, and a value
1742
+ the hook refuses (a Float into a time) is refused as it is in one cell.
1743
+ Masked cells come back as UNDEF and stay masked. */
1744
+ if ( ! ca_is_face(cval) && cval->data_type == CA_OBJECT ) {
1745
+ return ca_face_convert_store_rval(self, ca,
1746
+ rb_funcall(val, rb_intern("to_a"), 0));
1747
+ }
1642
1748
  /* Face RHS: reconcile to self's unit and descend to storage. Mirror of
1643
1749
  the scalar path (ca_face_element_to_storage) for a bulk CArray source.
1644
1750
  Guarded on self having to_comparable so a Face without a reconcile
1645
1751
  algebra (e.g. a fixlen-storage Face, whose same-storage store already
1646
1752
  takes the storage xfer branch) is left to the existing store path. */
1647
- if ( ca_is_face(cval) && rb_respond_to(self, rb_intern("to_comparable")) ) {
1753
+ if ( ca_is_face(cval) && rb_obj_respond_to(self, rb_intern("to_comparable"), Qtrue) ) {
1648
1754
  return ca_face_reconcile_operand(self, val, "[]=");
1649
1755
  }
1650
1756
  /* A Face of the same class and a compatible state holds values of the
@@ -1653,28 +1759,23 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1653
1759
  && ca_face_state_compatible(self, ca, val, cval) ) {
1654
1760
  return rb_ca_strip_face_value(val);
1655
1761
  }
1656
- /* Bare storage escape, integer storage: integer cells only (see above). */
1762
+ /* A bare array into a Face with integer storage holds values, not that
1763
+ storage: it goes in as its values, each through the write hook, as an
1764
+ object array does, so a value the hook refuses (an Integer or a Float
1765
+ into a time) is refused as it is in one cell. The storage itself is
1766
+ written through the Face's storage accessor (#ticks, #codes). */
1657
1767
  {
1658
1768
  CArray *storage = ca_strip_face(ca);
1659
- if ( storage && ca_is_integer_type(storage) && ! ca_is_integer_type(cval) ) {
1660
- const char *method = ca_face_storage_method(self);
1661
- if ( method ) {
1662
- rb_raise(rb_eTypeError,
1663
- "%s cannot store a bare %s array as raw storage "
1664
- "(write the %s storage, which #%s gives, directly)",
1665
- rb_obj_classname(self), ca_type_name[cval->data_type],
1666
- ca_type_name[storage->data_type], method);
1667
- }
1668
- rb_raise(rb_eTypeError,
1669
- "%s cannot store a bare %s array as raw storage",
1670
- rb_obj_classname(self), ca_type_name[cval->data_type]);
1769
+ if ( storage && ca_is_integer_type(storage) && ! ca_is_face(cval) ) {
1770
+ return ca_face_convert_store_rval(self, ca,
1771
+ rb_funcall(val, rb_intern("to_a"), 0));
1671
1772
  }
1672
1773
  }
1673
1774
  return val;
1674
1775
  }
1675
1776
  if ( TYPE(val) == T_ARRAY ) {
1676
1777
  long i, n = RARRAY_LEN(val);
1677
- volatile VALUE out = rb_ary_new2(n);
1778
+ volatile VALUE out = rb_ary_new_capa(n);
1678
1779
  for (i = 0; i < n; i++) {
1679
1780
  rb_ary_push(out, ca_face_convert_store_rval(self, ca, rb_ary_entry(val, i)));
1680
1781
  }
@@ -1684,7 +1785,7 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1684
1785
  }
1685
1786
 
1686
1787
  static VALUE
1687
- rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1788
+ rb_ca_store_method (int argc, VALUE *argv, volatile VALUE self)
1688
1789
  {
1689
1790
  volatile VALUE obj = Qnil, rval, dest;
1690
1791
  CArray *ca;
@@ -1692,13 +1793,15 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1692
1793
  int converted = 0; /* rval has been through the Face's write hook */
1693
1794
 
1694
1795
  rb_ca_modify(self);
1796
+ rb_reject_options(rb_pop_options(&argc, &argv));
1797
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1695
1798
 
1696
1799
  obj = rval = argv[argc-1];
1697
1800
  argc -= 1;
1698
1801
 
1699
1802
  retry:
1700
1803
 
1701
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1804
+ GetCArray(self, ca);
1702
1805
  rval = ca_face_source_for_store(self, ca, rval);
1703
1806
 
1704
1807
  /* Face store: bring a surface value object (Scalar / Time / DateTime) into
@@ -1721,7 +1824,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1721
1824
  GetCArray(rval, cr);
1722
1825
  if ( ! ca_is_face(cr) && ca_strip_face(ca)->data_type != ca->data_type ) {
1723
1826
  self = rb_ca_strip_face_value(self);
1724
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1827
+ GetCArray(self, ca);
1725
1828
  }
1726
1829
  }
1727
1830
  }
@@ -1740,8 +1843,8 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1740
1843
  supported" message instead of a misleading TypeError. */
1741
1844
  if ( ! ca_argv_is_method_call(argc, argv) && ca_argv_has_slab_sigil(argc, argv) ) {
1742
1845
  rb_raise(rb_eIndexError,
1743
- "assignment through a slab iterator (:>) is not supported "
1744
- "(use a block index, e.g. ca[range, nil] = val, or each_slab)");
1846
+ "%s: assignment through a slab iterator (:>) is not supported "
1847
+ "(use a block index, e.g. ca[range, nil] = val, or each_slab)", ca_calling_method_name());
1745
1848
  }
1746
1849
 
1747
1850
  info.range_check = 1;
@@ -1798,8 +1901,8 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1798
1901
  /* assignment through a slab iterator (:>) is not supported.
1799
1902
  Use a block index (ca[range, nil] = val) or each_slab/map_slab. */
1800
1903
  rb_raise(rb_eIndexError,
1801
- "assignment through a slab iterator (:>) is not supported "
1802
- "(use a block index, e.g. ca[range, nil] = val, or each_slab)");
1904
+ "%s: assignment through a slab iterator (:>) is not supported "
1905
+ "(use a block index, e.g. ca[range, nil] = val, or each_slab)", ca_calling_method_name());
1803
1906
  break;
1804
1907
  }
1805
1908
  case CA_REG_REPEAT: {
@@ -1807,7 +1910,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1807
1910
  obj = rb_ca_store_all(obj, rval);
1808
1911
  break;
1809
1912
  }
1810
- case CA_REG_MAPPING: {
1913
+ case CA_REG_ADDRESS_ARRAY: {
1811
1914
  obj = rb_ca_fancy_index_chain(dest, argv[0]);
1812
1915
  obj = rb_ca_store_all(obj, rval);
1813
1916
  break;
@@ -1822,7 +1925,9 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1822
1925
  store then goes through it, as the read does -- only the cells it
1823
1926
  picks are written. */
1824
1927
  volatile VALUE idx;
1825
- idx = rb_funcallv_public(self, SYM2ID(info.symbol), argc-1, argv+1);
1928
+ /* Ruby allows no keywords in an index assignment, so there are none
1929
+ to pass on. */
1930
+ idx = ca_index_method_answer(self, info.symbol, argc-1, argv+1, 0);
1826
1931
  obj = rb_ca_store(dest, idx, rval);
1827
1932
  break;
1828
1933
  }
@@ -1834,7 +1939,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1834
1939
  }
1835
1940
  else {
1836
1941
  rb_raise(rb_eIndexError,
1837
- "can't store member of carray doesn't have data_class");
1942
+ "%s: a String names a member, and only a struct array has members", ca_calling_method_name());
1838
1943
  }
1839
1944
  break;
1840
1945
  }
@@ -1844,13 +1949,19 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1844
1949
  }
1845
1950
 
1846
1951
  VALUE
1847
- rb_ca_fetch (VALUE self, VALUE index)
1952
+ rb_ca_fetch (VALUE self, volatile VALUE index)
1848
1953
  {
1849
1954
  switch ( TYPE(index) ) {
1850
- case T_ARRAY:
1851
- return rb_ca_fetch_method((int) RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1852
- default:
1853
- return rb_ca_fetch_method(1, &index, self);
1955
+ case T_ARRAY: {
1956
+ /* the fetch reads its arguments out of index */
1957
+ volatile VALUE result =
1958
+ rb_ca_fetch_method((int) RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1959
+ return result;
1960
+ }
1961
+ default: {
1962
+ VALUE args[1] = { index };
1963
+ return rb_ca_fetch_method(1, args, self);
1964
+ }
1854
1965
  }
1855
1966
  }
1856
1967
 
@@ -1861,14 +1972,13 @@ rb_ca_fetch2 (VALUE self, int n, VALUE *rindex)
1861
1972
  }
1862
1973
 
1863
1974
  VALUE
1864
- rb_ca_store (VALUE self, VALUE index, VALUE rval)
1975
+ rb_ca_store (VALUE self, volatile VALUE index, volatile VALUE rval)
1865
1976
  {
1866
1977
  switch ( TYPE(index) ) {
1867
1978
  case T_ARRAY:
1868
1979
  index = rb_obj_clone(index);
1869
1980
  rb_ary_push(index, rval);
1870
1981
  rval = rb_ca_store_method((int)RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1871
- RB_GC_GUARD(index); /* the store reads its arguments out of index */
1872
1982
  return rval;
1873
1983
  default: {
1874
1984
  VALUE rindex[2] = { index, rval };
@@ -1878,17 +1988,17 @@ rb_ca_store (VALUE self, VALUE index, VALUE rval)
1878
1988
  }
1879
1989
 
1880
1990
  VALUE
1881
- rb_ca_store2 (VALUE self, int n, VALUE *rindex, VALUE rval)
1991
+ rb_ca_store2 (VALUE self, int n, VALUE *rindex, volatile VALUE rval)
1882
1992
  {
1883
- volatile VALUE index = rb_ary_new4(n, rindex);
1993
+ volatile VALUE index = rb_ary_new_from_values(n, rindex);
1884
1994
  rb_ary_push(index, rval);
1885
1995
  rval = rb_ca_store_method((int)RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1886
- RB_GC_GUARD(index); /* the store reads its arguments out of index */
1887
1996
  return rval;
1888
1997
  }
1889
1998
 
1999
+ /* recursion: re-enters once, for a flat index */
1890
2000
  static VALUE
1891
- rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
2001
+ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1892
2002
  {
1893
2003
  volatile VALUE rtype, rindex;
1894
2004
  CAIndexInfo info;
@@ -1899,21 +2009,23 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1899
2009
 
1900
2010
  Check_Type(rdim, T_ARRAY);
1901
2011
  Check_Type(ridx, T_ARRAY);
2012
+ /* Converting an entry can run Ruby that changes the Array: read a copy. */
2013
+ ridx = rb_ary_dup(ridx);
1902
2014
 
1903
2015
  ndim = (int) RARRAY_LEN(rdim);
1904
- CA_CHECK_RANK_GIVEN(ndim);
2016
+ CA_CHECK_RANK(ndim);
1905
2017
  for (i=0; i<ndim; i++) {
1906
- dim[i] = NUM2SIZE(rb_ary_entry(rdim, i));
2018
+ dim[i] = ca_integer_arg(rb_ary_entry(rdim, i), "a dimension", NULL);
1907
2019
  }
1908
- CA_CHECK_DIM_GIVEN(ndim, dim);
2020
+ CA_CHECK_DIM(ndim, dim);
1909
2021
  elements = ca_shape_elements(ndim, dim, 1);
1910
2022
 
1911
2023
  info.range_check = 1;
1912
2024
  rb_ca_scan_index(ndim, dim, elements,
1913
2025
  RARRAY_LEN(ridx), (VALUE *)RARRAY_CONST_PTR(ridx), &info);
1914
2026
 
1915
- rtype = INT2NUM(info.type);
1916
- rindex = rb_ary_new2(info.ndim);
2027
+ rtype = ca_region_type_symbol(info.type);
2028
+ rindex = rb_ary_new_capa(info.ndim);
1917
2029
 
1918
2030
  switch ( info.type ) {
1919
2031
  case CA_REG_NONE:
@@ -1930,9 +2042,9 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1930
2042
  for (i=0; i<ndim; i++) {
1931
2043
  elements *= dim[i];
1932
2044
  }
1933
- rinfo = rb_ca_s_scan_index(self, rb_ary_new3(1, SIZE2NUM(elements)), ridx);
1934
- rtype = INT2NUM(CA_REG_ADDRESS_COMPLEX);
1935
- rindex = rb_struct_aref(rinfo, rb_str_new2("index"));
2045
+ rinfo = rb_ca_s_scan_index(self, rb_ary_new_from_args(1, SIZE2NUM(elements)), ridx);
2046
+ rtype = ca_region_type_symbol(CA_REG_ADDRESS_COMPLEX);
2047
+ rindex = rb_struct_aref(rinfo, rb_str_new_cstr("index"));
1936
2048
  break;
1937
2049
  }
1938
2050
  case CA_REG_POINT:
@@ -1951,23 +2063,23 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1951
2063
  break;
1952
2064
  case CA_IDX_ALL:
1953
2065
  rb_ary_store(rindex, i,
1954
- rb_ary_new3(3,
1955
- INT2NUM(0),
1956
- rb_ary_entry(rdim, i),
1957
- INT2NUM(1)));
2066
+ rb_ary_new_from_args(3,
2067
+ INT2NUM(0),
2068
+ rb_ary_entry(rdim, i),
2069
+ INT2NUM(1)));
1958
2070
  break;
1959
2071
  case CA_IDX_BLOCK:
1960
2072
  rb_ary_store(rindex, i,
1961
- rb_ary_new3(3,
1962
- SIZE2NUM(info.index[i].block.start),
1963
- SIZE2NUM(info.index[i].block.count),
1964
- SIZE2NUM(info.index[i].block.step)));
2073
+ rb_ary_new_from_args(3,
2074
+ SIZE2NUM(info.index[i].block.start),
2075
+ SIZE2NUM(info.index[i].block.count),
2076
+ SIZE2NUM(info.index[i].block.step)));
1965
2077
  break;
1966
2078
  case CA_IDX_SYMBOL:
1967
2079
  rb_ary_store(rindex, i,
1968
- rb_ary_new3(2,
1969
- ID2SYM(info.index[i].symbol.id),
1970
- info.index[i].symbol.spec));
2080
+ rb_ary_new_from_args(2,
2081
+ ID2SYM(info.index[i].symbol.id),
2082
+ info.index[i].symbol.spec));
1971
2083
  break;
1972
2084
  default:
1973
2085
  rb_raise(rb_eIndexError, "unknown index spec");
@@ -1976,7 +2088,7 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1976
2088
  break;
1977
2089
  case CA_REG_REPEAT:
1978
2090
  case CA_REG_GRID:
1979
- case CA_REG_MAPPING:
2091
+ case CA_REG_ADDRESS_ARRAY:
1980
2092
  case CA_REG_METHOD_CALL:
1981
2093
  case CA_REG_MEMBER:
1982
2094
  break;
@@ -1987,16 +2099,19 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1987
2099
  return rb_struct_new(S_CAInfo, rtype, rindex);
1988
2100
  }
1989
2101
 
2102
+ /* recursion: re-enters once, with the index rewritten */
1990
2103
  static VALUE
1991
- rb_ca_normalize_index (VALUE self, VALUE ridx)
2104
+ rb_ca_normalize_index (volatile VALUE self, volatile VALUE ridx)
1992
2105
  {
1993
2106
  volatile VALUE rindex;
1994
2107
  CArray *ca;
1995
2108
  CAIndexInfo info;
1996
2109
  int i;
1997
2110
 
1998
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2111
+ GetCArray(self, ca);
1999
2112
  Check_Type(ridx, T_ARRAY);
2113
+ /* Converting an entry can run Ruby that changes the Array: read a copy. */
2114
+ ridx = rb_ary_dup(ridx);
2000
2115
 
2001
2116
  info.range_check = 1;
2002
2117
  rb_ca_scan_index(ca->ndim, ca->dim, ca->elements,
@@ -2006,18 +2121,18 @@ rb_ca_normalize_index (VALUE self, VALUE ridx)
2006
2121
  case CA_REG_ALL:
2007
2122
  case CA_REG_SELECT:
2008
2123
  case CA_REG_ADDRESS:
2009
- rindex = rb_ary_new2(info.ndim);
2124
+ rindex = rb_ary_new_capa(info.ndim);
2010
2125
  rb_ary_store(rindex, 0, SIZE2NUM(info.index[0].scalar));
2011
2126
  return rindex;
2012
2127
  case CA_REG_POINT:
2013
- rindex = rb_ary_new2(info.ndim);
2128
+ rindex = rb_ary_new_capa(info.ndim);
2014
2129
  for (i=0; i<ca->ndim; i++) {
2015
2130
  rb_ary_store(rindex, i, SIZE2NUM(info.index[i].scalar));
2016
2131
  }
2017
2132
  return rindex;
2018
2133
  case CA_REG_BLOCK:
2019
2134
  case CA_REG_ITERATOR:
2020
- rindex = rb_ary_new2(info.ndim);
2135
+ rindex = rb_ary_new_capa(info.ndim);
2021
2136
  for (i=0; i<ca->ndim; i++) {
2022
2137
  switch ( info.index_type[i] ) {
2023
2138
  case CA_IDX_SCALAR:
@@ -2028,10 +2143,10 @@ rb_ca_normalize_index (VALUE self, VALUE ridx)
2028
2143
  break;
2029
2144
  case CA_IDX_BLOCK:
2030
2145
  rb_ary_store(rindex, i,
2031
- rb_ary_new3(3,
2032
- SIZE2NUM(info.index[i].block.start),
2033
- SIZE2NUM(info.index[i].block.count),
2034
- SIZE2NUM(info.index[i].block.step)));
2146
+ rb_ary_new_from_args(3,
2147
+ SIZE2NUM(info.index[i].block.start),
2148
+ SIZE2NUM(info.index[i].block.count),
2149
+ SIZE2NUM(info.index[i].block.step)));
2035
2150
  break;
2036
2151
  case CA_IDX_SYMBOL:
2037
2152
  rb_ary_store(rindex, i, ID2SYM(info.index[i].symbol.id));
@@ -2080,7 +2195,7 @@ parse_shape_kwarg (VALUE rshape, int *out_ndim, ca_size_t *dim,
2080
2195
  }
2081
2196
  ca_check_rank_fits(ndim, NULL);
2082
2197
  for (i = 0; i < ndim; i++) {
2083
- dim[i] = NUM2SIZE(rb_ary_entry(rshape, i));
2198
+ dim[i] = ca_integer_arg(rb_ary_entry(rshape, i), "a dimension", NULL);
2084
2199
  if ( dim[i] < 0 ) {
2085
2200
  rb_raise(rb_eArgError, "negative dim %" PRId64 " at axis %d",
2086
2201
  (ca_size_t) dim[i], i);
@@ -2091,17 +2206,26 @@ parse_shape_kwarg (VALUE rshape, int *out_ndim, ca_size_t *dim,
2091
2206
  *out_elements = elements;
2092
2207
  }
2093
2208
 
2209
+ /* A scalar key of index2addr / addr2index, by the rule of `[]`: a Float that
2210
+ is a whole number is that position, any other is no position at all. */
2094
2211
  static VALUE
2095
- addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2212
+ ca_scalar_position_key (VALUE v)
2213
+ {
2214
+ return RB_FLOAT_TYPE_P(v) ? SIZE2NUM(ca_index_key_value(v)) : v;
2215
+ }
2216
+
2217
+ static VALUE
2218
+ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, volatile VALUE raddr)
2096
2219
  {
2097
2220
  volatile VALUE out;
2098
2221
  int i;
2099
2222
 
2223
+ raddr = ca_scalar_position_key(raddr);
2100
2224
  /* Scalar path (Integer input): returns Ruby Array of N Integers.
2101
2225
  * Preserves legacy shape for callers like `i, j = ca.addr2index(k)`. */
2102
2226
  if ( rb_obj_is_kind_of(raddr, rb_cInteger) ) {
2103
2227
  ca_size_t addr = ca_index_in_range(NUM2SIZE(raddr), elements, "address");
2104
- out = rb_ary_new2(ndim);
2228
+ out = rb_ary_new_capa(ndim);
2105
2229
  for (i = ndim - 1; i >= 0; i--) {
2106
2230
  rb_ary_store(out, i, SIZE2NUM(addr % dim[i]));
2107
2231
  addr /= dim[i];
@@ -2120,12 +2244,12 @@ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2120
2244
 
2121
2245
  ca_check_index_array(raddr);
2122
2246
  cin = ca_wrap_readonly(raddr, CA_SIZE);
2123
- ca_attach(cin); /* window: detached before its one raise */
2247
+ ca_attach(cin); /* attach: detached before its one raise */
2124
2248
 
2125
- out = rb_ary_new2(ndim);
2249
+ out = rb_ary_new_capa(ndim);
2126
2250
  for (i = 0; i < ndim; i++) {
2127
2251
  objs[i] = rb_carray_new(CA_SIZE, cin->ndim, cin->dim, 0, NULL);
2128
- TypedData_Get_Struct(objs[i], CArray, &carray_data_type, co[i]);
2252
+ GetCArray(objs[i], co[i]);
2129
2253
  p_out[i] = (ca_size_t *) co[i]->ptr;
2130
2254
  rb_ary_store(out, i, objs[i]);
2131
2255
  }
@@ -2162,13 +2286,12 @@ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2162
2286
  }
2163
2287
 
2164
2288
  ca_detach(cin);
2165
- RB_GC_GUARD(raddr); /* the CA_SIZE view is reached only through cin */
2166
2289
  return out;
2167
2290
  }
2168
2291
  }
2169
2292
 
2170
- /* The vector path of index2addr reads every index array inside one
2171
- window: an index out of range raises part way through. */
2293
+ /* The vector path of index2addr reads every index array while they are
2294
+ attached, and an index out of range raises part way through. */
2172
2295
 
2173
2296
  typedef struct {
2174
2297
  int ndim;
@@ -2199,7 +2322,7 @@ ca_index2addr_body (VALUE arg)
2199
2322
  }
2200
2323
 
2201
2324
  obj = rb_carray_new(CA_SIZE, c->out_ndim, c->out_dim, 0, NULL);
2202
- TypedData_Get_Struct(obj, CArray, &carray_data_type, co);
2325
+ GetCArray(obj, co);
2203
2326
 
2204
2327
  q = (ca_size_t *) co->ptr;
2205
2328
 
@@ -2253,12 +2376,18 @@ index2addr_do (int ndim, ca_size_t *dim, int argc, VALUE *argv)
2253
2376
  ca_size_t out_dim[CA_RANK_MAX];
2254
2377
  ca_size_t out_elements = 1;
2255
2378
  int shape_from = -1;
2379
+ VALUE keys[CA_RANK_MAX];
2256
2380
 
2257
2381
  if ( argc != ndim ) {
2258
2382
  rb_raise(rb_eArgError,
2259
2383
  "wrong number of indices (%d for %d)", argc, ndim);
2260
2384
  }
2261
2385
 
2386
+ for (i = 0; i < ndim; i++) {
2387
+ keys[i] = ca_scalar_position_key(argv[i]);
2388
+ }
2389
+ argv = keys;
2390
+
2262
2391
  for (i = 0; i < ndim; i++) {
2263
2392
  if ( ! rb_obj_is_kind_of(argv[i], rb_cInteger) ) {
2264
2393
  all_number = 0;
@@ -2326,18 +2455,18 @@ index2addr_do (int ndim, ca_size_t *dim, int argc, VALUE *argv)
2326
2455
  }
2327
2456
 
2328
2457
  VALUE
2329
- rb_ca_addr2index (VALUE self, VALUE raddr)
2458
+ rb_ca_addr2index (volatile VALUE self, VALUE raddr)
2330
2459
  {
2331
2460
  CArray *ca;
2332
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2461
+ GetCArray(self, ca);
2333
2462
  return addr2index_do(ca->ndim, ca->dim, ca->elements, raddr);
2334
2463
  }
2335
2464
 
2336
2465
  VALUE
2337
- rb_ca_index2addr (int argc, VALUE *argv, VALUE self)
2466
+ rb_ca_index2addr (int argc, VALUE *argv, volatile VALUE self)
2338
2467
  {
2339
2468
  CArray *ca;
2340
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2469
+ GetCArray(self, ca);
2341
2470
  return index2addr_do(ca->ndim, ca->dim, argc, argv);
2342
2471
  }
2343
2472
 
@@ -2366,7 +2495,8 @@ rb_ca_s_addr2index (int argc, VALUE *argv, VALUE klass)
2366
2495
  static VALUE
2367
2496
  rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2368
2497
  {
2369
- VALUE rest, opts, out;
2498
+ VALUE rest_arg, opts;
2499
+ volatile VALUE rest, out;
2370
2500
  ID kw_ids[1];
2371
2501
  VALUE kw_vals[1];
2372
2502
  int ndim;
@@ -2374,7 +2504,8 @@ rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2374
2504
  ca_size_t elements;
2375
2505
 
2376
2506
  kw_ids[0] = rb_intern("shape");
2377
- rb_scan_args(argc, argv, "*:", &rest, &opts);
2507
+ rb_scan_args(argc, argv, "*:", &rest_arg, &opts);
2508
+ rest = rest_arg;
2378
2509
  if ( NIL_P(opts) ) {
2379
2510
  rb_raise(rb_eArgError, "missing keyword: shape");
2380
2511
  }
@@ -2382,7 +2513,6 @@ rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2382
2513
  parse_shape_kwarg(kw_vals[0], &ndim, dim, &elements);
2383
2514
  /* index2addr_do reads, and replaces, its arguments in rest */
2384
2515
  out = index2addr_do(ndim, dim, (int) RARRAY_LEN(rest), RARRAY_PTR(rest));
2385
- RB_GC_GUARD(rest);
2386
2516
  return out;
2387
2517
  }
2388
2518
 
@@ -2410,7 +2540,7 @@ Init_carray_access (void)
2410
2540
  rb_define_method(rb_cCArray, "fill", rb_ca_fill, 1);
2411
2541
  rb_define_method(rb_cCArray, "fill_copy", rb_ca_fill_copy, 1);
2412
2542
 
2413
- S_CAInfo = rb_struct_define("CAIndexInfo", "type", "index", NULL);
2543
+ S_CAInfo = rb_struct_define_under(rb_cCArray, "IndexInfo", "type", "index", NULL);
2414
2544
 
2415
2545
  rb_define_singleton_method(rb_cCArray, "guess_array_shape",
2416
2546
  rb_ca_s_guess_array_shape, 1);
@@ -2423,23 +2553,6 @@ Init_carray_access (void)
2423
2553
  rb_define_singleton_method(rb_cCArray, "addr2index", rb_ca_s_addr2index, -1);
2424
2554
  rb_define_singleton_method(rb_cCArray, "index2addr", rb_ca_s_index2addr, -1);
2425
2555
 
2426
- rb_define_const(rb_cObject, "CA_REG_NONE", INT2NUM(CA_REG_NONE));
2427
- rb_define_const(rb_cObject, "CA_REG_ALL", INT2NUM(CA_REG_ALL));
2428
- rb_define_const(rb_cObject, "CA_REG_ADDRESS", INT2NUM(CA_REG_ADDRESS));
2429
- rb_define_const(rb_cObject, "CA_REG_FLATTEN", INT2NUM(CA_REG_FLATTEN));
2430
- rb_define_const(rb_cObject, "CA_REG_ADDRESS_COMPLEX",
2431
- INT2NUM(CA_REG_ADDRESS_COMPLEX));
2432
- rb_define_const(rb_cObject, "CA_REG_POINT", INT2NUM(CA_REG_POINT));
2433
- rb_define_const(rb_cObject, "CA_REG_BLOCK", INT2NUM(CA_REG_BLOCK));
2434
- rb_define_const(rb_cObject, "CA_REG_SELECT", INT2NUM(CA_REG_SELECT));
2435
- rb_define_const(rb_cObject, "CA_REG_ITERATOR", INT2NUM(CA_REG_ITERATOR));
2436
- rb_define_const(rb_cObject, "CA_REG_REPEAT", INT2NUM(CA_REG_REPEAT));
2437
- rb_define_const(rb_cObject, "CA_REG_GRID", INT2NUM(CA_REG_GRID));
2438
- rb_define_const(rb_cObject, "CA_REG_MAPPING", INT2NUM(CA_REG_MAPPING));
2439
- rb_define_const(rb_cObject, "CA_REG_METHOD_CALL",
2440
- INT2NUM(CA_REG_METHOD_CALL));
2441
- rb_define_const(rb_cObject, "CA_REG_MEMBER", INT2NUM(CA_REG_MEMBER));
2442
- rb_define_const(rb_cObject, "CA_REG_ATTRIBUTE", INT2NUM(CA_REG_ATTRIBUTE));
2443
2556
 
2444
2557
  }
2445
2558