carray 3.0.5 → 3.0.7

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 (173) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +343 -0
  3. data/README.md +25 -4
  4. data/carray.gemspec +3 -2
  5. data/ext/ca_axis_dispatch.c +6 -0
  6. data/ext/ca_axis_group.c +7 -7
  7. data/ext/ca_binop_dispatch.c +8 -0
  8. data/ext/ca_binop_dispatch.h +1 -0
  9. data/ext/ca_categorical_iterator.c +14 -14
  10. data/ext/ca_kernel_iterator.c +116 -231
  11. data/ext/ca_kernel_iterator.h +416 -258
  12. data/ext/ca_obj_array.c +4 -4
  13. data/ext/ca_obj_bincmp.c +28 -4
  14. data/ext/ca_obj_binop.c +46 -12
  15. data/ext/ca_obj_bitarray.c +2 -4
  16. data/ext/ca_obj_bitfield.c +88 -4
  17. data/ext/ca_obj_byte_swap.c +4 -4
  18. data/ext/ca_obj_const_string.c +422 -257
  19. data/ext/ca_obj_face.c +73 -33
  20. data/ext/ca_obj_fake.c +114 -13
  21. data/ext/ca_obj_farray.c +7 -1
  22. data/ext/ca_obj_fixlen_string.c +51 -17
  23. data/ext/ca_obj_grid.c +179 -19
  24. data/ext/ca_obj_meld.c +74 -118
  25. data/ext/ca_obj_moncmp.c +9 -1
  26. data/ext/ca_obj_monop.c +100 -36
  27. data/ext/ca_obj_object.c +63 -29
  28. data/ext/ca_obj_reduce.c +17 -5
  29. data/ext/ca_obj_refer.c +162 -23
  30. data/ext/ca_obj_remap.c +69 -10
  31. data/ext/ca_obj_repeat.c +1 -1
  32. data/ext/ca_obj_roll.c +3 -4
  33. data/ext/ca_obj_select.c +12 -7
  34. data/ext/ca_obj_select_axis.c +9 -6
  35. data/ext/ca_obj_shift.c +1 -0
  36. data/ext/ca_obj_stack.c +315 -116
  37. data/ext/ca_obj_stride.c +177 -53
  38. data/ext/ca_obj_string.c +20 -4
  39. data/ext/ca_obj_tile.c +3 -4
  40. data/ext/ca_obj_triop.c +24 -13
  41. data/ext/ca_obj_window.c +23 -15
  42. data/ext/ca_op_ipower.c +71 -23
  43. data/ext/ca_op_powi.h +6 -29
  44. data/ext/ca_sort_kernels.h +3 -0
  45. data/ext/ca_sweep_engine.c +90 -23
  46. data/ext/ca_sweep_engine.h +5 -6
  47. data/ext/caframe_csv_reader.c +2 -2
  48. data/ext/carray.h +5 -2
  49. data/ext/carray_access.c +125 -103
  50. data/ext/carray_address_basis.c +209 -1
  51. data/ext/carray_attribute.c +8 -0
  52. data/ext/carray_broadcast.c +5 -6
  53. data/ext/carray_build_flags.h +1 -1
  54. data/ext/carray_call_cfunc.c +5 -8
  55. data/ext/carray_cast.c +319 -48
  56. data/ext/carray_cast_func.rb +37 -5
  57. data/ext/carray_conversion.c +65 -7
  58. data/ext/carray_copy.c +24 -0
  59. data/ext/carray_core.c +874 -72
  60. data/ext/carray_count.c +28 -9
  61. data/ext/carray_element.c +22 -12
  62. data/ext/carray_factorize.c +11 -11
  63. data/ext/carray_histogram.c +27 -5
  64. data/ext/carray_index_classifier.c +7 -5
  65. data/ext/carray_internal.h +241 -12
  66. data/ext/carray_kernels_bincmp.c +2007 -3
  67. data/ext/carray_kernels_binop.c +25502 -5336
  68. data/ext/carray_kernels_init.c +22 -22
  69. data/ext/carray_kernels_monop.c +10739 -3771
  70. data/ext/carray_kernels_reduce_aggregate.c +4429 -702
  71. data/ext/carray_kernels_reduce_boolean.c +1533 -15
  72. data/ext/carray_kernels_reduce_cumulative.c +1808 -248
  73. data/ext/carray_kernels_reduce_extreme.c +4508 -511
  74. data/ext/carray_kernels_reduce_variance.c +2284 -152
  75. data/ext/carray_kernels_scan.c +240 -240
  76. data/ext/carray_kernels_search.c +4211 -264
  77. data/ext/carray_kernels_sort.c +367 -450
  78. data/ext/carray_lazy.c +63 -1
  79. data/ext/carray_loop.c +10 -3
  80. data/ext/carray_mask.c +247 -149
  81. data/ext/carray_median_percentile.c +17 -12
  82. data/ext/carray_memory_view.c +54 -29
  83. data/ext/carray_operator.c +219 -55
  84. data/ext/carray_order.c +55 -11
  85. data/ext/carray_partition.c +1 -1
  86. data/ext/carray_random.c +5 -2
  87. data/ext/carray_round.c +24 -1
  88. data/ext/carray_scatter.c +4 -0
  89. data/ext/carray_segment.c +1 -1
  90. data/ext/carray_slab.c +110 -25
  91. data/ext/carray_sort.c +7 -4
  92. data/ext/carray_sort_kernel.c +24 -0
  93. data/ext/carray_struct.c +21 -6
  94. data/ext/carray_test.c +154 -53
  95. data/ext/carray_time_text.c +8 -2
  96. data/ext/carray_utils.c +124 -54
  97. data/ext/extconf.rb +11 -0
  98. data/ext/mk_call_cfunc.rb +6 -11
  99. data/ext/mkkernel.rb +383 -121
  100. data/ext/ruby_carray.c +4 -1
  101. data/ext/version.h +4 -4
  102. data/lib/carray/attribute.rb +25 -1
  103. data/lib/carray/autoload_carray.rb +3 -3
  104. data/lib/carray/autoload_method_extension.rb +5 -3
  105. data/lib/carray/axis_group.rb +22 -13
  106. data/lib/carray/basics.rb +27 -9
  107. data/lib/carray/bincount_nd.rb +8 -2
  108. data/lib/carray/block_iterator.rb +65 -24
  109. data/lib/carray/categorical.rb +27 -16
  110. data/lib/carray/categorical_iterator.rb +4 -3
  111. data/lib/carray/complex.rb +2 -2
  112. data/lib/carray/conditional.rb +55 -6
  113. data/lib/carray/const_string.rb +74 -0
  114. data/lib/carray/construct.rb +2 -2
  115. data/lib/carray/core_extensions.rb +19 -68
  116. data/lib/carray/data_type_extension.rb +1 -0
  117. data/lib/carray/fixlen_string.rb +83 -1
  118. data/lib/carray/frame/column_verbs.rb +102 -16
  119. data/lib/carray/frame/concat.rb +50 -6
  120. data/lib/carray/frame/convert.rb +19 -0
  121. data/lib/carray/frame/csv_parser.rb +45 -36
  122. data/lib/carray/frame/describe.rb +4 -2
  123. data/lib/carray/frame/frame.rb +436 -83
  124. data/lib/carray/frame/group.rb +731 -78
  125. data/lib/carray/frame/io.rb +216 -73
  126. data/lib/carray/frame/join.rb +166 -43
  127. data/lib/carray/frame/records.rb +9 -7
  128. data/lib/carray/frame/reshape.rb +109 -11
  129. data/lib/carray/frame/sort.rb +57 -9
  130. data/lib/carray/frame/verbs.rb +448 -60
  131. data/lib/carray/frame/windows.rb +71 -9
  132. data/lib/carray/fuse_source.rb +4 -2
  133. data/lib/carray/inspect.rb +7 -2
  134. data/lib/carray/iterator.rb +40 -0
  135. data/lib/carray/key_windows.rb +10 -4
  136. data/lib/carray/lazy.rb +28 -24
  137. data/lib/carray/mask_gap_fill.rb +37 -17
  138. data/lib/carray/meld_reduce.rb +1 -1
  139. data/lib/carray/methods/bin.rb +3 -0
  140. data/lib/carray/methods/choose.rb +3 -2
  141. data/lib/carray/methods/composition.rb +1 -1
  142. data/lib/carray/methods/factorize.rb +2 -0
  143. data/lib/carray/methods/gather_nd.rb +4 -1
  144. data/lib/carray/methods/is_in.rb +23 -3
  145. data/lib/carray/methods/locate_addr.rb +63 -12
  146. data/lib/carray/methods/meshgrid.rb +3 -1
  147. data/lib/carray/methods/repeat.rb +11 -1
  148. data/lib/carray/record_order.rb +35 -6
  149. data/lib/carray/runtime.rb +57 -8
  150. data/lib/carray/segment_iterator.rb +42 -24
  151. data/lib/carray/serialize.rb +56 -17
  152. data/lib/carray/slab_iterator.rb +1 -1
  153. data/lib/carray/stack.rb +21 -9
  154. data/lib/carray/string.rb +6 -6
  155. data/lib/carray/string_operation_extension.rb +25 -4
  156. data/lib/carray/struct_builder.rb +4 -1
  157. data/lib/carray/time.rb +195 -46
  158. data/lib/carray/window_iterator.rb +64 -16
  159. data/yard-stubs/ca_obj_array.rb +26 -1
  160. data/yard-stubs/ca_obj_bitfield.rb +4 -1
  161. data/yard-stubs/ca_obj_refer.rb +3 -0
  162. data/yard-stubs/ca_obj_window.rb +2 -1
  163. data/yard-stubs/carray_access.rb +4 -2
  164. data/yard-stubs/carray_cast.rb +14 -5
  165. data/yard-stubs/carray_conversion.rb +8 -2
  166. data/yard-stubs/carray_count.rb +6 -3
  167. data/yard-stubs/carray_mask.rb +20 -1
  168. data/yard-stubs/carray_math.rb +36 -16
  169. data/yard-stubs/carray_memory_view.rb +7 -0
  170. data/yard-stubs/carray_order.rb +6 -0
  171. data/yard-stubs/carray_scatter.rb +2 -1
  172. data/yard-stubs/carray_test.rb +23 -7
  173. metadata +17 -3
