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_order.c CHANGED
@@ -1,6 +1,5 @@
1
1
  #include "ruby.h"
2
2
  #include "carray.h"
3
- #include "carray_internal.h" /* ca_integer_arg */
4
3
  #include "ca_kernel_iterator.h"
5
4
  #include "ca_obj_face.h"
6
5
  #include <math.h>
@@ -9,7 +8,7 @@
9
8
 
10
9
  /* Interned IDs used by the dispatchers below. rb_funcall is retained
11
10
  * only where there is no clean C entry: `-` (operator dispatch in
12
- * rb_ca_order handles Integer vs CArray uniformly via method dispatch)
11
+ * rb_ca_rank_index handles Integer vs CArray uniformly via method dispatch)
13
12
  * and Symbol tag comparison for `method:` kwarg. */
14
13
  static ID id_axis, id_sub,
15
14
  id_sym_binary, id_sym_linear;
@@ -128,7 +127,7 @@ ca_project (CArray *ca, CArray *ci, char *lfill, char *ufill)
128
127
  obj = rb_carray_new(ca->data_type, ci->ndim, ci->dim, ca->bytes, NULL);
129
128
  GetCArray(obj, co);
130
129
 
131
- ca_attach_n(2, ca, ci); /* window: nothing raises inside */
130
+ ca_attach_n(2, ca, ci); /* attach: nothing raises before the detach */
132
131
  ca_project_loop(co, ca, ci, lfill, ufill);
133
132
  ca_detach_n(2, ca, ci); /* DETACH */
134
133
 
@@ -169,7 +168,7 @@ ca_project (CArray *ca, CArray *ci, char *lfill, char *ufill)
169
168
  else CA_COUNT_BELOW_LOOP(T, <)
170
169
 
171
170
  static VALUE
