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
@@ -35,7 +35,7 @@ require "carray/distance_limit"
35
35
  # reduction, takes no part in the reductions.
36
36
  #
37
37
  # The windows overlap, so the per-cell operations of the partition members
38
- # (`map`, the scans, `lag`, `order`, `strip_mask`) are not offered.
38
+ # (`map`, the scans, `lag`, `rank_index`, `strip_mask`) are not offered.
39
39
  class CAKeyWindowIterator < CAIterator
40
40
 
41
41
  EXCLUDE = [nil, :lower, :upper, :both].freeze
@@ -52,13 +52,17 @@ class CAKeyWindowIterator < CAIterator
52
52
  # interval : the grid step of the key, for {#is_complete}.
53
53
  # at : nil, or the points the windows sit at, a 1-D CArray of the
54
54
  # key's kind; the answers are then one per point.
55
- def initialize (value, on:, before: nil, after: nil, exclude: :lower,
55
+ def initialize(value, on:, before: nil, after: nil, exclude: :lower,
56
56
  by: nil, interval: nil, at: nil)
57
- @value = value
58
- unless on.is_a?(CArray) && on.ndim == 1 && on.elements == value.dim0
57
+ @source = value
58
+ unless on.is_a?(CArray)
59
+ raise TypeError,
60
+ "windows: on: is a CArray of one key per row (got #{on.class})"
61
+ end
62
+ unless on.ndim == 1 && on.elements == value.shape[0]
59
63
  raise ArgumentError,
60
64
  "windows: on: is a CArray of one key per row " \
61
- "(#{value.dim0} rows)"
65
+ "(#{value.shape[0]} rows)"
62
66
  end
63
67
  unless EXCLUDE.include?(exclude)
64
68
  raise ArgumentError,
@@ -76,8 +80,10 @@ class CAKeyWindowIterator < CAIterator
76
80
  raise ArgumentError, "windows: at: is a one-dimensional CArray of points"
77
81
  end
78
82
  end
79
- @key = on
80
- @n = value.dim0
83
+ # Taken as it is now, as the windows are: first_key, last_key and bounds
84
+ # read it later.
85
+ @key = on.copy
86
+ @n = value.shape[0]
81
87
  # Taken as they are now, as the windows are: bounds reads them later.
82
88
  @points = at&.copy
83
89
  @nout = at.nil? ? @n : at.elements
@@ -99,7 +105,6 @@ class CAKeyWindowIterator < CAIterator
99
105
  rows = CArray.int64(@n).seq[valid].copy
100
106
  perm = CArray.sort_addr(groups.project(rows), keys.project(rows), rows)
101
107
  @order = rows.project(perm)
102
- m = @order.elements
103
108
  sk = keys.project(@order).copy
104
109
  sg = groups.project(@order).int64.copy
105
110
 
@@ -147,17 +152,18 @@ class CAKeyWindowIterator < CAIterator
147
152
 
148
153
  # The same windows over another value with one row per key (a CAFrame
149
154
  # column): the sorting and the window bounds are not computed again.
150
- def for_value (value) # :nodoc:
151
- unless value.dim0 == @n
155
+ def for_value(value) # :nodoc:
156
+ unless value.shape[0] == @n
152
157
  raise ArgumentError,
153
158
  "windows: the value has shape #{value.shape.inspect}; " \
154
159
  "windows on a key take one row per key (#{@n} rows)"
155
160
  end
156
161
  other = dup
157
- other.instance_variable_set(:@value, value)
162
+ other.instance_variable_set(:@source, value)
158
163
  other.instance_variable_set(:@segments, segments_of(value))
159
164
  other
160
165
  end
166
+ private :for_value
161
167
 
162
168
  # @overload inspect
163
169
  # Returns a one-line summary.
@@ -248,67 +254,84 @@ class CAKeyWindowIterator < CAIterator
248
254
  :count, :count_not_masked, :count_masked, :all, :any,
249
255
  :min_index, :max_index], :segments_in_rows)
250
256
 
251
- private def segments_in_rows (op, *args, **kw)
257
+ private def segments_in_rows(op, *args, **kw)
258
+ refuse_axis_form(kw)
252
259
  over_columns { |seg| in_rows(seg.public_send(op, *args, **kw)) }
