carray 3.0.7 → 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 (275) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1940
  4. data/README.md +38 -32
  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 +6 -9
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +27 -0
  13. data/ext/ca_binop_dispatch.h +22 -47
  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 -1506
  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 +90 -163
  28. data/ext/ca_obj_bincmp.c +47 -46
  29. data/ext/ca_obj_binop.c +38 -38
  30. data/ext/ca_obj_bitarray.c +5 -17
  31. data/ext/ca_obj_bitfield.c +15 -30
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +32 -30
  34. data/ext/ca_obj_const_string.c +57 -62
  35. data/ext/ca_obj_face.c +99 -91
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +15 -21
  38. data/ext/ca_obj_farray.c +7 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +8 -19
  41. data/ext/ca_obj_grid.c +137 -106
  42. data/ext/ca_obj_meld.c +23 -21
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +24 -35
  45. data/ext/ca_obj_object.c +51 -37
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +8 -10
  48. data/ext/ca_obj_refer.c +42 -29
  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 +18 -24
  52. data/ext/ca_obj_select.c +10 -22
  53. data/ext/ca_obj_select_axis.c +27 -36
  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 +28 -24
  57. data/ext/ca_obj_stride.c +53 -54
  58. data/ext/ca_obj_string.c +6 -18
  59. data/ext/ca_obj_tile.c +12 -12
  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 +35 -39
  64. data/ext/ca_obj_window.c +67 -86
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +34 -12
  67. data/ext/ca_op_powi.h +4 -0
  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 +20 -26
  72. data/ext/ca_sweep_engine.c +2 -3
  73. data/ext/ca_sweep_engine.h +17 -9
  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 +11 -11
  77. data/ext/carray.h +300 -1011
  78. data/ext/carray_access.c +234 -180
  79. data/ext/carray_address_basis.c +33 -38
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +30 -26
  83. data/ext/carray_call_cfunc.c +169 -277
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +912 -346
  86. data/ext/carray_cast_func.rb +31 -3
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +43 -38
  89. data/ext/carray_copy.c +28 -26
  90. data/ext/carray_core.c +217 -126
  91. data/ext/carray_count.c +27 -15
  92. data/ext/carray_element.c +40 -25
  93. data/ext/carray_factorize.c +53 -52
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +8 -10
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +123 -228
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1274 -160
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +21 -36
  103. data/ext/carray_loop.c +19 -22
  104. data/ext/carray_mask.c +123 -145
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +95 -94
  108. data/ext/carray_memory_view.c +42 -47
  109. data/ext/carray_operator.c +423 -122
  110. data/ext/carray_order.c +121 -85
  111. data/ext/carray_partition.c +13 -15
  112. data/ext/carray_random.c +47 -41
  113. data/ext/carray_round.c +9 -4
  114. data/ext/carray_scatter.c +54 -40
  115. data/ext/carray_segment.c +41 -45
  116. data/ext/carray_slab.c +64 -47
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +32 -29
  119. data/ext/carray_sort_kernel.c +12 -10
  120. data/ext/carray_struct.c +38 -43
  121. data/ext/carray_test.c +47 -43
  122. data/ext/carray_time_text.c +24 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +258 -94
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +19 -20
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +782 -461
  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 +264 -180
  135. data/lib/carray/basics.rb +272 -48
  136. data/lib/carray/bincount_nd.rb +33 -16
  137. data/lib/carray/block_iterator.rb +123 -102
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +63 -51
  140. data/lib/carray/categorical_iterator.rb +89 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +49 -22
  143. data/lib/carray/const_string.rb +54 -62
  144. data/lib/carray/core_extensions.rb +1 -1
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +25 -24
  148. data/lib/carray/frame/column_verbs.rb +20 -8
  149. data/lib/carray/frame/concat.rb +3 -3
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +7 -1
  152. data/lib/carray/frame/frame.rb +35 -25
  153. data/lib/carray/frame/group.rb +183 -150
  154. data/lib/carray/frame/io.rb +26 -7
  155. data/lib/carray/frame/join.rb +37 -12
  156. data/lib/carray/frame/records.rb +1 -1
  157. data/lib/carray/frame/reshape.rb +3 -3
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +148 -118
  160. data/lib/carray/frame/windows.rb +232 -21
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +24 -21
  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 +97 -24
  167. data/lib/carray/key_windows.rb +83 -57
  168. data/lib/carray/lazy.rb +196 -94
  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 +5 -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 +72 -71
  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/gather_nd.rb +106 -106
  183. data/lib/carray/methods/insert_block.rb +2 -2
  184. data/lib/carray/methods/is_between.rb +6 -4
  185. data/lib/carray/methods/is_in.rb +165 -145
  186. data/lib/carray/methods/join.rb +2 -1
  187. data/lib/carray/methods/lag.rb +6 -2
  188. data/lib/carray/methods/locate_addr.rb +150 -138
  189. data/lib/carray/methods/mask_duplicates.rb +1 -1
  190. data/lib/carray/methods/meshgrid.rb +28 -16
  191. data/lib/carray/methods/mode.rb +67 -67
  192. data/lib/carray/methods/nunique.rb +9 -7
  193. data/lib/carray/methods/repeat.rb +12 -12
  194. data/lib/carray/methods/resize.rb +1 -1
  195. data/lib/carray/methods/snap.rb +24 -20
  196. data/lib/carray/methods/string_format.rb +2 -2
  197. data/lib/carray/methods/unique.rb +4 -2
  198. data/lib/carray/methods/value_counts.rb +1 -1
  199. data/lib/carray/mkmf.rb +25 -28
  200. data/lib/carray/percentile_arguments.rb +82 -0
  201. data/lib/carray/record_order.rb +18 -17
  202. data/lib/carray/rng.rb +1 -0
  203. data/lib/carray/runtime.rb +190 -22
  204. data/lib/carray/segment_iterator.rb +251 -129
  205. data/lib/carray/serialize.rb +618 -571
  206. data/lib/carray/slab_iterator.rb +114 -114
  207. data/lib/carray/stack.rb +11 -12
  208. data/lib/carray/string.rb +16 -22
  209. data/lib/carray/string_operation_extension.rb +76 -52
  210. data/lib/carray/struct.rb +122 -164
  211. data/lib/carray/struct_builder.rb +243 -72
  212. data/lib/carray/time.rb +496 -342
  213. data/lib/carray/window_iterator.rb +191 -150
  214. data/lib/carray.rb +6 -11
  215. data/yard-stubs/ca_obj_array.rb +20 -135
  216. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  217. data/yard-stubs/ca_obj_bitfield.rb +4 -3
  218. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  219. data/yard-stubs/ca_obj_fake.rb +3 -1
  220. data/yard-stubs/ca_obj_farray.rb +1 -1
  221. data/yard-stubs/ca_obj_field.rb +8 -6
  222. data/yard-stubs/ca_obj_refer.rb +7 -5
  223. data/yard-stubs/ca_obj_roll.rb +7 -6
  224. data/yard-stubs/ca_obj_shift.rb +2 -3
  225. data/yard-stubs/ca_obj_stride.rb +5 -7
  226. data/yard-stubs/ca_obj_tile.rb +1 -1
  227. data/yard-stubs/ca_obj_time.rb +43 -0
  228. data/yard-stubs/ca_obj_transpose.rb +1 -1
  229. data/yard-stubs/ca_obj_window.rb +26 -15
  230. data/yard-stubs/carray_access.rb +51 -4
  231. data/yard-stubs/carray_attribute.rb +26 -38
  232. data/yard-stubs/carray_cast.rb +118 -165
  233. data/yard-stubs/carray_class.rb +3 -4
  234. data/yard-stubs/carray_conversion.rb +6 -7
  235. data/yard-stubs/carray_copy.rb +2 -1
  236. data/yard-stubs/carray_count.rb +5 -3
  237. data/yard-stubs/carray_element.rb +4 -3
  238. data/yard-stubs/carray_limits.rb +100 -0
  239. data/yard-stubs/carray_loop.rb +0 -20
  240. data/yard-stubs/carray_mask.rb +59 -41
  241. data/yard-stubs/carray_math.rb +157 -100
  242. data/yard-stubs/carray_mathfunc.rb +415 -3
  243. data/yard-stubs/carray_median_percentile.rb +29 -11
  244. data/yard-stubs/carray_memory_view.rb +2 -2
  245. data/yard-stubs/carray_order.rb +67 -34
  246. data/yard-stubs/carray_random.rb +6 -6
  247. data/yard-stubs/carray_reduce.rb +353 -0
  248. data/yard-stubs/carray_rng.rb +92 -0
  249. data/yard-stubs/carray_scatter.rb +10 -2
  250. data/yard-stubs/carray_slab.rb +7 -4
  251. data/yard-stubs/carray_sort.rb +7 -6
  252. data/yard-stubs/carray_undef.rb +44 -5
  253. data/yard-stubs/ruby_carray.rb +75 -40
  254. metadata +62 -24
  255. data/CHANGELOG.v1.md +0 -126
  256. data/ext/carray_build_flags.h +0 -3
  257. data/ext/carray_config.h +0 -28
  258. data/ext/carray_data_type.c +0 -90
  259. data/ext/carray_kernels_bincmp.c +0 -6457
  260. data/ext/carray_kernels_binop.c +0 -31534
  261. data/ext/carray_kernels_init.c +0 -1734
  262. data/ext/carray_kernels_moncmp.c +0 -2104
  263. data/ext/carray_kernels_monop.c +0 -25337
  264. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  265. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  266. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  267. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  268. data/ext/carray_kernels_reduce_variance.c +0 -5981
  269. data/ext/carray_kernels_scan.c +0 -4302
  270. data/ext/carray_kernels_search.c +0 -37776
  271. data/ext/carray_kernels_sort.c +0 -10814
  272. data/ext/carray_kernels_triop.c +0 -1864
  273. data/lib/carray/construct.rb +0 -360
  274. data/lib/carray/data_type_extension.rb +0 -262
  275. 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
  {
@@ -614,11 +804,14 @@ OBJ2BOOL (VALUE v)
614
804
  return (boolean8_t) flag;
615
805
  }
616
806
  }
617
- {
618
- VALUE inspect = rb_inspect(v);
619
- rb_raise(rb_eCADataTypeError,
620
- "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");
621
812
  }
813
+ rb_raise(rb_eCADataTypeError, "%s: can't cast object '%"PRIsVALUE"' to <boolean>",
814
+ ca_calling_method_name(), rb_inspect(v));
622
815
  }
623
816
 
624
817
  VALUE
@@ -668,8 +861,14 @@ OBJ2DBL (VALUE val)
668
861
  ca_decimal_string_store(val, CA_FLOAT64, &d);
669
862
  return d;
670
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;
671
870
  default:
672
- return NUM2DBL(rb_Float(val));
871
+ return NUM2DBL(ca_number_of_value(val, 0, "a float"));
673
872
  }
674
873
  }
675
874
 
@@ -696,7 +895,7 @@ ca_kernel_integer (VALUE v)
696
895
  first character and not an exponent's. Returns 0 when the text is not
697
896
  of that form. */
698
897
  static int
699
- ca_str_to_cmplx_nonfinite (VALUE str, double complex *out)
898
+ ca_str_to_cmplx_nonfinite (volatile VALUE str, double complex *out)
700
899
  {
701
900
  const char *s = RSTRING_PTR(str);
702
901
  long n = RSTRING_LEN(str), k;
@@ -726,9 +925,15 @@ ca_str_to_cmplx_nonfinite (VALUE str, double complex *out)
726
925
  return 1;
727
926
  }
728
927
 
928
+ static VALUE
929
+ ca_kernel_complex (VALUE str)
930
+ {
931
+ return rb_funcall(rb_mKernel, rb_intern("Complex"), 1, str);
932
+ }
933
+
729
934
  /* A real is read as a decimal number; anything else goes to Complex(). */
