carray 3.0.6 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +7 -1830
  4. data/README.md +62 -35
  5. data/carray.gemspec +16 -4
  6. data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
  7. data/ext/ca_array_pool.c +4 -5
  8. data/ext/ca_axis_descriptor.h +45 -30
  9. data/ext/ca_axis_dispatch.c +7 -4
  10. data/ext/ca_axis_group.c +12 -15
  11. data/ext/ca_bincmp_dispatch.h +16 -38
  12. data/ext/ca_binop_dispatch.c +35 -0
  13. data/ext/ca_binop_dispatch.h +22 -46
  14. data/ext/ca_categorical_iterator.c +36 -34
  15. data/ext/ca_compare.c +0 -1
  16. data/ext/ca_compare.h +8 -1
  17. data/ext/ca_composite_dispatch.h +6 -0
  18. data/ext/ca_for_buffer.h +6 -1
  19. data/ext/ca_group_iter.c +17 -18
  20. data/ext/ca_iter_state_fields.h +196 -0
  21. data/ext/ca_iter_substrate.h +84 -103
  22. data/ext/ca_kernel_iterator.c +125 -89
  23. data/ext/ca_kernel_iterator.h +229 -1504
  24. data/ext/ca_kernel_iterator_internal.h +1167 -0
  25. data/ext/ca_moncmp_dispatch.h +17 -24
  26. data/ext/ca_monop_dispatch.h +25 -74
  27. data/ext/ca_obj_array.c +92 -165
  28. data/ext/ca_obj_bincmp.c +65 -48
  29. data/ext/ca_obj_binop.c +74 -49
  30. data/ext/ca_obj_bitarray.c +7 -21
  31. data/ext/ca_obj_bitfield.c +93 -26
  32. data/ext/ca_obj_block.c +11 -12
  33. data/ext/ca_obj_byte_swap.c +34 -34
  34. data/ext/ca_obj_const_string.c +464 -304
  35. data/ext/ca_obj_face.c +167 -121
  36. data/ext/ca_obj_face.h +32 -170
  37. data/ext/ca_obj_fake.c +127 -34
  38. data/ext/ca_obj_farray.c +13 -9
  39. data/ext/ca_obj_field.c +17 -13
  40. data/ext/ca_obj_fixlen_string.c +57 -34
  41. data/ext/ca_obj_grid.c +149 -113
  42. data/ext/ca_obj_meld.c +28 -26
  43. data/ext/ca_obj_moncmp.c +10 -21
  44. data/ext/ca_obj_monop.c +88 -73
  45. data/ext/ca_obj_object.c +60 -46
  46. data/ext/ca_obj_record.c +74 -22
  47. data/ext/ca_obj_reduce.c +10 -14
  48. data/ext/ca_obj_refer.c +146 -39
  49. data/ext/ca_obj_remap.c +11 -29
  50. data/ext/ca_obj_repeat.c +41 -23
  51. data/ext/ca_obj_roll.c +20 -28
  52. data/ext/ca_obj_select.c +17 -27
  53. data/ext/ca_obj_select_axis.c +35 -42
  54. data/ext/ca_obj_shift.c +22 -42
  55. data/ext/ca_obj_source.c +1 -5
  56. data/ext/ca_obj_stack.c +33 -29
  57. data/ext/ca_obj_stride.c +99 -62
  58. data/ext/ca_obj_string.c +8 -20
  59. data/ext/ca_obj_tile.c +14 -16
  60. data/ext/ca_obj_time.c +12 -23
  61. data/ext/ca_obj_timedelta.c +12 -23
  62. data/ext/ca_obj_transpose.c +8 -10
  63. data/ext/ca_obj_triop.c +48 -49
  64. data/ext/ca_obj_window.c +87 -99
  65. data/ext/ca_op_cmplx64.h +4 -0
  66. data/ext/ca_op_ipower.c +97 -27
  67. data/ext/ca_op_powi.h +9 -28
  68. data/ext/ca_pow5_table.h +2 -1
  69. data/ext/ca_rng_normal.h +4 -0
  70. data/ext/ca_rng_xoshiro256pp.h +3 -0
  71. data/ext/ca_sort_kernels.h +23 -26
  72. data/ext/ca_sweep_engine.c +92 -26
  73. data/ext/ca_sweep_engine.h +22 -15
  74. data/ext/ca_transform_common.c +3 -8
  75. data/ext/ca_triop_dispatch.h +20 -32
  76. data/ext/caframe_csv_reader.c +12 -12
  77. data/ext/carray.h +300 -1009
  78. data/ext/carray_access.c +320 -207
  79. data/ext/carray_address_basis.c +128 -59
  80. data/ext/carray_attribute.c +81 -104
  81. data/ext/carray_bincount.c +12 -14
  82. data/ext/carray_broadcast.c +31 -27
  83. data/ext/carray_call_cfunc.c +174 -285
  84. data/ext/carray_call_cfunc.h +36 -28
  85. data/ext/carray_cast.c +1191 -351
  86. data/ext/carray_cast_func.rb +68 -8
  87. data/ext/carray_class.c +6 -11
  88. data/ext/carray_conversion.c +98 -39
  89. data/ext/carray_copy.c +52 -26
  90. data/ext/carray_core.c +286 -132
  91. data/ext/carray_count.c +53 -22
  92. data/ext/carray_element.c +49 -28
  93. data/ext/carray_factorize.c +64 -63
  94. data/ext/carray_generate.c +26 -19
  95. data/ext/carray_histogram.c +9 -11
  96. data/ext/carray_hold.c +7 -9
  97. data/ext/carray_index_classifier.c +127 -230
  98. data/ext/carray_index_classifier.h +11 -10
  99. data/ext/carray_integer_total.c +523 -0
  100. data/ext/carray_internal.h +1364 -156
  101. data/ext/carray_key_windows.c +3 -3
  102. data/ext/carray_lazy.c +37 -37
  103. data/ext/carray_loop.c +27 -23
  104. data/ext/carray_mask.c +235 -150
  105. data/ext/carray_math_kernel.h +23 -47
  106. data/ext/carray_mathfunc.c +10 -3
  107. data/ext/carray_median_percentile.c +106 -100
  108. data/ext/carray_memory_view.c +83 -69
  109. data/ext/carray_operator.c +490 -128
  110. data/ext/carray_order.c +163 -84
  111. data/ext/carray_partition.c +14 -16
  112. data/ext/carray_random.c +48 -42
  113. data/ext/carray_round.c +33 -5
  114. data/ext/carray_scatter.c +57 -39
  115. data/ext/carray_segment.c +42 -46
  116. data/ext/carray_slab.c +77 -51
  117. data/ext/carray_slab.h +44 -33
  118. data/ext/carray_sort.c +37 -31
  119. data/ext/carray_sort_kernel.c +36 -10
  120. data/ext/carray_struct.c +44 -34
  121. data/ext/carray_test.c +131 -81
  122. data/ext/carray_time_text.c +30 -31
  123. data/ext/carray_undef.c +50 -7
  124. data/ext/carray_utils.c +373 -139
  125. data/ext/extconf.rb +24 -17
  126. data/ext/mk_call_cfunc.rb +25 -31
  127. data/ext/mk_pow5_table.rb +2 -1
  128. data/ext/mkkernel.rb +819 -488
  129. data/ext/ruby_carray.c +47 -42
  130. data/ext/version.h +7 -5
  131. data/lib/carray/attribute.rb +19 -18
  132. data/lib/carray/autoload_carray.rb +20 -15
  133. data/lib/carray/autoload_method_extension.rb +64 -57
  134. data/lib/carray/axis_group.rb +272 -185
  135. data/lib/carray/basics.rb +298 -56
  136. data/lib/carray/bincount_nd.rb +41 -18
  137. data/lib/carray/block_iterator.rb +167 -119
  138. data/lib/carray/boolean_reduce.rb +7 -4
  139. data/lib/carray/categorical.rb +84 -59
  140. data/lib/carray/categorical_iterator.rb +90 -67
  141. data/lib/carray/complex.rb +61 -41
  142. data/lib/carray/conditional.rb +98 -23
  143. data/lib/carray/const_string.rb +112 -46
  144. data/lib/carray/core_extensions.rb +20 -69
  145. data/lib/carray/distance_limit.rb +7 -5
  146. data/lib/carray/factories.rb +269 -0
  147. data/lib/carray/fixlen_string.rb +95 -12
  148. data/lib/carray/frame/column_verbs.rb +76 -9
  149. data/lib/carray/frame/concat.rb +51 -7
  150. data/lib/carray/frame/convert.rb +2 -1
  151. data/lib/carray/frame/csv_parser.rb +20 -5
  152. data/lib/carray/frame/frame.rb +73 -31
  153. data/lib/carray/frame/group.rb +247 -187
  154. data/lib/carray/frame/io.rb +36 -12
  155. data/lib/carray/frame/join.rb +56 -17
  156. data/lib/carray/frame/records.rb +7 -5
  157. data/lib/carray/frame/reshape.rb +27 -7
  158. data/lib/carray/frame/sort.rb +10 -8
  159. data/lib/carray/frame/verbs.rb +217 -134
  160. data/lib/carray/frame/windows.rb +254 -19
  161. data/lib/carray/frame.rb +1 -2
  162. data/lib/carray/fuse_source.rb +28 -23
  163. data/lib/carray/fusion.rb +30 -30
  164. data/lib/carray/histogram.rb +35 -16
  165. data/lib/carray/inspect.rb +7 -7
  166. data/lib/carray/iterator.rb +104 -23
  167. data/lib/carray/key_windows.rb +93 -61
  168. data/lib/carray/lazy.rb +208 -111
  169. data/lib/carray/limits.rb +80 -0
  170. data/lib/carray/mask_gap_fill.rb +21 -16
  171. data/lib/carray/math.rb +87 -50
  172. data/lib/carray/meld_reduce.rb +30 -8
  173. data/lib/carray/methods/align_addr.rb +29 -17
  174. data/lib/carray/methods/bin.rb +8 -1
  175. data/lib/carray/methods/bincount.rb +4 -2
  176. data/lib/carray/methods/bit_string.rb +19 -31
  177. data/lib/carray/methods/broadcast.rb +8 -3
  178. data/lib/carray/methods/choose.rb +5 -5
  179. data/lib/carray/methods/composition.rb +73 -72
  180. data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
  181. data/lib/carray/methods/discovery_along.rb +3 -3
  182. data/lib/carray/methods/factorize.rb +2 -0
  183. data/lib/carray/methods/gather_nd.rb +106 -103
  184. data/lib/carray/methods/insert_block.rb +2 -2
  185. data/lib/carray/methods/is_between.rb +6 -4
  186. data/lib/carray/methods/is_in.rb +183 -143
  187. data/lib/carray/methods/join.rb +2 -1
  188. data/lib/carray/methods/lag.rb +6 -2
  189. data/lib/carray/methods/locate_addr.rb +157 -94
  190. data/lib/carray/methods/mask_duplicates.rb +1 -1
  191. data/lib/carray/methods/meshgrid.rb +28 -16
  192. data/lib/carray/methods/mode.rb +67 -67
  193. data/lib/carray/methods/nunique.rb +9 -7
  194. data/lib/carray/methods/repeat.rb +23 -13
  195. data/lib/carray/methods/resize.rb +1 -1
  196. data/lib/carray/methods/snap.rb +24 -20
  197. data/lib/carray/methods/string_format.rb +2 -2
  198. data/lib/carray/methods/unique.rb +4 -2
  199. data/lib/carray/methods/value_counts.rb +1 -1
  200. data/lib/carray/mkmf.rb +25 -28
  201. data/lib/carray/percentile_arguments.rb +82 -0
  202. data/lib/carray/record_order.rb +50 -20
  203. data/lib/carray/rng.rb +1 -0
  204. data/lib/carray/runtime.rb +195 -17
  205. data/lib/carray/segment_iterator.rb +262 -137
  206. data/lib/carray/serialize.rb +635 -566
  207. data/lib/carray/slab_iterator.rb +114 -114
  208. data/lib/carray/stack.rb +14 -15
  209. data/lib/carray/string.rb +16 -22
  210. data/lib/carray/string_operation_extension.rb +97 -52
  211. data/lib/carray/struct.rb +122 -164
  212. data/lib/carray/struct_builder.rb +247 -73
  213. data/lib/carray/time.rb +663 -365
  214. data/lib/carray/window_iterator.rb +232 -155
  215. data/lib/carray.rb +6 -11
  216. data/yard-stubs/ca_obj_array.rb +20 -135
  217. data/yard-stubs/ca_obj_bitarray.rb +1 -8
  218. data/yard-stubs/ca_obj_bitfield.rb +6 -2
  219. data/yard-stubs/ca_obj_byte_swap.rb +21 -7
  220. data/yard-stubs/ca_obj_fake.rb +3 -1
  221. data/yard-stubs/ca_obj_farray.rb +1 -1
  222. data/yard-stubs/ca_obj_field.rb +8 -6
  223. data/yard-stubs/ca_obj_refer.rb +10 -5
  224. data/yard-stubs/ca_obj_roll.rb +7 -6
  225. data/yard-stubs/ca_obj_shift.rb +2 -3
  226. data/yard-stubs/ca_obj_stride.rb +5 -7
  227. data/yard-stubs/ca_obj_tile.rb +1 -1
  228. data/yard-stubs/ca_obj_time.rb +43 -0
  229. data/yard-stubs/ca_obj_transpose.rb +1 -1
  230. data/yard-stubs/ca_obj_window.rb +28 -16
  231. data/yard-stubs/carray_access.rb +55 -6
  232. data/yard-stubs/carray_attribute.rb +26 -38
  233. data/yard-stubs/carray_cast.rb +121 -162
  234. data/yard-stubs/carray_class.rb +3 -4
  235. data/yard-stubs/carray_conversion.rb +6 -7
  236. data/yard-stubs/carray_copy.rb +2 -1
  237. data/yard-stubs/carray_count.rb +10 -5
  238. data/yard-stubs/carray_element.rb +4 -3
  239. data/yard-stubs/carray_limits.rb +100 -0
  240. data/yard-stubs/carray_loop.rb +0 -20
  241. data/yard-stubs/carray_mask.rb +59 -41
  242. data/yard-stubs/carray_math.rb +162 -100
  243. data/yard-stubs/carray_mathfunc.rb +415 -3
  244. data/yard-stubs/carray_median_percentile.rb +29 -11
  245. data/yard-stubs/carray_memory_view.rb +2 -2
  246. data/yard-stubs/carray_order.rb +72 -33
  247. data/yard-stubs/carray_random.rb +6 -6
  248. data/yard-stubs/carray_reduce.rb +353 -0
  249. data/yard-stubs/carray_rng.rb +92 -0
  250. data/yard-stubs/carray_scatter.rb +12 -3
  251. data/yard-stubs/carray_slab.rb +7 -4
  252. data/yard-stubs/carray_sort.rb +7 -6
  253. data/yard-stubs/carray_test.rb +18 -5
  254. data/yard-stubs/carray_undef.rb +44 -5
  255. data/yard-stubs/ruby_carray.rb +75 -40
  256. metadata +63 -25
  257. data/CHANGELOG.v1.md +0 -126
  258. data/ext/carray_build_flags.h +0 -3
  259. data/ext/carray_config.h +0 -28
  260. data/ext/carray_data_type.c +0 -90
  261. data/ext/carray_kernels_bincmp.c +0 -6457
  262. data/ext/carray_kernels_binop.c +0 -31534
  263. data/ext/carray_kernels_init.c +0 -1734
  264. data/ext/carray_kernels_moncmp.c +0 -2104
  265. data/ext/carray_kernels_monop.c +0 -25387
  266. data/ext/carray_kernels_reduce_aggregate.c +0 -27545
  267. data/ext/carray_kernels_reduce_boolean.c +0 -1924
  268. data/ext/carray_kernels_reduce_cumulative.c +0 -11646
  269. data/ext/carray_kernels_reduce_extreme.c +0 -21631
  270. data/ext/carray_kernels_reduce_variance.c +0 -5981
  271. data/ext/carray_kernels_scan.c +0 -4302
  272. data/ext/carray_kernels_search.c +0 -37776
  273. data/ext/carray_kernels_sort.c +0 -10814
  274. data/ext/carray_kernels_triop.c +0 -1864
  275. data/lib/carray/construct.rb +0 -360
  276. data/lib/carray/data_type_extension.rb +0 -261
  277. data/lib/carray/data_type_limits.rb +0 -91
