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_count.c CHANGED
@@ -12,7 +12,6 @@
12
12
 
13
13
  #include "ruby.h"
14
14
  #include "carray.h"
15
- #include "carray_internal.h" /* ca_attach_window */
16
15
  #include "ca_obj_face.h"
17
16
  #include <math.h>
18
17
  #include <float.h>
@@ -82,7 +81,7 @@ ca_count_each_body (VALUE arg)
82
81
  /* Extract vk as a scalar VALUE via flat addr. rb_ca_fetch_addr
83
82
  takes a ca_size_t addr (not a Ruby VALUE). */
84
83
  VALUE vk = rb_ca_fetch_addr(c->rval, k);
85
- VALUE sub;
84
+ volatile VALUE sub;
86
85
  /* A masked query cell asks nothing: its count is masked. Passing it
87
86
  on would ask count(UNDEF), the count of self's masked cells. */
88
87
  if ( vk == CA_UNDEF ) {
@@ -110,7 +109,7 @@ ca_count_each_body (VALUE arg)
110
109
  } else if ( rb_obj_is_kind_of(sub, rb_cCArray) ) {
111
110
  CArray *csub;
112
111
  GetCArray(sub, csub);
113
- ca_attach(csub); /* window: the inner count answered an entity; nothing raises inside */
112
+ ca_attach(csub); /* attach: the inner count answered an entity; nothing raises before the detach */
114
113
  int64_t *sub_ptr = (int64_t *) csub->ptr;
115
114
  boolean8_t *sub_mask = (csub->mask) ? (boolean8_t *) csub->mask->ptr : NULL;
116
115
  for (ca_size_t i = 0; i < base_elements; i++) {
@@ -132,15 +131,16 @@ ca_count_each_body (VALUE arg)
132
131
  return Qnil;
133
132
  }
134
133
 
134
+ /* recursion: re-enters once or twice, with the arguments rewritten */
135
135
  static VALUE
136
- rb_ca_count (int argc, VALUE *argv, VALUE self)
136
+ rb_ca_count (int argc, VALUE *argv, volatile VALUE self)
137
137
  {
138
138
  CArray *src;
139
139
  GetCArray(self, src);
140
140
 
141
141
  /* count (no value argument) == count_not_masked: present-cell
142
- cardinality ("how many are there"), the arity-0 rung of the dispatch
143
- ladder. kwargs reach a -1 C method as a trailing options hash, so a
142
+ cardinality ("how many are there"), the arity-0 case of dispatch by
143
+ arity. kwargs reach a -1 C method as a trailing options hash, so a
144
144
  bare `count(axis: k)` arrives as argc==1 with argv[0] a Hash; discount
145
145
  that trailing hash (same convention as the CArray-broadcast branch
146
146
  below) before deciding there is no positional value. A Hash passed
@@ -154,7 +154,14 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
154
154
  }
155
155
  }
156
156
 
157
- volatile VALUE rval = argv[0];
157
+ /* true and false count the cells holding 1 and 0 of a numeric array */
158
+ volatile VALUE rval = ca_truth_as_number(src, argv[0]);
159
+ if ( rval != argv[0] ) {
160
+ VALUE *nargv = ALLOCA_N(VALUE, argc);
161
+ MEMCPY(nargv, argv, VALUE, argc);
162
+ nargv[0] = rval;
163
+ argv = nargv;
164
+ }
158
165
 
159
166
  /* count(UNDEF) == count_masked: UNDEF is mask-state vocabulary, not a
160
167
  value to compare against. Forward, dropping the UNDEF from argv. */
@@ -162,6 +169,28 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
162
169
  return rb_ca_count_masked(argc - 1, argv + 1, self);
163
170
  }
164
171
 
172
+ /* A Ruby Array is not one value to count (a CArray v counts cell by
173
+ cell); an object array holds Arrays and counts them as values. */
174
+ if ( RB_TYPE_P(rval, T_ARRAY) && ! ca_is_object_type(src) &&
175
+ ! ca_is_fixlen_type(src) && ! ca_is_face(src) ) {
176
+ ca_refuse_array_value(ca_is_boolean_type(src) ? "a boolean" :
177
+ ca_is_integer_type(src) ? "an integer" :
178
+ ca_is_complex_type(src) ? "a complex" : "a float");
179
+ }
180
+
181
+ /* An array that can not hold the value (nil, a String) counts no cell
182
+ for it: the cells that matched, none, are counted the way count(true)
183
+ counts a mask. A complex array, which has no count kernel, counts the
184
+ cells eq finds the same way. */
185
+ if ( ( ca_is_complex_type(src) && ! rb_obj_is_carray(rval) ) ||
186
+ ca_query_matches_nothing(self, src, rval) ) {
187
+ VALUE *nargv = ALLOCA_N(VALUE, argc);
188
+ MEMCPY(nargv, argv, VALUE, argc);
189
+ nargv[0] = Qtrue;
190
+ return rb_ca_count(argc, nargv,
191
+ rb_funcall(self, rb_intern("eq"), 1, rval));
192
+ }
193
+
165
194
  /* Face gate, mirroring the search / linear family (ext/carray_order.c):
166
195
  descend an ORDERABLE Face to its storage so the equality runs on the
167
196
  numeric storage, and let the reference reconcile the query into its own
@@ -255,7 +284,7 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
255
284
  int keep_axis = 0;
256
285
  if ( ! NIL_P(opt_hash) ) {
257
286
  axis_val = rb_hash_lookup2(opt_hash, ID2SYM(rb_intern("axis")), Qnil);
258
- keep_axis = RTEST(rb_hash_lookup2(opt_hash, ID2SYM(rb_intern("keep_axis")), Qfalse));
287
+ keep_axis = ca_bool_arg(rb_hash_lookup2(opt_hash, ID2SYM(rb_intern("keep_axis")), Qfalse), "keep_axis");
259
288
  }
260
289
  naxes = rb_ca_parse_reduce_axes_kw(axis_val, src, slab_axes);
261
290
  (void) inner_axes; /* axes no longer parsed from inner_axes */
@@ -284,13 +313,13 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
284
313
  }