172
- rb_ca_count_below (VALUE self, volatile VALUE vq, VALUE vinclusive)
171
+ rb_ca_count_below (volatile VALUE self, volatile VALUE vq, VALUE vinclusive)
173
172
  {
174
173
  volatile VALUE vout;
175
174
  CArray *cr, *cq, *co;
@@ -203,7 +202,7 @@ rb_ca_count_below (VALUE self, volatile VALUE vq, VALUE vinclusive)
203
202
  GetCArray(vout, co);
204
203
  out = (int64_t *) co->ptr;
205
204
 
206
- ca_attach_n(2, cr, cq); /* window: nothing raises inside */
205
+ ca_attach_n(2, cr, cq); /* attach: nothing raises before the detach */
207
206
  switch ( cr->data_type ) {
208
207
  case CA_BOOLEAN:
209
208
  case CA_UINT8: CA_COUNT_BELOW(uint8_t); break;
@@ -225,32 +224,35 @@ rb_ca_count_below (VALUE self, volatile VALUE vq, VALUE vinclusive)
225
224
  #undef CA_COUNT_BELOW
226
225
  #undef CA_COUNT_BELOW_LOOP
227
226
 
228
- /* project(idx, lval=nil, uval=nil) — gather: for each element of `idx`,
229
- * pick `self[idx[i]]`. Returns an entity CArray shaped like `idx`.
227
+ /* project(idx, fill_value: nil, lfill: nil, ufill: nil) — gather: for each
228
+ * element of `idx`, pick `self[idx[i]]`. Returns an entity CArray shaped
229
+ * like `idx`.
230
230
  *
231
231
  * Out-of-range index handling: negative or >= self.elements indices
232
- * produce `lval` (lower) / `uval` (upper) if provided, otherwise mask
233
- * the output cell. Masked inputs (`idx` mask or `self` mask) also
232
+ * produce `lfill` (lower) / `ufill` (upper), each defaulting to
233
+ * `fill_value`, otherwise mask the output cell. Masked inputs (`idx` mask or `self` mask) also
234
234
  * propagate as masked output.
235
235
  */
236
236
  VALUE
237
- rb_ca_project (int argc, VALUE *argv, VALUE self)
237
+ rb_ca_project (int argc, VALUE *argv, volatile VALUE self)
238
238
  {
239
- volatile VALUE obj, ridx, vlfval, vufval, vstorage;
239
+ volatile VALUE obj;
240
+ VALUE ridx;
241
+ volatile VALUE vlfval, vufval, vstorage;
240
242
  volatile VALUE hlfval = 0, hufval = 0;
241
243
  CArray *ca, *ci, *co;
242
244
  char *lfval, *ufval;
243
245
  int self_is_face;
244
246
 
245
247
  {
246
- VALUE ropt = Qnil;
247
- /* project takes no keyword: one given was read as a fill value. */
248
- rb_scan_args(argc, argv, "12:", (VALUE *)&ridx, (VALUE *) &vlfval,
249
- (VALUE *) &vufval, &ropt);
250
- rb_scan_options(ropt, "");
248
+ VALUE ropt = Qnil, vfill = Qnil, vl = Qnil, vu = Qnil;
249
+ rb_scan_args(argc, argv, "1:", &ridx, &ropt);
250
+ rb_scan_options(ropt, "fill_value,lfill,ufill", &vfill, &vl, &vu);
251
+ vlfval = NIL_P(vl) ? vfill : vl;
252
+ vufval = NIL_P(vu) ? vfill : vu;
251
253
  }
252
254
 
253
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
255
+ GetCArray(self, ca);
254
256
  self_is_face = ca_is_face(ca);
255
257
 
256
258
  rb_check_carray_object(ridx);
@@ -266,7 +268,6 @@ rb_ca_project (int argc, VALUE *argv, VALUE self)
266
268
 
267
269
  if ( ! NIL_P(vlfval) && vlfval != CA_UNDEF ) {
268
270
  rb_ca_obj2ptr(self, vlfval, lfval);
269
- rb_ca_obj2ptr(self, vlfval, ufval);
270
271
  }
271
272
 
272
273
  if ( ! NIL_P(vufval) && vufval != CA_UNDEF ) {
@@ -281,13 +282,13 @@ rb_ca_project (int argc, VALUE *argv, VALUE self)
281
282
  vstorage = self;
282
283
  if ( self_is_face ) {
283
284
  vstorage = rb_ca_strip_face_value(self);
284
- TypedData_Get_Struct(vstorage, CArray, &carray_data_type, ca);
285
+ GetCArray(vstorage, ca);
285
286
  }
286
287
 
287
288
  /* A fill of UNDEF is no fill: the cell is masked, as it is with none. */
288
289
  {
289
290
  VALUE vlower = vlfval;
290
- VALUE vupper = NIL_P(vufval) ? vlfval : vufval;
291
+ VALUE vupper = vufval;
291
292
  obj = ca_project(ca, ci,
292
293
  ( NIL_P(vlower) || vlower == CA_UNDEF ) ? NULL : lfval,
293
294
  ( NIL_P(vupper) || vupper == CA_UNDEF ) ? NULL : ufval);
@@ -405,6 +406,27 @@ ca_reshape_search_result (volatile VALUE query, VALUE rraw)
405
406
  return rb_ca_reshape((int) cq->ndim, shape_argv, rraw);
406
407
  }
407
408
 
409
+ /* A Complex with an imaginary part has no place in the order of a real
410
+ array, so bsearch and search_nearest refuse it (search, which asks for
411
+ equal cells, finds none). */
412
+ static void
413
+ ca_refuse_unordered_query (volatile VALUE self, VALUE query)
414
+ {
415
+ CArray *ca;
416
+ if ( ! RB_TYPE_P(query, T_COMPLEX) || ! rb_obj_is_carray(self) ) {
417
+ return;
418
+ }
419
+ GetCArray(self, ca);
420
+ if ( ca_is_face(ca) || ! ( ca_is_integer_type(ca) || ca_is_float_type(ca) ) ||
421
+ RTEST(rb_equal(rb_complex_imag(query), INT2FIX(0))) ) {
422
+ return;
423
+ }
424
+ rb_raise(rb_eTypeError, "%s: %" PRIsVALUE " has no place in the order of %s %s array",
425
+ ca_calling_method_name(), rb_inspect(query),
426
+ ( ca_type_name[ca->data_type][0] == 'i' ) ? "an" : "a",
427
+ ca_type_name[ca->data_type]);
428
+ }
429
+
408
430
  static VALUE
409
431
  rb_ca_bsearch_kw (int argc, VALUE *argv, VALUE self)
410
432
  {
@@ -412,6 +434,7 @@ rb_ca_bsearch_kw (int argc, VALUE *argv, VALUE self)
412
434
  VALUE raxis = Qnil;
413
435
  rb_scan_options(ropt, "axis", &raxis);
414
436
  rb_check_arity(argc, 1, 1);
437
+ ca_refuse_unordered_query(self, argv[0]);
415
438
 
416
439
  if ( NIL_P(raxis) ) {
417
440
  VALUE flat = rb_ca_flatten(self);
@@ -429,6 +452,7 @@ rb_ca_bsearch_addr_kw (int argc, VALUE *argv, VALUE self)
429
452
  VALUE raxis = Qnil;
430
453
  rb_scan_options(ropt, "axis", &raxis);
431
454
  rb_check_arity(argc, 1, 1);
455
+ ca_refuse_unordered_query(self, argv[0]);
432
456
 
433
457
  if ( NIL_P(raxis) ) {
434
458
  /* 1-D self: view-flat addr == axis-local index, so the no-axis
@@ -445,15 +469,14 @@ rb_ca_bsearch_addr_kw (int argc, VALUE *argv, VALUE self)
445
469
  to a numeric array. A value the array can not hold (nil, a String)
446
470
  finds nothing, as an Integer outside the array's type does. The query is asked of the
447
471
  cells that matched (none), in the type that holds the answer. A Face that
448
- can not be searched is refused as for any query, and a complex array,
449
- which search does not take, goes on to its refusal. */
472
+ can not be searched is refused as for any query. */
450
473
  static void
451
- ca_search_query_read (VALUE *pself, VALUE *pquery)
474
+ ca_search_query_read (volatile VALUE *pself, volatile VALUE *pquery)
452
475
  {
453
476
  CArray *ca;
454
477
  GetCArray(*pself, ca);
455
478
  *pquery = ca_truth_as_number(ca, *pquery);
456
- if ( ! ca_query_matches_nothing(ca, *pquery) || ca_is_complex_type(ca) ) {
479
+ if ( ! ca_query_matches_nothing(*pself, ca, *pquery) ) {
457
480
  return;
458
481
  }
459
482
  if ( ca_is_face(ca) ) {
@@ -467,18 +490,19 @@ static VALUE
467
490
  rb_ca_search_kw (int argc, VALUE *argv, VALUE self)
468
491
  {
469
492
  VALUE ropt = rb_pop_options(&argc, &argv);
470
- VALUE raxis = Qnil;
471
- rb_scan_options(ropt, "axis", &raxis);
472
- rb_check_arity(argc, 1, 2);
493
+ VALUE raxis = Qnil, rtol = Qnil;
494
+ rb_scan_options(ropt, "axis,tol", &raxis, &rtol);
495
+ rb_check_arity(argc, 1, 1);
473
496
  VALUE rquery = argv[0];
497
+ int has_tol = ! NIL_P(rtol);
474
498
  ca_search_query_read(&self, &rquery);
475
499
 
476
500
  if ( NIL_P(raxis) ) {
477
501
  VALUE flat = rb_ca_flatten(self);
478
502
  VALUE q = rb_obj_is_carray(rquery) ? rb_ca_flatten(rquery) : rquery;
479
503
  VALUE r;
480
- VALUE ki_argv[3] = { q, INT2FIX(0), argv[1] };
481
- if ( argc >= 2 ) {
504
+ VALUE ki_argv[3] = { q, INT2FIX(0), rtol };
505
+ if ( has_tol ) {
482
506
  r = rb_ca_search_ki(3, ki_argv, flat);
483
507
  }
484
508
  else {
@@ -486,9 +510,9 @@ rb_ca_search_kw (int argc, VALUE *argv, VALUE self)
486
510
  }
487
511
  return rb_obj_is_carray(rquery) ? ca_reshape_search_result(rquery, r) : r;
488
512
  }
489
- /* search_ki(val, axis, eps) -- argc=2 (val,axis) or 3 (val,axis,eps) */
490
- VALUE ki_argv[3] = { rquery, raxis, argv[1] };
491
- if ( argc >= 2 ) {
513
+ /* search_ki(val, axis, tol) -- argc=2 (val, axis) or 3 (val, axis, tol) */
514
+ VALUE ki_argv[3] = { rquery, raxis, rtol };
515
+ if ( has_tol ) {
492
516
  return rb_ca_search_ki(3, ki_argv, self);
493
517
  }
494
518
  return rb_ca_search_ki(2, ki_argv, self);
@@ -498,10 +522,11 @@ static VALUE
498
522
  rb_ca_search_addr_kw (int argc, VALUE *argv, VALUE self)
499
523
  {
500
524
  VALUE ropt = rb_pop_options(&argc, &argv);
501
- VALUE raxis = Qnil;
502
- rb_scan_options(ropt, "axis", &raxis);
503
- rb_check_arity(argc, 1, 2);
525
+ VALUE raxis = Qnil, rtol = Qnil;
526
+ rb_scan_options(ropt, "axis,tol", &raxis, &rtol);
527
+ rb_check_arity(argc, 1, 1);
504
528
  VALUE rquery = argv[0];
529
+ int has_tol = ! NIL_P(rtol);
505
530
  ca_search_query_read(&self, &rquery);
506
531
 
507
532
  if ( NIL_P(raxis) ) {
@@ -509,8 +534,8 @@ rb_ca_search_addr_kw (int argc, VALUE *argv, VALUE self)
509
534
  VALUE flat = rb_ca_flatten(self);
510
535
  VALUE q = rb_obj_is_carray(rquery) ? rb_ca_flatten(rquery) : rquery;
511
536
  VALUE r;
512
- VALUE ki_argv[3] = { q, INT2FIX(0), argv[1] };
513
- if ( argc >= 2 ) {
537
+ VALUE ki_argv[3] = { q, INT2FIX(0), rtol };
538
+ if ( has_tol ) {
514
539
  r = rb_ca_search_ki(3, ki_argv, flat);
515
540
  }
516
541
  else {
@@ -518,8 +543,8 @@ rb_ca_search_addr_kw (int argc, VALUE *argv, VALUE self)
518
543
  }
519
544
  return rb_obj_is_carray(rquery) ? ca_reshape_search_result(rquery, r) : r;
520
545
  }
521
- VALUE ki_argv[3] = { rquery, raxis, argv[1] };
522
- if ( argc >= 2 ) {
546
+ VALUE ki_argv[3] = { rquery, raxis, rtol };
547
+ if ( has_tol ) {
523
548
  return rb_ca_search_addr_ki(3, ki_argv, self);
524
549
  }
525
550
  return rb_ca_search_addr_ki(2, ki_argv, self);
@@ -532,6 +557,7 @@ rb_ca_search_nearest_kw (int argc, VALUE *argv, VALUE self)
532
557
  VALUE raxis = Qnil;
533
558
  rb_scan_options(ropt, "axis", &raxis);
534
559
  rb_check_arity(argc, 1, 1);
560
+ ca_refuse_unordered_query(self, argv[0]);
535
561
 
536
562
  if ( NIL_P(raxis) ) {
537
563
  VALUE flat = rb_ca_flatten(self);
@@ -549,6 +575,7 @@ rb_ca_search_nearest_addr_kw (int argc, VALUE *argv, VALUE self)
549
575
  VALUE raxis = Qnil;
550
576
  rb_scan_options(ropt, "axis", &raxis);
551
577
  rb_check_arity(argc, 1, 1);
578
+ ca_refuse_unordered_query(self, argv[0]);
552
579
 
553
580
  if ( NIL_P(raxis) ) {
554
581
  /* 1-D self: addr == index reuses _ki (not _addr_ki) -- same as bsearch_addr. */
@@ -575,13 +602,13 @@ rb_ca_search_nearest_addr_kw (int argc, VALUE *argv, VALUE self)
575
602
 
576
603
  /* The key orders self's cells one for one, so it has self's shape. */
577
604
  static void
578
- ca_by_key_check_shape (VALUE self, VALUE rkey, const char *name)
605
+ ca_by_key_check_shape (volatile VALUE self, volatile VALUE rkey, const char *name)
579
606
  {
580
607
  CArray *ca, *ck;
581
608
  int8_t i;
582
609
  rb_check_carray_object(rkey);
583
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
584
- TypedData_Get_Struct(rkey, CArray, &carray_data_type, ck);
610
+ GetCArray(self, ca);
611
+ GetCArray(rkey, ck);
585
612
  if ( ca->ndim == ck->ndim ) {
586
613
  for (i = 0; i < ca->ndim; i++) {
587
614
  if ( ca->dim[i] != ck->dim[i] ) break;
@@ -590,8 +617,7 @@ ca_by_key_check_shape (VALUE self, VALUE rkey, const char *name)
590
617
  }
591
618
  rb_raise(rb_eArgError, "%s: the key's shape %"PRIsVALUE" differs from "
592
619
  "the array's %"PRIsVALUE, name,
593
- rb_funcall(rkey, rb_intern("shape"), 0),
594
- rb_funcall(self, rb_intern("shape"), 0));
620
+ rb_ca_dim(rkey), rb_ca_dim(self));
595
621
  }
596
622
 
597
623
  /* self at the addresses an extremum by key found. A fiber with no cell to
@@ -608,32 +634,41 @@ ca_by_key_fetch (VALUE self, VALUE addrs)
608
634
  return rb_ca_fetch(self, addrs);
609
635
  }
610
636
 
611
- /* sort_by_key(key, axis: 0) -- gather self at key.sort_addr(axis:). */
637
+ /* sort_by_key(key, axis: nil, masked_position: :last) -- gather self at
638
+ key.sort_addr(axis:, masked_position:). Without an axis the whole array
639
+ is ordered and the answer is flat, as sort answers. */
612
640
  static VALUE
613
641
  rb_ca_sort_by_key (int argc, VALUE *argv, VALUE self)
614
642
  {
615
- VALUE rkey, rkw = Qnil, raxis = Qnil;
643
+ VALUE rkey, rkw = Qnil, raxis = Qnil, rmasked_position = Qnil;
644
+ int masked_last;
616
645
  rb_scan_args(argc, argv, "1:", &rkey, &rkw);
617
- rb_scan_options(rkw, "axis", &raxis);
646
+ rb_scan_options(rkw, "axis,masked_position", &raxis, &rmasked_position);
647
+ /* where the cells whose key is masked go: :last (default) or :first */
648
+ masked_last = NIL_P(rmasked_position) ? 1
649
+ : ca_symbol_choice(rmasked_position, "masked_position", "first", "last", NULL);
650
+ ca_by_key_check_shape(self, rkey, "sort_by_key");
618
651
  if ( NIL_P(raxis) ) {
619
- raxis = INT2FIX(0);
652
+ VALUE flat_key = rb_ca_flatten(rkey);
653
+ VALUE flat = rb_ca_flatten(self);
654
+ VALUE addrs = rb_ca_sort_addr_c(flat_key, INT2FIX(0), 0, masked_last);
655
+ return rb_ca_fetch(flat, addrs);
620
656
  }
621
- ca_by_key_check_shape(self, rkey, "sort_by_key");
622
- VALUE addrs = rb_ca_sort_addr_c(rkey, raxis, 0, 1); /* masked_last=1 (:last), unchanged default */
657
+ VALUE addrs = rb_ca_sort_addr_c(rkey, raxis, 0, masked_last);
623
658
  return rb_ca_fetch(self, addrs);
624
659
  }
625
660
 
626
661
  /* The extreme kernels have no fixlen lane: a fixlen key is read through the
627
662
  rank of its cells, which keeps ties and masked cells. */
628
663
  static VALUE
629
- ca_by_key_comparable (VALUE key)
664
+ ca_by_key_comparable (volatile VALUE key)
630
665
  {
631
666
  CArray *ca;
632
- TypedData_Get_Struct(key, CArray, &carray_data_type, ca);
667
+ GetCArray(key, ca);
633
668
  if ( ca->data_type == CA_FIXLEN ) {
634
669
  VALUE opts = rb_hash_new();
635
670
  rb_hash_aset(opts, ID2SYM(rb_intern("method")), ID2SYM(rb_intern("dense")));
636
- return rb_funcallv_kw(key, rb_intern("order"), 1, &opts, RB_PASS_KEYWORDS);
671
+ return rb_funcallv_kw(key, rb_intern("rank_index"), 1, &opts, RB_PASS_KEYWORDS);
637
672
  }
638
673
  return key;
639
674
  }
@@ -740,11 +775,10 @@ rb_ca_put_along_axis (int argc, VALUE *argv, VALUE self)
740
775
  broadcast, UNDEF or a Face value means what it means unmasked), and
741
776
  only the cells at unmasked indices are written. */
742
777
  if ( RTEST(rb_ca_has_mask(addrs)) ) {
743
- VALUE sel = rb_funcall(addrs, rb_intern("is_not_masked"), 0);
778
+ VALUE sel = rb_ca_is_not_masked(addrs);
744
779
  VALUE vals = rb_funcall(self, rb_intern("project"), 1, addrs);
745
- rb_funcall(vals, rb_intern("[]="), 1, argv[1]);
746
- rb_ca_store(self, rb_funcall(addrs, rb_intern("[]"), 1, sel),
747
- rb_funcall(vals, rb_intern("[]"), 1, sel));
780
+ rb_ca_store2(vals, 0, NULL, argv[1]);
781
+ rb_ca_store(self, rb_ca_fetch(addrs, sel), rb_ca_fetch(vals, sel));
748
782
  return self;
749
783
  }
750
784
  rb_ca_store(self, addrs, argv[1]);
@@ -754,12 +788,12 @@ rb_ca_put_along_axis (int argc, VALUE *argv, VALUE self)
754
788
  /* ---- range -- (min..max) via fused minmax ----------------------------- */
755
789
 
756
790
  static VALUE
757
- rb_ca_range_method (VALUE self)
791
+ rb_ca_range_method (volatile VALUE self)
758
792
  {
759
793
  VALUE mm_argv[1] = { Qnil };
760
794
  CArray *ca;
761
795
  VALUE pair;
762
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
796
+ GetCArray(self, ca);
763
797
  /* A Face answers its own minmax (in its order, with its elements). */
764
798
  pair = ca_is_face(ca) ? rb_funcall(self, rb_intern("minmax"), 0)
765
799
  : rb_ca_minmax_ki(0, mm_argv, self);
@@ -834,6 +868,7 @@ ca_topk_cmp (const ca_topk_fiber_t *f, ca_size_t i, ca_size_t j)
834
868
  #undef CA_TOPK_CMP
835
869
 
836
870
  /* Merge sort of positions by (order, position). */
871
+ /* recursion: a merge sort, log2 of the length deep */
837
872
  static void
838
873
  ca_topk_sort (const ca_topk_fiber_t *f, ca_size_t *p, ca_size_t *tmp, ca_size_t n)
839
874
  {
@@ -1190,17 +1225,17 @@ ca_topk_buffered_positions (volatile VALUE key, long cap, long axis, int desc)
1190
1225
  /* Per-axis top-k positions (cap clamped, axis normalized).
1191
1226
  * Called by rb_ca_topk_index below. */
1192
1227
  static VALUE
1193
- rb_ca_topk_positions (VALUE self, long cap, long axis_norm, int desc)
1228
+ rb_ca_topk_positions (volatile VALUE self, long cap, long axis_norm, int desc)
1194
1229
  {
1195
1230
  CArray *ca;
1196
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1231
+ GetCArray(self, ca);
1197
1232
  long dim_axis = (long) ca->dim[axis_norm];
1198
1233
  VALUE raxis_norm = LONG2NUM(axis_norm);
1199
1234
 
1200
1235
  /* The buffered walk reads any key but an object (its cells compare in
1201
1236
  Ruby) or a complex one (no order), and keeps cap cells per fiber. */
1202
1237
  if ( ca->data_type != CA_OBJECT && ! ca_is_complex_type(ca) ) {
1203
- VALUE key = ca_order_key(self, desc ? "nlargest" : "nsmallest");
1238
+ volatile VALUE key = ca_order_key(self, desc ? "nlargest" : "nsmallest");
1204
1239
  CArray *ck;
1205
1240
  GetCArray(key, ck);
1206
1241
  if ( ck->data_type != CA_OBJECT && ! ca_is_complex_type(ck) &&
@@ -1218,7 +1253,7 @@ rb_ca_topk_positions (VALUE self, long cap, long axis_norm, int desc)
1218
1253
  VALUE kw = rb_hash_new();
1219
1254
  rb_hash_aset(kw, ID2SYM(rb_intern("axis")), raxis_norm);
1220
1255
  rb_hash_aset(kw, ID2SYM(rb_intern("descending")), desc ? Qtrue : Qfalse);
1221
- VALUE rank = rb_funcallv_kw(self, rb_intern("order"), 1, &kw, RB_PASS_KEYWORDS);
1256
+ VALUE rank = rb_funcallv_kw(self, rb_intern("rank_index"), 1, &kw, RB_PASS_KEYWORDS);
1222
1257
  VALUE full = rb_ca_sort_index_ki_quick(rank, raxis_norm);
1223
1258
  if ( cap >= dim_axis ) {
1224
1259
  return rb_ca_copy(full);
@@ -1268,10 +1303,10 @@ rb_ca_topk_positions (VALUE self, long cap, long axis_norm, int desc)
1268
1303
  * Called by rb_ca_nlargest_index / rb_ca_nsmallest_index (index
1269
1304
  * variants) and indirectly by rb_ca_topk_values below. */
1270
1305
  static VALUE
1271
- rb_ca_topk_index (VALUE self, VALUE rn, VALUE raxis, int desc, const char *name)
1306
+ rb_ca_topk_index (volatile VALUE self, VALUE rn, VALUE raxis, int desc, const char *name)
1272
1307
  {
1273
1308
  CArray *ca;
1274
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1309
+ GetCArray(self, ca);
1275
1310
  long a = rb_ca_normalize_axis_value(self, raxis, name);
1276
1311
  long n_val = ca_integer_arg(rn, "n", name);
1277
1312
  long dim_a = (long) ca->dim[a];
@@ -1354,12 +1389,12 @@ rb_ca_nsmallest (int argc, VALUE *argv, VALUE self)
1354
1389
  return rb_ca_topk_values(self, argv[0], raxis, 0, "nsmallest");
1355
1390
  }
1356
1391
 
1357
- /* ---- order(axis:, descending:, method:) -------------------------------
1392
+ /* ---- rank_index(axis:, descending:, method:) ---------------------------
1358
1393
  *
1359
1394
  * method: :ordinal (default) assigns every cell a distinct rank (ties
1360
1395
  * broken by stable original position, scipy.stats.rankdata 'ordinal'
1361
1396
  * style). method: :dense assigns tied values the same rank (no gaps),
1362
- * the standard "dense rank" -- lets order(descending:) compose as a
1397
+ * the standard "dense rank" -- lets rank_index(descending:) compose as a
1363
1398
  * sort_addr priority key without silently dropping a lower-priority
1364
1399
  * key on ties: ordinal ranks are already a total order (no two cells
1365
1400
  * ever compare equal), so a later sort_addr key never gets consulted;
@@ -1371,7 +1406,7 @@ rb_ca_nsmallest (int argc, VALUE *argv, VALUE self)
1371
1406
  * order in both directions. */
1372
1407
 
1373
1408
  static VALUE
1374
- rb_ca_order (int argc, VALUE *argv, VALUE self)
1409
+ rb_ca_rank_index (int argc, VALUE *argv, volatile VALUE self)
1375
1410
  {
1376
1411
  VALUE ropt = rb_pop_options(&argc, &argv);
1377
1412
  VALUE raxis = Qnil, rdesc = Qfalse, rmethod = Qnil, rkind = Qnil;
@@ -1383,34 +1418,35 @@ rb_ca_order (int argc, VALUE *argv, VALUE self)
1383
1418
  int do_stable = NIL_P(rkind) ? 0
1384
1419
  : ca_symbol_choice(rkind, "kind", "quick", "stable", NULL);
1385
1420
 
1386
- if ( RTEST(rdesc) && ! dense ) {
1421
+ int desc = ca_bool_arg(rdesc, "descending");
1422
+ if ( desc && ! dense ) {
1387
1423
  VALUE kw = rb_hash_new();
1388
1424
  rb_hash_aset(kw, ID2SYM(rb_intern("axis")), raxis);
1389
1425
  rb_hash_aset(kw, ID2SYM(rb_intern("descending")), Qtrue);
1390
1426
  rb_hash_aset(kw, ID2SYM(rb_intern("method")), ID2SYM(rb_intern("dense")));
1391
1427
  rb_hash_aset(kw, ID2SYM(rb_intern("kind")), do_stable ? ID2SYM(rb_intern("stable")) : ID2SYM(rb_intern("quick")));
1392
- VALUE ranked = rb_funcallv_kw(self, rb_intern("order"), 1, &kw, RB_PASS_KEYWORDS);
1428
+ VALUE ranked = rb_funcallv_kw(self, rb_intern("rank_index"), 1, &kw, RB_PASS_KEYWORDS);
1393
1429
  rb_hash_delete(kw, ID2SYM(rb_intern("descending")));
1394
1430
  rb_hash_delete(kw, ID2SYM(rb_intern("method")));
1395
- return rb_funcallv_kw(ranked, rb_intern("order"), 1, &kw, RB_PASS_KEYWORDS);
1431
+ return rb_funcallv_kw(ranked, rb_intern("rank_index"), 1, &kw, RB_PASS_KEYWORDS);
1396
1432
  }
1397
1433
 
1398
1434
  /* The kernel's dense == 2 is the descending dense rank. */
1399
- int kmode = ( dense && RTEST(rdesc) ) ? 2 : dense;
1435
+ int kmode = ( dense && desc ) ? 2 : dense;
1400
1436
  if ( NIL_P(raxis) ) {
1401
1437
  VALUE flat = rb_ca_flatten(self);
1402
1438
  /* Direct C-level dispatch via c_callable: true extern (mkkernel). */
1403
1439
  VALUE ranked = rb_ca_rank_index_ki_kind_dense(flat, INT2FIX(0), do_stable, kmode);
1404
1440
  /* reshape(*shape) */
1405
1441
  CArray *ca;
1406
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1442
+ GetCArray(self, ca);
1407
1443
  VALUE shape_argv[CA_RANK_MAX];
1408
1444
  for ( long k = 0; k < (long) ca->ndim; k++ ) {
1409
1445
  shape_argv[k] = LONG2NUM((long) ca->dim[k]);
1410
1446
  }
1411
1447
  return rb_ca_reshape((int) ca->ndim, shape_argv, ranked);
1412
1448
  }
1413
- long axis_norm = rb_ca_normalize_axis_value(self, raxis, "order");
1449
+ long axis_norm = rb_ca_normalize_axis_value(self, raxis, "rank_index");
1414
1450
  /* Output data_type: CA_SIZE (int64 on 64-bit builds), as the rest of the
1415
1451
  *_index family. */
1416
1452
  return rb_ca_rank_index_ki_kind_dense(self, LONG2NUM(axis_norm), do_stable, kmode);
@@ -1422,10 +1458,10 @@ rb_ca_order (int argc, VALUE *argv, VALUE self)
1422
1458
  * rb_ca_to_float64), then optionally flatten when axis is nil.
1423
1459
  * Called by rb_ca_linear_section_m and rb_ca_linear_fetch_m. */
1424
1460
  static VALUE
1425
- ca_linear_prep (VALUE self, VALUE raxis, const char *name, long *axis_out)
1461
+ ca_linear_prep (volatile VALUE self, VALUE raxis, const char *name, long *axis_out)
1426
1462
  {
1427
1463
  CArray *ca;
1428
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1464
+ GetCArray(self, ca);
1429
1465
  /* Face gate: linear_section / linear_fetch treat the axis as a continuous
1430
1466
  numeric coordinate (fractional index by interpolating the stored values).
1431
1467
  An ORDERABLE Face (storage order == surface order) is descended to its
@@ -1437,12 +1473,12 @@ ca_linear_prep (VALUE self, VALUE raxis, const char *name, long *axis_out)
1437
1473
  if ( ca_is_face(ca) ) {
1438
1474
  ca_face_require_orderable(self, name, "order");
1439
1475
  self = rb_ca_strip_face_value(self);
1440
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1476
+ GetCArray(self, ca);
1441
1477
  }
1442
- VALUE sc = (ca->data_type == CA_FLOAT64) ? self : rb_ca_to_float64(self);
1478
+ volatile VALUE sc = (ca->data_type == CA_FLOAT64) ? self : rb_ca_to_float64(self);
1443
1479
  if ( NIL_P(raxis) ) {
1444
1480
  CArray *sca;
1445
- TypedData_Get_Struct(sc, CArray, &carray_data_type, sca);
1481
+ GetCArray(sc, sca);
1446
1482
  if ( sca->ndim > 1 ) {
1447
1483
  sc = rb_ca_flatten(sc);
1448
1484
  }
@@ -1454,7 +1490,7 @@ ca_linear_prep (VALUE self, VALUE raxis, const char *name, long *axis_out)
1454
1490
  }
1455
1491
 
1456
1492
  static VALUE
1457
- rb_ca_linear_section_m (int argc, VALUE *argv, VALUE self)
1493
+ rb_ca_linear_section_m (int argc, VALUE *argv, volatile VALUE self)
1458
1494
  {
1459
1495
  VALUE ropt = rb_pop_options(&argc, &argv);
1460
1496
  VALUE raxis = Qnil, rmethod = ID2SYM(id_sym_binary);
@@ -1466,7 +1502,7 @@ rb_ca_linear_section_m (int argc, VALUE *argv, VALUE self)
1466
1502
  brought into one space (ca_face_reconcile_pair). */
1467
1503
  if ( rb_obj_is_carray(self) ) {
1468
1504
  CArray *sca;
1469
- TypedData_Get_Struct(self, CArray, &carray_data_type, sca);
1505
+ GetCArray(self, sca);
1470
1506
  if ( ca_is_face(sca) ) {
1471
1507
  volatile VALUE ref = self, query = val;
1472
1508
  ca_face_reconcile_pair(&ref, &query, "linear_section");
@@ -1477,7 +1513,7 @@ rb_ca_linear_section_m (int argc, VALUE *argv, VALUE self)
1477
1513
 
1478
1514
  {
1479
1515
  CArray *ca;
1480
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1516
+ GetCArray(self, ca);
1481
1517
  if ( ca_is_complex_type(ca) ) {
1482
1518
  rb_raise(rb_eCADataTypeError,
1483
1519
  "linear_section: a %s array has no order (complex numbers are "
@@ -1502,7 +1538,7 @@ rb_ca_linear_section_m (int argc, VALUE *argv, VALUE self)
1502
1538
  }
1503
1539
 
1504
1540
  static VALUE
1505
- rb_ca_linear_fetch_m (int argc, VALUE *argv, VALUE self)
1541
+ rb_ca_linear_fetch_m (int argc, VALUE *argv, volatile VALUE self)
1506
1542
  {
1507
1543
  VALUE ropt = rb_pop_options(&argc, &argv);
1508
1544
  VALUE raxis = Qnil;
@@ -1514,7 +1550,7 @@ rb_ca_linear_fetch_m (int argc, VALUE *argv, VALUE self)
1514
1550
  weights are real, so each part is a real interpolation. */
1515
1551
  {
1516
1552
  CArray *ca;
1517
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1553
+ GetCArray(self, ca);
1518
1554
  if ( ca_is_complex_type(ca) ) {
1519
1555
  VALUE args[2], re, im;
1520
1556
  int n = 1;
@@ -1580,7 +1616,7 @@ Init_carray_order (void)
1580
1616
  rb_define_method(rb_cCArray, "nsmallest", rb_ca_nsmallest, -1);
1581
1617
  rb_define_method(rb_cCArray, "nlargest_index", rb_ca_nlargest_index, -1);
1582
1618
  rb_define_method(rb_cCArray, "nsmallest_index", rb_ca_nsmallest_index, -1);
1583
- rb_define_method(rb_cCArray, "order", rb_ca_order, -1);
1619
+ rb_define_method(rb_cCArray, "rank_index", rb_ca_rank_index, -1);
1584
1620
  rb_define_method(rb_cCArray, "linear_section", rb_ca_linear_section_m, -1);
1585
1621
  rb_define_method(rb_cCArray, "linear_fetch", rb_ca_linear_fetch_m, -1);
1586
1622
 
@@ -14,7 +14,6 @@
14
14
 
15
15
  #include "ruby.h"
16
16
  #include "carray.h"
17
- #include "carray_internal.h" /* ca_normalize_axis_arg */
18
17
  #include "ca_kernel_iterator.h" /* CA_FOR_EACH_FIBER_INOUT */
19
18
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE */
20
19
  #include "ca_sort_kernels.h" /* ca_partition_quick_* / ca_partition_nan_* */
@@ -143,7 +142,7 @@ rb_ca_partition_copy_c (VALUE self, VALUE vkth, VALUE vaxis)
143
142
  /* Mask handling (= same as partition view). */
144
143
  volatile VALUE target = self; /* cat points into it; the walk can run a GC */
145
144
  CArray *cat;
146
- TypedData_Get_Struct(target, CArray, &carray_data_type, cat);
145
+ GetCArray(target, cat);
147
146
  if ( ca_has_mask(cat) ) {
148
147
  if ( RTEST(rb_ca_is_any_masked(target)) ) {
149
148
  rb_raise(rb_eArgError,
@@ -151,11 +150,11 @@ rb_ca_partition_copy_c (VALUE self, VALUE vkth, VALUE vaxis)
151
150
  "(use ca.value or ca.strip_mask(fill))");
152
151
  }
153
152
  target = rb_ca_value_array(target);
154
- TypedData_Get_Struct(target, CArray, &carray_data_type, cat);
153
+ GetCArray(target, cat);
155
154
  }
156
155
 
157
156
  /* Normalize axis. */
158
- int axis = ca_normalize_axis_arg(vaxis, cat->ndim, NULL);
157
+ int axis = rb_ca_normalize_axis_arg(vaxis, cat->ndim, NULL);
159
158
 
160
159
  ca_size_t fiber_n = cat->dim[axis];
161
160
  ca_size_t kth = partition_kth(fiber_n, vkth);
@@ -189,7 +188,7 @@ rb_ca_partition_copy_c (VALUE self, VALUE vkth, VALUE vaxis)
189
188
  INT2NUM(cat->data_type),
190
189
  INT2NUM((int) fbytes));
191
190
  CArray *cao;
192
- TypedData_Get_Struct(vout, CArray, &carray_data_type, cao);
191
+ GetCArray(vout, cao);
193
192
  /* Two cells of scratch. ALLOCV, not the stack: a cell can be wide. */
194
193
  volatile VALUE scratch_holder = 0;
195
194
  char *swap_tmp = ALLOCV_N(char, scratch_holder, 2 * (size_t) fbytes);
@@ -220,7 +219,7 @@ rb_ca_partition_copy_c (VALUE self, VALUE vkth, VALUE vaxis)
220
219
  INT2NUM(cat->data_type),
221
220
  INT2NUM(0));
222
221
  CArray *cao;
223
- TypedData_Get_Struct(vout, CArray, &carray_data_type, cao);
222
+ GetCArray(vout, cao);
224
223
 
225
224
  /* Numeric path: per-type quickselect with inline cmp via the typed
226
225
  ca_partition_quick_* kernels (avoids the function-pointer
@@ -277,22 +276,21 @@ rb_ca_partition_copy_c (VALUE self, VALUE vkth, VALUE vaxis)
277
276
  * CA_FIXLEN / CA_OBJECT delegation pattern already used by sort_copy.
278
277
  * rb_ca_partition_copy_c itself keeps raising on masked input (its
279
278
  * other callers -- carray_median_percentile.c's flat lane -- always
280
- * pre-strip the mask before calling it, per
281
- * devel/MEMO_PER_AXIS_ORDER_STAT_MASK.md; per-axis order statistics
279
+ * pre-strip the mask before calling it; per-axis order statistics
282
280
  * with mask are a separate, still-open gap).
283
281
  */
284
282
  static VALUE
285
- rb_ca_partition_copy_c_mp (VALUE self, VALUE vkth, VALUE vaxis, int masked_last)
283
+ rb_ca_partition_copy_c_mp (volatile VALUE self, VALUE vkth, VALUE vaxis, int masked_last)
286
284
  {
287
285
  CArray *ca;
288
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
286
+ GetCArray(self, ca);
289
287
  if ( ! ca_has_mask(ca) ) {
290
288
  return rb_ca_partition_copy_c(self, vkth, vaxis);
291
289
  }
292
290
  /* Check axis and kth here, so a bad one is reported as partition_copy's
293
291
  rather than as the partition this delegates to. */
294
292
  {
295
- int axis = ca_normalize_axis_arg(vaxis, ca->ndim, NULL);
293
+ int axis = rb_ca_normalize_axis_arg(vaxis, ca->ndim, NULL);
296
294
  partition_kth(ca->dim[axis], vkth);
297
295
  }
298
296
  /* rb_ca_partitioned_view's rb_scan_args "1:" pattern needs call-frame
@@ -316,7 +314,7 @@ rb_ca_partition_copy_c_mp (VALUE self, VALUE vkth, VALUE vaxis, int masked_last)
316
314
  /* Ruby entry: partition_copy(kth, axis: nil, masked_position: :last).
317
315
  No axis means the whole array, as for sort_copy: flatten first. */
318
316
  static VALUE
319
- rb_ca_partition_copy_body (int argc, VALUE *argv, VALUE self)
317
+ rb_ca_partition_copy_body (int argc, VALUE *argv, volatile VALUE self)
320
318
  {
321
319
  VALUE vkth, rkw = Qnil;
322
320
  VALUE vaxis = Qnil;
@@ -339,7 +337,7 @@ rb_ca_partition_copy_body (int argc, VALUE *argv, VALUE self)
339
337
  volatile VALUE face = Qnil;
340
338
  {
341
339
  CArray *ca;
342
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
340
+ GetCArray(self, ca);
343
341
  if ( ca_is_face(ca) ) {
344
342
  if ( ca_face_orders_by_itself(self) ) {
345
343
  VALUE kw = rb_hash_new();
@@ -385,7 +383,7 @@ rb_ca_partition_copy_body (int argc, VALUE *argv, VALUE self)
385
383
  * from end).
386
384
  */
387
385
  static VALUE
388
- rb_ca_partitioned_view (int argc, VALUE *argv, VALUE self)
386
+ rb_ca_partitioned_view (int argc, VALUE *argv, volatile VALUE self)
389
387
  {
390
388
  VALUE vkth, rkw = Qnil;
391
389
  VALUE vaxis = Qnil;
@@ -398,7 +396,7 @@ rb_ca_partitioned_view (int argc, VALUE *argv, VALUE self)
398
396
  : ca_symbol_choice(vmasked_position, "masked_position", "first", "last", NULL);
399
397
 
400
398
  CArray *ca;
401
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
399
+ GetCArray(self, ca);
402
400
 
403
401
  /* CA_FIXLEN flows through the same partition_addr_ki fixlen dialect
404
402
  as numeric; masked_position: applies uniformly across data types.