@@ -30,8 +30,16 @@ class CArray
30
30
  # other branch (`cond.then_else(a, UNDEF)` has `a`'s data type).
31
31
  # String branches make the result a {CAString}
32
32
  # (`cond.then_else("yes", "no")`, also with `UNDEF` for one of them).
33
+ # Branches of one Face (two {CATime} arrays, two {CAString} arrays)
34
+ # make the result that Face; a {CACategorical} branch gives its labels,
35
+ # so two categorical branches make an object array.
33
36
  # A Symbol branch, or a String beside a value of another kind, makes
34
- # the result an object array.
37
+ # the result an object array. So does `nil`, a Time or another object
38
+ # beside numeric or boolean values, `true` or `false` beside numeric
39
+ # values, and an Integer beyond the 64-bit range beside integer or
40
+ # boolean values. A Ruby Array branch is one value of an
41
+ # object result; beside numeric or boolean values it is refused (make
42
+ # it a CArray of the type you mean, e.g. `CA_INT32([9, 9, 9])`).
35
43
  # When `self` or a branch is a lazy view, the result is a lazy view
36
44
  # (read-only) that joins the expression around it; `copy` makes an
37
45
  # array of it. An object result is always computed at once.
@@ -40,11 +48,13 @@ class CArray
40
48
  # @return [CArray] new array with the same shape as `self` (a
