carray 3.0.7 → 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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  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 +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  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 -1506
  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 +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  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 +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  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 +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  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 +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  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 +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  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 +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  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 +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  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 +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  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 +5 -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 +72 -71
  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/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. 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;
@@ -60,11 +59,11 @@ rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
60
59
  }
61
60
 
62
61
  VALUE
63
- rb_ca_fetch_index (VALUE self, ca_size_t *idx)
62
+ rb_ca_fetch_index (volatile VALUE self, ca_size_t *idx)
64
63
  {
65
64
  volatile VALUE out;
66
65
  CArray *ca;
67
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
66
+ GetCArray(self, ca);
68
67
 
69
68
  if ( ca_is_empty(ca) ) {
70
69
  return Qnil;
@@ -106,13 +105,13 @@ rb_ca_fetch_index (VALUE self, ca_size_t *idx)
106
105
  }
107
106
 
108
107
  VALUE
109
- 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)
110
109
  {
111
110
  CArray *ca;
112
111
  boolean8_t zero = 0, one = 1;
113
112
 
114
113
  rb_ca_modify(self);
115
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
114
+ GetCArray(self, ca);
116
115
 
117
116
  if ( ca_is_empty(ca) ) {
118
117
  return rval;
@@ -143,11 +142,11 @@ rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE 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 ) {
@@ -430,7 +431,7 @@ rb_ca_ref_address (VALUE self, CAIndexInfo *info)
430
431
  object array holds any value, the array itself included, so only a
431
432
  CScalar is opened there. */
432
433
  static VALUE
433
- ca_cell_of_store_value (VALUE self, VALUE rval)
434
+ ca_cell_of_store_value (volatile VALUE self, volatile VALUE rval)
434
435
  {
435
436
  CArray *ca, *cv;
436
437
  if ( rb_obj_is_cscalar(rval) ) {
@@ -439,11 +440,11 @@ ca_cell_of_store_value (VALUE self, VALUE rval)
439
440
  if ( ! rb_obj_is_carray(rval) ) {
440
441
  return rval;
441
442
  }
442
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
443
+ GetCArray(self, ca);
443
444
  if ( ca->data_type == CA_OBJECT ) {
444
445
  return rval;
445
446
  }
446
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
447
+ GetCArray(rval, cv);
447
448
  if ( cv->elements != 1 ) {
448
449
  rb_raise(rb_eArgError,
449
450
  "%s: shape mismatch writing to one cell (<- %" PRIi64 " cells)",
@@ -463,12 +464,12 @@ rb_ca_store_address (VALUE self, CAIndexInfo *info, volatile VALUE rval)
463
464
  }
464
465
 
465
466
  static VALUE
466
- rb_ca_ref_point (VALUE self, CAIndexInfo *info)
467
+ rb_ca_ref_point (volatile VALUE self, CAIndexInfo *info)
467
468
  {
468
469
  CArray *ca;
469
470
  ca_size_t idx[CA_RANK_MAX];
470
471
  int8_t i;
471
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
472
+ GetCArray(self, ca);
472
473
  for (i=0; i<ca->ndim; i++) {
473
474
  idx[i] = info->index[i].scalar;
474
475
  }
@@ -476,12 +477,12 @@ rb_ca_ref_point (VALUE self, CAIndexInfo *info)
476
477
  }
477
478
 
478
479
  static VALUE
479
- rb_ca_store_point (VALUE self, CAIndexInfo *info, volatile VALUE val)
480
+ rb_ca_store_point (volatile VALUE self, CAIndexInfo *info, volatile VALUE val)
480
481
  {
481
482
  CArray *ca;
482
483
  ca_size_t idx[CA_RANK_MAX];
483
484
  int8_t i;
484
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
485
+ GetCArray(self, ca);
485
486
  for (i=0; i<ca->ndim; i++) {
486
487
  idx[i] = info->index[i].scalar;
487
488
  }
@@ -504,9 +505,9 @@ ca_id_expression_evaluator (void)
504
505
  return id;
505
506
  }
506
507
 
507
- /* The casting stores below convert into the destination inside a window,
508
- and a conversion from Ruby objects can raise part way. Such a window
509
- 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
510
511
  are then pushed back with the others left as they were -- what an
511
512
  entity destination, which has no separate buffer, shows as well. A
512
513
  cast that cannot raise keeps the cheaper allocate. */
@@ -527,7 +528,7 @@ ca_store_all_mode (CArray *cv)
527
528
  return ( cv->data_type == CA_OBJECT ) ? "w" : "a";
528
529
  }
529
530
 
530
- /* 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,
531
532
  so the mask is written in its buffer. */
532
533
  static VALUE
533
534
  ca_store_all_cast_body (VALUE arg)
@@ -558,7 +559,7 @@ ca_store_all_list_body (VALUE arg)
558
559
  {
559
560
  ca_store_all_ctx_t *c = (ca_store_all_ctx_t *) arg;
560
561
  CArray *ca = c->ca;
561
- VALUE list = c->list;
562
+ volatile VALUE list = c->list;
562
563
  ca_size_t i;
563
564
  /* Face has surface != storage; the cast runs in the storage
564
565
  data_type. Passing ca directly makes the cast table see FIXLEN
@@ -613,7 +614,7 @@ ca_store_all_list_body (VALUE arg)
613
614
  class, or one that reconciles a Face (to_comparable), is left to
614
615
  ca_face_convert_store_rval. */
615
616
  static VALUE
616
- ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
617
+ ca_face_source_for_store (VALUE self, CArray *ca, volatile VALUE rval)
617
618
  {
618
619
  CArray *cr;
619
620
 
@@ -634,17 +635,17 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
634
635
  }
635
636
  if ( cr->obj_type == CA_OBJ_RECORD ) {
636
637
  rb_raise(rb_eTypeError,
637
- "can not store a record array of %"PRIsVALUE
638
- " 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(),
639
640
  rb_ca_data_class(rval), rb_ca_data_class(self));
640
641
  }
641
642
  rb_raise(rb_eTypeError,
642
- "can not store a %"PRIsVALUE" array into a record array of %"PRIsVALUE,
643
+ "%s: can not store a %"PRIsVALUE" array into a record array of %"PRIsVALUE, ca_calling_method_name(),
643
644
  rb_obj_class(rval), rb_ca_data_class(self));
644
645
  }
645
646
  if ( ca_is_face(ca) ) {
646
647
  if ( rb_obj_class(self) == rb_obj_class(rval)
647
- || rb_respond_to(self, rb_intern("to_comparable")) ) {
648
+ || rb_obj_respond_to(self, rb_intern("to_comparable"), Qtrue) ) {
648
649
  return rval;
649
650
  }
650
651
  }
@@ -659,11 +660,12 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
659
660
  return rval;
660
661
  }
661
662
  rb_raise(rb_eTypeError,
662
- "can not store a %"PRIsVALUE" array into %s array "
663
- "(to_type(:object) gives its values)",
663
+ "%s: can not store a %"PRIsVALUE" array into %s array "
664
+ "(to_type(:object) gives its values)", ca_calling_method_name(),
664
665
  rb_obj_class(rval), ca_type_name[ca->data_type]);
665
666
  }
666
- 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);
667
669
  }
