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
@@ -1,132 +1,9 @@
1
1
  class CArray
2
2
 
3
- # @overload is_in(values)
4
- # Returns a boolean CArray of the same shape as `self`, `true` at
5
- # each cell whose value appears in the set `values`.
6
- #
7
- # `values` is treated as a set, not as an operand to broadcast: it
8
- # may be any shape (or an Array) and is flattened to a
9
- # single seen-set, so its shape need not match `self`. Only one
10
- # argument is accepted; to test a few immediate values pass an
11
- # Array (`a.is_in([0, -1])`). A single value is a set of one, as
12
- # `[value]` is (`a.is_in(2)` is `a.is_in([2])`).
13
- #
14
- # When `self` and `values` have different numeric data types they are
15
- # promoted to a common type first (the same promotion binops use,
16
- # {CArray.result_type}), so membership is value-correct across
17
- # data types (e.g. an int cell equals a float set element of the same
18
- # value, and a fractional set element never truncates onto an int
19
- # cell). Integers of mixed sign are matched by value, as {#eq}
20
- # compares them: `-1` in an int8 array never matches `255` in a uint8
21
- # one, and the set operations answer such a pair in a type that holds
22
- # both (int16 for uint8 with int8, object for uint64 with int64).
23
- # Genuinely incompatible data types (e.g. numeric vs fixlen) raise.
24
- #
25
- # Membership is value-based and shares the distinctness of the
26
- # value-hash discovery family ({#unique} / {#value_counts}):
27
- # numeric follows `==` with all NaN collapsed to one value and
28
- # -0.0 == +0.0; `CA_OBJECT` follows Ruby `hash` / `eql?` with Float
29
- # NaN collapsed; `CA_FIXLEN` follows byte equality over the whole
30
- # cell, with a short String in the set padded out to the cell width
31
- # (so a 5-byte array written from `"be"` is `is_in(["be"])`). A
32
- # `CA_FIXLEN` set given as a CArray must already be of that width.
33
- #
34
- # An object array keeps Ruby's own split between its two kinds of
35
- # equality. A lookup through a hash -- `is_in`, the set operations,
36
- # {#locate_addr}, {#unique} and the rest of the discovery family --
37
- # uses `eql?`, as `Array#uniq`, `Array#&` and `Set` do, so `1` and
38
- # `1.0` are different values there. A cell-by-cell comparison --
39
- # {#eq}, {#count}, {#search}, {#bsearch} -- uses `==`, as
40
- # `Array#include?` does, so they are equal there. To match by value
41
- # across Integer and Float, convert the array to a numeric type first.
42
- #
43
- # Masked cells of `values` do not enter the set, and neither does `nil`
44
- # unless `self` is an `:object` array, which can hold it. Masked cells
45
- # of `self` stay masked in the result (membership is unknown), so
46
- # `is_in` propagates `self`'s mask like an element-wise comparison.
47
- #
48
- # For a per-fiber "does this fiber contain any of these values"
49
- # reduction, compose with {#any}: `a.is_in(values).any(axis: k)`.
50
- #
51
- # @param values [CArray, Array] the set to test membership
52
- # against. Promoted with `self` to a common data type.
53
- # @raise [ArgumentError] for a Range, which reads as an interval rather
54
- # than the set of its steps.
55
- # Between two time arrays the question is about instants, not ticks:
56
- # `values` is reconciled into `self`'s unit first, so a `:D` array and an
57
- # `:h` array match on the instants they share. The same holds for the set
58
- # operations below, whose results come back as `self`'s own type.
59
- #
60
- # @return [CArray] boolean CArray of the same shape as `self`.
61
- def is_in (values)
62
- refuse_range_set(values, "is_in")
63
- a, b = promote_value_set(values_of_set(values))
64
- a.__send__(:__is_in__, b)
65
- end
66
-
67
- # @overload intersection(other, sort: false)
68
- # Returns a 1-D CArray of the distinct values appearing in both
69
- # `self` and `other`, in `self`'s first-appearance order.
70
- #
71
- # Value-based, sharing the distinctness of the discovery family
72
- # (see {#is_in}); `self` and `other` are promoted to a common data type.
73
- # Masked cells of either array do not participate. The result is
74
- # always flat, like {#unique}, because the distinct values of a
75
- # fiber vary in number.
76
- #
77
- # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
78
- # @param sort [Boolean] when true, return the values sorted
79
- # ascending instead of in first-appearance order.
80
- # @return [CArray] 1-D CArray of the common distinct values.
81
- def intersection (other, sort: false)
82
- refuse_range_set(other, "intersection")
83
- a, b = promote_value_set(other)
84
- r = a.__send__(:__intersection__, b)
85
- r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with)
86
- sort ? r.sort : r
87
- end
88
-
89
- # @overload difference(other, sort: false)
90
- # Returns a 1-D CArray of the distinct values in `self` that are
91
- # absent from `other`, in `self`'s first-appearance order.
92
- # See {#intersection} for the shared semantics and options.
93
- #
94
- # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
95
- # @param sort [Boolean] when true, return the values sorted ascending.
96
- # @return [CArray] 1-D CArray of the self-only distinct values.
97
- def difference (other, sort: false)
98
- refuse_range_set(other, "difference")
99
- a, b = promote_value_set(other)
100
- r = a.__send__(:__difference__, b)
101
- r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with)
102
- sort ? r.sort : r
103
- end
104
-
105
- # @overload union(other, sort: false)
106
- # Returns a 1-D CArray of the distinct values appearing in either
107
- # `self` or `other`, in self-then-other first-appearance order.
108
- # See {#intersection} for the shared semantics and options. Two time
109
- # arrays of different units give their union in the finer unit, which
110
- # holds the values of both ({#intersection} and {#difference} answer in
111
- # `self`'s unit, since their values are `self`'s).
112
- #
113
- # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
114
- # @param sort [Boolean] when true, return the values sorted ascending
115
- # (a merged, ordered set — e.g. a common time axis).
116
- # @return [CArray] 1-D CArray of the combined distinct values.
117
- def union (other, sort: false)
118
- refuse_range_set(other, "union")
119
- a, b = promote_value_set(other)
120
- r = a.__send__(:__union__, b)
121
- sort ? r.sort : r
122
- end
123
-
124
- private
125
-
126
3
  # A Range reads as an interval in Ruby, which a set of its steps is not