41
49
  # {CAString} for String branches), or a lazy view (CATriOp) when an
42
50
  # operand is lazy.
51
+ # @raise [TypeError] when a branch is a Ruby Array beside numeric or
52
+ # boolean values.
43
53
  # @raise [ArgumentError] when `self` is not a boolean CArray, or when
44
54
  # an array branch's shape does not pair with `self`'s as an
45
55
  # operator's operands would (equal, or size-1 axes of the same ndim;
46
56
  # an object or fixlen result needs `self`'s shape exactly).
47
- def then_else (x, y)
57
+ def then_else(x, y)
48
58
  # Guard: self must be boolean. Integer / float receiver would be
49
59
  # silently reinterpreted by the indexer setter (`result[self] = ...`)
50
60
  # as an index array, producing surprising scatter rather than the
@@ -60,7 +70,7 @@ class CArray
60
70
  refuse_branch_mix(x, y, face)
61
71
  return face_then_else(x, y, face)
62
72
  end
63
- dt = CArray.send(:selection_type, [x, y])
73
+ dt = CArray.send(:selection_type, [x, y], "then_else")
64
74
  return lazy_then_else(x, y, dt) if lazy_then_else?(x, y, dt)
65
75
  # A branch shaped unlike the condition goes through the lazy select,
66
76
  # which pairs the shapes by the operator rule (equal, or size-1 axes
