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/serialize.rb
CHANGED
|
@@ -59,605 +59,560 @@
|
|
|
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
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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
|
|
408
454
|
end
|
|
409
|
-
end
|
|
410
|
-
ca
|
|
411
|
-
end
|
|
412
455
|
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
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)"
|
|
422
|
-
end
|
|
423
|
-
name = schema["name"]
|
|
424
|
-
if name
|
|
425
|
-
begin
|
|
426
|
-
klass = Kernel.const_get(name)
|
|
427
|
-
rescue NameError
|
|
428
|
-
klass = nil
|
|
429
|
-
end
|
|
430
|
-
if klass and CArray.data_class?(klass)
|
|
456
|
+
members = schema["members"]
|
|
457
|
+
if members.is_a?(Array) and not members.empty?
|
|
458
|
+
klass = synthesize_data_class(schema)
|
|
431
459
|
return CARecord.wrap(ca, klass)
|
|
432
460
|
end
|
|
433
|
-
end
|
|
434
461
|
|
|
435
|
-
|
|
436
|
-
if members.is_a?(Array) and not members.empty?
|
|
437
|
-
klass = synthesize_data_class(schema)
|
|
438
|
-
return CARecord.wrap(ca, klass)
|
|
462
|
+
ca
|
|
439
463
|
end
|
|
440
464
|
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
sym = PEP_TO_SYMBOL[m["type"]]
|
|
462
|
-
unless sym
|
|
463
|
-
raise ArgumentError, "unknown data_class member type #{m["type"].inspect} in CArray binary data"
|
|
464
|
-
end
|
|
465
|
-
opt = { :offset => m["offset"] }
|
|
466
|
-
if m.key?("endian")
|
|
467
|
-
endian = %w[big little native preserve].find { |e| e == m["endian"] }
|
|
468
|
-
unless endian
|
|
469
|
-
raise ArgumentError, "unknown data_class member endian #{m["endian"].inspect} in CArray binary data"
|
|
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"
|
|
470
485
|
end
|
|
471
|
-
opt[
|
|
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
|
|
472
495
|
end
|
|
473
|
-
member sym, m["name"], opt
|
|
474
496
|
end
|
|
475
497
|
end
|
|
476
|
-
end
|
|
477
|
-
|
|
478
|
-
# YAML flow-style emission. Reads like a JSON one-liner but with
|
|
479
|
-
# native non-finite Float literals (.inf / .nan).
|
|
480
|
-
def encode_trailer (trailer)
|
|
481
|
-
visitor = Psych::Visitors::YAMLTree.create
|
|
482
|
-
visitor << trailer
|
|
483
|
-
ast = visitor.tree
|
|
484
|
-
ast.grep(Psych::Nodes::Mapping).each { |n| n.style = Psych::Nodes::Mapping::FLOW }
|
|
485
|
-
ast.grep(Psych::Nodes::Sequence).each { |n| n.style = Psych::Nodes::Sequence::FLOW }
|
|
486
|
-
# line_width: -1 disables the emitter's line folding so the flow
|
|
487
|
-
# trailer stays a single line regardless of length.
|
|
488
|
-
ast.yaml(nil, :line_width => -1).sub(/\A---\s*/, "").strip.b
|
|
489
|
-
end
|
|
490
498
|
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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
|
|
497
557
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
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")
|
|
530
|
-
end
|
|
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
|
|
531
585
|
|
|
532
|
-
# Parse a HEADER_BYTES buffer (integers in `file_endian`) into a
|
|
533
|
-
# field Hash. Byte-slice unpack mirrors the C struct offsets.
|
|
534
|
-
def unpack_header (buf, file_endian)
|
|
535
|
-
e = (file_endian == CA_LITTLE_ENDIAN) ? "<" : ">"
|
|
536
|
-
{
|
|
537
|
-
endian_marker: buf[8, 4].unpack1("L#{e}"),
|
|
538
|
-
version_major: buf[12].ord,
|
|
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
|
-
}
|
|
558
586
|
end
|
|
559
587
|
|
|
560
588
|
end
|
|
561
|
-
|
|
589
|
+
# Loading this file defines the constant again, which drops the
|
|
590
|
+
# private_constant set beside the autoload.
|
|
591
|
+
private_constant :Serializer
|
|
562
592
|
end
|
|
563
593
|
|
|
564
594
|
class CArray
|
|
565
595
|
|
|
566
|
-
#
|
|
567
|
-
#
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
# @param ca [CArray] array to write.
|
|
571
|
-
# @param output [String, IO] destination path or IO.
|
|
572
|
-
# @param opt [Hash] serializer options (`:endian`).
|
|
573
|
-
# @return [CArray] `ca`.
|
|
574
|
-
def self.save (ca, output, **opt)
|
|
575
|
-
case output
|
|
576
|
-
when String
|
|
577
|
-
open(output, "wb:ASCII-8BIT") { |io|
|
|
578
|
-
return Serializer.new(io).save(ca, **opt)
|
|
579
|
-
}
|
|
580
|
-
else
|
|
581
|
-
return Serializer.new(output).save(ca, **opt)
|
|
582
|
-
end
|
|
583
|
-
end
|
|
584
|
-
|
|
585
|
-
# @overload load(input, **opt)
|
|
586
|
-
# Reads a _CARRAY3 payload from `input`. A String starting with
|
|
587
|
-
# the CArray magic is decoded in place; other Strings are treated
|
|
588
|
-
# as file paths; anything else is used as IO.
|
|
589
|
-
# @param input [String, IO] source path, in-memory payload, or IO.
|
|
590
|
-
# @param opt [Hash] loader options (`:data_type`).
|
|
591
|
-
# @return [CArray]
|
|
592
|
-
def self.load (input, **opt)
|
|
593
|
-
case input
|
|
594
|
-
when String
|
|
595
|
-
if input.bytesize >= Serializer::HEADER_BYTES and
|
|
596
|
-
input.byteslice(0, 8) == Serializer::MAGIC
|
|
597
|
-
io = StringIO.new(input)
|
|
598
|
-
return Serializer.new(io).load(**opt)
|
|
599
|
-
else
|
|
600
|
-
open(input, "rb:ASCII-8BIT") { |io|
|
|
601
|
-
return Serializer.new(io).load(**opt)
|
|
602
|
-
}
|
|
603
|
-
end
|
|
604
|
-
else
|
|
605
|
-
return Serializer.new(input).load(**opt)
|
|
606
|
-
end
|
|
607
|
-
end
|
|
608
|
-
|
|
609
|
-
# @overload dump(ca, **opt)
|
|
610
|
-
# Returns `ca` serialized to a String in the _CARRAY3 format.
|
|
611
|
-
# @param ca [CArray] array to serialize.
|
|
612
|
-
# @param opt [Hash] serializer options (`:endian`).
|
|
613
|
-
# @return [String]
|
|
614
|
-
def self.dump (ca, **opt)
|
|
615
|
-
io = StringIO.new("".b)
|
|
616
|
-
Serializer.new(io).save(ca, **opt)
|
|
617
|
-
return io.string
|
|
618
|
-
end
|
|
619
|
-
|
|
620
|
-
# for Marshal
|
|
621
|
-
#
|
|
622
|
-
# Decision 4: the Marshal path and the portable format are
|
|
623
|
-
# separated. A CA_OBJECT array Marshal-dumps its element VALUEs
|
|
624
|
-
# directly (Ruby-only, no portability promise); every other array
|
|
625
|
-
# reuses the portable format as a numeric container. So
|
|
626
|
-
# Marshal.dump works for object arrays even though CArray.save
|
|
627
|
-
# refuses them.
|
|
628
|
-
|
|
629
|
-
# @overload marshal_dump
|
|
630
|
-
# Returns the Marshal payload for `self`. Virtual / wrapped
|
|
631
|
-
# arrays are copied first so the payload always describes an
|
|
632
|
-
# owning array.
|
|
633
|
-
# @return [Array]
|
|
634
|
-
def marshal_dump ()
|
|
635
|
-
target = (self.class != CArray and self.class != CScalar) ? self.copy : self
|
|
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
|
|
636
600
|
if target.data_type == :object
|
|
637
|
-
["object",
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
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]
|
|
642
606
|
else
|
|
643
|
-
|
|
607
|
+
io = StringIO.new("".b)
|
|
608
|
+
Serializer.new(io, "Marshal.dump").save(target)
|
|
609
|
+
return ["portable", io.string]
|
|
644
610
|
end
|
|
645
611
|
end
|
|
646
|
-
|
|
647
|
-
# @overload marshal_load(data)
|
|
648
|
-
# Reconstitutes `self` from a Marshal payload produced by
|
|
649
|
-
# {#marshal_dump}.
|
|
650
|
-
# @param data [Array] Marshal payload.
|
|
651
|
-
# @return [void]
|
|
652
|
-
def marshal_load (data)
|
|
653
|
-
source = CArray.send(:array_from_marshal_payload, data)
|
|
654
|
-
initialize_copy(source)
|
|
655
|
-
attrs = source.attrs
|
|
656
|
-
set_attrs(attrs) unless attrs.empty?
|
|
657
|
-
end
|
|
612
|
+
private_class_method :marshal_payload
|
|
658
613
|
|
|
659
614
|
# The array a {#marshal_dump} payload describes.
|
|
660
|
-
def self.array_from_marshal_payload
|
|
615
|
+
def self.array_from_marshal_payload(data)
|
|
661
616
|
tag, *rest = data
|
|
662
617
|
case tag
|
|
663
618
|
when "object"
|
|
@@ -692,7 +647,7 @@ class CArray
|
|
|
692
647
|
# Whether `cells`, nested as #to_a nests them, has the extent of every axis
|
|
693
648
|
# of `shape`. A cell may itself hold an Array, so only shape.size levels
|
|
694
649
|
# are counted.
|
|
695
|
-
def self.marshal_cells_fill_shape?
|
|
650
|
+
def self.marshal_cells_fill_shape?(cells, shape)
|
|
696
651
|
return false unless shape.is_a?(Array) && !shape.empty? &&
|
|
697
652
|
shape.all? { |extent| extent.is_a?(Integer) && extent >= 0 }
|
|
698
653
|
level = [cells]
|
|
@@ -707,17 +662,31 @@ class CArray
|
|
|
707
662
|
|
|
708
663
|
end
|
|
709
664
|
|
|
710
|
-
# Marshal of a view, a mask and a scalar: the
|
|
711
|
-
# loaded back by the class method rather than
|
|
712
|
-
# view, a wrap and a mask come back as
|
|
713
|
-
# memory, as #copy gives); a scalar comes
|
|
714
|
-
|
|
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|
|
|
715
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
|
|
684
|
+
|
|
716
685
|
# A Face other than a record is refused: the payload would carry its
|
|
717
686
|
# storage without what makes it the Face (a unit, labels), and load
|
|
718
687
|
# could not put that back.
|
|
719
688
|
# @return [String]
|
|
720
|
-
def _dump
|
|
689
|
+
private def _dump(_level)
|
|
721
690
|
if face?
|
|
722
691
|
storage = face_storage_method
|
|
723
692
|
raise TypeError,
|
|
@@ -733,28 +702,15 @@ end
|
|
|
733
702
|
# as reading such a cell is.
|
|
734
703
|
refuse_reentry("Marshal.dump") { dump_payload }
|
|
735
704
|
end
|
|
736
|
-
|
|
737
|
-
private
|
|
738
|
-
|
|
739
|
-
# Marshal writes the instance variables of an object that has _dump
|
|
740
|
-
# after the String it returns, unless the String has instance
|
|
741
|
-
# variables of its own. A view keeps its parent in one, so a view of
|
|
742
|
-
# a large array would carry the whole parent, and a parent holding the
|
|
743
|
-
# view in a cell would be an object cycle. The String is given one of
|
|
744
|
-
# its own, which _load ignores.
|
|
745
|
-
def dump_payload
|
|
746
|
-
Marshal.dump(CArray.instance_method(:marshal_dump).bind_call(self))
|
|
747
|
-
.tap { |s| s.instance_variable_set(:@carray_payload, true) }
|
|
748
|
-
end
|
|
749
705
|
end
|
|
750
706
|
end
|
|
751
707
|
|
|
752
|
-
[CAView,
|
|
708
|
+
[CAView, CASource, CArray::Mask, CAObject::Mask].each do |klass|
|
|
753
709
|
# @return [CArray] the plain array a {#_dump} string describes.
|
|
754
710
|
# Defined with def, not define_singleton_method: a block's method keeps
|
|
755
711
|
# the block's self and cannot be called from another Ractor.
|
|
756
712
|
klass.singleton_class.class_eval do
|
|
757
|
-
def _load
|
|
713
|
+
private def _load(str)
|
|
758
714
|
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
759
715
|
end
|
|
760
716
|
end
|
|
@@ -764,14 +720,14 @@ class CAWrap
|
|
|
764
720
|
# A wrap borrows its memory, which Marshal cannot carry to another
|
|
765
721
|
# process: {#_dump} above writes its values, and the load gives a plain
|
|
766
722
|
# array that owns a copy of them, as it does for a view of the wrap.
|
|
767
|
-
def self._load
|
|
723
|
+
private_class_method def self._load(str)
|
|
768
724
|
CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
769
725
|
end
|
|
770
726
|
end
|
|
771
727
|
|
|
772
728
|
class CScalar
|
|
773
729
|
# @return [CScalar] the scalar a {#_dump} string describes.
|
|
774
|
-
def self._load
|
|
730
|
+
private_class_method def self._load(str)
|
|
775
731
|
a = CArray.send(:array_from_marshal_payload, Marshal.load(str))
|
|
776
732
|
s = CScalar.new(a.data_type, bytes: a.bytes)
|
|
777
733
|
s[0] = a[0]
|
|
@@ -779,6 +735,97 @@ class CScalar
|
|
|
779
735
|
end
|
|
780
736
|
end
|
|
781
737
|
|
|
738
|
+
class CArray
|
|
739
|
+
|
|
740
|
+
# @overload save(ca, output, **opt)
|
|
741
|
+
# Writes `ca` to `output` in the _CARRAY3 portable format. A
|
|
742
|
+
# String `output` is opened as a binary file; anything else is
|
|
743
|
+
# treated as an IO-like object.
|
|
744
|
+
# @param ca [CArray] array to write.
|
|
745
|
+
# @param output [String, IO] destination path or IO.
|
|
746
|
+
# @param opt [Hash] serializer options (`:endian`).
|
|
747
|
+
# @return [CArray] `ca`.
|
|
748
|
+
def self.save(ca, output, **opt)
|
|
749
|
+
case output
|
|
750
|
+
when String
|
|
751
|
+
# Refuse before opening: opening for writing empties an existing file.
|
|
752
|
+
plan = Serializer.new(nil).__send__(:save_plan, ca, **opt)
|
|
753
|
+
open(output, "wb:ASCII-8BIT") { |io|
|
|
754
|
+
Serializer.new(io).__send__(:write_plan, plan)
|
|
755
|
+
}
|
|
756
|
+
return ca
|
|
757
|
+
else
|
|
758
|
+
return Serializer.new(output).save(ca, **opt)
|
|
759
|
+
end
|
|
760
|
+
end
|
|
761
|
+
|
|
762
|
+
# @overload load(input, **opt)
|
|
763
|
+
# Reads a _CARRAY3 payload from `input`. A String starting with
|
|
764
|
+
# the CArray magic is decoded in place; other Strings are treated
|
|
765
|
+
# as file paths; anything else is used as IO.
|
|
766
|
+
# @param input [String, IO] source path, in-memory payload, or IO.
|
|
767
|
+
# @param opt [Hash] loader options (`:data_type`).
|
|
768
|
+
# @return [CArray]
|
|
769
|
+
def self.load(input, **opt)
|
|
770
|
+
case input
|
|
771
|
+
when String
|
|
772
|
+
if input.bytesize >= Serializer::HEADER_BYTES and
|
|
773
|
+
input.byteslice(0, 8) == Serializer::MAGIC
|
|
774
|
+
io = StringIO.new(input)
|
|
775
|
+
return Serializer.new(io).load(**opt)
|
|
776
|
+
else
|
|
777
|
+
open(input, "rb:ASCII-8BIT") { |io|
|
|
778
|
+
return Serializer.new(io).load(**opt)
|
|
779
|
+
}
|
|
780
|
+
end
|
|
781
|
+
else
|
|
782
|
+
return Serializer.new(input).load(**opt)
|
|
783
|
+
end
|
|
784
|
+
end
|
|
785
|
+
|
|
786
|
+
# @overload dump(ca, **opt)
|
|
787
|
+
# Returns `ca` serialized to a String in the _CARRAY3 format.
|
|
788
|
+
# @param ca [CArray] array to serialize.
|
|
789
|
+
# @param opt [Hash] serializer options (`:endian`).
|
|
790
|
+
# @return [String]
|
|
791
|
+
def self.dump(ca, **opt)
|
|
792
|
+
io = StringIO.new("".b)
|
|
793
|
+
Serializer.new(io, "CArray.dump").save(ca, **opt)
|
|
794
|
+
return io.string
|
|
795
|
+
end
|
|
796
|
+
|
|
797
|
+
# for Marshal
|
|
798
|
+
#
|
|
799
|
+
# Decision 4: the Marshal path and the portable format are
|
|
800
|
+
# separated. A CA_OBJECT array Marshal-dumps its element VALUEs
|
|
801
|
+
# directly (Ruby-only, no portability promise); every other array
|
|
802
|
+
# reuses the portable format as a numeric container. So
|
|
803
|
+
# Marshal.dump works for object arrays even though CArray.save
|
|
804
|
+
# refuses them.
|
|
805
|
+
|
|
806
|
+
# @overload marshal_dump
|
|
807
|
+
# Returns the Marshal payload for `self`. Virtual / wrapped
|
|
808
|
+
# arrays are copied first so the payload always describes an
|
|
809
|
+
# owning array.
|
|
810
|
+
# @return [Array]
|
|
811
|
+
private def marshal_dump()
|
|
812
|
+
return CArray.send(:marshal_payload, self)
|
|
813
|
+
end
|
|
814
|
+
|
|
815
|
+
# @overload marshal_load(data)
|
|
816
|
+
# Reconstitutes `self` from a Marshal payload produced by
|
|
817
|
+
# {#marshal_dump}.
|
|
818
|
+
# @param data [Array] Marshal payload.
|
|
819
|
+
# @return [void]
|
|
820
|
+
private def marshal_load(data)
|
|
821
|
+
source = CArray.send(:array_from_marshal_payload, data)
|
|
822
|
+
initialize_copy(source)
|
|
823
|
+
attrs = source.attrs
|
|
824
|
+
set_attrs(attrs) unless attrs.empty?
|
|
825
|
+
end
|
|
826
|
+
|
|
827
|
+
end
|
|
828
|
+
|
|
782
829
|
class CARecord
|
|
783
830
|
|
|
784
831
|
# A record array keeps marshal_dump: it round-trips with its class. A
|
|
@@ -786,10 +833,10 @@ class CARecord
|
|
|
786
833
|
# member (nested, fixlen, bit, array) travels. An anonymous struct has no
|
|
787
834
|
# name to write: it goes through the binary format, whose schema
|
|
788
835
|
# describes flat numeric members only.
|
|
789
|
-
def marshal_dump
|
|
790
|
-
return CArray.
|
|
836
|
+
private def marshal_dump
|
|
837
|
+
return CArray.send(:marshal_payload, self) unless data_class.name
|
|
791
838
|
["record", data_class,
|
|
792
|
-
CArray.
|
|
839
|
+
CArray.send(:marshal_payload, copy.parent), attrs.to_h]
|
|
793
840
|
end
|
|
794
841
|
|
|
795
842
|
end
|