285
314
 
286
315
  /* Allocate output i64 CArray. */
287
- VALUE vout = rb_funcall(rb_cCArray, rb_intern("new"), 2,
288
- INT2NUM(CA_INT64),
289
- rb_ary_new4(out_ndim,
290
- ({ VALUE *tmp = ALLOCA_N(VALUE, out_ndim);
291
- for (int k = 0; k < out_ndim; k++)
292
- tmp[k] = SIZE2NUM(out_dim[k]);
293
- tmp; })));
316
+ volatile VALUE vout = rb_funcall(rb_cCArray, rb_intern("new"), 2,
317
+ ID2SYM(ca_data_type_sym[CA_INT64]),
318
+ rb_ary_new_from_values(out_ndim,
319
+ ({ VALUE *tmp = ALLOCA_N(VALUE, out_ndim);
320
+ for (int k = 0; k < out_ndim; k++)
321
+ tmp[k] = SIZE2NUM(out_dim[k]);
322
+ tmp; })));
294
323
  CArray *cout;
295
324
  GetCArray(vout, cout);
296
325
 
@@ -309,7 +338,8 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
309
338
 
310
339
  /* Iterate v in flat order, call self.count(vk, *axes, **opts)
311
340
  recursively, place result at output[..., k]. v is held attached
312
- for the fetches while Ruby runs, so the loop is a window body. */
341
+ for the fetches while Ruby runs, so the loop runs through
342
+ ca_attach_window. */
313
343
  {
314
344
  ca_count_each_ctx_t c;
315
345
  c.self = self;
@@ -348,16 +378,17 @@ rb_ca_count (int argc, VALUE *argv, VALUE self)
348
378
  iv);
349
379
  } else {
350
380
  rb_raise(rb_eTypeError,
351
- "count(v) on boolean array: v must be true / false / 1 / 0, got %s",
352
- rb_obj_classname(rval));
381
+ "count(v) on boolean array: v must be true / false / 1 / 0, got %"PRIsVALUE"",
382
+ rb_obj_class(rval));
353
383
  }
354
384
  } else {
355
- /* true / false are the boolean array's own domain, not a number --
356
- except in an object array, where they are ordinary stored values. */
385
+ /* An integer, float or complex array has read true / false as 1 / 0
386
+ above (ca_truth_as_number); an object array stores them as values.
387
+ Any other array that reaches here can not count them. */
357
388
  if ( ( rval == Qtrue || rval == Qfalse ) && src->data_type != CA_OBJECT ) {
358
389
  rb_raise(rb_eTypeError,
359
- "count(v) on numeric array: v must be numeric, got %s",
360
- rb_obj_classname(rval));
390
+ "count(v) on numeric array: v must be numeric, got %"PRIsVALUE"",
391
+ rb_obj_class(rval));
361
392
  }