668
670
 
669
671
  /* A store into no cells writes nothing, but the value is checked as it
@@ -684,10 +686,10 @@ ca_refuse_complex_into_real (CArray *ca, CArray *cv)
684
686
  }
685
687
 
686
688
  static void
687
- ca_store_all_check_empty (VALUE self, VALUE rval)
689
+ ca_store_all_check_empty (volatile VALUE self, volatile VALUE rval)
688
690
  {
689
691
  CArray *ca;
690
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
692
+ GetCArray(self, ca);
691
693
  if ( rb_obj_is_cscalar(rval) ) {
692
694
  rval = rb_ca_fetch_addr(rval, 0);
693
695
  }
@@ -699,7 +701,7 @@ ca_store_all_check_empty (VALUE self, VALUE rval)
699
701
  if ( rb_obj_is_carray(rval) ) {
700
702
  CArray *cv;
701
703
  ca_broadcast_to_destination(self, (volatile VALUE *) &rval);
702
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
704
+ GetCArray(rval, cv);
703
705
  ca_refuse_complex_into_real(ca, cv);
704
706
  }
705
707
  else if ( TYPE(rval) == T_ARRAY ) {
@@ -716,7 +718,7 @@ ca_store_all_check_empty (VALUE self, VALUE rval)
716
718
  }
717
719
 
718
720
  VALUE
719
- rb_ca_store_all (VALUE self, VALUE rval)
721
+ rb_ca_store_all (volatile VALUE self, volatile VALUE rval)
720
722
  {
721
723
  CArray *ca;
722
724
 
@@ -734,14 +736,14 @@ rb_ca_store_all (VALUE self, VALUE rval)
734
736
  rval = rb_ca_fetch_addr(rval, 0);
735
737
  }
736
738
 
737
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
739
+ GetCArray(self, ca);
738
740
  rval = ca_face_source_for_store(self, ca, rval);
739
741
 
740
742
  retry:
741
743
 
742
744
  if ( rb_obj_is_carray(rval) ) {
743
745
  CArray *cv;
744
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
746
+ GetCArray(rval, cv);
745
747
 
746
748
  /* Where something is registered to compute an expression, and this is
747
749
  one, it is asked before the walk below -- it fills the destination
@@ -771,7 +773,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
771
773
 
772
774
  /* The destination owns the shape; see ca_broadcast_to_destination. */
773
775
  ca_broadcast_to_destination(self, &rval);
774
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
776
+ GetCArray(rval, cv);
775
777
  ca_refuse_complex_into_real(ca, cv);
776
778
 
