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
@@ -41,7 +41,6 @@
41
41
 
42
42
  #include "ruby.h"
43
43
  #include "carray.h"
44
- #include "carray_internal.h" /* ca_integer_arg, ca_calling_method_name */
45
44
  #include "ca_obj_face.h" /* ca_face_refuse_numeric */
46
45
  #include <math.h>
47
46
 
@@ -116,14 +115,14 @@ mp_geometry (CArray *ca, long axis, long *OUTER, long *M, long *INNER)
116
115
  /* [numeric] keep_axis: [1,1,...,1] CA_FLOAT64 entity (original ndim)
117
116
  holding a scalar (or a single masked cell when is_undef). */
118
117
  static VALUE
119
- mp_keep_axis_full_f64 (VALUE self, double v, int is_undef)
118
+ mp_keep_axis_full_f64 (volatile VALUE self, double v, int is_undef)
120
119
  {
121
120
  CArray *ca;
122
121
  GetCArray(self, ca);
123
122
  int nd = ca->ndim;
124
123
  ca_size_t *dim = ALLOCA_N(ca_size_t, nd);
125
124
  for ( int i = 0; i < nd; i++ ) dim[i] = 1;
126
- VALUE out = rb_carray_new(CA_FLOAT64, (int8_t) nd, dim, 0, NULL);
125
+ volatile VALUE out = rb_carray_new(CA_FLOAT64, (int8_t) nd, dim, 0, NULL);
127
126
  CArray *co;
128
127
  GetCArray(out, co);
129
128
  if ( is_undef ) {
@@ -138,18 +137,23 @@ mp_keep_axis_full_f64 (VALUE self, double v, int is_undef)
138
137
  /* [shared] all-UNDEF reduced CA_FLOAT64 output for a zero-length reduction
139
138
  axis. An order statistic of no elements has no value, so every reduced
140
139
  cell is masked -- matching mean / min (which return UNDEF cells), not a
141
- raise. keep_axis controls whether the reduced axis is dropped or kept
142
- as a length-1 axis. */
140
+ raise, unless fill_value (nil for none) gives the cells a value, as it
141
+ does for a fiber with no present cell. keep_axis controls whether the
142
+ reduced axis is dropped or kept as a length-1 axis. */
143
143
  static VALUE
144
- mp_axis_all_masked (VALUE self, long axis, int keep_axis)
144
+ mp_axis_all_masked (volatile VALUE self, long axis, VALUE fill_value, int keep_axis)
145
145
  {
146
146
  int8_t ax = (int8_t) axis;
147
147
  CArray *ca;
148
148
  GetCArray(self, ca);
149
- VALUE out = rb_ca_new_reduced(self, &ax, 1,
149
+ volatile VALUE out = rb_ca_new_reduced(self, &ax, 1,
150
150
  (ca->data_type == CA_OBJECT) ? CA_OBJECT
151
151
  : CA_FLOAT64,
152
152
  keep_axis);
153
+ if ( ! NIL_P(fill_value) ) {
154
+ rb_ca_store_all(out, fill_value);
155
+ return out;
156
+ }
153
157
  CArray *co;
154
158
  GetCArray(out, co);
155
159
  ca_create_mask(co);
@@ -257,19 +261,19 @@ median_fill (double *op, int8_t dt, const char *base,
257
261
 
258
262
  /* [numeric] per-axis median -> reduced CA_FLOAT64 CArray. */
259
263
  static VALUE
260
- median_axis (VALUE self, long axis, long n, int keep_axis)
264
+ median_axis (volatile VALUE self, long axis, long n, int keep_axis)
261
265
  {
262
266
  CArray *ca;
263
267
  GetCArray(self, ca);
264
268
  int8_t dt = ca->data_type;
265
269
  long k = (n % 2 == 0) ? (n / 2 - 1) : ((n - 1) / 2);
266
270
 
267
- VALUE pp = rb_ca_partition_copy_c(self, LONG2NUM(k), LONG2NUM(axis));
271
+ volatile VALUE pp = rb_ca_partition_copy_c(self, LONG2NUM(k), LONG2NUM(axis));
268
272
  CArray *cp;
269
273
  GetCArray(pp, cp);
270
274
 
271
275
  int8_t ax = (int8_t) axis;
272
- VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
276
+ volatile VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
273
277
  CArray *co;
274
278
  GetCArray(out, co);
275
279
 
@@ -277,33 +281,31 @@ median_axis (VALUE self, long axis, long n, int keep_axis)
277
281
  mp_geometry(ca, axis, &OUTER, &M, &INNER);
278
282
  median_fill((double *) co->ptr, dt, (const char *) cp->ptr,
279
283
  OUTER, M, INNER, n, k, (n % 2 != 0));
280
- RB_GC_GUARD(pp);
281
284
  return out;
282
285
  }
283
286
 
284
287
  /* [numeric] flat median scalar (double) of a 1-D numeric entity/view. */
285
288
  static double
286
- median_scalar_1d (VALUE src1d, long n)
289
+ median_scalar_1d (volatile VALUE src1d, long n)
287
290
  {
288
291
  CArray *ca;
289
292
  GetCArray(src1d, ca);
290
293
  long k = (n % 2 == 0) ? (n / 2 - 1) : ((n - 1) / 2);
291
- VALUE pp = rb_ca_partition_copy_c(src1d, LONG2NUM(k), INT2FIX(0));
294
+ volatile VALUE pp = rb_ca_partition_copy_c(src1d, LONG2NUM(k), INT2FIX(0));
292
295
  CArray *cp;
293
296
  GetCArray(pp, cp);
294
297
  double res;
295
298
  median_fill(&res, ca->data_type, (const char *) cp->ptr,
296
299
  1, n, 1, n, k, (n % 2 != 0)); /* OUTER=1, M=n, INNER=1 */
297
- RB_GC_GUARD(pp);
298
300
  return res;
299
301
  }
300
302
 
301
303
  /* [numeric] flat median: strip mask / honour min_count, flatten, then
302
304
  reduce on axis 0 to a bare Float (or keep_axis [1..1] entity). */
303
305
  static VALUE
304
- median_flat (VALUE self, long min_count, VALUE fill_value, int keep_axis)
306
+ median_flat (volatile VALUE self, long min_count, VALUE fill_value, int keep_axis)
305
307
  {
306
- VALUE src = self;
308
+ volatile VALUE src = self;
307
309
  int filled = 0; /* masked-out or empty -> use fill_value / UNDEF */
308
310
 
309
311
  if ( RTEST(rb_ca_has_mask(self)) ) {
@@ -325,11 +327,10 @@ median_flat (VALUE self, long min_count, VALUE fill_value, int keep_axis)
325
327
  return is_undef ? CA_UNDEF : fill_value;
326
328
  }
327
329
 
328
- VALUE flat = rb_ca_flatten(src);
330
+ volatile VALUE flat = rb_ca_flatten(src);
329
331
  CArray *fc;
330
332
  GetCArray(flat, fc);
331
333
  double scalar = median_scalar_1d(flat, (long) fc->elements);
332
- RB_GC_GUARD(flat);
333
334
  if ( keep_axis ) return mp_keep_axis_full_f64(self, scalar, 0);
334
335
  return rb_float_new(scalar);
335
336
  }
@@ -352,7 +353,7 @@ median_flat (VALUE self, long min_count, VALUE fill_value, int keep_axis)
352
353
  them on the per-axis one, so look through a CArray to an element. An
353
354
  empty one has nothing to interpolate and passes. */
354
355
  static int
355
- mp_object_interpolates (VALUE v)
356
+ mp_object_interpolates (volatile VALUE v)
356
357
  {
357
358
  if ( rb_obj_is_kind_of(v, rb_cNumeric) ) {
358
359
  return 1;
@@ -375,7 +376,7 @@ mp_object_interpolates (VALUE v)
375
376
  /* [object] The element an interpolation refusal names: lo itself, or the
376
377
  first element of a CArray of them that has no arithmetic. */
377
378
  static VALUE
378
- mp_first_non_numeric (VALUE v)
379
+ mp_first_non_numeric (volatile VALUE v)
379
380
  {
380
381
  if ( rb_obj_is_kind_of(v, rb_cCArray) ) {
381
382
  CArray *ca;
@@ -449,7 +450,7 @@ obj_call_axis (VALUE recv, ID mid, VALUE raxis)
449
450
 
450
451
  /* [object] arr[*spec] with spec[axis]=idx, rest nil (= drop one axis). */
451
452
  static VALUE
452
- obj_slice (VALUE arr, long axis, long idx)
453
+ obj_slice (volatile VALUE arr, long axis, long idx)
453
454
  {
454
455
  CArray *ca;
455
456
  GetCArray(arr, ca);
@@ -462,7 +463,7 @@ obj_slice (VALUE arr, long axis, long idx)
462
463
 
463
464
  /* [object] min over pp[*spec] with spec[axis] = (k+1)..(n-1) along axis. */
464
465
  static VALUE
465
- obj_min_upper (VALUE pp, long axis, long k, long n)
466
+ obj_min_upper (volatile VALUE pp, long axis, long k, long n)
466
467
  {
467
468
  CArray *ca;
468
469
  GetCArray(pp, ca);
@@ -495,7 +496,7 @@ obj_kth_pair (VALUE self, long axis, long k, long n, VALUE *lo, VALUE *hi)
495
496
  masked), in self's Face when self is one. Uses []= so UNDEF / arbitrary
496
497
  objects store correctly. */
497
498
  static VALUE
498
- obj_keep_axis_full (VALUE self, VALUE val)
499
+ obj_keep_axis_full (volatile VALUE self, VALUE val)
499
500
  {
500
501
  CArray *ca;
501
502
  GetCArray(self, ca);
@@ -551,7 +552,7 @@ median_object_axis (VALUE self, long axis, long n, int keep_axis)
551
552
  as a slice of its partitioned copy is made an array of its own, as the
552
553
  other reductions' answers are. */
553
554
  static VALUE
554
- mp_axis_answer (VALUE self, VALUE res, int keep_axis)
555
+ mp_axis_answer (volatile VALUE self, volatile VALUE res, int keep_axis)
555
556
  {
556
557
  CArray *ca;
557
558
  GetCArray(self, ca);
@@ -573,9 +574,10 @@ mp_axis_answer (VALUE self, VALUE res, int keep_axis)
573
574
 
574
575
  /* [object] flat median: mask-strip / min_count, flatten, axis-0 reduce. */
575
576
  static VALUE
576
- median_object_flat (VALUE self, long min_count, VALUE fill_value, int keep_axis)
577
+ median_object_flat (volatile VALUE self, long min_count, VALUE fill_value, int keep_axis)
577
578
  {
578
- VALUE src = self, result;
579
+ volatile VALUE src = self;
580
+ VALUE result;
579
581
  int filled = 0;
580
582
  if ( RTEST(rb_ca_has_mask(self)) ) {
581
583
  CArray *mc; GetCArray(self, mc);
@@ -591,11 +593,10 @@ median_object_flat (VALUE self, long min_count, VALUE fill_value, int keep_axis)
591
593
  if ( filled ) {
592
594
  result = NIL_P(fill_value) ? CA_UNDEF : fill_value;
593
595
  } else {
594
- VALUE flat = rb_ca_flatten(src);
596
+ volatile VALUE flat = rb_ca_flatten(src);
595
597
  CArray *fc;
596
598
  GetCArray(flat, fc);
597
599
  result = median_object_axis(flat, 0, (long) fc->elements, 0);
598
- RB_GC_GUARD(flat);
599
600
  }
600
601
  return keep_axis ? obj_keep_axis_full(self, result) : result;
601
602
  }
@@ -668,7 +669,7 @@ pct_object_one_sorted (VALUE sorted, long axis, long n, double p, VALUE method)
668
669
  /* [object] per-axis percentile -> array of (reduced CArray | scalar),
669
670
  one entry per requested p (keep_axis wrapping done by the entry). */
670
671
  static VALUE
671
- percentile_object_axis (VALUE self, VALUE pers, long axis, VALUE method)
672
+ percentile_object_axis (volatile VALUE self, VALUE pers, long axis, VALUE method)
672
673
  {
673
674
  CArray *ca;
674
675
  GetCArray(self, ca);
@@ -680,7 +681,7 @@ percentile_object_axis (VALUE self, VALUE pers, long axis, VALUE method)
680
681
  is applied by the entry (rb_ca_percentile_m), so build reduced. */
681
682
  VALUE res = rb_ary_new_capa(npers);
682
683
  for ( long i = 0; i < npers; i++ )
683
- rb_ary_push(res, mp_axis_all_masked(self, axis, 0));
684
+ rb_ary_push(res, mp_axis_all_masked(self, axis, Qnil, 0));
684
685
  return res;
685
686
  }
686
687
 
@@ -691,13 +692,12 @@ percentile_object_axis (VALUE self, VALUE pers, long axis, VALUE method)
691
692
  }
692
693
  /* multi-p: one shared sort (CA_OBJECT sort trampolines to the focused
693
694
  Ruby per-slab helper), materialised once, then C-driven slicing. */
694
- VALUE sorted = rb_funcall(obj_call_axis(self, id_sort, LONG2NUM(axis)), id_copy, 0);
695
+ volatile VALUE sorted = rb_funcall(obj_call_axis(self, id_sort, LONG2NUM(axis)), id_copy, 0);
695
696
  VALUE result = rb_ary_new_capa(npers);
696
697
  for ( long i = 0; i < npers; i++ ) {
697
698
  double p = NUM2DBL(rb_ary_entry(pers, i));
698
699
  rb_ary_push(result, pct_object_one_sorted(sorted, axis, n, p, method));
699
700
  }
700
- RB_GC_GUARD(sorted);
701
701
  return result;
702
702
  }
703
703
 
@@ -708,7 +708,7 @@ percentile_object_axis (VALUE self, VALUE pers, long axis, VALUE method)
708
708
  0 (or < min_count) yields an UNDEF cell (fill_value when given); the mask
709
709
  is created lazily, matching the numeric lane. */
710
710
  static VALUE
711
- pct_object_axis_masked_one (VALUE self, VALUE sorted, long axis, double p,
711
+ pct_object_axis_masked_one (VALUE self, volatile VALUE sorted, long axis, double p,
712
712
  VALUE method, long OUTER, long M, long INNER,
713
713
  const boolean8_t *sm, long min_count,
714
714
  VALUE fill_value, int keep_axis)
@@ -716,7 +716,7 @@ pct_object_axis_masked_one (VALUE self, VALUE sorted, long axis, double p,
716
716
  int8_t ax = (int8_t) axis;
717
717
  /* `sorted` is an entity of self's shape (the storage, when self is a
718
718
  Face); the answer is built on it and lifted back by the caller. */
719
- VALUE out = rb_ca_new_reduced(sorted, &ax, 1, CA_OBJECT, keep_axis);
719
+ volatile VALUE out = rb_ca_new_reduced(sorted, &ax, 1, CA_OBJECT, keep_axis);
720
720
  CArray *co;
721
721
  GetCArray(out, co);
722
722
  CArray *cs;
@@ -763,7 +763,7 @@ pct_object_axis_masked_one (VALUE self, VALUE sorted, long axis, double p,
763
763
  material, then each p is a per-fiber select. Returns an Array of reduced
764
764
  CA_OBJECT CArrays, one per p. */
765
765
  static VALUE
766
- percentile_object_axis_masked (VALUE self, VALUE pers, long axis, VALUE method,
766
+ percentile_object_axis_masked (volatile VALUE self, VALUE pers, long axis, VALUE method,
767
767
  long min_count, VALUE fill_value, int keep_axis)
768
768
  {
769
769
  CArray *ca;
@@ -774,13 +774,13 @@ percentile_object_axis_masked (VALUE self, VALUE pers, long axis, VALUE method,
774
774
  if ( M == 0 ) {
775
775
  VALUE res = rb_ary_new_capa(npers);
776
776
  for ( long i = 0; i < npers; i++ )
777
- rb_ary_push(res, mp_axis_all_masked(self, axis, keep_axis));
777
+ rb_ary_push(res, mp_axis_all_masked(self, axis, fill_value, keep_axis));
778
778
  return res;
779
779
  }
780
780
 
781
781
  /* A Face (a string array) is sorted as itself and read through its
782
782
  storage, whose cells are the values. */
783
- VALUE sorted = rb_funcall(obj_call_axis(self, id_sort, LONG2NUM(axis)), id_copy, 0);
783
+ volatile VALUE sorted = rb_funcall(obj_call_axis(self, id_sort, LONG2NUM(axis)), id_copy, 0);
784
784
  int is_face = ca_is_face(ca);
785
785
  if ( is_face ) sorted = rb_ca_strip_face_value(sorted);
786
786
  CArray *cs;
@@ -798,7 +798,6 @@ percentile_object_axis_masked (VALUE self, VALUE pers, long axis, VALUE method,
798
798
  fill_value, keep_axis);
799
799
  rb_ary_push(result, is_face ? ca_face_relift_entity(self, one) : one);
800
800
  }
801
- RB_GC_GUARD(sorted);
802
801
  return result;
803
802
  }
804
803
 
@@ -816,11 +815,11 @@ median_object_axis_masked (VALUE self, long axis, long min_count,
816
815
 
817
816
  /* [object] flat percentile: mask-strip / min_count, flatten, axis-0. */
818
817
  static VALUE
819
- percentile_object_flat (VALUE self, VALUE pers, long min_count, VALUE fill_value,
818
+ percentile_object_flat (volatile VALUE self, VALUE pers, long min_count, VALUE fill_value,
820
819
  VALUE method)
821
820
  {
822
821
  long npers = RARRAY_LEN(pers);
823
- VALUE src = self;
822
+ volatile VALUE src = self;
824
823
  int filled = 0;
825
824
  if ( RTEST(rb_ca_has_mask(self)) ) {
826
825
  CArray *mc; GetCArray(self, mc);
@@ -839,9 +838,8 @@ percentile_object_flat (VALUE self, VALUE pers, long min_count, VALUE fill_value
839
838
  for ( long i = 0; i < npers; i++ ) rb_ary_push(result, fill);
840
839
  return result;
841
840
  }
842
- VALUE flat = rb_ca_flatten(src);
841
+ volatile VALUE flat = rb_ca_flatten(src);
843
842
  VALUE r = percentile_object_axis(flat, pers, 0, method);
844
- RB_GC_GUARD(flat);
845
843
  return r;
846
844
  }
847
845
 
@@ -852,14 +850,14 @@ static VALUE median_axis_masked (VALUE self, long axis, long min_count,
852
850
  /* [entry] median(axis:, min_count:, fill_value:, keep_axis:). Dispatches
853
851
  fixlen -> reject, object -> object lane, numeric -> numeric lane. */
854
852
  static VALUE
855
- rb_ca_median_m_body (int argc, VALUE *argv, VALUE self)
853
+ rb_ca_median_m_body (int argc, VALUE *argv, volatile VALUE self)
856
854
  {
857
855
  VALUE ropt = rb_pop_options(&argc, &argv);
858
856
  VALUE raxis = Qnil, rmin_count = INT2FIX(0), fill_value = Qnil, rkeep = Qfalse;
859
857
  rb_scan_options(ropt, "axis,min_count,fill_value,keep_axis",
860
858
  &raxis, &rmin_count, &fill_value, &rkeep);
861
859
  if ( argc != 0 )
862
- rb_raise(rb_eArgError, "median: no positional args accepted (got %d)", argc);
860
+ rb_raise(rb_eArgError, "median: takes no positional arguments (given %d); give the axis as a keyword, e.g. a.median(axis: 0)", argc);
863
861
 
864
862
  CArray *ca;
865
863
  GetCArray(self, ca);
@@ -899,12 +897,14 @@ rb_ca_median_m_body (int argc, VALUE *argv, VALUE self)
899
897
  else if ( ! NIL_P(fill_value) && ! is_obj ) {
900
898
  fill_value = ca_fill_as(fill_value, CA_FLOAT64);
901
899
  }
902
- int keep_axis = RTEST(rkeep);
900
+ int keep_axis = ca_bool_arg(rkeep, "keep_axis");
903
901
 
904
902
  if ( !NIL_P(raxis) ) {
905
903
  long axis = rb_ca_normalize_axis_value(self, raxis, "median");
906
904
  long n = (long) ca->dim[axis];
907
- if ( n == 0 ) return mp_axis_all_masked(self, axis, keep_axis);
905
+ if ( n == 0 )
906
+ return mp_axis_answer(self, mp_axis_all_masked(self, axis, fill_value, keep_axis),
907
+ keep_axis);
908
908
  /* mask / min_count / fill_value require the per-fiber select (each fiber
909
909
  has its own n_present); the plain partition path assumes a uniform n.
910
910
  Switch ca_fast_path(CA_SC_MEDIAN_PARTITION): off, the per-fiber select. */
@@ -919,10 +919,14 @@ rb_ca_median_m_body (int argc, VALUE *argv, VALUE self)
919
919
  : median_axis(self, axis, n, keep_axis);
920
920
  return mp_axis_answer(self, res, keep_axis);
921
921
  }
922
- /* self may be the float64 view made above, read through its CArray */
923
- CA_RETURN_KEEPING(self,
924
- is_obj ? median_object_flat(self, min_count, fill_value, keep_axis)
925
- : median_flat(self, min_count, fill_value, keep_axis));
922
+ /* self may be the float64 view made above, read through its CArray:
923
+ received in a volatile variable, so that self is kept past the call */
924
+ {
925
+ volatile VALUE answer =
926
+ is_obj ? median_object_flat(self, min_count, fill_value, keep_axis)
927
+ : median_flat(self, min_count, fill_value, keep_axis);
928
+ return answer;
929
+ }
926
930
  }
927
931
 
928
932
  /* =====================================================================
@@ -1044,7 +1048,7 @@ pct_fill_masked (CArray *co, int8_t dt, const char *base, const boolean8_t *sm,
1044
1048
 
1045
1049
  /* [numeric] single p via one partition_copy -> reduced CA_FLOAT64. */
1046
1050
  static VALUE
1047
- pct_axis_one_partition (VALUE self, long axis, long n, double p,
1051
+ pct_axis_one_partition (volatile VALUE self, long axis, long n, double p,
1048
1052
  VALUE method, int keep_axis)
1049
1053
  {
1050
1054
  CArray *ca;
@@ -1063,19 +1067,18 @@ pct_axis_one_partition (VALUE self, long axis, long n, double p,
1063
1067
  keep one partition correct, partition at the index we actually read:
1064
1068
  if UPPER_ONLY we need sorted[k+1] -> partition at k, min-upper gives
1065
1069
  it. pct_fill handles that via from_sorted==0. */
1066
- VALUE pp = rb_ca_partition_copy_c(self, LONG2NUM(k), LONG2NUM(axis));
1070
+ volatile VALUE pp = rb_ca_partition_copy_c(self, LONG2NUM(k), LONG2NUM(axis));
1067
1071
  CArray *cp;
1068
1072
  GetCArray(pp, cp);
1069
1073
 
1070
1074
  int8_t ax = (int8_t) axis;
1071
- VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1075
+ volatile VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1072
1076
  CArray *co;
1073
1077
  GetCArray(out, co);
1074
1078
  long OUTER, M, INNER;
1075
1079
  mp_geometry(ca, axis, &OUTER, &M, &INNER);
1076
1080
  pct_fill((double *) co->ptr, dt, (const char *) cp->ptr, 0,
1077
1081
  OUTER, M, INNER, n, k, r, method, need);
1078
- RB_GC_GUARD(pp);
1079
1082
  return out;
1080
1083
  }
1081
1084
 
@@ -1083,7 +1086,7 @@ pct_axis_one_partition (VALUE self, long axis, long n, double p,
1083
1086
  sort_copy funcall is the only Ruby-surface call left on this lane; it
1084
1087
  could be replaced by a C-callable sort_copy twin like partition_copy_c.) */
1085
1088
  static VALUE
1086
- pct_axis_multi (VALUE self, VALUE pers_ary, long axis, long n,
1089
+ pct_axis_multi (volatile VALUE self, VALUE pers_ary, long axis, long n,
1087
1090
  VALUE method, int keep_axis)
1088
1091
  {
1089
1092
  CArray *ca;
@@ -1096,7 +1099,7 @@ pct_axis_multi (VALUE self, VALUE pers_ary, long axis, long n,
1096
1099
  VALUE kw = rb_hash_new();
1097
1100
  rb_hash_aset(kw, ID2SYM(id_axis), LONG2NUM(axis));
1098
1101
  VALUE sc_argv[1] = { kw };
1099
- VALUE sorted = rb_funcallv_kw(self, id_sort_copy, 1, sc_argv, RB_PASS_KEYWORDS);
1102
+ volatile VALUE sorted = rb_funcallv_kw(self, id_sort_copy, 1, sc_argv, RB_PASS_KEYWORDS);
1100
1103
  CArray *cs;
1101
1104
  GetCArray(sorted, cs);
1102
1105
 
@@ -1114,20 +1117,19 @@ pct_axis_multi (VALUE self, VALUE pers_ary, long axis, long n,
1114
1117
  pct_need_t need = pct_need(method, k, r, n);
1115
1118
 
1116
1119
  int8_t ax = (int8_t) axis;
1117
- VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1120
+ volatile VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1118
1121
  CArray *co;
1119
1122
  GetCArray(out, co);
1120
1123
  pct_fill((double *) co->ptr, dt, (const char *) cs->ptr, 1,
1121
1124
  OUTER, M, INNER, n, k, r, method, need);
1122
1125
  rb_ary_push(result, out);
1123
1126
  }
1124
- RB_GC_GUARD(sorted);
1125
1127
  return result;
1126
1128
  }
1127
1129
 
1128
1130
  /* [numeric] per-axis percentile: 1 p -> partition, many p -> shared sort. */
1129
1131
  static VALUE
1130
- pct_axis (VALUE self, VALUE pers_ary, long axis, VALUE method, int keep_axis)
1132
+ pct_axis (volatile VALUE self, VALUE pers_ary, long axis, VALUE method, int keep_axis)
1131
1133
  {
1132
1134
  CArray *ca;
1133
1135
  GetCArray(self, ca);
@@ -1138,7 +1140,7 @@ pct_axis (VALUE self, VALUE pers_ary, long axis, VALUE method, int keep_axis)
1138
1140
  /* zero-length axis: every requested p reduces to an all-UNDEF cell. */
1139
1141
  VALUE res = rb_ary_new_capa(npers);
1140
1142
  for ( long i = 0; i < npers; i++ )
1141
- rb_ary_push(res, mp_axis_all_masked(self, axis, keep_axis));
1143
+ rb_ary_push(res, mp_axis_all_masked(self, axis, Qnil, keep_axis));
1142
1144
  return res;
1143
1145
  }
1144
1146
 
@@ -1155,7 +1157,7 @@ pct_axis (VALUE self, VALUE pers_ary, long axis, VALUE method, int keep_axis)
1155
1157
  material, then each p is a per-fiber select honouring that fiber's own
1156
1158
  n_present. Returns an Array of reduced CA_FLOAT64 CArrays, one per p. */
1157
1159
  static VALUE
1158
- pct_axis_masked (VALUE self, VALUE pers_ary, long axis, VALUE method,
1160
+ pct_axis_masked (volatile VALUE self, VALUE pers_ary, long axis, VALUE method,
1159
1161
  long min_count, VALUE fill_value, int keep_axis)
1160
1162
  {
1161
1163
  CArray *ca;
@@ -1167,7 +1169,7 @@ pct_axis_masked (VALUE self, VALUE pers_ary, long axis, VALUE method,
1167
1169
  if ( M == 0 ) {
1168
1170
  VALUE res = rb_ary_new_capa(npers);
1169
1171
  for ( long i = 0; i < npers; i++ )
1170
- rb_ary_push(res, mp_axis_all_masked(self, axis, keep_axis));
1172
+ rb_ary_push(res, mp_axis_all_masked(self, axis, fill_value, keep_axis));
1171
1173
  return res;
1172
1174
  }
1173
1175
 
@@ -1175,7 +1177,7 @@ pct_axis_masked (VALUE self, VALUE pers_ary, long axis, VALUE method,
1175
1177
  VALUE kw = rb_hash_new();
1176
1178
  rb_hash_aset(kw, ID2SYM(id_axis), LONG2NUM(axis));
1177
1179
  VALUE sc_argv[1] = { kw };
1178
- VALUE sorted = rb_funcallv_kw(self, id_sort_copy, 1, sc_argv, RB_PASS_KEYWORDS);
1180
+ volatile VALUE sorted = rb_funcallv_kw(self, id_sort_copy, 1, sc_argv, RB_PASS_KEYWORDS);
1179
1181
  CArray *cs;
1180
1182
  GetCArray(sorted, cs);
1181
1183
  const boolean8_t *sm = cs->mask ? (const boolean8_t *) cs->mask->ptr : NULL;
@@ -1189,14 +1191,13 @@ pct_axis_masked (VALUE self, VALUE pers_ary, long axis, VALUE method,
1189
1191
  for ( long i = 0; i < npers; i++ ) {
1190
1192
  double p = NUM2DBL(rb_ary_entry(pers_ary, i));
1191
1193
  int8_t ax = (int8_t) axis;
1192
- VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1194
+ volatile VALUE out = rb_ca_new_reduced(self, &ax, 1, CA_FLOAT64, keep_axis);
1193
1195
  CArray *co;
1194
1196
  GetCArray(out, co);
1195
1197
  pct_fill_masked(co, dt, (const char *) cs->ptr, sm,
1196
1198
  OUTER, MM, INNER, p, method, min_count, is_undef, fillv);
1197
1199
  rb_ary_push(result, out);
1198
1200
  }
1199
- RB_GC_GUARD(sorted);
1200
1201
  return result;
1201
1202
  }
1202
1203
 
@@ -1216,11 +1217,11 @@ median_axis_masked (VALUE self, long axis, long min_count, VALUE fill_value,
1216
1217
  /* [numeric] flat percentile -> array of scalars (or keep_axis [1..1]
1217
1218
  entities), one per requested p. */
1218
1219
  static VALUE
1219
- pct_flat (VALUE self, VALUE pers_ary, long min_count, VALUE fill_value,
1220
+ pct_flat (volatile VALUE self, VALUE pers_ary, long min_count, VALUE fill_value,
1220
1221
  VALUE method, int keep_axis)
1221
1222
  {
1222
1223
  long npers = RARRAY_LEN(pers_ary);
1223
- VALUE src = self;
1224
+ volatile VALUE src = self;
1224
1225
  int masked_out = 0, is_undef = 0;
1225
1226
  double fillv = 0.0;
1226
1227
 
@@ -1256,7 +1257,7 @@ pct_flat (VALUE self, VALUE pers_ary, long min_count, VALUE fill_value,
1256
1257
  }
1257
1258
 
1258
1259
  /* flatten -> 1-D, reduce on axis 0, extract op[0] per p. */
1259
- VALUE flat = rb_ca_flatten(src);
1260
+ volatile VALUE flat = rb_ca_flatten(src);
1260
1261
  VALUE per_axis = pct_axis(flat, pers_ary, 0, method, 0); /* array of [1] CArrays */
1261
1262
  for ( long i = 0; i < npers; i++ ) {
1262
1263
  CArray *r1;
@@ -1265,21 +1266,21 @@ pct_flat (VALUE self, VALUE pers_ary, long min_count, VALUE fill_value,
1265
1266
  VALUE out = keep_axis ? mp_keep_axis_full_f64(self, v, 0) : rb_float_new(v);
1266
1267
  rb_ary_push(result, out);
1267
1268
  }
1268
- RB_GC_GUARD(flat);
1269
1269
  return result;
1270
1270
  }
1271
1271
 
1272
- /* [shared] normalise + validate the p list: unwrap a single Array/CArray
1273
- arg, reject empty, require each p be Numeric in [0,100]. */
1272
+ /* [shared] read the p arguments. The form the caller wrote p in decides
1273
+ the form of the answer; the private class methods CArray.percentile_arguments
1274
+ and CArray.percentile_answer (Ruby) hold that rule for the core and the
1275
+ iterators alike. Returns the p values as a flat
1276
+ Array and sets *form. Each p must be Numeric in [0,100]. */
1274
1277
  static VALUE
1275
- pct_flatten_validate_pers (VALUE pers)
1278
+ pct_read_pers (int argc, VALUE *argv, VALUE *form)
1276
1279
  {
1277
- if ( RARRAY_LEN(pers) == 1 ) {
1278
- VALUE first = rb_ary_entry(pers, 0);
1279
- if ( RB_TYPE_P(first, T_ARRAY) ) pers = first;
1280
- else if ( rb_obj_is_kind_of(first, rb_cCArray) )
1281
- pers = rb_funcall(first, rb_intern("to_a"), 0);
1282
- }
1280
+ VALUE read = rb_funcall(rb_cCArray, rb_intern("percentile_arguments"), 1,
1281
+ rb_ary_new_from_values(argc, argv));
1282
+ VALUE pers = rb_ary_entry(read, 0);
1283
+ *form = rb_ary_entry(read, 1);
1283
1284
  long len = RARRAY_LEN(pers);
1284
1285
  if ( len == 0 )
1285
1286
  rb_raise(rb_eArgError, "percentile: at least one p value required");
@@ -1296,11 +1297,11 @@ pct_flatten_validate_pers (VALUE pers)
1296
1297
  return pers;
1297
1298
  }
1298
1299
 
1299
- /* [entry] percentile(*pers, axis:, min_count:, fill_value:, method:,
1300
+ /* [entry] percentile(p, axis:, min_count:, fill_value:, method:,
1300
1301
  keep_axis:). Dispatches fixlen -> reject, object -> object lane,
1301
1302
  numeric -> numeric lane. */
1302
1303
  static VALUE
1303
- rb_ca_percentile_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
1304
+ rb_ca_percentile_kw (int argc, VALUE *argv, VALUE ropt, volatile VALUE self)
1304
1305
  {
1305
1306
  VALUE raxis = Qnil, rmin_count = INT2FIX(0), fill_value = Qnil,
1306
1307
  method = sym_linear, rkeep = Qfalse;
@@ -1344,21 +1345,19 @@ rb_ca_percentile_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
1344
1345
  fill_value = ca_fill_as(fill_value, CA_FLOAT64);
1345
1346
  }
1346
1347
 
1347
- VALUE pers = pct_flatten_validate_pers(rb_ary_new_from_values(argc, argv));
1348
+ VALUE form;
1349
+ VALUE pers = pct_read_pers(argc, argv, &form);
1348
1350
  pct_validate_method(method);
1349
- int keep_axis = RTEST(rkeep);
1350
- int single_p = (RARRAY_LEN(pers) == 1);
1351
+ int keep_axis = ca_bool_arg(rkeep, "keep_axis");
1351
1352
 
1352
1353
  /* numeric lane bakes keep_axis into the reduced output; object lane
1353
1354
  returns unwrapped results and is keep-wrapped here (insert_axis for
1354
1355
  the per-axis form, obj_keep_axis_full for the flat form) -- matching
1355
1356
  production's structure for arbitrary-object results.
1356
1357
 
1357
- Return shape: multi-p returns Array<Float> (flat) or Array<CArray>
1358
- (per-axis); single-p unwraps the length-1 Array so the caller gets
1359
- Float / CArray directly. A single Array or CArray p argument is
1360
- flattened first and follows the same rule (length 1 unwraps). */
1361
- VALUE result;
1358
+ The lanes answer an Array with one answer per p; the form the caller
1359
+ wrote p in decides what is returned (CArray.percentile_answer). */
1360
+ volatile VALUE result;
1362
1361
  if ( !NIL_P(raxis) ) {
1363
1362
  long axis = rb_ca_normalize_axis_value(self, raxis, NULL);
1364
1363
  /* mask / min_count / fill_value require the per-fiber select (each fiber
@@ -1403,10 +1402,12 @@ rb_ca_percentile_kw (int argc, VALUE *argv, VALUE ropt, VALUE self)
1403
1402
  }
1404
1403
  }
1405
1404
  /* self may be the float64 view made above, read through its CArray */
1406
- RB_GC_GUARD(self);
1407
-
1408
- if ( single_p ) return rb_ary_entry(result, 0);
1409
- return result;
1405
+ {
1406
+ VALUE dt = ID2SYM(rb_intern(is_obj ? "object" : "float64"));
1407
+ VALUE answer = rb_funcall(rb_cCArray, rb_intern("percentile_answer"), 3,
1408
+ result, form, dt);
1409
+ return answer;
1410
+ }
1410
1411
  }
1411
1412
 
1412
1413
  static VALUE
@@ -1416,22 +1417,27 @@ rb_ca_percentile_m_body (int argc, VALUE *argv, VALUE self)
1416
1417
  return rb_ca_percentile_kw(argc, argv, ropt, self);
1417
1418
  }
1418
1419
 
1419
- /* [entry] quantile(axis:, keep_axis:) = percentile(0, 25, 50, 75, 100, ...).
1420
- Return shape: Array<Float> len 5 (flat) or Array<CArray> len 5 (per-axis),
1421
- matching percentile's multi-p wrapping since we pass 5 fixed p values. */
1420
+ /* [entry] quantile(axis:, keep_axis:, min_count:, fill_value:) =
1421
+ percentile([0, 25, 50, 75, 100], ...), with the same keywords.
1422
+ The five p values go in as one Array, so the answer is an Array of five:
1423
+ Floats (flat) or CArrays (per-axis). */
1422
1424
  static VALUE
1423
1425
  rb_ca_quantile_m_body (int argc, VALUE *argv, VALUE self)
1424
1426
  {
1425
1427
  VALUE ropt = rb_pop_options(&argc, &argv);
1426
1428
  if ( argc != 0 )
1427
- rb_raise(rb_eArgError, "quantile: no positional args accepted (got %d)", argc);
1428
- VALUE raxis = Qnil, rkeep = Qfalse;
1429
- rb_scan_options(ropt, "axis,keep_axis", &raxis, &rkeep);
1429
+ rb_raise(rb_eArgError, "quantile: takes no positional arguments (got %d); for the value at p percent, use percentile(p)", argc);
1430
+ VALUE raxis = Qnil, rkeep = Qfalse, rmin = Qnil, rfill = Qnil;
1431
+ rb_scan_options(ropt, "axis,keep_axis,min_count,fill_value",
1432
+ &raxis, &rkeep, &rmin, &rfill);
1430
1433
  VALUE opts = rb_hash_new();
1431
1434
  if ( !NIL_P(raxis) ) rb_hash_aset(opts, ID2SYM(rb_intern("axis")), raxis);
1432
1435
  rb_hash_aset(opts, ID2SYM(rb_intern("keep_axis")), rkeep);
1433
- VALUE pa[5] = { INT2FIX(0), INT2FIX(25), INT2FIX(50), INT2FIX(75), INT2FIX(100) };
1434
- return rb_ca_percentile_kw(5, pa, opts, self);
1436
+ if ( !NIL_P(rmin) ) rb_hash_aset(opts, ID2SYM(rb_intern("min_count")), rmin);
1437
+ if ( !NIL_P(rfill) ) rb_hash_aset(opts, ID2SYM(rb_intern("fill_value")), rfill);
1438
+ VALUE pa[1] = { rb_ary_new_from_args(5, INT2FIX(0), INT2FIX(25), INT2FIX(50),
1439
+ INT2FIX(75), INT2FIX(100)) };
1440
+ return rb_ca_percentile_kw(1, pa, opts, self);
1435
1441
  }
1436
1442
 
1437
1443