@@ -24,6 +24,8 @@
24
24
  #ifndef CARRAY_INTERNAL_H
25
25
  #define CARRAY_INTERNAL_H
26
26
 
27
+ #include "ruby/thread_native.h" /* rb_nativethread_lock_t */
28
+
27
29
  #include "carray.h"
28
30
  #include <math.h> /* isnan, for CA_OBJ_ISNAN */
29
31
 
@@ -35,6 +37,18 @@
35
37
  CArray#initialize_dup / #initialize_clone. */
36
38
  void ca_keep_chain_flags (CArray *view, CArray *src);
37
39
 
40
+ /* A reshape / flatten folded onto the root of its source (CA_FLAG_FOLDED,
41
+ ca_obj_refer.c). The source is not on the view's C parent chain, so a
42
+ read-only flag it takes after the view is made would not be seen by
43
+ ca_is_readonly. The view records its source here; ca_is_readonly asks the
44
+ source of a view that carries the flag. The source stays alive while the
45
+ view does (the view's @parent). ca_fold_source answers NULL for a view
46
+ with no record; ca_fold_forget drops the record when the view is freed. */
47
+ #define CA_FLAG_FOLDED 8192
48
+ void ca_fold_note_source (CArray *view, CArray *src);
49
+ CArray *ca_fold_source (CArray *view);
50
+ void ca_fold_forget (CArray *view);
51
+
38
52
  /* ---- bulk bit pack / unpack (ca_obj_bitarray.c) --------------------------
39
53
 
40
54
  Drive the CABitarray whole-view xfer_all / xfer_addrs fast paths, which
@@ -73,6 +87,7 @@ void ca_lazy_arena_enter (void);
73
87
  void ca_lazy_arena_exit (void);
74
88
  void *ca_lazy_arena_acquire (ca_size_t bytes);
75
89
  void *ca_lazy_arena_acquire_object (ca_size_t n_elements);
90
+ void ca_lazy_arena_untag_objects (void *ptr);
76
91
  void ca_lazy_arena_release (void *ptr);
77
92
  VALUE ca_lazy_arena_protect (VALUE (*body)(VALUE),
78
93
  VALUE (*cleanup)(VALUE), VALUE arg);
@@ -133,6 +148,18 @@ ca_boolean_normalize_bytes (void *ptr, ca_size_t n)
133
148
  }
134
149
  }
135
150
 
151
+ /* A buffer for n cells of data_type, each bytes long: object cells start
152
+ as nil. */
153
+ static inline char *
154
+ ca_alloc_cells_of (int8_t data_type, ca_size_t elements, ca_size_t bytes)
155
+ {
156
+ char *p = (char *) xmalloc(elements > 0 ? elements * bytes : 1);
157
+ if ( data_type == CA_OBJECT ) {
158
+ ca_object_cells_clear(p, elements);
159
+ }
160
+ return p;
161
+ }
162
+
136
163
  static inline char *
