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_access.c CHANGED
@@ -39,15 +39,9 @@ rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
39
39
  ca_create_mask(ca);
40
40
  ca_store_index(ca->mask, idx, &one);
41
41
  }
42
- else { /* unset mask and set value of the element at the index 'idx' */
42
+ else { /* set value of the element at the index 'idx' and unset its mask */
43
43
 
44
- /* unset mask */
45
- ca_update_mask(ca);
46
- if ( ca->mask ) {
47
- ca_store_index(ca->mask, idx, &zero);
48
- }
49
-
50
- /* store value */
44
+ /* A value that can not be stored leaves the cell masked. */
51
45
  {
52
46
  volatile VALUE holder;
53
47
  char *v = ALLOCV_N(char, holder, ca->bytes);
@@ -55,6 +49,11 @@ rb_ca_store_index (VALUE self, ca_size_t *idx, VALUE rval)
55
49
  ca_store_index(ca, idx, v);
56
50
  ALLOCV_END(holder);
57
51
  }
52
+
53
+ ca_update_mask(ca);
54
+ if ( ca->mask ) {
55
+ ca_store_index(ca->mask, idx, &zero);
56
+ }
58
57
  }
59
58
 
60
59
  return rval;
@@ -123,13 +122,9 @@ rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE rval)
123
122
  ca_create_mask(ca);
124
123
  ca_store_addr(ca->mask, addr, &one);
125
124
  }
126
- else { /* set value at the element */
127
- ca_update_mask(ca);
128
- if ( ca->mask ) {
129
- ca_store_addr(ca->mask, addr, &zero); /* unset mask */
130
- }
125
+ else { /* set value at the element and unset its mask */
131
126
 
132
- /* store value */
127
+ /* A value that can not be stored leaves the cell masked. */
133
128
  {
134
129
  volatile VALUE holder;
135
130
  char *v = ALLOCV_N(char, holder, ca->bytes);
@@ -137,6 +132,11 @@ rb_ca_store_addr (VALUE self, ca_size_t addr, VALUE rval)
137
132
  ca_store_addr(ca, addr, v);
138
133
  ALLOCV_END(holder);
139
134
  }
135
+
136
+ ca_update_mask(ca);
137
+ if ( ca->mask ) {
138
+ ca_store_addr(ca->mask, addr, &zero);
139
+ }
140
140
  }
141
141
 
142
142
  return rval;
@@ -426,14 +426,38 @@ rb_ca_ref_address (VALUE self, CAIndexInfo *info)
426
426
  return rb_ca_fetch_addr(self, addr);
427
427
  }
428
428
 
429
+ /* What a store to one cell takes from an array: its one cell. A cell of an
430
+ object array holds any value, the array itself included, so only a
431
+ CScalar is opened there. */
432
+ static VALUE
433
+ ca_cell_of_store_value (VALUE self, VALUE rval)
434
+ {
435
+ CArray *ca, *cv;
436
+ if ( rb_obj_is_cscalar(rval) ) {
437
+ return rb_ca_fetch_addr(rval, 0);
438
+ }
439
+ if ( ! rb_obj_is_carray(rval) ) {
440
+ return rval;
441
+ }
442
+ TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
443
+ if ( ca->data_type == CA_OBJECT ) {
444
+ return rval;
445
+ }
446
+ TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
447
+ if ( cv->elements != 1 ) {
448
+ rb_raise(rb_eArgError,
449
+ "%s: shape mismatch writing to one cell (<- %" PRIi64 " cells)",
450
+ ca_calling_method_name(), (int64_t) cv->elements);
451
+ }
452
+ return rb_ca_fetch_addr(rval, 0);
453
+ }
454
+
429
455
  static VALUE
430
456
  rb_ca_store_address (VALUE self, CAIndexInfo *info, volatile VALUE rval)
431
457
  {
432
458
  ca_size_t addr;
433
459
  addr = info->index[0].scalar;
434
- if ( rb_obj_is_cscalar(rval) ) {
435
- rval = rb_ca_fetch_addr(rval, 0);
436
- }
460
+ rval = ca_cell_of_store_value(self, rval);
437
461
  rb_ca_store_addr(self, addr, rval);
438
462
  return rval;
439
463
  }
@@ -461,9 +485,7 @@ rb_ca_store_point (VALUE self, CAIndexInfo *info, volatile VALUE val)
461
485
  for (i=0; i<ca->ndim; i++) {
462
486
  idx[i] = info->index[i].scalar;
463
487
  }
