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/ca_obj_face.c CHANGED
@@ -49,7 +49,9 @@ ca_face_sync (void *ap)
49
49
  {
50
50
  CAView *cav = (CAView *) ap;
51
51
  /* alias means no copy-back is needed; parent sync propagates */
52
+ ca_view_pass_mask_to_parent(cav);
52
53
  ca_sync(cav->parent);
54
+ ca_view_sync_mask_after_parent(cav);
53
55
  }
54
56
 
55
57
  void
@@ -185,8 +187,22 @@ rb_ca_face_template (VALUE original_face, CArray *new_parent,
185
187
  dst);
186
188
  }
187
189
 
190
+ /* A Face put on a fresh entity has nothing to be read-only for: the template
191
+ copied the flag of the Face it came from, which may be read-only only as
192
+ that view was. A Face that cannot hold a new value keeps it. */
193
+ static void
194
+ ca_face_fresh_entity_is_writable (volatile VALUE lifted, VALUE face)
195
+ {
196
+ CArray *lc;
197
+ TypedData_Get_Struct(lifted, CArray, &carray_data_type, lc);
198
+ if ( ca_test_flag(lc, CA_FLAG_READ_ONLY)
199
+ && RTEST(rb_funcall(face, rb_intern("holds_new_values?"), 0)) ) {
200
+ ca_unset_flag(lc, CA_FLAG_READ_ONLY);
201
+ }
202
+ }
203
+
188
204
  VALUE
189
- ca_face_relift_entity (VALUE face, VALUE result)
205
+ ca_face_relift_entity (VALUE face, volatile VALUE result)
190
206
  {
191
207
  static ID id_copy_state = 0;
192
208
  CArray *rc, *lc;
@@ -195,6 +211,7 @@ ca_face_relift_entity (VALUE face, VALUE result)
195
211
  TypedData_Get_Struct(result, CArray, &carray_data_type, rc);
196
212
  lifted = rb_ca_face_template(face, rc, rc->dim);
197
213
  rb_ca_set_parent(lifted, result);
214
+ ca_face_fresh_entity_is_writable(lifted, face);
198
215
  if ( ca_has_mask(rc) ) {
199
216
  /* The template starts without a mask. */
200
217
  TypedData_Get_Struct(lifted, CArray, &carray_data_type, lc);
@@ -302,6 +319,9 @@ ca_face_lift (VALUE view, VALUE face_parent)
302
319
  if ( ca_is_view(view_ca) ) {
303
320
  rb_ca_set_lifted_from(lifted, face_parent);
304
321
  }
322
+ else {
323
+ ca_face_fresh_entity_is_writable(lifted, face_parent);
324
+ }
305
325
 
306
326
  /* Optional Ruby callback for ivar / state carry. Faces holding
307
327
  semantic state in the struct tail (CATime / CATimedelta
@@ -356,7 +376,7 @@ ca_face_lift (VALUE view, VALUE face_parent)
356
376
  * builder handed us a view whose parent is the marker, and leaving it
357
377
  * there would stack a redundant marker in the middle. */
358
378
  static VALUE
359
- ca_lazy_marker_lift (VALUE view, VALUE marker)
379
+ ca_lazy_marker_lift (VALUE view, volatile VALUE marker)
360
380
  {
361
381
  CArray *view_ca, *mp;
362
382
  extern VALUE rb_ca_lazy_marker_new (VALUE cary);
@@ -739,16 +759,16 @@ ca_face_refuse_order (const char *name, VALUE face, const char *verb)
739
759
  const char *storage = ca_face_storage_method(face);
740
760
  if ( storage ) {
741
761
  rb_raise(rb_eArgError,
742
- "%s: %s is not orderable by storage (a cell encodes the value "
762
+ "%s: %"PRIsVALUE" is not orderable by storage (a cell encodes the value "
743
763
  "rather than being it); %s its values instead -- %s -- or %s "
744
764
  "its storage, which #%s gives",
745
- name, rb_obj_classname(face), verb,
765
+ name, rb_obj_class(face), verb,
746
766
  ca_face_conversion_hint(face), verb, storage);
747
767
  }
748
768
  rb_raise(rb_eArgError,
749
- "%s: %s is not orderable by storage (a cell encodes the value "
769
+ "%s: %"PRIsVALUE" is not orderable by storage (a cell encodes the value "
750
770
  "rather than being it); %s its values instead -- %s",
751
- name, rb_obj_classname(face), verb,
771
+ name, rb_obj_class(face), verb,
752
772
  ca_face_conversion_hint(face));
753
773
  }
754
774
 
@@ -806,17 +826,17 @@ ca_face_refuse_numeric (VALUE self, const char *name, VALUE eclass)
806
826
  const char *storage = ca_face_storage_method(self);
807
827
  if ( storage ) {
808
828
  rb_raise(eclass,
809
- "%s: %s does not define it (a cell encodes the value rather "
829
+ "%s: %"PRIsVALUE" does not define it (a cell encodes the value rather "
810
830
  "than being a number); compute on its values instead -- %s -- "
811
831
  "or on its storage, which #%s gives",
812
- name, rb_obj_classname(self), ca_face_conversion_hint(self),
832
+ name, rb_obj_class(self), ca_face_conversion_hint(self),
813
833
  storage);
814
834
  }
815
835
  }
816
836
  rb_raise(eclass,
817
- "%s: %s does not define it (a cell encodes the value rather than "
837
+ "%s: %"PRIsVALUE" does not define it (a cell encodes the value rather than "
818
838
  "being a number); compute on its values instead -- %s",
819
- name, rb_obj_classname(self), ca_face_conversion_hint(self));
839
+ name, rb_obj_class(self), ca_face_conversion_hint(self));
820
840
  }