@@ -91,14 +101,14 @@ class CArray
91
101
  # select), so it joins the expression around it rather than computing
92
102
  # it here. The branches carry the result type: a scalar branch becomes
93
103
  # a CScalar of it, UNDEF a masked one.
94
- private def lazy_then_else? (x, y, dt)
104
+ private def lazy_then_else?(x, y, dt)
95
105
  return false if scalar? || dt == CA_OBJECT || dt == CA_FIXLEN
96
106
  [self, x, y].any? { |v| v.is_a?(CArray) && v.lazy_view? }
97
107
  end
98
108
 
99
109
  # The types the lazy select cannot carry keep the strict form: a full
100
110
  # branch must have the condition's shape.
101
- private def refuse_branch_shape (x, y)
111
+ private def refuse_branch_shape(x, y)
102
112
  bad = [x, y].find { |v| v.is_a?(CArray) && !v.scalar? && v.shape != shape }
103
113
  raise ArgumentError,
104
114
  "then_else: branch shape #{bad.shape.inspect} does not match the condition's #{shape.inspect}"
@@ -110,7 +120,7 @@ class CArray
110
120
  # so a plain array would be read as its storage (an integer as a time's
111
121
  # ticks) and the result would take the class of whichever branch came
112
122
  # second. A scalar branch is a value and goes through the Face's store.
113
- private def refuse_branch_mix (x, y, face)
123
+ private def refuse_branch_mix(x, y, face)
114
124
  arrays = [x, y].select { |v| v.is_a?(CArray) && !v.scalar? }
115
125
  return if arrays.all? { |v| v.face? && v.obj_type == face.obj_type }
116
126
  hint = face.is_a?(CAString) ?
@@ -120,7 +130,7 @@ class CArray
120
130
  "different Face classes; convert them first to one Face class or to no Face" + hint
121
131
  end
122
132
 
123
- private def lazy_then_else (x, y, dt)
133
+ private def lazy_then_else(x, y, dt)
124
134
  branch = lambda do |v|
125
135
  next v if v.is_a?(CArray) && !v.scalar?
126
136
  cell = CScalar.new(dt)
@@ -130,7 +140,14 @@ class CArray
130
140
  CATriOp.__build__(self, branch.(x), branch.(y), CATriOp::OP_SELECT)
131
141
  end
132
142
 
133
- private def face_then_else (x, y, face)
143
+ # The two branches of a selection, in a form that holds the cells of both.
144
+ # A Face whose cells differ by more than their values (a time's unit, a
145
+ # fixed string's width) brings them to one here, whichever branch is which.
146
+ private def branches_in_common(x, y)
147
+ [x, y]
148
+ end
149
+
150
+ private def face_then_else(x, y, face)
134
151
  # A Face branch decides the result: the other branch is written into a
135
152
  # copy of it through the Face's own store, so an instant stays an instant.
136
153
  # Promoting the two branches to a common data type would compare a Face's
@@ -141,10 +158,11 @@ class CArray
141
158
  sy = y.is_a?(CAConstString) ? y.to_string : y
142
159
  return CArray.const_string(then_else(sx, sy), encoding: face.encoding)
143
160
  end
161
+ x, y = face.__send__(:branches_in_common, x, y)
144
162
  x_full = x.is_a?(CArray) && !x.scalar?
145
163
  y_full = y.is_a?(CArray) && !y.scalar?
146
164
  base = y_full ? y : x
147
- if base.read_only?
165
+ unless base.__send__(:holds_new_values?)
148
166
  # No store to write through (a CACategorical's codes): select among
149
167
  # the values themselves.
150
168
  result = CArray.object(*shape)
@@ -174,13 +192,14 @@ class CArray
174
192
  # @param b [CArray, Numeric, Object] replacement value(s).
175
193
  # @return [CArray] new array.
176
194
  # @raise [ArgumentError] when `cond` is not a boolean CArray.
177
- def replace_where (cond, b)
195
+ def replace_where(cond, b)
178
196
  unless cond.is_a?(CArray) && cond.boolean?
179
197
  raise ArgumentError,
180
198
  "replace_where: cond must be a boolean CArray (data_type == CA_BOOLEAN)"
181
199
  end
182
200
  result = self.copy
183
- result[cond] = b.is_a?(CArray) ? b[cond] : b
201
+ # A CScalar takes no index: like a number, it is the value for every cell.
202
+ result[cond] = b.is_a?(CArray) && !b.scalar? ? b[cond] : b
184
203
  result
185
204
  end
186
205
 
@@ -205,6 +224,8 @@ class CArray
205
224
  # @param data_type [Symbol, Integer, nil] override for the result
206
225
  # data type.
207
226
  # @return [CArray] new array with the same shape as `self`.
227
+ # @raise [TypeError] when a callable returns a Ruby Array beside
228
+ # numeric or boolean values (return a CArray of the type you mean).
208
229
  # @raise [ArgumentError] when `cond` is not a same-shape
209
230
  # boolean CArray.
210
231
  # @example
@@ -213,7 +234,7 @@ class CArray
213
234
  # ->(v) { v.log }, # domain-safe: only positive cells
214
235
  # ->(v) { -v })
215
236
  # # => [2.0, 1.0, -0.0, 0.0, 0.6931..., 1.0986...]
