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.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1830
- data/README.md +62 -35
- data/carray.gemspec +16 -4
- data/docs/changelogs/CHANGELOG-3.0.md +2464 -0
- data/ext/ca_array_pool.c +4 -5
- data/ext/ca_axis_descriptor.h +45 -30
- data/ext/ca_axis_dispatch.c +7 -4
- data/ext/ca_axis_group.c +12 -15
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +35 -0
- data/ext/ca_binop_dispatch.h +22 -46
- data/ext/ca_categorical_iterator.c +36 -34
- data/ext/ca_compare.c +0 -1
- data/ext/ca_compare.h +8 -1
- data/ext/ca_composite_dispatch.h +6 -0
- data/ext/ca_for_buffer.h +6 -1
- data/ext/ca_group_iter.c +17 -18
- data/ext/ca_iter_state_fields.h +196 -0
- data/ext/ca_iter_substrate.h +84 -103
- data/ext/ca_kernel_iterator.c +125 -89
- data/ext/ca_kernel_iterator.h +229 -1504
- data/ext/ca_kernel_iterator_internal.h +1167 -0
- data/ext/ca_moncmp_dispatch.h +17 -24
- data/ext/ca_monop_dispatch.h +25 -74
- data/ext/ca_obj_array.c +92 -165
- data/ext/ca_obj_bincmp.c +65 -48
- data/ext/ca_obj_binop.c +74 -49
- data/ext/ca_obj_bitarray.c +7 -21
- data/ext/ca_obj_bitfield.c +93 -26
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +34 -34
- data/ext/ca_obj_const_string.c +464 -304
- data/ext/ca_obj_face.c +167 -121
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +127 -34
- data/ext/ca_obj_farray.c +13 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +57 -34
- data/ext/ca_obj_grid.c +149 -113
- data/ext/ca_obj_meld.c +28 -26
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +88 -73
- data/ext/ca_obj_object.c +60 -46
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +10 -14
- data/ext/ca_obj_refer.c +146 -39
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +20 -28
- data/ext/ca_obj_select.c +17 -27
- data/ext/ca_obj_select_axis.c +35 -42
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +33 -29
- data/ext/ca_obj_stride.c +99 -62
- data/ext/ca_obj_string.c +8 -20
- data/ext/ca_obj_tile.c +14 -16
- data/ext/ca_obj_time.c +12 -23
- data/ext/ca_obj_timedelta.c +12 -23
- data/ext/ca_obj_transpose.c +8 -10
- data/ext/ca_obj_triop.c +48 -49
- data/ext/ca_obj_window.c +87 -99
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +97 -27
- data/ext/ca_op_powi.h +9 -28
- data/ext/ca_pow5_table.h +2 -1
- data/ext/ca_rng_normal.h +4 -0
- data/ext/ca_rng_xoshiro256pp.h +3 -0
- data/ext/ca_sort_kernels.h +23 -26
- data/ext/ca_sweep_engine.c +92 -26
- data/ext/ca_sweep_engine.h +22 -15
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +12 -12
- data/ext/carray.h +300 -1009
- data/ext/carray_access.c +320 -207
- data/ext/carray_address_basis.c +128 -59
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +31 -27
- data/ext/carray_call_cfunc.c +174 -285
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +1191 -351
- data/ext/carray_cast_func.rb +68 -8
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +98 -39
- data/ext/carray_copy.c +52 -26
- data/ext/carray_core.c +286 -132
- data/ext/carray_count.c +53 -22
- data/ext/carray_element.c +49 -28
- data/ext/carray_factorize.c +64 -63
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +9 -11
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +127 -230
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1364 -156
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +37 -37
- data/ext/carray_loop.c +27 -23
- data/ext/carray_mask.c +235 -150
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +106 -100
- data/ext/carray_memory_view.c +83 -69
- data/ext/carray_operator.c +490 -128
- data/ext/carray_order.c +163 -84
- data/ext/carray_partition.c +14 -16
- data/ext/carray_random.c +48 -42
- data/ext/carray_round.c +33 -5
- data/ext/carray_scatter.c +57 -39
- data/ext/carray_segment.c +42 -46
- data/ext/carray_slab.c +77 -51
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +37 -31
- data/ext/carray_sort_kernel.c +36 -10
- data/ext/carray_struct.c +44 -34
- data/ext/carray_test.c +131 -81
- data/ext/carray_time_text.c +30 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +373 -139
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +25 -31
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +819 -488
- data/ext/ruby_carray.c +47 -42
- data/ext/version.h +7 -5
- data/lib/carray/attribute.rb +19 -18
- data/lib/carray/autoload_carray.rb +20 -15
- data/lib/carray/autoload_method_extension.rb +64 -57
- data/lib/carray/axis_group.rb +272 -185
- data/lib/carray/basics.rb +298 -56
- data/lib/carray/bincount_nd.rb +41 -18
- data/lib/carray/block_iterator.rb +167 -119
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +84 -59
- data/lib/carray/categorical_iterator.rb +90 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +98 -23
- data/lib/carray/const_string.rb +112 -46
- data/lib/carray/core_extensions.rb +20 -69
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +95 -12
- data/lib/carray/frame/column_verbs.rb +76 -9
- data/lib/carray/frame/concat.rb +51 -7
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +20 -5
- data/lib/carray/frame/frame.rb +73 -31
- data/lib/carray/frame/group.rb +247 -187
- data/lib/carray/frame/io.rb +36 -12
- data/lib/carray/frame/join.rb +56 -17
- data/lib/carray/frame/records.rb +7 -5
- data/lib/carray/frame/reshape.rb +27 -7
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +217 -134
- data/lib/carray/frame/windows.rb +254 -19
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +28 -23
- data/lib/carray/fusion.rb +30 -30
- data/lib/carray/histogram.rb +35 -16
- data/lib/carray/inspect.rb +7 -7
- data/lib/carray/iterator.rb +104 -23
- data/lib/carray/key_windows.rb +93 -61
- data/lib/carray/lazy.rb +208 -111
- data/lib/carray/limits.rb +80 -0
- data/lib/carray/mask_gap_fill.rb +21 -16
- data/lib/carray/math.rb +87 -50
- data/lib/carray/meld_reduce.rb +30 -8
- data/lib/carray/methods/align_addr.rb +29 -17
- data/lib/carray/methods/bin.rb +8 -1
- data/lib/carray/methods/bincount.rb +4 -2
- data/lib/carray/methods/bit_string.rb +19 -31
- data/lib/carray/methods/broadcast.rb +8 -3
- data/lib/carray/methods/choose.rb +5 -5
- data/lib/carray/methods/composition.rb +73 -72
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/factorize.rb +2 -0
- data/lib/carray/methods/gather_nd.rb +106 -103
- data/lib/carray/methods/insert_block.rb +2 -2
- data/lib/carray/methods/is_between.rb +6 -4
- data/lib/carray/methods/is_in.rb +183 -143
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +157 -94
- data/lib/carray/methods/mask_duplicates.rb +1 -1
- data/lib/carray/methods/meshgrid.rb +28 -16
- data/lib/carray/methods/mode.rb +67 -67
- data/lib/carray/methods/nunique.rb +9 -7
- data/lib/carray/methods/repeat.rb +23 -13
- data/lib/carray/methods/resize.rb +1 -1
- data/lib/carray/methods/snap.rb +24 -20
- data/lib/carray/methods/string_format.rb +2 -2
- data/lib/carray/methods/unique.rb +4 -2
- data/lib/carray/methods/value_counts.rb +1 -1
- data/lib/carray/mkmf.rb +25 -28
- data/lib/carray/percentile_arguments.rb +82 -0
- data/lib/carray/record_order.rb +50 -20
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +195 -17
- data/lib/carray/segment_iterator.rb +262 -137
- data/lib/carray/serialize.rb +635 -566
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +14 -15
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +97 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +247 -73
- data/lib/carray/time.rb +663 -365
- data/lib/carray/window_iterator.rb +232 -155
- data/lib/carray.rb +6 -11
- data/yard-stubs/ca_obj_array.rb +20 -135
- data/yard-stubs/ca_obj_bitarray.rb +1 -8
- data/yard-stubs/ca_obj_bitfield.rb +6 -2
- data/yard-stubs/ca_obj_byte_swap.rb +21 -7
- data/yard-stubs/ca_obj_fake.rb +3 -1
- data/yard-stubs/ca_obj_farray.rb +1 -1
- data/yard-stubs/ca_obj_field.rb +8 -6
- data/yard-stubs/ca_obj_refer.rb +10 -5
- data/yard-stubs/ca_obj_roll.rb +7 -6
- data/yard-stubs/ca_obj_shift.rb +2 -3
- data/yard-stubs/ca_obj_stride.rb +5 -7
- data/yard-stubs/ca_obj_tile.rb +1 -1
- data/yard-stubs/ca_obj_time.rb +43 -0
- data/yard-stubs/ca_obj_transpose.rb +1 -1
- data/yard-stubs/ca_obj_window.rb +28 -16
- data/yard-stubs/carray_access.rb +55 -6
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +121 -162
- data/yard-stubs/carray_class.rb +3 -4
- data/yard-stubs/carray_conversion.rb +6 -7
- data/yard-stubs/carray_copy.rb +2 -1
- data/yard-stubs/carray_count.rb +10 -5
- data/yard-stubs/carray_element.rb +4 -3
- data/yard-stubs/carray_limits.rb +100 -0
- data/yard-stubs/carray_loop.rb +0 -20
- data/yard-stubs/carray_mask.rb +59 -41
- data/yard-stubs/carray_math.rb +162 -100
- data/yard-stubs/carray_mathfunc.rb +415 -3
- data/yard-stubs/carray_median_percentile.rb +29 -11
- data/yard-stubs/carray_memory_view.rb +2 -2
- data/yard-stubs/carray_order.rb +72 -33
- data/yard-stubs/carray_random.rb +6 -6
- data/yard-stubs/carray_reduce.rb +353 -0
- data/yard-stubs/carray_rng.rb +92 -0
- data/yard-stubs/carray_scatter.rb +12 -3
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_test.rb +18 -5
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +63 -25
- data/CHANGELOG.v1.md +0 -126
- data/ext/carray_build_flags.h +0 -3
- data/ext/carray_config.h +0 -28
- data/ext/carray_data_type.c +0 -90
- data/ext/carray_kernels_bincmp.c +0 -6457
- data/ext/carray_kernels_binop.c +0 -31534
- data/ext/carray_kernels_init.c +0 -1734
- data/ext/carray_kernels_moncmp.c +0 -2104
- data/ext/carray_kernels_monop.c +0 -25387
- data/ext/carray_kernels_reduce_aggregate.c +0 -27545
- data/ext/carray_kernels_reduce_boolean.c +0 -1924
- data/ext/carray_kernels_reduce_cumulative.c +0 -11646
- data/ext/carray_kernels_reduce_extreme.c +0 -21631
- data/ext/carray_kernels_reduce_variance.c +0 -5981
- data/ext/carray_kernels_scan.c +0 -4302
- data/ext/carray_kernels_search.c +0 -37776
- data/ext/carray_kernels_sort.c +0 -10814
- data/ext/carray_kernels_triop.c +0 -1864
- data/lib/carray/construct.rb +0 -360
- data/lib/carray/data_type_extension.rb +0 -261
- data/lib/carray/data_type_limits.rb +0 -91
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
# Provides:
|
|
10
10
|
# - Postfix math on Float/Integer/Rational (sqrt, exp, log, sin, ...)
|
|
11
11
|
# enabling scalar/CArray polymorphism: the same expression
|
|
12
|
-
# `(0.0415*(t-218.8)).tanh` works whether t is a Float or a CArray
|
|
12
|
+
# `(0.0415*(t-218.8)).tanh` works whether t is a Float or a CArray,
|
|
13
|
+
# and answers as the array does (`(-1.0).sqrt` is NaN, not an exception).
|
|
13
14
|
# - The same postfix math on Complex, so an expression written for a
|
|
14
15
|
# complex CArray still reads for a single cell taken out of it.
|
|
15
16
|
# - Angle normalisation on Numeric (deg_360 / deg_180 / rad_2pi / rad_pi).
|
|
@@ -17,7 +18,7 @@
|
|
|
17
18
|
# `5.eq(ca)` dispatches to `ca.eq(5)`.
|
|
18
19
|
# - Symmetric bitwise operators on Integer / TrueClass / FalseClass
|
|
19
20
|
# so that `5 | bool_ca`, `0xff & flag_ca`, `1 << position_ca` work
|
|
20
|
-
# without writing
|
|
21
|
+
# without writing CA_INT32(5) on the left.
|
|
21
22
|
#
|
|
22
23
|
# None of these are loaded by default `require "carray"`. Each file
|
|
23
24
|
# that wants any of them must opt in:
|
|
@@ -43,11 +44,18 @@ class CArray
|
|
|
43
44
|
# lexically scoped: nothing changes for code that does not opt in.
|
|
44
45
|
module CoreExtensions
|
|
45
46
|
|
|
47
|
+
# What a real scalar answers through the float64 kernel of the array form.
|
|
48
|
+
# The refinement exists so that one expression reads for a scalar and for
|
|
49
|
+
# an array, so it answers as the array does: NaN outside a function's
|
|
50
|
+
# domain where Ruby's Math raises, the sign of a zero, and the accuracy of
|
|
51
|
+
# expm1 and log1p near zero. The cost is one CScalar per call.
|
|
52
|
+
#
|
|
46
53
|
# @!visibility private
|
|
47
54
|
MATH_METHODS = %i[
|
|
48
55
|
sqrt exp log log10
|
|
49
56
|
sin cos tan sinh cosh tanh
|
|
50
57
|
asin acos atan asinh acosh atanh
|
|
58
|
+
expm1 log1p rsqrt signbit
|
|
51
59
|
].freeze
|
|
52
60
|
|
|
53
61
|
[Float, Integer, Rational].each do |klass|
|
|
@@ -55,7 +63,7 @@ class CArray
|
|
|
55
63
|
MATH_METHODS.each do |m|
|
|
56
64
|
module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
57
65
|
def #{m}
|
|
58
|
-
|
|
66
|
+
CA_FLOAT64(self).#{m}[0]
|
|
59
67
|
end
|
|
60
68
|
RUBY
|
|
61
69
|
end
|
|
@@ -68,26 +76,12 @@ class CArray
|
|
|
68
76
|
def distance(other)
|
|
69
77
|
(self.to_f - other.to_f).abs
|
|
70
78
|
end
|
|
71
|
-
# M.1 (PyTorch alignment) scalar polymorphism additions.
|
|
72
79
|
# `trunc` is left to Ruby's built-in semantics (Float#truncate
|
|
73
80
|
# returns Integer when arg=0) — its CArray counterpart preserves
|
|
74
81
|
# data type, so scalar-side use should call .to_f.trunc explicitly.
|
|
75
|
-
# Math.expm1 / Math.log1p don't exist in stdlib; hand-roll.
|
|
76
|
-
def expm1
|
|
77
|
-
Math.exp(self) - 1.0
|
|
78
|
-
end
|
|
79
|
-
def log1p
|
|
80
|
-
Math.log(1.0 + self)
|
|
81
|
-
end
|
|
82
|
-
def rsqrt
|
|
83
|
-
1.0 / Math.sqrt(self)
|
|
84
|
-
end
|
|
85
82
|
def square
|
|
86
83
|
self * self
|
|
87
84
|
end
|
|
88
|
-
def signbit
|
|
89
|
-
self.negative?
|
|
90
|
-
end
|
|
91
85
|
end
|
|
92
86
|
end
|
|
93
87
|
|
|
@@ -109,14 +103,9 @@ class CArray
|
|
|
109
103
|
square rsqrt
|
|
110
104
|
].freeze
|
|
111
105
|
|
|
112
|
-
# Complex
|
|
113
|
-
#
|
|
114
|
-
#
|
|
115
|
-
# need their own implementation.
|
|
116
|
-
#
|
|
117
|
-
# Each one runs the value through a one-element cmplx128 CScalar, so
|
|
118
|
-
# the answer comes from the very kernel the array form would have
|
|
119
|
-
# used. That is the point: the branch cuts of csqrt / clog / casin /
|
|
106
|
+
# Complex goes the same way, through a one-element cmplx128 CScalar, so
|
|
107
|
+
# the answer comes from the very kernel the array form would have used.
|
|
108
|
+
# That is the point: the branch cuts of csqrt / clog / casin /
|
|
120
109
|
# cacosh / catanh, and the sign of a zero on either side of them, are
|
|
121
110
|
# whatever the platform's C99 complex.h says they are, and they are
|
|
122
111
|
# not the same everywhere. On this machine, for instance,
|
|
@@ -143,50 +132,12 @@ class CArray
|
|
|
143
132
|
end
|
|
144
133
|
|
|
145
134
|
refine Numeric do
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
(fa - fa.ceil + 1) * 360.0
|
|
153
|
-
end
|
|
154
|
-
end
|
|
155
|
-
|
|
156
|
-
def deg_180
|
|
157
|
-
a = self.to_f
|
|
158
|
-
fa = (a + 180.0) / 360.0
|
|
159
|
-
b = if a >= 0
|
|
160
|
-
(fa - fa.floor) * 360.0 - 180.0
|
|
161
|
-
else
|
|
162
|
-
(fa - fa.ceil) * 360.0 - 180.0
|
|
163
|
-
end
|
|
164
|
-
b += 360.0 if b <= -180.0
|
|
165
|
-
b
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
def rad_2pi
|
|
169
|
-
a = self.to_f
|
|
170
|
-
two_pi = 2 * Math::PI
|
|
171
|
-
fa = a / two_pi
|
|
172
|
-
if a >= 0
|
|
173
|
-
(fa - fa.floor) * two_pi
|
|
174
|
-
else
|
|
175
|
-
(fa - fa.ceil + 1) * two_pi
|
|
176
|
-
end
|
|
177
|
-
end
|
|
178
|
-
|
|
179
|
-
def rad_pi
|
|
180
|
-
a = self.to_f
|
|
181
|
-
two_pi = 2 * Math::PI
|
|
182
|
-
fa = (a + Math::PI) / two_pi
|
|
183
|
-
b = if a >= 0
|
|
184
|
-
(fa - fa.floor) * two_pi - Math::PI
|
|
185
|
-
else
|
|
186
|
-
(fa - fa.ceil) * two_pi - Math::PI
|
|
187
|
-
end
|
|
188
|
-
b += two_pi if b <= -Math::PI
|
|
189
|
-
b
|
|
135
|
+
%i[deg_360 deg_180 rad_2pi rad_pi].each do |m|
|
|
136
|
+
module_eval <<~RUBY, __FILE__, __LINE__ + 1
|
|
137
|
+
def #{m}
|
|
138
|
+
CA_FLOAT64(self.to_f).#{m}[0]
|
|
139
|
+
end
|
|
140
|
+
RUBY
|
|
190
141
|
end
|
|
191
142
|
|
|
192
143
|
def eq(other)
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
class CArray
|
|
4
4
|
|
|
5
|
-
# The length a match by distance is limited by -- the +
|
|
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 ("
|
|
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
|
|
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
|
|
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
|
|
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
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
# NumPy / Numo-style factory class methods on CArray, kept as a
|
|
2
|
+
# soft compatibility layer.
|
|
3
|
+
#
|
|
4
|
+
# carray's own idiom for building arrays is the typed constructor
|
|
5
|
+
# (CArray.float64(3, 3) { 0 }, CArray.new(:int32, [3]), CA_INT32([1, 2,
|
|
6
|
+
# 3]) and friends). The methods below -- zeros / ones / eye / identity
|
|
7
|
+
# / linspace / arange / full / empty -- do the same work under
|
|
8
|
+
# NumPy-flavoured names, so that code ported from NumPy or Numo does not
|
|
9
|
+
# stop at the first line that creates an array:
|
|
10
|
+
#
|
|
11
|
+
# np.zeros((3, 3)) -> CArray.zeros([3, 3])
|
|
12
|
+
# np.zeros((3, 3), dtype=np.int32) -> CArray.zeros(3, 3, data_type: :int32)
|
|
13
|
+
# np.full((3, 3), 7) -> CArray.full(3, 3, fill_value: 7)
|
|
14
|
+
# np.linspace(0.0, 1.0, 5) -> CArray.linspace(0.0, 1.0, 5)
|
|
15
|
+
#
|
|
16
|
+
# The shape-taking methods accept both the splat form (zeros(3, 3)) and
|
|
17
|
+
# a single Array (zeros([3, 3])). The element type is given with the
|
|
18
|
+
# `data_type:` keyword, as everywhere else in carray.
|
|
19
|
+
#
|
|
20
|
+
# meshgrid lives in lib/carray/methods/meshgrid.rb (autoloaded).
|
|
21
|
+
|
|
22
|
+
class CArray
|
|
23
|
+
|
|
24
|
+
class << self
|
|
25
|
+
|
|
26
|
+
# @overload zeros(*shape, data_type: :float64)
|
|
27
|
+
# Returns a new CArray of the given shape filled with zeros.
|
|
28
|
+
# Shape accepts both splat Integers and a single Array.
|
|
29
|
+
# @param shape [Array<Integer>, Array<Array<Integer>>] shape
|
|
30
|
+
# of the result.
|
|
31
|
+
# @param data_type [Symbol, String, Integer] element type.
|
|
32
|
+
# @return [CArray]
|
|
33
|
+
def zeros(*args, data_type: CA_FLOAT64)
|
|
34
|
+
CArray.new(data_type, factory_shape(args)).zero
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @overload ones(*shape, data_type: :float64)
|
|
38
|
+
# Returns a new CArray of the given shape filled with ones.
|
|
39
|
+
# Same shape rules as {zeros}.
|
|
40
|
+
# @param shape [Array<Integer>, Array<Array<Integer>>]
|
|
41
|
+
# @param data_type [Symbol, String, Integer] element type.
|
|
42
|
+
# @return [CArray]
|
|
43
|
+
def ones(*args, data_type: CA_FLOAT64)
|
|
44
|
+
CArray.new(data_type, factory_shape(args)).one
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# @overload eye(n, m = n, k = 0, data_type: :float64)
|
|
48
|
+
# Returns a 2-D CArray with ones on the `k`-th diagonal and
|
|
49
|
+
# zeros elsewhere.
|
|
50
|
+
# @param n [Integer] number of rows.
|
|
51
|
+
# @param m [Integer] number of columns; defaults to `n`.
|
|
52
|
+
# @param k [Integer] diagonal offset (positive above the main
|
|
53
|
+
# diagonal, negative below).
|
|
54
|
+
# @param data_type [Symbol, String, Integer] element type.
|
|
55
|
+
# @return [CArray] shape `(n, m)`.
|
|
56
|
+
def eye(n, m = nil, k = 0, data_type: CA_FLOAT64)
|
|
57
|
+
m ||= n
|
|
58
|
+
mat = CArray.new(data_type, [n, m])
|
|
59
|
+
if k >= 0
|
|
60
|
+
count = [n, m - k].min
|
|
61
|
+
start = k
|
|
62
|
+
else
|
|
63
|
+
count = [n + k, m].min
|
|
64
|
+
start = (-k) * m
|
|
65
|
+
end
|
|
66
|
+
if count > 0
|
|
67
|
+
mat[(start..start + (count - 1) * (m + 1)) % (m + 1)] = 1
|
|
68
|
+
end
|
|
69
|
+
mat
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @overload identity(n, data_type: :float64)
|
|
73
|
+
# Returns the `n` by `n` identity matrix.
|
|
74
|
+
# @param n [Integer] matrix size.
|
|
75
|
+
# @param data_type [Symbol, String, Integer] element type.
|
|
76
|
+
# @return [CArray] shape `(n, n)`.
|
|
77
|
+
def identity(n, data_type: CA_FLOAT64)
|
|
78
|
+
mat = CArray.new(data_type, [n, n])
|
|
79
|
+
mat[(0..) % (n + 1)] = 1
|
|
80
|
+
mat
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# @overload linspace(x1, x2, n = 100, data_type: nil)
|
|
84
|
+
# Returns a 1-D CArray of `n` values evenly spaced from `x1`
|
|
85
|
+
# to `x2` inclusive. The values are computed in float64 with the
|
|
86
|
+
# step `(x2 - x1) / (n - 1)`. For an integer `data_type` they are
|
|
87
|
+
# then floored (rounded toward -infinity, not toward zero), so
|
|
88
|
+
# both endpoints are hit exactly when `x1` and `x2` are
|
|
89
|
+
# integer-valued. With `n == 1` the one value is `x1`.
|
|
90
|
+
#
|
|
91
|
+
# Without `data_type:`, the result is float64 (integer, Rational
|
|
92
|
+
# and BigDecimal inputs included). `x1` and `x2` must be real
|
|
93
|
+
# numbers. A Complex is refused; for points along a line in the
|
|
94
|
+
# complex plane, fill a complex array with {#seq}.
|
|
95
|
+
#
|
|
96
|
+
# @param x1 [Numeric] first value.
|
|
97
|
+
# @param x2 [Numeric] last value.
|
|
98
|
+
# @param n [Integer] number of samples.
|
|
99
|
+
# @param data_type [Symbol, String, Integer, nil] element type.
|
|
100
|
+
# @return [CArray]
|
|
101
|
+
# @raise [RangeError] if a value does not fit in `data_type`.
|
|
102
|
+
# @raise [TypeError] if `x1` or `x2` is not a real number (a Complex,
|
|
103
|
+
# nil, a String, a Time, ...).
|
|
104
|
+
def linspace(x1, x2, n = 100, data_type: nil)
|
|
105
|
+
raise TypeError, "linspace: n must be an Integer (got #{n.class})" unless n.is_a?(Integer)
|
|
106
|
+
refuse_complex_bounds("linspace", x1, x2)
|
|
107
|
+
refuse_non_real_bounds("linspace", x1, x2)
|
|
108
|
+
data_type ||= CA_FLOAT64
|
|
109
|
+
# span is float-only ("N evenly-spaced integers" is ambiguous --
|
|
110
|
+
# see basics.rb). Another target goes through a float64 span and
|
|
111
|
+
# is cast; an integer target is floored first.
|
|
112
|
+
float_out = CArray.new(CA_FLOAT64, [n]).span(x1.to_f..x2.to_f)
|
|
113
|
+
return float_out if CArray.data_type_name(data_type) == "float64"
|
|
114
|
+
float_out = float_out.floor if CArray.new(data_type, [0]).integer?
|
|
115
|
+
# to_type masks a cell the type cannot hold; linspace refuses it
|
|
116
|
+
# instead, as arange does.
|
|
117
|
+
# coerce: the type the caller asked for
|
|
118
|
+
out = float_out.to_type(data_type)
|
|
119
|
+
if out.any_masked?
|
|
120
|
+
raise RangeError,
|
|
121
|
+
"linspace: #{x1.inspect}..#{x2.inspect} is out of range for #{out.data_type_name}"
|
|
122
|
+
end
|
|
123
|
+
out
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# @overload arange(stop, data_type: nil)
|
|
127
|
+
# Returns a 1-D CArray with values `0, 1, ..., stop - 1`.
|
|
128
|
+
# @param stop [Numeric] exclusive upper bound.
|
|
129
|
+
# @param data_type [Symbol, String, Integer, nil] element type;
|
|
130
|
+
# guessed from the arguments when omitted: int64 when every
|
|
131
|
+
# argument is an Integer, float64 when any is a Float, object
|
|
132
|
+
# when any is not an Integer or a Float.
|
|
133
|
+
# @return [CArray]
|
|
134
|
+
# @overload arange(start, stop, data_type: nil)
|
|
135
|
+
# Returns `start, start + 1, ..., stop - 1`.
|
|
136
|
+
# @param start [Numeric] inclusive lower bound.
|
|
137
|
+
# @param stop [Numeric] exclusive upper bound.
|
|
138
|
+
# @param data_type [Symbol, String, Integer, nil] element type.
|
|
139
|
+
# @return [CArray]
|
|
140
|
+
# @overload arange(start, stop, step, data_type: nil)
|
|
141
|
+
# Returns values from `start` to `stop` (exclusive) stepping
|
|
142
|
+
# by `step`.
|
|
143
|
+
# @param start [Numeric] inclusive lower bound.
|
|
144
|
+
# @param stop [Numeric] exclusive upper bound.
|
|
145
|
+
# @param step [Numeric] spacing.
|
|
146
|
+
# @param data_type [Symbol, String, Integer, nil] element type.
|
|
147
|
+
# @return [CArray]
|
|
148
|
+
# @raise [TypeError] if an argument is a Complex: the exclusive stop
|
|
149
|
+
# needs an order, which a complex value has not; fill a complex
|
|
150
|
+
# array with {#seq} instead.
|
|
151
|
+
# @raise [TypeError] if an argument is not a number (a String, nil).
|
|
152
|
+
def arange(*args, data_type: nil)
|
|
153
|
+
case args.size
|
|
154
|
+
when 3
|
|
155
|
+
start, stop, step = *args
|
|
156
|
+
when 2
|
|
157
|
+
start, stop = *args
|
|
158
|
+
step = 1
|
|
159
|
+
when 1
|
|
160
|
+
start = 0
|
|
161
|
+
stop, = *args
|
|
162
|
+
step = 1
|
|
163
|
+
else
|
|
164
|
+
raise ArgumentError, "wrong number of arguments (given #{args.size}, expected 1..3)"
|
|
165
|
+
end
|
|
166
|
+
refuse_complex_bounds("arange", start, stop, step)
|
|
167
|
+
[start, stop, step].each do |v|
|
|
168
|
+
next if v.is_a?(Numeric)
|
|
169
|
+
raise TypeError, "arange: #{v.inspect} (#{v.class}) is not a number"
|
|
170
|
+
end
|
|
171
|
+
raise ArgumentError, "step must not be 0" if step == 0
|
|
172
|
+
data_type ||= guess_data_type_from_values(start, stop, step)
|
|
173
|
+
# The element count comes from the arguments as given, not from
|
|
174
|
+
# the target data type: arange(0, 1, 0.25, data_type: :int32)
|
|
175
|
+
# counts four elements from the float step and truncates them on
|
|
176
|
+
# store. Integer arguments count exactly (divmod) so that a step
|
|
177
|
+
# dividing the span evenly does not gain a spurious element
|
|
178
|
+
# through float rounding.
|
|
179
|
+
span = stop - start
|
|
180
|
+
if span.is_a?(Integer) && step.is_a?(Integer)
|
|
181
|
+
q, r = span.divmod(step)
|
|
182
|
+
n = r.zero? ? q : q + 1
|
|
183
|
+
else
|
|
184
|
+
n = (span.to_f / step).ceil
|
|
185
|
+
end
|
|
186
|
+
n = 0 if n < 0
|
|
187
|
+
CArray.new(data_type, [n]).seq(start, step)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# @overload full(*shape, fill_value:, data_type: nil)
|
|
191
|
+
# Returns a new CArray of the given shape filled with
|
|
192
|
+
# `fill_value`. The shape is given as {zeros} takes it
|
|
193
|
+
# (`full(2, 3, fill_value: 7)` or `full([2, 3], fill_value: 7)`).
|
|
194
|
+
# Without `data_type:`, the element type is inferred from
|
|
195
|
+
# `fill_value`.
|
|
196
|
+
# @param shape [Array<Integer>, Array<Array<Integer>>]
|
|
197
|
+
# @param fill_value [Object] value used to fill the array.
|
|
198
|
+
# @param data_type [Symbol, String, Integer, nil] element type.
|
|
199
|
+
# @return [CArray]
|
|
200
|
+
def full(*args, fill_value:, data_type: nil)
|
|
201
|
+
data_type ||= guess_data_type_from_values(fill_value)
|
|
202
|
+
CArray.new(data_type, factory_shape(args)).fill(fill_value)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
# @overload empty(*shape, data_type: :float64, bytes: nil)
|
|
206
|
+
# Returns a new CArray of the given shape whose contents are
|
|
207
|
+
# **undefined**. It is {zeros} without the fill: the cost of
|
|
208
|
+
# zeroing the buffer is not paid. Use it when every cell is written
|
|
209
|
+
# before it is read. Same shape rules as {zeros}; this is
|
|
210
|
+
# `CArray.new(data_type, shape, bytes: bytes, raw: true)`.
|
|
211
|
+
#
|
|
212
|
+
# `:object` arrays are the exception: their cells are set to 0
|
|
213
|
+
# anyway, because the garbage collector walks them.
|
|
214
|
+
#
|
|
215
|
+
# @param shape [Array<Integer>, Array<Array<Integer>>]
|
|
216
|
+
# @param data_type [Symbol, String] element type.
|
|
217
|
+
# @param bytes [Integer, nil] element width, for `:fixlen` only.
|
|
218
|
+
# @return [CArray]
|
|
219
|
+
# @raise [ArgumentError] if a block is given -- filling an array is
|
|
220
|
+
# what `CArray.new` is for.
|
|
221
|
+
def empty(*args, data_type: CA_FLOAT64, bytes: nil, &block)
|
|
222
|
+
if block
|
|
223
|
+
raise ArgumentError,
|
|
224
|
+
"CArray.empty does not take a block " \
|
|
225
|
+
"(its contents are left undefined); use CArray.new to fill."
|
|
226
|
+
end
|
|
227
|
+
CArray.new(data_type, factory_shape(args), bytes: bytes, raw: true)
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
private
|
|
231
|
+
|
|
232
|
+
# arange stops before stop, which needs an order, and linspace spans a
|
|
233
|
+
# real interval; a complex value has neither.
|
|
234
|
+
def refuse_complex_bounds(name, *values)
|
|
235
|
+
return unless values.any? { |v| v.is_a?(Complex) }
|
|
236
|
+
raise TypeError,
|
|
237
|
+
"#{name}: a Complex bound or step is not accepted; fill a complex " \
|
|
238
|
+
"array with seq (CArray.cmplx128(n).seq(start, step))"
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# Integer, Float and Complex values together take the widest of
|
|
242
|
+
# int64 < float64 < cmplx128 (arange(0, 5, 0.5) is float64). Any
|
|
243
|
+
# other value (Rational, String, nil, ...) makes the result object.
|
|
244
|
+
def guess_data_type_from_values(*values)
|
|
245
|
+
if values.all? {|v| v == true || v == false }
|
|
246
|
+
CA_BOOLEAN
|
|
247
|
+
elsif !values.all? { |v| v.is_a?(Integer) || v.is_a?(Float) || v.is_a?(Complex) }
|
|
248
|
+
CA_OBJECT
|
|
249
|
+
elsif values.any? { |v| v.is_a?(Complex) }
|
|
250
|
+
CA_CMPLX128
|
|
251
|
+
elsif values.any? { |v| v.is_a?(Float) }
|
|
252
|
+
CA_FLOAT64
|
|
253
|
+
else
|
|
254
|
+
CA_INT64
|
|
255
|
+
end
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
# Accept both foo(3, 4) and foo([3, 4]).
|
|
259
|
+
def factory_shape(args)
|
|
260
|
+
if args.size == 1 && args.first.is_a?(Array)
|
|
261
|
+
args.first
|
|
262
|
+
else
|
|
263
|
+
args
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
end
|
data/lib/carray/fixlen_string.rb
CHANGED
|
@@ -17,7 +17,7 @@ class CArray
|
|
|
17
17
|
# CArray.fixlen_string(["ab", "cde"]) # width = max bytesize
|
|
18
18
|
# CArray.fixlen_string([a, nil, b], bytes: 8) # nil → masked element
|
|
19
19
|
#
|
|
20
|
-
#
|
|
20
|
+
# CAFixlenString exposes the bounded slot width of its storage.
|
|
21
21
|
# `truncate:` controls what happens when
|
|
22
22
|
# a value exceeds `bytes` (only reachable when `bytes` is given explicitly;
|
|
23
23
|
# the auto width can never overflow):
|
|
@@ -31,12 +31,14 @@ class CArray
|
|
|
31
31
|
# ({CArray.string_face_of}): a raw CA_FIXLEN of matching width wraps
|
|
32
32
|
# zero-copy, other string-bearing arrays materialise, numeric is rejected.
|
|
33
33
|
# @overload fixlen_string(values, bytes: nil, truncate: :error)
|
|
34
|
-
# @param values [Array<String, nil>] source values
|
|
34
|
+
# @param values [Array<String, nil>] source values; nil and UNDEF are
|
|
35
|
+
# masked cells.
|
|
35
36
|
# @param bytes [Integer, nil] slot width, 1 or more; defaults to the
|
|
36
37
|
# max bytesize (1 when every string is empty).
|
|
37
38
|
# @param truncate [Symbol] `:error` or `:silent`.
|
|
38
39
|
# @return [CAFixlenString]
|
|
39
40
|
# @raise [ArgumentError] when `bytes` is given below 1.
|
|
41
|
+
# @raise [TypeError] when a value is not a String, nil or UNDEF.
|
|
40
42
|
# @overload fixlen_string(ca, bytes: nil, truncate: :error)
|
|
41
43
|
# @param ca [CArray] a String Face, CA_OBJECT, or raw CA_FIXLEN array.
|
|
42
44
|
# @return [CAFixlenString]
|
|
@@ -46,7 +48,7 @@ class CArray
|
|
|
46
48
|
# @param bytes [Integer, nil] slot width; defaults to the max bytesize.
|
|
47
49
|
# @param truncate [Symbol] `:error` or `:silent`.
|
|
48
50
|
# @return [CAFixlenString]
|
|
49
|
-
def self.fixlen_string
|
|
51
|
+
def self.fixlen_string(arg, bytes: nil, truncate: :error, &block)
|
|
50
52
|
if arg.is_a?(CArray)
|
|
51
53
|
return string_face_of(arg, "CArray.fixlen_string").to_fixlen_string(bytes: bytes, truncate: truncate)
|
|
52
54
|
end
|
|
@@ -64,17 +66,18 @@ class CArray
|
|
|
64
66
|
else
|
|
65
67
|
values = arg.to_a
|
|
66
68
|
end
|
|
69
|
+
values = CArray.send(:string_cells, values, "CAFixlenString")
|
|
67
70
|
|
|
68
71
|
unless bytes.nil? || (bytes.is_a?(Integer) && bytes >= 1)
|
|
69
72
|
raise ArgumentError, "CArray.fixlen_string: invalid bytes: #{bytes.inspect} (a cell needs bytes: of 1 or more)"
|
|
70
73
|
end
|
|
71
|
-
width = bytes || values.compact.map
|
|
74
|
+
width = bytes || values.compact.map(&:bytesize).max || 1
|
|
72
75
|
width = 1 if width < 1 # every string empty: the narrowest cell
|
|
73
76
|
|
|
74
77
|
if truncate == :error
|
|
75
78
|
values.each_with_index do |s, i|
|
|
76
79
|
next if s.nil?
|
|
77
|
-
b = s.
|
|
80
|
+
b = s.bytesize
|
|
78
81
|
if b > width
|
|
79
82
|
raise ArgumentError,
|
|
80
83
|
"CArray.fixlen_string: value at #{i} is #{b} bytes, exceeds slot width #{width} " \
|
|
@@ -85,9 +88,9 @@ class CArray
|
|
|
85
88
|
|
|
86
89
|
entity = CArray.new(CA_FIXLEN, [values.size], :bytes => width)
|
|
87
90
|
values.each_with_index do |s, i|
|
|
88
|
-
entity[i] = s.nil? ? UNDEF : s
|
|
91
|
+
entity[i] = s.nil? ? UNDEF : s
|
|
89
92
|
end
|
|
90
|
-
CAFixlenString.wrap(entity)
|
|
93
|
+
CAFixlenString.wrap(entity, encoding: CAFixlenString.__send__(:encoding_of_strings, values))
|
|
91
94
|
end
|
|
92
95
|
|
|
93
96
|
end
|
|
@@ -95,6 +98,86 @@ end
|
|
|
95
98
|
# Mutable fixed-width String Face: L1 ops + in-place variants. In-place
|
|
96
99
|
# transforms re-store through the native fixlen path (silent truncate).
|
|
97
100
|
class CAFixlenString
|
|
101
|
+
|
|
102
|
+
# The greater String of each cell and `other`, cell by cell: `other` is a
|
|
103
|
+
# CAFixlenString of the same shape or one String. The result is as wide as
|
|
104
|
+
# the wider of the two.
|
|
105
|
+
# @return [CAFixlenString]
|
|
106
|
+
def pmax(other)
|
|
107
|
+
refuse_unordered_operands(__callee__, other)
|
|
108
|
+
pick_by_order(__callee__, other) { |a, b| a.lt(b) }
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# The lesser String of each cell and `other`; see {#pmax}.
|
|
112
|
+
# @return [CAFixlenString]
|
|
113
|
+
def pmin(other)
|
|
114
|
+
refuse_unordered_operands(__callee__, other)
|
|
115
|
+
pick_by_order(__callee__, other) { |a, b| a.gt(b) }
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# A String has no NaN to let win, so these are other names of pmax / pmin.
|
|
119
|
+
alias_method :maximum, :pmax
|
|
120
|
+
alias_method :minimum, :pmin
|
|
121
|
+
|
|
122
|
+
# @overload min_addr(axis: nil)
|
|
123
|
+
# @return [Integer, CArray] the flat address of the smallest String.
|
|
124
|
+
def min_addr(*args, **kw)
|
|
125
|
+
to_string.min_addr(*args, **kw)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# @overload max_addr(axis: nil)
|
|
129
|
+
# @return [Integer, CArray] the flat address of the largest String.
|
|
130
|
+
def max_addr(*args, **kw)
|
|
131
|
+
to_string.max_addr(*args, **kw)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# A value that is no String is in no cell, and the kernels below take only
|
|
135
|
+
# bytes: these ask the Strings, which answer it as any other value.
|
|
136
|
+
CArray.send(:define_named_methods, self,
|
|
137
|
+
[:eq, :ne, :count, :search, :search_addr, :is_in, :union, :difference, :intersection],
|
|
138
|
+
:through_strings_unless_text, super_block: true)
|
|
139
|
+
|
|
140
|
+
private def through_strings_unless_text(op, args, kw)
|
|
141
|
+
v = args[0]
|
|
142
|
+
return yield(*args, **kw) if args.empty? || v.is_a?(String) || v.is_a?(CArray) ||
|
|
143
|
+
v.is_a?(Array) || UNDEF.equal?(v)
|
|
144
|
+
to_string.public_send(op, *args, **kw)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
# `other` where the block says self's cell loses to it, else self's cell.
|
|
148
|
+
# Another array of Strings (a CAString, a CAConstString, an object array)
|
|
149
|
+
# is taken as fixed-length strings, so the result is one.
|
|
150
|
+
private def pick_by_order(verb, other)
|
|
151
|
+
raise ArgumentError, "#{verb}: comparison of String with UNDEF failed" if UNDEF.equal?(other)
|
|
152
|
+
if other.is_a?(CArray) && !other.is_a?(CAFixlenString)
|
|
153
|
+
other = CArray.string(other) if other.data_type == CA_OBJECT && !other.face?
|
|
154
|
+
other = other.to_fixlen_string if other.respond_to?(:to_fixlen_string)
|
|
155
|
+
end
|
|
156
|
+
yield(self, other).then_else(other, self)
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# Both branches of a selection at the width of the wider one.
|
|
160
|
+
private def branches_in_common(x, y)
|
|
161
|
+
width = [x, y].map { |v|
|
|
162
|
+
case v
|
|
163
|
+
when CArray then v.bytes
|
|
164
|
+
when String then v.bytesize
|
|
165
|
+
else 1
|
|
166
|
+
end
|
|
167
|
+
}.max
|
|
168
|
+
[x, y].map { |v|
|
|
169
|
+
v.is_a?(CAFixlenString) && !v.scalar? ? v.to_fixlen_string(bytes: width) : v
|
|
170
|
+
}
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
# The encoding a column of these strings holds its text in: that of the
|
|
174
|
+
# strings beyond ASCII, UTF-8 if none is, and BINARY if they disagree,
|
|
175
|
+
# since the column cannot read them all back.
|
|
176
|
+
private_class_method def self.encoding_of_strings(values)
|
|
177
|
+
text = values.compact.map(&:to_s).reject(&:ascii_only?).map(&:encoding).uniq
|
|
178
|
+
text.size > 1 ? Encoding::BINARY : (text.first || Encoding::UTF_8)
|
|
179
|
+
end
|
|
180
|
+
|
|
98
181
|
# inspect's name for the array: the class and the cell width, which is the
|
|
99
182
|
# longest string a cell holds.
|
|
100
183
|
private def inspect_label
|
|
@@ -113,28 +196,28 @@ class CAFixlenString
|
|
|
113
196
|
|
|
114
197
|
# @return [Array(String, String), Array(CAFixlenString, CAFixlenString)]
|
|
115
198
|
# `[min, max]`, with the keywords of {CArray#min} / {CArray#max}.
|
|
116
|
-
def minmax
|
|
199
|
+
def minmax(*args, **kw)
|
|
117
200
|
[min(*args, **kw), max(*args, **kw)]
|
|
118
201
|
end
|
|
119
202
|
|
|
120
203
|
# @return [CAFixlenString] the running maximum, as {CArray#cummax}.
|
|
121
|
-
def cummax
|
|
204
|
+
def cummax(*args, **kw)
|
|
122
205
|
to_string.cummax(*args, **kw).to_fixlen_string(bytes: bytes)
|
|
123
206
|
end
|
|
124
207
|
|
|
125
208
|
# @return [CAFixlenString] the running minimum, as {CArray#cummin}.
|
|
126
|
-
def cummin
|
|
209
|
+
def cummin(*args, **kw)
|
|
127
210
|
to_string.cummin(*args, **kw).to_fixlen_string(bytes: bytes)
|
|
128
211
|
end
|
|
129
212
|
|
|
130
213
|
# A String has no distance, so there is no nearest one; refused in the
|
|
131
214
|
# words CAString uses.
|
|
132
|
-
def search_nearest
|
|
215
|
+
def search_nearest(*args, **kw)
|
|
133
216
|
to_string.search_nearest(*args, **kw)
|
|
134
217
|
end
|
|
135
218
|
|
|
136
219
|
# @see #search_nearest
|
|
137
|
-
def search_nearest_addr
|
|
220
|
+
def search_nearest_addr(*args, **kw)
|
|
138
221
|
to_string.search_nearest_addr(*args, **kw)
|
|
139
222
|
end
|
|
140
223
|
end
|