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
data/lib/carray/serialize.rb
CHANGED
|
@@ -59,506 +59,680 @@
|
|
|
59
59
|
require "stringio"
|
|
60
60
|
require "psych"
|
|
61
61
|
|
|
62
|
-
class CArray
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
# The data region is raw contiguous bytes; attribute Hash and
|
|
114
|
-
# data_class schema (if any) ride in a YAML trailer at the tail.
|
|
115
|
-
# @param ca [CArray] array to write.
|
|
116
|
-
# @param endian [Symbol, Integer, nil] forces the output byte order
|
|
117
|
-
# (defaults to the host's; a differing value swaps data + header).
|
|
118
|
-
# @return [CArray] `ca`.
|
|
119
|
-
# @raise [ArgumentError] when `ca` is a CA_OBJECT array (use
|
|
120
|
-
# `Marshal.dump(ca)` instead), or carries a data_class the
|
|
121
|
-
# v1.0 flat-primitive schema cannot express.
|
|
122
|
-
# @raise [TypeError] when `ca` is a Face other than a record (save
|
|
123
|
-
# its storage through the Face's own accessor, such as `#ticks` or
|
|
124
|
-
# `#codes`, or its values with `Marshal.dump(ca.to_type(:object))`).
|
|
125
|
-
def save (ca, endian: nil)
|
|
126
|
-
if ca.data_type == :object
|
|
127
|
-
raise ArgumentError,
|
|
128
|
-
"CArray.save cannot serialise a CA_OBJECT array " \
|
|
129
|
-
"(arbitrary Ruby objects have no portable representation); " \
|
|
130
|
-
"use Marshal.dump(ca) for a Ruby-only round-trip"
|
|
131
|
-
end
|
|
132
|
-
# A Face other than a record is refused, as Marshal refuses it: the file
|
|
133
|
-
# would carry its storage without what makes it the Face (a unit, the
|
|
134
|
-
# string buffer, the labels), and load could not put that back.
|
|
135
|
-
if ca.face? && !ca.is_a?(CARecord)
|
|
136
|
-
storage = ca.__send__(:face_storage_method)
|
|
137
|
-
raise TypeError,
|
|
138
|
-
"CArray.save cannot carry a #{ca.class} yet (its Face would be " \
|
|
139
|
-
"lost); " +
|
|
140
|
-
(storage ? "save its storage, which ##{storage} gives, or " : "") +
|
|
141
|
-
"keep its values with Marshal.dump(ca.to_type(:object))"
|
|
62
|
+
class CArray
|
|
63
|
+
class Serializer # :nodoc:
|
|
64
|
+
|
|
65
|
+
# @!visibility private
|
|
66
|
+
MAGIC = "_CARRAY3"
|
|
67
|
+
Ractor.make_shareable(MAGIC)
|
|
68
|
+
# @!visibility private
|
|
69
|
+
HEADER_BYTES = 256
|
|
70
|
+
# @!visibility private
|
|
71
|
+
VERSION_MAJOR = 1
|
|
72
|
+
# @!visibility private
|
|
73
|
+
VERSION_MINOR = 0
|
|
74
|
+
# @!visibility private
|
|
75
|
+
ENDIAN_MARKER = 0x01020304
|
|
76
|
+
|
|
77
|
+
# Canonical primitive-type notation for the trailer data_class
|
|
78
|
+
# schema. The .ca format *owns* this mapping: it is the same bare
|
|
79
|
+
# PEP 3118 notation CArray's MemoryView layer emits via
|
|
80
|
+
# ca_mv_format_for, frozen here as the format's own copy so a future
|
|
81
|
+
# MemoryView producer flip does not silently change the on-disk spec.
|
|
82
|
+
# Non-primitive member types (:fixlen / :object / nested class /
|
|
83
|
+
# CArray template / :bitfield) are absent, so SYMBOL_TO_PEP[type] is
|
|
84
|
+
# nil for them, which enforces the flat-primitive-only rule with no extra check
|
|
85
|
+
# (a non-expressible member raises on save).
|
|
86
|
+
PEP_TO_SYMBOL = {
|
|
87
|
+
"?" => :boolean,
|
|
88
|
+
"b" => :int8, "B" => :uint8,
|
|
89
|
+
"h" => :int16, "H" => :uint16,
|
|
90
|
+
"i" => :int32, "I" => :uint32,
|
|
91
|
+
"q" => :int64, "Q" => :uint64,
|
|
92
|
+
"f" => :float32, "d" => :float64,
|
|
93
|
+
"Zf" => :cmplx64, "Zd" => :cmplx128,
|
|
94
|
+
}.freeze
|
|
95
|
+
|
|
96
|
+
# @!visibility private
|
|
97
|
+
SYMBOL_TO_PEP = PEP_TO_SYMBOL.invert.freeze
|
|
98
|
+
|
|
99
|
+
# @overload initialize(io)
|
|
100
|
+
# Allocates a Serializer around `io`. A String is wrapped in a
|
|
101
|
+
# `StringIO`; anything else is used directly.
|
|
102
|
+
# @param io [IO, String] destination or source.
|
|
103
|
+
# `verb` names the call in a refusal: CArray.save, CArray.dump, or
|
|
104
|
+
# Marshal.dump, whose payload is this format.
|
|
105
|
+
def initialize(io, verb = "CArray.save")
|
|
106
|
+
@verb = verb
|
|
107
|
+
case io
|
|
108
|
+
when String
|
|
109
|
+
@io = StringIO.new(io)
|
|
110
|
+
else
|
|
111
|
+
@io = io
|
|
112
|
+
end
|
|
142
113
|
end
|
|
143
114
|
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
# writes no trailer (both trailer fields zero), keeping the
|
|
163
|
-
# C / FORTRAN reader path a header + raw data.
|
|
164
|
-
trailer = build_trailer(ca)
|
|
165
|
-
trailer_str = trailer.empty? ? nil : encode_trailer(trailer)
|
|
166
|
-
|
|
167
|
-
data_offset = HEADER_BYTES
|
|
168
|
-
mask_offset = has_mask ? data_offset + data_bytes : 0
|
|
169
|
-
mask_bytes = has_mask ? elements : 0
|
|
170
|
-
tail_offset = has_mask ? mask_offset + mask_bytes : data_offset + data_bytes
|
|
171
|
-
trailer_offset = trailer_str ? tail_offset : 0
|
|
172
|
-
trailer_bytes = trailer_str ? trailer_str.bytesize : 0
|
|
173
|
-
|
|
174
|
-
dim = ca.shape
|
|
175
|
-
dim = dim + Array.new(CA_RANK_MAX - dim.size, 0)
|
|
176
|
-
|
|
177
|
-
header = {
|
|
178
|
-
has_mask: has_mask ? 1 : 0,
|
|
179
|
-
has_trailer: trailer_str ? 1 : 0,
|
|
180
|
-
data_type_code: CArray.data_type_code(ca.data_type),
|
|
181
|
-
ndim: ca.ndim,
|
|
182
|
-
shape: dim,
|
|
183
|
-
element_bytes: element_bytes,
|
|
184
|
-
flags: ca.flags,
|
|
185
|
-
elements: elements,
|
|
186
|
-
data_offset: data_offset,
|
|
187
|
-
data_bytes: data_bytes,
|
|
188
|
-
mask_offset: mask_offset,
|
|
189
|
-
mask_bytes: mask_bytes,
|
|
190
|
-
trailer_offset: trailer_offset,
|
|
191
|
-
trailer_bytes: trailer_bytes,
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
@io.write(self.class.pack_header(header, file_endian))
|
|
195
|
-
|
|
196
|
-
data_ca = swap ? to_other_byte_order(ca) : ca
|
|
197
|
-
data_ca.dump_binary(@io)
|
|
198
|
-
|
|
199
|
-
if has_mask
|
|
200
|
-
ca.mask.dump_binary(@io) # int8, endian-neutral
|
|
115
|
+
# @overload save(ca, endian: CArray.endian)
|
|
116
|
+
# Writes `ca` to the wrapped IO in the _CARRAY3 portable format.
|
|
117
|
+
# The data region is raw contiguous bytes; attribute Hash and
|
|
118
|
+
# data_class schema (if any) ride in a YAML trailer at the tail.
|
|
119
|
+
# @param ca [CArray] array to write.
|
|
120
|
+
# @param endian [Symbol, nil] forces the output byte order, `:big`,
|
|
121
|
+
# `:little` or `:native` (defaults to the host's; a differing value
|
|
122
|
+
# swaps data + header).
|
|
123
|
+
# @return [CArray] `ca`.
|
|
124
|
+
# @raise [ArgumentError] when `ca` is a CA_OBJECT array (use
|
|
125
|
+
# `Marshal.dump(ca)` instead), or carries a data_class the
|
|
126
|
+
# v1.0 flat-primitive schema cannot express.
|
|
127
|
+
# @raise [TypeError] when `ca` is a Face other than a record (save
|
|
128
|
+
# its storage through the Face's own accessor, such as `#ticks` or
|
|
129
|
+
# `#codes`, or its values with `Marshal.dump(ca.to_type(:object))`).
|
|
130
|
+
def save(ca, endian: nil)
|
|
131
|
+
write_plan(save_plan(ca, endian: endian))
|
|
132
|
+
return ca
|
|
201
133
|
end
|
|
202
134
|
|
|
203
|
-
|
|
135
|
+
private
|
|
204
136
|
|
|
205
|
-
|
|
206
|
-
|
|
137
|
+
# Checks `ca` and the options and builds everything save writes, without
|
|
138
|
+
# touching the IO, so a refusal leaves an existing file as it was.
|
|
139
|
+
def save_plan(ca, endian: nil)
|
|
140
|
+
unless ca.is_a?(CArray)
|
|
141
|
+
raise TypeError,
|
|
142
|
+
"#{@verb}: #{ca.class} is not a CArray" +
|
|
143
|
+
(ca.is_a?(CAFrame) ? "; keep a CAFrame with Marshal.dump(frame) or #write_csv" : "")
|
|
144
|
+
end
|
|
145
|
+
if ca.data_type == :object
|
|
146
|
+
raise ArgumentError,
|
|
147
|
+
"#{@verb} cannot serialise a CA_OBJECT array " \
|
|
148
|
+
"(arbitrary Ruby objects have no portable representation); " \
|
|
149
|
+
"use Marshal.dump(ca) for a Ruby-only round-trip"
|
|
150
|
+
end
|
|
151
|
+
# A Face other than a record is refused, as Marshal refuses it: the file
|
|
152
|
+
# would carry its storage without what makes it the Face (a unit, the
|
|
153
|
+
# string buffer, the labels), and load could not put that back.
|
|
154
|
+
if ca.face? && !ca.is_a?(CARecord)
|
|
155
|
+
storage = ca.__send__(:face_storage_method)
|
|
156
|
+
raise TypeError,
|
|
157
|
+
"#{@verb} cannot carry a #{ca.class} yet (its Face would be " \
|
|
158
|
+
"lost); " +
|
|
159
|
+
(storage ? "save its storage, which ##{storage} gives, or " : "") +
|
|
160
|
+
"keep its values with Marshal.dump(ca.to_type(:object))"
|
|
161
|
+
end
|
|
207
162
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
163
|
+
file_endian = case endian
|
|
164
|
+
when nil, :native then CArray.endian
|
|
165
|
+
when :big, :little then endian
|
|
166
|
+
else
|
|
167
|
+
raise ArgumentError,
|
|
168
|
+
"endian: must be :big, :little or :native (got #{endian.inspect})"
|
|
169
|
+
end
|
|
170
|
+
swap = (file_endian != CArray.endian)
|
|
171
|
+
|
|
172
|
+
elements = ca.elements
|
|
173
|
+
element_bytes = ca.bytes
|
|
174
|
+
data_bytes = elements * element_bytes
|
|
175
|
+
has_mask = ca.has_mask?
|
|
176
|
+
|
|
177
|
+
# Trailer is present only when there is semantic content: an
|
|
178
|
+
# attribute Hash or a data_class schema. A plain numeric array
|
|
179
|
+
# writes no trailer (both trailer fields zero), keeping the
|
|
180
|
+
# C / FORTRAN reader path a header + raw data.
|
|
181
|
+
trailer = build_trailer(ca)
|
|
182
|
+
trailer_str = trailer.empty? ? nil : encode_trailer(trailer)
|
|
183
|
+
|
|
184
|
+
data_offset = HEADER_BYTES
|
|
185
|
+
mask_offset = has_mask ? data_offset + data_bytes : 0
|
|
186
|
+
mask_bytes = has_mask ? elements : 0
|
|
187
|
+
tail_offset = has_mask ? mask_offset + mask_bytes : data_offset + data_bytes
|
|
188
|
+
trailer_offset = trailer_str ? tail_offset : 0
|
|
189
|
+
trailer_bytes = trailer_str ? trailer_str.bytesize : 0
|
|
190
|
+
|
|
191
|
+
dim = ca.shape
|
|
192
|
+
dim = dim + Array.new(CArray::RANK_MAX - dim.size, 0)
|
|
193
|
+
|
|
194
|
+
header = {
|
|
195
|
+
has_mask: has_mask ? 1 : 0,
|
|
196
|
+
has_trailer: trailer_str ? 1 : 0,
|
|
197
|
+
data_type_code: CArray.data_type_code(ca.data_type),
|
|
198
|
+
ndim: ca.ndim,
|
|
199
|
+
shape: dim,
|
|
200
|
+
element_bytes: element_bytes,
|
|
201
|
+
flags: ca.flags,
|
|
202
|
+
elements: elements,
|
|
203
|
+
data_offset: data_offset,
|
|
204
|
+
data_bytes: data_bytes,
|
|
205
|
+
mask_offset: mask_offset,
|
|
206
|
+
mask_bytes: mask_bytes,
|
|
207
|
+
trailer_offset: trailer_offset,
|
|
208
|
+
trailer_bytes: trailer_bytes,
|
|
209
|
+
}
|
|
226
210
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
211
|
+
data_ca = swap ? to_other_byte_order(ca) : ca
|
|
212
|
+
mask_ca = has_mask ? ca.mask : nil
|
|
213
|
+
return [self.class.pack_header(header, file_endian), data_ca, mask_ca, trailer_str]
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def write_plan(plan)
|
|
217
|
+
header_str, data_ca, mask_ca, trailer_str = plan
|
|
218
|
+
@io.write(header_str)
|
|
219
|
+
data_ca.dump_binary(@io)
|
|
220
|
+
mask_ca.dump_binary(@io) if mask_ca # int8, endian-neutral
|
|
221
|
+
@io.write(trailer_str) if trailer_str
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
public
|
|
225
|
+
|
|
226
|
+
# @overload load(data_type: nil)
|
|
227
|
+
# Reads a _CARRAY3 payload from the wrapped IO and returns the
|
|
228
|
+
# reconstructed array, re-wrapping it through its recorded
|
|
229
|
+
# data_class when the trailer carries one.
|
|
230
|
+
# @param data_type [Symbol, Integer, nil] overrides the element type for a
|
|
231
|
+
# bare `CA_FIXLEN` payload.
|
|
232
|
+
# @return [CArray]
|
|
233
|
+
# @raise [ArgumentError] on a bad magic string, corrupt header, or
|
|
234
|
+
# unsupported version.
|
|
235
|
+
def load(data_type: nil)
|
|
236
|
+
fixlen_type = data_type
|
|
237
|
+
buf = @io.read(HEADER_BYTES)
|
|
238
|
+
unless buf && buf.bytesize == HEADER_BYTES
|
|
239
|
+
raise ArgumentError, "not a CArray binary data (truncated header)"
|
|
240
|
+
end
|
|
241
|
+
if buf[0, 8] != MAGIC
|
|
242
|
+
raise ArgumentError, "not a CArray binary data (bad magic)"
|
|
243
|
+
end
|
|
238
244
|
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
end
|
|
251
|
-
check_header(h)
|
|
245
|
+
# The byte order is self-describing via endian_marker at offset 8:
|
|
246
|
+
# the writer stored 0x01020304 in file order, so the raw bytes are
|
|
247
|
+
# 01 02 03 04 on a big-endian file and 04 03 02 01 on a little-endian
|
|
248
|
+
# one. The leading byte alone decides; anything else is corrupt.
|
|
249
|
+
case buf[8].ord
|
|
250
|
+
when 0x01 then file_endian = :big
|
|
251
|
+
when 0x04 then file_endian = :little
|
|
252
|
+
else
|
|
253
|
+
raise ArgumentError, "corrupt CArray binary data (endian marker mismatch)"
|
|
254
|
+
end
|
|
255
|
+
h = self.class.unpack_header(buf, file_endian)
|
|
252
256
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
257
|
+
if h[:endian_marker] != ENDIAN_MARKER
|
|
258
|
+
raise ArgumentError, "corrupt CArray binary data (endian marker mismatch)"
|
|
259
|
+
end
|
|
260
|
+
if h[:version_major] != VERSION_MAJOR
|
|
261
|
+
raise ArgumentError, "unsupported CArray binary version #{h[:version_major]}.#{h[:version_minor]}"
|
|
262
|
+
end
|
|
263
|
+
if h[:header_bytes] != HEADER_BYTES
|
|
264
|
+
raise ArgumentError, "unsupported CArray binary header size #{h[:header_bytes]}"
|
|
265
|
+
end
|
|
266
|
+
if h[:data_bytes] != h[:elements] * h[:element_bytes]
|
|
267
|
+
raise ArgumentError, "corrupt CArray binary data (data_bytes cross-check failed)"
|
|
268
|
+
end
|
|
269
|
+
check_header(h)
|
|
258
270
|
|
|
259
|
-
|
|
260
|
-
data_type =
|
|
261
|
-
|
|
271
|
+
swap = (file_endian != CArray.endian)
|
|
272
|
+
data_type = h[:data_type_code]
|
|
273
|
+
ndim = h[:ndim]
|
|
274
|
+
dim = h[:shape][0, ndim]
|
|
275
|
+
bytes = h[:element_bytes]
|
|
262
276
|
|
|
263
|
-
|
|
264
|
-
|
|
277
|
+
if fixlen_type and data_type == CArray.data_type_code(CA_FIXLEN)
|
|
278
|
+
data_type = fixlen_type
|
|
279
|
+
else
|
|
280
|
+
# The file holds the type number; CArray.new takes its name.
|
|
281
|
+
data_type = CArray.data_type_name(data_type).to_sym
|
|
282
|
+
end
|
|
265
283
|
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
mask = CArray.boolean(*dim)
|
|
269
|
-
mask.load_binary(@io)
|
|
270
|
-
end
|
|
284
|
+
ca = CArray.new(data_type, dim, :bytes => bytes)
|
|
285
|
+
ca.load_binary(@io)
|
|
271
286
|
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
287
|
+
mask = nil
|
|
288
|
+
if h[:has_mask] != 0
|
|
289
|
+
mask = CArray.boolean(*dim)
|
|
290
|
+
mask.load_binary(@io)
|
|
276
291
|
end
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
292
|
+
|
|
293
|
+
if h[:trailer_bytes] > 0
|
|
294
|
+
trailer_raw = @io.read(h[:trailer_bytes])
|
|
295
|
+
unless trailer_raw && trailer_raw.bytesize == h[:trailer_bytes]
|
|
296
|
+
raise ArgumentError, "corrupt CArray binary data (truncated trailer)"
|
|
297
|
+
end
|
|
298
|
+
trailer = decode_trailer(trailer_raw)
|
|
299
|
+
unless trailer.is_a?(Hash)
|
|
300
|
+
raise ArgumentError, "corrupt CArray binary data (trailer is not a mapping)"
|
|
301
|
+
end
|
|
302
|
+
ca = apply_trailer(ca, trailer)
|
|
280
303
|
end
|
|
281
|
-
ca = apply_trailer(ca, trailer)
|
|
282
|
-
end
|
|
283
304
|
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
305
|
+
# Swap once the trailer is applied and before the mask is: a record is
|
|
306
|
+
# swapped member by member, as the writer swapped it, and only its
|
|
307
|
+
# data_class says where the members are. Swapping the bare fixlen
|
|
308
|
+
# first reversed the whole record.
|
|
309
|
+
ca[] = to_other_byte_order(ca) if swap
|
|
289
310
|
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
311
|
+
if mask
|
|
312
|
+
ca.mask = 0
|
|
313
|
+
ca.mask[] = mask
|
|
314
|
+
end
|
|
294
315
|
|
|
295
|
-
|
|
296
|
-
|
|
316
|
+
return ca
|
|
317
|
+
end
|
|
297
318
|
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
# `ca` in the other byte order. A member declared with `endian:` holds its
|
|
301
|
-
# bytes in that order on every machine and in every file, so it is left as
|
|
302
|
-
# it is; only the members in machine order are swapped.
|
|
303
|
-
def to_other_byte_order (ca)
|
|
304
|
-
swapped = ca.swap_bytes
|
|
305
|
-
declared = ca.respond_to?(:data_class) && ca.data_class &&
|
|
306
|
-
ca.data_class.fields.select(&:endian)
|
|
307
|
-
return swapped if !declared || declared.empty?
|
|
308
|
-
swapped = swapped.copy
|
|
309
|
-
declared.each { |f| swapped[f.name][] = ca[f.name].value }
|
|
310
|
-
swapped
|
|
311
|
-
end
|
|
319
|
+
private
|
|
312
320
|
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
321
|
+
# `ca` in the other byte order. A member declared with `endian:` holds its
|
|
322
|
+
# bytes in that order on every machine and in every file, so it is left as
|
|
323
|
+
# it is; only the members in machine order are swapped.
|
|
324
|
+
def to_other_byte_order(ca)
|
|
325
|
+
swapped = ca.swap_bytes
|
|
326
|
+
declared = ca.respond_to?(:data_class) && ca.data_class &&
|
|
327
|
+
ca.data_class.fields.select(&:endian)
|
|
328
|
+
return swapped if !declared || declared.empty?
|
|
329
|
+
swapped = swapped.copy
|
|
330
|
+
declared.each { |f| swapped[f.name][] = ca[f.name].value }
|
|
331
|
+
swapped
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
# The header fields that describe the same array have to agree, and a
|
|
335
|
+
# source whose size is known has to hold what the header declares --
|
|
336
|
+
# checked before the array is allocated, so a short file cannot make
|
|
337
|
+
# the reader allocate what it declares.
|
|
338
|
+
def check_header(h)
|
|
339
|
+
ndim = h[:ndim]
|
|
340
|
+
unless ndim >= 1 && ndim <= CArray::RANK_MAX
|
|
341
|
+
raise ArgumentError, "corrupt CArray binary data (ndim #{ndim} out of 1..#{CArray::RANK_MAX})"
|
|
342
|
+
end
|
|
343
|
+
dim = h[:shape][0, ndim]
|
|
344
|
+
if dim.any?(&:negative?) || dim.inject(:*) != h[:elements]
|
|
345
|
+
raise ArgumentError, "corrupt CArray binary data (shape #{dim.inspect} does not " \
|
|
346
|
+
"hold #{h[:elements]} elements)"
|
|
347
|
+
end
|
|
348
|
+
fixlen = CArray.data_type_code(CA_FIXLEN)
|
|
349
|
+
if h[:data_type_code] != fixlen &&
|
|
350
|
+
h[:data_type_code] <= CArray.data_type_code(CA_OBJECT) &&
|
|
351
|
+
h[:element_bytes] != CArray.sizeof(h[:data_type_code])
|
|
352
|
+
raise ArgumentError, "corrupt CArray binary data (element_bytes #{h[:element_bytes]} " \
|
|
353
|
+
"for data type #{CArray.data_type_name(h[:data_type_code]) rescue h[:data_type_code]})"
|
|
354
|
+
end
|
|
355
|
+
mask_bytes = (h[:has_mask] != 0) ? h[:elements] : 0
|
|
356
|
+
if h[:mask_bytes] != mask_bytes
|
|
357
|
+
raise ArgumentError, "corrupt CArray binary data (mask_bytes cross-check failed)"
|
|
358
|
+
end
|
|
359
|
+
if @io.respond_to?(:size) && @io.respond_to?(:pos)
|
|
360
|
+
declared = h[:data_bytes] + mask_bytes + h[:trailer_bytes]
|
|
361
|
+
if @io.size - @io.pos < declared
|
|
362
|
+
raise ArgumentError, "corrupt CArray binary data (declares #{declared} bytes after " \
|
|
363
|
+
"the header, the source holds #{@io.size - @io.pos})"
|
|
364
|
+
end
|
|
343
365
|
end
|
|
344
366
|
end
|
|
345
|
-
end
|
|
346
367
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
368
|
+
# Build the trailer mapping (String-keyed) from the array's
|
|
369
|
+
# attributes and data_class. Returns {} when there is nothing to
|
|
370
|
+
# serialise.
|
|
371
|
+
def build_trailer(ca)
|
|
372
|
+
trailer = {}
|
|
373
|
+
attrs = ca.attrs # frozen, merged along parent chain
|
|
374
|
+
trailer["attrs"] = attrs unless attrs.empty?
|
|
375
|
+
if ca.has_data_class? and ca.data_class
|
|
376
|
+
trailer["data_class"] = build_data_class_schema(ca.data_class)
|
|
377
|
+
end
|
|
378
|
+
trailer
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# Layer 1 (always) + Layer 2 `name` (when the class is named).
|
|
382
|
+
# ruby_marshal_b64 (full-identity round-trip of anonymous / dynamic
|
|
383
|
+
# classes) is deferred past v1.0 — anonymous classes round-trip via
|
|
384
|
+
# Layer 1 synthesis on load (duck-typed, identity not preserved),
|
|
385
|
+
# matching the "data_class identity out of scope for 3.0" pin.
|
|
386
|
+
def build_data_class_schema(klass)
|
|
387
|
+
members = klass.fields.map do |f|
|
|
388
|
+
if f.bitfield?
|
|
389
|
+
raise ArgumentError,
|
|
390
|
+
"#{@verb} cannot serialise data_class #{klass.inspect}: " \
|
|
391
|
+
"member #{f.name.inspect} is a bitfield, which the portable " \
|
|
392
|
+
"format does not describe; Marshal.dump(ca) carries it when " \
|
|
393
|
+
"the struct is named (assigned to a constant)"
|
|
394
|
+
end
|
|
395
|
+
pep = SYMBOL_TO_PEP[f.type]
|
|
396
|
+
unless pep
|
|
397
|
+
raise ArgumentError,
|
|
398
|
+
"#{@verb} cannot serialise data_class #{klass.inspect}: " \
|
|
399
|
+
"member #{f.name.inspect} has type #{f.type.inspect} " \
|
|
400
|
+
"(a nested struct, a template or a fixlen member), which the " \
|
|
401
|
+
"portable format does not describe; Marshal.dump(ca) carries " \
|
|
402
|
+
"it when the struct is named (assigned to a constant)"
|
|
403
|
+
end
|
|
404
|
+
m = { "name" => f.name, "type" => pep, "offset" => f.offset, "bytes" => f.bytes }
|
|
405
|
+
m["endian"] = f.endian.to_s if f.endian
|
|
406
|
+
m
|
|
407
|
+
end
|
|
408
|
+
|
|
409
|
+
schema = {
|
|
410
|
+
"kind" => (klass < CAUnion ? "union" : "struct"),
|
|
411
|
+
"record_bytes" => klass::DATA_SIZE,
|
|
412
|
+
"members" => members,
|
|
413
|
+
}
|
|
414
|
+
schema["name"] = klass.name if klass.name
|
|
415
|
+
schema["order_by"] = klass.order_by.map(&:to_s) if klass.order_by
|
|
416
|
+
schema
|
|
356
417
|
end
|
|
357
|
-
trailer
|
|
358
|
-
end
|
|
359
418
|
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
def build_data_class_schema (klass)
|
|
366
|
-
members = klass.fields.map do |f|
|
|
367
|
-
if f.bitfield?
|
|
368
|
-
raise ArgumentError,
|
|
369
|
-
"CArray.save cannot serialise data_class #{klass.inspect}: " \
|
|
370
|
-
"member #{f.name.inspect} is a bitfield (the v1.0 Layer 1 " \
|
|
371
|
-
"schema is flat-primitive only); Marshal.dump(ca) carries it " \
|
|
372
|
-
"when the struct is named (assigned to a constant)"
|
|
419
|
+
# Reconstruct attribute Hash + data_class wrapping from a parsed
|
|
420
|
+
# trailer. Returns the (possibly re-wrapped) array.
|
|
421
|
+
def apply_trailer(ca, trailer)
|
|
422
|
+
if trailer.key?("data_class")
|
|
423
|
+
ca = wrap_data_class(ca, trailer["data_class"])
|
|
373
424
|
end
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
"when the struct is named (assigned to a constant)"
|
|
382
|
-
end
|
|
383
|
-
m = { "name" => f.name, "type" => pep, "offset" => f.offset, "bytes" => f.bytes }
|
|
384
|
-
m["endian"] = f.endian.to_s if f.endian
|
|
385
|
-
m
|
|
425
|
+
if trailer.key?("attrs")
|
|
426
|
+
attrs = trailer["attrs"]
|
|
427
|
+
if attrs.is_a?(Hash) and not attrs.empty?
|
|
428
|
+
ca.set_attrs(attrs)
|
|
429
|
+
end
|
|
430
|
+
end
|
|
431
|
+
ca
|
|
386
432
|
end
|
|
387
433
|
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
434
|
+
# Load fall-through: prefer the named Ruby class (const_get), else
|
|
435
|
+
# synthesise an anonymous struct from the Layer 1 members[], else
|
|
436
|
+
# leave the raw CA_FIXLEN array untouched.
|
|
437
|
+
def wrap_data_class(ca, schema)
|
|
438
|
+
members = schema.is_a?(Hash) ? schema["members"] : nil
|
|
439
|
+
unless schema.is_a?(Hash) &&
|
|
440
|
+
(members.nil? || members.is_a?(Array) && members.all?(Hash))
|
|
441
|
+
raise ArgumentError, "corrupt CArray binary data (data_class schema is not a " \
|
|
442
|
+
"mapping with a list of member mappings)"
|
|
443
|
+
end
|
|
444
|
+
name = schema["name"]
|
|
445
|
+
if name
|
|
446
|
+
begin
|
|
447
|
+
klass = Kernel.const_get(name)
|
|
448
|
+
rescue NameError
|
|
449
|
+
klass = nil
|
|
450
|
+
end
|
|
451
|
+
if klass and CArray.data_class?(klass)
|
|
452
|
+
return CARecord.wrap(ca, klass)
|
|
453
|
+
end
|
|
454
|
+
end
|
|
397
455
|
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
ca = wrap_data_class(ca, trailer["data_class"])
|
|
403
|
-
end
|
|
404
|
-
if trailer.key?("attrs")
|
|
405
|
-
attrs = trailer["attrs"]
|
|
406
|
-
if attrs.is_a?(Hash) and not attrs.empty?
|
|
407
|
-
ca.set_attrs(attrs)
|
|
456
|
+
members = schema["members"]
|
|
457
|
+
if members.is_a?(Array) and not members.empty?
|
|
458
|
+
klass = synthesize_data_class(schema)
|
|
459
|
+
return CARecord.wrap(ca, klass)
|
|
408
460
|
end
|
|
409
|
-
end
|
|
410
|
-
ca
|
|
411
|
-
end
|
|
412
461
|
|
|
413
|
-
|
|
414
|
-
# synthesise an anonymous struct from the Layer 1 members[], else
|
|
415
|
-
# leave the raw CA_FIXLEN array untouched.
|
|
416
|
-
def wrap_data_class (ca, schema)
|
|
417
|
-
members = schema.is_a?(Hash) ? schema["members"] : nil
|
|
418
|
-
unless schema.is_a?(Hash) &&
|
|
419
|
-
(members.nil? || members.is_a?(Array) && members.all?(Hash))
|
|
420
|
-
raise ArgumentError, "corrupt CArray binary data (data_class schema is not a " \
|
|
421
|
-
"mapping with a list of member mappings)"
|
|
462
|
+
ca
|
|
422
463
|
end
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
464
|
+
|
|
465
|
+
# Build a runtime-defined CArray.struct / .union that reproduces the
|
|
466
|
+
# exact byte layout described by the Layer 1 schema (explicit offsets
|
|
467
|
+
# + total size so trailing padding is preserved).
|
|
468
|
+
def synthesize_data_class(schema)
|
|
469
|
+
members = schema["members"]
|
|
470
|
+
record_bytes = schema["record_bytes"]
|
|
471
|
+
builder = (schema["kind"] == "union") ? CArray.method(:union) : CArray.method(:struct)
|
|
472
|
+
order_by = schema["order_by"]
|
|
473
|
+
names = members.map { |m| m["name"].to_s }
|
|
474
|
+
if order_by && !(order_by.is_a?(Array) && order_by.all? { |n| names.include?(n.to_s) })
|
|
475
|
+
raise ArgumentError, "corrupt CArray binary data (order_by #{order_by.inspect} " \
|
|
476
|
+
"names a member the data_class does not have)"
|
|
477
|
+
end
|
|
478
|
+
opts = { :pack => 1, :size => record_bytes }
|
|
479
|
+
opts[:order_by] = order_by.map(&:to_sym) if order_by
|
|
480
|
+
builder.call(**opts) do
|
|
481
|
+
members.each do |m|
|
|
482
|
+
sym = PEP_TO_SYMBOL[m["type"]]
|
|
483
|
+
unless sym
|
|
484
|
+
raise ArgumentError, "unknown data_class member type #{m["type"].inspect} in CArray binary data"
|
|
485
|
+
end
|
|
486
|
+
opt = { :offset => m["offset"] }
|
|
487
|
+
if m.key?("endian")
|
|
488
|
+
endian = %w[big little native preserve].find { |e| e == m["endian"] }
|
|
489
|
+
unless endian
|
|
490
|
+
raise ArgumentError, "unknown data_class member endian #{m["endian"].inspect} in CArray binary data"
|
|
491
|
+
end
|
|
492
|
+
opt[:endian] = endian.to_sym
|
|
493
|
+
end
|
|
494
|
+
member sym, m["name"], opt
|
|
495
|
+
end
|
|
432
496
|
end
|
|
433
497
|
end
|
|
434
498
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
499
|
+
# YAML flow-style emission. Reads like a JSON one-liner but with
|
|
500
|
+
# native non-finite Float literals (.inf / .nan).
|
|
501
|
+
def encode_trailer(trailer)
|
|
502
|
+
visitor = Psych::Visitors::YAMLTree.create
|
|
503
|
+
visitor << trailer
|
|
504
|
+
ast = visitor.tree
|
|
505
|
+
ast.grep(Psych::Nodes::Mapping).each { |n| n.style = Psych::Nodes::Mapping::FLOW }
|
|
506
|
+
ast.grep(Psych::Nodes::Sequence).each { |n| n.style = Psych::Nodes::Sequence::FLOW }
|
|
507
|
+
# line_width: -1 disables the emitter's line folding so the flow
|
|
508
|
+
# trailer stays a single line regardless of length.
|
|
509
|
+
ast.yaml(nil, :line_width => -1).sub(/\A---\s*/, "").strip.b
|
|
510
|
+
end
|
|
511
|
+
|
|
512
|
+
# safe_load is the matching check on the read side: it restores the
|
|
513
|
+
# JSON-compatible value set + non-finite Floats and refuses any
|
|
514
|
+
# `!ruby/object:` tag, so a hostile trailer cannot execute code.
|
|
515
|
+
def decode_trailer(raw)
|
|
516
|
+
Psych.safe_load(raw, :permitted_classes => [], :aliases => false) || {}
|
|
517
|
+
rescue Psych::Exception => e
|
|
518
|
+
# A tag, an alias or text that is not YAML: the file is not one
|
|
519
|
+
# this format writes, which is what a caller rescues ArgumentError for.
|
|
520
|
+
raise ArgumentError,
|
|
521
|
+
"corrupt CArray binary data (trailer cannot be read: #{e.message})"
|
|
522
|
+
end
|
|
523
|
+
|
|
524
|
+
class << self
|
|
525
|
+
|
|
526
|
+
# Pack a header field Hash into a HEADER_BYTES buffer, integers in
|
|
527
|
+
# `file_endian` byte order.
|
|
528
|
+
def pack_header(h, file_endian)
|
|
529
|
+
e = (file_endian == :little) ? "<" : ">"
|
|
530
|
+
shape = h[:shape]
|
|
531
|
+
body = [
|
|
532
|
+
MAGIC, # a8 0
|
|
533
|
+
ENDIAN_MARKER, # L 8
|
|
534
|
+
VERSION_MAJOR, # C 12
|
|
535
|
+
VERSION_MINOR, # C 13
|
|
536
|
+
HEADER_BYTES, # S 14
|
|
537
|
+
h[:has_mask], # C 16
|
|
538
|
+
h[:has_trailer], # C 17
|
|
539
|
+
h[:data_type_code], # C 18
|
|
540
|
+
h[:ndim], # C 19
|
|
541
|
+
0, # L 20 reserved0
|
|
542
|
+
*shape, # q16 24
|
|
543
|
+
h[:element_bytes], # L 152
|
|
544
|
+
h[:flags], # l 156
|
|
545
|
+
h[:elements], # Q 160
|
|
546
|
+
h[:data_offset], # Q 168
|
|
547
|
+
h[:data_bytes], # Q 176
|
|
548
|
+
h[:mask_offset], # Q 184
|
|
549
|
+
h[:mask_bytes], # Q 192
|
|
550
|
+
h[:trailer_offset], # Q 200
|
|
551
|
+
h[:trailer_bytes], # Q 208
|
|
552
|
+
0, # Q 216 data_checksum
|
|
553
|
+
0, # C 224 checksum_algo
|
|
554
|
+
].pack("a8L#{e}CCS#{e}CCCCL#{e}q#{e}16L#{e}l#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}C")
|
|
555
|
+
body.ljust(HEADER_BYTES, "\x00")
|
|
556
|
+
end
|
|
557
|
+
|
|
558
|
+
# Parse a HEADER_BYTES buffer (integers in `file_endian`) into a
|
|
559
|
+
# field Hash. Byte-slice unpack mirrors the C struct offsets.
|
|
560
|
+
def unpack_header(buf, file_endian)
|
|
561
|
+
e = (file_endian == :little) ? "<" : ">"
|
|
562
|
+
{
|
|
563
|
+
endian_marker: buf[8, 4].unpack1("L#{e}"),
|
|
564
|
+
version_major: buf[12].ord,
|
|
565
|
+
version_minor: buf[13].ord,
|
|
566
|
+
header_bytes: buf[14, 2].unpack1("S#{e}"),
|
|
567
|
+
has_mask: buf[16].ord,
|
|
568
|
+
has_trailer: buf[17].ord,
|
|
569
|
+
data_type_code: buf[18].ord,
|
|
570
|
+
ndim: buf[19].ord,
|
|
571
|
+
shape: buf[24, 128].unpack("q#{e}16"),
|
|
572
|
+
element_bytes: buf[152, 4].unpack1("L#{e}"),
|
|
573
|
+
flags: buf[156, 4].unpack1("l#{e}"),
|
|
574
|
+
elements: buf[160, 8].unpack1("Q#{e}"),
|
|
575
|
+
data_offset: buf[168, 8].unpack1("Q#{e}"),
|
|
576
|
+
data_bytes: buf[176, 8].unpack1("Q#{e}"),
|
|
577
|
+
mask_offset: buf[184, 8].unpack1("Q#{e}"),
|
|
578
|
+
mask_bytes: buf[192, 8].unpack1("Q#{e}"),
|
|
579
|
+
trailer_offset: buf[200, 8].unpack1("Q#{e}"),
|
|
580
|
+
trailer_bytes: buf[208, 8].unpack1("Q#{e}"),
|
|
581
|
+
data_checksum: buf[216, 8].unpack1("Q#{e}"),
|
|
582
|
+
checksum_algo: buf[224].ord,
|
|
583
|
+
}
|
|
584
|
+
end
|
|
585
|
+
|
|
439
586
|
end
|
|
440
587
|
|
|
441
|
-
ca
|
|
442
588
|
end
|
|
589
|
+
# Loading this file defines the constant again, which drops the
|
|
590
|
+
# private_constant set beside the autoload.
|
|
591
|
+
private_constant :Serializer
|
|
592
|
+
end
|
|
593
|
+
|
|
594
|
+
class CArray
|
|
443
595
|
|
|
444
|
-
#
|
|
445
|
-
#
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
596
|
+
# The Marshal payload of `ca` ({#marshal_dump}, and the _dump of a view,
|
|
597
|
+
# a wrap, a source, a scalar and a mask).
|
|
598
|
+
def self.marshal_payload(ca)
|
|
599
|
+
target = (ca.class != CArray and ca.class != CScalar) ? ca.copy : ca
|
|
600
|
+
if target.data_type == :object
|
|
601
|
+
return ["object",
|
|
602
|
+
target.shape,
|
|
603
|
+
target.value.to_a,
|
|
604
|
+
(target.has_mask? ? target.mask.to_a : nil),
|
|
605
|
+
target.attrs.to_h]
|
|
606
|
+
else
|
|
607
|
+
io = StringIO.new("".b)
|
|
608
|
+
Serializer.new(io, "Marshal.dump").save(target)
|
|
609
|
+
return ["portable", io.string]
|
|
456
610
|
end
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
611
|
+
end
|
|
612
|
+
private_class_method :marshal_payload
|
|
613
|
+
|
|
614
|
+
# The array a {#marshal_dump} payload describes.
|
|
615
|
+
def self.array_from_marshal_payload(data)
|
|
616
|
+
tag, *rest = data
|
|
617
|
+
case tag
|
|
618
|
+
when "object"
|
|
619
|
+
shape, values, mask, attrs = rest
|
|
620
|
+
# The shape is only a claim until the cells are seen to fill it, and
|
|
621
|
+
# a damaged one can ask for more memory than the machine has.
|
|
622
|
+
unless marshal_cells_fill_shape?(values, shape) &&
|
|
623
|
+
(mask.nil? || marshal_cells_fill_shape?(mask, shape))
|
|
624
|
+
raise ArgumentError, "corrupt CArray Marshal payload (the cells do not fill shape #{shape.inspect})"
|
|
625
|
+
end
|
|
626
|
+
ca = CArray.object(*shape)
|
|
627
|
+
ca[] = values
|
|
628
|
+
if mask
|
|
629
|
+
ca.mask = 0
|
|
630
|
+
ca.mask[] = mask
|
|
474
631
|
end
|
|
632
|
+
ca.set_attrs(attrs) if attrs && !attrs.empty?
|
|
633
|
+
ca
|
|
634
|
+
when "portable"
|
|
635
|
+
CArray.load(StringIO.new(rest[0]))
|
|
636
|
+
when "record"
|
|
637
|
+
data_class, storage, attrs = rest
|
|
638
|
+
ca = CARecord.wrap(array_from_marshal_payload(storage), data_class)
|
|
639
|
+
ca.set_attrs(attrs) if attrs && !attrs.empty?
|
|
640
|
+
ca
|
|
641
|
+
else
|
|
642
|
+
raise TypeError, "unrecognised CArray Marshal payload"
|
|
475
643
|
end
|
|
476
644
|
end
|
|
645
|
+
private_class_method :array_from_marshal_payload
|
|
477
646
|
|
|
478
|
-
#
|
|
479
|
-
#
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
647
|
+
# Whether `cells`, nested as #to_a nests them, has the extent of every axis
|
|
648
|
+
# of `shape`. A cell may itself hold an Array, so only shape.size levels
|
|
649
|
+
# are counted.
|
|
650
|
+
def self.marshal_cells_fill_shape?(cells, shape)
|
|
651
|
+
return false unless shape.is_a?(Array) && !shape.empty? &&
|
|
652
|
+
shape.all? { |extent| extent.is_a?(Integer) && extent >= 0 }
|
|
653
|
+
level = [cells]
|
|
654
|
+
shape.each do |extent|
|
|
655
|
+
return false unless level.all? { |row| row.is_a?(Array) && row.size == extent }
|
|
656
|
+
level = level.flat_map { |row| row }
|
|
657
|
+
return true if level.empty?
|
|
658
|
+
end
|
|
659
|
+
true
|
|
489
660
|
end
|
|
661
|
+
private_class_method :marshal_cells_fill_shape?
|
|
490
662
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
663
|
+
end
|
|
664
|
+
|
|
665
|
+
# Marshal of a view, a mask, a wrap, a source and a scalar: the
|
|
666
|
+
# {CArray#marshal_dump} payload, loaded back by the class method rather than
|
|
667
|
+
# into an empty instance. A view, a wrap, a source and a mask come back as
|
|
668
|
+
# a plain array (a copy owns its memory, as #copy gives); a scalar comes
|
|
669
|
+
# back a scalar. Marshal writes the class name of what it dumps, and a
|
|
670
|
+
# later version loads it by that name, so a class here that is renamed
|
|
671
|
+
# keeps its old name as a constant that answers _load.
|
|
672
|
+
[CAView, CAWrap, CASource, CScalar, CArray::Mask, CAObject::Mask].each do |klass|
|
|
673
|
+
klass.class_eval do
|
|
674
|
+
# Marshal writes the instance variables of an object that has _dump
|
|
675
|
+
# after the String it returns, unless the String has instance
|
|
676
|
+
# variables of its own. A view keeps its parent in one, so a view of
|
|
677
|
+
# a large array would carry the whole parent, and a parent holding the
|
|
678
|
+
# view in a cell would be an object cycle. The String is given one of
|
|
679
|
+
# its own, which _load ignores.
|
|
680
|
+
private def dump_payload
|
|
681
|
+
Marshal.dump(CArray.send(:marshal_payload, self))
|
|
682
|
+
.tap { |s| s.instance_variable_set(:@carray_payload, true) }
|
|
683
|
+
end
|
|
497
684
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
#
|
|
501
|
-
#
|
|
502
|
-
def
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
h[:element_bytes], # L 152
|
|
518
|
-
h[:flags], # l 156
|
|
519
|
-
h[:elements], # Q 160
|
|
520
|
-
h[:data_offset], # Q 168
|
|
521
|
-
h[:data_bytes], # Q 176
|
|
522
|
-
h[:mask_offset], # Q 184
|
|
523
|
-
h[:mask_bytes], # Q 192
|
|
524
|
-
h[:trailer_offset], # Q 200
|
|
525
|
-
h[:trailer_bytes], # Q 208
|
|
526
|
-
0, # Q 216 data_checksum
|
|
527
|
-
0, # C 224 checksum_algo
|
|
528
|
-
].pack("a8L#{e}CCS#{e}CCCCL#{e}q#{e}16L#{e}l#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}Q#{e}C")
|
|
529
|
-
body.ljust(HEADER_BYTES, "\x00")
|
|
685
|
+
# A Face other than a record is refused: the payload would carry its
|
|
686
|
+
# storage without what makes it the Face (a unit, labels), and load
|
|
687
|
+
# could not put that back.
|
|
688
|
+
# @return [String]
|
|
689
|
+
private def _dump(_level)
|
|
690
|
+
if face?
|
|
691
|
+
storage = face_storage_method
|
|
692
|
+
raise TypeError,
|
|
693
|
+
"Marshal cannot carry a #{self.class} yet (its Face would be lost); " \
|
|
694
|
+
"dump its values with to_type(:object)" +
|
|
695
|
+
(storage ? ", or its storage, which ##{storage} gives" : "")
|
|
696
|
+
end
|
|
697
|
+
return dump_payload unless data_type == CA_OBJECT
|
|
698
|
+
# An object view dumps its cells by a nested Marshal.dump, which does
|
|
699
|
+
# not know the objects the outer dump has written. A cell holding the
|
|
700
|
+
# view again (directly or through other views) would dump it without
|
|
701
|
+
# end, so dumping the same view from inside its own dump is refused,
|
|
702
|
+
# as reading such a cell is.
|
|
703
|
+
refuse_reentry("Marshal.dump") { dump_payload }
|
|
530
704
|
end
|
|
705
|
+
end
|
|
706
|
+
end
|
|
531
707
|
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
version_minor: buf[13].ord,
|
|
540
|
-
header_bytes: buf[14, 2].unpack1("S#{e}"),
|
|
541
|
-
has_mask: buf[16].ord,
|
|
542
|
-
has_trailer: buf[17].ord,
|
|
543
|
-
data_type_code: buf[18].ord,
|
|
544
|
-
ndim: buf[19].ord,
|
|
545
|
-
shape: buf[24, 128].unpack("q#{e}16"),
|
|
546
|
-
element_bytes: buf[152, 4].unpack1("L#{e}"),
|
|
547
|
-
flags: buf[156, 4].unpack1("l#{e}"),
|
|
548
|
-
elements: buf[160, 8].unpack1("Q#{e}"),
|
|
549
|
-
data_offset: buf[168, 8].unpack1("Q#{e}"),
|
|
550
|
-
data_bytes: buf[176, 8].unpack1("Q#{e}"),
|
|
551
|
-
mask_offset: buf[184, 8].unpack1("Q#{e}"),
|
|
552
|
-
mask_bytes: buf[192, 8].unpack1("Q#{e}"),
|
|
553
|
-
trailer_offset: buf[200, 8].unpack1("Q#{e}"),
|
|
554
|
-
trailer_bytes: buf[208, 8].unpack1("Q#{e}"),
|
|
555
|
-
data_checksum: buf[216, 8].unpack1("Q#{e}"),
|
|
556
|
-
checksum_algo: buf[224].ord,
|
|
557
|
-
}
|
|
708
|
+
[CAView, CASource, CArray::Mask, CAObject::Mask].each do |klass|
|
|
709
|
+
# @return [CArray] the plain array a {#_dump} string describes.
|
|
710
|
+
# Defined with def, not define_singleton_method: a block's method keeps
|
|
711
|
+
# the block's self and cannot be called from another Ractor.
|
|
712
|
+
klass.singleton_class.class_eval do
|
|
713
|
+
private def _load(str)
|
|
714
|
+
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
558
715
|
end
|
|
716
|
+
end
|
|
717
|
+
end
|
|
559
718
|
|
|
719
|
+
class CAWrap
|
|
720
|
+
# A wrap borrows its memory, which Marshal cannot carry to another
|
|
721
|
+
# process: {#_dump} above writes its values, and the load gives a plain
|
|
722
|
+
# array that owns a copy of them, as it does for a view of the wrap.
|
|
723
|
+
private_class_method def self._load(str)
|
|
724
|
+
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
560
725
|
end
|
|
726
|
+
end
|
|
561
727
|
|
|
728
|
+
class CScalar
|
|
729
|
+
# @return [CScalar] the scalar a {#_dump} string describes.
|
|
730
|
+
private_class_method def self._load(str)
|
|
731
|
+
a = CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
732
|
+
s = CScalar.new(a.data_type, bytes: a.bytes)
|
|
733
|
+
s[0] = a[0]
|
|
734
|
+
s
|
|
735
|
+
end
|
|
562
736
|
end
|
|
563
737
|
|
|
564
738
|
class CArray
|
|
@@ -571,12 +745,15 @@ class CArray
|
|
|
571
745
|
# @param output [String, IO] destination path or IO.
|
|
572
746
|
# @param opt [Hash] serializer options (`:endian`).
|
|
573
747
|
# @return [CArray] `ca`.
|
|
574
|
-
def self.save
|
|
748
|
+
def self.save(ca, output, **opt)
|
|
575
749
|
case output
|
|
576
750
|
when String
|
|
751
|
+
# Refuse before opening: opening for writing empties an existing file.
|
|
752
|
+
plan = Serializer.new(nil).__send__(:save_plan, ca, **opt)
|
|
577
753
|
open(output, "wb:ASCII-8BIT") { |io|
|
|
578
|
-
|
|
754
|
+
Serializer.new(io).__send__(:write_plan, plan)
|
|
579
755
|
}
|
|
756
|
+
return ca
|
|
580
757
|
else
|
|
581
758
|
return Serializer.new(output).save(ca, **opt)
|
|
582
759
|
end
|
|
@@ -589,7 +766,7 @@ class CArray
|
|
|
589
766
|
# @param input [String, IO] source path, in-memory payload, or IO.
|
|
590
767
|
# @param opt [Hash] loader options (`:data_type`).
|
|
591
768
|
# @return [CArray]
|
|
592
|
-
def self.load
|
|
769
|
+
def self.load(input, **opt)
|
|
593
770
|
case input
|
|
594
771
|
when String
|
|
595
772
|
if input.bytesize >= Serializer::HEADER_BYTES and
|
|
@@ -611,9 +788,9 @@ class CArray
|
|
|
611
788
|
# @param ca [CArray] array to serialize.
|
|
612
789
|
# @param opt [Hash] serializer options (`:endian`).
|
|
613
790
|
# @return [String]
|
|
614
|
-
def self.dump
|
|
791
|
+
def self.dump(ca, **opt)
|
|
615
792
|
io = StringIO.new("".b)
|
|
616
|
-
Serializer.new(io).save(ca, **opt)
|
|
793
|
+
Serializer.new(io, "CArray.dump").save(ca, **opt)
|
|
617
794
|
return io.string
|
|
618
795
|
end
|
|
619
796
|
|
|
@@ -631,17 +808,8 @@ class CArray
|
|
|
631
808
|
# arrays are copied first so the payload always describes an
|
|
632
809
|
# owning array.
|
|
633
810
|
# @return [Array]
|
|
634
|
-
def marshal_dump
|
|
635
|
-
|
|
636
|
-
if target.data_type == :object
|
|
637
|
-
["object",
|
|
638
|
-
target.shape,
|
|
639
|
-
target.value.to_a,
|
|
640
|
-
(target.has_mask? ? target.mask.to_a : nil),
|
|
641
|
-
target.attrs.to_h]
|
|
642
|
-
else
|
|
643
|
-
["portable", CArray.dump(target)]
|
|
644
|
-
end
|
|
811
|
+
private def marshal_dump()
|
|
812
|
+
return CArray.send(:marshal_payload, self)
|
|
645
813
|
end
|
|
646
814
|
|
|
647
815
|
# @overload marshal_load(data)
|
|
@@ -649,112 +817,13 @@ class CArray
|
|
|
649
817
|
# {#marshal_dump}.
|
|
650
818
|
# @param data [Array] Marshal payload.
|
|
651
819
|
# @return [void]
|
|
652
|
-
def marshal_load
|
|
820
|
+
private def marshal_load(data)
|
|
653
821
|
source = CArray.send(:array_from_marshal_payload, data)
|
|
654
822
|
initialize_copy(source)
|
|
655
823
|
attrs = source.attrs
|
|
656
824
|
set_attrs(attrs) unless attrs.empty?
|
|
657
825
|
end
|
|
658
826
|
|
|
659
|
-
# The array a {#marshal_dump} payload describes.
|
|
660
|
-
def self.array_from_marshal_payload (data)
|
|
661
|
-
tag, *rest = data
|
|
662
|
-
case tag
|
|
663
|
-
when "object"
|
|
664
|
-
shape, values, mask, attrs = rest
|
|
665
|
-
ca = CArray.object(*shape)
|
|
666
|
-
ca[] = values
|
|
667
|
-
if mask
|
|
668
|
-
ca.mask = 0
|
|
669
|
-
ca.mask[] = mask
|
|
670
|
-
end
|
|
671
|
-
ca.set_attrs(attrs) if attrs && !attrs.empty?
|
|
672
|
-
ca
|
|
673
|
-
when "portable"
|
|
674
|
-
CArray.load(StringIO.new(rest[0]))
|
|
675
|
-
when "record"
|
|
676
|
-
data_class, storage, attrs = rest
|
|
677
|
-
ca = CARecord.wrap(array_from_marshal_payload(storage), data_class)
|
|
678
|
-
ca.set_attrs(attrs) if attrs && !attrs.empty?
|
|
679
|
-
ca
|
|
680
|
-
else
|
|
681
|
-
raise TypeError, "unrecognised CArray Marshal payload"
|
|
682
|
-
end
|
|
683
|
-
end
|
|
684
|
-
private_class_method :array_from_marshal_payload
|
|
685
|
-
|
|
686
|
-
end
|
|
687
|
-
|
|
688
|
-
# Marshal of a view, a mask and a scalar: the {CArray#marshal_dump} payload,
|
|
689
|
-
# loaded back by the class method rather than into an empty instance. A
|
|
690
|
-
# view, a wrap and a mask come back as a plain array (a copy owns its
|
|
691
|
-
# memory, as #copy gives); a scalar comes back a scalar.
|
|
692
|
-
[CAView, CAWrap, CScalar, CArrayMask, CAObjectMask].each do |klass|
|
|
693
|
-
klass.class_eval do
|
|
694
|
-
# A Face other than a record is refused: the payload would carry its
|
|
695
|
-
# storage without what makes it the Face (a unit, labels), and load
|
|
696
|
-
# could not put that back.
|
|
697
|
-
# @return [String]
|
|
698
|
-
def _dump (_level)
|
|
699
|
-
if face?
|
|
700
|
-
storage = face_storage_method
|
|
701
|
-
raise TypeError,
|
|
702
|
-
"Marshal cannot carry a #{self.class} yet (its Face would be lost); " \
|
|
703
|
-
"dump its values with to_type(:object)" +
|
|
704
|
-
(storage ? ", or its storage, which ##{storage} gives" : "")
|
|
705
|
-
end
|
|
706
|
-
return dump_payload unless data_type == CA_OBJECT
|
|
707
|
-
# An object view dumps its cells by a nested Marshal.dump, which does
|
|
708
|
-
# not know the objects the outer dump has written. A cell holding the
|
|
709
|
-
# view again (directly or through other views) would dump it without
|
|
710
|
-
# end, so dumping the same view from inside its own dump is refused,
|
|
711
|
-
# as reading such a cell is.
|
|
712
|
-
refuse_reentry("Marshal.dump") { dump_payload }
|
|
713
|
-
end
|
|
714
|
-
|
|
715
|
-
private
|
|
716
|
-
|
|
717
|
-
# Marshal writes the instance variables of an object that has _dump
|
|
718
|
-
# after the String it returns, unless the String has instance
|
|
719
|
-
# variables of its own. A view keeps its parent in one, so a view of
|
|
720
|
-
# a large array would carry the whole parent, and a parent holding the
|
|
721
|
-
# view in a cell would be an object cycle. The String is given one of
|
|
722
|
-
# its own, which _load ignores.
|
|
723
|
-
def dump_payload
|
|
724
|
-
Marshal.dump(CArray.instance_method(:marshal_dump).bind_call(self))
|
|
725
|
-
.tap { |s| s.instance_variable_set(:@carray_payload, true) }
|
|
726
|
-
end
|
|
727
|
-
end
|
|
728
|
-
end
|
|
729
|
-
|
|
730
|
-
[CAView, CArrayMask, CAObjectMask].each do |klass|
|
|
731
|
-
# @return [CArray] the plain array a {#_dump} string describes.
|
|
732
|
-
# Defined with def, not define_singleton_method: a block's method keeps
|
|
733
|
-
# the block's self and cannot be called from another Ractor.
|
|
734
|
-
klass.singleton_class.class_eval do
|
|
735
|
-
def _load (str)
|
|
736
|
-
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
737
|
-
end
|
|
738
|
-
end
|
|
739
|
-
end
|
|
740
|
-
|
|
741
|
-
class CAWrap
|
|
742
|
-
# A wrap borrows its memory, which Marshal cannot carry to another
|
|
743
|
-
# process: {#_dump} above writes its values, and the load gives a plain
|
|
744
|
-
# array that owns a copy of them, as it does for a view of the wrap.
|
|
745
|
-
def self._load (str)
|
|
746
|
-
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
747
|
-
end
|
|
748
|
-
end
|
|
749
|
-
|
|
750
|
-
class CScalar
|
|
751
|
-
# @return [CScalar] the scalar a {#_dump} string describes.
|
|
752
|
-
def self._load (str)
|
|
753
|
-
a = CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
754
|
-
s = CScalar.new(a.data_type, bytes: a.bytes)
|
|
755
|
-
s[0] = a[0]
|
|
756
|
-
s
|
|
757
|
-
end
|
|
758
827
|
end
|
|
759
828
|
|
|
760
829
|
class CARecord
|
|
@@ -764,10 +833,10 @@ class CARecord
|
|
|
764
833
|
# member (nested, fixlen, bit, array) travels. An anonymous struct has no
|
|
765
834
|
# name to write: it goes through the binary format, whose schema
|
|
766
835
|
# describes flat numeric members only.
|
|
767
|
-
def marshal_dump
|
|
768
|
-
return CArray.
|
|
836
|
+
private def marshal_dump
|
|
837
|
+
return CArray.send(:marshal_payload, self) unless data_class.name
|
|
769
838
|
["record", data_class,
|
|
770
|
-
CArray.
|
|
839
|
+
CArray.send(:marshal_payload, copy.parent), attrs.to_h]
|
|
771
840
|
end
|
|
772
841
|
|
|
773
842
|
end
|