127
4
  # (2.5 is not in [2, 3, 4]), so the set-valued verbs refuse one rather
128
5
  # than answer for the steps.
129
- def refuse_range_set (values, verb)
6
+ private def refuse_range_set(values, verb)
130
7
  return unless values.is_a?(Range)
131
8
  raise ArgumentError,
132
9
  "#{verb}: a Range is not taken as a set; pass its values as an Array " \
@@ -136,7 +13,7 @@ class CArray
136
13
  # The set as the array reads it. nil is no value of an array that can not
137
14
  # hold it, so it enters no set, and true and false are the numbers 1 and 0
138
15
  # to an integer, float or complex array, as they are to eq.
139
- def values_of_set (values)
16
+ private def values_of_set(values)
140
17
  return values if data_type == CA_OBJECT
141
18
  numeric = !face? && (integer? || float? || complex?)
142
19
  case values
@@ -166,10 +43,14 @@ class CArray
166
43
  # is not eql? Integer 2). A CArray argument uses its own data type; any other
167
44
  # operand (Numo, a MemoryView producer, ...) comes in through wrap_readonly,
168
45
  # the canonical type-coercion entry, so its format's data type drives the promote.
169
- def promote_value_set (values)
46
+ #
47
+ # With `lookup: true` (is_in) a value of a time set that no cell can be
48
+ # matches nothing (comparable_pair's lookup:); the set operations, whose
49
+ # results hold the values, refuse it.
50
+ private def promote_value_set(values, lookup: false)
170
51
  # Two time arrays of different units meet in the finer one.