730
935
  double complex
731
- ca_str_to_cmplx (VALUE str)
936
+ ca_str_to_cmplx (volatile VALUE str)
732
937
  {
733
938
  volatile VALUE rstr;
734
939
  volatile VALUE c;
@@ -743,10 +948,43 @@ ca_str_to_cmplx (VALUE str)
743
948
  return z;
744
949
  }
745
950
  rstr = rb_funcall(str, rb_intern("strip"), 0);
746
- 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
+ }
747
961
  return CMPLX(NUM2DBL(rb_complex_real(c)), NUM2DBL(rb_complex_imag(c)));
748
962
  }
749
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
+
750
988
  /* Object -> double with parse-failure signalling. Returns 1 and sets *out
751
989
  on success; returns 0 when the cell cannot be parsed, in which case the
752
990
  caller writes UNDEF (mask) for that cell. nil and unreadable
@@ -959,7 +1197,10 @@ rb_obj2long (VALUE val)
959
1197
  case T_BIGNUM:
960
1198
  return (long) NUM2LL(val);
961
1199
  case T_NIL:
962
- 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");
963
1204
  break;
964
1205
  case T_STRING: {
965
1206
  int64_t v;
@@ -967,7 +1208,7 @@ rb_obj2long (VALUE val)
967
1208
  return (long) v;
968
1209
  }
969
1210
  default:
970
- return NUM2LONG(rb_Integer(val));
1211
+ return NUM2LONG(ca_number_of_value(val, 1, "an integer"));
971
1212
  }
972
1213
  }
973
1214
 
@@ -980,7 +1221,10 @@ rb_obj2ulong (VALUE val)
980
1221
  case T_BIGNUM:
981
1222
  return (unsigned long) rb_num2ull(val);
982
1223
  case T_NIL:
983
- 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");
984
1228
  break;
985
1229
  case T_STRING: {
986
1230
  uint64_t v;
@@ -988,7 +1232,7 @@ rb_obj2ulong (VALUE val)
988
1232
  return (unsigned long) v;
989
1233
  }
990
1234
  default:
991
- return NUM2ULONG(rb_Integer(val));
1235
+ return NUM2ULONG(ca_number_of_value(val, 1, "an integer"));
992
1236
  }
993
1237
  }
994
1238
 
@@ -999,7 +1243,10 @@ rb_obj2ll (VALUE val)
999
1243
  case T_FIXNUM:
1000
1244
  return NUM2LONG(val);
1001
1245
  case T_NIL:
1002
- 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");
1003
1250
  break;
1004
1251
  case T_STRING: {
1005
1252
  int64_t v;
@@ -1007,7 +1254,7 @@ rb_obj2ll (VALUE val)
1007
1254
  return (long long) v;
1008
1255
  }
1009
1256
  default:
1010
- return NUM2LL(rb_Integer(val));
1257
+ return NUM2LL(ca_number_of_value(val, 1, "an integer"));
1011
1258
  }
1012
1259
  }
1013
1260
 
@@ -1018,7 +1265,10 @@ rb_obj2ull (VALUE val)
1018
1265
  case T_FIXNUM:
1019
1266
  return NUM2ULONG(val);
1020
1267
  case T_NIL:
1021
- 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");
1022
1272
  break;
1023
1273
  case T_STRING: {
1024
1274
  uint64_t v;
@@ -1026,7 +1276,7 @@ rb_obj2ull (VALUE val)
1026
1276
  return (unsigned long long) v;
1027
1277
  }
1028
1278
  default:
1029
- return rb_num2ull(rb_Integer(val));
1279
+ return rb_num2ull(ca_number_of_value(val, 1, "an integer"));
1030
1280
  }
1031
1281
  }
1032
1282
 
@@ -1147,7 +1397,7 @@ ca_ptr2val (void *ap1, void *ptr1, int8_t data_type, void *ptr2)
1147
1397
  {
1148
1398
  CArray *ca1 = (CArray *)ap1;
1149
1399
  static CArray dummy;
1150
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
1400
+ CA_CHECK_DATA_TYPE(data_type);
1151
1401
  dummy.data_type = data_type;
1152
1402
  ca_cast_func_table[ca1->data_type][data_type](1, ca1, ptr1, &dummy, ptr2, NULL);
1153
1403
  return;
@@ -1158,7 +1408,7 @@ ca_val2ptr (int8_t data_type, void *ptr1, void *ap2, void *ptr2)
1158
1408
  {
1159
1409
  CArray *ca2 = (CArray *)ap2;
1160
1410
  static CArray dummy;
1161
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
1411
+ CA_CHECK_DATA_TYPE(data_type);
1162
1412
  dummy.data_type = data_type;
1163
1413
  ca_cast_func_table[data_type][ca2->data_type](1, &dummy, ptr1, ca2, ptr2, NULL);
1164
1414
  return;
@@ -1168,8 +1418,8 @@ void
1168
1418
  ca_val2val (int8_t data_type1, void *ptr1, int8_t data_type2, void *ptr2)
1169
1419
  {
1170
1420
  static CArray dummy1, dummy2;
1171
- CA_CHECK_DATA_TYPE_GIVEN(data_type1);
1172
- CA_CHECK_DATA_TYPE_GIVEN(data_type2);
1421
+ CA_CHECK_DATA_TYPE(data_type1);
1422
+ CA_CHECK_DATA_TYPE(data_type2);
1173
1423
  dummy1.data_type = data_type1;
1174
1424
  dummy2.data_type = data_type2;
1175
1425
  ca_cast_func_table[data_type1][data_type2](1, &dummy1, ptr1, &dummy2, ptr2, NULL);
@@ -1187,13 +1437,13 @@ ca_val2val (int8_t data_type1, void *ptr1, int8_t data_type2, void *ptr2)
1187
1437
  the storage instead is `.parent`, which stays available and explicit. Mask
1188
1438
  and shape carry. */
1189
1439
  static VALUE
