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
@@ -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)
@@ -133,11 +143,11 @@ class CArray
133
143
  # The two branches of a selection, in a form that holds the cells of both.
134
144
  # A Face whose cells differ by more than their values (a time's unit, a
135
145
  # fixed string's width) brings them to one here, whichever branch is which.
136
- private def branches_in_common (x, y)
146
+ private def branches_in_common(x, y)
137
147
  [x, y]
138
148
  end
139
149
 
140
- private def face_then_else (x, y, face)
150
+ private def face_then_else(x, y, face)
141
151
  # A Face branch decides the result: the other branch is written into a
142
152
  # copy of it through the Face's own store, so an instant stays an instant.
143
153
  # Promoting the two branches to a common data type would compare a Face's
@@ -182,7 +192,7 @@ class CArray
182
192
  # @param b [CArray, Numeric, Object] replacement value(s).
183
193
  # @return [CArray] new array.
184
194
  # @raise [ArgumentError] when `cond` is not a boolean CArray.
185
- def replace_where (cond, b)
195
+ def replace_where(cond, b)
186
196
  unless cond.is_a?(CArray) && cond.boolean?
187
197
  raise ArgumentError,
188
198
  "replace_where: cond must be a boolean CArray (data_type == CA_BOOLEAN)"
@@ -214,6 +224,8 @@ class CArray
214
224
  # @param data_type [Symbol, Integer, nil] override for the result
215
225
  # data type.
216
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).
217
229
  # @raise [ArgumentError] when `cond` is not a same-shape
218
230
  # boolean CArray.
219
231
  # @example
@@ -222,7 +234,7 @@ class CArray
222
234
  # ->(v) { v.log }, # domain-safe: only positive cells
223
235
  # ->(v) { -v })
224
236
  # # => [2.0, 1.0, -0.0, 0.0, 0.6931..., 1.0986...]
225
- def conditional (cond, then_fn, else_fn, data_type: nil)
237
+ def conditional(cond, then_fn, else_fn, data_type: nil)
226
238
  unless cond.is_a?(CArray) && cond.boolean? && cond.shape == self.shape
227
239
  raise ArgumentError,
228
240
  "conditional: cond must be a boolean CArray with same shape as self"
@@ -239,13 +251,13 @@ class CArray
239
251
  # to the subset shape; wrap it here so the scatter step below sees a
240
252
  # same-length CArray.
241
253
  unless y_then.is_a?(CArray)
242
- 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)
243
255
  end
244
256
  unless y_else.is_a?(CArray)
245
- 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)
246
258
  end
247
259
 
248
- dt = data_type || CArray.send(:selection_type, [y_then, y_else])
260
+ dt = data_type || CArray.send(:selection_type, [y_then, y_else], "conditional")
249
261
  out = CArray.new(dt, self.shape)
250
262
  out[cond] = y_then
251
263
  out[cond.not] = y_else
@@ -281,6 +293,8 @@ class CArray
281
293
  # @param data_type [Symbol, Integer, nil] override for the result
282
294
  # data type.
283
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).
284
298
  # @raise [ArgumentError] on size mismatch, empty `condlist`, or a
285
299
  # non-boolean / wrong-shape entry in `condlist`.
286
300
  # @example
@@ -289,7 +303,7 @@ class CArray
289
303
  # [-x, x * 10],
290
304
  # default: 999)
291
305
  # # => [5.0, 3.0, -10.0, 0.0, 10.0, 999.0]
292
- def self.select (condlist, choicelist, default: (no_default = true; 0), data_type: nil)
306
+ def self.select(condlist, choicelist, default: (no_default = true; 0), data_type: nil)
293
307
  unless condlist.is_a?(Array) && choicelist.is_a?(Array)
294
308
  raise ArgumentError, "select: condlist and choicelist must be Arrays"
295
309
  end
@@ -315,7 +329,7 @@ class CArray
315
329
  end
316
330
  end
317
331
 
318
- dt = data_type || selection_type(choicelist + [default])
332
+ dt = data_type || selection_type(choicelist + [default], "select")
319
333
  # `default` can be either a same-shape CArray (per-cell fallback) or a
320
334
  # scalar (broadcast to every cell).
321
335
  default_full = default.is_a?(CArray) && !default.scalar?
@@ -353,7 +367,7 @@ class CArray
353
367
  # select over Face choices. Each condition is a then_else on the choices
354
368
  # below it, so the Faces meet as they do there; a Face that cannot be
355
369
  # written selects among its values.