821
841
 
822
842
  VALUE
@@ -837,21 +857,21 @@ ca_face_operand_descend (VALUE operand, const char *name)
837
857
  const char *storage = ca_face_storage_method(operand);
838
858
  if ( storage ) {
839
859
  rb_raise(rb_eArgError,
840
- "%s: cannot take a %s operand: its storage is not its surface "
860
+ "%s: cannot take a %"PRIsVALUE" operand: its storage is not its surface "
841
861
  "(a cell encodes the value rather than being it), so comparing "
842
862
  "the storage would compare the encoding. Convert the operand "
843
863
  "to the receiver's space first -- %s -- or compare the storage "
844
864
  "of both sides, which #%s gives",
845
- name, rb_obj_classname(operand),
865
+ name, rb_obj_class(operand),
846
866
  ca_face_conversion_hint(operand), storage);
847
867
  }
848
868
  }
849
869
  rb_raise(rb_eArgError,
850
- "%s: cannot take a %s operand: its storage is not its surface "
870
+ "%s: cannot take a %"PRIsVALUE" operand: its storage is not its surface "
851
871
  "(a cell encodes the value rather than being it), so comparing "
852
872
  "the storage would compare the encoding. Convert the operand to "
853
873
  "the receiver's space first -- %s",
854
- name, rb_obj_classname(operand), ca_face_conversion_hint(operand));
874
+ name, rb_obj_class(operand), ca_face_conversion_hint(operand));
855
875
  return Qnil; /* not reached */
856
876
  }
857
877
 
@@ -860,11 +880,12 @@ ca_face_operand_descend (VALUE operand, const char *name)
860
880
 
861
881
  A Face equals only a Face of its own class. When the two share their
862
882
  state (a time array's unit) and the storage holds the values in their
863
- order, equal storage is equal values, so the storage is compared.
864
- Otherwise `==` compares the values themselves (two time arrays of
865
- different units, two const strings over different pools, two
866
- categoricals with differently ordered labels), and `eql?`, which has to
867
- agree with #hash, answers false. */
883
+ order, cell for cell at one width, equal storage is equal values, so the
884
+ storage is compared. Otherwise `==` compares the values themselves (two
885
+ time arrays of different units, two const strings over different pools,
886
+ two categoricals with differently ordered labels, two fixed-length
887
+ strings of different widths), and `eql?`, which has to agree with #hash,
888
+ answers false. */
868
889
  int
869
890
  ca_face_equal (VALUE va, VALUE vb, int strict)
870
891
  {
@@ -887,7 +908,8 @@ ca_face_equal (VALUE va, VALUE vb, int strict)
887
908
  }
888
909
  }
889
910
  if ( ca_face_state_compatible(va, a, vb, b)
890
- && ( strict || ca_test_flag(a, CA_FLAG_FACE_ORDERABLE_STORAGE) ) ) {
911
+ && ( strict || ( ca_test_flag(a, CA_FLAG_FACE_ORDERABLE_STORAGE)
912
+ && a->bytes == b->bytes ) ) ) {
891
913
  return strict ? ca_eql_storage(ca_strip_face(a), ca_strip_face(b))
892
914
  : ca_equal(ca_strip_face(a), ca_strip_face(b));
893
915
  }