1190
- rb_ca_surface_to_object (VALUE self)
1440
+ rb_ca_surface_to_object (volatile VALUE self)
1191
1441
  {
1192
1442
  volatile VALUE obj;
1193
1443
  CArray *ca;
1194
1444
  ca_size_t i;
1195
1445
 
1196
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1446
+ GetCArray(self, ca);
1197
1447
 
1198
1448
  if ( ca_is_scalar(ca) ) {
1199
1449
  obj = rb_cscalar_new(CA_OBJECT, 0, ca->mask);
@@ -1229,7 +1479,7 @@ static VALUE
1229
1479
  rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1230
1480
  {
1231
1481
  static ID id_to_numeric = 0;
1232
- VALUE num;
1482
+ volatile VALUE num;
1233
1483
  CArray *cn;
1234
1484
 
1235
1485
  if ( id_to_numeric == 0 ) {
@@ -1252,7 +1502,7 @@ rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1252
1502
  rb_obj_class(self), rb_obj_class(num));
1253
1503
  }
1254
1504
 
1255
- TypedData_Get_Struct(num, CArray, &carray_data_type, cn);
1505
+ GetCArray(num, cn);
1256
1506
 
1257
1507
  if ( ca_is_face(cn) ) {
1258
1508
  rb_raise(rb_eTypeError,
@@ -1271,14 +1521,39 @@ rb_ca_face_numeric_projection (VALUE self, CArray *ca)
1271
1521
  }
1272
1522
 
1273
1523
  static VALUE
1274
- 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)
1275
1525
  {
1276
1526
  volatile VALUE obj, rval;
1277
1527
  CArray *ca;
1278
- int i;
1528
+ ca_size_t i;
1279
1529
  ID id_encode = rb_intern("encode");
1280
1530
 
1281
- 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
+ }
1282
1557
 
1283
1558
  if ( ca_is_scalar(ca) ) {
1284
1559
  obj = rb_cscalar_new(CA_FIXLEN, bytes, ca->mask);
@@ -1289,6 +1564,9 @@ rb_ca_object_to_data_class (VALUE self, VALUE rtype, ca_size_t bytes)
1289
1564
 
1290
1565
  for (i=0; i<ca->elements; i++) {
1291
1566
  rval = rb_ca_fetch_addr(self, i);
1567
+ if ( rval == CA_UNDEF ) {
1568
+ continue;
1569
+ }
1292
1570
  if ( TYPE(rval) == T_STRING ) {
1293
1571
  rb_ca_store_addr(obj, i, rval);
1294
1572
  }
@@ -1367,11 +1645,11 @@ ca_to_type_cast (VALUE arg)
1367
1645
  || ca->data_type == CA_CMPLX64 || ca->data_type == CA_CMPLX128 );
1368
1646
 
1369
1647
  if ( ( ca->data_type == CA_OBJECT
1370
- && ( to_integer
1648
+ && ( to_integer || cb->data_type == CA_BOOLEAN
1371
1649
  || cb->data_type == CA_FLOAT32 || cb->data_type == CA_FLOAT64
1372
1650
  || cb->data_type == CA_CMPLX64 || cb->data_type == CA_CMPLX128 ) )
1373
1651
  || ( from_float && to_integer ) ) {
1374
- /* 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
1375
1653
  cell becomes UNDEF (see ext/carray_cast_func.rb). Cast into a
1376
1654
  scratch mask seeded from the source mask, then attach it to the
1377
1655
  output only if some cell ended up masked -- an all-valid cast keeps
@@ -1380,6 +1658,7 @@ ca_to_type_cast (VALUE arg)
1380
1658
  ca_size_t i;
1381
1659
  boolean8_t any = 0;
1382
1660
  boolean8_t *scratch;
1661
+ /* buffer: kept in the context, so ca_to_type_release frees it when the cast raises */
1383
1662
  c->scratch = scratch = ALLOC_N(boolean8_t, ne > 0 ? ne : 1);
1384
1663
  if ( cb->mask ) {
1385
1664
  memcpy(scratch, cb->mask->ptr, ne); /* copy of source mask */
@@ -1448,18 +1727,20 @@ rb_ca_to_type_internal (int argc, VALUE *argv, VALUE self)
1448
1727
  return obj;
1449
1728
  }
1450
1729
 
1730
+ /* recursion: re-enters once, with the target type resolved */
1451
1731
  static VALUE
1452
- rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1732
+ rb_ca_to_type_convert (int argc, VALUE *argv, volatile VALUE self)
1453
1733
  {
1454
- volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
1734
+ volatile VALUE obj;
1735
+ VALUE rtype = Qnil, ropt, rbytes = Qnil;
1455
1736
  CArray *ca, *cb;
1456
1737
  ca_to_type_ctx_t ctx;
1457
1738
  int8_t data_type;
1458
1739
  ca_size_t bytes;
1459
1740
 
1460
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
1741
+ GetCArray(self, ca);
1461
1742
 
1462
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &ropt);
1743
+ rb_scan_args(argc, argv, "11", &rtype, &ropt);
1463
1744
  rb_scan_options(ropt, "bytes", &rbytes);
1464
1745
 
1465
1746
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
@@ -1469,7 +1750,9 @@ rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1469
1750
  }
1470
1751
 
1471
1752
  if ( rb_ca_is_object_type(self) && rb_obj_is_data_class(rtype) ) {
1472
- 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);
1473
1756
  }
1474
1757
 
1475
1758
  /* NonNumeric Face (CA_FIXLEN surface = the numeric gate) to anything but
@@ -1492,12 +1775,12 @@ rb_ca_to_type_convert (int argc, VALUE *argv, VALUE self)
1492
1775
  }
1493
1776
 
1494
1777
 
1495
- TypedData_Get_Struct(obj, CArray, &carray_data_type, cb);
1778
+ GetCArray(obj, cb);
1496
1779
 
1497
1780
  ctx.ca = ca;
1498
1781
  ctx.cb = cb;
1499
1782
  ctx.scratch = NULL;
1500
- 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 */
1501
1784
  rb_ensure(ca_to_type_cast, (VALUE) &ctx, ca_to_type_release, (VALUE) &ctx);
1502
1785
 
1503
1786
  /* When rtype is a data_class (e.g. CAStruct subclass), wrap the
@@ -1532,7 +1815,7 @@ rb_ca_to_fixlen (int argc, VALUE *argv, VALUE self)
1532
1815
  {
1533
1816
  volatile VALUE ropt = rb_pop_options(&argc, &argv);
1534
1817
  VALUE list[2];
1535
- list[0] = INT2NUM(CA_FIXLEN);
1818
+ list[0] = ID2SYM(ca_data_type_sym[CA_FIXLEN]);
1536
1819
  list[1] = ropt;
1537
1820
  return rb_ca_to_type_internal(2, list, self);
1538
1821
  }
@@ -1625,9 +1908,6 @@ VALUE rb_ca_to_VALUE (VALUE self)
1625
1908
 
1626
1909
  /* CArray#as_type */
1627
1910
 
1628
- /* Forward declaration: CAMonOp builder (ext/ca_obj_monop.c). */
1629
- extern VALUE rb_ca_monop_build (VALUE cary, uint16_t op_id);
1630
-
1631
1911
  /* Forward declaration: clip kernel (ext/carray_kernels.c, generated). */
1632
1912
  extern VALUE rb_ca_clip (VALUE self, VALUE lo, VALUE hi);
1633
1913
 
@@ -1656,11 +1936,11 @@ enum {
1656
1936
  or the Face's own #to_numeric for a numeric target. as_type and
1657
1937
  wrap_writable have no honest answer and say so. */
1658
1938
  static VALUE
1659
- 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)
1660
1940
  {
1661
1941
  CArray *ca;
1662
1942
 
1663
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
1943
+ GetCArray(obj, ca);
1664
1944
 
1665
1945
  /* Same split the eager cast makes: :object is the Face's surface for every
1666
1946
  Face, and a NonNumeric Face (CA_FIXLEN surface) has no reading of its
@@ -1699,11 +1979,12 @@ ca_type_adapt_view (VALUE obj, VALUE rtype, int8_t data_type, int mode)
1699
1979
  static VALUE
1700
1980
  rb_ca_as_type_internal (int argc, VALUE *argv, VALUE self)
1701
1981
  {
1702
- volatile VALUE obj, rtype = Qnil, ropt, rbytes = Qnil;
1982
+ volatile VALUE obj;
1983
+ VALUE rtype = Qnil, ropt, rbytes = Qnil;
1703
1984
  int8_t data_type;
1704
1985
  ca_size_t bytes;
1705
1986
 
1706
- rb_scan_args(argc, argv, "11", (VALUE *) &rtype, (VALUE *) &ropt);
1987
+ rb_scan_args(argc, argv, "11", &rtype, &ropt);
1707
1988
  rb_scan_options(ropt, "bytes", &rbytes);
1708
1989
 
1709
1990
  rb_ca_guess_type_and_bytes(rtype, rbytes, &data_type, &bytes);
@@ -1750,10 +2031,11 @@ rb_ca_as_type (VALUE self, VALUE rtype, VALUE rbytes)
1750
2031
  VALUE
1751
2032
  rb_ca_as_fixlen (int argc, VALUE *argv, VALUE self)
1752
2033
  {
1753
- volatile VALUE rtype, ropt = rb_pop_options(&argc, &argv);
2034
+ VALUE rtype;
2035
+ volatile VALUE ropt = rb_pop_options(&argc, &argv);
1754
2036
  VALUE list[2];
1755
- rb_scan_args(argc, argv, "01", (VALUE *) &rtype);
1756
- 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;
1757
2039
  list[1] = ropt;
1758
2040
  return rb_ca_as_type_internal(2, list, self);
1759
2041
  }
@@ -1824,12 +2106,6 @@ VALUE rb_ca_as_float64 (VALUE self)
1824
2106
  rb_ca_as_type_method_body(CA_FLOAT64);
1825
2107
  }
1826
2108
 
1827
- /* CArray#as_float128 -- short-hand of as_type(:float128). */
1828
- VALUE rb_ca_as_float128 (VALUE self)
1829
- {
1830
- rb_ca_as_type_method_body(CA_FLOAT128);
1831
- }
1832
-
1833
2109
  /* CArray#as_cmplx64 -- short-hand of as_type(:cmplx64). */
1834
2110
  VALUE rb_ca_as_cmplx64 (VALUE self)
1835
2111
  {
@@ -1842,12 +2118,6 @@ VALUE rb_ca_as_cmplx128 (VALUE self)
1842
2118
  rb_ca_as_type_method_body(CA_CMPLX128);
1843
2119
  }
1844
2120
 
1845
- /* CArray#as_cmplx256 -- short-hand of as_type(:cmplx256). */
1846
- VALUE rb_ca_as_cmplx256 (VALUE self)
1847
- {
1848
- rb_ca_as_type_method_body(CA_CMPLX256);
1849
- }
1850
-
1851
2121
  /* CArray#as_object -- short-hand of as_type(:object). */
1852
2122
  VALUE rb_ca_as_VALUE (VALUE self)
1853
2123
  {
@@ -1857,12 +2127,12 @@ VALUE rb_ca_as_VALUE (VALUE self)
1857
2127
  /* ------------------------------------------------------------------------*/
1858
2128
 
1859
2129
  VALUE
1860
- rb_ca_cast_block (ca_size_t n, VALUE ra1, void *ptr1,
1861
- VALUE ra2, void *ptr2)
2130
+ rb_ca_cast_block (ca_size_t n, volatile VALUE ra1, void *ptr1,
2131
+ volatile VALUE ra2, void *ptr2)
1862
2132
  {
1863
2133
  CArray *ca1, *ca2;
1864
- TypedData_Get_Struct(ra1, CArray, &carray_data_type, ca1);
1865
- TypedData_Get_Struct(ra2, CArray, &carray_data_type, ca2);
2134
+ GetCArray(ra1, ca1);
2135
+ GetCArray(ra2, ca2);
1866
2136
  if ( n < 0 ) {
1867
2137
  rb_raise(rb_eRuntimeError, "[BUG] in rb_ca_cast_block: negative count");
1868
2138
  }
@@ -1871,11 +2141,11 @@ rb_ca_cast_block (ca_size_t n, VALUE ra1, void *ptr1,
1871
2141
  }
1872
2142
 
1873
2143
  VALUE
1874
- 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)
1875
2145
  {
1876
2146
  CArray *ca1, *ca2;
1877
- TypedData_Get_Struct(ra1, CArray, &carray_data_type, ca1);
1878
- TypedData_Get_Struct(ra2, CArray, &carray_data_type, ca2);
2147
+ GetCArray(ra1, ca1);
2148
+ GetCArray(ra2, ca2);
1879
2149
  ca_cast_func_table[ca1->data_type][ca2->data_type](1, ca1, ptr1, ca2, ptr2, NULL);
1880
2150
  return Qnil;
1881
2151
  }
@@ -1885,12 +2155,12 @@ rb_ca_ptr2ptr (VALUE ra1, void *ptr1, VALUE ra2, void *ptr2)
1885
2155
  steps of ca[i]: a boolean is 0 or 1 and a Face cell is its storage
1886
2156
  value. The element readers call this, check the mask, then decode. */
1887
2157
  VALUE
1888
- rb_ca_ptr2obj_storage (VALUE self, void *ptr)
2158
+ rb_ca_ptr2obj_storage (volatile VALUE self, void *ptr)
1889
2159
  {
1890
2160
  volatile VALUE obj;
1891
2161
  static CArray dummy;
1892
2162
  CArray *ca;
1893
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2163
+ GetCArray(self, ca);
1894
2164
  dummy.data_type = CA_OBJECT;
1895
2165
  ca_cast_func_table[ca->data_type][CA_OBJECT](1, ca, ptr, &dummy, (void*)&obj, NULL);
1896
2166
  if ( ca_is_fixlen_type(ca) ) {
@@ -1902,11 +2172,11 @@ rb_ca_ptr2obj_storage (VALUE self, void *ptr)
1902
2172
  }
1903
2173
 
1904
2174
  VALUE
1905
- rb_ca_ptr2obj (VALUE self, void *ptr)
2175
+ rb_ca_ptr2obj (volatile VALUE self, void *ptr)
1906
2176
  {
1907
2177
  CArray *ca;
1908
2178
  VALUE obj = rb_ca_ptr2obj_storage(self, ptr);
1909
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2179
+ GetCArray(self, ca);
1910
2180
  CA_FACE_STORAGE_TO_ELEMENT_IF_FACE(obj, self, ca);
1911
2181
  if ( ca->data_type == CA_BOOLEAN &&
1912
2182
  ( obj == INT2FIX(0) || obj == INT2FIX(1) ) ) {
@@ -1938,7 +2208,7 @@ ca_face_element_to_storage (VALUE self, CArray *ca, VALUE obj)
1938
2208
  if ( id_element_to_storage == 0 ) {
1939
2209
  id_element_to_storage = rb_intern("element_to_storage");
1940
2210
  }
1941
- if ( rb_respond_to(self, id_element_to_storage) ) {
2211
+ if ( rb_obj_respond_to(self, id_element_to_storage, Qtrue) ) {
1942
2212
  return rb_funcall(self, id_element_to_storage, 1, obj);
1943
2213
  }
1944
2214
  }
@@ -1946,11 +2216,11 @@ ca_face_element_to_storage (VALUE self, CArray *ca, VALUE obj)
1946
2216
  }
1947
2217
 
1948
2218
  VALUE
1949
- rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
2219
+ rb_ca_obj2ptr (volatile VALUE self, VALUE obj, void *ptr)
1950
2220
  {
1951
2221
  static CArray dummy;
1952
2222
  CArray *ca;
1953
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2223
+ GetCArray(self, ca);
1954
2224
  if ( obj == CA_UNDEF ) {
1955
2225
  memset(ptr, 0, ca->bytes);
1956
2226
  }
@@ -1965,6 +2235,9 @@ rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
1965
2235
  obj = ca_face_element_to_storage(self, ca, obj);
1966
2236
  convert_type = ca_storage_type_of(ca);
1967
2237
  }
2238
+ else {
2239
+ ca_refuse_non_number_value(convert_type, obj);
2240
+ }
1968
2241
  if ( convert_type == CA_FIXLEN ) {
1969
2242
  obj = rb_ca_data_class_encode(self, obj);
1970
2243
  }
@@ -1974,6 +2247,17 @@ rb_ca_obj2ptr (VALUE self, VALUE obj, void *ptr)
1974
2247
  return Qnil;
1975
2248
  }
1976
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
+
1977
2261
  VALUE
1978
2262
  rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1979
2263
  {
@@ -1982,7 +2266,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1982
2266
  int8_t data_type;
1983
2267
 
1984
2268
  if ( rb_obj_is_carray(obj) ) { /* obj == carray */
1985
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2269
+ GetCArray(obj, ca);
1986
2270
  if ( ca_is_readonly(ca) ) {
1987
2271
  ca_refuse_read_only();
1988
2272
  }
@@ -1997,13 +2281,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
1997
2281
  }
1998
2282
  }
1999
2283
  else if ( NIL_P(obj) ) { /* obj == nil */
2000
- if ( NIL_P(rtype) ) {
2001
- data_type = CA_OBJECT;
2002
- }
2003
- else {
2004
- data_type = rb_ca_guess_type(rtype);
2005
- }
2006
- obj = rb_cscalar_new(data_type, 0, NULL);
2284
+ ca_refuse_nil_data();
2007
2285
  }
2008
2286
  else if ( rb_respond_to(obj, rb_intern("to_ca")) ) { /* respond_to obj.to_ca */
2009
2287
  /* `writable: true` is the caller's half of the bargain: it says the
@@ -2011,16 +2289,15 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
2011
2289
  only produce a copy raises rather than swallowing them. An object
2012
2290
  whose to_ca predates the keyword raises ArgumentError here, which is
2013
2291
  the honest report that it does not implement writable intake. */
2014
- volatile VALUE kw = rb_hash_new();
2292
+ VALUE kw = rb_hash_new();
2015
2293
  rb_hash_aset(kw, ID2SYM(rb_intern("writable")), Qtrue);
2016
- obj = rb_funcallv_kw(obj, rb_intern("to_ca"), 1, (VALUE *) &kw,
2017
- RB_PASS_KEYWORDS);
2294
+ obj = rb_funcallv_kw(obj, rb_intern("to_ca"), 1, &kw, RB_PASS_KEYWORDS);
2018
2295
  if ( ! rb_obj_is_carray(obj) ) {
2019
2296
  volatile VALUE inspect = rb_inspect(CLASS_OF(arg));
2020
2297
  rb_raise(rb_eTypeError,
2021
2298
  "%s#to_ca did not return a CArray", StringValuePtr(inspect));
2022
2299
  }
2023
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2300
+ GetCArray(obj, ca);
2024
2301
  if ( ca_is_readonly(ca) ) {
2025
2302
  ca_refuse_read_only();
2026
2303
  }
@@ -2038,7 +2315,7 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
2038
2315
  else if ( rb_memory_view_available_p(obj) ) { /* MemoryView producer */
2039
2316
  obj = ca_mv_wrap(obj, Qnil, rb_cCAWrap, -1,
2040
2317
  NIL_P(rtype) ? -1 : rb_ca_guess_type(rtype), 1);
2041
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2318
+ GetCArray(obj, ca);
2042
2319
  if ( NIL_P(rtype) ) {
2043
2320
  data_type = ca->data_type;
2044
2321
  }
@@ -2067,8 +2344,12 @@ rb_ca_wrap_writable (VALUE arg, VALUE rtype)
2067
2344
  static VALUE
2068
2345
  rb_ca_s_wrap_writable (int argc, VALUE *argv, VALUE klass)
2069
2346
  {
2070
- volatile VALUE obj, rtype;
2071
- 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
+ }
2072
2353
  return rb_ca_wrap_writable(obj, rtype);
2073
2354
  }
2074
2355
 
@@ -2080,7 +2361,7 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2080
2361
  int8_t data_type;
2081
2362
 
2082
2363
  if ( rb_obj_is_carray(obj) ) { /* carray */
2083
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2364
+ GetCArray(obj, ca);
2084
2365
  if ( NIL_P(rtype) ) {
2085
2366
  data_type = ca->data_type;
2086
2367
  }
@@ -2101,8 +2382,11 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2101
2382
  obj = rb_cscalar_new_with_value(data_type, 0, obj);
2102
2383
  }
2103
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
+ }
2104
2388
  obj = rb_funcall(obj, rb_intern("to_ca"), 0);
2105
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2389
+ GetCArray(obj, ca);
2106
2390
  if ( NIL_P(rtype) ) {
2107
2391
  data_type = CA_OBJECT;
2108
2392
  }
@@ -2128,25 +2412,29 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2128
2412
  CArray *ca_tmp;
2129
2413
  volatile VALUE ref = obj;
2130
2414
  ca_size_t bytes = ca_sizeof[data_type];
2131
- CA_CHECK_BYTES_GIVEN(data_type, bytes);
2132
- 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;
2133
2427
  obj = rb_carray_new(data_type, 1, &dim, 0, NULL);
2134
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca_tmp);
2428
+ GetCArray(obj, ca_tmp);
2135
2429
  memcpy(ca_tmp->ptr, RSTRING_PTR(ref), dim * ca_sizeof[data_type]);
2136
2430
  }
2137
2431
  }
2138
2432
  else if ( NIL_P(obj) ) { /* nil */
2139
- if ( NIL_P(rtype) ) {
2140
- data_type = CA_OBJECT;
2141
- }
2142
- else {
2143
- data_type = rb_ca_guess_type(rtype);
2144
- }
2145
- obj = rb_cscalar_new(data_type, 0, NULL);
2433
+ ca_refuse_nil_data();
2146
2434
  }
2147
2435
  else if ( rb_respond_to(obj, rb_intern("to_ca")) ) {
2148
2436
  obj = rb_funcall(obj, rb_intern("to_ca"), 0);
2149
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2437
+ GetCArray(obj, ca);
2150
2438
  if ( NIL_P(rtype) ) {
2151
2439
  data_type = ca->data_type;
2152
2440
  }
@@ -2161,7 +2449,7 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2161
2449
  else if ( rb_memory_view_available_p(obj) ) { /* MemoryView producer */
2162
2450
  obj = ca_mv_wrap(obj, Qnil, rb_cCAWrap, -1,
2163
2451
  NIL_P(rtype) ? -1 : rb_ca_guess_type(rtype), 0);
2164
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
2452
+ GetCArray(obj, ca);
2165
2453
  if ( NIL_P(rtype) ) {
2166
2454
  data_type = ca->data_type;
2167
2455
  }
@@ -2193,8 +2481,12 @@ rb_ca_wrap_readonly (VALUE arg, VALUE rtype)
2193
2481
  static VALUE
2194
2482
  rb_ca_s_wrap_readonly (int argc, VALUE *argv, VALUE klass)
2195
2483
  {
2196
- volatile VALUE obj, rtype;
2197
- 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
+ }
2198
2490
  return rb_ca_wrap_readonly(obj, rtype);
2199
2491
  }
2200
2492
 
@@ -2258,7 +2550,7 @@ ca_fixlen_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2258
2550
  if ( ! RB_TYPE_P(*scalar, T_STRING) || ! rb_obj_is_carray(reference) ) {
2259
2551
  return 0;
2260
2552
  }
2261
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2553
+ GetCArray(reference, cr);
2262
2554
  if ( cr->data_type != CA_FIXLEN ) {
2263
2555
  return 0;
2264
2556
  }
@@ -2274,24 +2566,6 @@ ca_fixlen_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2274
2566
  return 1;
2275
2567
  }
2276
2568
 
2277
- /* UNDEF as the scalar side of an operation with an array: a masked
2278
- scalar of the array's own data_type and width. UNDEF has no value, so it
2279
- cannot decide the type of the result; read as a Ruby object it would make
2280
- an integer, boolean or fixlen operation answer an object array, while a
2281
- float one kept its type. An operation the data type does not take
2282
- (fixlen `+`) is refused as for any other operand. */
2283
- static int
2284
- ca_undef_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2285
- {
2286
- CArray *cr;
2287
- if ( *scalar != CA_UNDEF || ! rb_obj_is_carray(reference) ) {
2288
- return 0;
2289
- }
2290
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2291
- *scalar = rb_cscalar_new_with_value(cr->data_type, cr->bytes, CA_UNDEF);
2292
- return 1;
2293
- }
2294
-
2295
2569
  /* Where a Ruby Integer falls against the range of integer data type `dt`:
2296
2570
  -1 below it, 1 above it, 0 inside. Anything but an Integer, and a
2297
2571
  non-integer `dt`, answer 0. */
@@ -2323,19 +2597,29 @@ void
2323
2597
  ca_check_integer_fits (VALUE v, int8_t dt)
2324
2598
  {
2325
2599
  if ( ca_integer_range_side(v, dt) != 0 ) {
2326
- rb_raise(rb_eRangeError, "%"PRIsVALUE" is out of range for %s",
2327
- 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]);
2328
2602
  }
2329
2603
  }
2330
2604
 
2331
2605
  /* The store reader for an integer cell: the value as an Integer, refused
2332
- when it does not fit the cell's type, as an operator refuses it. A
2333
- store raises where an array cast (to_type) keeps the C conversion. A
2334
- 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). */
2335
2612
  VALUE
2336
- ca_obj2integer_fitting (VALUE v, int8_t data_type)
2613
+ rb_ca_obj2integer_fitting (VALUE v, int8_t data_type)
2337
2614
  {
2338
- 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");
2339
2623
  if ( FIXNUM_P(integer) ) {
2340
2624
  long x = FIX2LONG(integer);
2341
2625
  int fits;
@@ -2365,7 +2649,214 @@ ca_obj2integer_store (CArray *dst, VALUE v, int8_t data_type)
2365
2649
  if ( field ) {
2366
2650
  return ca_bitfield_wrap_integer(field, v);
2367
2651
  }
2368
- return ca_obj2integer_fitting(v, data_type);
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);
2369
2860
  }
2370
2861
 
2371
2862
  /* A Ruby Integer operand against an integer array is taken in the array's
@@ -2379,7 +2870,7 @@ ca_integer_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2379
2870
  if ( ! RB_INTEGER_TYPE_P(*scalar) || ! rb_obj_is_carray(reference) ) {
2380
2871
  return 0;
2381
2872
  }
2382
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2873
+ GetCArray(reference, cr);
2383
2874
  if ( ! ca_is_integer_type(cr) || ca_is_face(cr) ) {
2384
2875
  return 0;
2385
2876
  }
@@ -2388,67 +2879,34 @@ ca_integer_scalar_operand (volatile VALUE *scalar, volatile VALUE reference)
2388
2879
  return 1;
2389
2880
  }
2390
2881
 
2391
- /* A Ruby Array operand against a numeric or boolean array is read as its
2392
- cells, as the array CArray.wrap_readonly makes of it (nested Arrays give
2393
- its axes). Its data type is the
2394
- one the cells would take one at a time as scalar operands of the same
2395
- operation (an Integer
2396
- against an integer array takes the array's type and has to fit it),
2397
- promoted together. Against an object array, a Face or a fixlen array an
2398
- Array stays one value. */
2399
- static int
2400
- ca_array_operand (volatile VALUE *ary, volatile 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)
2401
2891
  {
2402
2892
  CArray *cr;
2403
- volatile VALUE flat, probe_self, probe;
2404
- long i, n;
2405
- int8_t type = -1, seen_fix = -1, seen_big = -1, seen_flo = -1, seen_cpx = -1,
2406
- seen_bool = -1;
2893
+ const char *type, *article, *example;
2407
2894
 
2408
- if ( ! RB_TYPE_P(*ary, T_ARRAY) || ! rb_obj_is_carray(reference) ) {
2409
- return 0;
2895
+ if ( ! RB_TYPE_P(ary, T_ARRAY) || ! rb_obj_is_carray(reference) ) {
2896
+ return;
2410
2897
  }
2411
- TypedData_Get_Struct(reference, CArray, &carray_data_type, cr);
2898
+ GetCArray(reference, cr);
2412
2899
  if ( ca_is_face(cr) || ca_is_object_type(cr) || ca_is_fixlen_type(cr) ) {
2413
- return 0;
2414
- }
2415
-
2416
- flat = rb_funcall(*ary, rb_intern("flatten"), 0);
2417
- n = RARRAY_LEN(flat);
2418
- for (i = 0; i < n; i++) {
2419
- VALUE e = RARRAY_AREF(flat, i);
2420
- int8_t *seen, t;
2421
- CArray *cp;
2422
- if ( FIXNUM_P(e) ) seen = &seen_fix;
2423
- else if ( RB_TYPE_P(e, T_BIGNUM) ) seen = &seen_big;
2424
- else if ( RB_FLOAT_TYPE_P(e) ) seen = &seen_flo;
2425
- else if ( RB_TYPE_P(e, T_COMPLEX) ) seen = &seen_cpx;
2426
- else if ( e == Qtrue || e == Qfalse ) seen = &seen_bool;
2427
- else seen = NULL;
2428
- if ( RB_INTEGER_TYPE_P(e) && ca_is_integer_type(cr) ) {
2429
- ca_check_integer_fits(e, cr->data_type);
2430
- }
2431
- if ( seen && *seen >= 0 ) {
2432
- continue;
2433
- }
2434
- probe_self = reference;
2435
- probe = e;
2436
- if ( both ) {
2437
- rb_ca_cast_self_or_other(&probe_self, &probe);
2438
- }
2439
- else {
2440
- rb_ca_cast_other((VALUE *) &probe_self, (volatile VALUE *) &probe);
2441
- }
2442
- TypedData_Get_Struct(probe, CArray, &carray_data_type, cp);
2443
- t = cp->data_type;
2444
- if ( seen ) *seen = t;
2445
- type = ( type < 0 ) ? t : ca_promote_type(type, t);
2446
- }
2447
- if ( type < 0 ) {
2448
- type = cr->data_type;
2900
+ return;
2449
2901
  }
2450
- *ary = rb_ca_wrap_readonly(*ary, INT2NUM(type));
2451
- 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);
2452
2910
  }
2453
2911
 
2454
2912
  /* CArray.cast(value) -- singleton entry delegating to rb_ca_cast, which
@@ -2514,6 +2972,16 @@ ca_value_foreign_to (CArray *ca, VALUE v)
2514
2972
  return 1;
2515
2973
  }
2516
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
+
2517
2985
  VALUE
2518
2986
  ca_truth_as_number (CArray *ca, VALUE v)
2519
2987
  {
@@ -2545,6 +3013,29 @@ ca_cell_class_name (CArray *ca)
2545
3013
  return "Integer";
2546
3014
  }
2547
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));
3037
+ }
3038
+
2548
3039
  void
2549
3040
  ca_refuse_foreign_operand (VALUE v, volatile VALUE array)
2550
3041
  {
@@ -2553,6 +3044,7 @@ ca_refuse_foreign_operand (VALUE v, volatile VALUE array)
2553
3044
  return;
2554
3045
  }
2555
3046
  GetCArray(array, ca);
3047
+ ca_refuse_undef_operand(v, ca, rb_eTypeError, 0);
2556
3048
  if ( ca_value_foreign_to(ca, v) ) {
2557
3049
  rb_raise(rb_eTypeError, "%s: %" PRIsVALUE " can't be coerced into %s",
2558
3050
  ca_calling_method_name(), ca_foreign_value_name(v),
@@ -2568,6 +3060,7 @@ ca_refuse_foreign_order (VALUE v, volatile VALUE array)
2568
3060
  return;
2569
3061
  }
2570
3062
  GetCArray(array, ca);
3063
+ ca_refuse_undef_operand(v, ca, rb_eArgError, 1);
2571
3064
  if ( ca_value_foreign_to(ca, v) ) {
2572
3065
  rb_raise(rb_eArgError, "%s: comparison of %s with %" PRIsVALUE " failed",
2573
3066
  ca_calling_method_name(), ca_cell_class_name(ca),
@@ -2588,6 +3081,22 @@ ca_truth_scalar_operand (volatile VALUE *scalar)
2588
3081
  return 0;
2589
3082
  }
2590
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
+
2591
3100
  void
2592
3101
  rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2593
3102
  {
@@ -2607,9 +3116,8 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2607
3116
  *other = ca_operand_from_memory_view(*other);
2608
3117
  }
2609
3118
 
2610
- if ( ! ca_array_operand(other, *self, 1) ) {
2611
- ca_array_operand(self, *other, 1);
2612
- }
3119
+ ca_refuse_array_operand(*other, *self);
3120
+ ca_refuse_array_operand(*self, *other);
2613
3121
 
2614
3122
  if ( ! rb_obj_is_carray(*self) ) {
2615
3123
  self_is_object = 1;
@@ -2618,17 +3126,14 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2618
3126
  *self = rb_cscalar_new_with_value(CA_OBJECT, 0, *self);
2619
3127
  }
2620
3128
  #ifdef HAVE_COMPLEX_H
2621
- /* A Complex scalar with a non-object non-complex CArray on the other
2622
- side must wrap as CA_CMPLX128; falling into the float branch below
2623
- would NUM2DBL(Complex) and raise. Downstream promotion lifts the
2624
- 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. */
2625
3133
  else if ( RB_TYPE_P(*self, T_COMPLEX) ) {
2626
- *self = rb_cscalar_new_with_value(CA_CMPLX128, 0, *self);
3134
+ *self = rb_cscalar_new_with_value(ca_complex_scalar_type(*other), 0, *self);
2627
3135
  }
2628
3136
  #endif
2629
- else if ( ca_undef_scalar_operand(self, *other) ) {
2630
- /* the array's own data_type; see the helper above */
2631
- }
2632
3137
  else if ( ca_fixlen_scalar_operand(self, *other) ) {
2633
3138
  /* width taken from the fixlen reference; see the helper above */
2634
3139
  }
@@ -2659,17 +3164,11 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2659
3164
  *other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
2660
3165
  }
2661
3166
  #ifdef HAVE_COMPLEX_H
2662
- /* Symmetric to the self-branch above: Complex scalar wraps as
2663
- cmplx128 regardless of the CArray operand's numeric width, so
2664
- float64_array + Complex(0, 1) promotes rather than raising in
2665
- NUM2DBL(Complex). */
3167
+ /* Symmetric to the self-branch above. */
2666
3168
  else if ( RB_TYPE_P(*other, T_COMPLEX) ) {
2667
- *other = rb_cscalar_new_with_value(CA_CMPLX128, 0, *other);
3169
+ *other = rb_cscalar_new_with_value(ca_complex_scalar_type(*self), 0, *other);
2668
3170
  }
2669
3171
  #endif
2670
- else if ( ca_undef_scalar_operand(other, *self) ) {
2671
- /* the array's own data_type; see the helper above */
2672
- }
2673
3172
  else if ( ca_fixlen_scalar_operand(other, *self) ) {
2674
3173
  /* width taken from the fixlen reference; see the helper above */
2675
3174
  }
@@ -2692,8 +3191,8 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2692
3191
  }
