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
data/ext/carray_lazy.c CHANGED
@@ -788,6 +788,21 @@ ca_lazy_arena_acquire_object (ca_size_t n_elements)
788
788
  return ptr;
789
789
  }
790
790
 
791
+ /* The caller is about to overwrite a scratch's VALUEs with cells of another
792
+ data type: from here on they are not to be marked. */
793
+ void
794
+ ca_lazy_arena_untag_objects (void *ptr)
795
+ {
796
+ ca_lazy_arena_t *arena = ca_lazy_arena_get();
797
+ int i;
798
+ for ( i = 0; i < CA_LAZY_ARENA_SLOTS; i++ ) {
799
+ if ( arena->slots[i].ptr == ptr ) {
800
+ arena->slots[i].object_elements = 0;
801
+ return;
802
+ }
803
+ }
804
+ }
805
+
791
806
  static void
792
807
  ca_lazy_arena_mark_object_slots (ca_lazy_arena_t *arena)
793
808
  {
@@ -1084,6 +1099,53 @@ ca_lazy_settle_scalar (VALUE operand, int was_scalar)
1084
1099
  return operand;
1085
1100
  }
1086
1101
 
1102
+ typedef struct {
1103
+ ca_lazy_eval_fn eval;
1104
+ void *node;
1105
+ const ca_lazy_req_t *req;
1106
+ void *data;
1107
+ int dir;
1108
+ } ca_lazy_eval_args_t;
1109
+
1110
+ static VALUE
1111
+ ca_lazy_eval_body (VALUE varg)
1112
+ {
1113
+ ca_lazy_eval_args_t *a = (ca_lazy_eval_args_t *) varg;
1114
+ a->eval(a->node, a->req, a->data, a->dir);
1115
+ /* A cell computed as an expression over a node being evaluated (x + 1
1116
+ for a cell holding x) stays lazy, and comparing or computing it later
1117
+ evaluates the node again without end: refuse it here, while the node
1118
+ is still recorded. */
1119
+ if ( a->dir == CA_XFER_GET && ca_is_object_type((CArray *) a->node) ) {
1120
+ ca_refuse_cells_reaching_evaluation(a->req->n, (const VALUE *) a->data);
1121
+ }
1122
+ return Qnil;
1123
+ }
1124
+
1125
+ void
1126
+ ca_lazy_eval_recorded (void *node, int n, CArray **ops,
1127
+ int nchain, CArray **chain, ca_lazy_eval_fn eval,
1128
+ const ca_lazy_req_t *req, void *data, int dir)
1129
+ {
1130
+ CArray **rec;
1131
+ ca_lazy_eval_args_t a;
1132
+ int i, m = 0, object = ca_is_object_type((CArray *) node);
1133
+ for ( i = 0; i < n; i++ ) {
1134
+ if ( ops[i] && ca_is_object_type(ops[i]) ) object = 1;
1135
+ }
1136
+ if ( ! object ) {
1137
+ eval(node, req, data, dir);
1138
+ return;
1139
+ }
1140
+ rec = ALLOCA_N(CArray *, nchain + 1);
1141
+ rec[m++] = (CArray *) node;
1142
+ for ( i = 0; i < nchain; i++ ) {
1143
+ rec[m++] = chain[i];
1144
+ }
1145
+ a.eval = eval; a.node = node; a.req = req; a.data = data; a.dir = dir;
1146
+ ca_computing_run_nodes(m, rec, ca_lazy_eval_body, (VALUE) &a);
1147
+ }
1148
+
1087
1149
  void
1088
1150
  ca_lazy_req_pull (void *operand, const ca_lazy_req_t *req, void *buf, int dir)