253
260
  end
254
261
 
255
262
  # @overload percentile(p)
256
- # Returns each window's p-th percentile.
263
+ # Returns each window's p-th percentile, with p in the forms
264
+ # `CArray#percentile` takes and the answer in the form p was written in.
257
265
  # @return [CArray, Array<CArray>]
258
- def percentile (p, **kw)
259
- over_columns { |seg| in_rows(seg.percentile(p, **kw)) }
266
+ def percentile(*pers, **kw)
267
+ refuse_axis_form(kw)
268
+ CAIterator.send(:percentile_in_form, pers) { |p|
269
+ over_columns { |seg| in_rows(seg.percentile(p, **kw)) }
270
+ }
260
271
  end
261
272
 
262
273
  # @overload quantile
263
274
  # Returns each window's five-number summary `[min, Q1, median, Q3, max]`.
264
275
  # @return [Array<CArray>]
265
- def quantile (**kw)
276
+ def quantile(*args, **kw)
277
+ refuse_axis_form(kw)
278
+ CAIterator.send(:refuse_quantile_arguments, args)
266
279
  over_columns { |seg| in_rows(seg.quantile(**kw)) }
267
280
  end
268
281
 
269
282
  # @overload minmax
270
283
  # Returns `[min, max]` of each window.
271
284
  # @return [Array(CArray, CArray)]
272
- def minmax (**kw)
285
+ def minmax(**kw)
286
+ refuse_axis_form(kw)
273
287
  over_columns { |seg| in_rows(seg.minmax(**kw)) }
274
288
  end
275
289
 
276
290
  # @overload wsum(weights)
277
291
  # Returns each window's weighted sum; `weights` is shaped like the value.
278
292
  # @return [CArray]
279
- def wsum (weights, **kw)
293
+ def wsum(weights, **kw)
294
+ refuse_axis_form(kw)
295
+ CArray.send(:refuse_scalar_weights, "wsum", "the value") if weights.is_a?(Numeric)
280
296
  over_columns { |seg, j| in_rows(seg.wsum(cells_of(weights, j), **kw)) }
281
297
  end
282
298
 
283
299
  # @overload wmean(weights)
284
300
  # Returns each window's weighted mean.
285
301
  # @return [CArray]
286
- def wmean (weights, **kw)
302
+ def wmean(weights, **kw)
303
+ refuse_axis_form(kw)
304
+ CArray.send(:refuse_scalar_weights, "wmean", "the value") if weights.is_a?(Numeric)
287
305
  over_columns { |seg, j| in_rows(seg.wmean(cells_of(weights, j), **kw)) }
288
306
  end
289
307
 
290
308
  # @overload min_addr(min_count: nil, fill_value: nil)
291
309
  # Returns the row of each window's minimum (int64).
292
310
  # @return [CArray]
293
- def min_addr (fill_value: nil, **kw)
294
- over_columns { |seg| winner_row(seg.min_addr(**kw), fill_value) }
311
+ def min_addr(fill_value: nil, **kw)
312
+ refuse_axis_form(kw)
313
+ over_columns { |seg| winner_row(seg.min_addr(**kw), fill_value, :min_addr) }
295
314
  end
296
315
 
297
316
  # @overload max_addr(min_count: nil, fill_value: nil)
298
317
  # Returns the row of each window's maximum (int64).
299
318
  # @return [CArray]
300
- def max_addr (fill_value: nil, **kw)
301
- over_columns { |seg| winner_row(seg.max_addr(**kw), fill_value) }
319
+ def max_addr(fill_value: nil, **kw)
320
+ refuse_axis_form(kw)
321
+ over_columns { |seg| winner_row(seg.max_addr(**kw), fill_value, :max_addr) }
302
322
  end
303
323
 
304
324
  # @overload reduce(*args, data_type: nil) { |piece| ... }
305
325
  # Folds each window with the block, as {CASegmentIterator#reduce}.
306
326
  # @return [CArray]
307
- def reduce (*args, **kw, &block)
327
+ def reduce(*args, **kw, &block)
328
+ if args.size > 1
329
+ raise ArgumentError, "reduce: wrong number of arguments (given #{args.size}, expected 0..1)"
330
+ end
308
331
  if @segments.is_a?(Array)
