carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
data/ext/carray_cast.c CHANGED
@@ -24,17 +24,12 @@
24
24
  aliases (CA_BYTE / CA_INT / CA_FLOAT / CA_SIZE /
25
25
  ...) reuse the canonical entry.
26
26
 
27
- Sibling of ext/carray_data_type.c (type-tag class shells +
28
- data_type id <-> class mapping); the CA_<TYPE>(data) functions
29
- here pair with the typed classes registered there.
30
-
31
27
  ---------------------------------------------------------------------------- */
32
28
 
33
29
  #include "carray.h"
34
30
  #include "ruby/memory_view.h"
35
31
  #include "ca_monop_dispatch.h" /* CA_MONOP_CAST_BASE for as_type routing */
36
32
  #include "ca_obj_face.h" /* ca_strip_face for write path lift */
37
- #include "carray_internal.h" /* rb_ca_inherit_attr */
38
33
  #include "ruby/util.h" /* ruby_strtod */
39
34
  #include <math.h>
40
35
 
@@ -569,7 +564,7 @@ ca_decimal_read_bytes_as (const char *s, long n, int8_t data_type, void *out)
569
564
  }
570
565
 
571
566
  static int
572
- ca_decimal_string_read (VALUE str, int8_t data_type, void *out)
567
+ ca_decimal_string_read (volatile VALUE str, int8_t data_type, void *out)
573
568
  {
574
569
  return ca_decimal_read_bytes_as(RSTRING_PTR(str), RSTRING_LEN(str), data_type, out);
575
570
  }
@@ -598,6 +593,201 @@ ca_decimal_string_store (VALUE str, int8_t data_type, void *out)
598
593
  }
599
594
  }
600
595
 
596
+ /* The words a boolean cell is read from in text: 1 / 0 / true / false in
597
+ any case, with spaces around them ignored, as CAFrame#cast reads a text
598
+ column. 1 with the value at out, 0 for any other text. */
599
+ static int
600
+ ca_boolean_text_read (volatile VALUE str, boolean8_t *out)
601
+ {
602
+ const char *p = RSTRING_PTR(str);
603
+ long n = RSTRING_LEN(str);
604
+ while ( n > 0 && ISSPACE(*p) ) { p++; n--; }
605
+ while ( n > 0 && ISSPACE(p[n-1]) ) { n--; }
606
+ if ( n == 1 && ( *p == '0' || *p == '1' ) ) {
607
+ *out = (boolean8_t) ( *p == '1' );
608
+ return 1;
609
+ }
610
+ if ( n == 4 && ! strncasecmp(p, "true", 4) ) {
611
+ *out = 1;
612
+ return 1;
613
+ }
614
+ if ( n == 5 && ! strncasecmp(p, "false", 5) ) {
615
+ *out = 0;
616
+ return 1;
617
+ }
618
+ return 0;
619
+ }
620
+
621
+ /* The to_type reader of a boolean cell: a String by its words, any other
622
+ value as OBJ2BOOL takes it (true / false / 0 / 1). 1 with the value at
623
+ out, or 0 for a cell that becomes UNDEF. */
624
+ int
625
+ ca_boolean_value_to (VALUE v, void *out)
626
+ {
627
+ boolean8_t *b = (boolean8_t *) out;
628
+ if ( RB_TYPE_P(v, T_STRING) ) {
629
+ return ca_boolean_text_read(v, b);
630
+ }
631
+ if ( v == Qtrue || v == Qfalse ) {
632
+ *b = (boolean8_t) ( v == Qtrue );
633
+ return 1;
634
+ }
635
+ if ( FIXNUM_P(v) && ( FIX2LONG(v) == 0 || FIX2LONG(v) == 1 ) ) {
636
+ *b = (boolean8_t) FIX2LONG(v);
637
+ return 1;
638
+ }
639
+ return 0;
640
+ }
641
+
642
+ /* The store reader of a boolean cell: the value at out, or a raise. */
643
+ void
644
+ ca_boolean_value_store (VALUE v, void *out)
645
+ {
646
+ if ( RB_TYPE_P(v, T_STRING) ) {
647
+ if ( ! ca_boolean_text_read(v, (boolean8_t *) out) ) {
648
+ rb_raise(rb_eArgError, "%s: %+"PRIsVALUE" cannot be read as boolean",
649
+ ca_calling_method_name(), v);
650
+ }
651
+ return;
652
+ }
653
+ *(boolean8_t *) out = OBJ2BOOL(v);
654
+ }
655
+
656
+ /* A Ruby Array given where one value of a cell is read (fill, a search
657
+ query, a weight, a fill_value: ...). It has no data type of its own, so
658
+ it is not read as several values here either: the caller gives one value,
659
+ or a CArray of the type it means where the method takes an array. */
660
+ void
661
+ ca_refuse_array_value (const char *kind)
662
+ {
663
+ rb_raise(rb_eTypeError, "%s: a Ruby Array is not a value of %s cell",
664
+ ca_calling_method_name(), kind);
665
+ }
666
+
667
+ /* nil given where one value of a cell is read. nil is no number; UNDEF is
668
+ the value that masks a cell. */
669
+ void
670
+ ca_refuse_nil_value (const char *kind)
671
+ {
672
+ rb_raise(rb_eTypeError,
673
+ "%s: nil is not a value of %s cell; assign UNDEF to mask the cell",
674
+ ca_calling_method_name(), kind);
675
+ }
676
+
677
+ /* A CScalar is an array of one cell, so a method that reads one value of a
678
+ cell refuses it as it refuses any CArray. The refusal says how to take
679
+ its value: s[0] gives the value, or UNDEF when the cell is masked. */
680
+ static const char *
681
+ ca_cscalar_value_hint (VALUE v)
682
+ {
683
+ return RTEST(rb_obj_is_kind_of(v, rb_cCScalar))
684
+ ? "; take the value of a CScalar with [0] (UNDEF when it is masked)"
685
+ : "";
686
+ }
687
+
688
+ /* A Ruby value given for a number cell (an integer, float or complex one)
689
+ is a number: a Numeric, or UNDEF. A String or another object is
690
+ refused, also one with to_i or to_f (a Time): reading text or other
691
+ values as numbers is the work of to_type on an object array. true,
692
+ false, nil and an Array go on to the cell's reader, which refuses them
693
+ in its own words. A boolean cell refuses a String the same way, and
694
+ leaves any other value to its reader. */
695
+ void
696
+ ca_refuse_non_number_value (int8_t data_type, VALUE v)
697
+ {
698
+ const char *kind;
699
+ if ( v == CA_UNDEF || v == Qtrue || v == Qfalse || NIL_P(v) ||
700
+ RB_TYPE_P(v, T_ARRAY) ) {
701
+ return;
702
+ }
703
+ if ( data_type >= CA_INT8 && data_type <= CA_UINT64 ) {
704
+ kind = "an integer";
705
+ }
706
+ else if ( data_type == CA_FLOAT32 || data_type == CA_FLOAT64 ) {
707
+ kind = "a float";
708
+ }
709
+ else if ( data_type == CA_CMPLX64 || data_type == CA_CMPLX128 ) {
710
+ kind = "a complex";
711
+ }
712
+ else if ( data_type == CA_BOOLEAN ) {
713
+ /* text is read into a boolean cell by to_type / as_type only; any
714
+ other value goes on to OBJ2BOOL */
715
+ if ( ! RB_TYPE_P(v, T_STRING) ) {
716
+ return;
717
+ }
718
+ kind = "a boolean";
719
+ }
720
+ else {
721
+ return;
722
+ }
723
+ if ( RTEST(rb_obj_is_kind_of(v, rb_cNumeric)) ) {
724
+ return;
725
+ }
726
+ rb_raise(rb_eTypeError, "%s: %"PRIsVALUE" (%"PRIsVALUE") is not a value of %s cell%s",
727
+ ca_calling_method_name(), rb_inspect(v), rb_obj_class(v), kind,
728
+ ca_cscalar_value_hint(v));
729
+ }
730
+
731
+ /* The elements of a Ruby Array (nested ones included) given for number
732
+ cells, each held to ca_refuse_non_number_value. */
733
+ void
734
+ ca_refuse_non_number_elements (int8_t data_type, VALUE ary)
735
+ {
736
+ long i, n = RARRAY_LEN(ary);
737
+ ca_check_stack_room();
738
+ for (i = 0; i < n; i++) {
739
+ VALUE e = RARRAY_AREF(ary, i);
740
+ if ( RB_TYPE_P(e, T_ARRAY) ) {
741
+ ca_refuse_non_number_elements(data_type, e);
742
+ }
743
+ else {
744
+ ca_refuse_non_number_value(data_type, e);
745
+ }
746
+ }
747
+ }
748
+
749
+ /* A value with no conversion to a number (a Symbol, say) stored into a
750
+ numeric cell. Without this the store raises the conversion's own
751
+ TypeError, which does not name the method. */
752
+ static VALUE
753
+ ca_number_of_value (VALUE v, int integer, const char *kind)
754
+ {
755
+ ID conv = integer ? rb_intern("to_int") : rb_intern("to_f");
756
+ /* A Complex reads as real only with an exact zero imaginary part, as
757
+ Complex#to_f takes it; otherwise the conversion's RangeError would not
758
+ name the method. */
759
+ if ( RB_TYPE_P(v, T_COMPLEX) ) {
760
+ VALUE im = rb_complex_imag(v);
761
+ if ( ! ( RB_INTEGER_TYPE_P(im) || RB_TYPE_P(im, T_RATIONAL) ) ||
762
+ ! RTEST(rb_funcall(im, rb_intern("zero?"), 0)) ) {
763
+ rb_raise(rb_eRangeError,
764
+ "%s: %"PRIsVALUE" has an imaginary part; %s cell takes a real number",
765
+ ca_calling_method_name(), rb_inspect(v), kind);
766
+ }
767
+ }
768
+ if ( ! rb_respond_to(v, conv) &&
769
+ ! ( integer && rb_respond_to(v, rb_intern("to_i")) ) ) {
770
+ rb_raise(rb_eTypeError, "%s: %"PRIsVALUE" (%"PRIsVALUE") is not a value of %s cell%s",
771
+ ca_calling_method_name(), rb_inspect(v), rb_obj_class(v), kind,
772
+ ca_cscalar_value_hint(v));
773
+ }
774
+ return integer ? rb_Integer(v) : rb_Float(v);
775
+ }
776
+
777
+ /* A Ruby Array given where an array with a data type is expected, from C
778
+ (the Ruby side has CArray.refuse_ruby_array). role names what it would
779
+ have been ("an operand"). */
780
+ void
781
+ ca_refuse_ruby_array (VALUE v, const char *role)
782
+ {
783
+ if ( RB_TYPE_P(v, T_ARRAY) ) {
784
+ rb_raise(rb_eTypeError,
785
+ "%s: a Ruby Array is not %s; make it a CArray of the type you "
786
+ "mean first, e.g. CA_INT64(ary) or CA_FLOAT64(ary)",
787
+ ca_calling_method_name(), role);
788
+ }
789
+ }
790
+
601
791
  boolean8_t
602
792
  OBJ2BOOL (VALUE v)
603
793
  {
@@ -607,17 +797,21 @@ OBJ2BOOL (VALUE v)
607
797
  else if ( v == Qtrue ) {
608
798
  return 1;
609
799
  }
610
- else if ( rb_obj_is_kind_of(v, rb_cInteger) ) {
611
- int flag = NUM2INT(v);
800
+ else if ( FIXNUM_P(v) ) {
801
+ /* an Integer past a Fixnum is no 0 or 1, and is refused below */
802
+ long flag = FIX2LONG(v);
612
803
  if ( flag == 0 || flag == 1 ) {
613
- return flag;
804
+ return (boolean8_t) flag;
614
805
  }
615
806
  }
616
- {
617
- VALUE inspect = rb_inspect(v);
618
- rb_raise(rb_eCADataTypeError,
619
- "can't cast object '%s' to <boolean>", StringValuePtr(inspect));
807
+ if ( RB_TYPE_P(v, T_ARRAY) ) {
808
+ ca_refuse_array_value("a boolean");
809
+ }
810
+ if ( NIL_P(v) ) {
811
+ ca_refuse_nil_value("a boolean");
620
812
  }
813
+ rb_raise(rb_eCADataTypeError, "%s: can't cast object '%"PRIsVALUE"' to <boolean>",
814
+ ca_calling_method_name(), rb_inspect(v));
621
815
  }
622
816
 
623
817
  VALUE
@@ -638,7 +832,7 @@ ca_str_nonfinite (const char *str, double *out)
638
832
  if ( ! strcasecmp(str, "nan")
639
833
  || ! strcasecmp(str, "+nan")
640
834
  || ! strcasecmp(str, "-nan") ) {
641
- *out = 0.0/0.0;
835
+ *out = NAN; /* 0.0/0.0 sets the sign bit on x86 */
642
836
  return 1;
643
837
  }
644
838
  else if ( ! strcasecmp(str, "inf")
@@ -667,8 +861,14 @@ OBJ2DBL (VALUE val)
667
861
  ca_decimal_string_store(val, CA_FLOAT64, &d);
668
862
  return d;
669
863
  }
864
+ case T_ARRAY:
865
+ ca_refuse_array_value("a float");
866
+ return 0.0;
867
+ case T_NIL:
868
+ ca_refuse_nil_value("a float");
869
+ return 0.0;
670
870
  default:
671
- return NUM2DBL(rb_Float(val));
871
+ return NUM2DBL(ca_number_of_value(val, 0, "a float"));
672
872
  }
673
873
  }
674
874
 
@@ -695,7 +895,7 @@ ca_kernel_integer (VALUE v)
695
895
  first character and not an exponent's. Returns 0 when the text is not
696
896
  of that form. */
697
897
  static int
698
- ca_str_to_cmplx_nonfinite (VALUE str, double complex *out)
898
+ ca_str_to_cmplx_nonfinite (volatile VALUE str, double complex *out)
699
899
  {
700
900
  const char *s = RSTRING_PTR(str);
701
901
  long n = RSTRING_LEN(str), k;
@@ -725,9 +925,15 @@ ca_str_to_cmplx_nonfinite (VALUE str, double complex *out)
725
925
  return 1;
726
926
  }
727
927
 
928
+ static VALUE
929
+ ca_kernel_complex (VALUE str)
930
+ {
931
+ return rb_funcall(rb_mKernel, rb_intern("Complex"), 1, str);
932
+ }
933
+
728
934
  /* A real is read as a decimal number; anything else goes to Complex(). */
729
935
  double complex
730
- ca_str_to_cmplx (VALUE str)
936
+ ca_str_to_cmplx (volatile VALUE str)
731
937
  {
732
938
  volatile VALUE rstr;
733
939
  volatile VALUE c;
@@ -742,10 +948,43 @@ ca_str_to_cmplx (VALUE str)
742
948
  return z;
743
949
  }
744
950
  rstr = rb_funcall(str, rb_intern("strip"), 0);
745
- c = rb_funcall(rb_mKernel, rb_intern("Complex"), 1, rstr);
951
+ {
952
+ int state = 0;
953
+ /* protect: converts the caller's value; nothing is held */
954
+ c = rb_protect(ca_kernel_complex, rstr, &state);
955
+ if ( state ) {
956
+ rb_set_errinfo(Qnil);
957
+ rb_raise(rb_eArgError, "%s: %+"PRIsVALUE" cannot be read as a complex number",
958
+ ca_calling_method_name(), str);
959
+ }
960
+ }
746
961
  return CMPLX(NUM2DBL(rb_complex_real(c)), NUM2DBL(rb_complex_imag(c)));
747
962
  }
748
963
 
964
+ double complex
965
+ ca_obj_to_cmplx (VALUE num)
966
+ {
967
+ if ( NIL_P(num) ) {
968
+ ca_refuse_nil_value("a complex");
969
+ }
970
+ if ( RB_TYPE_P(num, T_ARRAY) ) {
971
+ ca_refuse_array_value("a complex");
972
+ }
973
+ if ( rb_respond_to(num, rb_intern("to_c")) ) {
974
+ VALUE c = rb_funcall(num, rb_intern("to_c"), 0);
975
+ if ( ! RB_TYPE_P(c, T_COMPLEX) ) {
976
+ rb_raise(rb_eTypeError, "%s: can't convert %"PRIsVALUE" to Complex (%"PRIsVALUE"#to_c gives %"PRIsVALUE")",
977
+ ca_calling_method_name(), rb_obj_class(num), rb_obj_class(num), rb_obj_class(c));
978
+ }
979
+ return CMPLX(NUM2DBL(rb_complex_real(c)), NUM2DBL(rb_complex_imag(c)));
980
+ }
981
+ if ( rb_respond_to(num, rb_intern("to_f")) ) {
982
+ return (double complex) NUM2DBL(rb_funcall(num, rb_intern("to_f"), 0));
983
+ }
984
+ rb_raise(rb_eTypeError, "%s: can't convert %"PRIsVALUE" to Complex",
985
+ ca_calling_method_name(), rb_obj_class(num));
986
+ }
987
+
749
988
  /* Object -> double with parse-failure signalling. Returns 1 and sets *out
750
989
  on success; returns 0 when the cell cannot be parsed, in which case the
751
990
  caller writes UNDEF (mask) for that cell. nil and unreadable
@@ -831,6 +1070,40 @@ ca_obj2integer_ok (VALUE val, int8_t wide_type, void *wide, VALUE *integer)
831
1070
  return 1;
832
1071
  }
833
1072
 
1073
+ /* The to_type reader of an object cell for an integer type: a number that
1074
+ is not an Integer (Float, Rational, BigDecimal, Complex) and does not fit
1075
+ the type is a missing value, as a float array's cell is. An Integer
1076
+ keeps its low bytes (the readers below). Returns 1 when the cell may be
1077
+ read on. */
1078
+ int
1079
+ ca_obj_number_fits_integer (VALUE val, int8_t data_type)
1080
+ {
1081
+ int state = 0;
1082
+ volatile VALUE integer;
1083
+ if ( RB_INTEGER_TYPE_P(val) || NIL_P(val) || RB_TYPE_P(val, T_STRING) ) {
1084
+ return 1;
1085
+ }
1086
+ /* protect: converts the caller's value and reports failure; nothing is held */
1087
+ integer = rb_protect(ca_kernel_integer, val, &state);
1088
+ if ( state ) {
1089
+ rb_set_errinfo(Qnil);
1090
+ return 1; /* the reader fails it on its own */
1091
+ }
1092
+ return ca_integer_range_side(integer, data_type) == 0;
1093
+ }
1094
+
1095
+ /* The low bytes of an Integer in two's complement, for the _ok readers
1096
+ below: an Integer wider than the C type is cut to it, as an Integer that
1097
+ fits a long is when the caller narrows it to the cell's type. These
1098
+ readers report failure through their return value and do not raise, so
1099
+ a Bignum cannot go through NUM2LONG and friends. */
1100
+ static void
1101
+ ca_integer_low_bytes (VALUE integer, void *out, size_t size)
1102
+ {
1103
+ rb_integer_pack(integer, out, 1, size, 0,
1104
+ INTEGER_PACK_2COMP | INTEGER_PACK_NATIVE_BYTE_ORDER);
1105
+ }
1106
+
834
1107
  int
