carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_mask.c CHANGED
@@ -11,7 +11,6 @@
11
11
  #include <stdint.h>
12
12
  #include "ruby.h"
13
13
  #include "carray.h"
14
- #include "carray_internal.h" /* ca_lazy_arena_* */
15
14
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE for the mask-lift path */
16
15
  #include <ruby/ractor.h>
17
16
 
@@ -299,6 +298,10 @@ ca_has_mask (void *ap)
299
298
  {
300
299
  CArray *ca = (CArray *) ap;
301
300
 
301
+ if ( ! ca->mask && ! ca_is_entity(ca) ) { /* an entity asks no parent */
302
+ ca_check_stack_room();
303
+ }
304
+
302
305
  if ( ca->mask ) { /* mask array already created */
303
306
  return 1;
304
307
  }
@@ -371,7 +374,7 @@ ca_is_any_masked (void *ap)
371
374
  non-zero word (the typical "all zero, no mask actually set" hot
372
375
  pattern). An O(n) scan cache is not viable here: the mask is
373
376
  dynamic and a view chain can introduce a mask mid-chain. */
374
- ca_attach(ca->mask); /* window: nothing raises inside */
377
+ ca_attach(ca->mask); /* attach: nothing raises before the detach */
375
378
  flag = ca_mask_word_any((boolean8_t *) ca->mask->ptr, ca->elements);
376
379
  ca_detach(ca->mask);
377
380
  }
@@ -398,7 +401,7 @@ ca_is_all_masked (void *ap)
398
401
  /* cheap mask: O(1) alias attach + word-level all_set with early exit.
399
402
  The word check uses (w == 0x0101...01ULL) under the {0,1}
400
403
  invariant; returns on the first non-fully-set word. */
401
- ca_attach(ca->mask); /* window: nothing raises inside */
404
+ ca_attach(ca->mask); /* attach: nothing raises before the detach */
402
405
  flag = ca_mask_word_all_set((boolean8_t *) ca->mask->ptr, ca->elements);
403
406
  ca_detach(ca->mask);
404
407
  }
@@ -427,7 +430,7 @@ ca_view_func_create_mask (void *ap)
427
430
  /* The sync slot of a view whose mask came from ca_view_func_create_mask
428
431
  calls this after syncing its parent. The view's mask is a refer over
429
432
  the parent's mask, and when the parent's mask is itself a view, both
430
- hold a buffer while the window is open; the refer writes past the
433
+ hold a buffer while the view is attached; the refer writes past the
431
434
  parent's buffer to the root. Syncing the parent then writes its
432
435
  buffer, from before the view's mask was written, back over the root,
433
436
  so the view's mask is synced once more. */
@@ -477,7 +480,7 @@ ca_update_mask (void *ap)
477
480
  static VALUE
478
481
  ca_create_mask_attach (VALUE arg)
479
482
  {
480
- ca_attach((CArray *) arg); /* window: released by the view's detach */
483
+ ca_attach((CArray *) arg); /* attach: released by the view's detach */
481
484
  return Qnil;
482
485
  }
483
486
 