464
- if ( rb_obj_is_cscalar(val) ) {
465
- val = rb_ca_fetch_addr(val, 0);
466
- }
488
+ val = ca_cell_of_store_value(self, val);
467
489
  rb_ca_store_index(self, idx, val);
468
490
  return val;
469
491
  }
@@ -522,8 +544,8 @@ ca_store_all_cast_body (VALUE arg)
522
544
  memset(ca->mask->ptr, 0, ca->elements);
523
545
  }
524
546
  if ( ca->mask ) {
525
- ca_cast_block_with_mask(ca->elements, c->cv, c->scratch, ca, ca->ptr,
526
- (boolean8_t*)ca->mask->ptr);
547
+ ca_cast_block_skip_masked(ca->elements, c->cv, c->scratch, ca, ca->ptr,
548
+ (boolean8_t*)ca->mask->ptr);
527
549
  }
528
550
  else {
529
551
  ca_cast_block(ca->elements, c->cv, c->scratch, ca, ca->ptr);
@@ -567,9 +589,9 @@ ca_store_all_list_body (VALUE arg)
567
589
  }
568
590
  m++;
569
591
  }
570
- ca_cast_block_with_mask(ca->elements, c->ico, (VALUE *)RARRAY_CONST_PTR(list),
571
- cast_target, ca->ptr,
572
- (boolean8_t*)ca->mask->ptr);
592
+ ca_cast_block_skip_masked(ca->elements, c->ico, (VALUE *)RARRAY_CONST_PTR(list),
593
+ cast_target, ca->ptr,
594
+ (boolean8_t*)ca->mask->ptr);
573
595
  }
574
596
  else {
575
597
  /* Every cell gets a value, so none stays masked. */
@@ -617,8 +639,8 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
617
639
  rb_ca_data_class(rval), rb_ca_data_class(self));
618
640
  }
619
641
  rb_raise(rb_eTypeError,
620
- "can not store a %s array into a record array of %"PRIsVALUE,
621
- rb_obj_classname(rval), rb_ca_data_class(self));
642
+ "can not store a %"PRIsVALUE" array into a record array of %"PRIsVALUE,
643
+ rb_obj_class(rval), rb_ca_data_class(self));
622
644
  }
623
645
  if ( ca_is_face(ca) ) {
624
646
  if ( rb_obj_class(self) == rb_obj_class(rval)
@@ -637,9 +659,9 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
637
659
  return rval;
638
660
  }
639
661
  rb_raise(rb_eTypeError,
640
- "can not store a %s array into %s array "
662
+ "can not store a %"PRIsVALUE" array into %s array "
641
663
  "(to_type(:object) gives its values)",
642
- rb_obj_classname(rval), ca_type_name[ca->data_type]);
664
+ rb_obj_class(rval), ca_type_name[ca->data_type]);
643
665
  }
644
666
  return rb_funcall(rval, rb_intern("to_type"), 1, INT2NUM(CA_OBJECT));
645
667
  }
@@ -647,6 +669,20 @@ ca_face_source_for_store (VALUE self, CArray *ca, VALUE rval)
647
669
  /* A store into no cells writes nothing, but the value is checked as it
648
670
  would be for any cells: the same statement does not raise or pass
649
671
  depending on how many cells the index picks. */
672
+ /* Writing complex cells into real ones would drop the imaginary parts
673
+ without a word, where a Complex scalar is refused. */
674
+ void
675
+ ca_refuse_complex_into_real (CArray *ca, CArray *cv)
676
+ {
677
+ if ( ca_is_complex_type(cv) && ! ca_is_complex_type(ca)
678
+ && ca->data_type != CA_OBJECT ) {
679
+ rb_raise(rb_eCADataTypeError,
680
+ "%s: cannot store %s cells in %s (#real and #to_type take the part wanted)",
681
+ ca_calling_method_name(), ca_type_name[cv->data_type],
682
+ ca_type_name[ca->data_type]);
683
+ }
684
+ }
685
+
650
686
  static void
651
687
  ca_store_all_check_empty (VALUE self, VALUE rval)
652
688
  {
@@ -661,7 +697,10 @@ ca_store_all_check_empty (VALUE self, VALUE rval)
661
697
  rval = rb_funcall(rval, id_to_ca, 0);
662
698
  }