1089
1151
  {
@@ -1239,7 +1301,7 @@ ca_mask_of_operands_func_clone (void *ap)
1239
1301
  static int
1240
1302
  ca_mask_of_operands_operand_is_scalar (CAMaskOfOperands *lm, CArray *op)
1241
1303
  {
1242
- return ( op->elements == 1 && lm->elements > 1 );
1304
+ return op->elements == 1;
1243
1305
  }
1244
1306
 
1245
1307
  static void
data/ext/carray_loop.c CHANGED
@@ -20,6 +20,7 @@ rb_ca_s_each_index_internal (int ndim, VALUE *dim, uint8_t indim, VALUE ridx)
20
20
  int32_t is_leaf = (indim == ndim - 1);
21
21
  ca_size_t i;
22
22
 
23
+ if ( ! NIL_P(dim[indim]) ) ca_integer_arg(dim[indim], "a dimension", "each_index");
23
24
  if ( NIL_P(dim[indim]) ) {
24
25
  rb_ary_store(ridx, indim, Qnil);
25
26
  if ( is_leaf ) {
@@ -140,15 +141,21 @@ rb_ca_index_walk (VALUE self, CArray *ca, int8_t level,
140
141
  that want the whole subscript should write |*idx|. */
141
142
  if ( mode & CA_LOOP_WITH_VALUE ) {
142
143
  int argc = (int)ca->ndim + 1;
143
- argv[0] = rb_ca_fetch_index(self, idx);
144
+ argv[0] = ( mode & CA_LOOP_STORE )
145
+ ? rb_ca_fetch_addr(self, ca_index2addr(ca, idx))
146
+ : rb_ca_fetch_index(self, idx);
144
147
  MEMCPY(argv + 1, RARRAY_CONST_PTR(ridx), VALUE, ca->ndim);
145
148
  obj = rb_yield_values2(argc, argv);
146
149
  }
147
150
  else {
148
151
  obj = rb_yield_values2((int)ca->ndim, RARRAY_CONST_PTR(ridx));
149
152
  }
153
+ /* map_index! writes in self's attach window. The buffer is what the
154
+ sync sends to the parent; a store by subscript goes past it to the
155
+ root and the sync then puts the old values back (rule R5). The
156
+ flat address reads and writes the buffer, as map! does. */
150
157
  if ( mode & CA_LOOP_STORE ) {
151
- rb_ca_store_index(self, idx, obj);
158
+ rb_ca_store_addr(self, ca_index2addr(ca, idx), obj);
152
159
  }
153
160
  ret = obj;
154
161
  }
@@ -247,7 +254,7 @@ ca_map_bang_run (VALUE self, VALUE (*body)(VALUE), int mode)
247
254
  c.self = self;
248
255
  c.ridx = rb_ary_new2(c.ca->ndim);
249
256
  c.mode = mode;
250
- ca_attach_window(1, &c.ca, "w", body, (VALUE) &c);
257
+ ca_attach_window_yielding(self, 1, &c.ca, "w", body, (VALUE) &c);
251
258
  return self;
252
259
  }
253
260
 
data/ext/carray_mask.c CHANGED
@@ -13,6 +13,7 @@
13
13
  #include "carray.h"
14
14
  #include "carray_internal.h" /* ca_lazy_arena_* */
15
15
  #include "ca_obj_face.h" /* CA_FACE_LIFT_IF_FACE for the mask-lift path */
16
+ #include <ruby/ractor.h>
16
17
 
17
18
  /* ============================================================================
18
19
  * Word-bulk mask substrate helpers.
@@ -301,7 +302,13 @@ ca_has_mask (void *ap)
301
302
  if ( ca->mask ) { /* mask array already created */
302
303
  return 1;
303
304
  }
304
- else if ( ca_is_value_array(ca) ) {
305
+ else if ( ca_test_flag(ca, CA_FLAG_VALUE_ARRAY) ) {
306
+ /* Only this array's own mark, not ca_is_value_array, which walks to
307
+ the root and would make every level of a view pay again for the
308
+ levels above it. A view below a value array still answers 0: it
309
+ asks its parent, and the question reaches the marked array. No
310
+ level in between has a mask, since ca_create_mask refuses one for
311
+ anything below a value array. */
305
312
  return 0; /* array itself is returned by CArray#value */
306
313
  }
307
314
  else if ( ca_is_entity(ca) ) { /* entity array */
@@ -316,6 +323,11 @@ ca_has_mask (void *ap)
316
323
  }
317
324
  return 0;
318
325
  }
326
+ /* The one level that answers without asking its parent: a value
327
+ array above it has to be looked for here. */
328
+ if ( ca_is_value_array(ca) ) {
329
+ return 0;
330
+ }
319
331
  if ( rb_obj_respond_to(co->self, rb_intern("mask_created?"), Qtrue) ) {
320
332
  return RTEST(rb_funcall(co->self, rb_intern("mask_created?"), 0));
321
333
  }
@@ -412,6 +424,45 @@ ca_view_func_create_mask (void *ap)
412
424
  CA_BOOLEAN, ca->ndim, ca->dim, 0, 0);