@@ -515,7 +518,7 @@ ca_mask_standin_current (CArray *mask_view, CArray *reading, CArray *parent,
515
518
  if ( standin == NULL || reading != standin ) {
516
519
  return reading;
517
520
  }
518
- /* Not inside an attach window whose buffer is out: that buffer and the
521
+ /* Not while an attach has published its buffer: that buffer and the
519
522
  detach to come are those of the stand-in. The attach and allocate
520
523
  slots run before the buffer is published, and may still give way. */
521
524
  if ( CAVIEW(mask_view)->attach > 0 && mask_view->ptr != NULL ) {
@@ -531,11 +534,70 @@ ca_mask_standin_current (CArray *mask_view, CArray *reading, CArray *parent,
531
534
  return ( parent->mask != NULL ) ? parent->mask : reading;
532
535
  }
533
536
 
537
+ /* The first view from ca up the parent chain whose mask is attached, or
538
+ NULL. An attached mask may alias its root: whether it does is not asked,
539
+ as ca_attach_is_alias answers yes for any view that already has a pointer,
540
+ and the copy made for one that does is a no-op. */
541
+ static CArray *
542
+ ca_mask_buffer_holder (CArray *ca)
543
+ {
544
+ while ( ca != NULL && ca_is_view(ca)
545
+ && ! ca_test_flag(ca, CA_FLAG_MULTI_PARENTS) ) {
546
+ if ( ca->mask != NULL && ca_is_view(ca->mask)
547
+ && ca_is_attached(ca->mask) ) {
548
+ return ca;
549
+ }
550
+ ca = CAVIEW(ca)->parent;
551
+ }
552
+ return NULL;
553
+ }
554
+
555
+ /* Marks the cells addrs of mask, the mask of a view at or below chain. The
556
+ write goes past any buffer an attached view holds on the way to the root,
557
+ and the sync before that view's detach sends the buffer: the cells this
558
+ write turned true are copied into it, read as the difference of two
559
+ gathers so that what was written there meanwhile through the attached
560
+ view stays. */
561
+ static void
562
+ ca_mask_fill_past_buffers (CArray *mask, CArray *chain, ca_size_t m,
563
+ ca_size_t *addrs)
564
+ {
565
+ volatile VALUE holder = 0, reader;
566
+ CArray *owner = ca_mask_buffer_holder(chain);
567
+ CArray *held, *rd;
568
+ boolean8_t *before, *after, *z;
569
+ boolean8_t one = 1;
570
+ ca_size_t i, n;
571
+ ca_check_stack_room();
572
+
573
+ if ( owner == NULL ) {
574
+ ca_fill_addrs(mask, m, addrs, &one);
575
+ return;
576
+ }
577
+ held = owner->mask;
578
+ n = held->elements;
579
+ /* A view answers from its buffer while it has one: the clone has none. */
580
+ reader = ca_wrap_struct((CArray *) ca_clone(held));
581
+ GetCArray(reader, rd);
582
+ before = ALLOCV_N(boolean8_t, holder, 2 * n);
583
+ after = before + n;
584
+ ca_xfer_all(rd, before, CA_XFER_GET);
585
+ ca_mask_fill_past_buffers(mask, CAVIEW(owner)->parent, m, addrs);
586
+ ca_xfer_all(rd, after, CA_XFER_GET);
587
+ z = (boolean8_t *) held->ptr;
588
+ for ( i = 0; i < n; i++ ) {
589
+ if ( after[i] && ! before[i] ) {
590
+ z[i] = 1;
591
+ }
592
+ }
593
+ ALLOCV_END(holder);
594
+ }
595
+
534
596
  void
535
597
  ca_mask_standin_settle (CArray *parent, CArray *standin)
536
598
  {
537
599
  volatile VALUE holder = 0;
538
- boolean8_t *z, one = 1;
600
+ boolean8_t *z;
539
601
  ca_size_t *addrs;
540
602
  ca_size_t i, m = 0, n;
541
603
 
@@ -561,7 +623,7 @@ ca_mask_standin_settle (CArray *parent, CArray *standin)
561
623
  }
562
624
  MEMZERO(z, boolean8_t, n);
563
625
  ca_create_mask(parent);
564
- ca_fill_addrs(parent->mask, m, addrs, &one);
626
+ ca_mask_fill_past_buffers(parent->mask, parent, m, addrs);
565
627
  ALLOCV_END(holder);
566
628
  }
567
629
 
@@ -628,26 +690,59 @@ ca_multi_parent_mask_adopt (CArray *mask, CArray **parents,
628
690
  CAVIEW(mask)->parent = mp->parents[0];
629
691
  }
630
692
 
693
+ typedef struct {
694
+ CArray *parent;
695
+ CArray *standin;
696
+ } ca_standin_settle_arg;
697
+
698
+ static VALUE
699
+ ca_standin_settle_body (VALUE arg)
700
+ {
701
+ ca_standin_settle_arg *a = (ca_standin_settle_arg *) arg;
702
+ ca_mask_standin_settle(a->parent, a->standin);
703
+ return Qnil;
704
+ }
705
+
706
+ /* Every part is settled, past a parent that refuses a mask, so no part keeps
707
+ a true in its stand-in; then the first exception is raised. */
631
708
  void
632
709
  ca_multi_parent_mask_settle (CArray *mask, CArray **parents,
633
710
  CArray **standins, int32_t n)
634
711
  {
635
712
  CAMultiParent *mp = (CAMultiParent *) mask;
713
+ volatile VALUE error = Qnil;
636
714
  int32_t k;
637
715
  if ( standins == NULL ) {
638
716
  return;
639
717
  }
640
718
  for ( k = 0; k < n; k++ ) {
641
719
  if ( standins[k] != NULL && mp->parents[k] == standins[k] ) {
642
- ca_mask_standin_settle(parents[k], standins[k]);
720
+ ca_standin_settle_arg a = { parents[k], standins[k] };
721
+ int tag = 0;
722
+ /* protect: goes on to the other parts; nothing to give back */
723
+ rb_protect(ca_standin_settle_body, (VALUE) &a, &tag);
724
+ if ( tag ) {
725
+ VALUE err = rb_errinfo();
726
+ if ( ! RTEST(rb_obj_is_kind_of(err, rb_eException)) ) {
727
+ rb_jump_tag(tag);
728
+ }
729
+ if ( NIL_P(error) ) {
730
+ error = err;
731
+ }
732
+ rb_set_errinfo(Qnil);
733
+ }
643
734
  }
644
735
  }
736
+ if ( ! NIL_P(error) ) {
737
+ rb_exc_raise(error);
738
+ }
645
739
  }
646
740
 
647
741
  void
648
742
  ca_create_mask (void *ap)
649
743
  {
650
744
  CArray *ca = (CArray *) ap;
745
+ ca_check_stack_room();
651
746
 
652
747
  if ( ca->mask ) {
653
748
  return;
@@ -756,7 +851,7 @@ ca_setup_mask (void *ap, CArray *mask)
756
851
  /* The overlay reads each source's mask into the destination's. A
757
852
  source's mask can be made or read by calling Ruby (a CAObject's mask
758
853
  hooks) and raise, so every mask is made first and the lot are read
759
- inside one attach window.
854
+ while they are all attached, through one ca_attach_window.
760
855
 
761
856
  A source whose cells live in the destination's root has its mask read
762
857
  into a snapshot before the destination's mask is made, cleared or
@@ -995,7 +1090,7 @@ ca_count_masked (void *ap)
995
1090
  if ( ca->mask ) {
996
1091
  if ( ca_attach_is_alias(ca->mask) ) {
997
1092
  /* cheap mask: O(1) alias attach + word-bulk count. */
998
- ca_attach(ca->mask); /* window: nothing raises inside */
1093
+ ca_attach(ca->mask); /* attach: nothing raises before the detach */
999
1094
  m = (boolean8_t *) ca->mask->ptr;
1000
1095
  count = ca_mask_word_count(m, ca->elements);
1001
1096
  ca_detach(ca->mask);
@@ -1061,7 +1156,7 @@ ca_unmask (void *ap, char *fill_value)
1061
1156
  ca_fill(ca->mask, &zero);
1062
1157
  }
1063
1158
  else {
1064
- ca_attach(ca); /* window: nothing raises inside */
1159
+ ca_attach(ca); /* attach: nothing raises before the detach */
1065
1160
 
1066
1161
  switch ( ca->bytes ) {
1067
1162
  case 1: proc_fill_bang_width(uint8_t); break;
@@ -1123,22 +1218,6 @@ ca_unmask_copy (void *ap, char *fill_value)
1123
1218
  return a.co;
1124
1219
  }
1125
1220
 
1126
- void
1127
- ca_invert_mask (void *ap)
1128
- {
1129
- CArray *ca = (CArray *) ap;
1130
- boolean8_t *m;
1131
-
1132
- ca_create_mask(ca);
1133
-
1134
- ca_attach(ca->mask); /* window: nothing raises inside */
1135
- m = (boolean8_t *) ca->mask->ptr;
1136
- ca_mask_word_not(m, m, ca->elements); /* in-place 1 - m, LSB-XOR word loop */
1137
- ca_sync_detach(ca->mask);
1138
-
1139
- return;
1140
- }
1141
-
1142
1221
  boolean8_t *
1143
1222
  ca_allocate_mask_iterator_n (int n, CArray **slist)
1144
1223
  {
@@ -1199,7 +1278,7 @@ ca_allocate_mask_iterator_n (int n, CArray **slist)
1199
1278
  masks[k] = cs->mask;
1200
1279
  scalar[k++] = (char) ca_is_scalar(cs);
1201
1280
  }
1202
- ca_attach_all(masks, k); /* window: nothing raises inside */
1281
+ ca_attach_all(masks, k); /* attach: nothing raises before the detach */
1203
1282
 
1204
1283
  m = xmalloc(sizeof(boolean8_t)*elements);
1205
1284
  memset(m, 0, elements);
@@ -1256,10 +1335,10 @@ Returns true if self has the mask array.
1256
1335
  */
1257
1336
 
1258
1337
  VALUE
1259
- rb_ca_has_mask (VALUE self)
1338
+ rb_ca_has_mask (volatile VALUE self)
1260
1339
  {
1261
1340
  CArray *ca;
1262
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1341
+ GetCArray(self, ca);
1263
1342
  return ( ca_has_mask(ca) ) ? Qtrue : Qfalse;
1264
1343
  }
1265
1344
 
@@ -1270,10 +1349,10 @@ Returns true if self has at least one masked element.
1270
1349
  */
1271
1350
 
1272
1351
  VALUE
1273
- rb_ca_is_any_masked (VALUE self)
1352
+ rb_ca_is_any_masked (volatile VALUE self)
1274
1353
  {
1275
1354
  CArray *ca;
1276
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1355
+ GetCArray(self, ca);
1277
1356
  return ( ca_is_any_masked(ca) ) ? Qtrue : Qfalse;
1278
1357
  }
1279
1358
 
@@ -1284,10 +1363,10 @@ Returns true if all elements of self are masked.
1284
1363
  */
1285
1364
 
1286
1365
  VALUE
1287
- rb_ca_is_all_masked (VALUE self)
1366
+ rb_ca_is_all_masked (volatile VALUE self)
1288
1367
  {
1289
1368
  CArray *ca;
1290
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1369
+ GetCArray(self, ca);
1291
1370
  return ( ca_is_all_masked(ca) ) ? Qtrue : Qfalse;
1292
1371
  }
1293
1372
 
@@ -1298,11 +1377,11 @@ Creates mask array internally (private method)
1298
1377
  */
1299
1378
 
1300
1379
  static VALUE
1301
- rb_ca_create_mask (VALUE self)
1380
+ rb_ca_create_mask (volatile VALUE self)
1302
1381
  {
1303
1382
  CArray *ca;
1304
1383
  rb_ca_modify(self);
1305
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1384
+ GetCArray(self, ca);
1306
1385
  ca_create_mask(ca);
1307
1386
  return Qnil;
1308
1387
  }
@@ -1318,7 +1397,7 @@ static VALUE
1318
1397
  rb_ca_update_mask (VALUE self)
1319
1398
  {
1320
1399
  CArray *ca;
1321
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1400
+ GetCArray(self, ca);
1322
1401
  ca_update_mask(ca);
1323
1402
  return Qnil;
1324
1403
  }
@@ -1333,16 +1412,16 @@ via the returned array. The value array can't be set mask.
1333
1412
  */
1334
1413
 
1335
1414
  VALUE
1336
- rb_ca_value_array (VALUE self)
1415
+ rb_ca_value_array (volatile VALUE self)
1337
1416
  {
1338
- VALUE obj;
1417
+ volatile VALUE obj;
1339
1418
  CArray *ca, *co;
1340
1419
 
1341
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1420
+ GetCArray(self, ca);
1342
1421
 
1343
1422
  obj = rb_ca_refer_new(self, ca->data_type, ca->ndim, ca->dim, ca->bytes, 0);
1344
1423
 
1345
- TypedData_Get_Struct(obj, CArray, &carray_data_type, co);
1424
+ GetCArray(obj, co);
1346
1425
 
1347
1426
  /* Value arrays ignore the mask by definition. ca_stride_setup
1348
1427
  auto-propagates the parent's mask into newly-built CAStride
@@ -1387,12 +1466,12 @@ The mask array can't be set mask.
1387
1466
  */
1388
1467
 
1389
1468
  VALUE
1390
- rb_ca_mask_array (VALUE self)
1469
+ rb_ca_mask_array (volatile VALUE self)
1391
1470
  {
1392
1471
  volatile VALUE obj;
1393
1472
  CArray *ca;
1394
1473
 
1395
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1474
+ GetCArray(self, ca);
1396
1475
 
1397
1476
  ca_update_mask(ca);
1398
1477
  if ( ca->mask ) {
@@ -1421,12 +1500,13 @@ rb_ca_mask_array (VALUE self)
1421
1500
  rebuilt here from its own view instead. The mask of an entity is an
1422
1501
  entity and is copied. */
1423
1502
  static VALUE
1424
- ca_mask_view_dup (VALUE self)
1503
+ ca_mask_view_dup (volatile VALUE self)
1425
1504
  {
1426
1505
  volatile VALUE obj;
1427
1506
  const rb_data_type_t *type;
1428
1507
  CArray *ca, *cp;
1429
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1508
+ ca_check_stack_room();
1509
+ GetCArray(self, ca);
1430
1510
  cp = (CArray *) ca_func[ca->obj_type].clone(ca);
1431
1511
  ca_keep_chain_flags(cp, ca);
1432
1512
  type = ca_typeddata[ca->obj_type];
@@ -1440,10 +1520,10 @@ ca_mask_view_dup (VALUE self)
1440
1520
  }
1441
1521
 
1442
1522
  static VALUE
1443
- rb_ca_mask_dup (VALUE self)
1523
+ rb_ca_mask_dup (volatile VALUE self)
1444
1524
  {
1445
1525
  CArray *ca;
1446
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1526
+ GetCArray(self, ca);
1447
1527
  if ( ! ca_is_view(ca) ) {
1448
1528
  return rb_call_super(0, NULL);
1449
1529
  }
@@ -1451,26 +1531,18 @@ rb_ca_mask_dup (VALUE self)
1451
1531
  }
1452
1532
 
1453
1533
  static VALUE
1454
- rb_ca_mask_clone (int argc, VALUE *argv, VALUE self)
1534
+ rb_ca_mask_clone (int argc, VALUE *argv, volatile VALUE self)
1455
1535
  {
1456
1536
  VALUE opts = Qnil, vfreeze = Qnil;
1457
1537
  volatile VALUE obj;
1458
1538
  CArray *ca;
1459
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1539
+ GetCArray(self, ca);
1460
1540
  if ( ! ca_is_view(ca) ) {
1461
1541
  return rb_call_super_kw(argc, argv, RB_PASS_CALLED_KEYWORDS);
1462
1542
  }
1463
1543
  rb_scan_args(argc, argv, "0:", &opts);
1464
- if ( ! NIL_P(opts) ) {
1465
- /* rb_get_kwargs refuses any key but freeze:, as Kernel#clone does. */
1466
- ID kw[1];
1467
- VALUE val[1];
1468
- kw[0] = rb_intern("freeze");
1469
- rb_get_kwargs(opts, kw, 0, 1, val);
1470
- if ( val[0] != Qundef ) {
1471
- vfreeze = val[0];
1472
- }
1473
- }
1544
+ /* Any key but freeze: is refused, as Kernel#clone does. */
1545
+ rb_scan_options(opts, "freeze", &vfreeze);
1474
1546
  obj = ca_mask_view_dup(self);
1475
1547
  if ( RTEST(vfreeze) || ( NIL_P(vfreeze) && OBJ_FROZEN(self) ) ) {
1476
1548
  rb_obj_freeze(obj);
@@ -1487,14 +1559,22 @@ before asignment.
1487
1559
  */
1488
1560
 
1489
1561
  VALUE
1490
- rb_ca_set_mask (VALUE self, VALUE rval)
1562
+ rb_ca_set_mask (volatile VALUE self, VALUE rval)
1491
1563
  {
1492
1564
  volatile VALUE rmask = rval;
1493
1565
  CArray *ca, *cv;
1494
1566
 
1495
1567
  rb_ca_modify(self);
1496
1568
 
1497
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1569
+ /* Whoever writes mask = nil means to clear the mask, so the refusal
1570
+ says how to do that rather than how to mask a cell. */
1571
+ if ( NIL_P(rmask) ) {
1572
+ rb_raise(rb_eTypeError,
1573
+ "mask=: nil is not a mask; to clear the mask, "
1574
+ "assign false or call unmask");
1575
+ }
1576
+
1577
+ GetCArray(self, ca);
1498
1578
 
1499
1579
  if ( ca_is_value_array(ca) ) {
1500
1580
  rb_raise(rb_eTypeError,
@@ -1525,7 +1605,7 @@ rb_ca_set_mask (VALUE self, VALUE rval)
1525
1605
  it, is 1-D or squeezes to it, or broadcasts along size-1 axes.
1526
1606
  Checked before the mask exists, so a refused one leaves none. */
1527
1607
  ca_broadcast_to_destination(self, &rmask);
1528
- TypedData_Get_Struct(rmask, CArray, &carray_data_type, cv);
1608
+ GetCArray(rmask, cv);
1529
1609
  /* A new mask taken from self's own root, or from self's mask, is
1530
1610
  read from a copy: making and writing self's mask would change it
1531
1611
  before it is read. */
@@ -1533,7 +1613,7 @@ rb_ca_set_mask (VALUE self, VALUE rval)
1533
1613
  if ( ca_shares_root(cv, ca) ||
1534
1614
  ( ca->mask && ca_shares_root(cv, ca->mask) ) ) {
1535
1615
  rmask = rb_ca_copy(rmask);
1536
- TypedData_Get_Struct(rmask, CArray, &carray_data_type, cv);
1616
+ GetCArray(rmask, cv);
1537
1617
  }
1538
1618
  if ( ! ca_is_boolean_type(cv) ) {
1539
1619
  cv = ca_wrap_readonly(rmask, CA_BOOLEAN);
@@ -1558,7 +1638,7 @@ with <code>self</code>. The returned array has 1 for the masked elements and
1558
1638
  */
1559
1639
 
1560
1640
  VALUE
1561
- rb_ca_is_masked (VALUE self)
1641
+ rb_ca_is_masked (volatile VALUE self)
1562
1642
  {
1563
1643
  volatile VALUE out;
1564
1644
  CArray *ca, *co;
@@ -1566,7 +1646,7 @@ rb_ca_is_masked (VALUE self)
1566
1646
  boolean8_t *p;
1567
1647
  ca_size_t i;
1568
1648
 
1569
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1649
+ GetCArray(self, ca);
1570
1650
 
1571
1651
  if ( ca_is_scalar(ca) ) {
1572
1652
  co = (CArray *)cscalar_new(CA_BOOLEAN, ca->bytes, NULL);
@@ -1601,7 +1681,7 @@ The returned array has 0 for the masked elements and
1601
1681
  */
1602
1682
 
1603
1683
  VALUE
1604
- rb_ca_is_not_masked (VALUE self)
1684
+ rb_ca_is_not_masked (volatile VALUE self)
1605
1685
  {
1606
1686
  volatile VALUE out;
1607
1687
  CArray *ca, *co;
@@ -1609,7 +1689,7 @@ rb_ca_is_not_masked (VALUE self)
1609
1689
  boolean8_t *p;
1610
1690
  ca_size_t i;
1611
1691
 
1612
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1692
+ GetCArray(self, ca);
1613
1693
 
1614
1694
  if ( ca_is_scalar(ca) ) {
1615
1695
  co = (CArray *)cscalar_new(CA_BOOLEAN, ca->bytes, NULL);
@@ -1638,7 +1718,7 @@ rb_ca_is_not_masked (VALUE self)
1638
1718
  VALUE
1639
1719
  rb_ca_scan_mask_count (VALUE self, VALUE axis)
1640
1720
  {
1641
- VALUE present = rb_funcall(self, rb_intern("is_not_masked"), 0);
1721
+ VALUE present = rb_ca_is_not_masked(self);
1642
1722
  VALUE count;
1643
1723
  if ( NIL_P(axis) ) {
1644
1724
  count = rb_funcall(present, rb_intern("cumsum"), 0);
@@ -1648,47 +1728,48 @@ rb_ca_scan_mask_count (VALUE self, VALUE axis)
1648
1728
  rb_hash_aset(kw, ID2SYM(rb_intern("axis")), axis);
1649
1729
  count = rb_funcallv_kw(present, rb_intern("cumsum"), 1, &kw, RB_PASS_KEYWORDS);
1650
1730
  }
1651
- return rb_funcall(count, rb_intern("to_type"), 1, INT2NUM(CA_INT64));
1731
+ /* coerce: a fixed type, a count is int64 */
1732
+ return rb_ca_to_type(count, ID2SYM(ca_data_type_sym[CA_INT64]), Qnil);
1652
1733
  }
1653
1734
 
1654
- /* @overload unmask (fill_value = nil)
1735
+ /* @overload unmask (fill_value)
1655
1736
 
1656
1737
  (Masking, Destructive)
1657
- Unmask all elements of the object.
1658
- If the optional argument <code>fill_value</code> is given,
1659
- the masked elements are filled by <code>fill_value</code>.
1660
- The returned array doesn't have the mask array.
1738
+ Fills the masked elements with fill_value and clears their mask.
1739
+ A fill value is required; mask = false clears the mask without one.
1661
1740
  */
1662
1741
 
1663
1742
  static VALUE
1664
- rb_ca_unmask_method (int argc, VALUE *argv, VALUE self)
1743
+ rb_ca_unmask_method (int argc, VALUE *argv, volatile VALUE self)
1665
1744
  {
1666
- volatile VALUE rfval = CA_UNSPECIFIED, rcs;
1745
+ volatile VALUE rfval, rcs;
1667
1746
  CArray *ca;
1668
1747
  CScalar *cv;
1669
- char *fval = NULL;
1748
+ char *fval;
1670
1749
 
1671
1750
  rb_ca_modify(self);
1672
1751
 
1673
- if ( argc >= 1 ) {
1674
- rfval = argv[0];
1752
+ rb_check_arity(argc, 0, 1);
1753
+ if ( argc == 0 ) {
1754
+ rb_raise(rb_eArgError,
1755
+ "unmask: a fill value is required (or method:); "
1756
+ "to clear the mask without a value, assign mask = false");
1675
1757
  }
1758
+ rfval = argv[0];
1676
1759
 
1677
1760
  /* Filling with UNDEF leaves the cells undefined, as storing UNDEF does. */
1678
1761
  if ( rfval == CA_UNDEF ) {
1679
1762
  return self;
1680
1763
  }
1681
1764
 
1682
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1765
+ GetCArray(self, ca);
1683
1766
 
1684
- if ( rfval != CA_UNSPECIFIED ) {
1685
- /* The fill is a value of the array (a struct, an instant for a Face):
1686
- rb_ca_obj2ptr writes it as one cell of the storage. */
1687
- rcs = rb_cscalar_new(ca_strip_face(ca)->data_type, ca->bytes, NULL);
1688
- TypedData_Get_Struct(rcs, CScalar, &cscalar_data_type, cv);
1689
- fval = cv->ptr;
1690
- rb_ca_obj2ptr(self, rfval, fval);
1691
- }
1767
+ /* The fill is a value of the array (a struct, an instant for a Face):
1768
+ rb_ca_obj2ptr writes it as one cell of the storage. */
1769
+ rcs = rb_cscalar_new(ca_strip_face(ca)->data_type, ca->bytes, NULL);
1770
+ TypedData_Get_Struct(rcs, CScalar, &cscalar_data_type, cv);
1771
+ fval = cv->ptr;
1772
+ rb_ca_obj2ptr(self, rfval, fval);
1692
1773
 
1693
1774
  ca_unmask(ca, fval);
1694
1775
 
@@ -1697,10 +1778,17 @@ rb_ca_unmask_method (int argc, VALUE *argv, VALUE self)
1697
1778
 
1698
1779
  /* api: rb_ca_unmask */
1699
1780
 
1781
+ /* Clears the mask without a value: the masked cells show whatever their
1782
+ storage holds. */
1783
+
1700
1784
  VALUE
1701
- rb_ca_unmask (VALUE self)
1785
+ rb_ca_unmask (volatile VALUE self)
1702
1786
  {
1703
- return rb_ca_unmask_method(0, NULL, self);
1787
+ CArray *ca;
1788
+ rb_ca_modify(self);
1789
+ GetCArray(self, ca);
1790
+ ca_unmask(ca, NULL);
1791
+ return self;
1704
1792
  }
1705
1793
 
1706
1794
  /* api: rb_ca_mask_fill */
@@ -1716,7 +1804,7 @@ rb_ca_mask_fill (VALUE self, VALUE fval)
1716
1804
  surface is strip_mask). */
1717
1805
 
1718
1806
  static VALUE
1719
- rb_ca_unmask_copy_method (int argc, VALUE *argv, VALUE self)
1807
+ rb_ca_unmask_copy_method (int argc, VALUE *argv, volatile VALUE self)
1720
1808
  {
1721
1809
  volatile VALUE obj, rfval = CA_UNSPECIFIED, rcs;
1722
1810
  CArray *ca, *co;
@@ -1733,7 +1821,7 @@ rb_ca_unmask_copy_method (int argc, VALUE *argv, VALUE self)
1733
1821
  return rb_ca_copy(self);
1734
1822
  }
1735
1823
 
1736
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1824
+ GetCArray(self, ca);
1737
1825
 
1738
1826
  if ( rfval != CA_UNSPECIFIED ) {
1739
1827
  /* The fill is a value of the array (a struct, an instant for a Face):
@@ -1766,22 +1854,6 @@ rb_ca_mask_fill_copy (VALUE self, VALUE fval)
1766
1854
  return rb_ca_unmask_copy_method(1, &fval, self);
1767
1855
  }
1768
1856
 
1769
- /* @overload invert_mask
1770
-
1771
- (Masking, Destructive)
1772
- Inverts mask state.
1773
- */
1774
-
1775
- VALUE
1776
- rb_ca_invert_mask (VALUE self)
1777
- {
1778
- CArray *ca;
1779
- rb_ca_modify(self);
1780
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1781
- ca_invert_mask(ca);
1782
- return self;
1783
- }
1784
-
1785
1857
  /* guard_undef(*values, fill_value: UNDEF, &block) — class method.
1786
1858
  Returns fill_value if any of values is UNDEF, otherwise yields values
1787
1859
  to the block and returns its result.
@@ -1814,32 +1886,26 @@ rb_ca_s_guard_undef(int argc, VALUE *argv, VALUE klass)
1814
1886
  key (`:eq, v`) is evaluated on the Face, where it compares values rather
1815
1887
  than storage. */
1816
1888
  static VALUE
1817
- ca_mask_copy_where (VALUE self, int argc, VALUE *argv)
1889
+ ca_mask_copy_where (volatile VALUE self, int argc, VALUE *argv)
1818
1890
  {
1819
1891
  CArray *ca;
1820
1892
  volatile VALUE obj;
1821
- VALUE *all_args = ALLOCA_N(VALUE, argc + 1);
1822
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1893
+ VALUE sel; /* its address is taken, so it stays on the stack */
1894
+ GetCArray(self, ca);
1823
1895
  if ( ca_is_face(ca) && ca_is_readonly(ca) ) {
1824
1896
  obj = rb_ca_copy(rb_ca_strip_face_value(self));
1825
1897
  if ( argc >= 1 && SYMBOL_P(argv[0]) ) {
1826
- all_args[0] = rb_funcallv(self, SYM2ID(argv[0]), argc - 1, argv + 1);
1827
- all_args[1] = CA_UNDEF;
1828
- rb_funcallv(obj, rb_intern("[]="), 2, all_args);
1898
+ sel = rb_funcallv(self, SYM2ID(argv[0]), argc - 1, argv + 1);
1899
+ rb_ca_store2(obj, 1, &sel, CA_UNDEF);
1829
1900
  }
1830
1901
  else {
1831
- memcpy(all_args, argv, argc * sizeof(VALUE));
1832
- all_args[argc] = CA_UNDEF;
1833
- rb_funcallv(obj, rb_intern("[]="), argc + 1, all_args);
1902
+ rb_ca_store2(obj, argc, argv, CA_UNDEF);
1834
1903
  }
1835
1904
  return rb_funcall(obj, rb_intern("face_lift"), 1, self);
1836
1905
  }
1837
1906
  obj = rb_ca_copy(self);
1838
- memcpy(all_args, argv, argc * sizeof(VALUE));
1839
- all_args[argc] = CA_UNDEF;
1840
- /* a CScalar takes no index; its one-cell view does */
1841
- rb_funcallv(ca_is_scalar(ca) ? rb_funcall(obj, rb_intern("flatten"), 0) : obj,
1842
- rb_intern("[]="), argc + 1, all_args);
1907
+ /* rb_ca_store2 indexes a CScalar as a one-cell array */
1908
+ rb_ca_store2(obj, argc, argv, CA_UNDEF);
1843
1909
  return obj;
1844
1910
  }
1845
1911
 
@@ -1865,6 +1931,7 @@ rb_ca_mask_invalid (VALUE self)
1865
1931
  static VALUE
1866
1932
  rb_ca_mask_where (int argc, VALUE *argv, VALUE self)
1867
1933
  {
1934
+ rb_reject_options(rb_pop_options(&argc, &argv));
1868
1935
  if (argc == 0) {
1869
1936
  rb_raise(rb_eArgError, "mask_where requires at least 1 argument");
1870
1937
  }
@@ -1892,7 +1959,7 @@ the mask states of <code>self</code> and arrays given in arguments.
1892
1959
  */
1893
1960
 
1894
1961
  static VALUE
1895
- rb_ca_inherit_mask_method (int argc, VALUE *argv, VALUE self)
1962
+ rb_ca_inherit_mask_method (int argc, VALUE *argv, volatile VALUE self)
1896
1963
  {
1897
1964
  CArray **slist;
1898
1965
  volatile VALUE holder;
@@ -1901,12 +1968,13 @@ rb_ca_inherit_mask_method (int argc, VALUE *argv, VALUE self)
1901
1968
 
1902
1969
  rb_ca_modify(self);
1903
1970
 
1904
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1971
+ GetCArray(self, ca);
1905
1972
 
1906
1973
  slist = ALLOCV_N(CArray *, holder, argc);
1907
1974
  for (i=0; i<argc; i++) {
1908
1975
  if ( rb_obj_is_carray(argv[i]) ) {
1909
- TypedData_Get_Struct(argv[i], CArray, &carray_data_type, cs);
1976
+ /* gc: argv holds it for the call */
1977
+ GetCArray(argv[i], cs);
1910
1978
  ca_inherit_mask_check_source(ca, cs);
1911
1979
  slist[i] = cs;
1912
1980
  }
@@ -1932,9 +2000,9 @@ rb_ca_inherit_mask_n (VALUE self, int n, VALUE *rothers)
1932
2000
  /* api: rb_ca_inherit_mask */
1933
2001
 
1934
2002
  VALUE
1935
- rb_ca_inherit_mask (VALUE self, int n, ...)
2003
+ rb_ca_inherit_mask (volatile VALUE self, int n, ...)
1936
2004
  {
1937
- VALUE other;
2005
+ volatile VALUE other;
1938
2006
  CArray **slist;
1939
2007
  volatile VALUE holder;
1940
2008
  CArray *ca, *cs;
@@ -1943,14 +2011,14 @@ rb_ca_inherit_mask (VALUE self, int n, ...)
1943
2011
 
1944
2012
  rb_ca_modify(self);
1945
2013
 
1946
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2014
+ GetCArray(self, ca);
1947
2015
 
1948
2016
  va_start(rothers, n);
1949
2017
  slist = ALLOCV_N(CArray *, holder, n);
1950
2018
  for (i=0; i<n; i++) {
1951
2019
  other = va_arg(rothers, VALUE);
1952
2020
  if ( rb_obj_is_carray(other) ) {
1953
- TypedData_Get_Struct(other, CArray, &carray_data_type, cs);
2021
+ GetCArray(other, cs);
1954
2022
  ca_inherit_mask_check_source(ca, cs);
1955
2023
  slist[i] = cs;
1956
2024
  }
@@ -1975,7 +2043,7 @@ from `CArray#inherit_mask`)
1975
2043
  */
1976
2044
 
1977
2045
  static VALUE
1978
- rb_ca_inherit_mask_replace_method (int argc, VALUE *argv, VALUE self)
2046
+ rb_ca_inherit_mask_replace_method (int argc, VALUE *argv, volatile VALUE self)
1979
2047
  {
1980
2048
  CArray **slist;
1981
2049
  volatile VALUE holder;
@@ -1984,12 +2052,13 @@ rb_ca_inherit_mask_replace_method (int argc, VALUE *argv, VALUE self)
1984
2052
 
1985
2053
  rb_ca_modify(self);
1986
2054
 
1987
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2055
+ GetCArray(self, ca);
1988
2056
 
1989
2057
  slist = ALLOCV_N(CArray *, holder, argc);
1990
2058
  for (i=0; i<argc; i++) {
1991
2059
  if ( rb_obj_is_carray(argv[i]) ) {
1992
- TypedData_Get_Struct(argv[i], CArray, &carray_data_type, cs);
2060
+ /* gc: argv holds it for the call */
2061
+ GetCArray(argv[i], cs);
1993
2062
  ca_inherit_mask_check_source(ca, cs);
1994
2063
  slist[i] = cs;
1995
2064
  }
@@ -2015,9 +2084,9 @@ rb_ca_inherit_mask_replace_n (VALUE self, int n, VALUE *rothers)
2015
2084
  /* api: rb_ca_inherit_mask_replace */
2016
2085
 
2017
2086
  VALUE
2018
- rb_ca_inherit_mask_replace (VALUE self, int n, ...)
2087
+ rb_ca_inherit_mask_replace (volatile VALUE self, int n, ...)
2019
2088
  {
2020
- VALUE other;
2089
+ volatile VALUE other;
2021
2090
  CArray **slist;
2022
2091
  volatile VALUE holder;
2023
2092
  CArray *ca, *cs;
@@ -2026,14 +2095,14 @@ rb_ca_inherit_mask_replace (VALUE self, int n, ...)
2026
2095
 
2027
2096
  rb_ca_modify(self);
2028
2097
 
2029
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2098
+ GetCArray(self, ca);
2030
2099
 
2031
2100
  va_start(rothers, n);
2032
2101
  slist = ALLOCV_N(CArray *, holder, n);
2033
2102
  for (i=0; i<n; i++) {
2034
2103
  other = va_arg(rothers, VALUE);
2035
2104
  if ( rb_obj_is_carray(other) ) {
2036
- TypedData_Get_Struct(other, CArray, &carray_data_type, cs);
2105
+ GetCArray(other, cs);
2037
2106
  ca_inherit_mask_check_source(ca, cs);
2038
2107
  slist[i] = cs;
2039
2108
  }
@@ -2060,7 +2129,7 @@ ca_count_uniform (CArray *ca, const int8_t *axes, int8_t naxes, ca_size_t fill)
2060
2129
  int8_t ndim = 0;
2061
2130
  int i, j;
2062
2131
  int64_t v = (int64_t) fill;
2063
- VALUE result;
2132
+ volatile VALUE result;
2064
2133
  CArray *co;
2065
2134
 
2066
2135
  if ( naxes == ca->ndim ) {
@@ -2074,7 +2143,7 @@ ca_count_uniform (CArray *ca, const int8_t *axes, int8_t naxes, ca_size_t fill)
2074
2143
  if (!is_axis) dim[ndim++] = ca->dim[i];
2075
2144
  }
2076
2145
  result = rb_carray_new(CA_INT64, ndim, dim, 0, NULL);
2077
- TypedData_Get_Struct(result, CArray, &carray_data_type, co);
2146
+ GetCArray(result, co);
2078
2147
  ca_fill(co, &v);
2079
2148
  return result;
2080
2149
  }
@@ -2098,11 +2167,11 @@ extern VALUE rb_ca_count_false_ki (int argc, VALUE *argv, VALUE self);
2098
2167
  Public (non-static) linkage so rb_ca_count in carray_count.c can forward
2099
2168
  count(UNDEF) here directly without going through Ruby method dispatch. */
2100
2169
  static VALUE
2101
- ca_count_by_mask (int argc, VALUE *argv, VALUE self, int masked)
2170
+ ca_count_by_mask (int argc, VALUE *argv, volatile VALUE self, int masked)
2102
2171
  {
2103
2172
  CArray *ca;
2104
2173
  VALUE kw_hash = Qnil, mask_obj;
2105
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2174
+ GetCArray(self, ca);
2106
2175
  rb_scan_args(argc, argv, "0:", &kw_hash);
2107
2176
 
2108
2177
  if ( ! ca_has_mask(ca) ) {
@@ -2123,7 +2192,7 @@ ca_count_by_mask (int argc, VALUE *argv, VALUE self, int masked)
2123
2192
  }
2124
2193
  {
2125
2194
  ca_size_t one[CA_RANK_MAX];
2126
- VALUE shape = rb_ary_new_capa(ca->ndim), cell;
2195
+ volatile VALUE shape = rb_ary_new_capa(ca->ndim), cell;
2127
2196
  boolean8_t zero = 0;
2128
2197
  CArray *cc;
2129
2198
  int i;
@@ -2132,7 +2201,7 @@ ca_count_by_mask (int argc, VALUE *argv, VALUE self, int masked)
2132
2201
  rb_ary_push(shape, SIZE2NUM(ca->dim[i]));
2133
2202
  }
2134
2203
  cell = rb_carray_new(CA_BOOLEAN, ca->ndim, one, 0, NULL);
2135
- TypedData_Get_Struct(cell, CArray, &carray_data_type, cc);
2204
+ GetCArray(cell, cc);
2136
2205
  ca_fill(cc, &zero);
2137
2206
  mask_obj = rb_funcallv(cell, rb_intern("broadcast_to"),
2138
2207
  (int) RARRAY_LEN(shape), RARRAY_CONST_PTR(shape));
@@ -2253,6 +2322,20 @@ ca_binop_bit_and_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2253
2322
  }
2254
2323
 
2255
2324
  /* ----- bit_or_i (boolean8_t fast) ----- */
2325
+ /* Every cell gets 0 or 1, even beside a masked cell: its byte is not
2326
+ defined (any value), and a cell Kleene resolves to true keeps the value
2327
+ written here. The word path folds each byte's eight bits into its low
2328
+ bit; a shift by less than 8 moves bits of the next byte only into the
2329
+ high bits, which the last mask drops. */
2330
+ static inline uint64_t
2331
+ ca_mask_word_nonzero_bytes (uint64_t w)
2332
+ {
2333
+ w |= w >> 4;
2334
+ w |= w >> 2;
2335
+ w |= w >> 1;
2336
+ return w & CA_MASK_WORD_LSB;
2337
+ }
2338
+
2256
2339
  static void
2257
2340
  ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2258
2341
  char *ptr1, ca_size_t i1,
@@ -2273,7 +2356,7 @@ ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2273
2356
  p1 = q1 + k*i1;
2274
2357
  p2 = q2 + k*i2;
2275
2358
  p3 = q3 + k*i3;
2276
- (*p3) = (*p1) | (*p2);
2359
+ (*p3) = ( (*p1) | (*p2) ) ? 1 : 0;
2277
2360
  }
2278
2361
  }
2279
2362
  }
@@ -2284,18 +2367,21 @@ ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2284
2367
  ca_size_t j;
2285
2368
  for (j = 0; j < nw; j++) {
2286
2369
  ca_mask_word_store(q3 + j*CA_MASK_WORD_BYTES,
2287
- ca_mask_word_load(q1 + j*CA_MASK_WORD_BYTES) | ca_mask_word_load(q2 + j*CA_MASK_WORD_BYTES));
2370
+ ca_mask_word_nonzero_bytes(
2371
+ ca_mask_word_load(q1 + j*CA_MASK_WORD_BYTES) |
2372
+ ca_mask_word_load(q2 + j*CA_MASK_WORD_BYTES)));
2288
2373
  }
2289
2374
  for (j = nw * CA_MASK_WORD_BYTES; j < n; j++) {
2290
- q3[j] = q1[j] | q2[j];
2375
+ q3[j] = ( q1[j] | q2[j] ) ? 1 : 0;
2291
2376
  }
2292
2377
  }
2293
2378
  else {
2379
+ /* non-contig view: byte loop */
2294
2380
  for (k = 0; k < n; k++) {
2295
2381
  p1 = q1 + k*i1;
2296
2382
  p2 = q2 + k*i2;
2297
2383
  p3 = q3 + k*i3;
2298
- (*p3) = (*p1) | (*p2);
2384
+ (*p3) = ( (*p1) | (*p2) ) ? 1 : 0;
2299
2385
  }
2300
2386
  }
2301
2387
  }
@@ -2407,8 +2493,8 @@ ca_inherit_mask_check_args (int argc, VALUE *argv, const char *name)
2407
2493
  for (i=0; i<RARRAY_LEN(rest); i++) {
2408
2494
  VALUE v = RARRAY_AREF(rest, i);
2409
2495
  if ( ! rb_obj_is_carray(v) ) {
2410
- rb_raise(rb_eTypeError, "%s: argument %ld must be a CArray (got %s)",
2411
- name, i, rb_obj_classname(v));
2496
+ rb_raise(rb_eTypeError, "%s: argument %ld must be a CArray (got %"PRIsVALUE")",
2497
+ name, i, rb_obj_class(v));
2412
2498
  }
2413
2499
  }
2414
2500
  return rest;
@@ -2463,7 +2549,6 @@ Init_carray_mask (void)
2463
2549
  (arity 1, no default), matching the "all arguments mandatory"
2464
2550
  regularity of the mask SET family. */
2465
2551
  rb_define_method(rb_cCArray, "strip_mask", rb_ca_mask_fill_copy, 1);
2466
- rb_define_method(rb_cCArray, "invert_mask", rb_ca_invert_mask, 0);
2467
2552
  rb_define_singleton_method(rb_cCArray, "guard_undef", rb_ca_s_guard_undef, -1);
2468
2553
  rb_define_method(rb_cCArray, "mask_eq", rb_ca_mask_eq, 1);
2469
2554
  rb_define_method(rb_cCArray, "mask_invalid", rb_ca_mask_invalid, 0);