171
52
  if values.is_a?(CArray) && values.class == self.class &&
172
- respond_to?(:in_common_unit_with)
53
+ respond_to?(:in_common_unit_with, true)
173
54
  return in_common_unit_with(values)
174
55
  end
175
56
  values = values_for_lookup(values) if values.is_a?(CArray)
@@ -180,27 +61,27 @@ class CArray
180
61
  # A Face that reconciles operands (CATime) reads a bare Array as values
181
62
  # of its own kind -- instants written as Time / String / elements -- and
182
63
  # meets them as a comparison does (comparable_pair).
183
- if values.is_a?(Array) && face? && respond_to?(:comparable_pair)
184
- return comparable_pair(values)
64
+ if values.is_a?(Array) && face? && respond_to?(:comparable_pair, true)
65
+ return comparable_pair(values, lookup: lookup)
185
66
  end
186
67
  # A single value (a Time, an element, a bare number) is a set of one,
187
68
  # read the same way; a bare number is refused like any other operand
188
69
  # that is not of its kind.
189
70
  if !values.is_a?(CArray) && !values.is_a?(Range) && face? &&
190
- respond_to?(:comparable_pair)
191
- return comparable_pair([values])
71
+ respond_to?(:comparable_pair, true)
72
+ return comparable_pair([values], lookup: lookup)
192
73
  end
193
74
  # ...and so does an array of values that are not of its kind (an object
194
75
  # column of Time values, a string column).
195
- if values.is_a?(CArray) && respond_to?(:comparable_pair) &&
196
- !values.respond_to?(:comparable_pair)
197
- return comparable_pair(values)
76
+ if values.is_a?(CArray) && respond_to?(:comparable_pair, true) &&
77
+ !values.respond_to?(:comparable_pair, true)
78
+ return comparable_pair(values, lookup: lookup)
198
79
  end
199
80
  # The same from the other side: values of a time array looked for in an
200
81
  # array that is not one.
201
- if values.is_a?(CArray) && values.respond_to?(:comparable_pair) &&
202
- !respond_to?(:comparable_pair)
203
- set, own = values.comparable_pair(self)
82
+ if values.is_a?(CArray) && values.respond_to?(:comparable_pair, true) &&
83
+ !respond_to?(:comparable_pair, true)
84
+ set, own = values.send(:comparable_pair, self, lookup: lookup)
204
85
  return [own, set]
205
86
  end
206
87
  # A Face whose storage is not of its surface type. When its cells are
@@ -213,7 +94,7 @@ class CArray
213
94
  set = values.is_a?(Range) ? values.to_a : values
214
95
  return [self, set.is_a?(CArray) ? set : CArray.wrap_readonly(set)]
215
96
  end
216
- s, set = storage.send(:promote_value_set, values)
97
+ s, set = storage.send(:promote_value_set, values, lookup: lookup)
217
98
  return [s.equal?(storage) ? self : s, set]
218
99
  end
219
100
  case values
@@ -238,7 +119,7 @@ class CArray
238
119
  # fractional literal promotes self to float instead of truncating. Against
239
120
  # object / fixlen self the elements are values, not data type specifiers (a
240
121
  # String is a value, not a type name), so build the set in self's data type.
241
- def promote_elements (elems)
122
+ private def promote_elements(elems)
242
123
  if data_type == CA_FIXLEN
243
124
  # A fixlen value is a cell-width blob, so the set has to be built at
244
125
  # this array's width -- to_type(:fixlen) with no bytes: builds it at
@@ -258,11 +139,18 @@ class CArray
258
139
  elsif data_type == CA_OBJECT
259
140
  # coerce: an object set holds values of self's kind
260
141
  [self, elems.to_ca.to_type(data_type)]