413
425
  }
414
426
 
427
+ /* The sync slot of a view whose mask came from ca_view_func_create_mask
428
+ calls this after syncing its parent. The view's mask is a refer over
429
+ 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
431
+ parent's buffer to the root. Syncing the parent then writes its
432
+ buffer, from before the view's mask was written, back over the root,
433
+ so the view's mask is synced once more. */
434
+
435
+ /* The sync slot of a view that maps its cells one to one onto its parent's
436
+ (a fake, a cast, a byte swap, a Face) calls this before syncing its
437
+ parent. The view's mask may have been written past the parent's mask
438
+ buffer, straight to the root (by a view over it), so the parent's
439
+ buffer can still hold the bits from before. The parent's own sync
440
+ decides from that buffer which cells it sends back, so the view's bits
441
+ are copied into it first. */
442
+
443
+ void
444
+ ca_view_pass_mask_to_parent (void *ap)
445
+ {
446
+ CAView *ca = (CAView *) ap;
447
+ if ( ca->mask != NULL && ca->mask->ptr != NULL
448
+ && ca->parent->mask != NULL && ca->parent->mask->ptr != NULL
449
+ && ca->mask->ptr != ca->parent->mask->ptr
450
+ && ca->elements == ca->parent->elements ) {
451
+ memmove(ca->parent->mask->ptr, ca->mask->ptr, (size_t) ca->elements);
452
+ }
453
+ }
454
+
455
+ void
456
+ ca_view_sync_mask_after_parent (void *ap)
457
+ {
458
+ CAView *ca = (CAView *) ap;
459
+ if ( ca->mask != NULL && ca->mask->ptr != NULL
460
+ && ca->parent->mask != NULL && ca->parent->mask->ptr != NULL
461
+ && ca_is_view(ca->parent->mask) ) {
462
+ ca_sync(ca->mask);
463
+ }
464
+ }
465
+
415
466
  /* create mask array if array has mask but has not mask array */
416
467
 
417
468
  void
@@ -441,20 +492,27 @@ ca_mask_standin_new (CArray *parent)
441
492
  return z;
442
493
  }
443
494
 
444
- /* A value array or a mask array: a parent that can never be given a mask,
445
- whose stand-in therefore stays in place for good. */
446
- static int
447
- ca_mask_standin_parent_unmaskable (CArray *parent)
495
+ /* A stack or meld refuses a value array or a mask array as a part: such an
496
+ array can have no mask, and a part of a stack or meld is an array like
497
+ any other, which can. */
498
+ void
499
+ ca_multi_parent_check_part (CArray *part, const char *name)
448
500
  {
449
- return ca_is_value_array(parent) || ca_is_mask_array(parent);
501
+ if ( ca_is_value_array(part) ) {
502
+ rb_raise(rb_eArgError,
503
+ "%s: a value array can have no mask and can not be a part", name);
504
+ }
505
+ if ( ca_is_mask_array(part) ) {
506
+ rb_raise(rb_eArgError,
507
+ "%s: a mask array can have no mask and can not be a part", name);
508
+ }
450
509
  }
451
510
 
452
511
  CArray *
453
512
  ca_mask_standin_current (CArray *mask_view, CArray *reading, CArray *parent,
454
513
  CArray *standin)