777
779
  /* Fixlen cells of another width are fitted cell by cell, padded with
@@ -782,7 +784,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
782
784
  && ca->bytes != cv->bytes ) {
783
785
  rval = rb_ca_to_type(rb_ca_strip_face_value(rval), INT2NUM(CA_FIXLEN),
784
786
  SIZE2NUM(ca->bytes));
785
- TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
787
+ GetCArray(rval, cv);
786
788
  }
787
789
 
788
790
  /* Source delivery via ca_xfer_all into a local scratch instead of
@@ -848,6 +850,9 @@ rb_ca_store_all (VALUE self, VALUE rval)
848
850
  volatile VALUE list =
849
851
  rb_ary_flatten_for_elements(rval, ca->elements, ca);
850
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
+ }
851
856
  if ( NIL_P(list) ) {
852
857
  rb_raise(rb_eArgError,
853
858
  "failed to guess data size of given array");
@@ -870,7 +875,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
870
875
  old values -- but converting a Ruby object can raise part way, and
871
876
  then the cells already converted are pushed back with the rest
872
877
  left as they were, which needs the old values in the buffer. So
873
- this window attaches (see ca_store_all_mode). */
878
+ this store attaches (see ca_store_all_mode). */
874
879
  {
875
880
  ca_store_all_ctx_t c;
876
881
  c.ca = ca;
@@ -985,7 +990,7 @@ rb_ca_index_restruct_block (int16_t *ndimp, ca_size_t *shrink, ca_size_t *dim,
985
990
  }
986
991
 
987
992
  VALUE
988
- rb_ca_ref_block (VALUE self, CAIndexInfo *info)
993
+ rb_ca_ref_block (volatile VALUE self, CAIndexInfo *info)
989
994
  {
990
995
  volatile VALUE refer;
991
996
  CArray *ca;
@@ -999,7 +1004,7 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
999
1004
  ca_size_t flag = 0;
1000
1005
  ca_size_t i;
1001
1006
 
1002
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1007
+ GetCArray(self, ca);
1003
1008
 
1004
1009
  ndim = info->ndim;
1005
1010
 
@@ -1055,7 +1060,7 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
1055
1060
  casts in the storage type. */
1056
1061
  if ( ca_is_face(ca) ) {
1057
1062
  CArray *blk_ca, *root = ca_strip_face(ca);
1058
- TypedData_Get_Struct(blk, CArray, &carray_data_type, blk_ca);
1063
+ GetCArray(blk, blk_ca);
1059
1064
  if (root && root->data_type != blk_ca->data_type) {
1060
1065
  blk_ca->data_type = root->data_type;
1061
1066
  }
@@ -1065,12 +1070,12 @@ rb_ca_ref_block (VALUE self, CAIndexInfo *info)
1065
1070
  }
1066
1071
 
1067
1072
  static VALUE
1068
- rb_ca_refer_new_flatten (VALUE self)
1073
+ rb_ca_refer_new_flatten (volatile VALUE self)
1069
1074
  {
1070
1075
  CArray *ca;
1071
1076
  ca_size_t dim0;
1072
1077
 
1073
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1078
+ GetCArray(self, ca);
1074
1079
  dim0 = ca->elements;
1075
1080
  return rb_ca_refer_new(self, ca->data_type, 1, &dim0, ca->bytes, 0);
1076
1081
  }
@@ -1092,16 +1097,17 @@ extern VALUE rb_ca_remap_new (VALUE cary, VALUE rmapper);
1092
1097
  A masked mapper cell selects nothing: the cell keeps its position and
1093
1098
  reads as masked (a CAGrid hole); the same-shape fast path is left to a
1094
1099
  mapper with no masked cell. */
1100
+ /* recursion: re-enters once, with the Face taken off */
1095
1101
  static VALUE
1096
- rb_ca_fancy_index_chain (VALUE self, volatile VALUE rmapper)
1102
+ rb_ca_fancy_index_chain (volatile VALUE self, volatile VALUE rmapper)
1097
1103
  {
1098
1104
  CArray *ca, *cm;
1099
1105
  volatile VALUE flat_self, flat_mapper, gridded, result;
1100
1106
  VALUE grid_argv[1];
1101
1107
  int i;
1102
1108
 
1103
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1104
- TypedData_Get_Struct(rmapper, CArray, &carray_data_type, cm);
1109
+ GetCArray(self, ca);
1110
+ GetCArray(rmapper, cm);
1105
1111
 
1106
1112
  /* (a) same-shape fast path: ndim + per-axis dim + CA_SIZE data_type.
1107
1113
  The view reads the index array it is given, so it is given a copy: the
@@ -1115,7 +1121,7 @@ rb_ca_fancy_index_chain (VALUE self, volatile VALUE rmapper)
1115
1121
  volatile VALUE snapshot = rb_ca_copy(rmapper);
1116
1122
  CArray *cs;
1117
1123
  ca_size_t k, *p;
1118
- TypedData_Get_Struct(snapshot, CArray, &carray_data_type, cs);
1124
+ GetCArray(snapshot, cs);
1119
1125
  p = (ca_size_t *) cs->ptr;
1120
1126
  for (k = 0; k < cs->elements; k++) {
1121
1127
  CA_CHECK_INDEX(p[k], ca->elements);
@@ -1160,11 +1166,56 @@ rb_ca_mapping_new (VALUE cary, CArray *mapper)
1160
1166
  return rb_ca_fancy_index_chain(cary, rmapper);
1161
1167
  }
1162
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
+
1163
1214
  VALUE
1164
1215
  rb_ca_mapping (int argc, VALUE *argv, VALUE self)
1165
1216
  {
1166
- volatile VALUE rmapper;
1167
- rb_scan_args(argc, argv, "1", (VALUE *) &rmapper);
1217
+ VALUE rmapper;
1218
+ rb_scan_args(argc, argv, "1", &rmapper);
1168
1219
  rb_check_carray_object(rmapper);
1169
1220
  return rb_ca_fancy_index_chain(self, rmapper);
1170
1221
  }
@@ -1214,12 +1265,13 @@ static VALUE rb_ca_slab_iter_new (VALUE self, CAIndexInfo *info);
1214
1265
  static VALUE rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self);
1215
1266
 
1216
1267
  /* axis-group apply (ext/ca_group_iter.c): type gate that routes a
1217
- CACategorical/AxisGroup index to the group path. */
1268
+ CACategorical/CAAxisGroup index to the group path. */
1218
1269
  int ca_argv_has_group (int argc, VALUE *argv);
1219
1270
  VALUE rb_ca_fetch_group (int argc, VALUE *argv, VALUE self);
1220
1271
 
1272
+ /* recursion: re-enters once, on the flattened array */
1221
1273
  static VALUE
1222
- rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1274
+ rb_ca_fetch_method (int argc, VALUE *argv, volatile VALUE self)
1223
1275
  {
1224
1276
  volatile VALUE obj = Qnil;
1225
1277
  CArray *ca;
@@ -1227,12 +1279,14 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1227
1279
 
1228
1280
  retry:
1229
1281
 
1230
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1282
+ GetCArray(self, ca);
1231
1283
 
1232
1284
  if ( ca_argv_is_method_call(argc, argv) ) {
1233
- goto classify;
1285
+ goto classify; /* the keywords go on to the method */
1234
1286
  }
1235
1287
 
1288
+ rb_reject_options(rb_pop_options(&argc, &argv));
1289
+
1236
1290
  /* newaxis (:_) interception (cheap pointer scan; zero overhead beyond
1237
1291
  the loop when no :_ is present). */
1238
1292
  if ( ca_argv_has_newaxis(argc, argv) ) {
@@ -1241,13 +1295,13 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1241
1295
 
1242
1296
  /* slab sigil (:>) interception. Pre-strip :> -> nil and Integer ->
1243
1297
  length-1 Range so mask / fancy outer slots route through the regular
1244
- dispatch (CA_REG_SELECT / CA_REG_GRID / CA_REG_MAPPING) before being
1245
- wrapped in CASlabIterator. */
1298
+ dispatch (CA_REG_SELECT / CA_REG_GRID / CA_REG_ADDRESS_ARRAY) before
1299
+ being wrapped in CASlabIterator. */
1246
1300
  if ( ca_argv_has_slab_sigil(argc, argv) ) {
1247
1301
  return rb_ca_fetch_slab_sigil(argc, argv, self);
1248
1302
  }
1249
1303
 
1250
- /* Axis-group type gate: a CACategorical/AxisGroup index routes to the
1304
+ /* Axis-group type gate: a CACategorical/CAAxisGroup index routes to the
1251
1305
  group apply path (CAGroupIterator); plain int-array indices fall
1252
1306
  through to the regular selection dispatch below. The scan short-circuits
1253
1307
  when the surface classes have not been loaded, so a normal index pays
@@ -1309,17 +1363,17 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1309
1363
  case CA_REG_REPEAT:
1310
1364
  obj = rb_ca_repeat(argc, argv, self);
1311
1365
  break;
1312
- case CA_REG_MAPPING:
1366
+ case CA_REG_ADDRESS_ARRAY:
1313
1367
  obj = rb_ca_fancy_index_chain(self, argv[0]);
1314
1368
  break;
1315
1369
  case CA_REG_GRID:
1316
1370
  obj = rb_ca_grid(argc, argv, self);
1317
1371
  break;
1318
1372
  case CA_REG_METHOD_CALL: {
1319
- /* a[:eq, 0] is a[a.eq(0)]: a public method of the array, as a call
1320
- 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 */
1321
1374
  volatile VALUE idx;
1322
- 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());
1323
1377
  obj = rb_ca_fetch(self, idx);
1324
1378
  break;
1325
1379
  }
@@ -1334,12 +1388,12 @@ rb_ca_fetch_method (int argc, VALUE *argv, VALUE self)
1334
1388
  }
1335
1389
  else {
1336
1390
  rb_raise(rb_eIndexError,
1337
- "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());
1338
1392
  }
1339
1393
  break;
1340
1394
  }
1341
1395
  default:
1342
- rb_raise(rb_eIndexError, "invalid index specified");
1396
+ rb_raise(rb_eIndexError, "%s: invalid index specified", ca_calling_method_name());
1343
1397
  }
1344
1398
 
1345
1399
  /* Wrapper lift at the read touch point: if `self` is a Face or a
@@ -1387,11 +1441,11 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1387
1441
  if ( a == sym_gt || a == sym_perc
1388
1442
  || a == Qfalse || a == sym_tilde ) {
1389
1443
  rb_raise(rb_eIndexError,
1390
- "newaxis (:_) cannot be combined with :>, :%%, "
1391
- "or rubber dim (false / :~)");
1444
+ "%s: newaxis (:_) cannot be combined with :>, :%%, "
1445
+ "or rubber dim (false / :~)", ca_calling_method_name());
1392
1446
  }
1393
1447
  if ( nclean >= CA_RANK_MAX ) { /* rank-check: a count of indices */
1394
- rb_raise(rb_eIndexError, "too many indices");
1448
+ rb_raise(rb_eIndexError, "%s: too many indices", ca_calling_method_name());
1395
1449
  }