309
332
  raise NotImplementedError,
310
333
  "reduce: windows over a value of several columns (shape " \
311
- "#{@value.shape.inspect}) have no block fold; reduce a column " \
334
+ "#{@source.shape.inspect}) have no block fold; reduce a column " \
312
335
  "at a time (value[nil, j])"
313
336
  end
314
337
  in_rows(@segments.reduce(*args, **kw, &block))
@@ -322,15 +345,15 @@ class CAKeyWindowIterator < CAIterator
322
345
  return to_enum(:each) unless block_given?
323
346
  pieces = []
324
347
  if @segments.is_a?(Array)
325
- rest = [nil] * (@value.ndim - 1)
348
+ rest = [nil] * (@source.ndim - 1)
326
349
  (@offsets.elements - 1).times do |p|
327
350
  rows = @src_addr[@offsets[p]...@offsets[p + 1]]
328
- pieces << @value[rows, *rest]
351
+ pieces << @source[rows, *rest]
329
352
  end
330
353
  else
331
354
  @segments.each { |piece| pieces << piece }
332
355
  end
333
- empty = @value[0...0, *[nil] * (@value.ndim - 1)]
356
+ empty = @source[0...0, *[nil] * (@source.ndim - 1)]
334
357
  @nout.times do |r|
335
358
  p = @position[r]
336
359
  yield(UNDEF.equal?(p) ? empty : pieces[p])
@@ -343,22 +366,20 @@ class CAKeyWindowIterator < CAIterator
343
366
  private
344
367
 
345
368
  # Answers per sorted row, put back in row order.
346
- def in_rows (result)
369
+ def in_rows(result)
347
370
  return result.map { |r| in_rows(r) } if result.is_a?(Array)
348
371
  result.project(@position)
349
372
  end
350
373
 
351
374
  # A winner's address among the gathered cells, as a row of self, in row
352
375
  # order; fill_value fills the windows with none, as the core fills it.
353
- def winner_row (addr, fill_value)
354
- out = in_rows(@src_addr.project(addr))
355
- out[out.is_masked] = fill_value unless fill_value.nil?
356
- out
376
+ def winner_row(addr, fill_value, op)
377
+ CArray.__fill_undefined_result_cells__(in_rows(@src_addr.project(addr)), fill_value, op)
357
378
  end
358
379
 
359
380
  # The row at sorted position pos[i] of the window of sorted row i, UNDEF
360
381
  # where the window is empty.
361
- def edge_addr (pos)
382
+ def edge_addr(pos)
362
383
  return CArray.int64(0) if pos.elements == 0
363
384
  out = @order.project(pos)
364
385
  out[(@hi - @lo).le(0)] = UNDEF
@@ -368,9 +389,9 @@ class CAKeyWindowIterator < CAIterator
368
389
  # A weights array taken through the same cells as the value: one weight
369
390
  # per row, or for a value of several columns, weights shaped like it
370
391
  # (column +j+ of them).
371
- def cells_of (weights, j = nil)
392
+ def cells_of(weights, j = nil)
372
393
  return weights unless weights.is_a?(CArray)
373
- if j && weights.ndim > 1 && weights.dim0 == @n
394
+ if j && weights.ndim > 1 && weights.shape[0] == @n
374
395
  weights = weights.reshape(@n, weights.elements / @n)[nil, j]
375
396
  end
376
397
  return weights unless weights.elements == @n
@@ -380,7 +401,7 @@ class CAKeyWindowIterator < CAIterator
380
401
  # The segment iterators the reductions run on: one for a value with one
381
402
  # cell per row, and for a value of several columns (each row a profile)
382
403
  # one per trailing column, every one over the same windows.
383
- def segments_of (value)
404
+ def segments_of(value)
384
405
  return CASegmentIterator.new(value.project(@src_addr), offsets: @offsets) if value.ndim == 1
385
406
  width = value.shape[1..].inject(1, :*)
386
407
  flat = value.reshape(@n, width)
@@ -389,15 +410,22 @@ class CAKeyWindowIterator < CAIterator
389
410
  end
390
411
  end
391
412
 