@@ -957,8 +979,8 @@ ca_face_reconcile_operand (VALUE reference, VALUE operand, const char *name)
957
979
  if ( ca_is_face(op) ) {
958
980
  if ( op->obj_type != CA_OBJ_RECORD ) {
959
981
  rb_raise(rb_eTypeError,
960
- "%s: can not take a %s operand for a record array of "
961
- "%"PRIsVALUE, name, rb_obj_classname(operand),
982
+ "%s: can not take a %"PRIsVALUE" operand for a record array of "
983
+ "%"PRIsVALUE, name, rb_obj_class(operand),
962
984
  rb_ca_data_class(reference));
963
985
  }
964
986
  if ( rb_ca_data_class(operand) != rb_ca_data_class(reference) ) {
@@ -985,16 +1007,16 @@ ca_face_reconcile_operand (VALUE reference, VALUE operand, const char *name)
985
1007
  const char *storage = ca_face_storage_method(reference);
986
1008
  if ( storage ) {
987
1009
  rb_raise(rb_eArgError,
988
- "%s: %s is not comparable by storage and has no to_comparable "
1010
+ "%s: %"PRIsVALUE" is not comparable by storage and has no to_comparable "
989
1011
  "to bring the operand into its space; compare the storage of "
990
1012
  "both sides, which #%s gives",
991
- name, rb_obj_classname(reference), storage);
1013
+ name, rb_obj_class(reference), storage);
992
1014
  }
993
1015
  }
994
1016
  rb_raise(rb_eArgError,
995
- "%s: %s is not comparable by storage and has no to_comparable "
1017
+ "%s: %"PRIsVALUE" is not comparable by storage and has no to_comparable "
996
1018
  "to bring the operand into its space",
997
- name, rb_obj_classname(reference));
1019
+ name, rb_obj_class(reference));
998
1020
  return Qnil; /* not reached */
999
1021
  }
1000
1022
 
@@ -1086,8 +1108,8 @@ ca_face_list_class (VALUE list, const char *who)
1086
1108
  "%s: cannot mix Face and non-Face arrays, or arrays of "
1087
1109
  "different Face classes; convert them first to one Face "
1088
1110
  "class or to no Face, or combine their storage (#%s gives "
1089
- "the storage of a %s)",
1090
- who, storage, rb_obj_classname(face));
1111
+ "the storage of a %"PRIsVALUE")",
1112
+ who, storage, rb_obj_class(face));
1091
1113
  }
1092
1114
  rb_raise(rb_eArgError,
1093
1115
  "%s: cannot mix Face and non-Face arrays, or arrays of "
@@ -1097,10 +1119,10 @@ ca_face_list_class (VALUE list, const char *who)
1097
1119
  }
1098
1120
  if ( n > 1 && ! ca_face_state_portable(ref->obj_type, face_class) ) {
1099
1121
  rb_raise(rb_eArgError,
1100
- "%s: %s state is not portable across several arrays (each "
1122
+ "%s: %"PRIsVALUE" state is not portable across several arrays (each "
1101
1123
  "keeps its values in storage of its own); convert them first "
1102
1124
  "(%s)",
1103
- who, rb_class2name(face_class),
1125
+ who, face_class,
1104
1126
  ca_face_conversion_hint(face));
1105
1127
  }
1106
1128
  for ( i = 1; i < n; i++ ) {
@@ -1109,8 +1131,8 @@ ca_face_list_class (VALUE list, const char *who)
1109
1131
  TypedData_Get_Struct(p, CArray, &carray_data_type, ca);
1110
1132
  if ( ! ca_face_state_compatible(rb_ary_entry(list, 0), ref, p, ca) ) {
1111
1133
  rb_raise(rb_eArgError,
1112
- "%s: Face state mismatch (the %s at index %ld differs from "
1113
- "index 0)", who, rb_class2name(face_class), i);
1134
+ "%s: Face state mismatch (the %"PRIsVALUE" at index %ld differs from "
1135
+ "index 0)", who, face_class, i);
1114
1136
  }
1115
1137
  }
1116
1138
  return face_class;
@@ -1193,7 +1215,7 @@ rb_ca_reconcile_operand (VALUE self, VALUE operand, VALUE rname)
1193
1215
  static VALUE
1194
1216
  rb_ca_face_lift_method (VALUE self, VALUE face_parent)