356
- def self.select_among_faces (condlist, choicelist, fallback, face, shape)
370
+ def self.select_among_faces(condlist, choicelist, fallback, face, shape)
357
371
  if face.is_a?(CAConstString)
358
372
  choices = choicelist.map { |v| v.is_a?(CAConstString) ? v.to_string : v }
359
373
  text = select(condlist, choices, default: fallback)
@@ -379,20 +393,33 @@ class CArray
379
393
  end
380
394
  private_class_method :select_among_faces
381
395
 
382
- def self.selection_type (values)
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)
383
400
  typed = values.reject { |v| v.equal?(UNDEF) }
384
401
  if typed.empty? || typed.any? { |v| v.is_a?(String) || v.is_a?(Symbol) }
385
- CA_OBJECT
386
- else
387
- 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
388
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)
389
416
  end
390
417
  private_class_method :selection_type
391
418
 
392
419
  # Whether a selection among `values` gives text: every value that is not
393
420
  # UNDEF is a String or a CAString, and there is at least one. The result
394
421
  # is then a CAString rather than an object array of Strings.
395
- def self.selection_text? (values)
422
+ def self.selection_text?(values)
396
423
  present = values.reject { |v| v.equal?(UNDEF) }
397
424
  !present.empty? && present.all? { |v| v.is_a?(String) || v.is_a?(CAString) }
398
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.
@@ -97,7 +99,7 @@ class CAConstString
97
99
  # window). A cell is a byte range of the shared buffer, which has no room
98
100
  # for a String it does not hold, so only UNDEF can be written.
99
101
  # @raise [ArgumentError] for any value but UNDEF.
100
- def element_to_storage (value)
102
+ private def element_to_storage(value)
101
103
  raise ArgumentError,
102
104
  "a CAConstString can't hold a new String (its strings share one " \
103
105
  "buffer); use UNDEF, or to_string first"
@@ -127,7 +129,7 @@ class CAConstString
127
129
  # and offsets. With no `axis:` the array is flattened first, as
128
130
  # `CArray#sort` does.
129
131
  # @return [CAConstString]
130
- def sort (*args, **kw)
132
+ def sort(*args, **kw)
131
133
  addr = sort_addr(*args, **kw)
132
134
  kw[:axis].nil? ? self[addr.flatten] : self[addr]
133
135
  end
@@ -136,50 +138,45 @@ class CAConstString
136
138
  # Returns an owned sorted {CAConstString}; the materialised counterpart
137
139
  # to {#sort}.
138
140
  # @return [CAConstString]
139
- def sort_copy (*args, **kw)
141
+ def sort_copy(*args, **kw)
140
142
  sort(*args, **kw).copy
141
143
  end
142
144
 
143
145
  # @overload sort_addr(axis: nil, kind: :quick, masked_position: :last)
144
146
  # Returns the view-flat addresses that index a sort by string order.
145
147
  # @return [CArray] `:int64` addresses.
146
- def sort_addr (*args, **kw)
148
+ def sort_addr(*args, **kw)
147
149
  # The native scan has no notion of an incomparable sentinel, so a masked
148
150
  # column goes the same way a per-axis one does.
149
- 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
150
156
  to_string.sort_addr(*args, **kw)
151
157
  end
152
158
 
153
159
  # @overload sort_index(axis: nil, kind: :quick, masked_position: :last)
154
160
  # Returns the per-fiber indices that index a sort by string order.
155
161
  # @return [CArray] `:int64` indices.
156
- def sort_index (*args, **kw)
157
- if args.empty? && kw.empty? && ! has_mask?
158
- # With no axis the indices are those of the flattened array, which are
159
- # its addresses.
160
- addr = sort_addr_by_bytes
161
- return ndim == 1 ? addr : addr.flatten.copy
162
- 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?
163
166
  to_string.sort_index(*args, **kw)
164
167
  end
165
168
 
166
- # @overload rank_index(axis: nil)
169
+ # @overload rank_index(axis: nil, descending: false, method: :ordinal, kind: :quick)
167
170
  # @return [CArray] each cell's rank in string order.
168
- def rank_index (*args, **kw)
171
+ def rank_index(*args, **kw)
169
172
  to_string.rank_index(*args, **kw)
170
173
  end
171
174
 
172
- # @overload order(*args)
173
- # @return [CArray] the ordering of the cells by string order.
174
- def order (*args, **kw)
175
- to_string.order(*args, **kw)
176
- end
177
-
178
175
  # @overload min(axis: nil, keep_axis: false)
179
176
  # Returns the byte-smallest string, skipping masked cells; UNDEF when
180
177
  # every cell is masked or the array is empty.
181
178
  # @return [String, CAConstString]
