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