1195
1217
  {
1196
- CArray *fp;
1218
+ CArray *fp, *vc, *storage;
1197
1219
  if ( ! rb_obj_is_kind_of(face_parent, rb_cCArray) ) {
1198
1220
  rb_raise(rb_eArgError, "face_lift: face_parent must be a CArray");
1199
1221
  }
@@ -1201,6 +1223,24 @@ rb_ca_face_lift_method (VALUE self, VALUE face_parent)
1201
1223
  if ( ! ca_is_face(fp) ) {
1202
1224
  rb_raise(rb_eArgError, "face_lift: face_parent must be a Face instance");
1203
1225
  }
1226
+ rb_check_carray_object(self);
1227
+ TypedData_Get_Struct(self, CArray, &carray_data_type, vc);
1228
+ /* A Face of the same class is already lifted (see ca_face_lift). */
1229
+ if ( ca_is_face(vc) && rb_obj_class(self) == rb_obj_class(face_parent) ) {
1230
+ return self;
1231
+ }
1232
+ storage = ca_strip_face(fp);
1233
+ /* A Face reads the cells under it as its own, so they must be held in
1234
+ its surface type or its storage type. A Face receiver is held in its
1235
+ surface type, and is checked the same way. */
1236
+ if ( ! ( vc->data_type == fp->data_type && vc->bytes == fp->bytes )
1237
+ && ! ( vc->data_type == storage->data_type
1238
+ && vc->bytes == storage->bytes ) ) {
1239
+ rb_raise(rb_eArgError,
1240
+ "face_lift: a %"PRIsVALUE" holds its cells as %s, not as %s",
1241
+ rb_obj_class(face_parent), ca_type_name[storage->data_type],
1242
+ ca_type_name[vc->data_type]);
1243
+ }
1204
1244
  return ca_face_lift(self, face_parent);
1205
1245
  }
1206
1246
 
data/ext/ca_obj_fake.c CHANGED
@@ -154,6 +154,71 @@ ca_fake_func_clone (void *ap)
154
154
  return ca_fake_new(ca->parent, ca->data_type, ca->bytes);
155
155
  }
156
156
 
157
+ /* A read does not raise for a masked cell (ca_fake_cast_get), as attach
158
+ does not: the value under a mask is not defined, and converting it can
159
+ raise (NaN to an integer type). The view's cells are its parent's at the same
160
+ addresses, so the parent's mask is read for the same cells. The view's
161
+ own mask is not made here: once it exists, sync leaves masked cells
162
+ alone, and a read must not change what a later write does. */
163
+ static CArray *
164
+ ca_fake_read_mask (CAFake *ca)
165
+ {
166
+ if ( ca->mask ) {
167
+ return ca->mask;
168
+ }
169
+ if ( ca_has_mask(ca->parent) ) {
170
+ return ca->parent->mask;
171
+ }
172
+ return NULL;
173
+ }
174
+
175
+ /* Convert n cells read from the parent. A masked cell is converted as
176
+ well, so that the cell holds what the parent holds (a walk that sends
177
+ back only the cells a kernel changed compares against it), but a value
178
+ that cannot be converted (NaN to an integer) leaves zero bytes instead
179
+ of raising: the value under a mask is not defined. */
180
+ typedef struct {
181
+ CArray *src_ca, *dst_ca;
182
+ char *src, *dst;
183
+ } ca_fake_cell_ctx_t;
184
+
185
+ static VALUE
186
+ ca_fake_cast_cell (VALUE arg)
187
+ {
188
+ ca_fake_cell_ctx_t *c = (ca_fake_cell_ctx_t *) arg;
189
+ ca_ptr2ptr(c->src_ca, c->src, c->dst_ca, c->dst);
190
+ return Qnil;
191
+ }
192
+
193
+ static void
194
+ ca_fake_cast_get (CAFake *ca, ca_size_t n, char *src, char *dst, boolean8_t *m)
195
+ {
196
+ CArray *parent = ca->parent;
197
+ ca_size_t i;
198
+ if ( ! m ) {
199
+ ca_cast_block(n, parent, src, (CArray *) ca, dst);
200
+ return;
201
+ }
202
+ ca_cast_block_skip_masked(n, parent, src, (CArray *) ca, dst, m);
203
+ for ( i = 0; i < n; i++ ) {
204
+ if ( m[i] ) {
205
+ ca_fake_cell_ctx_t c;
206
+ int state = 0;
207
+ c.src_ca = parent;
208
+ c.dst_ca = (CArray *) ca;
209
+ c.src = src + i * parent->bytes;
210
+ c.dst = dst + i * ca->bytes;
211
+ /* A masked cell that cannot be converted reads as zero bytes. */
212
+ /* protect: converts a masked cell; nothing is held */
213
+ rb_protect(ca_fake_cast_cell, (VALUE) &c, &state);
214
+ if ( state ) {
215
+ rb_set_errinfo(Qnil);
216
+ memset(c.dst, 0, ca->bytes);
217
+ }
218
+ }
219
+ }
220
+ }
221
+
157
222
  /* Per-cell GET/PUT via a parent-sized scratch cell — same code services
158
223
  both directions, casting through the parent data_type. */