182
- def min (*args, **kw)
179
+ def min(*args, **kw)
183
180
  return min_string_by_bytes || UNDEF if args.empty? && kw.empty?
184
181
  const_string_lift(to_string.min(*args, **kw))
185
182
  end
@@ -187,81 +184,81 @@ class CAConstString
187
184
  # @overload max(axis: nil, keep_axis: false)
188
185
  # Byte-largest counterpart of {#min}.
189
186
  # @return [String, CAConstString]
190
- def max (*args, **kw)
187
+ def max(*args, **kw)
191
188
  return max_string_by_bytes || UNDEF if args.empty? && kw.empty?
192
189
  const_string_lift(to_string.max(*args, **kw))
193
190
  end
194
191
 
195
192
  # @overload minmax(axis: nil, keep_axis: false)
196
193
  # @return [Array] `[min, max]`.
197
- def minmax (*args, **kw)
194
+ def minmax(*args, **kw)
198
195
  return [min, max] if args.empty? && kw.empty?
199
196
  to_string.minmax(*args, **kw).map { |r| const_string_lift(r) }
200
197
  end
201
198
 
202
- # @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)
203
200
  # Clips the strings in byte order (see {CArray#clip}).
204
201
  # @return [CAConstString]
205
- def clip (*args, **kw)
202
+ def clip(*args, **kw)
206
203
  const_string_lift(to_string.clip(*args, **kw))
207
204
  end
208
205
 
209
206
  # A String has no distance, so there is no nearest one; refused in the
210
207
  # words CAString uses.
211
- def search_nearest (*args, **kw)
208
+ def search_nearest(*args, **kw)
212
209
  to_string.search_nearest(*args, **kw)
213
210
  end
214
211
 
215
212
  # @see #search_nearest
216
- def search_nearest_addr (*args, **kw)
213
+ def search_nearest_addr(*args, **kw)
217
214
  to_string.search_nearest_addr(*args, **kw)
218
215
  end
219
216
 
220
217
  # @overload min_index(axis: nil)
221
218
  # @return [Integer, CArray] where the byte-smallest string sits.
222
- def min_index (*args, **kw)
219
+ def min_index(*args, **kw)
223
220
  to_string.min_index(*args, **kw)
224
221
  end
225
222
 
226
223
  # @overload max_index(axis: nil)
227
224
  # @return [Integer, CArray] where the byte-largest string sits.
228
- def max_index (*args, **kw)
225
+ def max_index(*args, **kw)
229
226
  to_string.max_index(*args, **kw)
230
227
  end
231
228
 
232
229
  # @overload min_addr(axis: nil)
233
230
  # @return [Integer, CArray] the flat address of the byte-smallest string.
234
- def min_addr (*args, **kw)
231
+ def min_addr(*args, **kw)
235
232
  to_string.min_addr(*args, **kw)
236
233
  end
237
234
 
238
235
  # @overload max_addr(axis: nil)
239
236
  # @return [Integer, CArray] the flat address of the byte-largest string.
240
- def max_addr (*args, **kw)
237
+ def max_addr(*args, **kw)
241
238
  to_string.max_addr(*args, **kw)
242
239
  end
243
240
 
244
241
  # @overload bsearch(value)
245
242
  # @return [Integer, nil] where a sorted column holds `value`.
246
- def bsearch (*args, **kw)
243
+ def bsearch(*args, **kw)
247
244
  to_string.bsearch(*args, **kw)
248
245
  end
249
246
 
250
247
  # @overload bsearch_addr(value)
251
248
  # @return [Integer, nil] the flat address of `value` in a sorted column.
252
- def bsearch_addr (*args, **kw)
249
+ def bsearch_addr(*args, **kw)
253
250
  to_string.bsearch_addr(*args, **kw)
254
251
  end
255
252
 
256
253
  # @overload search_addr(value)
257
254
  # @return [Integer, nil] the flat address of the first cell equal to `value`.
258
- def search_addr (*args, **kw)
255
+ def search_addr(*args, **kw)
259
256
  to_string.search_addr(*args, **kw)
260
257
  end
261
258
 
262
259
  # @overload match(pattern)
263
260
  # @return [CArray] boolean, as {CAString} answers.
264
- def match (*args)
261
+ def match(*args)
265
262
  to_string.match(*args)
266
263
  end
267
264
 
@@ -277,29 +274,23 @@ class CAConstString
277
274
  # @!method minimum(other)
278
275
  # Another name of {#pmin}.
279
276
  # @return [CAConstString]