261
- elsif elems.any? { |v| v.is_a?(String) || v.is_a?(Symbol) }
142
+ elsif elems.any? { |v| v.is_a?(String) || v.is_a?(Symbol) || v.is_a?(Array) }
262
143
  # A String is a value, which result_type would read as the name of a
263
- # data type; a numeric self meets it in the object lane.
144
+ # data type, and an Array has no data type; a numeric self meets them
145
+ # in the object lane.
264
146
  [to_type(CA_OBJECT), elems.to_ca.to_type(CA_OBJECT)]
265
147
  else
148
+ # true and false are the numbers 1 and 0 beside a numeric self, as
149
+ # they are to an operator; cast from an object array they would be
150
+ # masked cells instead.
151
+ unless data_type == CA_BOOLEAN
152
+ elems = elems.map { |v| v == true ? 1 : v == false ? 0 : v }
153
+ end
266
154
  t = CArray.result_type(self, *elems)
267
155
  [coerce_self(t), elems.to_ca.to_type(t)]
268
156
  end
@@ -273,7 +161,7 @@ class CArray
273
161
  # strings, and a categorical by its labels, since their storage is not
274
162
  # their values; anything else, or the same kind of array as self, is
275
163
  # taken as it is. A record is left to be refused.
276
- def values_for_lookup (other)
164
+ private def values_for_lookup(other)
277
165
  return other if other.send(:orderable_storage?)
278
166
  return other if face? && other.class == self.class
279
167
  if other.respond_to?(:to_string)
@@ -291,19 +179,151 @@ class CArray
291
179
  # it is held at. Raw fixlen cells of different widths are values of
292
180
  # different lengths, not padded strings, and are left as they are (the
293
181
  # lookup refuses them, naming the widths).
294
- def fixlen_widened_with (other)
182
+ private def fixlen_widened_with(other)
295
183
  return [self, other] if bytes == other.bytes
296
184
  return [self, other] unless is_a?(CAFixlenString) && other.is_a?(CAFixlenString)
297
185
  w = [bytes, other.bytes].max
298
186
  [widen_fixlen_string(self, w), widen_fixlen_string(other, w)]
299
187
  end
300
188
 
301
- def widen_fixlen_string (x, w)
189
+ private def widen_fixlen_string(x, w)
302
190
  x.bytes == w ? x : x.to_fixlen_string(bytes: w)
303
191
  end
304
192
 
305
- def coerce_self (t)
193
+ private def coerce_self(t)
306
194
  CArray.result_type(self) == t ? self : to_type(t)
307
195
  end
308
196
 