159
224
  static void
@@ -161,10 +226,16 @@ ca_fake_func_xfer_index (void *ap, ca_size_t *idx, void *data, int dir)
161
226
  {
162
227
  CAFake *ca = (CAFake *) ap;
163
228
  volatile VALUE holder;
164
- char *v = ALLOCV_N(char, holder, ca->parent->bytes);
229
+ char *v;
230
+ v = ALLOCV_N(char, holder, ca->parent->bytes);
165
231
  if ( dir == CA_XFER_GET ) {
232
+ CArray *mask = ca_fake_read_mask(ca);
233
+ boolean8_t masked = 0;
234
+ if ( mask ) {
235
+ ca_fetch_index(mask, idx, &masked);
236
+ }
166
237
  ca_fetch_index(ca->parent, idx, v);
167
- ca_ptr2ptr(ca->parent, v, ca, data);
238
+ ca_fake_cast_get(ca, 1, v, (char *) data, mask ? &masked : NULL);
168
239
  }
169
240
  else {
170
241
  ca_ptr2ptr(ca, data, ca->parent, v);
@@ -184,10 +255,22 @@ ca_fake_func_xfer_addrs (void *ap, ca_size_t n, ca_size_t *addrs,
184
255
  CAFake *ca = (CAFake *) ap;
185
256
  CArray *parent = ca->parent;
186
257
  CArray *eff;
258
+ CArray *mask = ( dir == CA_XFER_GET ) ? ca_fake_read_mask(ca) : NULL;
187
259
  ca_size_t base;
188
260
  char *v;
189
261
  volatile VALUE holder;
190
262
 
263
+ if ( mask ) {
264
+ volatile VALUE mholder;
265
+ boolean8_t *m = ALLOCV_N(boolean8_t, mholder, n + n * parent->bytes);
266
+ v = (char *) (m + n);
267
+ ca_xfer_addrs(mask, n, addrs, m, CA_XFER_GET);
268
+ ca_xfer_addrs(parent, n, addrs, v, CA_XFER_GET);
269
+ ca_fake_cast_get(ca, n, v, (char *) data, m);
270
+ ALLOCV_END(mholder);
271
+ return;
272
+ }
273
+
191
274
  /* Sequential-run + live-ptr fast path: cast directly from
192
275
  parent->ptr + base*pbytes to data, no intermediate scratch. */
193
276
  eff = ca_resolve_attached_root_via_identity(parent);
@@ -251,6 +334,24 @@ ca_fake_func_xfer_stride (void *ap, ca_size_t *starts, ca_size_t *counts,
251
334
  int8_t k;
252
335
  char *v;
253
336
  volatile VALUE holder;
337
+ CArray *mask = ( dir == CA_XFER_GET ) ? ca_fake_read_mask(ca) : NULL;
338
+
339
+ if ( mask ) {
340
+ ca_size_t mstrides[CA_RANK_MAX];
341
+ boolean8_t *m;
342
+ for ( k = 0; k < ndim; k++ ) {
343
+ n *= counts[k];
344
+ pstrides[k] = strides[k] / ca->bytes * ca->parent->bytes;
345
+ mstrides[k] = strides[k] / ca->bytes;
346
+ }
347
+ m = ALLOCV_N(boolean8_t, holder, n + n * ca->parent->bytes);
348
+ v = (char *) (m + n);
349
+ ca_xfer_stride(mask, starts, counts, mstrides, m, CA_XFER_GET);
350
+ ca_xfer_stride(ca->parent, starts, counts, pstrides, v, CA_XFER_GET);
351
+ ca_fake_cast_get(ca, n, v, (char *) data, m);
352
+ ALLOCV_END(holder);
353
+ return;
354
+ }
254
355
 
255
356
  /* Fast path: fused walk + cast for GET/PUT */
256
357
  if ( ca_xfer_stride_transform_fused((CArray *)ca, ca->parent,
@@ -282,21 +383,19 @@ static void
282
383
  ca_fake_func_allocate (void *ap)
283
384
  {
284
385
  CAFake *ca = (CAFake *) ap;
285
- ca_attach(ca->parent);
286
- ca->ptr = ca_alloc_cells((CArray *) ca);
386
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
287
387
  }
288
388
 
289
389
  static void
290
390
  ca_fake_func_attach (void *ap)
291
391
  {
292
392
  CAFake *ca = (CAFake *) ap;
293
- ca_attach(ca->parent);
294
- ca->ptr = ca_alloc_cells((CArray *) ca);
393
+ ca->ptr = ca_alloc_cells_then_attach((CArray *) ca, 1, &ca->parent);
295
394
 
296
395
  if ( ca->parent->mask ) {
297
- ca_cast_block_with_mask(ca->elements, ca->parent, ca->parent->ptr,
298
- ca, ca->ptr,
299
- (boolean8_t*)ca->parent->mask->ptr);
396
+ ca_cast_block_skip_masked(ca->elements, ca->parent, ca->parent->ptr,
397
+ ca, ca->ptr,
398
+ (boolean8_t*)ca->parent->mask->ptr);
300
399
  }
301
400
  else {
302
401
  ca_cast_block(ca->elements, ca->parent, ca->parent->ptr, ca, ca->ptr);
@@ -309,13 +408,15 @@ ca_fake_func_sync (void *ap)
309
408
  CAFake *ca = (CAFake *) ap;
310
409
  ca_update_mask(ca);
311
410
  if ( ca->mask ) {
312
- ca_cast_block_with_mask(ca->elements, ca, ca->ptr, ca->parent, ca->parent->ptr,
313
- (boolean8_t *)ca->parent->mask->ptr);
411
+ ca_cast_block_skip_masked(ca->elements, ca, ca->ptr, ca->parent, ca->parent->ptr,
412
+ (boolean8_t *)ca->mask->ptr);
314
413
  }
315
414
  else {
316
415
  ca_cast_block(ca->elements, ca, ca->ptr, ca->parent, ca->parent->ptr);
317
416
  }
417
+ ca_view_pass_mask_to_parent(ca);
318
418
  ca_sync(ca->parent);
419
+ ca_view_sync_mask_after_parent(ca);
319
420
  }
320
421
 
321
422
  static void
@@ -424,9 +525,9 @@ rb_ca_fake_new (VALUE cary, int8_t data_type, ca_size_t bytes)
424
525
  Both ways down stay open and say which one they are. A Numeric Face
425
526
  is not one of these: its surface is its storage. */
426
527
  rb_raise(rb_eTypeError,
427
- "%s has no view of its values in another data_type: "
528
+ "%"PRIsVALUE" has no view of its values in another data_type: "
428
529
  "#to_type gives the values, #parent.fake the raw storage",
429
- rb_obj_classname(cary));
530
+ rb_obj_class(cary));
430
531
  }
431
532
  ca = ca_fake_new(parent, data_type, bytes);
432
533
  obj = ca_wrap_struct(ca);
data/ext/ca_obj_farray.c CHANGED
@@ -8,6 +8,7 @@
8
8
 
9
9
  #include "carray.h"
10
10
  #include "carray_internal.h" /* ca_view_inherit_mask */
11
+ #include "ca_obj_face.h" /* CA_WRAPPER_LIFT */
11
12
 
12
13
  extern ca_operation_function_t ca_stride_func;
13
14
 
@@ -87,7 +88,12 @@ rb_ca_farray_new (VALUE cary)
87
88
  VALUE
88
89
  rb_ca_farray (VALUE self)
89
90
  {
90
- return rb_ca_farray_new(self);
91
+ volatile VALUE obj = rb_ca_farray_new(self);
92
+ CArray *ca;
93
+
94
+ TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
95
+ CA_WRAPPER_LIFT(obj, self, ca);
96
+ return obj;
91
97
  }
92
98
 
93
99
  static VALUE
@@ -10,8 +10,9 @@
10
10
  fixlen bytes as a struct via the data_class encode/decode hooks); here
11
11
  the bytes are the string, fetched and stored by the native fixlen path.
12
12
 
13
- The Face's only job is to carry the CAFixlenString class across the view
14
- chain and to organise the String method surface onto a dedicated class.
13
+ The Face carries the CAFixlenString class and the encoding of the text
14
+ across the view chain, and organises the String method surface onto a
15
+ dedicated class.
15
16
  Because the storage is CA_FIXLEN (portable), the Face wrap can be carried
16
17
  across multi-parent constructions (CAStack / Marshal / MemoryView).
17
18
 
@@ -23,6 +24,7 @@
23
24
  #include "carray.h"
24
25
  #include "ca_obj_face.h"
25
26
  #include "carray_internal.h"
27
+ #include <ruby/encoding.h>
26
28
 
27
29
  typedef struct {
28
30
  /* === CAView prefix === */
@@ -39,7 +41,8 @@ typedef struct {
39
41
  CArray *parent;
40
42
  uint32_t attach;
41
43
  uint8_t nosync;
42
- /* === Face tail === (none: CAFixlenString carries no per-instance state) */
44
+ /* === Face tail === */
45
+ int encoding_id; /* rb_enc_to_index() value of the cells' text */
43
46
  } CAFixlenString;
44
47
 
45
48
  static size_t
@@ -83,7 +86,7 @@ static VALUE rb_cCAFixlenString;
83
86
  /* ------------------------------------------------------------------- */
84
87
 
85
88
  int
86
- ca_fixlen_string_setup (CAFixlenString *ca, CArray *parent)
89
+ ca_fixlen_string_setup (CAFixlenString *ca, CArray *parent, int encoding_id)
87
90
  {
88
91
  if ( parent->data_type != CA_FIXLEN ) {
89
92
  rb_raise(rb_eTypeError,
@@ -136,6 +139,7 @@ ca_fixlen_string_setup (CAFixlenString *ca, CArray *parent)
136
139
  ca->parent = parent;
137
140
  ca->attach = 0;
138
141
  ca->nosync = 0;
142
+ ca->encoding_id = encoding_id;
139
143
 
140
144
 
141
145
  if ( ca_is_scalar(parent) ) {
@@ -150,12 +154,12 @@ ca_fixlen_string_setup (CAFixlenString *ca, CArray *parent)
150
154
  }
151
155
 
152
156
  CAFixlenString *
153
- ca_fixlen_string_new (CArray *parent)
157
+ ca_fixlen_string_new (CArray *parent, int encoding_id)
154
158
  {
155
159
  CAFixlenString *ca;
156
- ca_fixlen_string_setup(NULL, parent);
160
+ ca_fixlen_string_setup(NULL, parent, encoding_id);
157
161
  ca = (CAFixlenString *) ca_array_alloc(CA_OBJ_FIXLEN_STRING, parent->ndim);
158
- ca_fixlen_string_setup(ca, parent);
162
+ ca_fixlen_string_setup(ca, parent, encoding_id);
159
163
  ca_fill_or_free((CArray *) ca, ca_view_inherit_mask, (VALUE) ca);
160
164
  return ca;
161
165
  }
@@ -182,7 +186,7 @@ static void *
182
186
  ca_fixlen_string_func_clone (void *ap)
183
187
  {
184
188
  CAFixlenString *ca = (CAFixlenString *) ap;
185
- return ca_fixlen_string_new(ca->parent);
189
+ return ca_fixlen_string_new(ca->parent, ca->encoding_id);
186
190
  }
187
191
 
188
192
  static void
@@ -216,9 +220,10 @@ ca_operation_function_t ca_fixlen_string_func = {
216
220
 
217
221
  /* ------------------------------------------------------------------- */
218
222
 
219
- /* CAFixlenString.wrap(fixlen_array) — zero-copy Face wrap */
223
+ /* CAFixlenString.wrap(fixlen_array, encoding: nil) — zero-copy Face wrap.
224
+ Without an encoding the cells are bytes that claim no text encoding. */
220
225
  VALUE
221
- rb_ca_fixlen_string_wrap (VALUE parent_val)
226
+ rb_ca_fixlen_string_wrap (VALUE parent_val, int encoding_id)
222
227
  {
223
228
  volatile VALUE obj;
224
229
  CArray *parent;
@@ -227,17 +232,41 @@ rb_ca_fixlen_string_wrap (VALUE parent_val)
227
232
  rb_check_carray_object(parent_val);
228
233
  TypedData_Get_Struct(parent_val, CArray, &carray_data_type, parent);
229
234
 
230
- ca = ca_fixlen_string_new(parent);
235
+ ca = ca_fixlen_string_new(parent, encoding_id);
231
236
  obj = ca_wrap_struct(ca);
232
237
  rb_ca_set_parent(obj, parent_val);
233
238
  return obj;
234
239
  }
235
240
 
236
241
  static VALUE
237
- rb_ca_fixlen_string_wrap_method (VALUE klass, VALUE raw)
242
+ rb_ca_fixlen_string_wrap_method (int argc, VALUE *argv, VALUE klass)
238
243
  {
244
+ VALUE raw, ropt, renc = Qnil;
239
245
  (void) klass;
240
- return rb_ca_fixlen_string_wrap(raw);
246
+ rb_scan_args(argc, argv, "1:", &raw, &ropt);
247
+ rb_scan_options(ropt, "encoding", &renc);
248
+ return rb_ca_fixlen_string_wrap(raw, NIL_P(renc) ? rb_ascii8bit_encindex()
249
+ : rb_to_encoding_index(renc));
250
+ }
251
+
252
+ /* The encoding of the text in the cells; BINARY when the cells claim none. */
253
+ static VALUE
254
+ rb_ca_fixlen_string_encoding (VALUE self)
255
+ {
256
+ CAFixlenString *ca;
257
+ TypedData_Get_Struct(self, CAFixlenString, &cafixlen_string_data_type, ca);
258
+ return rb_enc_from_encoding(rb_enc_from_index(ca->encoding_id));
259
+ }
260
+
261
+ /* Arrays of two text encodings cannot be read as one column; cells that
262
+ claim no encoding go with either. */
263
+ static int
264
+ ca_fixlen_string_state_compatible (CArray *a, CArray *b)
265
+ {
266
+ int ea = ((CAFixlenString *) a)->encoding_id;
267
+ int eb = ((CAFixlenString *) b)->encoding_id;
268
+ int binary = rb_ascii8bit_encindex();
269
+ return ( ea == eb || ea == binary || eb == binary ) ? 1 : 0;
241
270
  }
242
271
 
243
272
  /* Per-cell scalar decode: strip trailing NUL padding from the raw K-byte
@@ -249,14 +278,15 @@ rb_ca_fixlen_string_wrap_method (VALUE klass, VALUE raw)
249
278
  static VALUE
250
279
  rb_ca_fixlen_string_storage_to_element (VALUE self, VALUE raw)
251
280
  {
252
- (void) self;
253
281
  if ( TYPE(raw) == T_STRING ) {
282
+ CAFixlenString *ca;
254
283
  const char *p = RSTRING_PTR(raw);
255
284
  long n = RSTRING_LEN(raw);
256
285
  while ( n > 0 && p[n - 1] == '\0' ) {
257
286
  n--;
258
287
  }
259
- return rb_str_new(p, n);
288
+ TypedData_Get_Struct(self, CAFixlenString, &cafixlen_string_data_type, ca);
289
+ return rb_enc_str_new(p, n, rb_enc_from_index(ca->encoding_id));
260
290
  }
261
291
  return raw;
262
292
  }
@@ -279,7 +309,7 @@ rb_ca_fixlen_string_initialize_copy (VALUE self, VALUE other)
279
309
  if ( ca_func[CA_OBJ_FIXLEN_STRING].pool_init ) {
280
310
  ca_array_pool_alloc(ca, CA_OBJ_FIXLEN_STRING, cs->parent->ndim);
281
311
  }
282
- ca_fixlen_string_setup(ca, cs->parent);
312
+ ca_fixlen_string_setup(ca, cs->parent, cs->encoding_id);
283
313
  ca_view_inherit_mask((VALUE) ca);
284
314
  return self;
285
315
  }
@@ -304,7 +334,9 @@ Init_ca_obj_fixlen_string (void)
304
334
  rb_define_method(rb_cCAFixlenString, "initialize_copy",
305
335
  rb_ca_fixlen_string_initialize_copy, 1);
306
336
  rb_define_singleton_method(rb_cCAFixlenString, "wrap",
307
- rb_ca_fixlen_string_wrap_method, 1);
337
+ rb_ca_fixlen_string_wrap_method, -1);
338
+ rb_define_method(rb_cCAFixlenString, "encoding",
339
+ rb_ca_fixlen_string_encoding, 0);
308
340
  rb_define_method(rb_cCAFixlenString, "storage_to_element",
309
341
  rb_ca_fixlen_string_storage_to_element, 1);
310
342
  ca_face_register_storage_to_element(CA_OBJ_FIXLEN_STRING,
@@ -314,6 +346,8 @@ Init_ca_obj_fixlen_string (void)
314
346
  or per-parent buffer), so the Face can be carried across multi-parent
315
347
  constructions like CAStack. */
316
348
  ca_face_register_state_portable(CA_OBJ_FIXLEN_STRING, 1);
349
+ ca_face_register_state_compatible(CA_OBJ_FIXLEN_STRING,
350
+ ca_fixlen_string_state_compatible);
317
351
 
318
352
  /* The bytes are the strings: a MemoryView consumer reads them as "Ns". */
319
353
  ca_face_register_memory_view(CA_OBJ_FIXLEN_STRING, 1);