280
- # @!method fmax(other)
281
- # Another name of {#pmax}.
282
- # @return [CAConstString]
283
- # @!method fmin(other)
284
- # Another name of {#pmin}.
285
- # @return [CAConstString]
286
277
  CArray.send(:define_named_methods, self,
287
- [:pmax, :pmin, :maximum, :minimum, :fmax, :fmin], :pick_among_strings)
278
+ [:pmax, :pmin, :maximum, :minimum], :pick_among_strings)
288
279
 
289
- private def pick_among_strings (op, other)
280
+ private def pick_among_strings(op, other)
290
281
  refuse_unordered_operands(op, other)
291
282
  const_string_lift(to_string.public_send(op, string_operand(other)))
292
283
  end
293
284
 
294
285
  # @overload partition_copy(kth, axis: nil)
295
286
  # @return [CAConstString] partitioned about the `kth` string in order.
296
- def partition_copy (*args, **kw)
287
+ def partition_copy(*args, **kw)
297
288
  const_string_lift(to_string.partition_copy(*args, **kw))
298
289
  end
299
290
 
300
291
  # @overload partition_index(kth, axis: nil)
301
292
  # @return [CArray] the indices that partition about the `kth` string.
302
- def partition_index (*args, **kw)
293
+ def partition_index(*args, **kw)
303
294
  to_string.partition_index(*args, **kw)
304
295
  end
305
296
 
@@ -312,7 +303,7 @@ class CAConstString
312
303
  # `object` array (a fillable string container), honouring a block.
313
304
  # @overload template(type = nil, **opts)
314
305
  # @return [CArray]
315
- def template (*args, &block)
306
+ def template(*args, &block)
316
307
  return super unless args.empty? # a type was given: generic plain path
317
308
  out = CArray.object(*shape)
318
309
  if block
@@ -340,75 +331,76 @@ class CAConstString
340
331
 
341
332
  # @overload unique(sort: false)
342
333
  # @return [CAConstString] the distinct strings.
343
- def unique (sort: false)
334
+ def unique(sort: false)
344
335
  const_string_lift(to_string.unique(sort: sort))
345
336
  end
346
337
 
347
338
  # @overload value_counts(sort: false)
348
339
  # @return [Array(CAConstString, CArray)] `[values, counts]`.
349
- def value_counts (sort: false)
340
+ def value_counts(sort: false)
350
341
  values, counts = to_string.value_counts(sort: sort)
351
342
  [const_string_lift(values), counts]
352
343
  end
353
344
 
354
345
  # @overload nunique(axis: nil, keep_axis: false)
355
346
  # @return [Integer, CArray] the number of distinct strings.
356
- def nunique (axis: nil, keep_axis: false)
347
+ def nunique(axis: nil, keep_axis: false)
357
348
  to_string.nunique(axis: axis, keep_axis: keep_axis)
358
349
  end
359
350
 
360
351
  # @overload mode(axis: nil)
361
352
  # @return [CAConstString, Array] the most frequent string(s).
362
- def mode (axis: nil)
353
+ def mode(axis: nil)
363
354
  r = to_string.mode(axis: axis)
364
355
  r.is_a?(Array) ? r.map { |c| const_string_lift(c) } : const_string_lift(r)
365
356
  end
366
357
 
367
358
  # @overload is_mode(axis: nil)
368
359
  # @return [CArray] boolean, true where the cell holds a modal string.
369
- def is_mode (axis: nil)
360
+ def is_mode(axis: nil)
370
361
  to_string.is_mode(axis: axis)
371
362
  end
372
363
 
373
364
  # @overload mask_duplicates(axis: nil)
374
365
  # @return [CAConstString] a copy with every repeat occurrence masked.
375
- def mask_duplicates (axis: nil)
366
+ def mask_duplicates(axis: nil)
376
367
  const_string_lift(to_string.mask_duplicates(axis: axis))
377
368
  end
378
369
 
379
370
  # @overload is_in(values)
380
371
  # @return [CArray] boolean, true where the cell's string is in `values`.
381
- def is_in (values)
372
+ def is_in(values)
382
373
  to_string.is_in(string_operand(values))
383
374
  end
384
375
 
385
376
  # @overload intersection(other, sort: false)
386
377
  # @return [CAConstString] the distinct strings present in both.
387
- def intersection (other, sort: false)
378
+ def intersection(other, sort: false)
388
379
  const_string_lift(to_string.intersection(string_operand(other), sort: sort))
389
380
  end
390
381
 
391
382
  # @overload difference(other, sort: false)
392
383
  # @return [CAConstString] the distinct strings only `self` has.