455
514
  {
456
- if ( standin == NULL || reading != standin
457
- || ca_mask_standin_parent_unmaskable(parent) ) {
515
+ if ( standin == NULL || reading != standin ) {
458
516
  return reading;
459
517
  }
460
518
  /* Not inside an attach window whose buffer is out: that buffer and the
@@ -473,11 +531,69 @@ ca_mask_standin_current (CArray *mask_view, CArray *reading, CArray *parent,
473
531
  return ( parent->mask != NULL ) ? parent->mask : reading;
474
532
  }
475
533
 
534
+ /* The first view from ca up the parent chain whose mask is attached, or
535
+ NULL. An attached mask may alias its root: whether it does is not asked,
536
+ as ca_attach_is_alias answers yes for any view that already has a pointer,
537
+ and the copy made for one that does is a no-op. */
538
+ static CArray *
539
+ ca_mask_buffer_holder (CArray *ca)
540
+ {
541
+ while ( ca != NULL && ca_is_view(ca)
542
+ && ! ca_test_flag(ca, CA_FLAG_MULTI_PARENTS) ) {
543
+ if ( ca->mask != NULL && ca_is_view(ca->mask)
544
+ && ca_is_attached(ca->mask) ) {
545
+ return ca;
546
+ }
547
+ ca = CAVIEW(ca)->parent;
548
+ }
549
+ return NULL;
550
+ }
551
+
552
+ /* Marks the cells addrs of mask, the mask of a view at or below chain. The
553
+ write goes past any buffer an attach window holds on the way to the root,
554
+ and the sync that closes that window sends the buffer: the cells this
555
+ write turned true are copied into it, read as the difference of two
556
+ gathers so that what the window has written there meanwhile stays. */
557
+ static void
558
+ ca_mask_fill_past_buffers (CArray *mask, CArray *chain, ca_size_t m,
559
+ ca_size_t *addrs)
560
+ {
561
+ volatile VALUE holder = 0, reader;
562
+ CArray *owner = ca_mask_buffer_holder(chain);
563
+ CArray *held, *rd;
564
+ boolean8_t *before, *after, *z;
565
+ boolean8_t one = 1;
566
+ ca_size_t i, n;
567
+
568
+ if ( owner == NULL ) {
569
+ ca_fill_addrs(mask, m, addrs, &one);
570
+ return;
571
+ }
572
+ held = owner->mask;
573
+ n = held->elements;
574
+ /* A view answers from its buffer while it has one: the clone has none. */
575
+ rd = (CArray *) ca_clone(held);
576
+ reader = ca_wrap_struct(rd);
577
+ before = ALLOCV_N(boolean8_t, holder, 2 * n);
578
+ after = before + n;
579
+ ca_xfer_all(rd, before, CA_XFER_GET);
580
+ ca_mask_fill_past_buffers(mask, CAVIEW(owner)->parent, m, addrs);
581
+ ca_xfer_all(rd, after, CA_XFER_GET);
582
+ z = (boolean8_t *) held->ptr;
583
+ for ( i = 0; i < n; i++ ) {
584
+ if ( after[i] && ! before[i] ) {
585
+ z[i] = 1;
586
+ }
587
+ }
588
+ ALLOCV_END(holder);
589
+ RB_GC_GUARD(reader);
590
+ }
591
+
476
592
  void
477
593
  ca_mask_standin_settle (CArray *parent, CArray *standin)
478
594
  {
479
595
  volatile VALUE holder = 0;
480
- boolean8_t *z, one = 1;
596
+ boolean8_t *z;
481
597
  ca_size_t *addrs;
482
598
  ca_size_t i, m = 0, n;
483
599
 
@@ -490,23 +606,21 @@ ca_mask_standin_settle (CArray *parent, CArray *standin)
490
606
  if ( i == n ) {
491
607
  return;
492
608
  }
493
- if ( ca_mask_standin_parent_unmaskable(parent) ) {
494
- MEMZERO(z, boolean8_t, n);
495
- rb_raise(rb_eTypeError, "can not create mask array for the %s array",
496
- ca_is_value_array(parent) ? "value" : "mask");
497
- }
498
- ca_create_mask(parent);
499
609
  /* The trues are added to the parent's mask, not copied over it: the
500
- parent may have come to have masked cells of its own meanwhile. */
610
+ parent may have come to have masked cells of its own meanwhile. The
611
+ stand-in is cleared before the parent is asked for a mask, so a
612
+ parent that refuses one leaves it all false, as its readers take it
613
+ to be at rest. */
501
614
  addrs = ALLOCV_N(ca_size_t, holder, n);
502
615
  for ( ; i < n; i++ ) {
503
616
  if ( z[i] ) {
504
617
  addrs[m++] = i;
505
618
  }
506
619
  }
507
- ca_fill_addrs(parent->mask, m, addrs, &one);
508
- ALLOCV_END(holder);
509
620
  MEMZERO(z, boolean8_t, n);
621
+ ca_create_mask(parent);
622
+ ca_mask_fill_past_buffers(parent->mask, parent, m, addrs);
623
+ ALLOCV_END(holder);
510
624
  }
511
625
 
512
626
  CArray *
@@ -539,136 +653,85 @@ ca_multi_parent_mask_refresh (CArray *mask, CArray **parents,
539
653
  CAVIEW(mask)->parent = mp->parents[0];
540
654
  }
541
655
 
542
- typedef struct {
543
- CArray *mask;
544
- CArray **blanks;
545
- CArray **saved;
546
- CArray *saved_parent;
547
- int32_t n;
548
- int finished;
549
- VALUE (*body)(VALUE);
550
- VALUE arg;
551
- } ca_mp_trial_t;
552
-
553
- static VALUE
554
- ca_mp_trial_body (VALUE p)
555
- {
556
- ca_mp_trial_t *t = (ca_mp_trial_t *) p;
557
- t->body(t->arg);
558
- t->finished = 1;
559
- return Qnil;
560
- }
561
-
562
- static void
563
- ca_mp_trial_free_blanks (ca_mp_trial_t *t)
564
- {
565
- int32_t k;
566
- for ( k = 0; k < t->n; k++ ) {
567
- ca_free(t->blanks[k]);
568
- t->blanks[k] = NULL;
569
- }
570
- }
571
-
572
- static VALUE
573
- ca_mp_trial_restore (VALUE p)
574
- {
575
- ca_mp_trial_t *t = (ca_mp_trial_t *) p;
576
- CAMultiParent *mp = (CAMultiParent *) t->mask;
577
- int32_t k;
578
- for ( k = 0; k < t->n; k++ ) {
579
- mp->parents[k] = t->saved[k];
580
- }
581
- CAVIEW(t->mask)->parent = t->saved_parent;
582
- if ( ! t->finished ) {
583
- ca_mp_trial_free_blanks(t);
584
- }
585
- return Qnil;
586
- }
587
-
656
+ /* Before a write that puts a true into the mask of a stack or meld (not one
657
+ with its buffer out): each part still reading its stand-in that shares
658
+ its root with another part is given its own mask, and the write goes to
659
+ those parts' masks in their order. Parts that share cells then take the
660
+ write of the last of them, as they do for values; through stand-ins,
661
+ settled afterwards, the trues would be added together. A part that
662
+ shares nothing keeps its stand-in, and gets no mask from falses. */
588
663
  void
589
- ca_multi_parent_mask_refuse_first (CArray *mask, CArray **parents,
590
- CArray **standins, int32_t n,
591
- VALUE (*body)(VALUE), VALUE arg)
664
+ ca_multi_parent_mask_adopt (CArray *mask, CArray **parents,
665
+ CArray **standins, int32_t n)
592
666
  {
593
667
  CAMultiParent *mp = (CAMultiParent *) mask;
594
- volatile VALUE h_blank = 0, h_saved = 0;
595
- ca_mp_trial_t t;
596
- int32_t k, refused = -1;
597
- if ( standins == NULL ) {
668
+ int32_t k, j;
669
+ if ( standins == NULL || mask->ptr != NULL ) {
598
670
  return;
599
671
  }
600
672
  for ( k = 0; k < n; k++ ) {
601
- if ( standins[k] != NULL && mp->parents[k] == standins[k]
602
- && ca_mask_standin_parent_unmaskable(parents[k]) ) {
603
- break;
673
+ if ( standins[k] == NULL || mp->parents[k] != standins[k]
674
+ || ca_test_flag(standins[k], CA_FLAG_READ_ONLY) ) {
675
+ continue;
604
676
  }
605
- }
606
- if ( k == n ) {
607
- return;
608
- }
609
- t.mask = mask;
610
- t.n = n;
611
- t.finished = 0;
612
- t.body = body;
613
- t.arg = arg;
614
- t.saved_parent = CAVIEW(mask)->parent;
615
- t.blanks = ALLOCV_N(CArray *, h_blank, n);
616
- t.saved = ALLOCV_N(CArray *, h_saved, n);
617
- for ( k = 0; k < n; k++ ) {
618
- t.blanks[k] = NULL;
619
- }
620
- for ( k = 0; k < n; k++ ) {
621
- CArray *m = mp->parents[k];
622
- t.saved[k] = m;
623
- t.blanks[k] = carray_new(CA_BOOLEAN, m->ndim, m->dim, 0, NULL);
624
- MEMZERO(t.blanks[k]->ptr, boolean8_t, t.blanks[k]->elements);
625
- }
626
- for ( k = 0; k < n; k++ ) {
627
- mp->parents[k] = t.blanks[k];
628
- }
629
- CAVIEW(mask)->parent = t.blanks[0];
630
- rb_ensure(ca_mp_trial_body, (VALUE) &t, ca_mp_trial_restore, (VALUE) &t);
631
- for ( k = 0; k < n && refused < 0; k++ ) {
632
- CArray *z = t.blanks[k];
633
- ca_size_t i;
634
- if ( standins[k] == NULL || t.saved[k] != standins[k]
635
- || ! ca_mask_standin_parent_unmaskable(parents[k]) ) {
677
+ for ( j = 0; j < n && ( j == k || ! ca_shares_root(parents[j], parents[k]) ); j++ ) ;
678
+ if ( j == n ) {
636
679
  continue;
637
680
  }
638
- for ( i = 0; i < z->elements && ! ((boolean8_t *) z->ptr)[i]; i++ ) ;
639
- if ( i < z->elements ) {
640
- refused = k;
681
+ ca_create_mask(parents[k]);
682
+ if ( parents[k]->mask != NULL ) {
683
+ mp->parents[k] = parents[k]->mask;
641
684
  }
642
685
  }
643
- ca_mp_trial_free_blanks(&t);
644
- ALLOCV_END(h_blank);
645
- ALLOCV_END(h_saved);
646
- if ( refused >= 0 ) {
647
- rb_raise(rb_eTypeError, "can not create mask array for the %s array",
648
- ca_is_value_array(parents[refused]) ? "value" : "mask");
649
- }
686
+ CAVIEW(mask)->parent = mp->parents[0];
687
+ }
688
+
689
+ typedef struct {
690
+ CArray *parent;
691
+ CArray *standin;
692
+ } ca_standin_settle_arg;
693
+
694
+ static VALUE
695
+ ca_standin_settle_body (VALUE arg)
696
+ {
697
+ ca_standin_settle_arg *a = (ca_standin_settle_arg *) arg;
698
+ ca_mask_standin_settle(a->parent, a->standin);
699
+ return Qnil;
650
700
  }
651
701
 
702
+ /* Every part is settled, past a parent that refuses a mask, so no part keeps
703
+ a true in its stand-in; then the first exception is raised. */
652
704
  void
653
705
  ca_multi_parent_mask_settle (CArray *mask, CArray **parents,
654
706
  CArray **standins, int32_t n)
655
707
  {
656
708
  CAMultiParent *mp = (CAMultiParent *) mask;
657
- int32_t k, pass;
709
+ volatile VALUE error = Qnil;
710
+ int32_t k;
658
711
  if ( standins == NULL ) {
659
712
  return;
660
713
  }
661
- /* The parts that can have no mask first, so that a refused true leaves
662
- the other parts as they were. */
663
- for ( pass = 0; pass < 2; pass++ ) {
664
- for ( k = 0; k < n; k++ ) {
665
- if ( standins[k] == NULL || mp->parents[k] != standins[k]
666
- || ca_mask_standin_parent_unmaskable(parents[k]) != ( pass == 0 ) ) {
667
- continue;
714
+ for ( k = 0; k < n; k++ ) {
715
+ if ( standins[k] != NULL && mp->parents[k] == standins[k] ) {
716
+ ca_standin_settle_arg a = { parents[k], standins[k] };
717
+ int tag = 0;
718
+ /* protect: goes on to the other parts; nothing to give back */
719
+ rb_protect(ca_standin_settle_body, (VALUE) &a, &tag);
720
+ if ( tag ) {
721
+ VALUE err = rb_errinfo();
722
+ if ( ! RTEST(rb_obj_is_kind_of(err, rb_eException)) ) {
723
+ rb_jump_tag(tag);
724
+ }
725
+ if ( NIL_P(error) ) {
726
+ error = err;
727
+ }
728
+ rb_set_errinfo(Qnil);
668
729
  }
669
- ca_mask_standin_settle(parents[k], standins[k]);
670
730
  }
671
731
  }
732
+ if ( ! NIL_P(error) ) {
733
+ rb_exc_raise(error);
734
+ }
672
735
  }
673
736
 
674
737
  void
@@ -797,6 +860,9 @@ typedef struct {
797
860
  int k; /* entries in list */
798
861
  int ns; /* sources */
799
862
  ca_size_t elements;
863
+ CArray *dest; /* the destination */
864
+ int overwrite; /* clear the destination's mask first */
865
+ char *modes; /* attach modes, one per entry of list */
800
866
  } ca_mask_overlay_t;
801
867
 
802
868
  static VALUE
@@ -823,6 +889,31 @@ ca_mask_overlay_body (VALUE arg)
823
889
  return Qnil;
824
890
  }
825
891
 
892
+ /* Clear the destination's mask if asked, and OR the sources into it. */
893
+ static VALUE
894
+ ca_mask_overlay_write (VALUE arg)
895
+ {
896
+ ca_mask_overlay_t *o = (ca_mask_overlay_t *) arg;
897
+ CArray *ca = o->dest;
898
+ int i;
899
+ if ( o->overwrite ) {
900
+ boolean8_t zero = 0;
901
+ ca_fill(ca->mask, &zero);
902
+ }
903
+ o->k = 0;
904
+ o->list[o->k] = ca->mask;
905
+ o->modes[o->k++] = 'w';
906
+ for (i=0; i<o->ns; i++) {
907
+ if ( o->att[i] ) {
908
+ o->list[o->k] = o->att[i];
909
+ o->modes[o->k++] = 'r';
910
+ }
911
+ }
912
+ o->modes[o->k] = '\0';
913
+ ca_attach_window(o->k, o->list, o->modes, ca_mask_overlay_body, arg);
914
+ return Qnil;
915
+ }
916
+
826
917
  /* overwrite: the destination's mask is cleared before the sources are
827
918
  OR'd in (and left alone when it has none and no source has a mask). */
828
919
  static void
@@ -907,22 +998,10 @@ ca_copy_mask_overlay_core (CArray *ca, ca_size_t elements, int n,
907
998
  }
908
999
 
909
1000
  ca_create_mask(ca);
910
- if ( overwrite ) {
911
- boolean8_t zero = 0;
912
- ca_fill(ca->mask, &zero);
913
- }
914
-
915
- o.k = 0;
916
- o.list[o.k] = ca->mask;
917
- modes[o.k++] = 'w';
918
- for (i=0; i<o.ns; i++) {
919
- if ( o.att[i] ) {
920
- o.list[o.k] = o.att[i];
921
- modes[o.k++] = 'r';
922
- }
923
- }
924
- modes[o.k] = '\0';
925
- ca_attach_window(o.k, o.list, modes, ca_mask_overlay_body, (VALUE) &o);
1001
+ o.dest = ca;
1002
+ o.overwrite = overwrite;
1003
+ o.modes = modes;
1004
+ ca_mask_overlay_write((VALUE) &o);
926
1005
 
927
1006
  for (i=0; i<n; i++) {
928
1007
  if ( holders[i] ) {
@@ -1848,7 +1927,9 @@ ca_mask_copy_where (VALUE self, int argc, VALUE *argv)
1848
1927
  obj = rb_ca_copy(self);
1849
1928
  memcpy(all_args, argv, argc * sizeof(VALUE));
1850
1929
  all_args[argc] = CA_UNDEF;
1851
- rb_funcallv(obj, rb_intern("[]="), argc + 1, all_args);
1930
+ /* a CScalar takes no index; its one-cell view does */
1931
+ rb_funcallv(ca_is_scalar(ca) ? rb_funcall(obj, rb_intern("flatten"), 0) : obj,
1932
+ rb_intern("[]="), argc + 1, all_args);
1852
1933
  return obj;
1853
1934
  }
1854
1935
 
@@ -1943,7 +2024,7 @@ rb_ca_inherit_mask_n (VALUE self, int n, VALUE *rothers)
1943
2024
  VALUE
1944
2025
  rb_ca_inherit_mask (VALUE self, int n, ...)
1945
2026
  {
1946
- VALUE other;
2027
+ volatile VALUE other;
1947
2028
  CArray **slist;
1948
2029
  volatile VALUE holder;
1949
2030
  CArray *ca, *cs;
@@ -2026,7 +2107,7 @@ rb_ca_inherit_mask_replace_n (VALUE self, int n, VALUE *rothers)
2026
2107
  VALUE
2027
2108
  rb_ca_inherit_mask_replace (VALUE self, int n, ...)
2028
2109
  {
2029
- VALUE other;
2110
+ volatile VALUE other;
2030
2111
  CArray **slist;
2031
2112
  volatile VALUE holder;
2032
2113
  CArray *ca, *cs;
@@ -2262,6 +2343,20 @@ ca_binop_bit_and_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2262
2343
  }
2263
2344
 
2264
2345
  /* ----- bit_or_i (boolean8_t fast) ----- */
2346
+ /* Every cell gets 0 or 1, even beside a masked cell: its byte is not
2347
+ defined (any value), and a cell Kleene resolves to true keeps the value
2348
+ written here. The word path folds each byte's eight bits into its low
2349
+ bit; a shift by less than 8 moves bits of the next byte only into the
2350
+ high bits, which the last mask drops. */
2351
+ static inline uint64_t
2352
+ ca_mask_word_nonzero_bytes (uint64_t w)
2353
+ {
2354
+ w |= w >> 4;
2355
+ w |= w >> 2;
2356
+ w |= w >> 1;
2357
+ return w & CA_MASK_WORD_LSB;
2358
+ }
2359
+
2265
2360
  static void
2266
2361
  ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2267
2362
  char *ptr1, ca_size_t i1,
@@ -2282,7 +2377,7 @@ ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2282
2377
  p1 = q1 + k*i1;
2283
2378
  p2 = q2 + k*i2;
2284
2379
  p3 = q3 + k*i3;
2285
- (*p3) = (*p1) | (*p2);
2380
+ (*p3) = ( (*p1) | (*p2) ) ? 1 : 0;
2286
2381
  }
2287
2382
  }
2288
2383
  }
@@ -2293,18 +2388,21 @@ ca_binop_bit_or_i_boolean8_fast (ca_size_t n, boolean8_t *m,
2293
2388
  ca_size_t j;
2294
2389
  for (j = 0; j < nw; j++) {
2295
2390
  ca_mask_word_store(q3 + j*CA_MASK_WORD_BYTES,
2296
- ca_mask_word_load(q1 + j*CA_MASK_WORD_BYTES) | ca_mask_word_load(q2 + j*CA_MASK_WORD_BYTES));
2391
+ ca_mask_word_nonzero_bytes(
2392
+ ca_mask_word_load(q1 + j*CA_MASK_WORD_BYTES) |
2393
+ ca_mask_word_load(q2 + j*CA_MASK_WORD_BYTES)));
2297
2394
  }
2298
2395
  for (j = nw * CA_MASK_WORD_BYTES; j < n; j++) {
2299
- q3[j] = q1[j] | q2[j];
2396
+ q3[j] = ( q1[j] | q2[j] ) ? 1 : 0;
2300
2397
  }
2301
2398
  }
2302
2399
  else {
2400
+ /* non-contig view: byte loop */
2303
2401
  for (k = 0; k < n; k++) {
2304
2402
  p1 = q1 + k*i1;
2305
2403
  p2 = q2 + k*i2;
2306
2404
  p3 = q3 + k*i3;
2307
- (*p3) = (*p1) | (*p2);
2405
+ (*p3) = ( (*p1) | (*p2) ) ? 1 : 0;
2308
2406
  }
2309
2407
  }
2310
2408
  }
@@ -2416,8 +2514,8 @@ ca_inherit_mask_check_args (int argc, VALUE *argv, const char *name)
2416
2514
  for (i=0; i<RARRAY_LEN(rest); i++) {
2417
2515
  VALUE v = RARRAY_AREF(rest, i);
2418
2516
  if ( ! rb_obj_is_carray(v) ) {
2419
- rb_raise(rb_eTypeError, "%s: argument %ld must be a CArray (got %s)",
2420
- name, i, rb_obj_classname(v));
2517
+ rb_raise(rb_eTypeError, "%s: argument %ld must be a CArray (got %"PRIsVALUE")",
2518
+ name, i, rb_obj_class(v));
2421
2519
  }
2422
2520
  }
2423
2521
  return rest;