197
+ # @overload is_in(values)
198
+ # Returns a boolean CArray of the same shape as `self`, `true` at
199
+ # each cell whose value appears in the set `values`.
200
+ #
201
+ # `values` is treated as a set, not as an operand to broadcast: it
202
+ # may be any shape (or an Array) and is flattened to a
203
+ # single seen-set, so its shape need not match `self`. Only one
204
+ # argument is accepted; to test a few immediate values pass an
205
+ # Array (`a.is_in([0, -1])`). A single value is a set of one, as
206
+ # `[value]` is (`a.is_in(2)` is `a.is_in([2])`).
207
+ #
208
+ # When `self` and `values` have different numeric data types they are
209
+ # promoted to a common type first (the same promotion binops use,
210
+ # {CArray.result_type}), so membership is value-correct across
211
+ # data types (e.g. an int cell equals a float set element of the same
212
+ # value, and a fractional set element never truncates onto an int
213
+ # cell). Integers of mixed sign are matched by value, as {#eq}
214
+ # compares them: `-1` in an int8 array never matches `255` in a uint8
215
+ # one, and the set operations answer such a pair in a type that holds
216
+ # both (int16 for uint8 with int8, object for uint64 with int64).
217
+ # Genuinely incompatible data types (e.g. numeric vs fixlen) raise.
218
+ #
219
+ # Membership is value-based and shares the distinctness of the
220
+ # value-hash discovery family ({#unique} / {#value_counts}):
221
+ # numeric follows `==` with all NaN collapsed to one value and
222
+ # -0.0 == +0.0; `CA_OBJECT` follows Ruby `hash` / `eql?` with Float
223
+ # NaN collapsed; `CA_FIXLEN` follows byte equality over the whole
224
+ # cell, with a short String in the set padded out to the cell width
225
+ # (so a 5-byte array written from `"be"` is `is_in(["be"])`). A
226
+ # `CA_FIXLEN` set given as a CArray must already be of that width.
227
+ #
228
+ # An object array keeps Ruby's own split between its two kinds of
229
+ # equality. A lookup through a hash -- `is_in`, the set operations,
230
+ # {#locate_addr}, {#unique} and the rest of the discovery family --
231
+ # uses `eql?`, as `Array#uniq`, `Array#&` and `Set` do, so `1` and
232
+ # `1.0` are different values there. A cell-by-cell comparison --
233
+ # {#eq}, {#count}, {#search}, {#bsearch} -- uses `==`, as
234
+ # `Array#include?` does, so they are equal there. To match by value
235
+ # across Integer and Float, convert the array to a numeric type first.
236
+ #
237
+ # Masked cells of `values` do not enter the set, and neither does `nil`
238
+ # unless `self` is an `:object` array, which can hold it. Masked cells
239
+ # of `self` stay masked in the result (membership is unknown), so
240
+ # `is_in` propagates `self`'s mask like an element-wise comparison.
241
+ #
242
+ # For a per-fiber "does this fiber contain any of these values"
243
+ # reduction, compose with {#any}: `a.is_in(values).any(axis: k)`.
244
+ #
245
+ # @param values [CArray, Array] the set to test membership
246
+ # against. Promoted with `self` to a common data type.
247
+ # @raise [ArgumentError] for a Range, which reads as an interval rather
248
+ # than the set of its steps.
249
+ # Between two time arrays the question is about instants, not ticks:
250
+ # `values` is reconciled into `self`'s unit first, so a `:D` array and an
251
+ # `:h` array match on the instants they share. The same holds for the set
252
+ # operations below, whose results come back as `self`'s own type.
253
+ #
254
+ # @return [CArray] boolean CArray of the same shape as `self`.
255
+ def is_in(values)
256
+ refuse_range_set(values, "is_in")
257
+ a, b = promote_value_set(values_of_set(values), lookup: true)
258
+ r = a.__send__(:__is_in__, b)
259
+ # A cell of self that the set's kind can not hold (a Float looked for
260
+ # among times) is masked in a, and is in no set.
261
+ if !a.equal?(self) && a.has_mask?
262
+ gone = has_mask? ? a.is_masked.and(is_not_masked) : a.is_masked
263
+ r[gone] = false
264
+ end
265
+ r
266
+ end
267
+
268
+ # @overload intersection(other, sort: false)
269
+ # Returns a 1-D CArray of the distinct values appearing in both
270
+ # `self` and `other`, in `self`'s first-appearance order.
271
+ #
272
+ # Value-based, sharing the distinctness of the discovery family
273
+ # (see {#is_in}); `self` and `other` are promoted to a common data type.
274
+ # Masked cells of either array do not participate. The result is
275
+ # always flat, like {#unique}, because the distinct values of a
276
+ # fiber vary in number.
277
+ #
278
+ # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
279
+ # @param sort [Boolean] when true, return the values sorted
280
+ # ascending instead of in first-appearance order (`nil` reads as
281
+ # false; any other value raises ArgumentError).
282
+ # @return [CArray] 1-D CArray of the common distinct values.
283
+ def intersection(other, sort: false)
284
+ sort = sort_flag(sort, "intersection")
285
+ refuse_range_set(other, "intersection")
286
+ a, b = promote_value_set(other)
287
+ r = a.__send__(:__intersection__, b)
288
+ r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with, true)
289
+ sort ? r.sort : r
290
+ end
291
+
292
+ # @overload difference(other, sort: false)
293
+ # Returns a 1-D CArray of the distinct values in `self` that are
294
+ # absent from `other`, in `self`'s first-appearance order.
295
+ # See {#intersection} for the shared semantics and options.
296
+ #
297
+ # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
298
+ # @param sort [Boolean] when true, return the values sorted ascending.
299
+ # @return [CArray] 1-D CArray of the self-only distinct values.
300
+ def difference(other, sort: false)
301
+ sort = sort_flag(sort, "difference")
302
+ refuse_range_set(other, "difference")
303
+ a, b = promote_value_set(other)
304
+ r = a.__send__(:__difference__, b)
305
+ r = r.to_unit(unit) if !a.equal?(self) && respond_to?(:in_common_unit_with, true)
306
+ sort ? r.sort : r
307
+ end
308
+
309
+ # @overload union(other, sort: false)
310
+ # Returns a 1-D CArray of the distinct values appearing in either
311
+ # `self` or `other`, in self-then-other first-appearance order.
312
+ # See {#intersection} for the shared semantics and options. Two time
313
+ # arrays of different units give their union in the finer unit, which
314
+ # holds the values of both ({#intersection} and {#difference} answer in
315
+ # `self`'s unit, since their values are `self`'s).
316
+ #
317
+ # @param other [CArray, Array] promoted with `self` (a Range is refused, as by {#is_in}).
318
+ # @param sort [Boolean] when true, return the values sorted ascending
319
+ # (a merged, ordered set — e.g. a common time axis).
320
+ # @return [CArray] 1-D CArray of the combined distinct values.
321
+ def union(other, sort: false)
322
+ sort = sort_flag(sort, "union")
323
+ refuse_range_set(other, "union")
324
+ a, b = promote_value_set(other)
325
+ r = a.__send__(:__union__, b)
326
+ sort ? r.sort : r
327
+ end
328
+
309
329
  end