1396
1450
  clean[nclean++] = a;
1397
1451
  }
@@ -1400,8 +1454,8 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1400
1454
  for partial indices, which is incoherent with axis-position newaxis). */
1401
1455
  if ( nclean != ca->ndim ) {
1402
1456
  rb_raise(rb_eIndexError,
1403
- "newaxis (:_) requires full per-axis indexing "
1404
- "(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(),
1405
1459
  nclean, (int) ca->ndim);
1406
1460
  }
1407
1461
 
@@ -1415,7 +1469,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1415
1469
  VALUE blocks[CA_RANK_MAX];
1416
1470
  ca_check_rank_fits(nunder, NULL); /* the result has the :_ axes only */
1417
1471
  for (i = 0; i < nclean; i++) {
1418
- blocks[i] = rb_assoc_new(clean[i], INT2FIX(1));
1472
+ blocks[i] = rb_range_new(clean[i], clean[i], 0);
1419
1473
  }
1420
1474
  base = rb_ca_fetch_method(nclean, blocks, self);
1421
1475
  for (i = 0; i < nunder; i++) {
@@ -1426,7 +1480,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1426
1480
 
1427
1481
  base = rb_ca_fetch_method(nclean, clean, self);
1428
1482
 
1429
- TypedData_Get_Struct(base, CArray, &carray_data_type, cb);
1483
+ GetCArray(base, cb);
1430
1484
 
1431
1485
  /* The result has the base's axes and one for each :_, before any is
1432
1486
  written into final_dims. */
@@ -1470,7 +1524,7 @@ rb_ca_fetch_newaxis (int argc, VALUE *argv, VALUE self, CArray *ca)
1470
1524
  }
1471
1525
  }
1472
1526
  if ( base_cursor != cb->ndim ) {
1473
- rb_raise(rb_eIndexError, "newaxis: base axis count mismatch");
1527
+ rb_raise(rb_eIndexError, "%s: newaxis: base axis count mismatch", ca_calling_method_name());
1474
1528
  }
1475
1529
 
1476
1530
  for (i = 0; i < nfinal; i++) {
@@ -1513,7 +1567,7 @@ rb_ca_slab_iter_new (VALUE self, CAIndexInfo *info)
1513
1567
  reference (CABlock / CASelectAxis / CAGrid / ...), then
1514
1568
  wrap in CASlabIterator with the recorded slab_axes positions. */
1515
1569
  static VALUE
1516
- rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1570
+ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, volatile VALUE self)
1517
1571
  {
1518
1572
  VALUE expanded[CA_RANK_MAX];
1519
1573
  VALUE clean[CA_RANK_MAX];
@@ -1526,7 +1580,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1526
1580
  VALUE *eargv;
1527
1581
  int rubber_pos = -1;
1528
1582
 
1529
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1583
+ GetCArray(self, ca);
1530
1584
 
1531
1585
  /* Rubber-dim expansion (mirrors ca_classifier_expand_rubber_dim).
1532
1586
  Only the first rubber marker is honoured; later ones are left for
@@ -1545,7 +1599,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1545
1599
  int rndim;
1546
1600
  if ( argc > ca->ndim + 1 ) {
1547
1601
  rb_raise(rb_eIndexError,
1548
- "index specification exceeds the ndim of carray (%i)",
1602
+ "%s: index specification exceeds the ndim of carray (%i)", ca_calling_method_name(),
1549
1603
  ca->ndim);
1550
1604
  }
1551
1605
  rndim = ca->ndim - argc + 1;
@@ -1565,7 +1619,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1565
1619
  }
1566
1620
 
1567
1621
  if ( eargc > CA_RANK_MAX ) { /* rank-check: a count of indices */
1568
- rb_raise(rb_eIndexError, "too many indices");
1622
+ rb_raise(rb_eIndexError, "%s: too many indices", ca_calling_method_name());
1569
1623
  }
1570
1624
 