393
- def difference (other, sort: false)
384
+ def difference(other, sort: false)
394
385
  const_string_lift(to_string.difference(string_operand(other), sort: sort))
395
386
  end
396
387
 
397
388
  # @overload union(other, sort: false)
398
389
  # @return [CAConstString] the distinct strings of either side.
399
- def union (other, sort: false)
390
+ def union(other, sort: false)
400
391
  const_string_lift(to_string.union(string_operand(other), sort: sort))
401
392
  end
402
393
 
403
394
  # @overload locate_addr(ref)
404
395
  # @return [CArray] for each cell, its address in `ref` (UNDEF when absent).
405
- def locate_addr (ref)
396
+ def locate_addr(ref)
406
397
  to_string.locate_addr(string_operand(ref))
407
398
  end
408
399
 
409
400
  # @overload categorize(labels: nil, sort_labels: false)
410
401
  # @return [CACategorical] categories keyed on the strings.
411
- 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")
412
404
  # Discovery reads the bytes (a String per distinct value only); a given
413
405
  # vocabulary is matched through the strings.
414
406
  return super if labels.nil?
@@ -443,7 +435,7 @@ class CAConstString
443
435
 
444
436
  # A CAConstString operand has to be decoded too; anything else (a CAString,
445
437
  # an object array, an Array) already compares as strings.
446
- private def string_operand (other)
438
+ private def string_operand(other)
447
439
  other.is_a?(CAConstString) ? other.to_string : other
448
440
  end
449
441
 
@@ -451,7 +443,7 @@ class CAConstString
451
443
  # The encoding has to be carried: the builder checks each element against
452
444
  # the column's, so a Shift_JIS column rebuilt as the UTF-8 default raised
453
445
  # rather than coming back.
454
- private def const_string_lift (x)
446
+ private def const_string_lift(x)
455
447
  x.is_a?(CArray) ? CArray.const_string(x, encoding: encoding) : x
456
448
  end
457
449
 
@@ -18,7 +18,7 @@
18
18
  # `5.eq(ca)` dispatches to `ca.eq(5)`.
19
19
  # - Symmetric bitwise operators on Integer / TrueClass / FalseClass
20
20
  # so that `5 | bool_ca`, `0xff & flag_ca`, `1 << position_ca` work
21
- # without writing CA_INT(5) on the left.
21
+ # without writing CA_INT32(5) on the left.
22
22
  #
23
23
  # None of these are loaded by default `require "carray"`. Each file
24
24
  # that wants any of them must opt in:
@@ -2,7 +2,7 @@
2
2
 
3
3
  class CArray
4
4
 
5
- # The length a match by distance is limited by -- the +tolerance:+ of
5
+ # The length a match by distance is limited by -- the +tol:+ of
6
6
  # locate_nearest_addr (and of align_nearest_addr and CAFrame#join_asof
7
7
  # through it), the +before:+, +after:+ and +interval:+ of a key window --
8
8
  # read the same way wherever it is given: a length of the key's kind,
@@ -16,14 +16,14 @@ class CArray
16
16
  # unit.
17
17
  #
18
18
  # Every refusal is ArgumentError naming +label+, the keyword as the
19
- # caller says it ("tolerance:", "windows: before:").
19
+ # caller says it ("tol:", "windows: before:").
20
20
  module DistanceLimit
21
21
 
22
22
  ZERO_DURATION = /\A\s*0+\s+([A-Za-z]+)\s*\z/
23
23
 
24
24
  # +width+ as a Numeric (number key) or a CATimedelta::Element (time or
25
25
  # duration key); +nil+ passes through.
26
- def self.read (width, key, label)
26
+ def self.read(width, key, label)
27
27
  return nil if width.nil?
28
28
  if key.is_a?(CATime) || key.is_a?(CATimedelta)
29
29
  duration(width, label)
@@ -32,7 +32,7 @@ class CArray
32
32
  end
33
33
  end
34
34
 
35
- def self.number (width, key, label)
35
+ def self.number(width, key, label)
36
36
  if width.is_a?(String) || width.is_a?(CATimedelta::Element)
37
37
  raise ArgumentError,
38
38
  "#{label} #{width.inspect} is a duration, but the key is " \
@@ -44,7 +44,7 @@ class CArray
44
44
  width
45
45
  end
46
46
 
47
- def self.duration (width, label)
47
+ def self.duration(width, label)
48
48
  element =
49
49
  case width
50
50
  when CATimedelta::Element
@@ -73,5 +73,7 @@ class CArray
73
73
  end
74
74
  element
75
75
  end
76
+
77
+ private_constant :ZERO_DURATION
76
78
  end
77
79
  end