216
- def conditional (cond, then_fn, else_fn, data_type: nil)
237
+ def conditional(cond, then_fn, else_fn, data_type: nil)
217
238
  unless cond.is_a?(CArray) && cond.boolean? && cond.shape == self.shape
218
239
  raise ArgumentError,
219
240
  "conditional: cond must be a boolean CArray with same shape as self"
@@ -230,13 +251,13 @@ class CArray
230
251
  # to the subset shape; wrap it here so the scatter step below sees a
231
252
  # same-length CArray.
232
253
  unless y_then.is_a?(CArray)
233
- y_then = CArray.new(data_type || CArray.send(:selection_type, [y_then]), x_then.shape).fill(y_then)
254
+ y_then = CArray.new(data_type || CArray.send(:selection_type, [y_then], "conditional"), x_then.shape).fill(y_then)
234
255
  end
235
256
  unless y_else.is_a?(CArray)
236
- y_else = CArray.new(data_type || CArray.send(:selection_type, [y_else]), x_else.shape).fill(y_else)
257
+ y_else = CArray.new(data_type || CArray.send(:selection_type, [y_else], "conditional"), x_else.shape).fill(y_else)
237
258
  end
238
259
 
239
- dt = data_type || CArray.send(:selection_type, [y_then, y_else])
260
+ dt = data_type || CArray.send(:selection_type, [y_then, y_else], "conditional")
240
261
  out = CArray.new(dt, self.shape)
241
262
  out[cond] = y_then
242
263
  out[cond.not] = y_else
@@ -265,11 +286,15 @@ class CArray
265
286
  # @param condlist [Array<CArray>] boolean selectors.
266
287
  # @param choicelist [Array<CArray, Numeric, Object>] values, one
267
288
  # per condition (same length as `condlist`).
289
+ # When a choice is a Face the result is that Face (the choices meet as
290
+ # in {#then_else}), and `default` is UNDEF unless it is given.
268
291
  # @param default [CArray, Numeric, Object] value written where no
269
292
  # condition holds.
270
293
  # @param data_type [Symbol, Integer, nil] override for the result
271
294
  # data type.
272
295
  # @return [CArray] new array with the shape of `condlist[0]`.
296
+ # @raise [TypeError] when a choice is a Ruby Array beside numeric or
297
+ # boolean values (make it a CArray of the type you mean).
273
298
  # @raise [ArgumentError] on size mismatch, empty `condlist`, or a
274
299
  # non-boolean / wrong-shape entry in `condlist`.
275
300
  # @example
@@ -278,7 +303,7 @@ class CArray
278
303
  # [-x, x * 10],
279
304
  # default: 999)
280
305
  # # => [5.0, 3.0, -10.0, 0.0, 10.0, 999.0]
281
- def self.select (condlist, choicelist, default: 0, data_type: nil)
306
+ def self.select(condlist, choicelist, default: (no_default = true; 0), data_type: nil)
282
307
  unless condlist.is_a?(Array) && choicelist.is_a?(Array)
283
308
  raise ArgumentError, "select: condlist and choicelist must be Arrays"
284
309
  end
@@ -296,7 +321,15 @@ class CArray
296
321
  end
297
322
  shape = first.shape
298
323
 
299
- dt = data_type || selection_type(choicelist + [default])
324
+ if data_type.nil?
325
+ face = choicelist.find { |v| v.is_a?(CArray) && !v.scalar? && v.face? }
326
+ if face
327
+ fallback = no_default ? UNDEF : default
328
+ return select_among_faces(condlist, choicelist, fallback, face, shape)
329
+ end
330
+ end
331
+
332
+ dt = data_type || selection_type(choicelist + [default], "select")
300
333
  # `default` can be either a same-shape CArray (per-cell fallback) or a
301
334
  # scalar (broadcast to every cell).
302
335
  default_full = default.is_a?(CArray) && !default.scalar?
@@ -317,7 +350,7 @@ class CArray
317
350
  "select: condlist[#{k}] must be a same-shape boolean CArray"
318
351
  end
319
352
  v = choicelist[k]
320
- out[c] = v.is_a?(CArray) ? v[c] : v
353
+ out[c] = v.is_a?(CArray) && !v.scalar? ? v[c] : v
321
354
  end
322
355
  if !data_type && selection_text?(choicelist + [default])
323
356
  out = CAString.wrap(out)
@@ -331,20 +364,62 @@ class CArray
331
364
  # some type, so it leaves the type to the others. A String or Symbol is a
332
365
  # value, which CArray.result_type would read as the name of a data type
333
366
  # ("int32"), so it makes the result object.
334
- def self.selection_type (values)
367
+ # select over Face choices. Each condition is a then_else on the choices
368
+ # below it, so the Faces meet as they do there; a Face that cannot be
369
+ # written selects among its values.
370
+ def self.select_among_faces(condlist, choicelist, fallback, face, shape)
371
+ if face.is_a?(CAConstString)
372
+ choices = choicelist.map { |v| v.is_a?(CAConstString) ? v.to_string : v }
373
+ text = select(condlist, choices, default: fallback)
374
+ return CArray.const_string(text, encoding: face.encoding)
375
+ end
376
+ unless face.__send__(:holds_new_values?)
377
+ choices = choicelist.map { |v| v.is_a?(CArray) && v.face? ? v.to_type(CA_OBJECT) : v }
378
+ return select(condlist, choices, default: fallback)
379
+ end
380
+ unless face.shape == shape
381
+ raise ArgumentError, "select: choicelist shape #{face.shape} differs from condlist shape #{shape}"
382
+ end
383
+ out = CArray.boolean(*shape).zero.then_else(face, fallback)
384
+ (condlist.size - 1).downto(0) do |k|
385
+ c = condlist[k]
386
+ unless c.is_a?(CArray) && c.boolean? && c.shape == shape
387
+ raise ArgumentError,
388
+ "select: condlist[#{k}] must be a same-shape boolean CArray"
389
+ end
390
+ out = c.then_else(choicelist[k], out)
391
+ end
392
+ out
393
+ end
394
+ private_class_method :select_among_faces
395
+
396
+ # A Ruby Array has no data type of its own: it stays one value of an
397
+ # object result, and beside values that give a numeric or boolean type it
398
+ # is refused, as an operator refuses it.
399
+ def self.selection_type(values, name)
335
400
  typed = values.reject { |v| v.equal?(UNDEF) }