1571
1625
  for (i = 0; i < eargc; i++) {
@@ -1588,7 +1642,7 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1588
1642
  base = rb_ca_fetch_method(eargc, clean, self);
1589
1643
  if ( ! rb_obj_is_kind_of(base, rb_cCArray) ) {
1590
1644
  rb_raise(rb_eIndexError,
1591
- "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());
1592
1646
  }
1593
1647
  klass = rb_const_get(rb_cObject, rb_intern("CASlabIterator"));
1594
1648
  return rb_funcall(klass, rb_intern("new"), 2, base, slab_axes);
@@ -1642,15 +1696,17 @@ rb_cs_store_method (int argc, VALUE *argv, VALUE self)
1642
1696
  integer-storage Face takes integer cells only. The scalar path already
1643
1697
  refuses a bare Float there (to_comparable does not recognise it) instead
1644
1698
  of letting the storage cast truncate it, and the bulk path must not be
1645
- the looser door.
1699
+ the looser door. An object array is not storage but values, and goes
1700
+ in as an Array does.
1646
1701
  - an Array is mapped structure-preserving (leaves converted),
1647
1702
  - a scalar goes through the write hook (ca_face_element_to_storage), which
1648
1703
  leaves a bare Integer / String unchanged.
1649
1704
 
1650
1705
  Idempotent: an already-converted Integer passes through, so re-running on
1651
1706
  a `goto retry` iteration is harmless. */
1707
+ /* recursion: re-enters once, with the value converted */
1652
1708
  static VALUE
1653
- ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1709
+ ca_face_convert_store_rval (VALUE self, CArray *ca, volatile VALUE val)
1654
1710
  {
1655
1711
  if ( rb_obj_is_carray(val) ) {
1656
1712
  CArray *cval;
@@ -1679,12 +1735,22 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1679
1735
  return ca_face_convert_store_rval(self, ca,
1680
1736
  rb_funcall(val, rb_intern("to_a"), 0));
1681
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
+ }
1682
1748
  /* Face RHS: reconcile to self's unit and descend to storage. Mirror of
1683
1749
  the scalar path (ca_face_element_to_storage) for a bulk CArray source.
1684
1750
  Guarded on self having to_comparable so a Face without a reconcile
1685
1751
  algebra (e.g. a fixlen-storage Face, whose same-storage store already
1686
1752
  takes the storage xfer branch) is left to the existing store path. */
1687
- 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) ) {
1688
1754
  return ca_face_reconcile_operand(self, val, "[]=");
1689
1755
  }
1690
1756
  /* A Face of the same class and a compatible state holds values of the
@@ -1693,28 +1759,23 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1693
1759
  && ca_face_state_compatible(self, ca, val, cval) ) {
1694
1760
  return rb_ca_strip_face_value(val);
1695
1761
  }
1696
- /* 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). */
1697
1767
  {
1698
1768
  CArray *storage = ca_strip_face(ca);
1699
- if ( storage && ca_is_integer_type(storage) && ! ca_is_integer_type(cval) ) {
1700
- const char *method = ca_face_storage_method(self);
1701
- if ( method ) {
1702
- rb_raise(rb_eTypeError,
1703
- "%"PRIsVALUE" cannot store a bare %s array as raw storage "
1704
- "(write the %s storage, which #%s gives, directly)",
1705
- rb_obj_class(self), ca_type_name[cval->data_type],
1706
- ca_type_name[storage->data_type], method);
1707
- }
1708
- rb_raise(rb_eTypeError,
1709
- "%"PRIsVALUE" cannot store a bare %s array as raw storage",
1710
- rb_obj_class(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));
1711
1772
  }
1712
1773
  }
1713
1774
  return val;
1714
1775
  }
1715
1776
  if ( TYPE(val) == T_ARRAY ) {
1716
1777
  long i, n = RARRAY_LEN(val);
1717
- volatile VALUE out = rb_ary_new2(n);
1778
+ volatile VALUE out = rb_ary_new_capa(n);
1718
1779
  for (i = 0; i < n; i++) {
1719
1780
  rb_ary_push(out, ca_face_convert_store_rval(self, ca, rb_ary_entry(val, i)));
1720
1781
  }
@@ -1724,7 +1785,7 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1724
1785
  }
1725
1786
 
1726
1787
  static VALUE
1727
- rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1788
+ rb_ca_store_method (int argc, VALUE *argv, volatile VALUE self)
1728
1789
  {
1729
1790
  volatile VALUE obj = Qnil, rval, dest;
1730
1791
  CArray *ca;
@@ -1732,13 +1793,15 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1732
1793
  int converted = 0; /* rval has been through the Face's write hook */
1733
1794
 
1734
1795
  rb_ca_modify(self);
1796
+ rb_reject_options(rb_pop_options(&argc, &argv));
1797
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1735
1798
 
1736
1799
  obj = rval = argv[argc-1];
1737
1800
  argc -= 1;
1738
1801
 
1739
1802
  retry:
1740
1803
 
1741
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1804
+ GetCArray(self, ca);
1742
1805
  rval = ca_face_source_for_store(self, ca, rval);
1743
1806
 
1744
1807
  /* Face store: bring a surface value object (Scalar / Time / DateTime) into
@@ -1761,7 +1824,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1761
1824
  GetCArray(rval, cr);
1762
1825
  if ( ! ca_is_face(cr) && ca_strip_face(ca)->data_type != ca->data_type ) {
1763
1826
  self = rb_ca_strip_face_value(self);
1764
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1827
+ GetCArray(self, ca);
1765
1828
  }
1766
1829
  }
1767
1830
  }
@@ -1780,8 +1843,8 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1780
1843
  supported" message instead of a misleading TypeError. */
1781
1844
  if ( ! ca_argv_is_method_call(argc, argv) && ca_argv_has_slab_sigil(argc, argv) ) {
1782
1845
  rb_raise(rb_eIndexError,
1783
- "assignment through a slab iterator (:>) is not supported "
1784
- "(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());
1785
1848
  }
1786
1849
 
1787
1850
  info.range_check = 1;
@@ -1838,8 +1901,8 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1838
1901
  /* assignment through a slab iterator (:>) is not supported.
1839
1902
  Use a block index (ca[range, nil] = val) or each_slab/map_slab. */
1840
1903
  rb_raise(rb_eIndexError,
1841
- "assignment through a slab iterator (:>) is not supported "
1842
- "(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());
1843
1906
  break;
1844
1907
  }
1845
1908
  case CA_REG_REPEAT: {
@@ -1847,7 +1910,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1847
1910
  obj = rb_ca_store_all(obj, rval);
1848
1911
  break;
1849
1912
  }
1850
- case CA_REG_MAPPING: {
1913
+ case CA_REG_ADDRESS_ARRAY: {
1851
1914
  obj = rb_ca_fancy_index_chain(dest, argv[0]);
1852
1915
  obj = rb_ca_store_all(obj, rval);
1853
1916
  break;
@@ -1862,7 +1925,9 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1862
1925
  store then goes through it, as the read does -- only the cells it
1863
1926
  picks are written. */
1864
1927
  volatile VALUE idx;
1865
- 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);
1866
1931
  obj = rb_ca_store(dest, idx, rval);
1867
1932
  break;
1868
1933
  }
@@ -1874,7 +1939,7 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1874
1939
  }
1875
1940
  else {
1876
1941
  rb_raise(rb_eIndexError,
1877
- "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());
1878
1943
  }
1879
1944
  break;
1880
1945
  }
@@ -1884,13 +1949,19 @@ rb_ca_store_method (int argc, VALUE *argv, VALUE self)
1884
1949
  }