413
+ # The windows are fixed by the key, so a reduction takes no axis. Refused
414
+ # here so that the refusal names this class, not the segment iterator the
415
+ # reduction is handed to.
416
+ def refuse_axis_form(kw)
417
+ raise NotImplementedError, "#{self.class} has no axis: form" unless kw[:axis].nil?
418
+ end
419
+
392
420
  # A reduction of the windows: as it is for a value with one cell per row;
393
421
  # for a value of several columns, of each column, the answers laid side
394
422
  # by side in the value's trailing shape, as a group reduces a profile
395
423
  # column along the rows.
396
- def over_columns (&block)
424
+ def over_columns(&block)
397
425
  return block.call(@segments, nil) unless @segments.is_a?(Array)
398
- trail = @value.shape[1..]
426
+ trail = @source.shape[1..]
399
427
  if @segments.empty?
400
- one = block.call(CASegmentIterator.new(CArray.new(@value.data_type, [0], bytes: @value.bytes),
428
+ one = block.call(CASegmentIterator.new(CArray.new(@source.data_type, [0], bytes: @source.bytes),
401
429
  offsets: CArray.int64(1)), nil)
402
430
  return lay_out(Array.new(0), one, trail)
403
431
  end
@@ -408,18 +436,22 @@ class CAKeyWindowIterator < CAIterator
408
436
  # Answers of the columns (each one per row) as one array of the rows and
409
437
  # the trailing shape; an Array of answers (minmax, quantile) answer by
410
438
  # answer.
411
- def lay_out (results, sample, trail)
439
+ def lay_out(results, sample, trail)
412
440
  if sample.is_a?(Array)
413
441
  return sample.each_index.map { |k| lay_out(results.map { |r| r[k] }, sample[k], trail) }
414
442
  end
415
- out = CArray.new(sample.data_type, [@nout, *trail], bytes: sample.bytes)
443
+ # A Face that holds no new value cannot be written cell by cell, so its
444
+ # values are held as the surface objects they are.
445
+ surface = sample.face? && !sample.__send__(:holds_new_values?)
446
+ shape = [@nout, *trail]
447
+ out = surface ? CArray.new(CA_OBJECT, shape) : sample.__send__(:blank_like, shape)
416
448
  flat = out.reshape(@nout, results.size)
417
- results.each_with_index { |r, j| flat[nil, j] = r }
449
+ results.each_with_index { |r, j| flat[nil, j] = surface ? r.to_a : r }
418
450
  out
419
451
  end
420
452
 
421
453
  # The rows of every window, window after window.
422
- def window_cells (counts, starts, total)
454
+ def window_cells(counts, starts, total)
423
455
  return CArray.int64(0) if total == 0
424
456
  nz = counts.gt(0)
425
457
  owner = CArray.int64(counts.elements).seq[nz].copy
@@ -442,7 +474,7 @@ class CAKeyWindowIterator < CAIterator
442
474
  # Points (at:) are converted together with the keys -- joined, converted
443
475
  # once, and split -- so that both are measured from the same origin on
444
476
  # the same grid.
445
- def key_numbers (key, before, after, interval, points = nil)
477
+ def key_numbers(key, before, after, interval, points = nil)
446
478
  if key.is_a?(CATime) || key.is_a?(CATimedelta)
447
479
  if points
448
480
  unless points.instance_of?(key.class)
@@ -450,13 +482,13 @@ class CAKeyWindowIterator < CAIterator
450
482
  "windows: at: holds points of the key's kind, a #{key.class} " \
451
483
  "(got #{CArray::Inspector.type_label(points)})"
452
484
  end
453
- unless CATimeUnitAlgebra.same_group?(key.unit.base, points.unit.base)
485
+ unless CATime::UnitAlgebra.same_group?(key.unit.base, points.unit.base)
454
486
  raise ArgumentError,
455
487
  "windows: at: holds points in #{points.unit.count} #{points.unit.base}, " \
456
488
  "which cannot be measured on a key in #{key.unit.count} #{key.unit.base} " \
457
489
  "(months and years have no fixed length); convert one with to_unit"
458
490
  end
459
- unit = CATimeUnitAlgebra.finer(key.unit, points.unit)
491
+ unit = CATime::UnitAlgebra.finer(key.unit, points.unit)
460
492
  key, points = key.to_unit(unit), points.to_unit(unit)
461
493
  end
462
494
  widths = [before, after, interval].map { |w| w.nil? ? nil : duration_in_ticks(w, key.unit) }
@@ -524,53 +556,53 @@ class CAKeyWindowIterator < CAIterator
524
556
  end
525
557
 
526
558
  # Whether a width is a whole number (an infinite one is not).
527
- def whole? (w)
559
+ def whole?(w)
528
560
  !(w.is_a?(Float) && w.infinite?) && w.to_r.denominator == 1
529
561
  end
530
562
 
531
563
  # A width on integer keys as an integer: its floor, or the int64 limit
532
564
  # for an infinite one.
533
- def integer_width (w)
565
+ def integer_width(w)
534
566
  return 2**63 - 1 if w.is_a?(Float) && w.infinite?
535
567
  [w.to_r.floor, 2**63 - 1].min
536
568
  end
537
569
 
538
570
  # The keys followed by the points, one array, when there are points.
539
- def joined (keys, points)
571
+ def joined(keys, points)
540
572
  points ? CArray.concatenate([keys, points]) : keys
541
573
  end
542
574
 
543
575
  # The keys and the points (nil without them) back out of a joined array.
544
- def split (numbers, points)
576
+ def split(numbers, points)
545
577
  return [numbers, nil] unless points
546
578
  [numbers[0...@n].copy, numbers[@n...numbers.elements].copy]
547
579
  end
548
580
 
549
- def window_bound (width, op)
581
+ def window_bound(width, op)
550
582
  key = @points || @key
551
- return key if width.nil?
583
+ return key.copy if width.nil?
552
584
  if key.is_a?(CATime) || key.is_a?(CATimedelta)
553
- key = key.to_unit(CATimeUnitAlgebra.finer(key.unit, width.unit))
585
+ key = key.to_unit(CATime::UnitAlgebra.finer(key.unit, width.unit))
554
586
  end
555
587
  key.public_send(op, width)
556
588
  end
557
589
 
558
590
  # A duration in ticks of the key's unit, as a Rational.
559
- def duration_in_ticks (w, unit)
591
+ def duration_in_ticks(w, unit)
560
592
  count, base = w.value * w.unit.count, w.unit.base
561
- unless CATimeUnitAlgebra.same_group?(base, unit.base)
593
+ unless CATime::UnitAlgebra.same_group?(base, unit.base)
562
594
  raise ArgumentError,
563
595
  "windows: a width of #{count} #{base} cannot be measured on a key in " \
564
596
  "#{unit.count} #{unit.base} (months and years have no fixed length)"
565
597
  end
566
- Rational(count) * CATimeUnitAlgebra.base_ratio(base) /
567
- (unit.count * CATimeUnitAlgebra.base_ratio(unit.base))
598
+ Rational(count) * CATime::UnitAlgebra.base_ratio(base) /
599
+ (unit.count * CATime::UnitAlgebra.base_ratio(unit.base))
568
600
  end
569
601
 
570
602
  # An integer key as int64 numbers scaled by `scale`: the values
571
603
  # themselves when they fit, else measured from the smallest value,
572
604
  # refused when even the span does not fit.
573
- def from_smallest (key, scale, shift: false)
605
+ def from_smallest(key, scale, shift: false)
574
606
  lo = key.min
575
607
  return key.int64 if lo == UNDEF
576
608
  hi = key.max
@@ -588,7 +620,7 @@ class CAKeyWindowIterator < CAIterator
588
620
  scale == 1 ? numbers : numbers * scale
589
621
  end
590
622
 
591
- def check_widths (bw, aw, iv)
623
+ def check_widths(bw, aw, iv)
592
624
  return unless iv
593
625
  unless iv > 0
594
626
  raise ArgumentError, "windows: interval: is positive"
@@ -604,7 +636,7 @@ class CAKeyWindowIterator < CAIterator
604
636
 
605
637
  # A code per row for the by: keys, equal codes for equal keys, none for a
606
638
  # row with a masked by: cell; zeros when there is no by:.
607
- def group_codes (by)
639
+ def group_codes(by)
608
640
  return CArray.int64(@n).fill(0) if by.nil?
609
641
  keys = by.is_a?(Array) ? by : [by]
610
642
  if keys.empty?