2693
3192
  }
2694
3193
 
2695
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
2696
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3194
+ GetCArray(*self, ca);
3195
+ GetCArray(*other, cb);
2697
3196
 
2698
3197
  /* Implicit size-1 broadcasting (case A only). No-op if either side
2699
3198
  is scalar, ndim differs, or shapes are already equal. If shapes
@@ -2701,8 +3200,8 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2701
3200
  no-op — the downstream element-count check in ca_set_iterator
2702
3201
  raises with the existing message. */
2703
3202
  ca_broadcast_pair(self, other);
2704
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
2705
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3203
+ GetCArray(*self, ca);
3204
+ GetCArray(*other, cb);
2706
3205
 
2707
3206
  if ( ca_is_scalar(ca) ^ ca_is_scalar(cb) ||
2708
3207
  self_is_object ||
@@ -2730,7 +3229,15 @@ rb_ca_cast_self_or_other (volatile VALUE *self, volatile VALUE *other)
2730
3229
  }
2731
3230
  }
2732
3231
  }
2733
-
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
+
2734
3241
  test = ca_cast_table[cb->data_type][ca->data_type];
2735
3242
 
2736
3243
  if ( test == 0 ) {
@@ -2785,11 +3292,11 @@ rb_ca_cast_three (volatile VALUE *v1, volatile VALUE *v2, volatile VALUE *v3)
2785
3292
  rb_ca_cast_self_or_other(v2, v3);
2786
3293
  rb_ca_cast_self_or_other(v1, v2);
2787
3294
 
2788
- TypedData_Get_Struct(*v1, CArray, &carray_data_type, ca);
3295
+ GetCArray(*v1, ca);
2789
3296
  common = ca->data_type;
2790
3297
  for ( i = 0; i < 3; i++ ) {
2791
3298
  if ( ! rb_obj_is_carray(original[i]) ) continue;
2792
- TypedData_Get_Struct(original[i], CArray, &carray_data_type, ca);
3299
+ GetCArray(original[i], ca);
2793
3300
  *cast[i] = ( ca->data_type != common && ! ca_is_face(ca) )
2794
3301
  ? rb_ca_wrap_readonly(original[i], INT2NUM(common))
2795
3302
  : original[i];
@@ -2806,11 +3313,11 @@ rb_ca_cast_three (volatile VALUE *v1, volatile VALUE *v2, volatile VALUE *v3)
2806
3313
  the kernel's data_types, so that a pair the eager operator refuses is
2807
3314
  refused the same way. */
2808
3315
  void
2809
- ca_refuse_without_common_type (VALUE self, VALUE other)
3316
+ ca_refuse_without_common_type (volatile VALUE self, volatile VALUE other)
2810
3317
  {
2811
3318
  CArray *ca, *cb;
2812
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
2813
- TypedData_Get_Struct(other, CArray, &carray_data_type, cb);
3319
+ GetCArray(self, ca);
3320
+ GetCArray(other, cb);
2814
3321
  if ( ca->data_type != cb->data_type
2815
3322
  && ca_cast_table[cb->data_type][ca->data_type] < 0
2816
3323
  && ca_cast_table[ca->data_type][cb->data_type] < 0 ) {
@@ -2827,7 +3334,7 @@ VALUE
2827
3334
  rb_ca_s_cast_self_or_other (VALUE klass, VALUE self, VALUE other)
2828
3335
  {
2829
3336
  rb_ca_cast_self_or_other(&self, &other);
2830
- return rb_assoc_new(self, other);
3337
+ return rb_ary_new_from_args(2, self, other);
2831
3338
  }
2832
3339
 
2833
3340
  /* Extract a data_type code (0..CA_NTYPE-1) from an argument.
@@ -2840,12 +3347,12 @@ rb_ca_s_cast_self_or_other (VALUE klass, VALUE self, VALUE other)
2840
3347
  as a value -> :int64, not as a data_type
2841
3348
  code). */
2842
3349
  static int8_t
2843
- ca_arg_to_data_type (VALUE obj)
3350
+ ca_arg_to_data_type (volatile VALUE obj)
2844
3351
  {
2845
3352
  int8_t data_type;
2846
3353
  if ( rb_obj_is_carray(obj) ) {
2847
3354
  CArray *ca;
2848
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
3355
+ GetCArray(obj, ca);
2849
3356
  data_type = ca->data_type;
2850
3357
  }
2851
3358
  else if ( TYPE(obj) == T_SYMBOL || TYPE(obj) == T_STRING ||
@@ -2860,15 +3367,15 @@ ca_arg_to_data_type (VALUE obj)
2860
3367
  /* A MemoryView producer whose format says nothing usable (typeless, the
2861
3368
  other byte order) is read as the operators read it, and refused in
2862
3369
  the same words. */
2863
- VALUE ca_obj = ca_operand_from_memory_view(obj);
3370
+ volatile VALUE ca_obj = ca_operand_from_memory_view(obj);
2864
3371
  CArray *ca;
2865
- TypedData_Get_Struct(ca_obj, CArray, &carray_data_type, ca);
3372
+ GetCArray(ca_obj, ca);
2866
3373
  data_type = ca->data_type;
2867
3374
  }
2868
3375
  else {
2869
3376
  data_type = ca_value_to_data_type(obj);
2870
3377
  }
2871
- CA_CHECK_DATA_TYPE_GIVEN(data_type);
3378
+ CA_CHECK_DATA_TYPE(data_type);
2872
3379
  if ( ! ca_valid[data_type] ) {
2873
3380
  rb_raise(rb_eArgError,
2874
3381
  "data type '%s' is not valid in this build",
@@ -2898,8 +3405,8 @@ ca_value_to_data_type (VALUE obj)
2898
3405
  }
2899
3406
 
2900
3407
  /* Promote two data_type codes according to ca_cast_table.
2901
- Returns the higher (= the type both can be cast to without loss
2902
- 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.
2903
3410
 
2904
3411
  Externally callable so the lazy view layer (CABinOp constructor)
2905
3412
  can compute its output_data_type using the same single-source
@@ -2914,6 +3421,13 @@ ca_promote_type (int8_t a, int8_t b)
2914
3421
  if ( a == b ) {
2915
3422
  return a;
2916
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
+ }
2917
3431
  test = ca_cast_table[a][b];
2918
3432
  if ( test == 0 ) {
2919
3433
  return b;
@@ -2978,13 +3492,13 @@ ca_value_match_type (int8_t t, int8_t a, int8_t b)
2978
3492
  by ca_value_match_type). Beside a reference that is not a number, a
2979
3493
  scalar query keeps the reference's type. */
2980
3494
  int8_t
2981
- ca_query_match_type (int8_t ref, VALUE query)
3495
+ ca_query_match_type (int8_t ref, volatile VALUE query)
2982
3496
  {
2983
3497
  int ref_int = ( ref >= CA_INT8 && ref <= CA_UINT64 );
2984
3498
  int ref_float = ( ref == CA_FLOAT32 || ref == CA_FLOAT64 );
2985
3499
  if ( rb_obj_is_carray(query) ) {
2986
3500
  CArray *q;
2987
- TypedData_Get_Struct(query, CArray, &carray_data_type, q);
3501
+ GetCArray(query, q);
2988
3502
  return ca_value_match_type(ca_promote_type(ref, q->data_type), ref,
2989
3503
  q->data_type);
2990
3504
  }
@@ -3005,23 +3519,23 @@ ca_query_match_type (int8_t ref, VALUE query)
3005
3519
  cut to the cell: "abX" matches no two-byte cell, as a wider scalar does
3006
3520
  not in `eq`. Anything else is returned as given. */
3007
3521
  VALUE
3008
- ca_fixlen_widen_for_query (VALUE self, VALUE query)
3522
+ ca_fixlen_widen_for_query (volatile VALUE self, VALUE query)
3009
3523
  {
3010
3524
  CArray *ca;
3011
3525
  if ( ! RB_TYPE_P(query, T_STRING) || ! rb_obj_is_carray(self) ) {
3012
3526
  return self;
3013
3527
  }
3014
- TypedData_Get_Struct(self, CArray, &carray_data_type, ca);
3528
+ GetCArray(self, ca);
3015
3529
  if ( ca->data_type != CA_FIXLEN || RSTRING_LEN(query) <= ca->bytes ) {
3016
3530
  return self;
3017
3531
  }
3018
- 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)));
3019
3533
  }
3020
3534
 
3021
3535
  /* ca.query_match_type(query) -- ca_query_match_type with self as the
3022
3536
  reference, for the set family and locate_addr. */
3023
3537
  static VALUE
3024
- rb_ca_query_match_type (VALUE self, VALUE query)
3538
+ rb_ca_query_match_type (volatile VALUE self, VALUE query)
3025
3539
  {
3026
3540
  CArray *ca;
3027
3541
  GetCArray(self, ca);
@@ -3031,7 +3545,7 @@ rb_ca_query_match_type (VALUE self, VALUE query)
3031
3545
  /* ca.value_match_type(t, other_type) -- ca_value_match_type with self's
3032
3546
  data type as the first operand, for the set family and locate_addr. */
3033
3547
  static VALUE
3034
- rb_ca_value_match_type (VALUE self, VALUE vt, VALUE vother)
3548
+ rb_ca_value_match_type (volatile VALUE self, VALUE vt, VALUE vother)
3035
3549
  {
3036
3550
  CArray *ca;
3037
3551
  GetCArray(self, ca);
@@ -3043,11 +3557,11 @@ rb_ca_value_match_type (VALUE self, VALUE vt, VALUE vother)
3043
3557
  true, ...) or a CScalar. The operators let a scalar take the type of the
3044
3558
  array beside it, so result_type does too. */
3045
3559
  static int
3046
- ca_arg_is_scalar (VALUE obj)
3560
+ ca_arg_is_scalar (volatile VALUE obj)
3047
3561
  {
3048
3562
  if ( rb_obj_is_carray(obj) ) {
3049
3563
  CArray *ca;
3050
- TypedData_Get_Struct(obj, CArray, &carray_data_type, ca);
3564
+ GetCArray(obj, ca);
3051
3565
  return ca_is_scalar(ca);
3052
3566
  }
3053
3567
  if ( TYPE(obj) == T_SYMBOL || TYPE(obj) == T_STRING || TYPE(obj) == T_CLASS ) {
@@ -3059,13 +3573,20 @@ ca_arg_is_scalar (VALUE obj)
3059
3573
  /* The type a scalar `obj` (of type `st`) and an array of type `at` share,
3060
3574
  by the rule the operators apply: a scalar of the same kind takes the
3061
3575
  array's type (0.1 beside float32 is float32, 1 beside int8 is int8); one
3062
- 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
3063
3578
  does not fit the array's integer type takes a type that holds it and the
3064
3579
  array's values (int64, or object where no integer type does), where an
3065
3580
  operator would raise RangeError. */
3066
3581
  int8_t
3067
3582
  ca_promote_scalar_type (int8_t at, VALUE obj, int8_t st)
3068
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
+ }
3069
3590
  if ( RB_INTEGER_TYPE_P(obj) && at >= CA_INT8 && at <= CA_UINT64 ) {
3070
3591
  int8_t vt;
3071
3592
  if ( ca_integer_range_side(obj, at) == 0 ) {
@@ -3078,6 +3599,12 @@ ca_promote_scalar_type (int8_t at, VALUE obj, int8_t st)
3078
3599
  : CA_OBJECT;
3079
3600
  return ca_value_match_type(ca_promote_type(at, vt), at, vt);
3080
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
3081
3608
  if ( ca_cast_table2[st][at] >= 0 ) {
3082
3609
  return at;
3083
3610
  }
@@ -3097,12 +3624,14 @@ static VALUE
3097
3624
  rb_ca_s_result_type (int argc, VALUE *argv, VALUE klass)
3098
3625
  {
3099
3626
  int8_t cur = -1;
3100
- int i;
3101
- VALUE rest, ropt;
3627
+ int i, has_array = 0;
3628
+ VALUE rest_arg, ropt;
3629
+ volatile VALUE rest;
3102
3630
 
3103
3631
  /* Keywords are refused; without this a keyword Hash would be classified
3104
3632
  as one more operand and the answer would turn into object. */
3105
- rb_scan_args(argc, argv, "*:", &rest, &ropt);
3633
+ rb_scan_args(argc, argv, "*:", &rest_arg, &ropt);
3634
+ rest = rest_arg;
3106
3635
  rb_reject_options(ropt);
3107
3636
  argc = (int) RARRAY_LEN(rest);
3108
3637
  argv = RARRAY_PTR(rest);
@@ -3111,20 +3640,32 @@ rb_ca_s_result_type (int argc, VALUE *argv, VALUE klass)
3111
3640
  rb_raise(rb_eArgError, "result_type requires at least one argument");
3112
3641
  }
3113
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. */
3114
3646
  for ( i = 0; i < argc; i++ ) {
3115
- 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]) ) {
3116
3651
  int8_t t = ca_arg_to_data_type(argv[i]);
3117
3652
  cur = ( cur < 0 ) ? t : ca_promote_type(cur, t);
3118
3653
  }
3119
3654
  }
3120
3655
  for ( i = 0; i < argc; i++ ) {
3121
- if ( ca_arg_is_scalar(argv[i]) ) {
3656
+ if ( ! RB_TYPE_P(argv[i], T_ARRAY) && ca_arg_is_scalar(argv[i]) ) {
3122
3657
  int8_t t = ca_arg_to_data_type(argv[i]);
3123
3658
  cur = ( cur < 0 ) ? t : ca_promote_scalar_type(cur, argv[i], t);
3124
3659
  }
3125
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
+ }
3126
3668
  /* argv points into rest, and classifying an argument can allocate */
3127
- RB_GC_GUARD(rest);
3128
3669
  /* Return Symbol for family consistency with
3129
3670
  CArray.value_to_data_type and ca.data_type. */
3130
3671
  return rb_ca_data_type_to_sym(cur);
@@ -3174,6 +3715,16 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
3174
3715
  rb_raise(rb_eArgError, "promote_list: list must not be empty");
3175
3716
  }
3176
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
+
3177
3728
  /* Explicit data_type validation: reject Class / non-Symbol targets up
3178
3729
  front so the rest of the function only deals with `nil` or a
3179
3730
  primitive-shaped Symbol/String/Integer. */
@@ -3190,10 +3741,10 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
3190
3741
  primitive. Reject if any Face in list. */
3191
3742
  if ( ! NIL_P(data_type) ) {
3192
3743
  for ( i = 0; i < n; i++ ) {
3193
- VALUE elem = rb_ary_entry(list, i);
3744
+ volatile VALUE elem = rb_ary_entry(list, i);
3194
3745
  CArray *ca;
3195
3746
  rb_check_carray_object(elem);
3196
- TypedData_Get_Struct(elem, CArray, &carray_data_type, ca);
3747
+ GetCArray(elem, ca);
3197
3748
  if ( ca_is_face(ca) ) {
3198
3749
  rb_raise(rb_eArgError,
3199
3750
  "promote_list: data_type %"PRIsVALUE" cannot be applied to a list "
@@ -3223,16 +3774,25 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
3223
3774
  return out;
3224
3775
  }
3225
3776
 
3226
- /* All primitive: result_type to determine common data_type, then
3227
- wrap_readonly each element. */
3228
- /* The arrays are joined value for value, so a list mixing signed and
3229
- unsigned integers takes a type that holds both (uint8 with int8 is
3230
- int16, not uint8, where -1 would read as 255): the rule equality
3231
- 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. */
3232
3783
  common = ca_arg_to_data_type(rb_ary_entry(list, 0));
3233
3784
  for ( i = 1; i < n; i++ ) {
3234
3785
  int8_t next = ca_arg_to_data_type(rb_ary_entry(list, i));
3235
- 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;
3236
3796
  }
3237
3797
  {
3238
3798
  VALUE common_sym = rb_ca_data_type_to_sym(common);
@@ -3249,13 +3809,13 @@ rb_ca_s_promote_list (int argc, VALUE *argv, VALUE klass)
3249
3809
  }
3250
3810
 
3251
3811
  void
3252
- rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3812
+ rb_ca_cast_other (volatile VALUE *self, volatile VALUE *other)
3253
3813
  {
3254
3814
  CArray *ca, *cb;
3255
3815
  CScalar *cs;
3256
3816
  int test0, test1;
3257
3817
 
3258
- TypedData_Get_Struct(*self, CArray, &carray_data_type, ca);
3818
+ GetCArray(*self, ca);
3259
3819
 
3260
3820
  /* Promote MemoryView-aware operand to CArray (CAWrap or CAStride).
3261
3821
  Excludes T_STRING which has its own legacy interpretation. */
@@ -3264,15 +3824,14 @@ rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3264
3824
  *other = ca_operand_from_memory_view(*other);
3265
3825
  }
3266
3826
 
3267
- ca_array_operand(other, *self, 0);
3827
+ ca_refuse_array_operand(*other, *self);
3268
3828
 
3269
3829
  if ( ! rb_obj_is_carray(*other) ) {
3270
3830
  ca_refuse_foreign_operand(*other, *self);
3271
3831
  if ( rb_ca_is_object_type(*self) ) {
3272
3832
  *other = rb_cscalar_new_with_value(CA_OBJECT, 0, *other);
3273
3833
  }
3274
- else if ( ca_undef_scalar_operand(other, *self) ||
3275
- ca_fixlen_scalar_operand(other, *self) ) {
3834
+ else if ( ca_fixlen_scalar_operand(other, *self) ) {
3276
3835
  return; /* already the receiver's data_type and width */
3277
3836
  }
3278
3837
  else if ( ca_truth_scalar_operand(other) ) {
@@ -3301,7 +3860,7 @@ rb_ca_cast_other (VALUE *self, volatile VALUE *other)
3301
3860
 
3302
3861
  }
3303
3862
 
3304
- TypedData_Get_Struct(*other, CArray, &carray_data_type, cb);
3863
+ GetCArray(*other, cb);
3305
3864
 
3306
3865
  test1 = ca_cast_table[cb->data_type][ca->data_type];
3307
3866
 
@@ -3335,7 +3894,7 @@ rb_ca_cast_with (VALUE self, VALUE other)
3335
3894
  else {
3336
3895
  rb_raise(rb_eTypeError, "first argument should be a carray");
3337
3896
  }
3338
- return rb_assoc_new(self, other);
3897
+ return rb_ary_new_from_args(2, self, other);
3339
3898
  }
3340
3899
 
3341
3900
  /* -----------------------------------------------------------------------
@@ -3352,7 +3911,7 @@ rb_ca_cast_with (VALUE self, VALUE other)
3352
3911
  ----------------------------------------------------------------------- */
3353
3912
 
3354
3913
  static VALUE
3355
- ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3914
+ ca_clip_float_to_integer (volatile VALUE self, int8_t to_dt)
3356
3915
  {
3357
3916
  CArray *ca;
3358
3917
  GetCArray(self, ca);
@@ -3362,7 +3921,7 @@ ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3362
3921
  volatile VALUE vout = rb_carray_new(to_dt, ca->ndim, ca->dim, 0, NULL);
3363
3922
  CArray *out;
3364
3923
  GetCArray(vout, out);
3365
- ca_attach(src); /* window: nothing raises inside */
3924
+ ca_attach(src); /* attach: nothing raises before the detach */
3366
3925
  double *sp = (double *) src->ptr;
3367
3926
  char *op = out->ptr;
3368
3927
  ca_size_t n = ca->elements;
@@ -3403,7 +3962,7 @@ ca_clip_float_to_integer (VALUE self, int8_t to_dt)
3403
3962
 
3404
3963
  #define CLIP_CAST_FUNC(name, to_dt, lo_expr, hi_expr, cast_func) \
3405
3964
  static VALUE \
3406
- rb_ca_##name (VALUE self) \
3965
+ rb_ca_##name (volatile VALUE self) \
3407
3966
  { \
3408
3967
  CArray *ca; \
3409
3968
  GetCArray(self, ca); \
@@ -3432,7 +3991,7 @@ CLIP_CAST_FUNC(clip_uint64, CA_UINT64, INT2NUM(0), ULL2NUM(1
3432
3991
 
3433
3992
  Array -> shape-guessed CArray, element-wise store
3434
3993
  CArray -> copy if same data_type, else #to_type
3435
- nil -> empty CArray
3994
+ nil -> refused (an empty array is CA_INT32([]))
3436
3995
  Range -> arange; a second argument, if given, is the step
3437
3996
  Numeric -> CScalar, or #to_ca when the object answers it
3438
3997
  String -> the whitespace / `,` / `;` / `_`->UNDEF parser, which reads
@@ -3462,19 +4021,20 @@ ca_cast_to_type1 (VALUE v, VALUE sym)
3462
4021
  return rb_ca_to_type_internal(1, list, v);
3463
4022
  }
3464
4023
 
4024
+ /* recursion: re-enters once, as object */
3465
4025
  static VALUE
3466
4026
  ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3467
4027
  {
3468
- VALUE v = ( argc >= 1 ) ? argv[0] : Qnil;
4028
+ volatile VALUE v = ( argc >= 1 ) ? argv[0] : Qnil;
3469
4029
  VALUE sym = ID2SYM(ca_data_type_sym[data_type]);
3470
4030
 
3471
4031
  /* Only the Range branch below reads a second argument (the step), and
3472
4032
  nothing reads a third. The arity is -1 for the step's sake, so the count
3473
4033
  is checked here; without it `CA_INT32(0, 2)`, which looks like the shape
3474
4034
  spelling `CArray.int32(3, 3)`, casts the 0 and drops the 2. */
3475
- if ( argc > 2 ) {
4035
+ if ( argc < 1 || argc > 2 ) {
3476
4036
  rb_raise(rb_eArgError,
3477
- "wrong number of arguments (given %d, expected 0..2)", argc);
4037
+ "wrong number of arguments (given %d, expected 1..2)", argc);
3478
4038
  }
3479
4039
  if ( argc == 2 && ! RTEST(rb_obj_is_kind_of(v, rb_cRange)) ) {
3480
4040
  rb_raise(rb_eArgError,
@@ -3492,15 +4052,14 @@ ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3492
4052
  }
3493
4053
  else if ( RTEST(rb_obj_is_kind_of(v, rb_cCArray)) ) {
3494
4054
  CArray *ca;
3495
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
4055
+ GetCArray(v, ca);
3496
4056
  if ( ca->data_type == data_type ) {
3497
4057
  return rb_ca_copy(v);
3498
4058
  }
3499
4059
  return ca_cast_to_type1(v, sym);
3500
4060
  }
3501
4061
  else if ( NIL_P(v) ) {
3502
- ca_size_t dim0 = 0;
3503
- return rb_carray_new(data_type, 1, &dim0, 0, NULL);
4062
+ ca_refuse_nil_data();
3504
4063
  }
3505
4064
  else if ( RTEST(rb_obj_is_kind_of(v, rb_cRange)) ) {
3506
4065
  /* arange: n = ((end - begin).abs / step).floor + 1, or for an excluded
@@ -3548,9 +4107,9 @@ ca_cast_impl (int8_t data_type, int argc, VALUE *argv)
3548
4107
  else {
3549
4108
  /* Numeric / #to_ca responder */
3550
4109
  if ( rb_respond_to(v, id_to_ca_cast) ) {
3551
- VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
4110
+ volatile VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
3552
4111
  CArray *cap;
3553
- TypedData_Get_Struct(ca, CArray, &carray_data_type, cap);
4112
+ GetCArray(ca, cap);
3554
4113
  return ( cap->data_type == data_type ) ? ca : ca_cast_to_type1(ca, sym);
3555
4114
  }
3556
4115
  return rb_cscalar_new_with_value(data_type, 0, v);
@@ -3598,20 +4157,22 @@ ca_cast_to_fixlen (VALUE v, VALUE fixlen_sym, VALUE bytes_v)
3598
4157
  Ruby, unlike the numeric casts).
3599
4158
  Array -> bytes defaults to max top-level element #length; new + store
3600
4159
  CArray -> copy if already fixlen, else #to_type(:fixlen, bytes:)
3601
- nil -> empty fixlen (bytes 0 when omitted, matching the old default)
4160
+ nil -> refused (an empty array is CA_FIXLEN([]))
3602
4161
  other -> #to_ca coerce, else CScalar.new(:fixlen, bytes:) { data } */
3603
4162
  static VALUE
3604
4163
  rb_ca_cast_fixlen (int argc, VALUE *argv, VALUE self)
3605
4164
  {
3606
- VALUE v, opts = Qnil, bytes_v = Qnil;
4165
+ VALUE v_arg, opts = Qnil, bytes_v = Qnil;
4166
+ volatile VALUE v;
3607
4167
  VALUE fixlen_sym = ID2SYM(ca_data_type_sym[CA_FIXLEN]);
3608
4168
 
3609
- rb_scan_args(argc, argv, "11", &v, &opts);
4169
+ rb_scan_args(argc, argv, "11", &v_arg, &opts);
3610
4170
  rb_scan_options(opts, "bytes", &bytes_v);
4171
+ v = v_arg;
3611
4172
 
3612
4173
  if ( RTEST(rb_obj_is_kind_of(v, rb_cCArray)) ) {
3613
4174
  CArray *ca;
3614
- TypedData_Get_Struct(v, CArray, &carray_data_type, ca);
4175
+ GetCArray(v, ca);
3615
4176
  if ( ca->data_type == CA_FIXLEN ) {
3616
4177
  return rb_ca_copy(v);
3617
4178
  }
@@ -3643,15 +4204,13 @@ rb_ca_cast_fixlen (int argc, VALUE *argv, VALUE self)
3643
4204
  return obj;
3644
4205
  }
3645
4206
  else if ( NIL_P(v) ) {
3646
- ca_size_t dim0 = 0;
3647
- ca_size_t bytes = NIL_P(bytes_v) ? 1 : ca_integer_arg(bytes_v, "bytes", NULL);
3648
- return rb_carray_new(CA_FIXLEN, 1, &dim0, bytes, NULL);
4207
+ ca_refuse_nil_data();
3649
4208
  }
3650
4209
  else {
3651
4210
  if ( rb_respond_to(v, id_to_ca_cast) ) {
3652
- VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
4211
+ volatile VALUE ca = rb_funcall(v, id_to_ca_cast, 0);
3653
4212
  CArray *cap;
3654
- TypedData_Get_Struct(ca, CArray, &carray_data_type, cap);
4213
+ GetCArray(ca, cap);
3655
4214
  return ( cap->data_type == CA_FIXLEN )
3656
4215
  ? ca : ca_cast_to_fixlen(ca, fixlen_sym, bytes_v);
3657
4216
  }
@@ -3701,22 +4260,28 @@ rb_ca_read_text_as_number (int argc, VALUE *argv, VALUE self)
3701
4260
  return Qnil; /* not a number type: the caller casts */
3702
4261
  }
3703
4262
 
3704
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4263
+ GetCArray(src, ca);
3705
4264
  if ( ca->data_type != CA_OBJECT ) {
3706
4265
  rb_raise(rb_eArgError, "decimal reading needs an object array");
3707
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
+ }
3708
4273
  if ( ! ca_is_entity(ca) || ! ca->ptr ) {
3709
4274
  src = rb_ca_copy(src);
3710
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4275
+ GetCArray(src, ca);
3711
4276
  }
3712
4277
 
3713
4278
  out = rb_carray_new(data_type, ca->ndim, ca->dim, 0, NULL);
3714
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
4279
+ GetCArray(out, co);
3715
4280
  vmask = rb_carray_new(CA_BOOLEAN, ca->ndim, ca->dim, 0, NULL);
3716
4281
 
3717
4282
  cells = (VALUE *) ca->ptr;
3718
4283
  m_in = ca_has_mask(ca) ? (boolean8_t *) ca->mask->ptr : NULL;
3719
- TypedData_Get_Struct(vmask, CArray, &carray_data_type, cmask);
4284
+ GetCArray(vmask, cmask);
3720
4285
  m_out = (boolean8_t *) cmask->ptr;
3721
4286
 
3722
4287
  for (i = 0; i < ca->elements; i++) {
@@ -3764,8 +4329,6 @@ rb_ca_read_text_as_number (int argc, VALUE *argv, VALUE self)
3764
4329
 
3765
4330
  ca_mask_from_bytes(co, m_out);
3766
4331
  rb_ca_inherit_attr(out, self);
3767
- RB_GC_GUARD(src);
3768
- RB_GC_GUARD(vmask);
3769
4332
  return out;
3770
4333
  }
3771
4334
 
@@ -3838,19 +4401,25 @@ rb_ca_infer_number_type_of_text (VALUE self)
3838
4401
  ca_size_t i;
3839
4402
  int present = 0, any_float = 0;
3840
4403
 
3841
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4404
+ GetCArray(src, ca);
3842
4405
  if ( ca->data_type != CA_OBJECT ) {
3843
4406
  rb_raise(rb_eArgError, "type inference needs an object array");
3844
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
+ }
3845
4414
  if ( ! ca_is_entity(ca) || ! ca->ptr ) {
3846
4415
  src = rb_ca_copy(src);
3847
- TypedData_Get_Struct(src, CArray, &carray_data_type, ca);
4416
+ GetCArray(src, ca);
3848
4417
  }
3849
4418
  cells = (VALUE *) ca->ptr;
3850
4419
  m = ca_has_mask(ca) ? (boolean8_t *) ca->mask->ptr : NULL;
3851
4420
 
3852
4421
  for (i = 0; i < ca->elements; i++) {
3853
- VALUE v = cells[i];
4422
+ volatile VALUE v = cells[i];
3854
4423
  int kind;
3855
4424
  if ( ( m && m[i] ) || NIL_P(v) ) {
3856
4425
  continue;
@@ -3872,7 +4441,6 @@ rb_ca_infer_number_type_of_text (VALUE self)
3872
4441
  kind = CA_INFER_TEXT;
3873
4442
  }
3874
4443
  if ( kind == CA_INFER_TEXT ) {
3875
- RB_GC_GUARD(src);
3876
4444
  return Qnil;
3877
4445
  }
3878
4446
  if ( kind != CA_INFER_MISSING ) {
@@ -3880,7 +4448,6 @@ rb_ca_infer_number_type_of_text (VALUE self)
3880
4448
  any_float |= ( kind == CA_INFER_FLOAT );
3881
4449
  }
3882
4450
  }
3883
- RB_GC_GUARD(src);
3884
4451
  if ( ! present ) {
3885
4452
  return Qnil;
3886
4453
  }
@@ -3892,22 +4459,23 @@ rb_ca_infer_number_type_of_text (VALUE self)
3892
4459
  __infer_number_type_of_text__ / __read_text_as_number__ call them, so
3893
4460
  CAFrame#infer_types and #cast read a text column from a file this way. */
3894
4461
 
3895
- /* The buffer of a CAConstString and an entity of its (start, end) byte
3896
- 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. */
3897
4465
  static VALUE
3898
- ca_const_string_buffer_and_ranges (VALUE cs, VALUE *buffer, CArray **ranges)
4466
+ ca_const_string_buffer_and_ranges (VALUE cs, volatile VALUE *buffer)
3899
4467
  {
3900
4468
  volatile VALUE vranges;
4469
+ CArray *ranges;
3901
4470
  VALUE klass = rb_const_get(rb_cObject, rb_intern("CAConstString"));
3902
4471
  if ( ! RTEST(rb_obj_is_kind_of(cs, klass)) ) {
3903
4472
  rb_raise(rb_eTypeError, "needs a CAConstString");
3904
4473
  }
3905
4474
  *buffer = rb_funcall(cs, rb_intern("buffer"), 0);
3906
4475
  vranges = rb_ca_parent(cs);
3907
- TypedData_Get_Struct(vranges, CArray, &carray_data_type, *ranges);
3908
- if ( ! ca_is_entity(*ranges) || ! (*ranges)->ptr ) {
4476
+ GetCArray(vranges, ranges);
4477
+ if ( ! ca_is_entity(ranges) || ! ranges->ptr ) {
3909
4478
  vranges = rb_ca_copy(vranges);
3910
- TypedData_Get_Struct(vranges, CArray, &carray_data_type, *ranges);
3911
4479
  }
3912
4480
  return vranges;
3913
4481
  }
@@ -3916,7 +4484,7 @@ static VALUE
3916
4484
  rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3917
4485
  {
3918
4486
  volatile VALUE src;
3919
- VALUE buffer;
4487
+ volatile VALUE buffer;
3920
4488
  CArray *pe;
3921
4489
  const char *buf;
3922
4490
  const int64_t *pair;
@@ -3924,7 +4492,8 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3924
4492
  ca_size_t i;
3925
4493
  int present = 0, any_float = 0;
3926
4494
 
3927
- src = ca_const_string_buffer_and_ranges(cs, &buffer, &pe);
4495
+ src = ca_const_string_buffer_and_ranges(cs, &buffer);
4496
+ GetCArray(src, pe);
3928
4497
  buf = RSTRING_PTR(buffer);
3929
4498
  pair = (const int64_t *) pe->ptr;
3930
4499
  m = ca_has_mask(pe) ? (boolean8_t *) pe->mask->ptr : NULL;
@@ -3935,7 +4504,6 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3935
4504
  }
3936
4505
  kind = ca_infer_decimal_kind_of_bytes(buf + pair[2 * i], (long) ( pair[2 * i + 1] - pair[2 * i] ));
3937
4506
  if ( kind == CA_INFER_TEXT ) {
3938
- RB_GC_GUARD(src);
3939
4507
  return Qnil;
3940
4508
  }
3941
4509
  if ( kind != CA_INFER_MISSING ) {
@@ -3943,8 +4511,6 @@ rb_ca_s_const_string_infer_number_type_of_text (VALUE klass, VALUE cs)
3943
4511
  any_float |= ( kind == CA_INFER_FLOAT );
3944
4512
  }
3945
4513
  }
3946
- RB_GC_GUARD(src);
3947
- RB_GC_GUARD(buffer);
3948
4514
  if ( ! present ) {
3949
4515
  return Qnil;
3950
4516
  }
@@ -3955,7 +4521,7 @@ static VALUE
3955
4521
  rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VALUE report)
3956
4522
  {
3957
4523
  volatile VALUE src, out;
3958
- VALUE buffer;
4524
+ volatile VALUE buffer;
3959
4525
  CArray *pe, *co;
3960
4526
  int8_t data_type;
3961
4527
  const char *buf;
@@ -3975,9 +4541,10 @@ rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VA
3975
4541
  default:
3976
4542
  return Qnil; /* not a number type: the caller casts */
3977
4543
  }
3978
- src = ca_const_string_buffer_and_ranges(cs, &buffer, &pe);
4544
+ src = ca_const_string_buffer_and_ranges(cs, &buffer);
4545
+ GetCArray(src, pe);
3979
4546
  out = rb_carray_new(data_type, pe->ndim, pe->dim, 0, NULL);
3980
- TypedData_Get_Struct(out, CArray, &carray_data_type, co);
4547
+ GetCArray(out, co);
3981
4548
  buf = RSTRING_PTR(buffer);
3982
4549
  pair = (const int64_t *) pe->ptr;
3983
4550
  m_in = ca_has_mask(pe) ? (boolean8_t *) pe->mask->ptr : NULL;
@@ -4002,11 +4569,36 @@ rb_ca_s_const_string_read_text_as_number (VALUE klass, VALUE cs, VALUE rtype, VA
4002
4569
  }
4003
4570
  }
4004
4571
  }
4005
- RB_GC_GUARD(src);
4006
- RB_GC_GUARD(buffer);
4007
4572
  return out;
4008
4573
  }
4009
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
+
4010
4602
  void
4011
4603
  Init_carray_cast (void)
4012
4604
  {
@@ -4023,7 +4615,7 @@ Init_carray_cast (void)
4023
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);
4024
4616
  rb_define_singleton_method(rb_cCArray, "__const_string_read_text_as_number__", rb_ca_s_const_string_read_text_as_number, 3);
4025
4617
 
4026
- 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);
4027
4619
 
4028
4620
  rb_define_method(rb_cCArray, "fixlen", rb_ca_to_fixlen, -1);
4029
4621
  rb_define_method(rb_cCArray, "boolean", rb_ca_to_boolean, 0);
@@ -4041,15 +4633,7 @@ Init_carray_cast (void)
4041
4633
  rb_define_method(rb_cCArray, "cmplx128", rb_ca_to_cmplx128, 0);
4042
4634
  rb_define_method(rb_cCArray, "object", rb_ca_to_VALUE, 0);
4043
4635
 
4044
- rb_define_alias(rb_cCArray, "byte", "uint8");
4045
- rb_define_alias(rb_cCArray, "short", "int16");
4046
- rb_define_alias(rb_cCArray, "int", "int32");
4047
- rb_define_alias(rb_cCArray, "float", "float32");
4048
- rb_define_alias(rb_cCArray, "double", "float64");
4049
- rb_define_alias(rb_cCArray, "complex", "cmplx64");
4050
- rb_define_alias(rb_cCArray, "dcomplex", "cmplx128");
4051
-
4052
- 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);
4053
4637
 
4054
4638
  rb_define_method(rb_cCArray, "as_fixlen", rb_ca_as_fixlen, -1);
4055
4639
  rb_define_method(rb_cCArray, "as_boolean", rb_ca_as_boolean, 0);
@@ -4063,20 +4647,10 @@ Init_carray_cast (void)
4063
4647
  rb_define_method(rb_cCArray, "as_uint64", rb_ca_as_uint64, 0);
4064
4648
  rb_define_method(rb_cCArray, "as_float32", rb_ca_as_float32, 0);
4065
4649
  rb_define_method(rb_cCArray, "as_float64", rb_ca_as_float64, 0);
4066
- rb_define_method(rb_cCArray, "as_float128", rb_ca_as_float128, 0);
4067
4650
  rb_define_method(rb_cCArray, "as_cmplx64", rb_ca_as_cmplx64, 0);
4068
4651
  rb_define_method(rb_cCArray, "as_cmplx128", rb_ca_as_cmplx128, 0);
4069
- rb_define_method(rb_cCArray, "as_cmplx256", rb_ca_as_cmplx256, 0);
4070
4652
  rb_define_method(rb_cCArray, "as_object", rb_ca_as_VALUE, 0);
4071
4653
 
4072
- rb_define_alias(rb_cCArray, "as_byte", "as_uint8");
4073
- rb_define_alias(rb_cCArray, "as_short", "as_int16");
4074
- rb_define_alias(rb_cCArray, "as_int", "as_int32");
4075
- rb_define_alias(rb_cCArray, "as_float", "as_float32");
4076
- rb_define_alias(rb_cCArray, "as_double", "as_float64");
4077
- rb_define_alias(rb_cCArray, "as_complex", "as_cmplx64");
4078
- rb_define_alias(rb_cCArray, "as_dcomplex", "as_cmplx128");
4079
-
4080
4654
  rb_define_singleton_method(rb_cCArray,
4081
4655
  "wrap_writable", rb_ca_s_wrap_writable, -1);
4082
4656
  rb_define_singleton_method(rb_cCArray,
@@ -4134,26 +4708,18 @@ Init_carray_cast (void)
4134
4708
  rb_define_global_function("CA_CMPLX64", rb_ca_cast_cmplx64, -1);
4135
4709
  rb_define_global_function("CA_CMPLX128", rb_ca_cast_cmplx128, -1);
4136
4710
  rb_define_global_function("CA_OBJECT", rb_ca_cast_object, -1);
4137
- /* type-name aliases */
4138
- rb_define_global_function("CA_BYTE", rb_ca_cast_uint8, -1);
4139
- rb_define_global_function("CA_SHORT", rb_ca_cast_int16, -1);
4140
- rb_define_global_function("CA_INT", rb_ca_cast_int32, -1);
4141
- rb_define_global_function("CA_FLOAT", rb_ca_cast_float32, -1);
4142
- rb_define_global_function("CA_DOUBLE", rb_ca_cast_float64, -1);
4143
- rb_define_global_function("CA_COMPLEX", rb_ca_cast_cmplx64, -1);
4144
- rb_define_global_function("CA_DCOMPLEX", rb_ca_cast_cmplx128, -1);
4145
4711
  rb_define_global_function("CA_SIZE", rb_ca_cast_size, -1);
4146
4712
  rb_define_global_function("CA_FIXLEN", rb_ca_cast_fixlen, -1);
4147
4713
 
4148
4714
  rb_define_method(rb_cCArray, "cast_with", rb_ca_cast_with, 1);
4149
4715
 
4150
- rb_define_method(rb_cCArray, "clip_int8", rb_ca_clip_int8, 0);
4151
- rb_define_method(rb_cCArray, "clip_int16", rb_ca_clip_int16, 0);
4152
- rb_define_method(rb_cCArray, "clip_int32", rb_ca_clip_int32, 0);
4153
- rb_define_method(rb_cCArray, "clip_int64", rb_ca_clip_int64, 0);
4154
- rb_define_method(rb_cCArray, "clip_uint8", rb_ca_clip_uint8, 0);
4155
- rb_define_method(rb_cCArray, "clip_uint16", rb_ca_clip_uint16, 0);
4156
- rb_define_method(rb_cCArray, "clip_uint32", rb_ca_clip_uint32, 0);
4157
- 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);
4158
4724
  }
4159
4725