835
1108
  rb_obj2long_ok (VALUE val, long *out)
836
1109
  {
@@ -843,7 +1116,12 @@ rb_obj2long_ok (VALUE val, long *out)
843
1116
  if ( ! ca_obj2integer_ok(val, CA_INT64, &wide, &integer) ) {
844
1117
  return 0;
845
1118
  }
846
- *out = NIL_P(integer) ? (long) wide : NUM2LONG(integer);
1119
+ if ( NIL_P(integer) ) {
1120
+ *out = (long) wide;
1121
+ }
1122
+ else {
1123
+ ca_integer_low_bytes(integer, out, sizeof(*out));
1124
+ }
847
1125
  return 1;
848
1126
  }
849
1127
 
@@ -859,7 +1137,12 @@ rb_obj2ulong_ok (VALUE val, unsigned long *out)
859
1137
  if ( ! ca_obj2integer_ok(val, CA_UINT64, &wide, &integer) ) {
860
1138
  return 0;
861
1139
  }
862
- *out = NIL_P(integer) ? (unsigned long) wide : NUM2ULONG(integer);
1140
+ if ( NIL_P(integer) ) {
1141
+ *out = (unsigned long) wide;
1142
+ }
1143
+ else {
1144
+ ca_integer_low_bytes(integer, out, sizeof(*out));
1145
+ }
863
1146
  return 1;
864
1147
  }
865
1148
 
@@ -875,7 +1158,12 @@ rb_obj2ll_ok (VALUE val, long long *out)
875
1158
  if ( ! ca_obj2integer_ok(val, CA_INT64, &wide, &integer) ) {
876
1159
  return 0;
877
1160
  }
878
- *out = NIL_P(integer) ? (long long) wide : NUM2LL(integer);
1161
+ if ( NIL_P(integer) ) {
1162
+ *out = (long long) wide;
1163
+ }
1164
+ else {
1165
+ ca_integer_low_bytes(integer, out, sizeof(*out));
1166
+ }
879
1167
  return 1;
880
1168
  }
881
1169
 
@@ -891,7 +1179,12 @@ rb_obj2ull_ok (VALUE val, unsigned long long *out)
891
1179
  if ( ! ca_obj2integer_ok(val, CA_UINT64, &wide, &integer) ) {
892
1180
  return 0;
893
1181
  }
894
- *out = NIL_P(integer) ? (unsigned long long) wide : rb_num2ull(integer);
1182
+ if ( NIL_P(integer) ) {
1183
+ *out = (unsigned long long) wide;
1184
+ }
1185
+ else {
1186
+ ca_integer_low_bytes(integer, out, sizeof(*out));
1187
+ }
895
1188
  return 1;
896
1189
  }
897
1190
 
@@ -904,7 +1197,10 @@ rb_obj2long (VALUE val)
904
1197
  case T_BIGNUM:
905
1198
  return (long) NUM2LL(val);
906
1199
  case T_NIL:
907
- rb_raise(rb_eTypeError, "can't convert nil into Integer");
1200
+ ca_refuse_nil_value("an integer");
1201
+ break;
1202
+ case T_ARRAY:
1203
+ ca_refuse_array_value("an integer");
908
1204
  break;
909
1205
  case T_STRING: {
910
1206
  int64_t v;
@@ -912,7 +1208,7 @@ rb_obj2long (VALUE val)
912
1208
  return (long) v;
913
1209
  }
914
1210
  default:
915
- return NUM2LONG(rb_Integer(val));
1211
+ return NUM2LONG(ca_number_of_value(val, 1, "an integer"));
916
1212
  }
917
1213
  }
918
1214
 
@@ -925,7 +1221,10 @@ rb_obj2ulong (VALUE val)
925
1221
  case T_BIGNUM:
926
1222
  return (unsigned long) rb_num2ull(val);
927
1223
  case T_NIL:
928
- rb_raise(rb_eTypeError, "can't convert nil into Integer");
1224
+ ca_refuse_nil_value("an integer");
1225
+ break;
1226
+ case T_ARRAY:
1227
+ ca_refuse_array_value("an integer");
929
1228
  break;
930
1229
  case T_STRING: {
931
1230
  uint64_t v;
@@ -933,7 +1232,7 @@ rb_obj2ulong (VALUE val)
933
1232
  return (unsigned long) v;
934
1233
  }
935
1234
  default:
936
- return NUM2ULONG(rb_Integer(val));
1235
+ return NUM2ULONG(ca_number_of_value(val, 1, "an integer"));
937
1236
  }
938
1237
  }
939
1238
 
@@ -944,7 +1243,10 @@ rb_obj2ll (VALUE val)
944
1243
  case T_FIXNUM:
945
1244
  return NUM2LONG(val);
946
1245
  case T_NIL:
947
- rb_raise(rb_eTypeError, "can't convert nil into Integer");
1246
+ ca_refuse_nil_value("an integer");
1247
+ break;
1248
+ case T_ARRAY:
1249
+ ca_refuse_array_value("an integer");
948
1250
  break;
949
1251
  case T_STRING: {
950
1252
  int64_t v;
@@ -952,7 +1254,7 @@ rb_obj2ll (VALUE val)
952
1254
  return (long long) v;
953
1255
  }
954
1256
  default:
955
- return NUM2LL(rb_Integer(val));
1257
+ return NUM2LL(ca_number_of_value(val, 1, "an integer"));
956
1258
  }
957
1259
  }
958
1260
 
@@ -963,7 +1265,10 @@ rb_obj2ull (VALUE val)
963
1265
  case T_FIXNUM:
964
1266
  return NUM2ULONG(val);
965
1267
  case T_NIL:
966
- rb_raise(rb_eTypeError, "can't convert nil into Integer");
1268
+ ca_refuse_nil_value("an integer");
1269
+ break;
1270
+ case T_ARRAY:
1271
+ ca_refuse_array_value("an integer");
967
1272
  break;
968
1273
  case T_STRING: {
969
1274
  uint64_t v;
@@ -971,7 +1276,7 @@ rb_obj2ull (VALUE val)
971
1276
  return (unsigned long long) v;
972
1277
  }
973
1278
  default:
974
- return rb_num2ull(rb_Integer(val));
1279
+ return rb_num2ull(ca_number_of_value(val, 1, "an integer"));
975
1280
  }
976
1281
  }
977
1282
 
@@ -1021,6 +1326,42 @@ ca_cast_block_with_mask (ca_size_t n, void *ap1, void *ptr1,
1021
1326
  ca_cast_func_table[ca1->data_type][ca2->data_type](n, ca1, ptr1, ca2, ptr2, m);
1022
1327
  }
1023
1328
 
1329
+ /* A store and a cast view: the masked cells are left alone and every other
1330
+ cell is converted as it is with no mask at all, so that the answer for a
1331
+ cell does not depend on another cell being masked. An object cell needs
1332
+ this: the table's loop with a mask is #to_type's, which wraps an integer
1333
+ the cell cannot hold and masks a value it cannot read. */
1334
+ void
1335
+ ca_cast_block_skip_masked (ca_size_t n, void *ap1, void *ptr1,
1336
+ void *ap2, void *ptr2, boolean8_t *m)
1337
+ {
1338
+ CArray *ca1 = (CArray *) ap1;
1339
+ CArray *ca2 = (CArray *) ap2;
1340
+ char *p1 = (char *) ptr1;
1341
+ char *p2 = (char *) ptr2;
1342
+ ca_size_t i = 0;
1343
+
1344
+ if ( m == NULL ) {
1345
+ ca_cast_block(n, ap1, ptr1, ap2, ptr2);
1346
+ return;
1347
+ }
1348
+ if ( ca1->data_type != CA_OBJECT ) {
1349
+ ca_cast_block_with_mask(n, ap1, ptr1, ap2, ptr2, m);
1350
+ return;
1351
+ }
1352
+ while ( i < n ) {
1353
+ ca_size_t j;
1354
+ if ( m[i] ) {
1355
+ i++;
1356
+ continue;
1357
+ }
1358
+ for ( j = i; j < n && ! m[j]; j++ ) {
1359
+ }
1360
+ ca_cast_block(j - i, ap1, p1 + i * ca1->bytes, ap2, p2 + i * ca2->bytes);
1361
+ i = j;
1362
+ }
1363
+ }
1364
+
1024
1365
  VALUE
1025
1366
  ca_ptr2obj (void *ap, void *ptr)
