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.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +7 -1940
- data/README.md +38 -32
- 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 +6 -9
- data/ext/ca_bincmp_dispatch.h +16 -38
- data/ext/ca_binop_dispatch.c +27 -0
- data/ext/ca_binop_dispatch.h +22 -47
- 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 -1506
- 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 +90 -163
- data/ext/ca_obj_bincmp.c +47 -46
- data/ext/ca_obj_binop.c +38 -38
- data/ext/ca_obj_bitarray.c +5 -17
- data/ext/ca_obj_bitfield.c +15 -30
- data/ext/ca_obj_block.c +11 -12
- data/ext/ca_obj_byte_swap.c +32 -30
- data/ext/ca_obj_const_string.c +57 -62
- data/ext/ca_obj_face.c +99 -91
- data/ext/ca_obj_face.h +32 -170
- data/ext/ca_obj_fake.c +15 -21
- data/ext/ca_obj_farray.c +7 -9
- data/ext/ca_obj_field.c +17 -13
- data/ext/ca_obj_fixlen_string.c +8 -19
- data/ext/ca_obj_grid.c +137 -106
- data/ext/ca_obj_meld.c +23 -21
- data/ext/ca_obj_moncmp.c +10 -21
- data/ext/ca_obj_monop.c +24 -35
- data/ext/ca_obj_object.c +51 -37
- data/ext/ca_obj_record.c +74 -22
- data/ext/ca_obj_reduce.c +8 -10
- data/ext/ca_obj_refer.c +42 -29
- data/ext/ca_obj_remap.c +11 -29
- data/ext/ca_obj_repeat.c +41 -23
- data/ext/ca_obj_roll.c +18 -24
- data/ext/ca_obj_select.c +10 -22
- data/ext/ca_obj_select_axis.c +27 -36
- data/ext/ca_obj_shift.c +22 -42
- data/ext/ca_obj_source.c +1 -5
- data/ext/ca_obj_stack.c +28 -24
- data/ext/ca_obj_stride.c +53 -54
- data/ext/ca_obj_string.c +6 -18
- data/ext/ca_obj_tile.c +12 -12
- 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 +35 -39
- data/ext/ca_obj_window.c +67 -86
- data/ext/ca_op_cmplx64.h +4 -0
- data/ext/ca_op_ipower.c +34 -12
- data/ext/ca_op_powi.h +4 -0
- 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 +20 -26
- data/ext/ca_sweep_engine.c +2 -3
- data/ext/ca_sweep_engine.h +17 -9
- data/ext/ca_transform_common.c +3 -8
- data/ext/ca_triop_dispatch.h +20 -32
- data/ext/caframe_csv_reader.c +11 -11
- data/ext/carray.h +300 -1011
- data/ext/carray_access.c +234 -180
- data/ext/carray_address_basis.c +33 -38
- data/ext/carray_attribute.c +81 -104
- data/ext/carray_bincount.c +12 -14
- data/ext/carray_broadcast.c +30 -26
- data/ext/carray_call_cfunc.c +169 -277
- data/ext/carray_call_cfunc.h +36 -28
- data/ext/carray_cast.c +912 -346
- data/ext/carray_cast_func.rb +31 -3
- data/ext/carray_class.c +6 -11
- data/ext/carray_conversion.c +43 -38
- data/ext/carray_copy.c +28 -26
- data/ext/carray_core.c +217 -126
- data/ext/carray_count.c +27 -15
- data/ext/carray_element.c +40 -25
- data/ext/carray_factorize.c +53 -52
- data/ext/carray_generate.c +26 -19
- data/ext/carray_histogram.c +8 -10
- data/ext/carray_hold.c +7 -9
- data/ext/carray_index_classifier.c +123 -228
- data/ext/carray_index_classifier.h +11 -10
- data/ext/carray_integer_total.c +523 -0
- data/ext/carray_internal.h +1274 -160
- data/ext/carray_key_windows.c +3 -3
- data/ext/carray_lazy.c +21 -36
- data/ext/carray_loop.c +19 -22
- data/ext/carray_mask.c +123 -145
- data/ext/carray_math_kernel.h +23 -47
- data/ext/carray_mathfunc.c +10 -3
- data/ext/carray_median_percentile.c +95 -94
- data/ext/carray_memory_view.c +42 -47
- data/ext/carray_operator.c +423 -122
- data/ext/carray_order.c +121 -85
- data/ext/carray_partition.c +13 -15
- data/ext/carray_random.c +47 -41
- data/ext/carray_round.c +9 -4
- data/ext/carray_scatter.c +54 -40
- data/ext/carray_segment.c +41 -45
- data/ext/carray_slab.c +64 -47
- data/ext/carray_slab.h +44 -33
- data/ext/carray_sort.c +32 -29
- data/ext/carray_sort_kernel.c +12 -10
- data/ext/carray_struct.c +38 -43
- data/ext/carray_test.c +47 -43
- data/ext/carray_time_text.c +24 -31
- data/ext/carray_undef.c +50 -7
- data/ext/carray_utils.c +258 -94
- data/ext/extconf.rb +24 -17
- data/ext/mk_call_cfunc.rb +19 -20
- data/ext/mk_pow5_table.rb +2 -1
- data/ext/mkkernel.rb +782 -461
- 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 +264 -180
- data/lib/carray/basics.rb +272 -48
- data/lib/carray/bincount_nd.rb +33 -16
- data/lib/carray/block_iterator.rb +123 -102
- data/lib/carray/boolean_reduce.rb +7 -4
- data/lib/carray/categorical.rb +63 -51
- data/lib/carray/categorical_iterator.rb +89 -67
- data/lib/carray/complex.rb +61 -41
- data/lib/carray/conditional.rb +49 -22
- data/lib/carray/const_string.rb +54 -62
- data/lib/carray/core_extensions.rb +1 -1
- data/lib/carray/distance_limit.rb +7 -5
- data/lib/carray/factories.rb +269 -0
- data/lib/carray/fixlen_string.rb +25 -24
- data/lib/carray/frame/column_verbs.rb +20 -8
- data/lib/carray/frame/concat.rb +3 -3
- data/lib/carray/frame/convert.rb +2 -1
- data/lib/carray/frame/csv_parser.rb +7 -1
- data/lib/carray/frame/frame.rb +35 -25
- data/lib/carray/frame/group.rb +183 -150
- data/lib/carray/frame/io.rb +26 -7
- data/lib/carray/frame/join.rb +37 -12
- data/lib/carray/frame/records.rb +1 -1
- data/lib/carray/frame/reshape.rb +3 -3
- data/lib/carray/frame/sort.rb +10 -8
- data/lib/carray/frame/verbs.rb +148 -118
- data/lib/carray/frame/windows.rb +232 -21
- data/lib/carray/frame.rb +1 -2
- data/lib/carray/fuse_source.rb +24 -21
- 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 +97 -24
- data/lib/carray/key_windows.rb +83 -57
- data/lib/carray/lazy.rb +196 -94
- 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 +5 -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 +72 -71
- data/lib/carray/methods/{index.rb → coordinate.rb} +13 -13
- data/lib/carray/methods/discovery_along.rb +3 -3
- data/lib/carray/methods/gather_nd.rb +106 -106
- 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 +165 -145
- data/lib/carray/methods/join.rb +2 -1
- data/lib/carray/methods/lag.rb +6 -2
- data/lib/carray/methods/locate_addr.rb +150 -138
- 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 +12 -12
- 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 +18 -17
- data/lib/carray/rng.rb +1 -0
- data/lib/carray/runtime.rb +190 -22
- data/lib/carray/segment_iterator.rb +251 -129
- data/lib/carray/serialize.rb +618 -571
- data/lib/carray/slab_iterator.rb +114 -114
- data/lib/carray/stack.rb +11 -12
- data/lib/carray/string.rb +16 -22
- data/lib/carray/string_operation_extension.rb +76 -52
- data/lib/carray/struct.rb +122 -164
- data/lib/carray/struct_builder.rb +243 -72
- data/lib/carray/time.rb +496 -342
- data/lib/carray/window_iterator.rb +191 -150
- 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 +4 -3
- 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 +7 -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 +26 -15
- data/yard-stubs/carray_access.rb +51 -4
- data/yard-stubs/carray_attribute.rb +26 -38
- data/yard-stubs/carray_cast.rb +118 -165
- 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 +5 -3
- 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 +157 -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 +67 -34
- 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 +10 -2
- data/yard-stubs/carray_slab.rb +7 -4
- data/yard-stubs/carray_sort.rb +7 -6
- data/yard-stubs/carray_undef.rb +44 -5
- data/yard-stubs/ruby_carray.rb +75 -40
- metadata +62 -24
- 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 -25337
- 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 -262
- data/lib/carray/data_type_limits.rb +0 -91
data/lib/carray/struct.rb
CHANGED
|
@@ -13,7 +13,7 @@
|
|
|
13
13
|
# CAStruct and CAUnion are examples of such data class.
|
|
14
14
|
#
|
|
15
15
|
# option = {
|
|
16
|
-
# :pack => 1, # nil
|
|
16
|
+
# :pack => 1, # nil aligns each member, 1 packs them
|
|
17
17
|
# :size => 1024 # user defined size (with padding)
|
|
18
18
|
# }
|
|
19
19
|
#
|
|
@@ -24,18 +24,14 @@
|
|
|
24
24
|
# int8 :a, :b, :c
|
|
25
25
|
#
|
|
26
26
|
# float32 :f1, :f2
|
|
27
|
-
# float :f5, :f6
|
|
28
|
-
#
|
|
29
27
|
# float64 :d1, :d2
|
|
30
|
-
# double :d5, :d6
|
|
31
28
|
#
|
|
32
29
|
# # fixed length or string
|
|
33
30
|
#
|
|
34
31
|
# fixlen :str1, :str2, :bytes => 3
|
|
35
|
-
# char_p :str3, :str4, :bytes => 3
|
|
36
32
|
#
|
|
37
33
|
# # array type
|
|
38
|
-
# array :ary1, :ary2, :type => CArray.
|
|
34
|
+
# array :ary1, :ary2, :type => CArray.int32(3)
|
|
39
35
|
#
|
|
40
36
|
# # struct type
|
|
41
37
|
# struct(:st1, :st2) { uint8 :a, :b, :c }
|
|
@@ -55,9 +51,9 @@
|
|
|
55
51
|
# member :int8, :mem0, :mem1
|
|
56
52
|
# member "int8", :mem0, :mem1
|
|
57
53
|
# member :uint8, nil ### anonymous
|
|
58
|
-
# member CArray.
|
|
54
|
+
# member CArray.int32(3), :ary3
|
|
59
55
|
# member struct{ int8 :a, :b, :c }, :st5, :st6
|
|
60
|
-
# member union{ int8 :a; int16 :b;
|
|
56
|
+
# member union{ int8 :a; int16 :b; float32 :c }, :st5, :st6
|
|
61
57
|
#
|
|
62
58
|
# }
|
|
63
59
|
|
|
@@ -100,7 +96,7 @@ class CAStruct
|
|
|
100
96
|
class Field
|
|
101
97
|
attr_reader :name, :offset, :type, :bytes, :bits, :bit_offset, :endian
|
|
102
98
|
|
|
103
|
-
def initialize
|
|
99
|
+
def initialize(name:, offset:, type:, bytes: nil,
|
|
104
100
|
bits: nil, bit_offset: nil, endian: nil)
|
|
105
101
|
@name = name
|
|
106
102
|
@offset = offset
|
|
@@ -134,7 +130,7 @@ class CAStruct
|
|
|
134
130
|
# Field-wise comparison of the member's declaration (name, offset, type,
|
|
135
131
|
# width, bit placement, endianness). Aliased as `eql?`.
|
|
136
132
|
# @return [Boolean]
|
|
137
|
-
def ==
|
|
133
|
+
def ==(other)
|
|
138
134
|
other.is_a?(Field) &&
|
|
139
135
|
@name == other.name &&
|
|
140
136
|
@offset == other.offset &&
|
|
@@ -155,28 +151,6 @@ class CAStruct
|
|
|
155
151
|
|
|
156
152
|
end
|
|
157
153
|
|
|
158
|
-
class CArray
|
|
159
|
-
|
|
160
|
-
# @overload st
|
|
161
|
-
# Returns a Ruby `Struct` view exposing every {CAStruct} member
|
|
162
|
-
# of `self` as a `Struct` attribute holding the corresponding
|
|
163
|
-
# member column. Cached per receiver.
|
|
164
|
-
# @return [Struct]
|
|
165
|
-
# @raise [CAStruct::Error] when `self` has no `data_class`.
|
|
166
|
-
def st
|
|
167
|
-
unless has_data_class?
|
|
168
|
-
raise CAStruct::Error, "carray does not have a data_class"
|
|
169
|
-
end
|
|
170
|
-
unless @struct
|
|
171
|
-
struct_class = Struct.new(nil, *data_class::MEMBERS)
|
|
172
|
-
members = data_class::MEMBERS.map{|name| self[name]}
|
|
173
|
-
@struct = struct_class.new(*members)
|
|
174
|
-
end
|
|
175
|
-
return @struct
|
|
176
|
-
end
|
|
177
|
-
|
|
178
|
-
end
|
|
179
|
-
|
|
180
154
|
class CAStruct
|
|
181
155
|
|
|
182
156
|
include Enumerable
|
|
@@ -196,7 +170,7 @@ class CAStruct
|
|
|
196
170
|
# @param values [Array<Object>]
|
|
197
171
|
# @return [CAStruct]
|
|
198
172
|
# @raise [ArgumentError] when too many values are given.
|
|
199
|
-
def []
|
|
173
|
+
def [](*argv)
|
|
200
174
|
if argv.size > self::MEMBERS.size
|
|
201
175
|
raise ArgumentError,
|
|
202
176
|
format("too many arguments for %s.[] (<%i> for <%i>)",
|
|
@@ -227,10 +201,21 @@ class CAStruct
|
|
|
227
201
|
|
|
228
202
|
# @overload fields
|
|
229
203
|
# Returns the struct's members as {CAStruct::Field} objects
|
|
230
|
-
# in declaration order.
|
|
204
|
+
# in declaration order.
|
|
231
205
|
# @return [Array<CAStruct::Field>]
|
|
232
206
|
def fields
|
|
233
|
-
@__fields__
|
|
207
|
+
return @__fields__ if instance_variable_defined?(:@__fields__)
|
|
208
|
+
list = build_fields
|
|
209
|
+
# Only the main Ractor may set an instance variable on a class, and a
|
|
210
|
+
# frozen class takes none; elsewhere the list is built each time.
|
|
211
|
+
if Ractor.current == Ractor.main and not frozen?
|
|
212
|
+
@__fields__ = list
|
|
213
|
+
end
|
|
214
|
+
return list
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
private def build_fields
|
|
218
|
+
list = self::MEMBERS.map { |name|
|
|
234
219
|
offset, type, opts = *self::MEMBER_TABLE[name]
|
|
235
220
|
opts ||= {}
|
|
236
221
|
if type == :bitfield
|
|
@@ -246,7 +231,8 @@ class CAStruct
|
|
|
246
231
|
bytes: opts[:bytes],
|
|
247
232
|
endian: opts[:endian])
|
|
248
233
|
end
|
|
249
|
-
}
|
|
234
|
+
}
|
|
235
|
+
return Ractor.make_shareable(list)
|
|
250
236
|
end
|
|
251
237
|
|
|
252
238
|
# @overload field_info(name)
|
|
@@ -254,7 +240,7 @@ class CAStruct
|
|
|
254
240
|
# member is unknown.
|
|
255
241
|
# @param name [Symbol, String]
|
|
256
242
|
# @return [CAStruct::Field, nil]
|
|
257
|
-
def field_info
|
|
243
|
+
def field_info(name)
|
|
258
244
|
key = name.to_s
|
|
259
245
|
fields.find { |f| f.name == key }
|
|
260
246
|
end
|
|
@@ -265,7 +251,7 @@ class CAStruct
|
|
|
265
251
|
# unknown.
|
|
266
252
|
# @param name [Symbol, String]
|
|
267
253
|
# @return [Integer, nil]
|
|
268
|
-
def offset_of
|
|
254
|
+
def offset_of(name)
|
|
269
255
|
f = field_info(name)
|
|
270
256
|
return nil unless f
|
|
271
257
|
f.bitfield? ? f.bit_offset : f.offset
|
|
@@ -276,7 +262,7 @@ class CAStruct
|
|
|
276
262
|
# (String or CArray).
|
|
277
263
|
# @param data [String, CArray]
|
|
278
264
|
# @return [CAStruct]
|
|
279
|
-
def decode
|
|
265
|
+
def decode(data) ### required element as data class
|
|
280
266
|
return new.decode(data)
|
|
281
267
|
end
|
|
282
268
|
|
|
@@ -289,46 +275,18 @@ class CAStruct
|
|
|
289
275
|
|
|
290
276
|
end
|
|
291
277
|
|
|
292
|
-
#
|
|
278
|
+
# @!method initialize(*values)
|
|
293
279
|
# Allocates a new struct record. Members are set from the
|
|
294
280
|
# trailing positional `values` in declaration order, or from a
|
|
295
281
|
# single Hash argument keyed by member name.
|
|
296
282
|
# @param values [Array<Object>, Array<Hash>]
|
|
297
283
|
# @raise [ArgumentError] on unknown Hash keys or an excess of
|
|
298
284
|
# positional values.
|
|
299
|
-
|
|
300
|
-
@data = CScalar.new(self.class)
|
|
301
|
-
mems = members
|
|
302
|
-
if argv.size == 1 and argv.first.is_a?(Hash)
|
|
303
|
-
# Validate keys up-front so the caller gets one clear error
|
|
304
|
-
# listing all unknown keys, rather than a NoMethodError from the
|
|
305
|
-
# first one and silent partial-init for the rest.
|
|
306
|
-
known = self.class::MEMBER_TABLE
|
|
307
|
-
unknown = argv.first.keys.reject { |k| known.key?(k.to_s) }
|
|
308
|
-
unless unknown.empty?
|
|
309
|
-
raise ArgumentError,
|
|
310
|
-
format("unknown member(s) for %s: %s (known: %s)",
|
|
311
|
-
self.class.inspect,
|
|
312
|
-
unknown.map(&:inspect).join(", "),
|
|
313
|
-
mems.map(&:inspect).join(", "))
|
|
314
|
-
end
|
|
315
|
-
argv.first.each do |k, v|
|
|
316
|
-
self[k] = v
|
|
317
|
-
end
|
|
318
|
-
elsif argv.size <= mems.size
|
|
319
|
-
argv.each_with_index do |v, i|
|
|
320
|
-
self[mems[i]] = v
|
|
321
|
-
end
|
|
322
|
-
else
|
|
323
|
-
raise ArgumentError,
|
|
324
|
-
format("too many arguments for %s.new (<%i> for <%i>)",
|
|
325
|
-
self.class.inspect, argv.size, mems.size)
|
|
326
|
-
end
|
|
327
|
-
end
|
|
285
|
+
# Defined in C (ext/carray_struct.c).
|
|
328
286
|
|
|
329
287
|
# A copy owns its bytes (dup and clone of the struct object would
|
|
330
288
|
# otherwise share the one-record array behind it).
|
|
331
|
-
def initialize_copy
|
|
289
|
+
def initialize_copy(other)
|
|
332
290
|
super
|
|
333
291
|
@data = @data.copy
|
|
334
292
|
end
|
|
@@ -343,54 +301,23 @@ class CAStruct
|
|
|
343
301
|
# Member access. A member is named by a String (`s["lat"]`) or its
|
|
344
302
|
# Integer position (`s[0]`); its accessor (`s.lat`) reads it too. A
|
|
345
303
|
# Symbol is not a member name here, and a name that is not a member
|
|
346
|
-
# raises.
|
|
347
|
-
#
|
|
348
|
-
#
|
|
304
|
+
# raises. `[]`, `[]=` and `initialize` are defined in C
|
|
305
|
+
# (ext/carray_struct.c).
|
|
306
|
+
#
|
|
307
|
+
# @!method [](name)
|
|
349
308
|
# Returns the value of member `name`.
|
|
350
309
|
# @param name [String, Integer]
|
|
351
310
|
# @return [Object]
|
|
352
311
|
# @raise [TypeError] for a Symbol or another type of name.
|
|
353
312
|
# @raise [ArgumentError] when `name` is not a member.
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
pair[0].call(@data)
|
|
357
|
-
end
|
|
358
|
-
|
|
359
|
-
# @overload []=(name, val)
|
|
313
|
+
#
|
|
314
|
+
# @!method []=(name, val)
|
|
360
315
|
# Sets member `name` to `val`.
|
|
361
316
|
# @param name [String, Integer]
|
|
362
317
|
# @param val [Object]
|
|
363
318
|
# @return [Object] `val`.
|
|
364
319
|
# @raise [TypeError] for a Symbol or another type of name.
|
|
365
320
|
# @raise [ArgumentError] when `name` is not a member.
|
|
366
|
-
def []= (name, val)
|
|
367
|
-
pair = self.class::DISPATCH_TABLE[member_name(name)]
|
|
368
|
-
pair[1].call(@data, val)
|
|
369
|
-
end
|
|
370
|
-
|
|
371
|
-
private def member_name (name)
|
|
372
|
-
case name
|
|
373
|
-
when Symbol
|
|
374
|
-
if self.class::MEMBER_TABLE.key?(name.to_s)
|
|
375
|
-
raise TypeError, "a struct member is named by a String (s[\"#{name}\"]) " \
|
|
376
|
-
"or read by its accessor (s.#{name}), not by a Symbol"
|
|
377
|
-
end
|
|
378
|
-
raise TypeError, "a struct member is named by a String, not by a Symbol " \
|
|
379
|
-
"(#{self.class.inspect} has no member #{name.to_s.inspect} either)"
|
|
380
|
-
when Integer
|
|
381
|
-
key = members[name]
|
|
382
|
-
when String
|
|
383
|
-
key = name
|
|
384
|
-
else
|
|
385
|
-
raise TypeError, "a struct member is named by a String or an Integer " \
|
|
386
|
-
"position (got #{name.class})"
|
|
387
|
-
end
|
|
388
|
-
unless key && self.class::MEMBER_TABLE.key?(key)
|
|
389
|
-
raise ArgumentError, "#{self.class.inspect} has no member #{name.inspect} " \
|
|
390
|
-
"(members: #{members.inspect})"
|
|
391
|
-
end
|
|
392
|
-
key
|
|
393
|
-
end
|
|
394
321
|
|
|
395
322
|
# @overload each { |value| ... }
|
|
396
323
|
# Yields each member value in declaration order (an Enumerator
|
|
@@ -456,7 +383,7 @@ class CAStruct
|
|
|
456
383
|
# Returns the values of the named members in the given order.
|
|
457
384
|
# @param names [Array<Symbol, String, Integer>]
|
|
458
385
|
# @return [Array<Object>]
|
|
459
|
-
def values_at
|
|
386
|
+
def values_at(*names)
|
|
460
387
|
return names.map{|name| self[name] }
|
|
461
388
|
end
|
|
462
389
|
|
|
@@ -476,7 +403,7 @@ class CAStruct
|
|
|
476
403
|
# itself, 0.0 and -0.0 differ, and so do records whose padding bytes
|
|
477
404
|
# differ. A record array's `eq` compares records the same way.
|
|
478
405
|
# @return [Boolean]
|
|
479
|
-
def ==
|
|
406
|
+
def ==(other)
|
|
480
407
|
other.is_a?(self.class) && encode == other.encode
|
|
481
408
|
end
|
|
482
409
|
|
|
@@ -485,22 +412,40 @@ class CAStruct
|
|
|
485
412
|
# after every number. nil when the other is not a record of this class,
|
|
486
413
|
# or when the struct declares no order.
|
|
487
414
|
# @return [Integer, nil]
|
|
488
|
-
def <=>
|
|
415
|
+
def <=>(other)
|
|
489
416
|
keys = self.class.order_by
|
|
490
417
|
return nil unless keys && other.class.equal?(self.class)
|
|
491
418
|
keys.each do |key|
|
|
492
|
-
c = CAStruct.compare_key
|
|
419
|
+
c = CAStruct.send(:compare_key, self[key], other[key])
|
|
493
420
|
return c unless c == 0
|
|
494
421
|
end
|
|
495
422
|
0
|
|
496
423
|
end
|
|
497
424
|
|
|
498
425
|
# @!method <(other)
|
|
426
|
+
# Returns whether this record is ordered before `other`
|
|
427
|
+
# by {#<=>}.
|
|
428
|
+
# @return [Boolean]
|
|
499
429
|
# @raise [ArgumentError] when the struct declares no `order_by:`, or
|
|
500
430
|
# `other` is not a record of the same class.
|
|
501
431
|
# @!method <=(other)
|
|
432
|
+
# Returns whether this record is ordered before or with `other`
|
|
433
|
+
# by {#<=>}.
|
|
434
|
+
# @return [Boolean]
|
|
435
|
+
# @raise [ArgumentError] when the struct declares no `order_by:`, or
|
|
436
|
+
# `other` is not a record of the same class.
|
|
502
437
|
# @!method >(other)
|
|
438
|
+
# Returns whether this record is ordered after `other`
|
|
439
|
+
# by {#<=>}.
|
|
440
|
+
# @return [Boolean]
|
|
441
|
+
# @raise [ArgumentError] when the struct declares no `order_by:`, or
|
|
442
|
+
# `other` is not a record of the same class.
|
|
503
443
|
# @!method >=(other)
|
|
444
|
+
# Returns whether this record is ordered after or with `other`
|
|
445
|
+
# by {#<=>}.
|
|
446
|
+
# @return [Boolean]
|
|
447
|
+
# @raise [ArgumentError] when the struct declares no `order_by:`, or
|
|
448
|
+
# `other` is not a record of the same class.
|
|
504
449
|
[:<, :<=, :>, :>=].each do |op|
|
|
505
450
|
define_method(op, &Ractor.make_shareable(proc do |other|
|
|
506
451
|
c = (self <=> other)
|
|
@@ -515,14 +460,14 @@ class CAStruct
|
|
|
515
460
|
|
|
516
461
|
# One member of {#<=>}: the order a sort puts the member's values in
|
|
517
462
|
# (NaN after every number).
|
|
518
|
-
def self.compare_key
|
|
463
|
+
private_class_method def self.compare_key(a, b)
|
|
519
464
|
CArray.send(:object_order_cmp, a, b)
|
|
520
465
|
end
|
|
521
466
|
|
|
522
467
|
# Byte-level identity. Two CAStruct instances are eql? iff they are
|
|
523
468
|
# of exactly the same class and their binary representations match.
|
|
524
469
|
# Lets struct records work as Hash keys / Set members.
|
|
525
|
-
def eql?
|
|
470
|
+
def eql?(other)
|
|
526
471
|
other.is_a?(self.class) && encode == other.encode
|
|
527
472
|
end
|
|
528
473
|
|
|
@@ -539,7 +484,7 @@ class CAStruct
|
|
|
539
484
|
# @param data [String, CArray]
|
|
540
485
|
# @return [self]
|
|
541
486
|
# @raise [CAStruct::DecodeError] on an unsupported input.
|
|
542
|
-
def decode
|
|
487
|
+
def decode(data)
|
|
543
488
|
raise FrozenError.new("can't modify frozen #{self.class}", receiver: self) if frozen?
|
|
544
489
|
case data
|
|
545
490
|
when String
|
|
@@ -590,43 +535,6 @@ class CAUnion < CAStruct
|
|
|
590
535
|
end
|
|
591
536
|
end
|
|
592
537
|
|
|
593
|
-
class CArray
|
|
594
|
-
|
|
595
|
-
# @overload struct(opt = {}, &block)
|
|
596
|
-
# Returns a new {CAStruct} subclass defined by the block via
|
|
597
|
-
# {CAStruct::Builder}. Options control alignment, packing, and
|
|
598
|
-
# endianness.
|
|
599
|
-
#
|
|
600
|
-
# `order_by:` names the members records are ordered by, compared in
|
|
601
|
-
# that order (`order_by: [:lat, :id]` sorts by `lat`, then `id` among
|
|
602
|
-
# equal `lat`). It gives the records `<=>` and the comparison
|
|
603
|
-
# operators, and gives a {CARecord} of them `sort`, `min`, `max`,
|
|
604
|
-
# `partition_copy` and their index forms. A struct without it has no
|
|
605
|
-
# order, and those raise. NaN sorts after every number, as in a
|
|
606
|
-
# numeric array.
|
|
607
|
-
# @param opt [Hash] `pack:`, `size:`, `order_by:`.
|
|
608
|
-
# @yield DSL calls in {CAStruct::Builder}.
|
|
609
|
-
# @return [Class] anonymous {CAStruct} subclass.
|
|
610
|
-
# @raise [CAStruct::DefinitionError] when `order_by:` names something
|
|
611
|
-
# that is not a member, or a nested record.
|
|
612
|
-
def self.struct (opt={}, &block)
|
|
613
|
-
return CAStruct::Builder.new(:struct, opt).define(&block)
|
|
614
|
-
end
|
|
615
|
-
|
|
616
|
-
# @overload union(opt = {}, &block)
|
|
617
|
-
# Returns a new {CAUnion} subclass defined by the block. Same
|
|
618
|
-
# options and DSL as {.struct} but every member occupies the
|
|
619
|
-
# same offset.
|
|
620
|
-
# @param opt [Hash]
|
|
621
|
-
# @yield DSL calls in {CAStruct::Builder}.
|
|
622
|
-
# @return [Class] anonymous {CAUnion} subclass.
|
|
623
|
-
def self.union (opt={}, &block)
|
|
624
|
-
return CAStruct::Builder.new(:union, opt).define(&block)
|
|
625
|
-
end
|
|
626
|
-
|
|
627
|
-
end
|
|
628
|
-
|
|
629
|
-
|
|
630
538
|
class CAStruct
|
|
631
539
|
|
|
632
540
|
# Renders the struct's layout as the definition source that would rebuild
|
|
@@ -706,18 +614,68 @@ class CAStruct
|
|
|
706
614
|
|
|
707
615
|
end
|
|
708
616
|
|
|
709
|
-
#
|
|
710
|
-
#
|
|
711
|
-
|
|
712
|
-
#
|
|
713
|
-
# CAREFUL: this must run AFTER CAStruct's Ruby [] / []= are defined
|
|
714
|
-
# above (so the C versions can replace them) and AFTER FAST_PRIMITIVES /
|
|
715
|
-
# DISPATCH_TABLE keys are agreed upon (so the per-class struct definition
|
|
716
|
-
# in Builder sets them up before any instance is created). Subclasses
|
|
717
|
-
# created by Builder later don't need re-installation -- they inherit the
|
|
718
|
-
# C methods from CAStruct.
|
|
719
|
-
if CArray.respond_to?(:install_struct_methods, true)
|
|
720
|
-
CArray.send(:install_struct_methods, CAStruct)
|
|
721
|
-
end
|
|
617
|
+
# Install the C `[]`, `[]=` and `initialize` (ext/carray_struct.c) on
|
|
618
|
+
# CAStruct. The subclasses Builder makes inherit them.
|
|
619
|
+
CArray.send(:install_struct_methods, CAStruct)
|
|
722
620
|
|
|
723
621
|
require 'carray/record_order'
|
|
622
|
+
|
|
623
|
+
class CArray
|
|
624
|
+
|
|
625
|
+
# @overload st
|
|
626
|
+
# Returns a Ruby `Struct` view exposing every {CAStruct} member
|
|
627
|
+
# of `self` as a `Struct` attribute holding the corresponding
|
|
628
|
+
# member column. Cached per receiver.
|
|
629
|
+
# @return [Struct]
|
|
630
|
+
# @raise [CAStruct::Error] when `self` has no `data_class`.
|
|
631
|
+
def st
|
|
632
|
+
unless has_data_class?
|
|
633
|
+
raise CAStruct::Error, "carray does not have a data_class"
|
|
634
|
+
end
|
|
635
|
+
unless @struct
|
|
636
|
+
struct_class = Struct.new(nil, *data_class::MEMBERS)
|
|
637
|
+
members = data_class::MEMBERS.map{|name| self[name]}
|
|
638
|
+
@struct = struct_class.new(*members)
|
|
639
|
+
end
|
|
640
|
+
return @struct
|
|
641
|
+
end
|
|
642
|
+
|
|
643
|
+
end
|
|
644
|
+
|
|
645
|
+
class CArray
|
|
646
|
+
|
|
647
|
+
# @overload struct(opt = {}, &block)
|
|
648
|
+
# Returns a new {CAStruct} subclass defined by the block via
|
|
649
|
+
# {CAStruct::Builder}. Options control alignment, packing, and
|
|
650
|
+
# endianness.
|
|
651
|
+
#
|
|
652
|
+
# `order_by:` names the members records are ordered by, compared in
|
|
653
|
+
# that order (`order_by: [:lat, :id]` sorts by `lat`, then `id` among
|
|
654
|
+
# equal `lat`). It gives the records `<=>` and the comparison
|
|
655
|
+
# operators, and gives a {CARecord} of them `sort`, `min`, `max`,
|
|
656
|
+
# `partition_copy` and their index forms. A struct without it has no
|
|
657
|
+
# order, and those raise. NaN sorts after every number, as in a
|
|
658
|
+
# numeric array.
|
|
659
|
+
# `pack: 1` places each member right after the previous one; without
|
|
660
|
+
# it each member is aligned to its type. No other value is accepted.
|
|
661
|
+
# @param opt [Hash] `pack:`, `size:`, `order_by:`.
|
|
662
|
+
# @yield DSL calls in {CAStruct::Builder}.
|
|
663
|
+
# @return [Class] anonymous {CAStruct} subclass.
|
|
664
|
+
# @raise [CAStruct::DefinitionError] when `order_by:` names something
|
|
665
|
+
# that is not a member, or a nested record.
|
|
666
|
+
def self.struct(opt={}, &block)
|
|
667
|
+
return CAStruct::Builder.new(:struct, opt).send(:define, &block)
|
|
668
|
+
end
|
|
669
|
+
|
|
670
|
+
# @overload union(opt = {}, &block)
|
|
671
|
+
# Returns a new {CAUnion} subclass defined by the block. Same
|
|
672
|
+
# options and DSL as {.struct} but every member occupies the
|
|
673
|
+
# same offset.
|
|
674
|
+
# @param opt [Hash]
|
|
675
|
+
# @yield DSL calls in {CAStruct::Builder}.
|
|
676
|
+
# @return [Class] anonymous {CAUnion} subclass.
|
|
677
|
+
def self.union(opt={}, &block)
|
|
678
|
+
return CAStruct::Builder.new(:union, opt).send(:define, &block)
|
|
679
|
+
end
|
|
680
|
+
|
|
681
|
+
end
|