336
401
  if typed.empty? || typed.any? { |v| v.is_a?(String) || v.is_a?(Symbol) }
337
- CA_OBJECT
338
- else
339
- CArray.result_type(*typed)
402
+ return CA_OBJECT
403
+ end
404
+ arrays, others = typed.partition { |v| v.is_a?(Array) }
405
+ dt = others.empty? ? nil : CArray.result_type(*others)
406
+ # true and false are values of a boolean cell only; beside a number
407
+ # they make the result object, as nil or another object does.
408
+ if dt && dt != CA_BOOLEAN && dt != CA_OBJECT &&
409
+ others.any? { |v| v == true || v == false }
410
+ dt = CA_OBJECT
340
411
  end
412
+ return dt if arrays.empty?
413
+ return CA_OBJECT if dt == CA_OBJECT || dt == CA_FIXLEN
414
+ beside = dt ? " beside #{CArray.data_type_name(dt)} values" : ""
415
+ refuse_ruby_array(name, "a value to select#{beside}", dt)
341
416
  end
342
417
  private_class_method :selection_type
343
418
 
344
419
  # Whether a selection among `values` gives text: every value that is not
345
420
  # UNDEF is a String or a CAString, and there is at least one. The result
346
421
  # is then a CAString rather than an object array of Strings.
347
- def self.selection_text? (values)
422
+ def self.selection_text?(values)
348
423
  present = values.reject { |v| v.equal?(UNDEF) }
349
424
  !present.empty? && present.all? { |v| v.is_a?(String) || v.is_a?(CAString) }
350
425
  end
@@ -36,10 +36,11 @@ class CArray
36
36
  # @overload const_string(values, encoding: Encoding::UTF_8)
37
37
  # Returns a read-only {CAConstString} column packing each String's
38
38
  # bytes into a shared buffer. `values` may be any Array (or
39
- # Array-like) of Strings; `nil` entries are masked.
39
+ # Array-like) of Strings; `nil` and `UNDEF` entries are masked.
40
40
  # @param values [Array<String, nil>] source values.
41
41
  # @param encoding [Encoding] column encoding.
42
42
  # @return [CAConstString]
43
+ # @raise [TypeError] when a value is not a String, nil or UNDEF.
43
44
  # @overload const_string(ca, encoding: Encoding::UTF_8)
44
45
  # Builds from a string-bearing CArray (String Face / CA_OBJECT / raw
45
46
  # CA_FIXLEN, the last read as NUL-stripped strings); always materialises.
@@ -53,7 +54,7 @@ class CArray
53
54
  # @yieldparam i [Integer] cell index.
54
55
  # @yieldreturn [String, nil] value for cell `i`.
55
56
  # @return [CAConstString]
56
- def self.const_string (arg, encoding: Encoding::UTF_8, &block)
57
+ def self.const_string(arg, encoding: Encoding::UTF_8, &block)
57
58
  if arg.is_a?(CArray)
58
59
  return string_face_of(arg, "CArray.const_string").to_const_string(encoding: encoding)
59
60
  end
@@ -70,6 +71,7 @@ class CArray
70
71
  else
71
72
  values = arg.to_a
72
73
  end
74
+ values = CArray.send(:string_cells, values, "CAConstString")
73
75
 
74
76
  # Arrow-style layout, built in one C pass: pure-concatenation buffer +
75
77
  # one (start,end) int64 pair per element, mask for nil.
@@ -86,6 +88,7 @@ end
86
88
  # (categorical labels) prefer {CACategorical}, which stores each distinct
87
89
  # value once.
88
90
  class CAConstString
91
+ private def holds_new_values? = false
89
92
 
90
93
  # Read-only String Face: L1 string operations only (no in-place Mutable).
91
94
  # Its C-native byte predicates / search / comparison (ext/ca_obj_const_string.c)
@@ -96,7 +99,7 @@ class CAConstString
96
99
  # window). A cell is a byte range of the shared buffer, which has no room
97
100
  # for a String it does not hold, so only UNDEF can be written.
98
101
  # @raise [ArgumentError] for any value but UNDEF.
99
- def element_to_storage (value)
102
+ private def element_to_storage(value)
100
103
  raise ArgumentError,
101
104
  "a CAConstString can't hold a new String (its strings share one " \
102
105
  "buffer); use UNDEF, or to_string first"
@@ -126,7 +129,7 @@ class CAConstString
126
129
  # and offsets. With no `axis:` the array is flattened first, as
127
130
  # `CArray#sort` does.
128
131
  # @return [CAConstString]
129
- def sort (*args, **kw)
132
+ def sort(*args, **kw)
130
133
  addr = sort_addr(*args, **kw)
131
134
  kw[:axis].nil? ? self[addr.flatten] : self[addr]
132
135
  end
@@ -135,50 +138,45 @@ class CAConstString
135
138
  # Returns an owned sorted {CAConstString}; the materialised counterpart
136
139
  # to {#sort}.
137
140
  # @return [CAConstString]
138
- def sort_copy (*args, **kw)
141
+ def sort_copy(*args, **kw)
139
142
  sort(*args, **kw).copy
140
143
  end
141
144
 
142
145
  # @overload sort_addr(axis: nil, kind: :quick, masked_position: :last)
143
146
  # Returns the view-flat addresses that index a sort by string order.
144
147
  # @return [CArray] `:int64` addresses.
145
- def sort_addr (*args, **kw)
148
+ def sort_addr(*args, **kw)
146
149
  # The native scan has no notion of an incomparable sentinel, so a masked
147
150
  # column goes the same way a per-axis one does.
148
- return sort_addr_by_bytes if args.empty? && kw.empty? && ! has_mask?
151
+ if args.empty? && kw.empty? && ! has_mask?
152
+ # Flat, as CArray#sort_addr answers without an axis.
153
+ addr = sort_addr_by_bytes
154
+ return ndim == 1 ? addr : addr.flatten.copy
155
+ end
149
156
  to_string.sort_addr(*args, **kw)
150
157
  end
151
158
 
152
159
  # @overload sort_index(axis: nil, kind: :quick, masked_position: :last)