1885
1950
 
1886
1951
  VALUE
1887
- rb_ca_fetch (VALUE self, VALUE index)
1952
+ rb_ca_fetch (VALUE self, volatile VALUE index)
1888
1953
  {
1889
1954
  switch ( TYPE(index) ) {
1890
- case T_ARRAY:
1891
- return rb_ca_fetch_method((int) RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1892
- default:
1893
- 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
+ }
1894
1965
  }
1895
1966
  }
1896
1967
 
@@ -1901,14 +1972,13 @@ rb_ca_fetch2 (VALUE self, int n, VALUE *rindex)
1901
1972
  }
1902
1973
 
1903
1974
  VALUE
1904
- rb_ca_store (VALUE self, VALUE index, VALUE rval)
1975
+ rb_ca_store (VALUE self, volatile VALUE index, volatile VALUE rval)
1905
1976
  {
1906
1977
  switch ( TYPE(index) ) {
1907
1978
  case T_ARRAY:
1908
1979
  index = rb_obj_clone(index);
1909
1980
  rb_ary_push(index, rval);
1910
1981
  rval = rb_ca_store_method((int)RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1911
- RB_GC_GUARD(index); /* the store reads its arguments out of index */
1912
1982
  return rval;
1913
1983
  default: {
1914
1984
  VALUE rindex[2] = { index, rval };
@@ -1918,15 +1988,15 @@ rb_ca_store (VALUE self, VALUE index, VALUE rval)
1918
1988
  }
1919
1989
 
1920
1990
  VALUE
1921
- rb_ca_store2 (VALUE self, int n, VALUE *rindex, VALUE rval)
1991
+ rb_ca_store2 (VALUE self, int n, VALUE *rindex, volatile VALUE rval)
1922
1992
  {
1923
- volatile VALUE index = rb_ary_new4(n, rindex);
1993
+ volatile VALUE index = rb_ary_new_from_values(n, rindex);
1924
1994
  rb_ary_push(index, rval);
1925
1995
  rval = rb_ca_store_method((int)RARRAY_LEN(index), (VALUE *)RARRAY_CONST_PTR(index), self);
1926
- RB_GC_GUARD(index); /* the store reads its arguments out of index */
1927
1996
  return rval;
1928
1997
  }
1929
1998
 
1999
+ /* recursion: re-enters once, for a flat index */
1930
2000
  static VALUE
1931
2001
  rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1932
2002
  {
@@ -1943,19 +2013,19 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1943
2013
  ridx = rb_ary_dup(ridx);
1944
2014
 
1945
2015
  ndim = (int) RARRAY_LEN(rdim);
1946
- CA_CHECK_RANK_GIVEN(ndim);
2016
+ CA_CHECK_RANK(ndim);
1947
2017
  for (i=0; i<ndim; i++) {
1948
2018
  dim[i] = ca_integer_arg(rb_ary_entry(rdim, i), "a dimension", NULL);
1949
2019
  }
1950
- CA_CHECK_DIM_GIVEN(ndim, dim);
2020
+ CA_CHECK_DIM(ndim, dim);
1951
2021
  elements = ca_shape_elements(ndim, dim, 1);
1952
2022
 
1953
2023
  info.range_check = 1;
1954
2024
  rb_ca_scan_index(ndim, dim, elements,
1955
2025
  RARRAY_LEN(ridx), (VALUE *)RARRAY_CONST_PTR(ridx), &info);
1956
2026
 
1957
- rtype = INT2NUM(info.type);
1958
- rindex = rb_ary_new2(info.ndim);
2027
+ rtype = ca_region_type_symbol(info.type);
2028
+ rindex = rb_ary_new_capa(info.ndim);
1959
2029
 
1960
2030
  switch ( info.type ) {
1961
2031
  case CA_REG_NONE:
@@ -1972,9 +2042,9 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1972
2042
  for (i=0; i<ndim; i++) {
1973
2043
  elements *= dim[i];
1974
2044
  }
1975
- rinfo = rb_ca_s_scan_index(self, rb_ary_new3(1, SIZE2NUM(elements)), ridx);
1976
- rtype = INT2NUM(CA_REG_ADDRESS_COMPLEX);
1977
- 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"));
1978
2048
  break;
1979
2049
  }
1980
2050
  case CA_REG_POINT:
@@ -1993,23 +2063,23 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1993
2063
  break;
1994
2064
  case CA_IDX_ALL:
1995
2065
  rb_ary_store(rindex, i,
1996
- rb_ary_new3(3,
1997
- INT2NUM(0),
1998
- rb_ary_entry(rdim, i),
1999
- INT2NUM(1)));
2066
+ rb_ary_new_from_args(3,
2067
+ INT2NUM(0),
2068
+ rb_ary_entry(rdim, i),
2069
+ INT2NUM(1)));
2000
2070
  break;
2001
2071
  case CA_IDX_BLOCK:
2002
2072
  rb_ary_store(rindex, i,
2003
- rb_ary_new3(3,
2004
- SIZE2NUM(info.index[i].block.start),
2005
- SIZE2NUM(info.index[i].block.count),
2006
- 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)));
2007
2077
  break;
2008
2078
  case CA_IDX_SYMBOL:
2009
2079
  rb_ary_store(rindex, i,
2010
- rb_ary_new3(2,
2011
- ID2SYM(info.index[i].symbol.id),
2012
- 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));
2013
2083
  break;
2014
2084
  default:
2015
2085
  rb_raise(rb_eIndexError, "unknown index spec");
@@ -2018,7 +2088,7 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
2018
2088
  break;
2019
2089
  case CA_REG_REPEAT:
2020
2090
  case CA_REG_GRID:
2021
- case CA_REG_MAPPING:
2091
+ case CA_REG_ADDRESS_ARRAY:
2022
2092
  case CA_REG_METHOD_CALL:
2023
2093
  case CA_REG_MEMBER:
2024
2094
  break;
@@ -2029,15 +2099,16 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
2029
2099
  return rb_struct_new(S_CAInfo, rtype, rindex);
2030
2100
  }
2031
2101
 
2102
+ /* recursion: re-enters once, with the index rewritten */
2032
2103
  static VALUE
2033
- rb_ca_normalize_index (VALUE self, volatile VALUE ridx)
2104
+ rb_ca_normalize_index (volatile VALUE self, volatile VALUE ridx)
2034
2105
  {
2035
2106
  volatile VALUE rindex;
2036
2107
  CArray *ca;
2037
2108
  CAIndexInfo info;
2038
2109
  int i;
2039
2110
 
2040
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2111
+ GetCArray(self, ca);
2041
2112
  Check_Type(ridx, T_ARRAY);
2042
2113
  /* Converting an entry can run Ruby that changes the Array: read a copy. */
2043
2114
  ridx = rb_ary_dup(ridx);
@@ -2050,18 +2121,18 @@ rb_ca_normalize_index (VALUE self, volatile VALUE ridx)
2050
2121
  case CA_REG_ALL:
2051
2122
  case CA_REG_SELECT:
2052
2123
  case CA_REG_ADDRESS:
2053
- rindex = rb_ary_new2(info.ndim);
2124
+ rindex = rb_ary_new_capa(info.ndim);
2054
2125
  rb_ary_store(rindex, 0, SIZE2NUM(info.index[0].scalar));
2055
2126
  return rindex;
2056
2127
  case CA_REG_POINT:
2057
- rindex = rb_ary_new2(info.ndim);
2128
+ rindex = rb_ary_new_capa(info.ndim);
2058
2129
  for (i=0; i<ca->ndim; i++) {
2059
2130
  rb_ary_store(rindex, i, SIZE2NUM(info.index[i].scalar));
2060
2131
  }
2061
2132
  return rindex;
2062
2133
  case CA_REG_BLOCK:
2063
2134
  case CA_REG_ITERATOR:
2064
- rindex = rb_ary_new2(info.ndim);
2135
+ rindex = rb_ary_new_capa(info.ndim);
2065
2136
  for (i=0; i<ca->ndim; i++) {
2066
2137
  switch ( info.index_type[i] ) {
2067
2138
  case CA_IDX_SCALAR:
@@ -2072,10 +2143,10 @@ rb_ca_normalize_index (VALUE self, volatile VALUE ridx)
2072
2143
  break;
2073
2144
  case CA_IDX_BLOCK:
2074
2145
  rb_ary_store(rindex, i,
2075
- rb_ary_new3(3,
2076
- SIZE2NUM(info.index[i].block.start),
2077
- SIZE2NUM(info.index[i].block.count),
2078
- 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)));
2079
2150
  break;
2080
2151
  case CA_IDX_SYMBOL:
2081
2152
  rb_ary_store(rindex, i, ID2SYM(info.index[i].symbol.id));
@@ -2144,7 +2215,7 @@ ca_scalar_position_key (VALUE v)
2144
2215
  }
2145
2216
 
2146
2217
  static VALUE
2147
- addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2218
+ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, volatile VALUE raddr)
2148
2219
  {
2149
2220
  volatile VALUE out;
2150
2221
  int i;
@@ -2154,7 +2225,7 @@ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2154
2225
  * Preserves legacy shape for callers like `i, j = ca.addr2index(k)`. */
2155
2226
  if ( rb_obj_is_kind_of(raddr, rb_cInteger) ) {
2156
2227
  ca_size_t addr = ca_index_in_range(NUM2SIZE(raddr), elements, "address");
2157
- out = rb_ary_new2(ndim);
2228
+ out = rb_ary_new_capa(ndim);
2158
2229
  for (i = ndim - 1; i >= 0; i--) {
2159
2230
  rb_ary_store(out, i, SIZE2NUM(addr % dim[i]));
2160
2231
  addr /= dim[i];
@@ -2173,12 +2244,12 @@ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2173
2244
 
2174
2245
  ca_check_index_array(raddr);
2175
2246
  cin = ca_wrap_readonly(raddr, CA_SIZE);
2176
- ca_attach(cin); /* window: detached before its one raise */
2247
+ ca_attach(cin); /* attach: detached before its one raise */
2177
2248
 
2178
- out = rb_ary_new2(ndim);
2249
+ out = rb_ary_new_capa(ndim);
2179
2250
  for (i = 0; i < ndim; i++) {
2180
2251
  objs[i] = rb_carray_new(CA_SIZE, cin->ndim, cin->dim, 0, NULL);
2181
- TypedData_Get_Struct(objs[i], CArray, &carray_data_type, co[i]);
2252
+ GetCArray(objs[i], co[i]);
2182
2253
  p_out[i] = (ca_size_t *) co[i]->ptr;
2183
2254
  rb_ary_store(out, i, objs[i]);
2184
2255
  }
@@ -2215,13 +2286,12 @@ addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2215
2286
  }
2216
2287
 
2217
2288
  ca_detach(cin);
2218
- RB_GC_GUARD(raddr); /* the CA_SIZE view is reached only through cin */
2219
2289
  return out;
2220
2290
  }
2221
2291
  }
2222
2292
 
2223
- /* The vector path of index2addr reads every index array inside one
2224
- 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. */
2225
2295
 
2226
2296
  typedef struct {
2227
2297
  int ndim;
@@ -2252,7 +2322,7 @@ ca_index2addr_body (VALUE arg)
2252
2322
  }
2253
2323
 
2254
2324
  obj = rb_carray_new(CA_SIZE, c->out_ndim, c->out_dim, 0, NULL);
2255
- TypedData_Get_Struct(obj, CArray, &carray_data_type, co);
2325
+ GetCArray(obj, co);
2256
2326
 
2257
2327
  q = (ca_size_t *) co->ptr;
2258
2328
 
@@ -2385,18 +2455,18 @@ index2addr_do (int ndim, ca_size_t *dim, int argc, VALUE *argv)
2385
2455
  }