362
393
  /* The query is compared in the type it and the array match in, as `eq`
363
394
  compares (ca_query_match_type): 1.5 does not match 1, and an Integer
data/ext/carray_element.c CHANGED
@@ -18,7 +18,6 @@
18
18
  #include "ruby.h"
19
19
  #include "carray.h"
20
20
  #include "ca_obj_face.h"
21
- #include "carray_internal.h" /* ca_obj_extremum_replaces */
22
21
 
23
22
  /* An element index is read as `[]` reads a key: an Integer, or a Float
24
23
  with no fraction. */
@@ -38,22 +37,28 @@ elem_index_value (VALUE v)
38
37
  }
39
38
  }
40
39
 
40
+ ca_size_t
41
+ ca_index_key_value (VALUE v)
42
+ {
43
+ return elem_index_value(v);
44
+ }
45
+
41
46
  /* T_ARRAY index parser. ridx is assumed already known to be T_ARRAY.
42
47
  Direct VALUE * access + FIXNUM_P fast path eliminates rb_ary_entry
43
48
  bounds check and NUM2SIZE Bignum branching for the common case. */
44
49
  static inline void
45
50
  elem_parse_idx_array (CArray *ca, VALUE rary, ca_size_t *idx)
46
51
  {
47
- const VALUE *iarr;
48
52
  int8_t ndim = ca->ndim;
49
53
  int8_t i;
50
54
  if ( RARRAY_LEN(rary) != ndim ) {
51
- rb_raise(rb_eIndexError, "%ld indices given for a carray of ndim %i",
52
- (long) RARRAY_LEN(rary), (int) ndim);
55
+ rb_raise(rb_eIndexError, "%s: %ld indices given for a carray of ndim %i",
56
+ ca_calling_method_name(), (long) RARRAY_LEN(rary), (int) ndim);
53
57
  }
54
- iarr = RARRAY_CONST_PTR(rary);
55
58
  for (i=0; i<ndim; i++) {
56
- ca_size_t k = elem_index_value(iarr[i]);
59
+ /* Read each entry afresh: converting one can run Ruby that changes the
60
+ Array (an entry gone reads as nil, which is refused). */
61
+ ca_size_t k = elem_index_value(rb_ary_entry(rary, i));
57
62
  CA_CHECK_INDEX(k, ca->dim[i]);
58
63
  idx[i] = k;
59
64
  }
@@ -103,7 +108,7 @@ elem_entity_addr (CArray *ca, ca_size_t *idx)
103
108
  * a flat Integer address or an Array<Integer> of per-axis indices. */
104
109
 
105
110
  VALUE
106
- rb_ca_elem_swap (VALUE self, VALUE ridx1, VALUE ridx2)
111
+ rb_ca_elem_swap (volatile VALUE self, VALUE ridx1, VALUE ridx2)
107
112
  {
108
113
  CArray *ca;
109
114
  ca_size_t idx1[CA_RANK_MAX], idx2[CA_RANK_MAX];
@@ -115,7 +120,7 @@ rb_ca_elem_swap (VALUE self, VALUE ridx1, VALUE ridx2)
115
120
 
116
121
  rb_ca_modify(self);
117
122
 
118
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
123
+ GetCArray(self, ca);
119
124
 
120
125
  has_mask = elem_probe_mask(ca);
121
126
 
@@ -192,7 +197,7 @@ rb_ca_elem_swap (VALUE self, VALUE ridx1, VALUE ridx2)
192
197
  * `idx1` into the cell at `idx2`; source cell is left unchanged. */
193
198
 
194
199
  VALUE
195
- rb_ca_elem_copy (VALUE self, VALUE ridx1, VALUE ridx2)
200
+ rb_ca_elem_copy (volatile VALUE self, VALUE ridx1, VALUE ridx2)
196
201
  {
197
202
  CArray *ca;
198
203
  ca_size_t idx1[CA_RANK_MAX], idx2[CA_RANK_MAX];
@@ -204,7 +209,7 @@ rb_ca_elem_copy (VALUE self, VALUE ridx1, VALUE ridx2)
204
209
 
205
210
  rb_ca_modify(self);
206
211
 
207
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
212
+ GetCArray(self, ca);
208
213
 
209
214
  has_mask = elem_probe_mask(ca);
210
215
 
@@ -250,7 +255,7 @@ rb_ca_elem_copy (VALUE self, VALUE ridx1, VALUE ridx2)
250
255
  * masks the cell. */
251
256
 
252
257
  VALUE
253
- rb_ca_elem_store (VALUE self, VALUE ridx, VALUE obj)
258
+ rb_ca_elem_store (volatile VALUE self, VALUE ridx, VALUE obj)
254
259
  {
255
260
  CArray *ca;
256
261
  ca_size_t idx[CA_RANK_MAX];
@@ -259,7 +264,7 @@ rb_ca_elem_store (VALUE self, VALUE ridx, VALUE obj)
259
264
 
260
265
  rb_ca_modify(self);
261
266
 
262
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
267
+ GetCArray(self, ca);
263
268
 
264
269
  if ( TYPE(ridx) == T_ARRAY ) {
265
270
  elem_parse_idx_array(ca, ridx, idx);
@@ -370,12 +375,12 @@ rb_ca_elem_store (VALUE self, VALUE ridx, VALUE obj)
370
375
  * `self` is empty. */
371
376
 
372
377
  VALUE
373
- rb_ca_elem_fetch (VALUE self, VALUE ridx)
378
+ rb_ca_elem_fetch (volatile VALUE self, VALUE ridx)
374
379
  {
375
380
  CArray *ca;
376
381
  ca_size_t idx[CA_RANK_MAX];
377
382
 
378
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
383
+ GetCArray(self, ca);
379
384
 
380
385
  if ( TYPE(ridx) == T_ARRAY ) {
381
386
  volatile VALUE out;
@@ -473,7 +478,7 @@ rb_ca_elem_fetch (VALUE self, VALUE ridx)
473
478
  * `nil`). Raises for non-integer data_type. */
474
479
 
475
480
  VALUE
476
- rb_ca_elem_incr (VALUE self, VALUE ridx1)
481
+ rb_ca_elem_incr (volatile VALUE self, VALUE ridx1)
477
482
  {
478
483
  volatile VALUE out;
479
484
  CArray *ca;
@@ -486,7 +491,7 @@ rb_ca_elem_incr (VALUE self, VALUE ridx1)
486
491
 
487
492
  rb_ca_modify(self);
488
493
 
489
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
494
+ GetCArray(self, ca);
490
495
 
491
496
  if ( ! ca_is_integer_type(ca) ) {
492
497
  rb_raise(rb_eCADataTypeError,
@@ -564,7 +569,7 @@ rb_ca_elem_incr (VALUE self, VALUE ridx1)
564
569
  * `nil`). Raises for non-integer data_type. */
565
570
 
566
571
  VALUE
567
- rb_ca_elem_decr (VALUE self, VALUE ridx1)
572
+ rb_ca_elem_decr (volatile VALUE self, VALUE ridx1)
568
573
  {
569
574
  volatile VALUE out;
570
575
  CArray *ca;
@@ -577,7 +582,7 @@ rb_ca_elem_decr (VALUE self, VALUE ridx1)
577
582
 
578
583
  rb_ca_modify(self);
579
584
 
580
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
585
+ GetCArray(self, ca);
581
586
 
582
587
  if ( ! ca_is_integer_type(ca) ) {
583
588
  rb_raise(rb_eCADataTypeError,
@@ -652,13 +657,13 @@ rb_ca_elem_decr (VALUE self, VALUE ridx1)
652
657
  * masked. */
653
658
 
654
659
  VALUE
655
- rb_ca_elem_test_masked (VALUE self, VALUE ridx1)
660
+ rb_ca_elem_test_masked (volatile VALUE self, VALUE ridx1)
656
661
  {
657
662
  CArray *ca;
658
663
  ca_size_t idx1[CA_RANK_MAX];
659
664
  boolean8_t m = 0;
660
665
 
661
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
666
+ GetCArray(self, ca);
662
667
 
663
668
  /* Super-hot path: entity. Covers both no-mask (Qfalse fast) and masked
664
669
  (direct ca->mask->ptr read) cases without dispatch. Mask itself is
@@ -701,14 +706,14 @@ rb_ca_elem_test_masked (VALUE self, VALUE ridx1)
701
706
  * later `elem_unmask(idx)` restores it. */
702
707
 
703
708
  VALUE
704
- rb_ca_elem_mask (VALUE self, VALUE ridx)
709
+ rb_ca_elem_mask (volatile VALUE self, VALUE ridx)
705
710
  {
706
711
  CArray *ca;
707
712
  ca_size_t idx[CA_RANK_MAX];
708
713
 
709
714
  rb_ca_modify(self);
710
715
 
711
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
716
+ GetCArray(self, ca);
712
717
 
713
718
  /* Super-hot path: entity. Materialise mask on first call (one-time per
714
719
  array), then direct ptr write. */
@@ -749,14 +754,14 @@ rb_ca_elem_mask (VALUE self, VALUE ridx)
749
754
  * the stored data unchanged. No-op if `self` has no mask. */
750
755
 
751
756
  VALUE
752
- rb_ca_elem_unmask (VALUE self, VALUE ridx)
757
+ rb_ca_elem_unmask (volatile VALUE self, VALUE ridx)
753
758
  {
754
759
  CArray *ca;
755
760
  ca_size_t idx[CA_RANK_MAX];
756
761
 
757
762
  rb_ca_modify(self);
758
763
 
759
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
764
+ GetCArray(self, ca);
760
765
 
761
766
  /* Super-hot path: entity. If no mask exists, nothing to clear. */
762
767
  if ( ca_is_entity(ca) ) {
@@ -839,14 +844,14 @@ rb_ca_elem_unmask (VALUE self, VALUE ridx)
839
844
  } while (0)
840
845
 
841
846
  VALUE
842
- rb_ca_elem_min (VALUE self, VALUE ridx, VALUE rval)
847
+ rb_ca_elem_min (volatile VALUE self, VALUE ridx, VALUE rval)
843
848
  {
844
849
  CArray *ca;
845
850
  ca_size_t idx[CA_RANK_MAX];
846
851
 
847
852
  rb_ca_modify(self);
848
853
 
849
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
854
+ GetCArray(self, ca);
850
855
 
851
856
  if ( ! ca_is_numeric_type(ca) ) {
852
857
  rb_raise(rb_eCADataTypeError,
@@ -856,6 +861,14 @@ rb_ca_elem_min (VALUE self, VALUE ridx, VALUE rval)
856
861
  rb_raise(rb_eCADataTypeError,
857
862
  "elem_min requires a real array (complex values have no order)");
858
863
  }
864
+ /* picks by order, as pmin does: an Integer the type can not hold is
865
+ brought to the type's bound when it can never win, and refused when
866
+ it would always win */
867
+ {
868
+ volatile VALUE value = rval;
869
+ ca_scalar_operand_for_op(&value, self, CA_SCALAR_FOR_MIN);
870
+ rval = value;
871
+ }
859
872
 
860
873
  /* Super-hot path: entity + numeric data type + Fixnum/Float rval. */
861
874
  ELEM_MINMAX_BODY(<);
@@ -899,14 +912,14 @@ rb_ca_elem_min (VALUE self, VALUE ridx, VALUE rval)
899
912
  * rule. Raises for non-numeric data_type. */
900
913
 
901
914
  VALUE
902
- rb_ca_elem_max (VALUE self, VALUE ridx, VALUE rval)
915
+ rb_ca_elem_max (volatile VALUE self, VALUE ridx, VALUE rval)
903
916
  {
904
917
  CArray *ca;
905
918
  ca_size_t idx[CA_RANK_MAX];
906
919
 
907
920
  rb_ca_modify(self);
908
921
 
909
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
922
+ GetCArray(self, ca);
910
923
 
911
924
  if ( ! ca_is_numeric_type(ca) ) {
912
925
  rb_raise(rb_eCADataTypeError,
@@ -916,6 +929,14 @@ rb_ca_elem_max (VALUE self, VALUE ridx, VALUE rval)
916
929
  rb_raise(rb_eCADataTypeError,
917
930
  "elem_max requires a real array (complex values have no order)");
918
931
  }
932
+ /* picks by order, as pmax does: an Integer the type can not hold is
933
+ brought to the type's bound when it can never win, and refused when
934
+ it would always win */
935
+ {
936
+ volatile VALUE value = rval;
937
+ ca_scalar_operand_for_op(&value, self, CA_SCALAR_FOR_MAX);
938
+ rval = value;
939
+ }
919
940
 
920
941
  ELEM_MINMAX_BODY(>);
921
942