137
164
  ca_alloc_cells (const CArray *ca)
138
165
  {
@@ -188,9 +215,24 @@ ca_mask_from_bytes (CArray *ca, const boolean8_t *m)
188
215
 
189
216
  void ca_attach_all (CArray **list, int32_t n);
190
217
  void ca_sync_all (CArray **list, int32_t n);
218
+ void ca_sync_carry_on (CArray *ca, volatile VALUE *error);
191
219
  VALUE ca_attach_window (int32_t n, CArray **list, const char *modes,
192
220
  VALUE (*body)(VALUE), VALUE arg);
193
221
 
222
+ /* A window that yields to a block can be left open for good: Ruby does not
223
+ run the ensure of a suspended Fiber that is collected (an Enumerator
224
+ stopped with #next and dropped). ca_window_guard_arm makes a guard that
225
+ runs cleanup(arg) from a finalizer when that happens; the window disarms
226
+ it when it closes normally. arg is heap memory the guard frees, and
227
+ mark marks the VALUEs it holds. ca_attach_window_yielding is
228
+ ca_attach_window with such a guard; owners keeps the arrays alive. */
229
+ VALUE ca_window_guard_arm (void (*cleanup)(void *), void (*mark)(void *),
230
+ void *arg, VALUE *sentinel);
231
+ void ca_window_guard_disarm (VALUE guard, VALUE sentinel);
232
+ VALUE ca_attach_window_yielding (VALUE owners, int32_t n, CArray **list,
233
+ const char *modes, VALUE (*body)(VALUE),
234
+ VALUE arg);
235
+
194
236
  /* ---- Filling a result Ruby does not own yet (carray_copy.c) ---------------
195
237
 
196
238
  A C builder that allocates its result with carray_new / ca_template and
@@ -210,6 +252,17 @@ void ca_fill_or_free (CArray *co, VALUE (*fill)(VALUE), VALUE arg);
210
252
  void ca_fill_or_xfree (void *buf, VALUE (*fill)(VALUE), VALUE arg);
211
253
  void ca_fill_or_detach (int n, CArray **list, VALUE (*fill)(VALUE), VALUE arg);
212
254
 
255
+ /* The buffer of a view's own cells, for an attach or allocate slot that
256
+ attaches parents[0..n-1] and then works in a buffer of its own. The
257
+ buffer is allocated before the parents are attached, and freed if
258
+ attaching them raises: a slot that raises before it publishes ca->ptr
259
+ gives ca_attach no detach slot to call, and a parent left attached keeps
260
+ reading and writing the copy it materialised instead of its root. The
261
+ caller publishes the result as ca->ptr. utils/check_attach_then_alloc.rb
262
+ finds a slot that allocates after attaching. */
263
+
264
+ char *ca_alloc_cells_then_attach (CArray *ca, int32_t n, CArray **parents);
265
+
213
266
  /* A view takes its parent's mask by asking the parent whether it has one,
214
267
  and a parent that is a CAObject answers in Ruby and can raise. A view's
215
268
  setup therefore does not ask: its constructor calls this under
@@ -272,6 +325,8 @@ int ca_shares_root (void *ap, void *aq);
272
325
  view's shape (carray_mask.c). Installed directly in operation tables. */
273
326
 
274
327
  void ca_view_func_create_mask (void *ap);
328
+ void ca_view_sync_mask_after_parent (void *ap);
329
+ void ca_view_pass_mask_to_parent (void *ap);
275
330
 
276
331
  /* Running count of the present cells of any data type along axis (Qnil:
277
332
  the flattened array), read off the mask alone (carray_mask.c). The
@@ -307,6 +362,21 @@ int ca_lazy_req_is_packed (const ca_lazy_req_t *req, void *view);
307
362
  void ca_lazy_req_pull (void *operand, const ca_lazy_req_t *req,
308
363
  void *buf, int dir);
309
364
  boolean8_t *ca_lazy_req_mask (const ca_lazy_req_t *req, int n, CArray **ops);
365
+
366
+ /* Runs eval(node, req, data, dir), the evaluation of a lazy node over the
367
+ cells req names. When the node or one of its operands ops[0..n) is an
368
+ object array, the evaluation calls Ruby on each cell, and a cell holding
369
+ an expression over the node comes back into it: the node, and the nodes
370
+ of its chain in chain[0..nchain), are then recorded as computed over
371
+ (ca_computing_run_nodes), so coming back is refused as cyclic. The
372
+ operands are not recorded: an expression over the same operand that does
373
+ not need the node is no cycle. Otherwise eval runs as it is. */
374
+ typedef void (*ca_lazy_eval_fn) (void *node, const ca_lazy_req_t *req,
375
+ void *data, int dir);
376
+ void ca_lazy_eval_recorded (void *node, int n, CArray **ops,
377
+ int nchain, CArray **chain,
378
+ ca_lazy_eval_fn eval, const ca_lazy_req_t *req,
379
+ void *data, int dir);
310
380
  VALUE ca_lazy_settle_scalar (VALUE operand, int was_scalar);
311
381
 
312
382
  /* Non-zero iff the array a view's cells come from -- the parent, or the
@@ -316,6 +386,7 @@ VALUE ca_lazy_settle_scalar (VALUE operand, int was_scalar);
316
386
  of one view all take the same branch. */
317
387
 
318
388
  int ca_parent_lends_no_memory (void *ap);
389
+ void rb_ca_cast_three (volatile VALUE *v1, volatile VALUE *v2, volatile VALUE *v3);
319
390
 
320
391
  /* Transfers of the descriptor views (CAGrid, CASelect, CASelectAxis,
321
392
  CAWindow) whose parent holds no ptr (ca_axis_dispatch.c).
@@ -389,6 +460,13 @@ CAReduce *ca_reduce_new (CArray *carray, ca_size_t count, ca_size_t offset);
389
460
  void ca_broadcast_to_destination (VALUE dst, volatile VALUE *src);
390
461
  VALUE rb_ary_flatten_for_elements (VALUE ary, ca_size_t elements, void *ap);
391
462
 
463
+ /* carray_access.c */
464
+ void ca_refuse_complex_into_real (CArray *ca, CArray *cv);
465
+
466
+ /* carray_cast.c */
467
+ void ca_cast_block_skip_masked (ca_size_t n, void *ap1, void *ptr1,
468
+ void *ap2, void *ptr2, boolean8_t *m);
469
+
392
470
  /* carray_utils.c */
393
471
  const char *ca_calling_method_name (void);
394
472
  VALUE ca_answer_as_caller (VALUE (*body)(VALUE), VALUE arg);
@@ -396,13 +474,26 @@ long ca_integer_arg (VALUE v, const char *arg, const char *name);
396
474
  /* 1 with the position of an index key (an Integer, or a Float with no
397
475
  fraction while exact), 2 for an Integer past int64, 0 otherwise. */
398
476
  int ca_index_key_position (VALUE v, ca_size_t *out);
477
+ ca_size_t ca_index_key_value (VALUE v);
399
478
  void ca_refuse_without_common_type (VALUE self, VALUE other);
400
479
  ca_size_t ca_shape_elements (int8_t ndim, const ca_size_t *dim, ca_size_t bytes);
401
480
  void ca_check_index_array (VALUE v);
402
481
  VALUE ca_order_key (VALUE v, const char *name);
403
482
  void ca_check_integer_fits (VALUE v, int8_t data_type);
404
483
  VALUE ca_obj2integer_fitting (VALUE v, int8_t data_type);
484
+ /* A Ruby value stored into an integer cell of `dst`: refused when it does
485
+ not fit (ca_obj2integer_fitting), except into the cells of a bit field
486
+ view (ca_bitfield_beneath), which keep it modulo 2 ** bits
487
+ (ca_integer_wrap_bits). */
488
+ VALUE ca_obj2integer_store (CArray *dst, VALUE v, int8_t data_type);
489
+ CArray *ca_bitfield_beneath (CArray *ca);
490
+ VALUE ca_bitfield_wrap_integer (CArray *ca, VALUE v);
491
+ /* v as an Integer modulo 2 ** bits; with sign_extend, in
492
+ -2 ** (bits - 1) ... 2 ** (bits - 1) - 1. A String is read as a
493
+ decimal integer, any other value through Integer() (1.5 is 1). */
494
+ VALUE ca_integer_wrap_bits (VALUE v, int bits, int sign_extend);
405
495
  int ca_integer_range_side (VALUE v, int8_t data_type);
496
+ int ca_obj_number_fits_integer (VALUE v, int8_t data_type);
406
497
  int ca_bincmp_integer_prepare (volatile VALUE *self, volatile VALUE *other,
407
498
  int op_id, int *signed_side);
408
499
  int ca_bincmp_answer_when_negative (int op_id, int signed_side);
@@ -425,6 +516,32 @@ void ca_check_rank_fits (long ndim, const char *name);
425
516
  #define CA_FLAG_LENT 2048
426
517
  #define CA_FLAG_LENT_VIEWED 4096
427
518
 
519
+ /* A computation over object arrays. A cell whose value is the array
520
+ itself or a view of it makes the kernel call back into a computation
521
+ over the same cells; entering an array that the same method is already
522
+ computing over, in this thread and fiber, is refused as cyclic, as
523
+ reading such a cell is (ca_test_cyclic_check). ca_computing_run runs
524
+ body(arg) with the object arrays among ops[0..n) recorded
525
+ (rb_exec_recursive, carray_core.c); nothing is written to the arrays. */
526
+ VALUE ca_computing_run (int n, CArray **ops, VALUE (*body)(VALUE), VALUE arg);
527
+ VALUE ca_computing_run_nodes (int n, CArray **ops, VALUE (*body)(VALUE), VALUE arg);
528
+ /* Refuses as cyclic a cell among cells[0..n) that is an array reaching a
529
+ lazy node being evaluated: computing it would come back into the node. */
530
+ void ca_refuse_cells_reaching_evaluation (ca_size_t n, const VALUE *cells);
531
+ /* Runs body(arg), a walk into the cells of an object array, one level deeper
532
+ in this fiber; past the limit it raises ArgumentError. A cell can give a
533
+ new array each time it is opened, which the records above never meet
534
+ twice. */
535
+ VALUE ca_cell_walk_run (VALUE (*body)(VALUE), VALUE arg);
536
+
537
+ /* Registers a view type whose transfers only move cells between its parents
538
+ and the caller (an index view: CAGrid, CASelect, CAWindow, CAStack, ...),
539
+ calling no Ruby. A transfer through such views down to entities cannot
540
+ come back into an array, and is not recorded (ca_with_cyclic_check). */
541
+ void ca_register_cell_moving_view (int8_t obj_type);
542
+ VALUE ca_iter_ensure_computing (CArray *ca, int32_t n, ca_iter_state *states,
543
+ VALUE (*body)(VALUE), VALUE arg);
544
+
428
545
  #define CA_CHECK_DATA_TYPE_GIVEN(data_type) \
429
546
  do { \
430
547
  int ca_dt_ = (data_type); \
@@ -696,19 +813,41 @@ CArray *ca_multi_parent_mask (CArray *p, CArray **standin);
696
813
  void ca_multi_parent_mask_refresh (CArray *mask, CArray **parents,
697
814
  CArray **standins, int32_t n);
698
815
 
699
- /* Before a write to a multi-parent view's mask: when a parent that can
700
- never carry a mask (a value array, a mask array) is among the parents,
701
- run the write `body(arg)` once against blank all-false masks in place of
702
- every parent's mask and refuse it with that TypeError if it put a true
703
- into such a parent's part. A refused write then changes no parent.
704
- Does nothing when no such parent is there. */
705
- void ca_multi_parent_mask_refuse_first (CArray *mask, CArray **parents,
706
- CArray **standins, int32_t n,
707
- VALUE (*body)(VALUE),
708
- VALUE arg);
816
+ void ca_multi_parent_check_part (CArray *part, const char *name);
817
+
818
+ /* A write to a stack or meld goes to its parts one after another, and a
819
+ part that converts the value on its way (a writable cast) can refuse it
820
+ after the parts before it were written. ca_multi_parent_value_trial
821
+ gives a stack or meld laid out as `ca` over scratch arrays standing for
822
+ its parts (held by `keep`), to run the write against first, when
823
+ ca_multi_parent_value_needs_trial says such a part is there. */
824
+ int ca_multi_parent_value_needs_trial (CArray *ca);
825
+ CArray *ca_multi_parent_value_trial (CArray *ca, VALUE keep);
826
+
827
+ /* A write through the view `ca` that reaches a stack or meld needing the
828
+ trial, in pieces: ca_multi_parent_value_trial_below gives the stack or
829
+ meld, and ca_multi_parent_value_trial_through runs the write against its
830
+ stand-ins first. */
831
+ CArray *ca_multi_parent_value_trial_below (CArray *ca);
832
+ void ca_multi_parent_value_trial_through (CArray *m, VALUE (*body)(VALUE),
833
+ VALUE arg);
834
+
835
+ #define CA_MULTI_PARENT_VALUE_TRIAL(ap, t, call) \
836
+ do { \
837
+ if ( ca_multi_parent_value_needs_trial((CArray *) (ap)) ) { \
838
+ volatile VALUE keep_ = rb_ary_new(); \
839
+ CArray *t = ca_multi_parent_value_trial((CArray *) (ap), keep_); \
840
+ call; \
841
+ RB_GC_GUARD(keep_); \
842
+ } \
843
+ } while (0)
844
+ int ca_monop_writable_cast_target (CArray *ca, int8_t *target);
845
+
846
+ void ca_multi_parent_mask_adopt (CArray *mask, CArray **parents,
847
+ CArray **standins, int32_t n);
709
848
 
710
849
  /* ca_mask_standin_settle for each part of a multi-parent view's mask
711
- still reading its stand-in, the parts that can have no mask first. */
850
+ still reading its stand-in. */
712
851
  void ca_multi_parent_mask_settle (CArray *mask, CArray **parents,
713
852
  CArray **standins, int32_t n);
714
853
 
@@ -758,6 +897,40 @@ ca_query_match_applies (CArray *ref, VALUE query)
758
897
  return 1;
759
898
  }
760
899
 
900
+ /* A Ruby value an integer, float, complex or boolean array can not hold:
901
+ anything but a Numeric, true, false and UNDEF that does not make an
902
+ array (an Array, a #to_ca, a memory view). nil, a String, a Symbol and
903
+ other objects are foreign. An object or fixlen array and a Face hold or
904
+ read such values, so nothing is foreign to them (ca_cast.c). */
905
+ int ca_value_foreign_to (CArray *ca, VALUE v);
906
+
907
+ /* An operator refuses a foreign value as Float#+ does (TypeError), and an
908
+ ordering refuses it as Float#< does (ArgumentError). `array` is the
909
+ CArray operand (ca_cast.c). */
910
+ void ca_refuse_foreign_operand (VALUE v, VALUE array);
911
+ void ca_refuse_foreign_order (VALUE v, VALUE array);
912
+
913
+ /* true and false as the numbers 1 and 0 for a value query on an integer,
914
+ float or complex array; any other value, or array, is returned as is
915
+ (ca_cast.c). */
916
+ VALUE ca_truth_as_number (CArray *ca, VALUE v);
917
+
918
+ /* eq / ne when no cell can match (carray_operator.c). */
919
+ VALUE ca_bincmp_no_match (VALUE self, int ne);
920
+
921
+ /* A value an array can not hold matches no cell as a value query: eq, ne,
922
+ count and search answer as they do for an Integer outside the array's
923
+ type. nil is such a value for every array but an object one, which
924
+ holds nil and is asked. */
925
+ static inline int
926
+ ca_query_matches_nothing (CArray *ca, VALUE query)
927
+ {
928
+ if ( NIL_P(query) ) {
929
+ return ca->data_type != CA_OBJECT;
930
+ }
931
+ return ca_value_foreign_to(ca, query);
932
+ }
933
+
761
934
  /* Equality of two object cells, as the float lanes answer it: Ruby's `==`,
762
935
  except that a Float NaN equals nothing, itself included. rb_equal alone
763
936
  answers true for the same object, so whether two NaN cells matched would
@@ -774,7 +947,7 @@ ca_obj_equal (VALUE a, VALUE b)
774
947
  /* ---- fast paths (carray_core.c) --------------------------------------------
775
948
 
776
949
  A fast path answers what a general path also answers, by a different
777
- algorithm. Each one tests ca_fast_paths_enabled() alongside its own
950
+ algorithm. Each one tests ca_fast_path(CA_SC_name) alongside its own
778
951
  condition, so a development build run with CARRAY_GENERAL_PATH=1 in the
779
952
  environment takes the general path everywhere and the test suite
780
953
  compares the two. A release build has no switch: the test is the
@@ -794,13 +967,61 @@ ca_obj_equal (VALUE a, VALUE b)
794
967
 
795
968
  ---------------------------------------------------------------------------- */
796
969
 
970
+ /* The number of obj_types carray installs for itself (carray_core.c). */
971
+ extern int ca_obj_num_builtin;
972
+
973
+ /* Each fast path has a name, so that a development build can switch it off
974
+ alone: CARRAY_SHORTCUTS_OFF=stack,stack_outer_k in the environment turns
975
+ off the ones named, CARRAY_GENERAL_PATH=1 all of them. A fast path tests
976
+ ca_fast_path(CA_SC_name) alongside its own condition. The names are in
977
+ ca_shortcut_names (carray_core.c) in this order; the Ruby ones are asked
978
+ through CArray.__fast_paths_enabled__(:name). */
979
+ enum ca_shortcut {
980
+ CA_SC_SHIFT_WHOLE_VIEW,
981
+ CA_SC_STACK,
982
+ CA_SC_STACK_OUTER_K,
983
+ CA_SC_FIBER_FUSED_CONVERSION,
984
+ CA_SC_PER_SLAB_HOIST,
985
+ CA_SC_PROTECT_ONLY_MAY_RAISE,
986
+ CA_SC_STACK_ADDRS_K0,
987
+ CA_SC_STACK_STRIDE_BOX,
988
+ CA_SC_STACK_STRIDE_RELOC,
989
+ CA_SC_STACK_PARENTS_K0,
990
+ CA_SC_STACK_FOLD,
991
+ CA_SC_MELD_STRIDE_BOX,
992
+ CA_SC_MELD_PARENTS_AXIS0,
993
+ CA_SC_MEDIAN_PARTITION,
994
+ CA_SC_PERCENTILE_PARTITION,
995
+ CA_SC_LOOP_INTERCHANGE,
996
+ CA_SC_STACK_LOOP_INTERCHANGE,
997
+ CA_SC_LAZY_STREAMING_REDUCE,
998
+ CA_SC_SHORT_FIBER_REDUCE,
999
+ /* asked from lib/carray */
1000
+ CA_SC_MELD_REDUCE,
1001
+ CA_SC_SEGMENT_NUNIQUE,
1002
+ CA_SC_SEGMENT_SCAN_KERNEL,
1003
+ CA_SC_SEGMENT_EDGE_KERNEL,
1004
+ CA_SC_SEGMENT_REDUCE_KERNEL,
1005
+ CA_SC_SEGMENT_ALL_ANY_KERNEL,
1006
+ CA_SC_CATEGORICAL_GROUP_BY_INDEX,
1007
+ CA_SC_AXIS_GROUP_COUNT_KERNEL,
1008
+ CA_SC_AXIS_GROUP_WHOLE_BLOCK,
1009
+ CA_SC_AXIS_GROUP_REDUCE_KERNEL,
1010
+ CA_SC_AXIS_GROUP_SCAN_KERNEL,
1011
+ CA_SC_WINDOW_FOLD_BY_OFFSET,
1012
+ CA_SC_COUNT
1013
+ };
1014
+
797
1015
  #ifdef CARRAY_DEV_BUILD
798
1016
  extern int ca_fast_paths_on;
799
1017
  extern int ca_iter_transfer_only_on;
1018
+ extern unsigned char ca_shortcut_off[CA_SC_COUNT];
800
1019
  #define ca_fast_paths_enabled() (ca_fast_paths_on)
1020
+ #define ca_fast_path(id) (ca_fast_paths_on && ! ca_shortcut_off[id])
801
1021
  #define ca_iter_by_transfer_only() (ca_iter_transfer_only_on)
802
1022
  #else
803
1023
  #define ca_fast_paths_enabled() (1)
1024
+ #define ca_fast_path(id) (1)
804
1025
  #define ca_iter_by_transfer_only() (0)
805
1026
  #endif
806
1027
 
@@ -851,4 +1072,12 @@ int ca_index_key_is_scalar (VALUE arg);
851
1072
  /* Whether this runs in the main Ractor (carray_utils.c). */
852
1073
  int ca_in_main_ractor (void);
853
1074
 
1075
+ /* Take a native lock that guards a table shared by every Ractor, waiting
1076
+ without the GVL (carray_utils.c). A Ractor that waits for the lock while
1077
+ it holds its GVL cannot stop for a GC, so one that holds the lock and
1078
+ allocates (a GC) would wait for it, and it for the lock. The lock is only
1079
+ ever taken outside the wait, so an interrupt that raises there leaves it
1080
+ free. The lock must not be held across a call that can raise. */
1081
+ void ca_native_lock_without_gvl (rb_nativethread_lock_t *lock);
1082
+
854
1083
  #endif /* CARRAY_INTERNAL_H */