2386
2456
 
2387
2457
  VALUE
2388
- rb_ca_addr2index (VALUE self, VALUE raddr)
2458
+ rb_ca_addr2index (volatile VALUE self, VALUE raddr)
2389
2459
  {
2390
2460
  CArray *ca;
2391
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2461
+ GetCArray(self, ca);
2392
2462
  return addr2index_do(ca->ndim, ca->dim, ca->elements, raddr);
2393
2463
  }
2394
2464
 
2395
2465
  VALUE
2396
- rb_ca_index2addr (int argc, VALUE *argv, VALUE self)
2466
+ rb_ca_index2addr (int argc, VALUE *argv, volatile VALUE self)
2397
2467
  {
2398
2468
  CArray *ca;
2399
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2469
+ GetCArray(self, ca);
2400
2470
  return index2addr_do(ca->ndim, ca->dim, argc, argv);
2401
2471
  }
2402
2472
 
@@ -2425,7 +2495,8 @@ rb_ca_s_addr2index (int argc, VALUE *argv, VALUE klass)
2425
2495
  static VALUE
2426
2496
  rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2427
2497
  {
2428
- VALUE rest, opts, out;
2498
+ VALUE rest_arg, opts;
2499
+ volatile VALUE rest, out;
2429
2500
  ID kw_ids[1];
2430
2501
  VALUE kw_vals[1];
2431
2502
  int ndim;
@@ -2433,7 +2504,8 @@ rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2433
2504
  ca_size_t elements;
2434
2505
 
2435
2506
  kw_ids[0] = rb_intern("shape");
2436
- rb_scan_args(argc, argv, "*:", &rest, &opts);
2507
+ rb_scan_args(argc, argv, "*:", &rest_arg, &opts);
2508
+ rest = rest_arg;
2437
2509
  if ( NIL_P(opts) ) {
2438
2510
  rb_raise(rb_eArgError, "missing keyword: shape");
2439
2511
  }
@@ -2441,7 +2513,6 @@ rb_ca_s_index2addr (int argc, VALUE *argv, VALUE klass)
2441
2513
  parse_shape_kwarg(kw_vals[0], &ndim, dim, &elements);
2442
2514
  /* index2addr_do reads, and replaces, its arguments in rest */
2443
2515
  out = index2addr_do(ndim, dim, (int) RARRAY_LEN(rest), RARRAY_PTR(rest));
2444
- RB_GC_GUARD(rest);
2445
2516
  return out;
2446
2517
  }