@@ -33,7 +33,8 @@ class CArray
33
33
  # @note The 2.x multi-separator form `a.join("\n", ",")` was removed
34
34
  # in 3.0; use the axis form and chain, e.g.
35
35
  # `a.join(",", axis: 1).join("\n")`.
36
- def join (*argv, axis: nil, keep_axis: false)
36
+ def join(*argv, axis: nil, keep_axis: false)
37
+ keep_axis = CArray.send(:bool_arg, keep_axis, "keep_axis", "join")
37
38
  if argv.size > 1
38
39
  raise ArgumentError,
39
40
  "join accepts at most one positional separator " \
@@ -4,7 +4,11 @@ class CArray
4
4
  # Returns the value `n` steps earlier along `axis`, cell by cell: cell
5
5
  # `i` of the result is cell `i - n` of `self`. A cell with no value `n`
6
6
  # steps back is UNDEF, so a difference against it is UNDEF rather than
7
- # a difference against a stand-in value:
7
+ # a difference against a stand-in value. (`shift` is the view that
8
+ # moves the cells, and its cells outside take `fill_value`, 0 by
9
+ # default, for the uses that want 0 there; `lag` looks a value up, and
10
+ # a value that is not there is UNDEF, as it is for `project` and
11
+ # `windows`.)
8
12
  #
9
13
  # ```
10
14
  # x = CA_FLOAT64([1.0, 4.0, 9.0])
@@ -29,7 +33,7 @@ class CArray
29
33
  # @raise [TypeError] when `n` or `axis` is not an Integer.
30
34
  # @raise [ArgumentError] when `axis` is out of range, or left out for an
31
35
  # array of more than one dimension.
32
- def lag (n = 1, axis: nil)
36
+ def lag(n = 1, axis: nil)
33
37
  unless n.is_a?(Integer)
34
38
  raise TypeError, "lag: n must be an Integer (got #{n.class})"
35
39
  end