663
699
  if ( rb_obj_is_carray(rval) ) {
700
+ CArray *cv;
664
701
  ca_broadcast_to_destination(self, (volatile VALUE *) &rval);
702
+ TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
703
+ ca_refuse_complex_into_real(ca, cv);
665
704
  }
666
705
  else if ( TYPE(rval) == T_ARRAY ) {
667
706
  if ( NIL_P(rb_ary_flatten_for_elements(rval, ca->elements, ca)) ) {
@@ -733,6 +772,7 @@ rb_ca_store_all (VALUE self, VALUE rval)
733
772
  /* The destination owns the shape; see ca_broadcast_to_destination. */
734
773
  ca_broadcast_to_destination(self, &rval);
735
774
  TypedData_Get_Struct(rval, CArray, &carray_data_type, cv);
775
+ ca_refuse_complex_into_real(ca, cv);
736
776
 
737
777
  /* Fixlen cells of another width are fitted cell by cell, padded with
738
778
  NUL or cut to the destination's width, as a String or an Array of
@@ -1053,7 +1093,7 @@ extern VALUE rb_ca_remap_new (VALUE cary, VALUE rmapper);
1053
1093
  reads as masked (a CAGrid hole); the same-shape fast path is left to a
1054
1094
  mapper with no masked cell. */
1055
1095
  static VALUE
1056
- rb_ca_fancy_index_chain (VALUE self, VALUE rmapper)
1096
+ rb_ca_fancy_index_chain (VALUE self, volatile VALUE rmapper)
1057
1097
  {
1058
1098
  CArray *ca, *cm;
1059
1099
  volatile VALUE flat_self, flat_mapper, gridded, result;
@@ -1144,7 +1184,7 @@ ca_argv_is_method_call (int argc, VALUE *argv)
1144
1184
  {
1145
1185
  const char *name;
1146
1186
  if ( argc < 1 || ! SYMBOL_P(argv[0]) ) return 0;
1147
- name = rb_id2name(SYM2ID(argv[0]));
1187
+ name = rb_id2name(SYM2ID(argv[0])); /* name: read at once by strlen */
1148
1188
  return ( name != NULL && strlen(name) > 1 );
1149
1189
  }
1150
1190
 
@@ -1554,75 +1594,37 @@ rb_ca_fetch_slab_sigil (int argc, VALUE *argv, VALUE self)
1554
1594
  return rb_funcall(klass, rb_intern("new"), 2, base, slab_axes);
1555
1595
  }
1556
1596
 
1597
+ /* A CScalar is one cell and takes no index: s[] and s[0] name that cell,
1598
+ anything else is refused. */
1599
+
1600
+ static void
1601
+ rb_cs_check_index (int argc, VALUE *argv, const char *op)
1602
+ {
1603
+ if ( argc == 0 ) {
1604
+ return;
1605
+ }
1606
+ if ( argc == 1 && FIXNUM_P(argv[0]) && FIX2LONG(argv[0]) == 0 ) {
1607
+ return;
1608
+ }
1609
+ rb_raise(rb_eArgError,
1610
+ "CScalar%s takes no index other than [] or [0]", op);
1611
+ }
1612
+
1557
1613
  static VALUE
1558
1614
  rb_cs_fetch_method (int argc, VALUE *argv, VALUE self)
1559
1615
  {
1560
- volatile VALUE obj = Qnil;
1561
- CArray *ca;
1562
- CAIndexInfo info;
1563
-
1564
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1565
-
1566
- info.range_check = 0;
1567
- rb_ca_scan_index(ca->ndim, ca->dim, ca->elements, argc, argv, &info);
1616
+ rb_cs_check_index(argc, argv, "#[]");
1617
+ return rb_ca_fetch_addr(self, 0);
1618
+ }
1568
1619
 
1569
- switch ( info.type ) {
1570
- case CA_REG_ADDRESS_COMPLEX:
1571
- obj = rb_ca_fetch_addr(self, 0);
1572
- break;
1573
- case CA_REG_ADDRESS:
1574
- obj = rb_ca_fetch_addr(self, 0);
1575
- break;
1576
- case CA_REG_FLATTEN:
1577
- obj = self; /* rb_funcall(self, rb_intern("refer"), 0); */
1578
- break;
1579
- case CA_REG_POINT:
1580
- obj = rb_ca_fetch_addr(self, 0);
1581
- break;
1582
- case CA_REG_ALL:
1583
- obj = self; /* rb_funcall(self, rb_intern("refer"), 0); */
1584
- break;
1585
- case CA_REG_BLOCK:
1586
- obj = self; /* rb_funcall(self, rb_intern("refer"), 0); */
1587
- break;
1588
- case CA_REG_SELECT:
1589
- obj = rb_ca_select_new(self, argv[0]);
1590
- break;
1591
- case CA_REG_ITERATOR:
1592
- obj = rb_ca_slab_iter_new(self, &info);
1593
- break;
1594
- case CA_REG_REPEAT:
1595
- obj = rb_ca_repeat(argc, argv, self);
1596
- break;
1597
- case CA_REG_MAPPING:
1598
- obj = rb_ca_fancy_index_chain(self, argv[0]);
1599
- break;
1600
- case CA_REG_GRID:
1601
- obj = rb_ca_grid(argc, argv, self);
1602
- break;
1603
- case CA_REG_METHOD_CALL: {
1604
- volatile VALUE idx;
1605
- idx = rb_funcallv_public(self, SYM2ID(info.symbol), argc-1, argv+1);
1606
- obj = rb_ca_fetch(self, idx);
1607
- break;
1608
- }
1609
- case CA_REG_MEMBER: {
1610
- volatile VALUE data_class = rb_ca_data_class(self);
1611
- if ( ! NIL_P(data_class) ) {
1612
- obj = rb_ca_face_field(self, info.symbol);
1613
- break;
1614
- }
1615
- else {
1616
- rb_raise(rb_eIndexError,
1617
- "can't refer member of carray doesn't have data_class");
1618
- }
1619
- break;
1620
- }
1621
- default:
1622
- rb_raise(rb_eIndexError, "invalid index specified");
1623
- }
1620
+ static VALUE rb_ca_store_method (int argc, VALUE *argv, VALUE self);
1624
1621
 
1625
- return obj;
1622
+ static VALUE
1623
+ rb_cs_store_method (int argc, VALUE *argv, VALUE self)
1624
+ {
1625
+ rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS);
1626
+ rb_cs_check_index(argc - 1, argv, "#[]=");
1627
+ return rb_ca_store_method(argc, argv, self);
1626
1628
  }
1627
1629
 
1628
1630
  /* Recursively convert a store rvalue's surface value objects into
@@ -1698,14 +1700,14 @@ ca_face_convert_store_rval (VALUE self, CArray *ca, VALUE val)
1698
1700
  const char *method = ca_face_storage_method(self);
1699
1701
  if ( method ) {
1700
1702
  rb_raise(rb_eTypeError,
1701
- "%s cannot store a bare %s array as raw storage "
1703
+ "%"PRIsVALUE" cannot store a bare %s array as raw storage "
1702
1704
  "(write the %s storage, which #%s gives, directly)",
1703
- rb_obj_classname(self), ca_type_name[cval->data_type],
1705
+ rb_obj_class(self), ca_type_name[cval->data_type],
1704
1706
  ca_type_name[storage->data_type], method);
1705
1707
  }
1706
1708
  rb_raise(rb_eTypeError,
1707
- "%s cannot store a bare %s array as raw storage",
1708
- rb_obj_classname(self), ca_type_name[cval->data_type]);
1709
+ "%"PRIsVALUE" cannot store a bare %s array as raw storage",
1710
+ rb_obj_class(self), ca_type_name[cval->data_type]);
1709
1711
  }
1710
1712
  }
1711
1713
  return val;
@@ -1926,7 +1928,7 @@ rb_ca_store2 (VALUE self, int n, VALUE *rindex, VALUE rval)
1926
1928
  }
1927
1929
 
1928
1930
  static VALUE
1929
- rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1931
+ rb_ca_s_scan_index (VALUE self, VALUE rdim, volatile VALUE ridx)
1930
1932
  {
1931
1933
  volatile VALUE rtype, rindex;
1932
1934
  CAIndexInfo info;
@@ -1937,11 +1939,13 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
1937
1939
 
1938
1940
  Check_Type(rdim, T_ARRAY);
1939
1941
  Check_Type(ridx, T_ARRAY);
1942
+ /* Converting an entry can run Ruby that changes the Array: read a copy. */
1943
+ ridx = rb_ary_dup(ridx);
1940
1944
 
1941
1945
  ndim = (int) RARRAY_LEN(rdim);
1942
1946
  CA_CHECK_RANK_GIVEN(ndim);
1943
1947
  for (i=0; i<ndim; i++) {
1944
- dim[i] = NUM2SIZE(rb_ary_entry(rdim, i));
1948
+ dim[i] = ca_integer_arg(rb_ary_entry(rdim, i), "a dimension", NULL);
1945
1949
  }
1946
1950
  CA_CHECK_DIM_GIVEN(ndim, dim);
1947
1951
  elements = ca_shape_elements(ndim, dim, 1);
@@ -2026,7 +2030,7 @@ rb_ca_s_scan_index (VALUE self, VALUE rdim, VALUE ridx)
2026
2030
  }