2447
2518
 
@@ -2469,7 +2540,7 @@ Init_carray_access (void)
2469
2540
  rb_define_method(rb_cCArray, "fill", rb_ca_fill, 1);
2470
2541
  rb_define_method(rb_cCArray, "fill_copy", rb_ca_fill_copy, 1);
2471
2542
 
2472
- S_CAInfo = rb_struct_define("CAIndexInfo", "type", "index", NULL);
2543
+ S_CAInfo = rb_struct_define_under(rb_cCArray, "IndexInfo", "type", "index", NULL);
2473
2544
 
2474
2545
  rb_define_singleton_method(rb_cCArray, "guess_array_shape",
2475
2546
  rb_ca_s_guess_array_shape, 1);
@@ -2482,23 +2553,6 @@ Init_carray_access (void)
2482
2553
  rb_define_singleton_method(rb_cCArray, "addr2index", rb_ca_s_addr2index, -1);
2483
2554
  rb_define_singleton_method(rb_cCArray, "index2addr", rb_ca_s_index2addr, -1);
2484
2555
 
2485
- rb_define_const(rb_cObject, "CA_REG_NONE", INT2NUM(CA_REG_NONE));
2486
- rb_define_const(rb_cObject, "CA_REG_ALL", INT2NUM(CA_REG_ALL));
2487
- rb_define_const(rb_cObject, "CA_REG_ADDRESS", INT2NUM(CA_REG_ADDRESS));
2488
- rb_define_const(rb_cObject, "CA_REG_FLATTEN", INT2NUM(CA_REG_FLATTEN));
2489
- rb_define_const(rb_cObject, "CA_REG_ADDRESS_COMPLEX",
2490
- INT2NUM(CA_REG_ADDRESS_COMPLEX));
2491
- rb_define_const(rb_cObject, "CA_REG_POINT", INT2NUM(CA_REG_POINT));
2492
- rb_define_const(rb_cObject, "CA_REG_BLOCK", INT2NUM(CA_REG_BLOCK));
2493
- rb_define_const(rb_cObject, "CA_REG_SELECT", INT2NUM(CA_REG_SELECT));
2494
- rb_define_const(rb_cObject, "CA_REG_ITERATOR", INT2NUM(CA_REG_ITERATOR));
2495
- rb_define_const(rb_cObject, "CA_REG_REPEAT", INT2NUM(CA_REG_REPEAT));
2496
- rb_define_const(rb_cObject, "CA_REG_GRID", INT2NUM(CA_REG_GRID));
2497
- rb_define_const(rb_cObject, "CA_REG_MAPPING", INT2NUM(CA_REG_MAPPING));
2498
- rb_define_const(rb_cObject, "CA_REG_METHOD_CALL",
2499
- INT2NUM(CA_REG_METHOD_CALL));
2500
- rb_define_const(rb_cObject, "CA_REG_MEMBER", INT2NUM(CA_REG_MEMBER));
2501
- rb_define_const(rb_cObject, "CA_REG_ATTRIBUTE", INT2NUM(CA_REG_ATTRIBUTE));
2502
2556
 
2503
2557
  }
2504
2558