153
160
  # Returns the per-fiber indices that index a sort by string order.
154
161
  # @return [CArray] `:int64` indices.
155
- def sort_index (*args, **kw)
156
- if args.empty? && kw.empty? && ! has_mask?
157
- # With no axis the indices are those of the flattened array, which are
158
- # its addresses.
159
- addr = sort_addr_by_bytes
160
- return ndim == 1 ? addr : addr.flatten.copy
161
- end
162
+ def sort_index(*args, **kw)
163
+ # With no axis the indices are those of the flattened array, which are
164
+ # its addresses.
165
+ return sort_addr if args.empty? && kw.empty? && ! has_mask?
162
166
  to_string.sort_index(*args, **kw)
163
167
  end
164
168
 
165
- # @overload rank_index(axis: nil)
169
+ # @overload rank_index(axis: nil, descending: false, method: :ordinal, kind: :quick)
166
170
  # @return [CArray] each cell's rank in string order.
167
- def rank_index (*args, **kw)
171
+ def rank_index(*args, **kw)
168
172
  to_string.rank_index(*args, **kw)
169
173
  end
170
174
 
171
- # @overload order(*args)
172
- # @return [CArray] the ordering of the cells by string order.
173
- def order (*args, **kw)
174
- to_string.order(*args, **kw)
175
- end
176
-
177
175
  # @overload min(axis: nil, keep_axis: false)
178
176
  # Returns the byte-smallest string, skipping masked cells; UNDEF when
179
177
  # every cell is masked or the array is empty.
180
178
  # @return [String, CAConstString]
181
- def min (*args, **kw)
179
+ def min(*args, **kw)
182
180
  return min_string_by_bytes || UNDEF if args.empty? && kw.empty?
183
181
  const_string_lift(to_string.min(*args, **kw))
184
182
  end
@@ -186,46 +184,113 @@ class CAConstString
186
184
  # @overload max(axis: nil, keep_axis: false)
187
185
  # Byte-largest counterpart of {#min}.
188
186
  # @return [String, CAConstString]
189
- def max (*args, **kw)
187
+ def max(*args, **kw)
190
188
  return max_string_by_bytes || UNDEF if args.empty? && kw.empty?
191
189
  const_string_lift(to_string.max(*args, **kw))
192
190
  end
193
191
 
194
192
  # @overload minmax(axis: nil, keep_axis: false)
195
193
  # @return [Array] `[min, max]`.
196
- def minmax (*args, **kw)
194
+ def minmax(*args, **kw)
197
195
  return [min, max] if args.empty? && kw.empty?
198
196
  to_string.minmax(*args, **kw).map { |r| const_string_lift(r) }
199
197
  end
200
198
 
201
- # @overload clip(min, max = nil, fill_value = nil, lfill: nil, ufill: nil)
199
+ # @overload clip(min, max = nil, fill_value: nil, lfill: nil, ufill: nil)
202
200
  # Clips the strings in byte order (see {CArray#clip}).
203
201
  # @return [CAConstString]
204
- def clip (*args, **kw)
202
+ def clip(*args, **kw)
205
203
  const_string_lift(to_string.clip(*args, **kw))
206
204
  end
207
205
 
206
+ # A String has no distance, so there is no nearest one; refused in the
207
+ # words CAString uses.
208
+ def search_nearest(*args, **kw)
209
+ to_string.search_nearest(*args, **kw)
210
+ end
211
+
212
+ # @see #search_nearest
213
+ def search_nearest_addr(*args, **kw)
214
+ to_string.search_nearest_addr(*args, **kw)
215
+ end
216
+
208
217
  # @overload min_index(axis: nil)
209
218
  # @return [Integer, CArray] where the byte-smallest string sits.
210
- def min_index (*args, **kw)
219
+ def min_index(*args, **kw)
211
220
  to_string.min_index(*args, **kw)
212
221
  end
213
222
 
214
223
  # @overload max_index(axis: nil)
215
224
  # @return [Integer, CArray] where the byte-largest string sits.
216
- def max_index (*args, **kw)
225
+ def max_index(*args, **kw)
217
226
  to_string.max_index(*args, **kw)
218
227
  end
219
228
 
229
+ # @overload min_addr(axis: nil)
230
+ # @return [Integer, CArray] the flat address of the byte-smallest string.
231
+ def min_addr(*args, **kw)
232
+ to_string.min_addr(*args, **kw)
233
+ end
234
+
235
+ # @overload max_addr(axis: nil)
236
+ # @return [Integer, CArray] the flat address of the byte-largest string.
237
+ def max_addr(*args, **kw)
238
+ to_string.max_addr(*args, **kw)
239
+ end
240
+
241
+ # @overload bsearch(value)
242
+ # @return [Integer, nil] where a sorted column holds `value`.
243
+ def bsearch(*args, **kw)
244
+ to_string.bsearch(*args, **kw)
245
+ end
246
+
247
+ # @overload bsearch_addr(value)
248
+ # @return [Integer, nil] the flat address of `value` in a sorted column.
249
+ def bsearch_addr(*args, **kw)
250
+ to_string.bsearch_addr(*args, **kw)
251
+ end
252
+
253
+ # @overload search_addr(value)
254
+ # @return [Integer, nil] the flat address of the first cell equal to `value`.
255
+ def search_addr(*args, **kw)
256
+ to_string.search_addr(*args, **kw)
257
+ end
258
+
259
+ # @overload match(pattern)
260
+ # @return [CArray] boolean, as {CAString} answers.
261
+ def match(*args)
262
+ to_string.match(*args)
263
+ end
264
+
265
+ # @!method pmax(other)
266
+ # The later String in byte order of each cell and `other`.
267
+ # @return [CAConstString]
268
+ # @!method pmin(other)
269
+ # The earlier String in byte order of each cell and `other`.
270
+ # @return [CAConstString]
271
+ # @!method maximum(other)
272
+ # Another name of {#pmax}.
273
+ # @return [CAConstString]
274
+ # @!method minimum(other)
275
+ # Another name of {#pmin}.
276
+ # @return [CAConstString]
277
+ CArray.send(:define_named_methods, self,
278
+ [:pmax, :pmin, :maximum, :minimum], :pick_among_strings)
279
+
280
+ private def pick_among_strings(op, other)
281
+ refuse_unordered_operands(op, other)
282
+ const_string_lift(to_string.public_send(op, string_operand(other)))
283
+ end
284
+
220
285
  # @overload partition_copy(kth, axis: nil)