2027
2031
 
2028
2032
  static VALUE
2029
- rb_ca_normalize_index (VALUE self, VALUE ridx)
2033
+ rb_ca_normalize_index (VALUE self, volatile VALUE ridx)
2030
2034
  {
2031
2035
  volatile VALUE rindex;
2032
2036
  CArray *ca;
@@ -2035,6 +2039,8 @@ rb_ca_normalize_index (VALUE self, VALUE ridx)
2035
2039
 
2036
2040
  TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2037
2041
  Check_Type(ridx, T_ARRAY);
2042
+ /* Converting an entry can run Ruby that changes the Array: read a copy. */
2043
+ ridx = rb_ary_dup(ridx);
2038
2044
 
2039
2045
  info.range_check = 1;
2040
2046
  rb_ca_scan_index(ca->ndim, ca->dim, ca->elements,
@@ -2118,7 +2124,7 @@ parse_shape_kwarg (VALUE rshape, int *out_ndim, ca_size_t *dim,
2118
2124
  }
2119
2125
  ca_check_rank_fits(ndim, NULL);
2120
2126
  for (i = 0; i < ndim; i++) {
2121
- dim[i] = NUM2SIZE(rb_ary_entry(rshape, i));
2127
+ dim[i] = ca_integer_arg(rb_ary_entry(rshape, i), "a dimension", NULL);
2122
2128
  if ( dim[i] < 0 ) {
2123
2129
  rb_raise(rb_eArgError, "negative dim %" PRId64 " at axis %d",
2124
2130
  (ca_size_t) dim[i], i);
@@ -2129,12 +2135,21 @@ parse_shape_kwarg (VALUE rshape, int *out_ndim, ca_size_t *dim,
2129
2135
  *out_elements = elements;
2130
2136
  }
2131
2137
 
2138
+ /* A scalar key of index2addr / addr2index, by the rule of `[]`: a Float that
2139
+ is a whole number is that position, any other is no position at all. */
2140
+ static VALUE
2141
+ ca_scalar_position_key (VALUE v)
2142
+ {
2143
+ return RB_FLOAT_TYPE_P(v) ? SIZE2NUM(ca_index_key_value(v)) : v;
2144
+ }
2145
+
2132
2146
  static VALUE
2133
2147
  addr2index_do (int ndim, ca_size_t *dim, ca_size_t elements, VALUE raddr)
2134
2148
  {
2135
2149
  volatile VALUE out;
2136
2150
  int i;
2137
2151
 
2152
+ raddr = ca_scalar_position_key(raddr);
2138
2153
  /* Scalar path (Integer input): returns Ruby Array of N Integers.
2139
2154
  * Preserves legacy shape for callers like `i, j = ca.addr2index(k)`. */
2140
2155
  if ( rb_obj_is_kind_of(raddr, rb_cInteger) ) {
@@ -2291,12 +2306,18 @@ index2addr_do (int ndim, ca_size_t *dim, int argc, VALUE *argv)
2291
2306
  ca_size_t out_dim[CA_RANK_MAX];
2292
2307
  ca_size_t out_elements = 1;
2293
2308
  int shape_from = -1;
2309
+ VALUE keys[CA_RANK_MAX];
2294
2310
 
2295
2311
  if ( argc != ndim ) {
2296
2312
  rb_raise(rb_eArgError,
2297
2313
  "wrong number of indices (%d for %d)", argc, ndim);
2298
2314
  }
2299
2315
 
2316
+ for (i = 0; i < ndim; i++) {
2317
+ keys[i] = ca_scalar_position_key(argv[i]);
2318
+ }
2319
+ argv = keys;
2320
+
2300
2321
  for (i = 0; i < ndim; i++) {
2301
2322
  if ( ! rb_obj_is_kind_of(argv[i], rb_cInteger) ) {
2302
2323
  all_number = 0;
@@ -2443,6 +2464,7 @@ Init_carray_access (void)
2443
2464
  rb_define_method(rb_cCArray, "[]=", rb_ca_store_method, -1);
2444
2465
 
2445
2466
  rb_define_method(rb_cCScalar, "[]", rb_cs_fetch_method, -1);
2467
+ rb_define_method(rb_cCScalar, "[]=", rb_cs_store_method, -1);
2446
2468
 
2447
2469
  rb_define_method(rb_cCArray, "fill", rb_ca_fill, 1);
2448
2470
  rb_define_method(rb_cCArray, "fill_copy", rb_ca_fill_copy, 1);
@@ -815,7 +815,7 @@ address_basis_open (int argc, VALUE *argv, VALUE module)
815
815
  ca_size_t *cells = (ca_size_t *) (bc + n);
816
816
  int *flags = (int *) (cells + (size_t) n * 2 * CA_RANK_MAX);
817
817
  for ( i = 0; i < n; i++ ) {
818
- VALUE object = rb_ary_entry(arrays, i);
818
+ volatile VALUE object = rb_ary_entry(arrays, i);
819
819
  VALUE starts = NIL_P(box_start) ? Qnil : rb_ary_entry(box_start, i);
820
820
  VALUE counts = NIL_P(box_count) ? Qnil : rb_ary_entry(box_count, i);
821
821
  int8_t k;
@@ -965,6 +965,213 @@ address_basis_tier (VALUE module, VALUE object)
965
965
  return INT2NUM(tier_for(ca));
966
966
  }
967
967
 
968
+ /* ------------------------------------------------------------------- */
969
+
970
+ /* The call of a sweep, with the parts locked: a view that runs Ruby while
971
+ it hands over its cells (a CAObject) could otherwise resize a part under
972
+ the pointer the slab writes through. */
973
+ typedef struct {
974
+ ca_cslab_r_t function;
975
+ const char *fsync;
976
+ int n;
977
+ const VALUE *arrays;
978
+ void **table;
979
+ volatile VALUE *parts;
980
+ int nparts;
981
+ int nlocked; /* parts[0..nlocked) were considered for a lock */
982
+ } address_basis_sweep_call_t;
983
+
984
+ /* Whether parts[i] is a String to lock: not frozen, and not one that an
985
+ earlier part already is. */
986
+ static int
987
+ address_basis_sweep_locks (address_basis_sweep_call_t *c, int i)
988
+ {
989
+ int k;
990
+ if ( OBJ_FROZEN(c->parts[i]) ) return 0;
991
+ for ( k = 0; k < i; k++ ) {
992
+ if ( c->parts[k] == c->parts[i] ) return 0;
993
+ }
994
+ return 1;
995
+ }
996
+
997
+ static VALUE
998
+ address_basis_sweep_body (VALUE arg)
999
+ {
1000
+ address_basis_sweep_call_t *c = (address_basis_sweep_call_t *) arg;
1001
+ const VALUE *a = c->arrays;
1002
+ for ( ; c->nlocked < c->nparts; c->nlocked++ ) {
1003
+ if ( address_basis_sweep_locks(c, c->nlocked) ) {
1004
+ rb_str_locktmp(c->parts[c->nlocked]);
1005
+ }
1006
+ }
1007
+ switch ( c->n ) {
1008
+ case 1: ca_call_cslab_1_r(c->function, c->fsync, a[0], c->table); break;
1009
+ case 2: ca_call_cslab_2_r(c->function, c->fsync, a[0], a[1], c->table); break;
1010
+ case 3: ca_call_cslab_3_r(c->function, c->fsync, a[0], a[1], a[2], c->table); break;
1011
+ case 4: ca_call_cslab_4_r(c->function, c->fsync, a[0], a[1], a[2], a[3],
1012
+ c->table); break;
1013
+ case 5: ca_call_cslab_5_r(c->function, c->fsync, a[0], a[1], a[2], a[3], a[4],
1014
+ c->table); break;
1015
+ case 6: ca_call_cslab_6_r(c->function, c->fsync, a[0], a[1], a[2], a[3], a[4],
1016
+ a[5], c->table); break;
1017
+ case 7: ca_call_cslab_7_r(c->function, c->fsync, a[0], a[1], a[2], a[3], a[4],
1018
+ a[5], a[6], c->table); break;
1019
+ }
1020
+ return Qnil;
1021
+ }
1022
+
1023
+ static VALUE
1024
+ address_basis_sweep_unlock (VALUE arg)
1025
+ {
1026
+ address_basis_sweep_call_t *c = (address_basis_sweep_call_t *) arg;
1027
+ int i;
1028
+ for ( i = 0; i < c->nlocked; i++ ) {
1029
+ if ( address_basis_sweep_locks(c, i) ) {
1030
+ rb_str_unlocktmp(c->parts[i]);
1031
+ }
1032
+ }
1033
+ return Qnil;
1034
+ }
1035
+
1036
+ /*
1037
+ * call-seq:
1038
+ * CArray::AddressBasis.sweep(function, fsync, arrays, *parts) -> nil
1039
+ *
1040
+ * Runs ca_call_cslab_N_r over `arrays` with the slab function at the address
1041
+ * `function`, and hands it as `userdata` a table of pointers to the bytes of
1042
+ * each of `parts`, in order.
1043
+ *
1044
+ * This is what a consumer did through Fiddle: copy the parts into memory
1045
+ * from malloc, write the table in front of them, and call the family member
1046
+ * for the arity. The copy was there because a String's bytes may be moved
1047
+ * by GC compaction, and the sweep allocates -- so a collection can run
1048
+ * between taking an address and the slab reading it. It is not needed
1049
+ * here: every part is also held in an array on the machine stack for the
1050
+ * length of this call, and an object referenced from there is pinned. The
1051
+ * VM stack is not enough by itself -- a splat of 128 or more arguments
1052
+ * reaches a C method as a heap array, whose elements compaction moves.
1053
+ * The machine stack is why the parts are limited to 1024.
1054
+ *
1055
+ * A part that is not frozen is the slab's to write (an error slot), and is
1056
+ * made unshared first so the write lands in that String alone. A frozen
1057
+ * part is read only. Each part has to start on an 8-byte boundary, since a
1058
+ * slab reads doubles and int64s out of them.
1059
+ */
1060
+ static VALUE
1061
+ address_basis_sweep (int argc, VALUE *argv, VALUE module)
1062
+ {
1063
+ volatile VALUE rfunction, rfsync, rarrays;
1064
+ ca_cslab_r_t function;
1065
+ char fsync[8];
1066
+ VALUE vholder = 0;
1067
+ void **table;
1068
+ volatile VALUE arrays[7];
1069
+ volatile VALUE *parts;
1070
+ address_basis_sweep_call_t call;
1071
+ int i, n, nparts;
1072
+
1073
+ rb_check_arity(argc, 3, UNLIMITED_ARGUMENTS);
1074
+ rfunction = argv[0];
1075
+ rfsync = argv[1];
1076
+ rarrays = argv[2];
1077
+ nparts = argc - 3;
1078
+
1079
+ if ( nparts > 1024 ) {
1080
+ rb_raise(rb_eArgError, "%s: a sweep takes at most 1024 parts, got %d",
1081
+ ca_calling_method_name(), nparts);
1082
+ }
1083
+ /* Held on the machine stack, which the GC scans conservatively and so
1084
+ pins. argv is not written: it may be the storage of a heap array. */
1085
+ parts = ALLOCA_N(VALUE, nparts > 0 ? nparts : 1);
1086
+ for ( i = 0; i < nparts; i++ ) {
1087
+ parts[i] = argv[3 + i];
1088
+ }
1089
+
1090
+ Check_Type(rarrays, T_ARRAY);
1091
+ n = (int) RARRAY_LEN(rarrays);
1092
+ if ( n < 1 || n > 7 ) {
1093
+ rb_raise(rb_eArgError, "%s: a sweep takes 1 to 7 arrays, got %d",
1094
+ ca_calling_method_name(), n);
1095
+ }
1096
+ {
1097
+ /* Copied: nothing may change it while the sweep reads it. */
1098
+ const char *f = StringValueCStr(rfsync);
1099
+ long len = (long) strlen(f);
1100
+ if ( len != n ) {
1101
+ rb_raise(rb_eArgError, "%s: fsync has %ld characters for %d arrays",
1102
+ ca_calling_method_name(), len, n);
1103
+ }
1104
+ memcpy(fsync, f, (size_t) n);
1105
+ fsync[n] = '\0';
1106
+ }
1107
+ for ( i = 0; i < n; i++ ) {
1108
+ if ( fsync[i] != '0' && fsync[i] != '1' ) {
1109
+ rb_raise(rb_eArgError,
1110
+ "%s: fsync character %d is '%c', not '0' (read) or '1' (written)",
1111
+ ca_calling_method_name(), i, fsync[i]);
1112
+ }
1113
+ }
1114
+ function = (ca_cslab_r_t) (uintptr_t) NUM2ULL(rfunction);
1115
+ if ( ! function ) {
1116
+ rb_raise(rb_eArgError, "%s: the slab function's address is zero",
1117
+ ca_calling_method_name());
1118
+ }
1119
+
1120
+ /* Copied onto the machine stack, which pins them: the list itself can be
1121
+ changed by Ruby that runs during the sweep. */
1122
+ for ( i = 0; i < n; i++ ) {
1123
+ arrays[i] = RARRAY_AREF(rarrays, i);
1124
+ }
1125
+ for ( i = 0; i < n; i++ ) {
1126
+ if ( ! rb_obj_is_kind_of(arrays[i], rb_cCArray) ) {
1127
+ rb_raise(rb_eTypeError, "%s: array %d is not a CArray",
1128
+ ca_calling_method_name(), i);
1129
+ }
1130
+ }
1131
+
1132
+ for ( i = 0; i < nparts; i++ ) {
1133
+ VALUE part = parts[i];
1134
+ StringValue(part);
1135
+ parts[i] = part;
1136
+ if ( ! OBJ_FROZEN(part) ) {
1137
+ rb_str_modify(part);
1138
+ }
1139
+ if ( (uintptr_t) RSTRING_PTR(part) & 7 ) {
1140
+ rb_raise(rb_eArgError, "%s: part %d does not start on an 8-byte boundary",
1141
+ ca_calling_method_name(), i);
1142
+ }
1143
+ }
1144
+
1145
+ /* Taken after every check, so a refusal has nothing to free. The sweep
1146
+ itself can raise (a shape that will not broadcast); then the table is
1147
+ left to ALLOCV's own holder, which the GC reclaims. */
1148
+ table = ALLOCV_N(void *, vholder, nparts > 0 ? nparts : 1);
1149
+ for ( i = 0; i < nparts; i++ ) {
1150
+ table[i] = RSTRING_PTR(parts[i]);
1151
+ }
1152
+
1153
+ call.function = function;
1154
+ call.fsync = fsync;
1155
+ call.n = n;
1156
+ call.arrays = (const VALUE *) arrays;
1157
+ call.table = table;
1158
+ call.parts = parts;
1159
+ call.nparts = nparts;
1160
+ call.nlocked = 0;
1161
+ rb_ensure(address_basis_sweep_body, (VALUE) &call,
1162
+ address_basis_sweep_unlock, (VALUE) &call);
1163
+
1164
+ ALLOCV_END(vholder);
1165
+ for ( i = 0; i < nparts; i++ ) {
1166
+ RB_GC_GUARD(parts[i]);
1167
+ }
1168
+ for ( i = 0; i < n; i++ ) {
1169
+ RB_GC_GUARD(arrays[i]);
1170
+ }
1171
+ RB_GC_GUARD(rarrays);
1172
+ return Qnil;
1173
+ }
1174
+
968
1175
  /* ------------------------------------------------------------------- */
969
1176
  /* Init_carray_address_basis */
970
1177
  /* ------------------------------------------------------------------- */
@@ -978,6 +1185,7 @@ Init_carray_address_basis (void)
978
1185
  rb_define_singleton_method(mAddressBasis, "classify",
979
1186
  address_basis_classify, 1);
980
1187
  rb_define_singleton_method(mAddressBasis, "tier", address_basis_tier, 1);
1188
+ rb_define_singleton_method(mAddressBasis, "sweep", address_basis_sweep, -1);
981
1189
 
982
1190
  rb_define_const(mAddressBasis, "TIER_ENTITY", INT2NUM(TIER_ENTITY));
983
1191
  rb_define_const(mAddressBasis, "TIER_STRIDE", INT2NUM(TIER_STRIDE));
@@ -223,6 +223,14 @@ ca_is_readonly (void *ap)
223
223
  return 1;
224
224
  }
225
225
  }
226
+ if ( ca_test_flag(ca, CA_FLAG_FOLDED) ) {
227
+ /* a folded reshape: its source is not on the parent chain */
228
+ CArray *src = ca_fold_source(ca);
229
+ if ( src && ca_is_readonly(src) ) {
230
+ ca_set_flag(ca, CA_FLAG_READ_ONLY);
231
+ return 1;
232
+ }
233
+ }
226
234
  return 0; /* all test -> false */
227
235
  }
228
236
  }