1026
1367
  {
@@ -1056,7 +1397,7 @@ ca_ptr2val (void *ap1, void *ptr1, int8_t data_type, void *ptr2)
1056
1397
  {
1057
1398
  CArray *ca1 = (CArray *)ap1;
1058
1399
  static CArray dummy;
1059
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
1400
+ CA_CHECK_DATA_TYPE(data_type);
1060
1401
  dummy.data_type = data_type;
1061
1402
  ca_cast_func_table[ca1->data_type][data_type](1, ca1, ptr1, &dummy, ptr2, NULL);
1062
1403
  return;
@@ -1067,7 +1408,7 @@ ca_val2ptr (int8_t data_type, void *ptr1, void *ap2, void *ptr2)
1067
1408
  {
1068
1409
  CArray *ca2 = (CArray *)ap2;
1069
1410
  static CArray dummy;
1070
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
1411
+ CA_CHECK_DATA_TYPE(data_type);
1071
1412
  dummy.data_type = data_type;
1072
1413
  ca_cast_func_table[data_type][ca2->data_type](1, &dummy, ptr1, ca2, ptr2, NULL);
1073
1414
  return;
@@ -1077,8 +1418,8 @@ void
1077
1418
  ca_val2val (int8_t data_type1, void *ptr1, int8_t data_type2, void *ptr2)
1078
1419
  {
1079
1420
  static CArray dummy1, dummy2;
1080
- CA_CHECK_DATA_TYPE_GIVEN(data_type1);
1081
- CA_CHECK_DATA_TYPE_GIVEN(data_type2);
1421
+ CA_CHECK_DATA_TYPE(data_type1);
1422
+ CA_CHECK_DATA_TYPE(data_type2);
1082
1423
  dummy1.data_type = data_type1;
1083
1424
  dummy2.data_type = data_type2;
1084
1425
  ca_cast_func_table[data_type1][data_type2](1, &dummy1, ptr1, &dummy2, ptr2, NULL);
@@ -1096,13 +1437,13 @@ ca_val2val (int8_t data_type1, void *ptr1, int8_t data_type2, void *ptr2)
1096
1437
  the storage instead is `.parent`, which stays available and explicit. Mask
1097
1438
  and shape carry. */
1098
1439
  static VALUE
1099
- rb_ca_surface_to_object (VALUE self)
1440
+ rb_ca_surface_to_object (volatile VALUE self)
1100
1441
  {
1101
1442
  volatile VALUE obj;
1102
1443
  CArray *ca;
1103
1444
  ca_size_t i;
1104
1445
 
1105
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1446
+ GetCArray(self, ca);
1106
1447
 
1107
1448
  if ( ca_is_scalar(ca) ) {
1108
1449
  obj = rb_cscalar_new(CA_OBJECT, 0, ca->mask);
@@ -1138,7 +1479,7 @@ static VALUE
1138
1479
  rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1139
1480
  {
1140
1481
  static ID id_to_numeric = 0;
1141
- VALUE num;
1482
+ volatile VALUE num;
1142
1483
  CArray *cn;
1143
1484
 
1144
1485
  if ( id_to_numeric == 0 ) {
@@ -1147,32 +1488,32 @@ rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1147
1488
 
1148
1489
  if ( ! rb_respond_to(self, id_to_numeric) ) {
1149
1490
  rb_raise(rb_eTypeError,
1150
- "%s declares no numeric conversion: define #to_numeric to say "
1491
+ "%"PRIsVALUE" declares no numeric conversion: define #to_numeric to say "
1151
1492
  "how its values become numbers, or take the surface with "
1152
1493
  "to_type(:object) / the storage with #parent",
1153
- rb_obj_classname(self));
1494
+ rb_obj_class(self));
1154
1495
  }
1155
1496
 
1156
1497
  num = rb_funcall(self, id_to_numeric, 0);
1157
1498
 
1158
1499
  if ( ! rb_obj_is_carray(num) ) {
1159
1500
  rb_raise(rb_eTypeError,
1160
- "%s#to_numeric must return a CArray (got %s)",
1161
- rb_obj_classname(self), rb_obj_classname(num));
1501
+ "%"PRIsVALUE"#to_numeric must return a CArray (got %"PRIsVALUE")",
1502
+ rb_obj_class(self), rb_obj_class(num));
1162
1503
  }
1163
1504
 
1164
- TypedData_Get_Struct(num, CArray, &carray_data_type, cn);
1505
+ GetCArray(num, cn);
1165
1506
 
1166
1507
  if ( ca_is_face(cn) ) {
1167
1508
  rb_raise(rb_eTypeError,
1168
- "%s#to_numeric must return a plain CArray, not a Face (got %s)",
1169
- rb_obj_classname(self), rb_obj_classname(num));
1509
+ "%"PRIsVALUE"#to_numeric must return a plain CArray, not a Face (got %"PRIsVALUE")",
1510
+ rb_obj_class(self), rb_obj_class(num));
1170
1511
  }
1171
1512
 
1172
1513
  if ( cn->elements != ca->elements ) {
1173
1514
  rb_raise(rb_eArgError,
1174
- "%s#to_numeric returned %lld elements, expected %lld",
1175
- rb_obj_classname(self),
1515
+ "%"PRIsVALUE"#to_numeric returned %lld elements, expected %lld",
1516
+ rb_obj_class(self),
1176
1517
  (long long) cn->elements, (long long) ca->elements);
1177
1518
  }
1178
1519
 
@@ -1180,14 +1521,39 @@ rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1180
1521
  }
1181
1522
 
1182
1523
  static VALUE
1183
- rb_ca_object_to_data_class (VALUE self, VALUE rtype, ca_size_t bytes)
1524
+ rb_ca_object_to_data_class (volatile VALUE self, VALUE rtype, ca_size_t bytes)
1184
1525
  {
1185
1526
  volatile VALUE obj, rval;
1186
1527
  CArray *ca;
1187
- int i;
1528
+ ca_size_t i;
1188
1529
  ID id_encode = rb_intern("encode");
1189
1530
 
1190
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1531
+ GetCArray(self, ca);
1532
+
1533
+ /* Each cell is an instance of the class, or a String of its bytes. The
1534
+ check runs before the result is made, so a refusal leaves nothing
1535
+ half-written. A masked cell stays masked. */
1536
+ for (i=0; i<ca->elements; i++) {
1537
+ rval = rb_ca_fetch_addr(self, i);
1538
+ if ( rval == CA_UNDEF ) {
1539
+ continue;
1540
+ }
1541
+ if ( TYPE(rval) == T_STRING ) {
1542
+ if ( RSTRING_LEN(rval) != bytes ) {
1543
+ rb_raise(rb_eTypeError,
1544
+ "%s: cell %lld is a String of %ld bytes, not the %lld bytes "
1545
+ "of one %"PRIsVALUE,
1546
+ ca_calling_method_name(), (long long) i, RSTRING_LEN(rval),
1547
+ (long long) bytes, rtype);
1548
+ }
1549
+ }
1550
+ else if ( ! rb_obj_is_kind_of(rval, rtype) ) {
1551
+ rb_raise(rb_eTypeError,
1552
+ "%s: cell %lld is %"PRIsVALUE", not an instance of %"PRIsVALUE
1553
+ " (mask a cell to leave it out)",
1554
+ ca_calling_method_name(), (long long) i, rb_inspect(rval), rtype);
1555
+ }
1556
+ }
1191
1557
 
1192
1558
  if ( ca_is_scalar(ca) ) {
1193
1559
  obj = rb_cscalar_new(CA_FIXLEN, bytes, ca->mask);
@@ -1198,6 +1564,9 @@ rb_ca_object_to_data_class (VALUE self, VALUE rtype, ca_size_t bytes)
1198
1564
 
1199
1565
  for (i=0; i<ca->elements; i++) {
1200
1566
  rval = rb_ca_fetch_addr(self, i);
1567
+ if ( rval == CA_UNDEF ) {
1568
+ continue;
1569
+ }
1201
1570
  if ( TYPE(rval) == T_STRING ) {
1202
1571
  rb_ca_store_addr(obj, i, rval);
1203
1572
  }
@@ -1276,11 +1645,11 @@ ca_to_type_cast (VALUE arg)
1276
1645
  || ca->data_type == CA_CMPLX64 || ca->data_type == CA_CMPLX128 );
1277
1646
 
1278
1647
  if ( ( ca->data_type == CA_OBJECT
1279
- && ( to_integer
1648
+ && ( to_integer || cb->data_type == CA_BOOLEAN
1280
1649
  || cb->data_type == CA_FLOAT32 || cb->data_type == CA_FLOAT64
1281
1650
  || cb->data_type == CA_CMPLX64 || cb->data_type == CA_CMPLX128 ) )
1282
1651
  || ( from_float && to_integer ) ) {
1283
- /* object -> int/float/complex: give the cast a mask buffer so an unparseable
1652
+ /* object -> boolean/int/float/complex: give the cast a mask buffer so an unparseable
1284
1653
  cell becomes UNDEF (see ext/carray_cast_func.rb). Cast into a
1285
1654
  scratch mask seeded from the source mask, then attach it to the
1286
1655
  output only if some cell ended up masked -- an all-valid cast keeps
@@ -1289,6 +1658,7 @@ ca_to_type_cast (VALUE arg)
1289
1658
  ca_size_t i;
1290
1659
  boolean8_t any = 0;
1291
1660
  boolean8_t *scratch;
1661
+ /* buffer: kept in the context, so ca_to_type_release frees it when the cast raises */
1292
1662
  c->scratch = scratch = ALLOC_N(boolean8_t, ne > 0 ? ne : 1);
1293
1663
  if ( cb->mask ) {
1294
1664
  memcpy(scratch, cb->mask->ptr, ne); /* copy of source mask */
@@ -1357,18 +1727,20 @@ rb_ca_to_type_internal (int argc, VALUE *argv, VALUE self)
1357
1727
  return obj;
1358
1728
  }
1359
1729
 
1730
+ /* recursion: re-enters once, with the target type resolved */
1360
1731
  static VALUE
1361
- rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1732
+ rb_ca_to_type_convert (int argc, VALUE *argv, volatile VALUE self)
1362
1733
  {
1363
- volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
1734
+ volatile VALUE obj;
1735
+ VALUE rtype = Qnil, ropt, rbytes = Qnil;
1364
1736
  CArray *ca, *cb;
1365
1737
  ca_to_type_ctx_t ctx;
1366
1738
  int8_t data_type;
1367
1739
  ca_size_t bytes;
1368
1740
 
1369
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1741
+ GetCArray(self, ca);
1370
1742
 
1371
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &ropt);
1743
+ rb_scan_args(argc, argv, "11", &rtype, &ropt);
1372
1744
  rb_scan_options(ropt, "bytes", &rbytes);
1373
1745
 
1374
1746
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
@@ -1378,7 +1750,9 @@ rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1378
1750
  }
1379
1751
 
1380
1752
  if ( rb_ca_is_object_type(self) && rb_obj_is_data_class(rtype) ) {
1381
- return rb_ca_object_to_data_class(self, rtype, bytes);
1753
+ obj = rb_ca_object_to_data_class(self, rtype, bytes);
1754
+ return rb_funcall(rb_const_get(rb_cObject, rb_intern("CARecord")),
1755
+ rb_intern("wrap"), 2, obj, rtype);
1382
1756
  }
1383
1757
 
1384
1758
  /* NonNumeric Face (CA_FIXLEN surface = the numeric gate) to anything but
@@ -1401,12 +1775,12 @@ rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1401
1775
  }
1402
1776
 
1403
1777
 
1404
- TypedData_Get_Struct(obj, CArray, &carray_data_type, cb);
1778
+ GetCArray(obj, cb);
1405
1779
 
1406
1780
  ctx.ca = ca;
1407
1781
  ctx.cb = cb;
1408
1782
  ctx.scratch = NULL;
1409
- ca_attach(ca); /* window: released by ca_to_type_release under rb_ensure */
1783
+ ca_attach(ca); /* attach: released by ca_to_type_release under rb_ensure */
1410
1784
  rb_ensure(ca_to_type_cast, (VALUE) &ctx, ca_to_type_release, (VALUE) &ctx);
1411
1785
 
1412
1786
  /* When rtype is a data_class (e.g. CAStruct subclass), wrap the
@@ -1441,7 +1815,7 @@ rb_ca_to_fixlen (int argc, VALUE *argv, VALUE self)
1441
1815
  {
1442
1816
  volatile VALUE ropt = rb_pop_options(&argc, &argv);
1443
1817
  VALUE list[2];
1444
- list[0] = INT2NUM(CA_FIXLEN);
1818
+ list[0] = ID2SYM(ca_data_type_sym[CA_FIXLEN]);
1445
1819
  list[1] = ropt;
1446
1820
  return rb_ca_to_type_internal(2, list, self);
1447
1821
  }
@@ -1534,9 +1908,6 @@ VALUE rb_ca_to_VALUE (VALUE self)
1534
1908
 
1535
1909
  /* CArray#as_type */
1536
1910
 
1537
- /* Forward declaration: CAMonOp builder (ext/ca_obj_monop.c). */
1538
- extern VALUE rb_ca_monop_build (VALUE cary, uint16_t op_id);
1539
-
1540
1911
  /* Forward declaration: clip kernel (ext/carray_kernels.c, generated). */
1541
1912
  extern VALUE rb_ca_clip (VALUE self, VALUE lo, VALUE hi);
1542
1913
 
@@ -1565,11 +1936,11 @@ enum {
1565
1936
  or the Face's own #to_numeric for a numeric target. as_type and
1566
1937
  wrap_writable have no honest answer and say so. */
1567
1938
  static VALUE
1568
- ca_type_adapt_view (VALUE obj, VALUE rtype, int8_t data_type, int mode)
1939
+ ca_type_adapt_view (volatile VALUE obj, VALUE rtype, int8_t data_type, int mode)
1569
1940
  {
1570
1941
  CArray *ca;
1571
1942
 
1572
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
1943
+ GetCArray(obj, ca);
1573
1944
 
1574
1945
  /* Same split the eager cast makes: :object is the Face's surface for every
1575
1946
  Face, and a NonNumeric Face (CA_FIXLEN surface) has no reading of its
@@ -1584,15 +1955,15 @@ ca_type_adapt_view (VALUE obj, VALUE rtype, int8_t data_type, int mode)
1584
1955
  return rb_ca_to_type_internal(1, args, obj);
1585
1956
  case CA_ADAPT_WRITABLE:
1586
1957
  rb_raise(rb_eTypeError,
1587
- "%s has no writable view in another data_type: the writes "
1958
+ "%"PRIsVALUE" has no writable view in another data_type: the writes "
1588
1959
  "would land on the storage its surface hides -- wrap "
1589
1960
  "#parent to reach that storage",
1590
- rb_obj_classname(obj));
1961
+ rb_obj_class(obj));
1591
1962
  default:
1592
1963
  rb_raise(rb_eTypeError,
1593
- "%s has no view of its values in another data_type: "
1964
+ "%"PRIsVALUE" has no view of its values in another data_type: "
1594
1965
  "#to_type gives the values, #parent the raw storage",
1595
- rb_obj_classname(obj));
1966
+ rb_obj_class(obj));
1596
1967
  }
1597
1968
  }
1598
1969
 
@@ -1608,11 +1979,12 @@ ca_type_adapt_view (VALUE obj, VALUE rtype, int8_t data_type, int mode)
1608
1979
  static VALUE
1609
1980
  rb_ca_as_type_internal (int argc, VALUE *argv, VALUE self)
1610
1981
  {
1611
- volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
1982
+ volatile VALUE obj;
1983
+ VALUE rtype = Qnil, ropt, rbytes = Qnil;
1612
1984
  int8_t data_type;
1613
1985
  ca_size_t bytes;
1614
1986
 
1615
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &ropt);
1987
+ rb_scan_args(argc, argv, "11", &rtype, &ropt);
1616
1988
  rb_scan_options(ropt, "bytes", &rbytes);
1617
1989
 
1618
1990
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
@@ -1659,10 +2031,11 @@ rb_ca_as_type (VALUE self, VALUE rtype, VALUE rbytes)
1659
2031
  VALUE
1660
2032
  rb_ca_as_fixlen (int argc, VALUE *argv, VALUE self)
1661
2033
  {
1662
- volatile VALUE rtype, ropt = rb_pop_options(&argc, &argv);
2034
+ VALUE rtype;
2035
+ volatile VALUE ropt = rb_pop_options(&argc, &argv);
1663
2036
  VALUE list[2];
1664
- rb_scan_args(argc, argv, "01", (VALUE *) &rtype);
1665
- list[0] = ( NIL_P(rtype) ) ? INT2NUM(CA_FIXLEN) : rtype;
2037
+ rb_scan_args(argc, argv, "01", &rtype);
2038
+ list[0] = ( NIL_P(rtype) ) ? ID2SYM(ca_data_type_sym[CA_FIXLEN]) : rtype;
1666
2039
  list[1] = ropt;
1667
2040
  return rb_ca_as_type_internal(2, list, self);
1668
2041
  }
@@ -1733,12 +2106,6 @@ VALUE rb_ca_as_float64 (VALUE self)
1733
2106
  rb_ca_as_type_method_body(CA_FLOAT64);
1734
2107
  }
1735
2108
 
1736
- /* CArray#as_float128 -- short-hand of as_type(:float128). */
1737
- VALUE rb_ca_as_float128 (VALUE self)
1738
- {
1739
- rb_ca_as_type_method_body(CA_FLOAT128);
1740
- }
1741
-
1742
2109
  /* CArray#as_cmplx64 -- short-hand of as_type(:cmplx64). */
1743
2110
  VALUE rb_ca_as_cmplx64 (VALUE self)
1744
2111
  {
@@ -1751,12 +2118,6 @@ VALUE rb_ca_as_cmplx128 (VALUE self)
1751
2118
  rb_ca_as_type_method_body(CA_CMPLX128);
1752
2119
  }
1753
2120
 
1754
- /* CArray#as_cmplx256 -- short-hand of as_type(:cmplx256). */
1755
- VALUE rb_ca_as_cmplx256 (VALUE self)
1756
- {
1757
- rb_ca_as_type_method_body(CA_CMPLX256);
1758
- }
1759
-
1760
2121
  /* CArray#as_object -- short-hand of as_type(:object). */
1761
2122
  VALUE rb_ca_as_VALUE (VALUE self)
1762
2123
  {
@@ -1766,12 +2127,12 @@ VALUE rb_ca_as_VALUE (VALUE self)
1766
2127
  /* ------------------------------------------------------------------------*/
1767
2128
 
1768
2129
  VALUE
1769
- rb_ca_cast_block (ca_size_t n, VALUE ra1, void *ptr1,
1770
- VALUE ra2, void *ptr2)
2130
+ rb_ca_cast_block (ca_size_t n, volatile VALUE ra1, void *ptr1,
2131
+ volatile VALUE ra2, void *ptr2)
1771
2132
  {
1772
2133
  CArray *ca1, *ca2;
1773
- TypedData_Get_Struct(ra1, CArray, &carray_data_type, ca1);
1774
- TypedData_Get_Struct(ra2, CArray, &carray_data_type, ca2);
2134
+ GetCArray(ra1, ca1);
2135
+ GetCArray(ra2, ca2);
1775
2136
  if ( n < 0 ) {
1776
2137
  rb_raise(rb_eRuntimeError, "[BUG] in rb_ca_cast_block: negative count");
1777
2138
  }
@@ -1780,11 +2141,11 @@ rb_ca_cast_block (ca_size_t n, VALUE ra1, void *ptr1,
1780
2141
  }
1781
2142
 
1782
2143
  VALUE
1783
- rb_ca_ptr2ptr (VALUE ra1, void *ptr1, VALUE ra2, void *ptr2)
2144
+ rb_ca_ptr2ptr (volatile VALUE ra1, void *ptr1, volatile VALUE ra2, void *ptr2)
1784
2145
  {
1785
2146
  CArray *ca1, *ca2;
1786
- TypedData_Get_Struct(ra1, CArray, &carray_data_type, ca1);
1787
- TypedData_Get_Struct(ra2, CArray, &carray_data_type, ca2);
2147
+ GetCArray(ra1, ca1);
2148
+ GetCArray(ra2, ca2);
1788
2149
  ca_cast_func_table[ca1->data_type][ca2->data_type](1, ca1, ptr1, ca2, ptr2, NULL);
1789
2150
  return Qnil;
1790
2151
  }
@@ -1794,12 +2155,12 @@ rb_ca_ptr2ptr (VALUE ra1, void *ptr1, VALUE ra2, void *ptr2)
1794
2155
  steps of ca[i]: a boolean is 0 or 1 and a Face cell is its storage
1795
2156
  value. The element readers call this, check the mask, then decode. */
1796
2157
  VALUE
1797
- rb_ca_ptr2obj_storage (VALUE self, void *ptr)
2158
+ rb_ca_ptr2obj_storage (volatile VALUE self, void *ptr)
1798
2159
  {
1799
2160
  volatile VALUE obj;
1800
2161
  static CArray dummy;
1801
2162
  CArray *ca;
1802
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2163
+ GetCArray(self, ca);
1803
2164
  dummy.data_type = CA_OBJECT;
1804
2165
  ca_cast_func_table[ca->data_type][CA_OBJECT](1, ca, ptr, &dummy, (void*)&obj, NULL);
1805
2166
  if ( ca_is_fixlen_type(ca) ) {
@@ -1811,11 +2172,11 @@ rb_ca_ptr2obj_storage (VALUE self, void *ptr)
1811
2172
  }
1812
2173
 
1813
2174
  VALUE
1814
- rb_ca_ptr2obj (VALUE self, void *ptr)
2175
+ rb_ca_ptr2obj (volatile VALUE self, void *ptr)
1815
2176
  {
1816
2177
  CArray *ca;
1817
2178
  VALUE obj = rb_ca_ptr2obj_storage(self, ptr);
1818
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2179
+ GetCArray(self, ca);
1819
2180
  CA_FACE_STORAGE_TO_ELEMENT_IF_FACE(obj, self, ca);
1820
2181
  if ( ca->data_type == CA_BOOLEAN &&
1821
2182
  ( obj == INT2FIX(0) || obj == INT2FIX(1) ) ) {
@@ -1847,7 +2208,7 @@ ca_face_element_to_storage (VALUE self, CArray *ca, VALUE obj)
1847
2208
  if ( id_element_to_storage == 0 ) {
1848
2209
  id_element_to_storage = rb_intern("element_to_storage");
1849
2210
  }
1850
- if ( rb_respond_to(self, id_element_to_storage) ) {
2211
+ if ( rb_obj_respond_to(self, id_element_to_storage, Qtrue) ) {
1851
2212
  return rb_funcall(self, id_element_to_storage, 1, obj);
1852
2213
  }
1853
2214
  }
@@ -1855,11 +2216,11 @@ ca_face_element_to_storage (VALUE self, CArray *ca, VALUE obj)
1855
2216
  }
1856
2217
 
1857
2218
  VALUE
1858
- rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
2219
+ rb_ca_obj2ptr (volatile VALUE self, VALUE obj, void *ptr)
1859
2220
  {
1860
2221
  static CArray dummy;
1861
2222
  CArray *ca;
1862
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2223
+ GetCArray(self, ca);
1863
2224
  if ( obj == CA_UNDEF ) {
1864
2225
  memset(ptr, 0, ca->bytes);
1865
2226
  }
@@ -1874,6 +2235,9 @@ rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
1874
2235
  obj = ca_face_element_to_storage(self, ca, obj);
1875
2236
  convert_type = ca_storage_type_of(ca);
1876
2237
  }
2238
+ else {
2239
+ ca_refuse_non_number_value(convert_type, obj);
2240
+ }
1877
2241
  if ( convert_type == CA_FIXLEN ) {
1878
2242
  obj = rb_ca_data_class_encode(self, obj);
1879
2243
  }
@@ -1883,6 +2247,17 @@ rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
1883
2247
  return Qnil;
1884
2248
  }
1885
2249
 
2250
+ /* nil given where the data of an array is read (CA_INT32(nil),
2251
+ wrap_readonly(nil)): nil is no data. An empty array is CA_INT32([]). */
2252
+ NORETURN(static void ca_refuse_nil_data (void));
2253
+ static void
2254
+ ca_refuse_nil_data (void)
2255
+ {
2256
+ rb_raise(rb_eTypeError,
2257
+ "%s: nil is not data for an array; pass [] for an empty array",
2258
+ ca_calling_method_name());
2259
+ }
2260
+
1886
2261
  VALUE
1887
2262
  rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1888
2263
  {
@@ -1891,7 +2266,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1891
2266
  int8_t data_type;
1892
2267
 
1893
2268
  if ( rb_obj_is_carray(obj) ) { /* obj == carray */
1894
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2269
+ GetCArray(obj, ca);
1895
2270
  if ( ca_is_readonly(ca) ) {
1896
2271
  ca_refuse_read_only();
1897
2272
  }
@@ -1906,13 +2281,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1906
2281
  }
1907
2282
  }
1908
2283
  else if ( NIL_P(obj) ) { /* obj == nil */
1909
- if ( NIL_P(rtype) ) {
1910
- data_type = CA_OBJECT;
1911
- }
1912
- else {
1913
- data_type = rb_ca_guess_type(rtype);
1914
- }
1915
- obj = rb_cscalar_new(data_type, 0, NULL);
2284
+ ca_refuse_nil_data();
1916
2285
  }
1917
2286
  else if ( rb_respond_to(obj, rb_intern("to_ca")) ) { /* respond_to obj.to_ca */
1918
2287
  /* `writable: true` is the caller's half of the bargain: it says the
@@ -1920,16 +2289,15 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1920
2289
  only produce a copy raises rather than swallowing them. An object
1921
2290
  whose to_ca predates the keyword raises ArgumentError here, which is
1922
2291
  the honest report that it does not implement writable intake. */
1923
- volatile VALUE kw = rb_hash_new();
2292
+ VALUE kw = rb_hash_new();
1924
2293
  rb_hash_aset(kw, ID2SYM(rb_intern("writable")), Qtrue);
1925
- obj = rb_funcallv_kw(obj, rb_intern("to_ca"), 1, (VALUE *) &kw,
1926
- RB_PASS_KEYWORDS);
2294
+ obj = rb_funcallv_kw(obj, rb_intern("to_ca"), 1, &kw, RB_PASS_KEYWORDS);
1927
2295
  if ( ! rb_obj_is_carray(obj) ) {
1928
2296
  volatile VALUE inspect = rb_inspect(CLASS_OF(arg));
1929
2297
  rb_raise(rb_eTypeError,
1930
2298
  "%s#to_ca did not return a CArray", StringValuePtr(inspect));
1931
2299
  }
1932
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2300
+ GetCArray(obj, ca);
1933
2301
  if ( ca_is_readonly(ca) ) {
1934
2302
  ca_refuse_read_only();
1935
2303
  }
@@ -1947,7 +2315,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1947
2315
  else if ( rb_memory_view_available_p(obj) ) { /* MemoryView producer */
1948
2316
  obj = ca_mv_wrap(obj, Qnil, rb_cCAWrap, -1,
1949
2317
  NIL_P(rtype) ? -1 : rb_ca_guess_type(rtype), 1);
1950
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2318
+ GetCArray(obj, ca);
1951
2319
  if ( NIL_P(rtype) ) {
1952
2320
  data_type = ca->data_type;
1953
2321
  }
@@ -1976,8 +2344,12 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1976
2344
  static VALUE
1977
2345
  rb_ca_s_wrap_writable (int argc, VALUE *argv, VALUE klass)
1978
2346
  {
1979
- volatile VALUE obj, rtype;
1980
- rb_scan_args(argc, argv, "11", (VALUE *) &obj, (VALUE *) &rtype);
2347
+ VALUE obj, rtype;
2348
+ rb_scan_args(argc, argv, "11", &obj, &rtype);
2349
+ ca_refuse_type_number(rtype);
2350
+ if ( RTEST(rb_obj_is_data_class(rtype)) ) {
2351
+ return ca_record_view_of(obj, rtype, "wrap_writable");
2352
+ }
1981
2353
  return rb_ca_wrap_writable(obj, rtype);
1982
2354
  }
1983
2355
 
@@ -1989,7 +2361,7 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
1989
2361
  int8_t data_type;
1990
2362
 
1991
2363
  if ( rb_obj_is_carray(obj) ) { /* carray */
1992
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2364
+ GetCArray(obj, ca);
1993
2365
  if ( NIL_P(rtype) ) {
1994
2366
  data_type = ca->data_type;
1995
2367
  }
@@ -2010,8 +2382,11 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2010
2382
  obj = rb_cscalar_new_with_value(data_type, 0, obj);
2011
2383
  }
2012
2384
  else if ( TYPE(obj) == T_ARRAY ) { /* array */
2385
+ if ( ! NIL_P(rtype) ) {
2386
+ ca_refuse_non_number_elements(rb_ca_guess_type(rtype), obj);
2387
+ }
2013
2388
  obj = rb_funcall(obj, rb_intern("to_ca"), 0);
2014
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2389
+ GetCArray(obj, ca);
2015
2390
  if ( NIL_P(rtype) ) {
2016
2391
  data_type = CA_OBJECT;
2017
2392
  }
@@ -2037,25 +2412,29 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2037
2412
  CArray *ca_tmp;
2038
2413
  volatile VALUE ref = obj;
2039
2414
  ca_size_t bytes = ca_sizeof[data_type];
2040
- CA_CHECK_BYTES_GIVEN(data_type, bytes);
2041
- ca_size_t dim = RSTRING_LEN(ref)/bytes;
2415
+ CA_CHECK_BYTES(data_type, bytes);
2416
+ ca_size_t dim;
2417
+ /* The String is the bytes of the cells; a length that is no whole
2418
+ number of cells would leave bytes unread. */
2419
+ if ( RSTRING_LEN(ref) % bytes != 0 ) {
2420
+ rb_raise(rb_eArgError,
2421
+ "%s: a String of %ld bytes is not a whole number of "
2422
+ "%"PRIsVALUE" cells (%ld bytes each)",
2423
+ ca_calling_method_name(), (long) RSTRING_LEN(ref),
2424
+ rb_id2str(ca_data_type_sym[data_type]), (long) bytes);
2425
+ }
2426
+ dim = RSTRING_LEN(ref)/bytes;
2042
2427
  obj = rb_carray_new(data_type, 1, &dim, 0, NULL);
2043
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca_tmp);
2428
+ GetCArray(obj, ca_tmp);
2044
2429
  memcpy(ca_tmp->ptr, RSTRING_PTR(ref), dim * ca_sizeof[data_type]);
2045
2430
  }
2046
2431
  }
2047
2432
  else if ( NIL_P(obj) ) { /* nil */
2048
- if ( NIL_P(rtype) ) {
2049
- data_type = CA_OBJECT;
2050
- }
2051
- else {
2052
- data_type = rb_ca_guess_type(rtype);
2053
- }
2054
- obj = rb_cscalar_new(data_type, 0, NULL);
2433
+ ca_refuse_nil_data();
2055
2434
  }
2056
2435
  else if ( rb_respond_to(obj, rb_intern("to_ca")) ) {
2057
2436
  obj = rb_funcall(obj, rb_intern("to_ca"), 0);
2058
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2437
+ GetCArray(obj, ca);
2059
2438
  if ( NIL_P(rtype) ) {
2060
2439
  data_type = ca->data_type;
2061
2440
  }
@@ -2070,7 +2449,7 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2070
2449
  else if ( rb_memory_view_available_p(obj) ) { /* MemoryView producer */
2071
2450
  obj = ca_mv_wrap(obj, Qnil, rb_cCAWrap, -1,
2072
2451
  NIL_P(rtype) ? -1 : rb_ca_guess_type(rtype), 0);
2073
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2452
+ GetCArray(obj, ca);
2074
2453
  if ( NIL_P(rtype) ) {
2075
2454
  data_type = ca->data_type;
2076
2455
  }
@@ -2102,8 +2481,12 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2102
2481
  static VALUE
2103
2482
  rb_ca_s_wrap_readonly (int argc, VALUE *argv, VALUE klass)
2104
2483
  {
2105
- volatile VALUE obj, rtype;
2106
- rb_scan_args(argc, argv, "11", (VALUE *) &obj, (VALUE *) &rtype);
2484
+ VALUE obj, rtype;
2485
+ rb_scan_args(argc, argv, "11", &obj, &rtype);
2486
+ ca_refuse_type_number(rtype);
2487
+ if ( RTEST(rb_obj_is_data_class(rtype)) ) {
2488
+ return ca_record_view_of(obj, rtype, "wrap_readonly");
2489
+ }
2107
2490
  return rb_ca_wrap_readonly(obj, rtype);
2108
2491
  }
2109
2492
 
@@ -2158,7 +2541,7 @@ rb_ca_cast (volatile VALUE self)
2158
2541
  string semantics those bodies implement (min-width memcmp with a length
2159
2542
  tiebreak); this is only about a scalar standing in for a cell. */
2160
2543
  static int
2161
- ca_fixlen_scalar_operand (volatile VALUE *scalar, VALUE reference)
2544
+ ca_fixlen_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2162
2545
  {
2163
2546
  CArray *cr;
2164
2547
  /* A String is the only operand that means a cell value here. A Regexp
@@ -2167,7 +2550,7 @@ ca_fixlen_scalar_operand (volatile VALUE *scalar, VALUE reference)
2167
2550
  if ( ! RB_TYPE_P(*scalar, T_STRING) || ! rb_obj_is_carray(reference) ) {
2168
2551
  return 0;
2169
2552
  }
2170
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2553
+ GetCArray(reference, cr);
2171
2554
  if ( cr->data_type != CA_FIXLEN ) {
2172
2555
  return 0;
2173
2556
  }
@@ -2214,19 +2597,29 @@ void
2214
2597
  ca_check_integer_fits (VALUE v, int8_t dt)
2215
2598
  {
2216
2599
  if ( ca_integer_range_side(v, dt) != 0 ) {
2217
- rb_raise(rb_eRangeError, "%"PRIsVALUE" is out of range for %s",
2218
- rb_inspect(v), ca_type_name[dt]);
2600
+ rb_raise(rb_eRangeError, "%s: %"PRIsVALUE" is out of range for %s",
2601
+ ca_calling_method_name(), rb_inspect(v), ca_type_name[dt]);
2219
2602
  }
2220
2603
  }
2221
2604
 
2222
2605
  /* The store reader for an integer cell: the value as an Integer, refused
2223
- when it does not fit the cell's type, as an operator refuses it. A
2224
- store raises where an array cast (to_type) keeps the C conversion. A
2225
- non-Integer goes through Integer() (1.5 is 1; nil and NaN raise). */
2606
+ when it does not fit the cell's type. A store writes a value of the
2607
+ cell's type, so a Ruby value it can not hold -- an Integer or a Float
2608
+ alike -- is refused, though an operation such as + keeps its answer
2609
+ modulo 2 ** bits. An array cast (to_type, or a store from an integer
2610
+ array) keeps the C conversion. A non-Integer goes through Integer()
2611
+ (1.5 is 1; nil and NaN raise). */
2226
2612
  VALUE
2227
- ca_obj2integer_fitting (VALUE v, int8_t data_type)
2613
+ rb_ca_obj2integer_fitting (VALUE v, int8_t data_type)
2228
2614
  {
2229
- VALUE integer = RB_INTEGER_TYPE_P(v) ? v : rb_Integer(v);
2615
+ VALUE integer;
2616
+ if ( RB_TYPE_P(v, T_ARRAY) ) {
2617
+ ca_refuse_array_value("an integer");
2618
+ }
2619
+ if ( NIL_P(v) ) {
2620
+ ca_refuse_nil_value("an integer");
2621
+ }
2622
+ integer = RB_INTEGER_TYPE_P(v) ? v : ca_number_of_value(v, 1, "an integer");
2230
2623
  if ( FIXNUM_P(integer) ) {
2231
2624
  long x = FIX2LONG(integer);
2232
2625
  int fits;
@@ -2249,18 +2642,235 @@ ca_obj2integer_fitting (VALUE v, int8_t data_type)
2249
2642
  return integer;
2250
2643
  }
2251
2644
 
2645
+ VALUE
2646
+ ca_obj2integer_store (CArray *dst, VALUE v, int8_t data_type)
2647
+ {
2648
+ CArray *field = dst ? ca_bitfield_beneath(dst) : NULL;
2649
+ if ( field ) {
2650
+ return ca_bitfield_wrap_integer(field, v);
2651
+ }
2652
+ return rb_ca_obj2integer_fitting(v, data_type);
2653
+ }
2654
+
2655
+ /* Whether Integer v is in the range of integer data type dt; a Fixnum
2656
+ is answered without calling Ruby. */
2657
+ static int
2658
+ ca_integer_fits_type (VALUE v, int8_t dt)
2659
+ {
2660
+ if ( FIXNUM_P(v) ) {
2661
+ long x = FIX2LONG(v);
2662
+ switch ( dt ) {
2663
+ case CA_INT8: return ( x >= INT8_MIN && x <= INT8_MAX );
2664
+ case CA_UINT8: return ( x >= 0 && x <= UINT8_MAX );
2665
+ case CA_INT16: return ( x >= INT16_MIN && x <= INT16_MAX );
2666
+ case CA_UINT16: return ( x >= 0 && x <= UINT16_MAX );
2667
+ case CA_INT32: return ( x >= INT32_MIN && x <= INT32_MAX );
2668
+ case CA_UINT32: return ( x >= 0 && (unsigned long) x <= UINT32_MAX );
2669
+ case CA_INT64: return 1;
2670
+ case CA_UINT64: return ( x >= 0 );
2671
+ default: return 1;
2672
+ }
2673
+ }
2674
+ return ca_integer_range_side(v, dt) == 0;
2675
+ }
2676
+
2677
+ /* Whether every value of integer type `inner` is a value of `outer`. */
2678
+ static int
2679
+ ca_integer_type_within (int8_t inner, int8_t outer)
2680
+ {
2681
+ int inner_unsigned = ( inner == CA_UINT8 || inner == CA_UINT16 ||
2682
+ inner == CA_UINT32 || inner == CA_UINT64 );
2683
+ int outer_unsigned = ( outer == CA_UINT8 || outer == CA_UINT16 ||
2684
+ outer == CA_UINT32 || outer == CA_UINT64 );
2685
+ if ( inner_unsigned == outer_unsigned ) {
2686
+ return ca_sizeof[inner] <= ca_sizeof[outer];
2687
+ }
2688
+ return inner_unsigned && ca_sizeof[inner] < ca_sizeof[outer];
2689
+ }
2690
+
2691
+ /* An Integer taken into integer data type `dt` as C converts a wider
2692
+ integer to it: modulo 2 ** bits, read as signed for a signed type. */
2693
+ VALUE
2694
+ ca_integer_wrap_to_type (VALUE v, int8_t dt)
2695
+ {
2696
+ int sign_extend = ( dt == CA_INT8 || dt == CA_INT16 ||
2697
+ dt == CA_INT32 || dt == CA_INT64 );
2698
+ if ( ca_integer_fits_type(v, dt) ) {
2699
+ return v;
2700
+ }
2701
+ return ca_integer_wrap_bits(v, (int) ca_sizeof[dt] * 8, sign_extend);
2702
+ }
2703
+
2704
+ /* recursion: one level per dimension of an array's to_a */
2705
+ static VALUE
2706
+ ca_integer_tree_wrap (VALUE v, int8_t dt)
2707
+ {
2708
+ volatile VALUE out;
2709
+ long i, n;
2710
+ if ( RB_INTEGER_TYPE_P(v) ) {
2711
+ return ca_integer_wrap_to_type(v, dt);
2712
+ }
2713
+ if ( ! RB_TYPE_P(v, T_ARRAY) ) {
2714
+ return v;
2715
+ }
2716
+ n = RARRAY_LEN(v);
2717
+ out = rb_ary_new_capa(n);
2718
+ for (i=0; i<n; i++) {
2719
+ rb_ary_push(out, ca_integer_tree_wrap(RARRAY_AREF(v, i), dt));
2720
+ }
2721
+ return out;
2722
+ }
2723
+
2724
+ /* The bound of integer data type dt on the side of v (side < 0: the
2725
+ least value, side > 0: the greatest). */
2726
+ static VALUE
2727
+ ca_integer_type_bound (int8_t dt, int side)
2728
+ {
2729
+ switch ( dt ) {
2730
+ case CA_INT8: return INT2FIX(side < 0 ? INT8_MIN : INT8_MAX);
2731
+ case CA_UINT8: return INT2FIX(side < 0 ? 0 : UINT8_MAX);
2732
+ case CA_INT16: return INT2FIX(side < 0 ? INT16_MIN : INT16_MAX);
2733
+ case CA_UINT16: return INT2FIX(side < 0 ? 0 : UINT16_MAX);
2734
+ case CA_INT32: return side < 0 ? LL2NUM(INT32_MIN) : LL2NUM(INT32_MAX);
2735
+ case CA_UINT32: return side < 0 ? INT2FIX(0) : ULL2NUM(UINT32_MAX);
2736
+ case CA_INT64: return side < 0 ? LL2NUM(INT64_MIN) : LL2NUM(INT64_MAX);
2737
+ default: return side < 0 ? INT2FIX(0) : ULL2NUM(UINT64_MAX);
2738
+ }
2739
+ }
2740
+
2741
+ /* An operand of min (how = CA_SCALAR_FOR_MIN) or max beside an array of
2742
+ integer type dt. An Integer past the side where it can never be picked
2743
+ -- above the type for min, below it for max -- gives the same answer as
2744
+ the type's bound on that side, which the type holds, so it is brought to
2745
+ that bound. One past the other side would be picked in every cell, and
2746
+ the type can not hold that answer, so it is refused. */
2747
+ /* recursion: one level per dimension of an array's to_a */
2748
+ static VALUE
2749
+ ca_integer_tree_saturate (VALUE v, int8_t dt, int how)
2750
+ {
2751
+ volatile VALUE out;
2752
+ long i, n;
2753
+ if ( RB_INTEGER_TYPE_P(v) ) {
2754
+ int side;
2755
+ if ( ca_integer_fits_type(v, dt) ) {
2756
+ return v;
2757
+ }
2758
+ side = ca_integer_range_side(v, dt);
2759
+ if ( ( how == CA_SCALAR_FOR_MIN && side > 0 ) ||
2760
+ ( how == CA_SCALAR_FOR_MAX && side < 0 ) ) {
2761
+ return ca_integer_type_bound(dt, side);
2762
+ }
2763
+ ca_check_integer_fits(v, dt);
2764
+ return v;
2765
+ }
2766
+ if ( ! RB_TYPE_P(v, T_ARRAY) ) {
2767
+ return v;
2768
+ }
2769
+ n = RARRAY_LEN(v);
2770
+ out = rb_ary_new_capa(n);
2771
+ for (i=0; i<n; i++) {
2772
+ rb_ary_push(out, ca_integer_tree_saturate(RARRAY_AREF(v, i), dt, how));
2773
+ }
2774
+ return out;
2775
+ }
2776
+
2777
+ void
2778
+ ca_scalar_operand_for_op (volatile VALUE *scalar, volatile VALUE array, int how)
2779
+ {
2780
+ CArray *ca, *cs;
2781
+ VALUE value;
2782
+ int8_t data_type;
2783
+
2784
+ if ( ! rb_obj_is_carray(array) ) {
2785
+ return;
2786
+ }
2787
+ GetCArray(array, ca);
2788
+ if ( ca_is_face(ca) ) {
2789
+ return;
2790
+ }
2791
+ /* A boolean array computes as its 0 / 1 int64 numbers (rb_ca_call_binop
2792
+ promotes it), so a scalar beside it is taken as beside an int64 array.
2793
+ & | ^ read the scalar as true / false before this. */
2794
+ if ( ca->data_type == CA_BOOLEAN && how != CA_SCALAR_FOR_COMPARE ) {
2795
+ data_type = CA_INT64;
2796
+ }
2797
+ else if ( ca_is_integer_type(ca) ) {
2798
+ data_type = ca->data_type;
2799
+ }
2800
+ else {
2801
+ return;
2802
+ }
2803
+
2804
+ /* A CScalar beside an array is a scalar operand, as a Ruby value is:
2805
+ it is taken at its value, not converted to the array's type first.
2806
+ One whose value the array's type holds is passed on as it is. */
2807
+ if ( rb_obj_is_carray(*scalar) ) {
2808
+ if ( ! RTEST(rb_obj_is_cscalar(*scalar)) || RTEST(rb_obj_is_cscalar(array)) ) {
2809
+ return;
2810
+ }
2811
+ GetCArray(*scalar, cs);
2812
+ if ( ! ca_is_integer_type(cs) || ca_is_face(cs) || ca_is_any_masked(cs) ||
2813
+ ca_integer_type_within(cs->data_type, data_type) ) {
2814
+ return;
2815
+ }
2816
+ value = rb_ca_fetch_addr(*scalar, 0);
2817
+ if ( ca_integer_fits_type(value, data_type) ) {
2818
+ return;
2819
+ }
2820
+ *scalar = value;
2821
+ }
2822
+
2823
+ if ( how == CA_SCALAR_FOR_COMPARE ) {
2824
+ return;
2825
+ }
2826
+ if ( how == CA_SCALAR_FOR_RING ) {
2827
+ *scalar = ca_integer_tree_wrap(*scalar, data_type);
2828
+ }
2829
+ else if ( how == CA_SCALAR_FOR_MIN || how == CA_SCALAR_FOR_MAX ) {
2830
+ *scalar = ca_integer_tree_saturate(*scalar, data_type, how);
2831
+ }
2832
+ else if ( RB_INTEGER_TYPE_P(*scalar) && ! ca_integer_fits_type(*scalar, data_type) ) {
2833
+ ca_check_integer_fits(*scalar, data_type);
2834
+ }
2835
+ }
2836
+
2837
+ /* Prepares the scalar operands of an operation beside an integer array.
2838
+ An integer CScalar is read as its Integer, so that it is taken as a
2839
+ Ruby Integer is. Then, by `how`:
2840
+ CA_SCALAR_FOR_RING + - * & | ^ (and fma / fms): the answer modulo
2841
+ 2 ** bits does not depend on whether the
2842
+ operand was cut first, so an Integer the type
2843
+ can not hold is cut to it, as the cells of the
2844
+ answer are;
2845
+ CA_SCALAR_FOR_MIN an operand of min (pmin, minimum, the upper
2846
+ CA_SCALAR_FOR_MAX bound of clip, ...) or of max: one the type can
2847
+ not hold is brought to the type's bound when it
2848
+ can never be picked, which gives the same
2849
+ answer, and refused when it would be picked in
2850
+ every cell (ca_integer_tree_saturate);
2851
+ CA_SCALAR_FOR_ORDER any other operation (/ % divmod fmod): cut, the
2852
+ operand would give another answer, so one the
2853
+ type can not hold is refused;
2854
+ CA_SCALAR_FOR_COMPARE a comparison, which answers by value. */
2855
+ void
2856
+ ca_scalar_operands_for_op (volatile VALUE *self, volatile VALUE *other, int how)
2857
+ {
2858
+ ca_scalar_operand_for_op(other, *self, how);
2859
+ ca_scalar_operand_for_op(self, *other, how);
2860
+ }
2861
+
2252
2862
  /* A Ruby Integer operand against an integer array is taken in the array's
2253
2863
  own data type, refused when it does not fit (ca_check_integer_fits).
2254
2864
  Widening instead would make the result's type depend on a scalar's
2255
2865
  value. A Float, a Face and an object array keep the coercion below. */
2256
2866
  static int
2257
- ca_integer_scalar_operand (volatile VALUE *scalar, VALUE reference)
2867
+ ca_integer_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2258
2868
  {
2259
2869
  CArray *cr;
2260
2870
  if ( ! RB_INTEGER_TYPE_P(*scalar) || ! rb_obj_is_carray(reference) ) {
2261
2871
  return 0;
2262
2872
  }
2263
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2873
+ GetCArray(reference, cr);
2264
2874
  if ( ! ca_is_integer_type(cr) || ca_is_face(cr) ) {
2265
2875
  return 0;
2266
2876
  }
@@ -2269,67 +2879,34 @@ ca_integer_scalar_operand (volatile VALUE *scalar, VALUE reference)
2269
2879
  return 1;
2270
2880
  }
2271
2881
 
2272
- /* A Ruby Array operand against a numeric or boolean array is read as its
2273
- cells, as the array CArray.wrap_readonly makes of it (nested Arrays give
2274
- its axes). Its data type is the
2275
- one the cells would take one at a time as scalar operands of the same
2276
- operation (an Integer
2277
- against an integer array takes the array's type and has to fit it),
2278
- promoted together. Against an object array, a Face or a fixlen array an
2279
- Array stays one value. */
2280
- static int
2281
- ca_array_operand (volatile VALUE *ary, VALUE reference, int both)
2882
+ /* A Ruby Array is not an operand of a numeric or boolean array. It has no
2883
+ data type of its own, so no reading of it agrees with both the scalar
2884
+ rules and the promotion of typed arrays: the caller makes it a CArray of
2885
+ the type it means. The message names types to write rather than the
2886
+ receiver's type, since a CArray of the receiver's type would truncate a
2887
+ Float cell before the operation sees it. Against an object array, a Face
2888
+ or a fixlen array an Array stays one value. */
2889
+ static void
2890
+ ca_refuse_array_operand (VALUE ary, volatile VALUE reference)
2282
2891
  {
2283
2892
  CArray *cr;
2284
- volatile VALUE flat, probe_self, probe;
2285
- long i, n;
2286
- int8_t type = -1, seen_fix = -1, seen_big = -1, seen_flo = -1, seen_cpx = -1,
2287
- seen_bool = -1;
2893
+ const char *type, *article, *example;
2288
2894
 
2289
- if ( ! RB_TYPE_P(*ary, T_ARRAY) || ! rb_obj_is_carray(reference) ) {
2290
- return 0;
2895
+ if ( ! RB_TYPE_P(ary, T_ARRAY) || ! rb_obj_is_carray(reference) ) {
2896
+ return;
2291
2897
  }
2292
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2898
+ GetCArray(reference, cr);
2293
2899
  if ( ca_is_face(cr) || ca_is_object_type(cr) || ca_is_fixlen_type(cr) ) {
2294
- return 0;
2295
- }
2296
-
2297
- flat = rb_funcall(*ary, rb_intern("flatten"), 0);
2298
- n = RARRAY_LEN(flat);
2299
- for (i = 0; i < n; i++) {
2300
- VALUE e = RARRAY_AREF(flat, i);
2301
- int8_t *seen, t;
2302
- CArray *cp;
2303
- if ( FIXNUM_P(e) ) seen = &seen_fix;
2304
- else if ( RB_TYPE_P(e, T_BIGNUM) ) seen = &seen_big;
2305
- else if ( RB_FLOAT_TYPE_P(e) ) seen = &seen_flo;
2306
- else if ( RB_TYPE_P(e, T_COMPLEX) ) seen = &seen_cpx;
2307
- else if ( e == Qtrue || e == Qfalse ) seen = &seen_bool;
2308
- else seen = NULL;
2309
- if ( RB_INTEGER_TYPE_P(e) && ca_is_integer_type(cr) ) {
2310
- ca_check_integer_fits(e, cr->data_type);
2311
- }
2312
- if ( seen && *seen >= 0 ) {
2313
- continue;
2314
- }
2315
- probe_self = reference;
2316
- probe = e;
2317
- if ( both ) {
2318
- rb_ca_cast_self_or_other(&probe_self, &probe);
2319
- }
2320
- else {
2321
- rb_ca_cast_other((VALUE *) &probe_self, (volatile VALUE *) &probe);
2322
- }
2323
- TypedData_Get_Struct(probe, CArray, &carray_data_type, cp);
2324
- t = cp->data_type;
2325
- if ( seen ) *seen = t;
2326
- type = ( type < 0 ) ? t : ca_promote_type(type, t);
2327
- }
2328
- if ( type < 0 ) {
2329
- type = cr->data_type;
2900
+ return;
2330
2901
  }
2331
- *ary = rb_ca_wrap_readonly(*ary, INT2NUM(type));
2332
- return 1;
2902
+ type = ca_type_name[cr->data_type];
2903
+ article = ( type[0] == 'i' ) ? "an" : "a";
2904
+ example = ( cr->data_type == CA_BOOLEAN ) ? "CA_BOOLEAN(ary)"
2905
+ : "CA_INT64(ary) or CA_FLOAT64(ary)";
2906
+ rb_raise(rb_eTypeError,
2907
+ "%s: a Ruby Array is not an operand of %s %s array; make it a "
2908
+ "CArray of the type you mean first, e.g. %s",
2909
+ ca_calling_method_name(), article, type, example);
2333
2910
  }
2334
2911
 
2335
2912
  /* CArray.cast(value) -- singleton entry delegating to rb_ca_cast, which
@@ -2368,15 +2945,158 @@ ca_operand_from_memory_view (VALUE obj)
2368
2945
  its class. A Face reports the data type of its storage ("fixlen" for a
2369
2946
  time, a categorical, a record or a string column), which says nothing
2370
2947
  about what the operand is. */
2371
- static const char *
2948
+ static VALUE
2372
2949
  ca_coerce_operand_name (VALUE obj, CArray *ca)
2373
2950
  {
2374
2951
  if ( ca_is_face(ca) ) {
2375
- return rb_obj_classname(obj);
2952
+ return rb_obj_class(obj);
2953
+ }
2954
+ return rb_str_new_cstr(ca_type_name[ca->data_type]);
2955
+ }
2956
+
2957
+ int
2958
+ ca_value_foreign_to (CArray *ca, VALUE v)
2959
+ {
2960
+ if ( ca_is_face(ca) || ca->data_type == CA_OBJECT || ca->data_type == CA_FIXLEN ) {
2961
+ return 0;
2962
+ }
2963
+ if ( v == Qtrue || v == Qfalse || v == CA_UNDEF || rb_obj_is_carray(v) ) {
2964
+ return 0;
2965
+ }
2966
+ if ( RTEST(rb_obj_is_kind_of(v, rb_cNumeric)) || RB_TYPE_P(v, T_ARRAY) ) {
2967
+ return 0;
2968
+ }
2969
+ if ( rb_respond_to(v, rb_intern("to_ca")) || rb_memory_view_available_p(v) ) {
2970
+ return 0;
2971
+ }
2972
+ return 1;
2973
+ }
2974
+
2975
+ int
2976
+ ca_face_value_foreign (VALUE self, VALUE v)
2977
+ {
2978
+ ID id = rb_intern("foreign_value?");
2979
+ if ( rb_obj_is_carray(v) || ! rb_obj_respond_to(self, id, Qtrue) ) {
2980
+ return 0;
2981
+ }
2982
+ return RTEST(rb_funcall(self, id, 1, v));
2983
+ }
2984
+
2985
+ VALUE
2986
+ ca_truth_as_number (CArray *ca, VALUE v)
2987
+ {
2988
+ if ( ( v == Qtrue || v == Qfalse ) && ! ca_is_face(ca) &&
2989
+ ( ca_is_integer_type(ca) || ca_is_float_type(ca) ||
2990
+ ca_is_complex_type(ca) ) ) {
2991
+ return INT2FIX(v == Qtrue);
2376
2992
  }
2377
- return ca_type_name[ca->data_type];
2993
+ return v;
2994
+ }
2995
+
2996
+ /* The name Ruby's coercion error gives a value: nil and a Symbol by
2997
+ themselves, anything else by its class. */
2998
+ static VALUE
2999
+ ca_foreign_value_name (VALUE v)
3000
+ {
3001
+ if ( NIL_P(v) || SYMBOL_P(v) ) {
3002
+ return rb_inspect(v);
3003
+ }
3004
+ return rb_class_name(CLASS_OF(v));
3005
+ }
3006
+
3007
+ /* The Ruby class whose operator the array's cells answer for. */
3008
+ static const char *
3009
+ ca_cell_class_name (CArray *ca)
3010
+ {
3011
+ if ( ca_is_float_type(ca) ) return "Float";
3012
+ if ( ca_is_complex_type(ca) ) return "Complex";
3013
+ return "Integer";
3014
+ }
3015
+
3016
+ /* UNDEF beside an array, in an operator or a pick by order: it marks a
3017
+ missing cell where a value is written, and is asked about by eq / ne /
3018
+ count, but it is no value to compute with, in an array of any data
3019
+ type. */
3020
+ static void
3021
+ ca_refuse_undef_operand (VALUE v, CArray *ca, VALUE exc, int order)
3022
+ {
3023
+ if ( v != CA_UNDEF ) {
3024
+ return;
3025
+ }
3026
+ if ( ca_is_face(ca) || ca->data_type == CA_OBJECT ||
3027
+ ca->data_type == CA_FIXLEN ) {
3028
+ rb_raise(exc, "%s: UNDEF is no value to %s",
3029
+ ca_calling_method_name(), order ? "compare with" : "compute with");
3030
+ }
3031
+ if ( order ) {
3032
+ rb_raise(exc, "%s: comparison of %s with UNDEF failed",
3033
+ ca_calling_method_name(), ca_cell_class_name(ca));
3034
+ }
3035
+ rb_raise(exc, "%s: UNDEF can't be coerced into %s",
3036
+ ca_calling_method_name(), ca_cell_class_name(ca));
2378
3037
  }
2379
3038
 
3039
+ void
3040
+ ca_refuse_foreign_operand (VALUE v, volatile VALUE array)
3041
+ {
3042
+ CArray *ca;
3043
+ if ( ! rb_obj_is_carray(array) ) {
3044
+ return;
3045
+ }
3046
+ GetCArray(array, ca);
3047
+ ca_refuse_undef_operand(v, ca, rb_eTypeError, 0);
3048
+ if ( ca_value_foreign_to(ca, v) ) {
3049
+ rb_raise(rb_eTypeError, "%s: %" PRIsVALUE " can't be coerced into %s",
3050
+ ca_calling_method_name(), ca_foreign_value_name(v),
3051
+ ca_cell_class_name(ca));
3052
+ }
3053
+ }
3054
+
3055
+ void
3056
+ ca_refuse_foreign_order (VALUE v, volatile VALUE array)
3057
+ {
3058
+ CArray *ca;
3059
+ if ( ! rb_obj_is_carray(array) ) {
3060
+ return;
3061
+ }
3062
+ GetCArray(array, ca);
3063
+ ca_refuse_undef_operand(v, ca, rb_eArgError, 1);
3064
+ if ( ca_value_foreign_to(ca, v) ) {
3065
+ rb_raise(rb_eArgError, "%s: comparison of %s with %" PRIsVALUE " failed",
3066
+ ca_calling_method_name(), ca_cell_class_name(ca),
3067
+ ca_foreign_value_name(v));
3068
+ }
3069
+ }
3070
+
3071
+ /* true and false are the numbers 0 and 1 to an operator, as a boolean
3072
+ array is: an integer array reads them through ca_value_to_data_type, and
3073
+ a float or complex array reads them here rather than through Float(). */
3074
+ static int
3075
+ ca_truth_scalar_operand (volatile VALUE *scalar)
3076
+ {
3077
+ if ( *scalar == Qtrue || *scalar == Qfalse ) {
3078
+ *scalar = rb_cscalar_new_with_value(CA_BOOLEAN, 0, *scalar);
3079
+ return 1;
3080
+ }
3081
+ return 0;
3082
+ }
3083
+
3084
+ #ifdef HAVE_COMPLEX_H
3085
+ /* The type a Complex scalar takes beside `array`: cmplx64 beside a float32
3086
+ array (a Float scalar keeps float32 there, and a Complex keeps the same
3087
+ width), cmplx128 beside anything else. */
3088
+ static int8_t
3089
+ ca_complex_scalar_type (volatile VALUE array)
3090
+ {
3091
+ if ( rb_obj_is_carray(array) ) {
3092
+ CArray *ca;
3093
+ GetCArray(array, ca);
3094
+ if ( ca->data_type == CA_FLOAT32 ) return CA_CMPLX64;
3095
+ }
3096
+ return CA_CMPLX128;
3097
+ }
3098
+ #endif
3099
+
2380
3100
  void
2381
3101
  rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2382
3102
  {
@@ -2396,22 +3116,22 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2396
3116
  *other = ca_operand_from_memory_view(*other);
2397
3117
  }
2398
3118
 
2399
- if ( ! ca_array_operand(other, *self, 1) ) {
2400
- ca_array_operand(self, *other, 1);
2401
- }
3119
+ ca_refuse_array_operand(*other, *self);
3120
+ ca_refuse_array_operand(*self, *other);
2402
3121
 
2403
3122
  if ( ! rb_obj_is_carray(*self) ) {
2404
3123
  self_is_object = 1;
3124
+ ca_refuse_foreign_operand(*self, *other);
2405
3125
  if ( rb_ca_is_object_type(*other) ) {
2406
3126
  *self = rb_cscalar_new_with_value(CA_OBJECT, 0, *self);
2407
3127
  }
2408
3128
  #ifdef HAVE_COMPLEX_H
2409
- /* A Complex scalar with a non-object non-complex CArray on the other
2410
- side must wrap as CA_CMPLX128; falling into the float branch below
2411
- would NUM2DBL(Complex) and raise. Downstream promotion lifts the
2412
- other operand to cmplx128. */
3129
+ /* A Complex scalar wraps as a complex type before the float branch
3130
+ below, which would NUM2DBL(Complex) and raise: cmplx64 beside a
3131
+ float32 array, which keeps its width as beside a Float, and
3132
+ cmplx128 otherwise. */
2413
3133
  else if ( RB_TYPE_P(*self, T_COMPLEX) ) {
2414
- *self = rb_cscalar_new_with_value(CA_CMPLX128, 0, *self);
3134
+ *self = rb_cscalar_new_with_value(ca_complex_scalar_type(*other), 0, *self);
2415
3135
  }
2416
3136
  #endif
2417
3137
  else if ( ca_fixlen_scalar_operand(self, *other) ) {
@@ -2420,6 +3140,9 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2420
3140
  else if ( ca_integer_scalar_operand(self, *other) ) {
2421
3141
  /* the array's own integer type; see the helper above */
2422
3142
  }
3143
+ else if ( ca_truth_scalar_operand(self) ) {
3144
+ /* boolean, promoted with the array below */
3145
+ }
2423
3146
  else if ( rb_ca_is_float_type(*other) ) {
2424
3147
  *self = rb_cscalar_new_with_value(CA_FLOAT64, 0, *self);
2425
3148
  }
@@ -2435,17 +3158,15 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2435
3158
 
2436
3159
  if ( ! rb_obj_is_carray(*other) ) {
2437
3160
  other_is_object = 1;
3161
+ ca_refuse_foreign_operand(*other, *self);
2438
3162
 
2439
3163
  if ( rb_ca_is_object_type(*self) ) {
2440
3164
  *other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
2441
3165
  }
2442
3166
  #ifdef HAVE_COMPLEX_H
2443
- /* Symmetric to the self-branch above: Complex scalar wraps as
2444
- cmplx128 regardless of the CArray operand's numeric width, so
2445
- float64_array + Complex(0, 1) promotes rather than raising in
2446
- NUM2DBL(Complex). */
3167
+ /* Symmetric to the self-branch above. */
2447
3168
  else if ( RB_TYPE_P(*other, T_COMPLEX) ) {
2448
- *other = rb_cscalar_new_with_value(CA_CMPLX128, 0, *other);
3169
+ *other = rb_cscalar_new_with_value(ca_complex_scalar_type(*self), 0, *other);
2449
3170
  }
2450
3171
  #endif
2451
3172
  else if ( ca_fixlen_scalar_operand(other, *self) ) {
@@ -2454,6 +3175,9 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2454
3175
  else if ( ca_integer_scalar_operand(other, *self) ) {
2455
3176
  /* the array's own integer type; see the helper above */
2456
3177
  }
3178
+ else if ( ca_truth_scalar_operand(other) ) {
3179
+ /* boolean, promoted with the array below */
3180
+ }
2457
3181
  else if ( rb_ca_is_float_type(*self) ) {
2458
3182
  *other = rb_cscalar_new_with_value(CA_FLOAT64, 0, *other);
2459
3183
  }
@@ -2467,8 +3191,8 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2467
3191
  }
2468
3192
  }
2469
3193
 
2470
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
2471
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3194
+ GetCArray(*self, ca);
3195
+ GetCArray(*other, cb);
2472
3196
 
2473
3197
  /* Implicit size-1 broadcasting (case A only). No-op if either side
2474
3198
  is scalar, ndim differs, or shapes are already equal. If shapes
@@ -2476,8 +3200,8 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2476
3200
  no-op — the downstream element-count check in ca_set_iterator
2477
3201
  raises with the existing message. */
2478
3202
  ca_broadcast_pair(self, other);
2479
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
2480
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3203
+ GetCArray(*self, ca);
3204
+ GetCArray(*other, cb);
2481
3205
 
2482
3206
  if ( ca_is_scalar(ca) ^ ca_is_scalar(cb) ||
2483
3207
  self_is_object ||
@@ -2505,7 +3229,15 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2505
3229
  }
2506
3230
  }
2507
3231
  }
2508
-
3232
+
3233
+ /* float64 with cmplx64 meets in cmplx128 (see ca_promote_type). */
3234
+ if ( ( ca->data_type == CA_FLOAT64 && cb->data_type == CA_CMPLX64 ) ||
3235
+ ( ca->data_type == CA_CMPLX64 && cb->data_type == CA_FLOAT64 ) ) {
3236
+ *self = rb_ca_wrap_readonly(*self, INT2NUM(CA_CMPLX128));
3237
+ *other = rb_ca_wrap_readonly(*other, INT2NUM(CA_CMPLX128));
3238
+ return;
3239
+ }
3240
+
2509
3241
  test = ca_cast_table[cb->data_type][ca->data_type];
2510
3242
 
2511
3243
  if ( test == 0 ) {
@@ -2526,22 +3258,66 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2526
3258
  /* A data type the operation does not take, as `sqrt!` on an integer
2527
3259
  array is refused (CArray::DataTypeError is a TypeError). */
2528
3260
  rb_raise(rb_eCADataTypeError,
2529
- "can't coerce carray with data_types of '%s' and '%s'",
3261
+ "can't coerce carray with data_types of '%"PRIsVALUE"' and '%"PRIsVALUE"'",
2530
3262
  ca_coerce_operand_name(*self, ca),
2531
3263
  ca_coerce_operand_name(*other, cb));
2532
3264
  }
2533
3265
 
3266
+ /* The three operands of a three-operand expression at their common data_type.
3267
+ Pairs find that type, and they find it for operands that are not arrays
3268
+ yet. An array then goes to it in one step: taken through the pairs, it
3269
+ would pass through the type of the first pair (an int8 -1 becomes 255 in
3270
+ uint8 and stays 255 in uint64), and the answer would depend on the order
3271
+ of the operands. The arrays are then brought to one shape together
3272
+ (the size-1 broadcast of the pairs), after the cast: the cast starts from
3273
+ the operand as given, so a shape the pairs settled on is set again here. */
3274
+ void
3275
+ rb_ca_cast_three (volatile VALUE *v1, volatile VALUE *v2, volatile VALUE *v3)
3276
+ {
3277
+ volatile VALUE original[3];
3278
+ volatile VALUE *cast[3];
3279
+ CArray *ca;
3280
+ int8_t common;
3281
+ int i;
3282
+
3283
+ original[0] = *v1;
3284
+ original[1] = *v2;
3285
+ original[2] = *v3;
3286
+ cast[0] = v1;
3287
+ cast[1] = v2;
3288
+ cast[2] = v3;
3289
+
3290
+ rb_ca_cast_self_or_other(v1, v2);
3291
+ rb_ca_cast_self_or_other(v1, v3);
3292
+ rb_ca_cast_self_or_other(v2, v3);
3293
+ rb_ca_cast_self_or_other(v1, v2);
3294
+
3295
+ GetCArray(*v1, ca);
3296
+ common = ca->data_type;
3297
+ for ( i = 0; i < 3; i++ ) {
3298
+ if ( ! rb_obj_is_carray(original[i]) ) continue;
3299
+ GetCArray(original[i], ca);
3300
+ *cast[i] = ( ca->data_type != common && ! ca_is_face(ca) )
3301
+ ? rb_ca_wrap_readonly(original[i], INT2NUM(common))
3302
+ : original[i];
3303
+ }
3304
+ ca_broadcast_pair(v1, v2);
3305
+ ca_broadcast_pair(v1, v3);
3306
+ ca_broadcast_pair(v2, v3);
3307
+ ca_broadcast_pair(v1, v2);
3308
+ }
3309
+
2534
3310
  /* Refuses a pair of arrays with no common data_type in the words of
2535
3311
  rb_ca_cast_self_or_other (the eager operators'), and returns otherwise
2536
3312
  without casting either. A lazy builder calls it before it works out
2537
3313
  the kernel's data_types, so that a pair the eager operator refuses is
2538
3314
  refused the same way. */
2539
3315
  void
2540
- ca_refuse_without_common_type (VALUE self, VALUE other)
3316
+ ca_refuse_without_common_type (volatile VALUE self, volatile VALUE other)
2541
3317
  {
2542
3318
  CArray *ca, *cb;
2543
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2544
- TypedData_Get_Struct(other, CArray, &carray_data_type, cb);
3319
+ GetCArray(self, ca);
3320
+ GetCArray(other, cb);
2545
3321
  if ( ca->data_type != cb->data_type
2546
3322
  && ca_cast_table[cb->data_type][ca->data_type] < 0
2547
3323
  && ca_cast_table[ca->data_type][cb->data_type] < 0 ) {
@@ -2558,7 +3334,7 @@ VALUE
2558
3334
  rb_ca_s_cast_self_or_other (VALUE klass, VALUE self, VALUE other)
2559
3335
  {
2560
3336
  rb_ca_cast_self_or_other(&self, &other);
2561
- return rb_assoc_new(self, other);
3337
+ return rb_ary_new_from_args(2, self, other);
2562
3338
  }
2563
3339
 
2564
3340
  /* Extract a data_type code (0..CA_NTYPE-1) from an argument.
@@ -2571,12 +3347,12 @@ rb_ca_s_cast_self_or_other (VALUE klass, VALUE self, VALUE other)
2571
3347
  as a value -> :int64, not as a data_type
2572
3348
  code). */
2573
3349
  static int8_t
2574
- ca_arg_to_data_type (VALUE obj)
3350
+ ca_arg_to_data_type (volatile VALUE obj)
2575
3351
  {
2576
3352
  int8_t data_type;
2577
3353
  if ( rb_obj_is_carray(obj) ) {
2578
3354
  CArray *ca;
2579
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
3355
+ GetCArray(obj, ca);
2580
3356
  data_type = ca->data_type;
2581
3357
  }
2582
3358
  else if ( TYPE(obj) == T_SYMBOL || TYPE(obj) == T_STRING ||
@@ -2591,15 +3367,15 @@ ca_arg_to_data_type (VALUE obj)
2591
3367
  /* A MemoryView producer whose format says nothing usable (typeless, the
2592
3368
  other byte order) is read as the operators read it, and refused in
2593
3369
  the same words. */
2594
- VALUE ca_obj = ca_operand_from_memory_view(obj);
3370
+ volatile VALUE ca_obj = ca_operand_from_memory_view(obj);
2595
3371
  CArray *ca;
2596
- TypedData_Get_Struct(ca_obj, CArray, &carray_data_type, ca);
3372
+ GetCArray(ca_obj, ca);
2597
3373
  data_type = ca->data_type;
2598
3374
  }
2599
3375
  else {
2600
3376
  data_type = ca_value_to_data_type(obj);
2601
3377
  }
2602
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
3378
+ CA_CHECK_DATA_TYPE(data_type);
2603
3379
  if ( ! ca_valid[data_type] ) {
2604
3380
  rb_raise(rb_eArgError,
2605
3381
  "data type '%s' is not valid in this build",
@@ -2629,8 +3405,8 @@ ca_value_to_data_type (VALUE obj)
2629
3405
  }
2630
3406
 
2631
3407
  /* Promote two data_type codes according to ca_cast_table.
2632
- Returns the higher (= the type both can be cast to without loss
2633
- under the table's policy). Raises if incompatible.
3408
+ Returns the later type in the table's order, except that float64 with
3409
+ cmplx64 gives cmplx128. Raises if incompatible.
2634
3410
 
2635
3411
  Externally callable so the lazy view layer (CABinOp constructor)
2636
3412
  can compute its output_data_type using the same single-source
@@ -2645,6 +3421,13 @@ ca_promote_type (int8_t a, int8_t b)
2645
3421
  if ( a == b ) {
2646
3422
  return a;
2647
3423
  }
3424
+ /* float64 with cmplx64: the table puts cmplx64 after float64, but its
3425
+ parts are float32, so the pair meets in cmplx128, whose parts hold
3426
+ every float64. */
3427
+ if ( ( a == CA_FLOAT64 && b == CA_CMPLX64 ) ||
3428
+ ( a == CA_CMPLX64 && b == CA_FLOAT64 ) ) {
3429
+ return CA_CMPLX128;
3430
+ }
2648
3431
  test = ca_cast_table[a][b];
2649
3432
  if ( test == 0 ) {
2650
3433
  return b;
@@ -2709,13 +3492,13 @@ ca_value_match_type (int8_t t, int8_t a, int8_t b)
2709
3492
  by ca_value_match_type). Beside a reference that is not a number, a
2710
3493
  scalar query keeps the reference's type. */
2711
3494
  int8_t
2712
- ca_query_match_type (int8_t ref, VALUE query)
3495
+ ca_query_match_type (int8_t ref, volatile VALUE query)
2713
3496
  {
2714
3497
  int ref_int = ( ref >= CA_INT8 && ref <= CA_UINT64 );
2715
3498
  int ref_float = ( ref == CA_FLOAT32 || ref == CA_FLOAT64 );
2716
3499
  if ( rb_obj_is_carray(query) ) {
2717
3500
  CArray *q;
2718
- TypedData_Get_Struct(query, CArray, &carray_data_type, q);
3501
+ GetCArray(query, q);
2719
3502
  return ca_value_match_type(ca_promote_type(ref, q->data_type), ref,
2720
3503
  q->data_type);
2721
3504
  }
@@ -2736,23 +3519,23 @@ ca_query_match_type (int8_t ref, VALUE query)
2736
3519
  cut to the cell: "abX" matches no two-byte cell, as a wider scalar does
2737
3520
  not in `eq`. Anything else is returned as given. */
2738
3521
  VALUE
2739
- ca_fixlen_widen_for_query (VALUE self, VALUE query)
3522
+ ca_fixlen_widen_for_query (volatile VALUE self, VALUE query)
2740
3523
  {
2741
3524
  CArray *ca;
2742
3525
  if ( ! RB_TYPE_P(query, T_STRING) || ! rb_obj_is_carray(self) ) {
2743
3526
  return self;
2744
3527
  }
2745
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3528
+ GetCArray(self, ca);
2746
3529
  if ( ca->data_type != CA_FIXLEN || RSTRING_LEN(query) <= ca->bytes ) {
2747
3530
  return self;
2748
3531
  }
2749
- return rb_ca_to_type(self, INT2NUM(CA_FIXLEN), SIZE2NUM(RSTRING_LEN(query)));
3532
+ return rb_ca_to_type(self, ID2SYM(ca_data_type_sym[CA_FIXLEN]), SIZE2NUM(RSTRING_LEN(query)));
2750
3533
  }
2751
3534
 
2752
3535
  /* ca.query_match_type(query) -- ca_query_match_type with self as the
2753
3536
  reference, for the set family and locate_addr. */
2754
3537
  static VALUE
2755
- rb_ca_query_match_type (VALUE self, VALUE query)
3538
+ rb_ca_query_match_type (volatile VALUE self, VALUE query)
2756
3539
  {
2757
3540
  CArray *ca;
2758
3541
  GetCArray(self, ca);
@@ -2762,7 +3545,7 @@ rb_ca_query_match_type (VALUE self, VALUE query)
2762
3545
  /* ca.value_match_type(t, other_type) -- ca_value_match_type with self's
2763
3546
  data type as the first operand, for the set family and locate_addr. */
2764
3547
  static VALUE
2765
- rb_ca_value_match_type (VALUE self, VALUE vt, VALUE vother)
3548
+ rb_ca_value_match_type (volatile VALUE self, VALUE vt, VALUE vother)
2766
3549
  {
2767
3550
  CArray *ca;
2768
3551
  GetCArray(self, ca);
@@ -2774,11 +3557,11 @@ rb_ca_value_match_type (VALUE self, VALUE vt, VALUE vother)
2774
3557
  true, ...) or a CScalar. The operators let a scalar take the type of the
2775
3558
  array beside it, so result_type does too. */
2776
3559
  static int
2777
- ca_arg_is_scalar (VALUE obj)
3560
+ ca_arg_is_scalar (volatile VALUE obj)
2778
3561
  {
2779
3562
  if ( rb_obj_is_carray(obj) ) {
2780
3563
  CArray *ca;
2781
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
3564
+ GetCArray(obj, ca);
2782
3565
  return ca_is_scalar(ca);
2783
3566
  }
2784
3567
  if ( TYPE(obj) == T_SYMBOL || TYPE(obj) == T_STRING || TYPE(obj) == T_CLASS ) {
@@ -2790,13 +3573,20 @@ ca_arg_is_scalar (VALUE obj)
2790
3573
  /* The type a scalar `obj` (of type `st`) and an array of type `at` share,
2791
3574
  by the rule the operators apply: a scalar of the same kind takes the
2792
3575
  array's type (0.1 beside float32 is float32, 1 beside int8 is int8); one
2793
- of another kind promotes (1.5 beside int32 is float64). An Integer that
3576
+ of another kind promotes (1.5 beside int32 is float64), and a Complex
3577
+ beside float32 takes cmplx64, keeping the width. An Integer that
2794
3578
  does not fit the array's integer type takes a type that holds it and the
2795
3579
  array's values (int64, or object where no integer type does), where an
2796
3580
  operator would raise RangeError. */
2797
3581
  int8_t
2798
3582
  ca_promote_scalar_type (int8_t at, VALUE obj, int8_t st)
2799
3583
  {
3584
+ /* A boolean array beside an Integer is the numbers 0 and 1 in int64, as
3585
+ the operators read it, so an Integer past int64 takes object there as
3586
+ it does beside an int64 array. */
3587
+ if ( RB_INTEGER_TYPE_P(obj) && at == CA_BOOLEAN ) {
3588
+ at = CA_INT64;
3589
+ }
2800
3590
  if ( RB_INTEGER_TYPE_P(obj) && at >= CA_INT8 && at <= CA_UINT64 ) {
2801
3591
  int8_t vt;
2802
3592
  if ( ca_integer_range_side(obj, at) == 0 ) {
@@ -2809,6 +3599,12 @@ ca_promote_scalar_type (int8_t at, VALUE obj, int8_t st)
2809
3599
  : CA_OBJECT;
2810
3600
  return ca_value_match_type(ca_promote_type(at, vt), at, vt);
2811
3601
  }
3602
+ #ifdef HAVE_COMPLEX_H
3603
+ /* A Complex beside float32 keeps the width, as a Float does. */
3604
+ if ( RB_TYPE_P(obj, T_COMPLEX) && at == CA_FLOAT32 ) {
3605
+ return CA_CMPLX64;
3606
+ }
3607
+ #endif
2812
3608
  if ( ca_cast_table2[st][at] >= 0 ) {
2813
3609
  return at;
2814
3610
  }
@@ -2828,12 +3624,14 @@ static VALUE
2828
3624
  rb_ca_s_result_type (int argc, VALUE *argv, VALUE klass)
2829
3625
  {
2830
3626
  int8_t cur = -1;
2831
- int i;
2832
- VALUE rest, ropt;
3627
+ int i, has_array = 0;
3628
+ VALUE rest_arg, ropt;
3629
+ volatile VALUE rest;
2833
3630
 
2834
3631
  /* Keywords are refused; without this a keyword Hash would be classified
2835
3632
  as one more operand and the answer would turn into object. */
2836
- rb_scan_args(argc, argv, "*:", &rest, &ropt);
3633
+ rb_scan_args(argc, argv, "*:", &rest_arg, &ropt);
3634
+ rest = rest_arg;
2837
3635
  rb_reject_options(ropt);
2838
3636
  argc = (int) RARRAY_LEN(rest);
2839
3637
  argv = RARRAY_PTR(rest);
@@ -2842,20 +3640,32 @@ rb_ca_s_result_type (int argc, VALUE *argv, VALUE klass)
2842
3640
  rb_raise(rb_eArgError, "result_type requires at least one argument");
2843
3641
  }
2844
3642
 
3643
+ /* A Ruby Array has no data type: beside an object array it is one more
3644
+ object value, and anywhere else it is refused, as an operator refuses
3645
+ it. */
2845
3646
  for ( i = 0; i < argc; i++ ) {
2846
- if ( ! ca_arg_is_scalar(argv[i]) ) {
3647
+ if ( RB_TYPE_P(argv[i], T_ARRAY) ) {
3648
+ has_array = 1;
3649
+ }
3650
+ else if ( ! ca_arg_is_scalar(argv[i]) ) {
2847
3651
  int8_t t = ca_arg_to_data_type(argv[i]);
2848
3652
  cur = ( cur < 0 ) ? t : ca_promote_type(cur, t);
2849
3653
  }
2850
3654
  }
2851
3655
  for ( i = 0; i < argc; i++ ) {
2852
- if ( ca_arg_is_scalar(argv[i]) ) {
3656
+ if ( ! RB_TYPE_P(argv[i], T_ARRAY) && ca_arg_is_scalar(argv[i]) ) {
2853
3657
  int8_t t = ca_arg_to_data_type(argv[i]);
2854
3658
  cur = ( cur < 0 ) ? t : ca_promote_scalar_type(cur, argv[i], t);
2855
3659
  }
2856
3660
  }
3661
+ if ( has_array && cur != CA_OBJECT ) {
3662
+ rb_raise(rb_eTypeError,
3663
+ "result_type: a Ruby Array has no data type; make it a CArray of "
3664
+ "the type you mean first, e.g. %s",
3665
+ ( cur == CA_BOOLEAN ) ? "CA_BOOLEAN(ary)"
3666
+ : "CA_INT64(ary) or CA_FLOAT64(ary)");
3667
+ }
2857
3668
  /* argv points into rest, and classifying an argument can allocate */
2858
- RB_GC_GUARD(rest);
2859
3669
  /* Return Symbol for family consistency with
2860
3670
  CArray.value_to_data_type and ca.data_type. */
2861
3671
  return rb_ca_data_type_to_sym(cur);
@@ -2905,6 +3715,16 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
2905
3715
  rb_raise(rb_eArgError, "promote_list: list must not be empty");
2906
3716
  }
2907
3717
 
3718
+ /* A Ruby Array has no data type, so it is not an array to join. */
3719
+ for ( i = 0; i < n; i++ ) {
3720
+ if ( RB_TYPE_P(rb_ary_entry(list, i), T_ARRAY) ) {
3721
+ rb_raise(rb_eTypeError,
3722
+ "promote_list: a Ruby Array is not an array to join; make it a "
3723
+ "CArray of the type you mean first, e.g. CA_INT64(ary) or "
3724
+ "CA_FLOAT64(ary)");
3725
+ }
3726
+ }
3727
+
2908
3728
  /* Explicit data_type validation: reject Class / non-Symbol targets up
2909
3729
  front so the rest of the function only deals with `nil` or a
2910
3730
  primitive-shaped Symbol/String/Integer. */
@@ -2921,16 +3741,16 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
2921
3741
  primitive. Reject if any Face in list. */
2922
3742
  if ( ! NIL_P(data_type) ) {
2923
3743
  for ( i = 0; i < n; i++ ) {
2924
- VALUE elem = rb_ary_entry(list, i);
3744
+ volatile VALUE elem = rb_ary_entry(list, i);
2925
3745
  CArray *ca;
2926
3746
  rb_check_carray_object(elem);
2927
- TypedData_Get_Struct(elem, CArray, &carray_data_type, ca);
3747
+ GetCArray(elem, ca);
2928
3748
  if ( ca_is_face(ca) ) {
2929
3749
  rb_raise(rb_eArgError,
2930
- "promote_list: data_type %s cannot be applied to a list "
3750
+ "promote_list: data_type %"PRIsVALUE" cannot be applied to a list "
2931
3751
  "containing Face elements (= Face cannot be coerced to "
2932
3752
  "a primitive without losing identity)",
2933
- RSTRING_PTR(rb_inspect(data_type)));
3753
+ rb_inspect(data_type));
2934
3754
  }
2935
3755
  }
2936
3756
  {
@@ -2954,16 +3774,25 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
2954
3774
  return out;
2955
3775
  }
2956
3776
 
2957
- /* All primitive: result_type to determine common data_type, then
2958
- wrap_readonly each element. */
2959
- /* The arrays are joined value for value, so a list mixing signed and
2960
- unsigned integers takes a type that holds both (uint8 with int8 is
2961
- int16, not uint8, where -1 would read as 255): the rule equality
2962
- matches such a pair by. */
3777
+ /* All primitive: the common type of the operators, then wrap_readonly
3778
+ each element. The arrays are joined value for value, so a pair whose
3779
+ common type cannot hold every value of both (uint8 with int8, where -1
3780
+ would read as 255; int32 with float32, which holds integers only up to
3781
+ 2**24) is refused: the caller names the type to join them in
3782
+ (data_type:). The message gives the type that holds both. */
2963
3783
  common = ca_arg_to_data_type(rb_ary_entry(list, 0));
2964
3784
  for ( i = 1; i < n; i++ ) {
2965
3785
  int8_t next = ca_arg_to_data_type(rb_ary_entry(list, i));
2966
- common = ca_value_match_type(ca_promote_type(common, next), common, next);
3786
+ int8_t t = ca_promote_type(common, next);
3787
+ int8_t holds = ca_value_match_type(t, common, next);
3788
+ if ( holds != t ) {
3789
+ rb_raise(rb_eCADataTypeError,
3790
+ "promote_list: %s and %s meet in %s, which changes some of "
3791
+ "their values; name the type to join them in (%s holds both)",
3792
+ ca_type_name[common], ca_type_name[next],
3793
+ ca_type_name[t], ca_type_name[holds]);
3794
+ }
3795
+ common = t;
2967
3796
  }
2968
3797
  {
2969
3798
  VALUE common_sym = rb_ca_data_type_to_sym(common);
@@ -2980,13 +3809,13 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
2980
3809
  }
2981
3810
 
2982
3811
  void
2983
- rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3812
+ rb_ca_cast_other (volatile VALUE *self, volatile VALUE *other)
2984
3813
  {
2985
3814
  CArray *ca, *cb;
2986
3815
  CScalar *cs;
2987
3816
  int test0, test1;
2988
3817
 
2989
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
3818
+ GetCArray(*self, ca);
2990
3819
 
2991
3820
  /* Promote MemoryView-aware operand to CArray (CAWrap or CAStride).
2992
3821
  Excludes T_STRING which has its own legacy interpretation. */
@@ -2995,15 +3824,19 @@ rb_ca_cast_other (VALUE *self, volatile VALUE *other)
2995
3824
  *other = ca_operand_from_memory_view(*other);
2996
3825
  }
2997
3826
 
2998
- ca_array_operand(other, *self, 0);
3827
+ ca_refuse_array_operand(*other, *self);
2999
3828
 
3000
3829
  if ( ! rb_obj_is_carray(*other) ) {
3830
+ ca_refuse_foreign_operand(*other, *self);
3001
3831
  if ( rb_ca_is_object_type(*self) ) {
3002
3832
  *other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
3003
3833
  }
3004
3834
  else if ( ca_fixlen_scalar_operand(other, *self) ) {
3005
3835
  return; /* already the receiver's data_type and width */
3006
3836
  }
3837
+ else if ( ca_truth_scalar_operand(other) ) {
3838
+ /* boolean, promoted with the array below */
3839
+ }
3007
3840
  else if ( rb_ca_is_float_type(*self) ) {
3008
3841
  *other = rb_cscalar_new_with_value(CA_FLOAT64, 0, *other);
3009
3842
  }
@@ -3027,7 +3860,7 @@ rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3027
3860
 
3028
3861
  }
3029
3862
 
3030
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3863
+ GetCArray(*other, cb);
3031
3864
 
3032
3865
  test1 = ca_cast_table[cb->data_type][ca->data_type];
3033
3866
 
@@ -3042,7 +3875,7 @@ rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3042
3875
  /* A data type the operation does not take, as `sqrt!` on an integer
3043
3876
  array is refused (CArray::DataTypeError is a TypeError). */
3044
3877
  rb_raise(rb_eCADataTypeError,
3045
- "can't coerce carray with data_types of '%s' and '%s'",
3878
+ "can't coerce carray with data_types of '%"PRIsVALUE"' and '%"PRIsVALUE"'",
3046
3879
  ca_coerce_operand_name(*self, ca),
3047
3880
  ca_coerce_operand_name(*other, cb));
3048
3881
  }
@@ -3061,7 +3894,7 @@ rb_ca_cast_with (VALUE self, VALUE other)
3061
3894
  else {
3062
3895
  rb_raise(rb_eTypeError, "first argument should be a carray");
3063
3896
  }
3064
- return rb_assoc_new(self, other);
3897
+ return rb_ary_new_from_args(2, self, other);
3065
3898
  }
3066
3899
 
3067
3900
  /* -----------------------------------------------------------------------
@@ -3078,7 +3911,7 @@ rb_ca_cast_with (VALUE self, VALUE other)
3078
3911
  ----------------------------------------------------------------------- */
3079
3912
 
3080
3913
  static VALUE
3081
- ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3914
+ ca_clip_float_to_integer (volatile VALUE self, int8_t to_dt)
3082
3915
  {
3083
3916
  CArray *ca;
3084
3917
  GetCArray(self, ca);
@@ -3088,7 +3921,7 @@ ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3088
3921
  volatile VALUE vout = rb_carray_new(to_dt, ca->ndim, ca->dim, 0, NULL);
3089
3922
  CArray *out;
3090
3923
  GetCArray(vout, out);
3091
- ca_attach(src); /* window: nothing raises inside */
3924
+ ca_attach(src); /* attach: nothing raises before the detach */
3092
3925
  double *sp = (double *) src->ptr;
3093
3926
  char *op = out->ptr;
3094
3927
  ca_size_t n = ca->elements;
@@ -3129,7 +3962,7 @@ ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3129
3962
 
3130
3963
  #define CLIP_CAST_FUNC(name, to_dt, lo_expr, hi_expr, cast_func) \
3131
3964
  static VALUE \
3132
- rb_ca_##name (VALUE self) \
3965
+ rb_ca_##name (volatile VALUE self) \
3133
3966
  { \
3134
3967
  CArray *ca; \
3135
3968
  GetCArray(self, ca); \
@@ -3158,7 +3991,7 @@ CLIP_CAST_FUNC(clip_uint64, CA_UINT64, INT2NUM(0), ULL2NUM(1
3158
3991
 
3159
3992
  Array -> shape-guessed CArray, element-wise store
3160
3993
  CArray -> copy if same data_type, else #to_type
3161
- nil -> empty CArray
3994
+ nil -> refused (an empty array is CA_INT32([]))
3162
3995
  Range -> arange; a second argument, if given, is the step
3163
3996
  Numeric -> CScalar, or #to_ca when the object answers it
3164
3997
  String -> the whitespace / `,` / `;` / `_`->UNDEF parser, which reads
@@ -3188,25 +4021,26 @@ ca_cast_to_type1 (VALUE v, VALUE sym)
3188
4021
  return rb_ca_to_type_internal(1, list, v);
3189
4022
  }
3190
4023
 
4024
+ /* recursion: re-enters once, as object */
3191
4025
  static VALUE
3192
4026
  ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3193
4027
  {
3194
- VALUE v = ( argc >= 1 ) ? argv[0] : Qnil;
4028
+ volatile VALUE v = ( argc >= 1 ) ? argv[0] : Qnil;
3195
4029
  VALUE sym = ID2SYM(ca_data_type_sym[data_type]);
3196
4030
 
3197
4031
  /* Only the Range branch below reads a second argument (the step), and
3198
4032
  nothing reads a third. The arity is -1 for the step's sake, so the count
3199
4033
  is checked here; without it `CA_INT32(0, 2)`, which looks like the shape
3200
4034
  spelling `CArray.int32(3, 3)`, casts the 0 and drops the 2. */
3201
- if ( argc > 2 ) {
4035
+ if ( argc < 1 || argc > 2 ) {
3202
4036
  rb_raise(rb_eArgError,
3203
- "wrong number of arguments (given %d, expected 0..2)", argc);
4037
+ "wrong number of arguments (given %d, expected 1..2)", argc);
3204
4038
  }
3205
4039
  if ( argc == 2 && ! RTEST(rb_obj_is_kind_of(v, rb_cRange)) ) {
3206
4040
  rb_raise(rb_eArgError,
3207
4041
  "a step (the second argument) is only accepted "
3208
- "when the first argument is a Range (got %s)",
3209
- rb_obj_classname(v));
4042
+ "when the first argument is a Range (got %"PRIsVALUE")",
4043
+ rb_obj_class(v));
3210
4044
  }
3211
4045
 
3212
4046
  if ( TYPE(v) == T_ARRAY ) {
@@ -3218,15 +4052,14 @@ ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3218
4052
  }
3219
4053
  else if ( RTEST(rb_obj_is_kind_of(v, rb_cCArray)) ) {
3220
4054
  CArray *ca;
3221
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
4055
+ GetCArray(v, ca);
3222
4056
  if ( ca->data_type == data_type ) {
3223
4057
  return rb_ca_copy(v);
3224
4058
  }
3225
4059
  return ca_cast_to_type1(v, sym);
3226
4060
  }
3227
4061
  else if ( NIL_P(v) ) {
3228
- ca_size_t dim0 = 0;
3229
- return rb_carray_new(data_type, 1, &dim0, 0, NULL);
4062
+ ca_refuse_nil_data();
3230
4063
  }
3231
4064
  else if ( RTEST(rb_obj_is_kind_of(v, rb_cRange)) ) {
3232
4065
  /* arange: n = ((end - begin).abs / step).floor + 1, or for an excluded
@@ -3274,9 +4107,9 @@ ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3274
4107
  else {
3275
4108
  /* Numeric / #to_ca responder */
3276
4109
  if ( rb_respond_to(v, id_to_ca_cast) ) {
3277
- VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
4110
+ volatile VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
3278
4111
  CArray *cap;
3279
- TypedData_Get_Struct(ca, CArray, &carray_data_type, cap);
4112
+ GetCArray(ca, cap);
3280
4113
  return ( cap->data_type == data_type ) ? ca : ca_cast_to_type1(ca, sym);
3281
4114
  }
3282
4115
  return rb_cscalar_new_with_value(data_type, 0, v);
@@ -3324,20 +4157,22 @@ ca_cast_to_fixlen (VALUE v, VALUE fixlen_sym, VALUE bytes_v)
3324
4157
  Ruby, unlike the numeric casts).
3325
4158
  Array -> bytes defaults to max top-level element #length; new + store
3326
4159
  CArray -> copy if already fixlen, else #to_type(:fixlen, bytes:)
3327
- nil -> empty fixlen (bytes 0 when omitted, matching the old default)
4160
+ nil -> refused (an empty array is CA_FIXLEN([]))
3328
4161
  other -> #to_ca coerce, else CScalar.new(:fixlen, bytes:) { data } */
3329
4162
  static VALUE
3330
4163
  rb_ca_cast_fixlen (int argc, VALUE *argv, VALUE self)
3331
4164
  {
3332
- VALUE v, opts = Qnil, bytes_v = Qnil;
4165
+ VALUE v_arg, opts = Qnil, bytes_v = Qnil;
4166
+ volatile VALUE v;
3333
4167
  VALUE fixlen_sym = ID2SYM(ca_data_type_sym[CA_FIXLEN]);
3334
4168
 
3335
- rb_scan_args(argc, argv, "11", &v, &opts);
4169
+ rb_scan_args(argc, argv, "11", &v_arg, &opts);
3336
4170
  rb_scan_options(opts, "bytes", &bytes_v);
4171
+ v = v_arg;
3337
4172
 
3338
4173
  if ( RTEST(rb_obj_is_kind_of(v, rb_cCArray)) ) {
3339
4174
  CArray *ca;
3340
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
4175
+ GetCArray(v, ca);
3341
4176
  if ( ca->data_type == CA_FIXLEN ) {
3342
4177
  return rb_ca_copy(v);
3343
4178
  }
@@ -3359,25 +4194,23 @@ rb_ca_cast_fixlen (int argc, VALUE *argv, VALUE self)
3359
4194
  }
3360
4195
  bytes = ( maxlen > 0 ) ? maxlen : 1; /* all empty: one NUL byte */
3361
4196
  } else {
3362
- bytes = NUM2SIZE(bytes_v);
4197
+ bytes = ca_integer_arg(bytes_v, "bytes", NULL);
3363
4198
  }
3364
4199
  shape = rb_funcall(rb_cCArray, id_guess_array_shape_cast, 1, v);
3365
4200
  ndim = RARRAY_LEN(shape);
3366
- for ( i = 0; i < ndim; i++ ) dim[i] = NUM2SIZE(rb_ary_entry(shape, i));
4201
+ for ( i = 0; i < ndim; i++ ) dim[i] = ca_integer_arg(rb_ary_entry(shape, i), "a dimension", NULL);
3367
4202
  obj = rb_carray_new(CA_FIXLEN, (int8_t) ndim, dim, bytes, NULL);
3368
4203
  rb_ca_store_all(obj, v);
3369
4204
  return obj;
3370
4205
  }
3371
4206
  else if ( NIL_P(v) ) {
3372
- ca_size_t dim0 = 0;
3373
- ca_size_t bytes = NIL_P(bytes_v) ? 1 : NUM2SIZE(bytes_v);
3374
- return rb_carray_new(CA_FIXLEN, 1, &dim0, bytes, NULL);
4207
+ ca_refuse_nil_data();
3375
4208
  }
3376
4209
  else {
3377
4210
  if ( rb_respond_to(v, id_to_ca_cast) ) {
3378
- VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
4211
+ volatile VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
3379
4212
  CArray *cap;
3380
- TypedData_Get_Struct(ca, CArray, &carray_data_type, cap);
4213
+ GetCArray(ca, cap);
3381
4214
  return ( cap->data_type == CA_FIXLEN )
3382
4215
  ? ca : ca_cast_to_fixlen(ca, fixlen_sym, bytes_v);
3383
4216
  }
@@ -3387,7 +4220,7 @@ rb_ca_cast_fixlen (int argc, VALUE *argv, VALUE self)
3387
4220
  check reports it. */
3388
4221
  ca_size_t bytes = 0;
3389
4222
  if ( ! NIL_P(bytes_v) ) {
3390
- bytes = NUM2SIZE(bytes_v);
4223
+ bytes = ca_integer_arg(bytes_v, "bytes", NULL);
3391
4224
  }
3392
4225
  else if ( RB_TYPE_P(v, T_STRING) ) {
3393
4226
  bytes = ( RSTRING_LEN(v) > 0 ) ? RSTRING_LEN(v) : 1;
@@ -3427,22 +4260,28 @@ rb_ca_read_text_as_number (int argc, VALUE *argv, VALUE self)
3427
4260
  return Qnil; /* not a number type: the caller casts */
3428
4261
  }
3429
4262
 
3430
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4263
+ GetCArray(src, ca);
3431
4264
  if ( ca->data_type != CA_OBJECT ) {
3432
4265
  rb_raise(rb_eArgError, "decimal reading needs an object array");
3433
4266
  }
4267
+ /* A Face (CAString) holds no buffer of its own: read the array it is a
4268
+ view of. */
4269
+ while ( ca_is_face(ca) ) {
4270
+ src = rb_ca_parent(src);
4271
+ GetCArray(src, ca);
4272
+ }
3434
4273
  if ( ! ca_is_entity(ca) || ! ca->ptr ) {
3435
4274
  src = rb_ca_copy(src);
3436
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4275
+ GetCArray(src, ca);
3437
4276
  }
3438
4277
 
3439
4278
  out = rb_carray_new(data_type, ca->ndim, ca->dim, 0, NULL);
3440
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
4279
+ GetCArray(out, co);
3441
4280
  vmask = rb_carray_new(CA_BOOLEAN, ca->ndim, ca->dim, 0, NULL);
3442
4281
 
3443
4282
  cells = (VALUE *) ca->ptr;
3444
4283
  m_in = ca_has_mask(ca) ? (boolean8_t *) ca->mask->ptr : NULL;
3445
- TypedData_Get_Struct(vmask, CArray, &carray_data_type, cmask);
4284
+ GetCArray(vmask, cmask);
3446
4285
  m_out = (boolean8_t *) cmask->ptr;
3447
4286
 
3448
4287
  for (i = 0; i < ca->elements; i++) {
@@ -3490,8 +4329,6 @@ rb_ca_read_text_as_number (int argc, VALUE *argv, VALUE self)
3490
4329
 
3491
4330
  ca_mask_from_bytes(co, m_out);
3492
4331
  rb_ca_inherit_attr(out, self);
3493
- RB_GC_GUARD(src);
3494
- RB_GC_GUARD(vmask);
3495
4332
  return out;
3496
4333
  }
3497
4334
 
@@ -3564,19 +4401,25 @@ rb_ca_infer_number_type_of_text (VALUE self)
3564
4401
  ca_size_t i;
3565
4402
  int present = 0, any_float = 0;
3566
4403
 
3567
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4404
+ GetCArray(src, ca);
3568
4405
  if ( ca->data_type != CA_OBJECT ) {
3569
4406
  rb_raise(rb_eArgError, "type inference needs an object array");
3570
4407
  }
4408
+ /* A Face (CAString) copies to a Face, which holds no buffer of its own:
4409
+ read the cells of the array it is a view of. */
4410
+ while ( ca_is_face(ca) ) {
4411
+ src = rb_ca_parent(src);
4412
+ GetCArray(src, ca);
4413
+ }
3571
4414
  if ( ! ca_is_entity(ca) || ! ca->ptr ) {
3572
4415
  src = rb_ca_copy(src);
3573
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4416
+ GetCArray(src, ca);
3574
4417
  }
3575
4418
  cells = (VALUE *) ca->ptr;
3576
4419
  m = ca_has_mask(ca) ? (boolean8_t *) ca->mask->ptr : NULL;
3577
4420
 
3578
4421
  for (i = 0; i < ca->elements; i++) {
3579
- VALUE v = cells[i];
4422
+ volatile VALUE v = cells[i];
3580
4423
  int kind;
3581
4424
  if ( ( m && m[i] ) || NIL_P(v) ) {
3582
4425
  continue;
@@ -3598,7 +4441,6 @@ rb_ca_infer_number_type_of_text (VALUE self)
3598
4441
  kind = CA_INFER_TEXT;
3599
4442
  }
3600
4443
  if ( kind == CA_INFER_TEXT ) {
3601
- RB_GC_GUARD(src);
3602
4444
  return Qnil;
3603
4445
  }
3604
4446
  if ( kind != CA_INFER_MISSING ) {
@@ -3606,7 +4448,6 @@ rb_ca_infer_number_type_of_text (VALUE self)
3606
4448
  any_float |= ( kind == CA_INFER_FLOAT );
3607
4449
  }
3608
4450
  }
3609
- RB_GC_GUARD(src);
3610
4451
  if ( ! present ) {
3611
4452
  return Qnil;
3612
4453
  }
@@ -3618,22 +4459,23 @@ rb_ca_infer_number_type_of_text (VALUE self)
3618
4459
  __infer_number_type_of_text__ / __read_text_as_number__ call them, so
3619
4460
  CAFrame#infer_types and #cast read a text column from a file this way. */
3620
4461
 
3621
- /* The buffer of a CAConstString and an entity of its (start, end) byte
3622
- ranges, masked where a cell is missing. */
4462
+ /* An entity of the (start, end) byte ranges of a CAConstString, masked
4463
+ where a cell is missing; the buffer of the CAConstString goes to
4464
+ *buffer. */
3623
4465
  static VALUE
3624
- ca_const_string_buffer_and_ranges (VALUE cs, VALUE *buffer, CArray **ranges)
4466
+ ca_const_string_buffer_and_ranges (VALUE cs, volatile VALUE *buffer)
3625
4467
  {
3626
4468
  volatile VALUE vranges;
4469
+ CArray *ranges;
3627
4470
  VALUE klass = rb_const_get(rb_cObject, rb_intern("CAConstString"));
3628
4471
  if ( ! RTEST(rb_obj_is_kind_of(cs, klass)) ) {
3629
4472
  rb_raise(rb_eTypeError, "needs a CAConstString");
3630
4473
  }
3631
4474
  *buffer = rb_funcall(cs, rb_intern("buffer"), 0);
3632
4475
  vranges = rb_ca_parent(cs);
3633
- TypedData_Get_Struct(vranges, CArray, &carray_data_type, *ranges);
3634
- if ( ! ca_is_entity(*ranges) || ! (*ranges)->ptr ) {
4476
+ GetCArray(vranges, ranges);
4477
+ if ( ! ca_is_entity(ranges) || ! ranges->ptr ) {
3635
4478
  vranges = rb_ca_copy(vranges);
3636
- TypedData_Get_Struct(vranges, CArray, &carray_data_type, *ranges);
3637
4479
  }
3638
4480
  return vranges;
3639
4481
  }
@@ -3642,7 +4484,7 @@ static VALUE
3642
4484
  rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3643
4485
  {
3644
4486
  volatile VALUE src;
3645
- VALUE buffer;
4487
+ volatile VALUE buffer;
3646
4488
  CArray *pe;
3647
4489
  const char *buf;
3648
4490
  const int64_t *pair;
@@ -3650,7 +4492,8 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3650
4492
  ca_size_t i;
3651
4493
  int present = 0, any_float = 0;
3652
4494
 
3653
- src = ca_const_string_buffer_and_ranges(cs, &buffer, &pe);
4495
+ src = ca_const_string_buffer_and_ranges(cs, &buffer);
4496
+ GetCArray(src, pe);
3654
4497
  buf = RSTRING_PTR(buffer);
3655
4498
  pair = (const int64_t *) pe->ptr;
3656
4499
  m = ca_has_mask(pe) ? (boolean8_t *) pe->mask->ptr : NULL;
@@ -3661,7 +4504,6 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3661
4504
  }
3662
4505
  kind = ca_infer_decimal_kind_of_bytes(buf + pair[2 * i], (long) ( pair[2 * i + 1] - pair[2 * i] ));
3663
4506
  if ( kind == CA_INFER_TEXT ) {
3664
- RB_GC_GUARD(src);
3665
4507
  return Qnil;
3666
4508
  }
3667
4509
  if ( kind != CA_INFER_MISSING ) {
@@ -3669,8 +4511,6 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3669
4511
  any_float |= ( kind == CA_INFER_FLOAT );
3670
4512
  }
3671
4513
  }
3672
- RB_GC_GUARD(src);
3673
- RB_GC_GUARD(buffer);
3674
4514
  if ( ! present ) {
3675
4515
  return Qnil;
3676
4516
  }
@@ -3681,7 +4521,7 @@ static VALUE
3681
4521
  rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VALUE report)
3682
4522
  {
3683
4523
  volatile VALUE src, out;
3684
- VALUE buffer;
4524
+ volatile VALUE buffer;
3685
4525
  CArray *pe, *co;
3686
4526
  int8_t data_type;
3687
4527
  const char *buf;
@@ -3701,9 +4541,10 @@ rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VA
3701
4541
  default:
3702
4542
  return Qnil; /* not a number type: the caller casts */
3703
4543
  }
3704
- src = ca_const_string_buffer_and_ranges(cs, &buffer, &pe);
4544
+ src = ca_const_string_buffer_and_ranges(cs, &buffer);
4545
+ GetCArray(src, pe);
3705
4546
  out = rb_carray_new(data_type, pe->ndim, pe->dim, 0, NULL);
3706
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
4547
+ GetCArray(out, co);
3707
4548
  buf = RSTRING_PTR(buffer);
3708
4549
  pair = (const int64_t *) pe->ptr;
3709
4550
  m_in = ca_has_mask(pe) ? (boolean8_t *) pe->mask->ptr : NULL;
@@ -3728,11 +4569,36 @@ rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VA
3728
4569
  }
3729
4570
  }
3730
4571
  }
3731
- RB_GC_GUARD(src);
3732
- RB_GC_GUARD(buffer);
3733
4572
  return out;
3734
4573
  }
3735
4574
 
4575
+ /* to_type / as_type as Ruby methods: a data_type from Ruby is named, not
4576
+ numbered. The C callers above pass the number and are not checked. */
4577
+ static VALUE
4578
+ rb_ca_to_type_method (int argc, VALUE *argv, VALUE self)
4579
+ {
4580
+ /* args: looks at the type only; rb_ca_to_type_internal reads them all */
4581
+ if ( argc >= 1 ) {
4582
+ ca_refuse_type_number(argv[0]);
4583
+ }
4584
+ return rb_ca_to_type_internal(argc, argv, self);
4585
+ }
4586
+
4587
+ static VALUE
4588
+ rb_ca_as_type_method (int argc, VALUE *argv, VALUE self)
4589
+ {
4590
+ /* args: looks at the type only; rb_ca_as_type_internal reads them all */
4591
+ if ( argc >= 1 ) {
4592
+ ca_refuse_type_number(argv[0]);
4593
+ if ( RTEST(rb_obj_is_data_class(argv[0])) ) {
4594
+ rb_reject_options(rb_pop_options(&argc, &argv));
4595
+ rb_check_arity(argc, 1, 1);
4596
+ return ca_record_view_of(self, argv[0], "as_type");
4597
+ }
4598
+ }
4599
+ return rb_ca_as_type_internal(argc, argv, self);
4600
+ }
4601
+
3736
4602
  void
3737
4603
  Init_carray_cast (void)
3738
4604
  {
@@ -3749,7 +4615,7 @@ Init_carray_cast (void)
3749
4615
  rb_define_singleton_method(rb_cCArray, "__const_string_infer_number_type_of_text__", rb_ca_s_const_string_infer_number_type_of_text, 1);
3750
4616
  rb_define_singleton_method(rb_cCArray, "__const_string_read_text_as_number__", rb_ca_s_const_string_read_text_as_number, 3);
3751
4617
 
3752
- rb_define_method(rb_cCArray, "to_type", rb_ca_to_type_internal, -1);
4618
+ rb_define_method(rb_cCArray, "to_type", rb_ca_to_type_method, -1);
3753
4619
 
3754
4620
  rb_define_method(rb_cCArray, "fixlen", rb_ca_to_fixlen, -1);
3755
4621
  rb_define_method(rb_cCArray, "boolean", rb_ca_to_boolean, 0);
@@ -3767,15 +4633,7 @@ Init_carray_cast (void)
3767
4633
  rb_define_method(rb_cCArray, "cmplx128", rb_ca_to_cmplx128, 0);
3768
4634
  rb_define_method(rb_cCArray, "object", rb_ca_to_VALUE, 0);
3769
4635
 
3770
- rb_define_alias(rb_cCArray, "byte", "uint8");
3771
- rb_define_alias(rb_cCArray, "short", "int16");
3772
- rb_define_alias(rb_cCArray, "int", "int32");
3773
- rb_define_alias(rb_cCArray, "float", "float32");
3774
- rb_define_alias(rb_cCArray, "double", "float64");
3775
- rb_define_alias(rb_cCArray, "complex", "cmplx64");
3776
- rb_define_alias(rb_cCArray, "dcomplex", "cmplx128");
3777
-
3778
- rb_define_method(rb_cCArray, "as_type", rb_ca_as_type_internal, -1);
4636
+ rb_define_method(rb_cCArray, "as_type", rb_ca_as_type_method, -1);
3779
4637
 
3780
4638
  rb_define_method(rb_cCArray, "as_fixlen", rb_ca_as_fixlen, -1);
3781
4639
  rb_define_method(rb_cCArray, "as_boolean", rb_ca_as_boolean, 0);
@@ -3789,20 +4647,10 @@ Init_carray_cast (void)
3789
4647
  rb_define_method(rb_cCArray, "as_uint64", rb_ca_as_uint64, 0);
3790
4648
  rb_define_method(rb_cCArray, "as_float32", rb_ca_as_float32, 0);
3791
4649
  rb_define_method(rb_cCArray, "as_float64", rb_ca_as_float64, 0);
3792
- rb_define_method(rb_cCArray, "as_float128", rb_ca_as_float128, 0);
3793
4650
  rb_define_method(rb_cCArray, "as_cmplx64", rb_ca_as_cmplx64, 0);
3794
4651
  rb_define_method(rb_cCArray, "as_cmplx128", rb_ca_as_cmplx128, 0);
3795
- rb_define_method(rb_cCArray, "as_cmplx256", rb_ca_as_cmplx256, 0);
3796
4652
  rb_define_method(rb_cCArray, "as_object", rb_ca_as_VALUE, 0);
3797
4653
 
3798
- rb_define_alias(rb_cCArray, "as_byte", "as_uint8");
3799
- rb_define_alias(rb_cCArray, "as_short", "as_int16");
3800
- rb_define_alias(rb_cCArray, "as_int", "as_int32");
3801
- rb_define_alias(rb_cCArray, "as_float", "as_float32");
3802
- rb_define_alias(rb_cCArray, "as_double", "as_float64");
3803
- rb_define_alias(rb_cCArray, "as_complex", "as_cmplx64");
3804
- rb_define_alias(rb_cCArray, "as_dcomplex", "as_cmplx128");
3805
-
3806
4654
  rb_define_singleton_method(rb_cCArray,
3807
4655
  "wrap_writable", rb_ca_s_wrap_writable, -1);
3808
4656
  rb_define_singleton_method(rb_cCArray,
@@ -3860,26 +4708,18 @@ Init_carray_cast (void)
3860
4708
  rb_define_global_function("CA_CMPLX64", rb_ca_cast_cmplx64, -1);
3861
4709
  rb_define_global_function("CA_CMPLX128", rb_ca_cast_cmplx128, -1);
3862
4710
  rb_define_global_function("CA_OBJECT", rb_ca_cast_object, -1);
3863
- /* type-name aliases */
3864
- rb_define_global_function("CA_BYTE", rb_ca_cast_uint8, -1);
3865
- rb_define_global_function("CA_SHORT", rb_ca_cast_int16, -1);
3866
- rb_define_global_function("CA_INT", rb_ca_cast_int32, -1);
3867
- rb_define_global_function("CA_FLOAT", rb_ca_cast_float32, -1);
3868
- rb_define_global_function("CA_DOUBLE", rb_ca_cast_float64, -1);
3869
- rb_define_global_function("CA_COMPLEX", rb_ca_cast_cmplx64, -1);
3870
- rb_define_global_function("CA_DCOMPLEX", rb_ca_cast_cmplx128, -1);
3871
4711
  rb_define_global_function("CA_SIZE", rb_ca_cast_size, -1);
3872
4712
  rb_define_global_function("CA_FIXLEN", rb_ca_cast_fixlen, -1);
3873
4713
 
3874
4714
  rb_define_method(rb_cCArray, "cast_with", rb_ca_cast_with, 1);
3875
4715
 
3876
- rb_define_method(rb_cCArray, "clip_int8", rb_ca_clip_int8, 0);
3877
- rb_define_method(rb_cCArray, "clip_int16", rb_ca_clip_int16, 0);
3878
- rb_define_method(rb_cCArray, "clip_int32", rb_ca_clip_int32, 0);
3879
- rb_define_method(rb_cCArray, "clip_int64", rb_ca_clip_int64, 0);
3880
- rb_define_method(rb_cCArray, "clip_uint8", rb_ca_clip_uint8, 0);
3881
- rb_define_method(rb_cCArray, "clip_uint16", rb_ca_clip_uint16, 0);
3882
- rb_define_method(rb_cCArray, "clip_uint32", rb_ca_clip_uint32, 0);
3883
- rb_define_method(rb_cCArray, "clip_uint64", rb_ca_clip_uint64, 0);
4716
+ rb_define_private_method(rb_cCArray, "clip_int8", rb_ca_clip_int8, 0);
4717
+ rb_define_private_method(rb_cCArray, "clip_int16", rb_ca_clip_int16, 0);
4718
+ rb_define_private_method(rb_cCArray, "clip_int32", rb_ca_clip_int32, 0);
4719
+ rb_define_private_method(rb_cCArray, "clip_int64", rb_ca_clip_int64, 0);
4720
+ rb_define_private_method(rb_cCArray, "clip_uint8", rb_ca_clip_uint8, 0);
4721
+ rb_define_private_method(rb_cCArray, "clip_uint16", rb_ca_clip_uint16, 0);
4722
+ rb_define_private_method(rb_cCArray, "clip_uint32", rb_ca_clip_uint32, 0);
4723
+ rb_define_private_method(rb_cCArray, "clip_uint64", rb_ca_clip_uint64, 0);
3884
4724
  }
3885
4725