221
286
  # @return [CAConstString] partitioned about the `kth` string in order.
222
- def partition_copy (*args, **kw)
287
+ def partition_copy(*args, **kw)
223
288
  const_string_lift(to_string.partition_copy(*args, **kw))
224
289
  end
225
290
 
226
291
  # @overload partition_index(kth, axis: nil)
227
292
  # @return [CArray] the indices that partition about the `kth` string.
228
- def partition_index (*args, **kw)
293
+ def partition_index(*args, **kw)
229
294
  to_string.partition_index(*args, **kw)
230
295
  end
231
296
 
@@ -238,7 +303,7 @@ class CAConstString
238
303
  # `object` array (a fillable string container), honouring a block.
239
304
  # @overload template(type = nil, **opts)
240
305
  # @return [CArray]
241
- def template (*args, &block)
306
+ def template(*args, &block)
242
307
  return super unless args.empty? # a type was given: generic plain path
243
308
  out = CArray.object(*shape)
244
309
  if block
@@ -266,75 +331,76 @@ class CAConstString
266
331
 
267
332
  # @overload unique(sort: false)
268
333
  # @return [CAConstString] the distinct strings.
269
- def unique (sort: false)
334
+ def unique(sort: false)
270
335
  const_string_lift(to_string.unique(sort: sort))
271
336
  end
272
337
 
273
338
  # @overload value_counts(sort: false)
274
339
  # @return [Array(CAConstString, CArray)] `[values, counts]`.
275
- def value_counts (sort: false)
340
+ def value_counts(sort: false)
276
341
  values, counts = to_string.value_counts(sort: sort)
277
342
  [const_string_lift(values), counts]
278
343
  end
279
344
 
280
345
  # @overload nunique(axis: nil, keep_axis: false)
281
346
  # @return [Integer, CArray] the number of distinct strings.
282
- def nunique (axis: nil, keep_axis: false)
347
+ def nunique(axis: nil, keep_axis: false)
283
348
  to_string.nunique(axis: axis, keep_axis: keep_axis)
284
349
  end
285
350
 
286
351
  # @overload mode(axis: nil)
287
352
  # @return [CAConstString, Array] the most frequent string(s).
288
- def mode (axis: nil)
353
+ def mode(axis: nil)
289
354
  r = to_string.mode(axis: axis)
290
355
  r.is_a?(Array) ? r.map { |c| const_string_lift(c) } : const_string_lift(r)
291
356
  end
292
357
 
293
358
  # @overload is_mode(axis: nil)
294
359
  # @return [CArray] boolean, true where the cell holds a modal string.
295
- def is_mode (axis: nil)
360
+ def is_mode(axis: nil)
296
361
  to_string.is_mode(axis: axis)
297
362
  end
298
363
 
299
364
  # @overload mask_duplicates(axis: nil)
300
365
  # @return [CAConstString] a copy with every repeat occurrence masked.
301
- def mask_duplicates (axis: nil)
366
+ def mask_duplicates(axis: nil)
302
367
  const_string_lift(to_string.mask_duplicates(axis: axis))
303
368
  end
304
369
 
305
370
  # @overload is_in(values)
306
371
  # @return [CArray] boolean, true where the cell's string is in `values`.
307
- def is_in (values)
372
+ def is_in(values)
308
373
  to_string.is_in(string_operand(values))
309
374
  end
310
375
 
311
376
  # @overload intersection(other, sort: false)
312
377
  # @return [CAConstString] the distinct strings present in both.
313
- def intersection (other, sort: false)
378
+ def intersection(other, sort: false)
314
379
  const_string_lift(to_string.intersection(string_operand(other), sort: sort))
315
380
  end
316
381
 
317
382
  # @overload difference(other, sort: false)
318
383
  # @return [CAConstString] the distinct strings only `self` has.
319
- def difference (other, sort: false)
384
+ def difference(other, sort: false)
320
385
  const_string_lift(to_string.difference(string_operand(other), sort: sort))
321
386
  end
322
387
 
323
388
  # @overload union(other, sort: false)
324
389
  # @return [CAConstString] the distinct strings of either side.
325
- def union (other, sort: false)
390
+ def union(other, sort: false)
326
391
  const_string_lift(to_string.union(string_operand(other), sort: sort))
327
392
  end
328
393
 
329
394
  # @overload locate_addr(ref)
330
395
  # @return [CArray] for each cell, its address in `ref` (UNDEF when absent).
331
- def locate_addr (ref)
396
+ def locate_addr(ref)
332
397
  to_string.locate_addr(string_operand(ref))
333
398
  end
334
399
 
335
400
  # @overload categorize(labels: nil, sort_labels: false)
336
401
  # @return [CACategorical] categories keyed on the strings.
337
- def categorize (labels: nil, sort_labels: false)
402
+ def categorize(labels: nil, sort_labels: false)
403
+ sort_labels = CArray.send(:bool_arg, sort_labels, "sort_labels", "categorize")
338
404
  # Discovery reads the bytes (a String per distinct value only); a given
339
405
  # vocabulary is matched through the strings.
340
406
  return super if labels.nil?
@@ -369,7 +435,7 @@ class CAConstString
369
435
 
370
436
  # A CAConstString operand has to be decoded too; anything else (a CAString,
371
437
  # an object array, an Array) already compares as strings.
372
- private def string_operand (other)
438
+ private def string_operand(other)
373
439
  other.is_a?(CAConstString) ? other.to_string : other
374
440
  end
375
441
 
@@ -377,7 +443,7 @@ class CAConstString
377
443
  # The encoding has to be carried: the builder checks each element against
378
444
  # the column's, so a Shift_JIS column rebuilt as the UTF-8 default raised
379
445
  # rather than coming back.
380
- private def const_string_lift (x)
446
+ private def const_string_lift(x)
381
447
  x.is_a?(CArray) ? CArray.const_string(x, encoding: encoding) : x
382
448
  end
383
449