blib/lib/PDL/IO/Storable.pm

filter: Statementstmt 89.4% 85 / 95 Branchbran 52.5% 21 / 40 Conditioncond 40.0% 6 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 91.6% 11 / 12 Podpod 0.0% 0 / 7 totaltotal 69.2% 124 / 179 CC 27 SCAR 27.3
CC
27
Cov
75%
pdlunpack
29.3
STORABLE_thaw
5.2
unpack64bit
3.2
CRAP 15.4
« blib/lib/PDL/Reduce.pm ↑ Summary blib/lib/PDL/Perldl2/Script.pm »
1  
2 #
3 # GENERATED WITH PDL::PP! Don't modify!
4 #
5 package PDL::IO::Storable;
6  
7 48 @EXPORT_OK = qw( );
8 48 %EXPORT_TAGS = (Func=>[@EXPORT_OK]);
9  
10 48 ▶use PDL::Core;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 48 ▶use PDL::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 48 ▶use DynaLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15  
16
17 48 @ISA = ( 'PDL::Exporter','DynaLoader' );
18 48 push @PDL::Core::PP, __PACKAGE__;
19 48 bootstrap PDL::IO::Storable ;
20  
21  
22  
23  
24  
25  
26  
27  
28  
29 =head1 NAME
30  
31 PDL::IO::Storable - helper functions to make PDL usable with Storable
32  
33 =head1 SYNOPSIS
34  
35 use Storable;
36 use PDL::IO::Storable;
37 $hash = {
38 'foo' => 42,
39 'bar' => zeroes(23,45),
40 };
41 store $hash, 'perlhash.dat';
42  
43 =head1 DESCRIPTION
44  
45 C<Storable> implements object persistence for Perl data structures that can
46 contain arbitrary Perl objects. This module implements the relevant methods to
47 be able to store and retrieve piddles via Storable.
48  
49 =head1 FUNCTIONS
50  
51 =cut
52  
53  
54  
55  
56 48 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 { package PDL;
59 # routines to make PDL work with Storable >= 1.03
60  
61 # pdlpack() serializes a piddle, while pdlunpack() unserializes it. Earlier
62 # versions of PDL didn't control for endianness, type sizes and enumerated type
63 # values; this made stored data unportable across different architectures and
64 # PDL versions. This is no longer the case, but the reading code is still able
65 # to read the old files. The old files have no meta-information in them so it's
66 # impossible to read them correctly with 100% accuracy, but we try to make an
67 # educated guess
68 #
69 # Old data format:
70 #
71 # int type
72 # int ndims
73 # int dims[ndims]
74 # data
75 #
76 # Note that here all the sizes and endiannesses are the native. This is
77 # un-portable. Furthermore, the "type" is an enum, and its values could change
78 # between PDL versions. Here I assume that the old format input data is indeed
79 # native, so the old data files have the same portability issues, but at least
80 # things will remain working and broken in the same way they were before
81 #
82 #
83 # New format:
84 #
85 # uint64 0xFFFF FFFF FFFF FFFF # meant to be different from the old-style data
86 # char type[16] # ' '-padded, left-aligned type string such as 'PDL_LL'
87 # uint32 sizeof(type) # little-endian
88 # uint32 one # native-endian. Used to determine the endianness
89 # uint64 ndims # little-endian
90 # uint64 dims[ndims] # little-endian
91 # data
92 #
93 # The header data is all little-endian here. The data is stored with native
94 # endianness. On load it is checked, and a swap happens, if it is required
95  
96 sub pdlpack {
97 5 ▶ my ($pdl) = @_;
Subroutine pdlpack: called CC=3 SCAR=11.2
Pod undocumented
98  
99 5 my $hdr = pack( 'c8A16VL',
100 (-1) x 8,
101 $pdl->type->symbol,
102 PDL::Core::howbig( $pdl->get_datatype ), 1 );
103  
104 # I'd like this to be simply
105 # my $dimhdr = pack( 'Q<*', $pdl->getndims, $pdl->dims )
106 # but my pack() may not support Q, so I break it up manually
107 #
108 # if sizeof(int) == 4 here, then $_>>32 will not return 0 necessarily (this in
109 # undefined). I thus manually make sure this is the case
110 #
111 5 ▶ my $noMSW = (PDL::Core::howbig($PDL::Types::PDL_IND) < 8) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
PDL::Core::howbig($PDL::Types::PDL_IND) < 8 ? : 0 5 5
112 my $dimhdr = pack( 'V*',
113 5 ▶ map( { $_ & 0xFFFFFFFF, $noMSW ? 0 : ($_ >> 32) } ($pdl->getndims, $pdl->dims ) ) );
Branch: 1 branch
BranchTrueFalseTotal
$noMSW ? : 0 11 11
114  
115 5 my $dref = $pdl->get_dataref;
116 5 return $hdr . $dimhdr . $$dref;
117 }
118  
119 sub pdlunpack {
120 48 ▶ use Config ();
Subroutine BEGIN: called CC=1 SCAR=0.0
121 9 ▶ my ($pdl,$pack) = @_;
Subroutine pdlunpack: called CC=18 SCAR=33.8
Pod undocumented
122  
123 9 my ($type, $ndims);
124 9 my @dims = ();
125  
126 9 my $do_swap = 0;
127  
128 # first I try to infer the type of this storable
129 9 my $offset = 0;
130 9 my @magicheader = unpack( "ll", substr( $pack, $offset ) );
131 9 $offset += 8;
132  
133 9 ▶ if( $magicheader[0] != -1 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($magicheader[0] != -1 or $magicheader[1] != -1) { } 2 7 9
Condition66%2/3
$magicheader[0] != -1 or $magicheader[1] != -1
l!l&&r!l&&!r
207
MC/DC50%1/2
$magicheader[0] != -1 or $magicheader[1] != -1
$magicheader[0] != -1 $magicheader[1] != -1
Truth table66%2/3
A or B
A= $magicheader[0] != -1B= $magicheader[1] != -1
ABresult
1X1
011
000
134 $magicheader[1] != -1 )
135 {
136 2 ▶ print "PDL::IO::Storable detected an old-style pdl\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 2 2
137  
138 # old-style data. I leave the data sizes, endianness native, since I don't
139 # know any better. This at least won't break anything.
140 #
141 # The "type" however needs attention. Most-recent old-format data had these
142 # values for the type:
143 #
144 # enum { byte,
145 # short,
146 # unsigned short,
147 # long,
148 # long long,
149 # float,
150 # double }
151 #
152 # The $type I read from the file is assumed to be in this enum even though
153 # PDL may have added other types in the middle of this enum.
154 2 my @reftypes = ($PDL::Types::PDL_B,
155 $PDL::Types::PDL_S,
156 $PDL::Types::PDL_U,
157 $PDL::Types::PDL_L,
158 $PDL::Types::PDL_LL,
159 $PDL::Types::PDL_F,
160 $PDL::Types::PDL_D);
161  
162 2 my $stride = $Config::Config{intsize};
163 2 ($type,$ndims) = unpack 'i2', $pack;
164 2 ▶ @dims = $ndims > 0 ? unpack 'i*', substr $pack, 2*$stride,
Branch: 1 branch
BranchTrueFalseTotal
$ndims > 0 ? : 2 0 2
165 $ndims*$stride : ();
166  
167 2 $offset = (2+$ndims)*$stride;
168  
169 2 ▶ if( $type < 0 || $type >= @reftypes )
Branch: 1 branch
BranchTrueFalseTotal
if ($type < 0 or $type >= @reftypes) 0 2 2
Condition33%1/3
$type < 0 or $type >= @reftypes
l!l&&r!l&&!r
002
MC/DC0%0/2
$type < 0 or $type >= @reftypes
$type < 0 $type >= @reftypes
Truth table33%1/3
A or B
A= $type < 0B= $type >= @reftypes
ABresult
1X1
011
000
170 {
171 0 croak "Reading in old-style pdl with unknown type: $type. Giving up.";
172 }
173 2 $type = $reftypes[$type];
174 }
175 else
176 {
177 7 ▶ print "PDL::IO::Storable detected a new-style pdl\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 7 7
178  
179 # new-style data. I KNOW the data sizes, endianness and the type enum
180 7 my ($typestring) = unpack( 'A16', substr( $pack, $offset ) );
181 7 $offset += 16;
182  
183 7 $type = eval( '$PDL::Types::' . $typestring );
184 7 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
185 {
186 0 croak "PDL::IO::Storable couldn't parse type string '$typestring'. Giving up";
187 }
188  
189 7 my ($sizeof) = unpack( 'V', substr( $pack, $offset ) );
190 7 $offset += 4;
191 7 ▶ if( $sizeof != PDL::Core::howbig( $type ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($sizeof != PDL::Core::howbig($type)) 0 7 7
192 {
193 0 croak
194 "PDL::IO::Storable sees mismatched data type sizes when reading data of type '$typestring'\n" .
195 "Stored data has sizeof = $sizeof, while here it is " . PDL::Core::howbig( $type ) . ".\n" .
196 "Giving up";
197 }
198  
199 # check the endianness, if the "1" I read is interpreted as "1" on my
200 # machine then the endiannesses match, and I can just read the data
201 7 my ($one) = unpack( 'L', substr( $pack, $offset ) );
202 7 $offset += 4;
203  
204 7 ▶ if( $one == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($one == 1) { } 7 0 7
205 {
206 7 ▶ print "PDL::IO::Storable detected matching endianness\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 7 7
207 }
208 else
209 {
210 0 ▶ print "PDL::IO::Storable detected non-matching endianness. Correcting data on load\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
211  
212 # mismatched endianness. Let's make sure it's a big/little issue, not
213 # something weird. If mismatched, the '00000001' should be seen as
214 # '01000000'
215 0 ▶ if( $one != 0x01000000 )
Branch: 1 branch
BranchTrueFalseTotal
if ($one != 16777216) 0 0 0
216 {
217 0 croak
218 "PDL::IO::Storable sees confused endianness. A '1' was read as '$one'.\n" .
219 "This is neither matching nor swapped endianness. I don't know what's going on,\n" .
220 "so I'm giving up."
221 }
222  
223 # all righty. Everything's fine, but I need to swap all the data
224 0 $do_swap = 1;
225 }
226  
227  
228  
229 # mostly this acts like unpack('Q<'...), but works even if my unpack()
230 # doesn't support 'Q'. This also makes sure that my PDL_Indx is large enough
231 # to read this piddle
232 sub unpack64bit
233 {
234 14 ▶ my ($count, $pack, $offset) = @_;
Subroutine unpack64bit: called CC=3 SCAR=11.6
Pod undocumented
235  
236 return map
237 {
238 14 my ($lsw, $msw) = unpack('VV', substr($$pack, $$offset));
239 16 $$offset += 8;
240  
241 16 ▶ croak( "PDL::IO::Storable tried reading a file with dimensions that don't fit into 32 bits.\n" .
Branch: 1 branch
BranchTrueFalseTotal
if PDL::Core::howbig($PDL::Types::PDL_IND) < 8 and $msw != 0 0 16 16
Condition33%1/3
PDL::Core::howbig($PDL::Types::PDL_IND) < 8 and $msw != 0
!ll&&!rl&&r
1600
MC/DC0%0/2
PDL::Core::howbig($PDL::Types::PDL_IND) < 8 and $msw != 0
PDL::Core::howbig($PDL::Types::PDL_IND) < 8 $msw != 0
Truth table33%1/3
A and B
A= PDL::Core::howbig($PDL::Types::PDL_IND) < 8B= $msw != 0
ABresult
0X0
100
111
242 "However here PDL_Indx can't store a number so large. Giving up." )
243 if( PDL::Core::howbig($PDL::Types::PDL_IND) < 8 && $msw != 0 );
244  
245 16 (($msw << 32) | $lsw)
246 } (1..$count);
247 }
248  
249 7 ($ndims) = unpack64bit( 1, \$pack, \$offset );
250 7 ▶ @dims = unpack64bit( $ndims, \$pack, \$offset ) if $ndims > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ndims > 0 7 0 7
251 }
252  
253 9 ▶ print "thawing PDL, Dims: [",join(',',@dims),"]\n" if $PDL::verbose;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 9 9
254 9 $pdl->make_null; # make this a real piddle -- this is the tricky bit!
255 9 $pdl->set_datatype($type);
256 9 $pdl->setdims([@dims]);
257 9 my $dref = $pdl->get_dataref;
258  
259 9 $$dref = substr $pack, $offset;
260 9 ▶ if( $do_swap && PDL::Core::howbig( $type ) != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($do_swap and PDL::Core::howbig($type) != 1) 0 9 9
Condition33%1/3
$do_swap and PDL::Core::howbig($type) != 1
!ll&&!rl&&r
900
MC/DC0%0/2
$do_swap and PDL::Core::howbig($type) != 1
$do_swap PDL::Core::howbig($type) != 1
Truth table33%1/3
A and B
A= $do_swapB= PDL::Core::howbig($type) != 1
ABresult
0X0
100
111
261 {
262 0 swapEndian( $$dref, PDL::Core::howbig( $type ) );
263 }
264 9 $pdl->upd_data;
265 9 return $pdl;
266 }
267  
268 sub STORABLE_freeze {
269 6 ▶ my ($self, $cloning) = @_;
Subroutine STORABLE_freeze: called CC=2 SCAR=6.9
Pod undocumented
270 # return if $cloning; # Regular default serialization
271 6 ▶ return UNIVERSAL::isa($self, "HASH") ? ("",{%$self}) # hash ref -> Storable
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($self, 'HASH') ? : 1 5 6
272 : (pdlpack $self); # pack the piddle into a long string
273 }
274  
275 sub STORABLE_thaw {
276 10 ▶ my ($pdl,$cloning,$serial,$hashref) = @_;
Subroutine STORABLE_thaw: called CC=5 SCAR=16.6
Pod undocumented
277 # print "in STORABLE_thaw\n";
278 # return if $cloning;
279 10 my $class = ref $pdl;
280 10 ▶ if (defined $hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hashref) { } 1 9 10
281 1 ▶ croak "serial data with hashref!" unless !defined $serial ||
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::not defined $serial or $serial eq "" 0 1 1
Condition33%1/3
CORE::not defined $serial or $serial eq ""
l!l&&r!l&&!r
010
MC/DC0%0/2
CORE::not defined $serial or $serial eq ""
CORE::not defined $serial $serial eq ""
Truth table33%1/3
not(A) or B
A= CORE::not defined $serialB= $serial eq ""
ABresult
1X1
011
000
282 $serial eq "";
283 1 for (keys %$hashref) { $pdl->{$_} = $hashref->{$_} }
284 } else {
285 # all the magic is happening in pdlunpack
286 9 $pdl->pdlunpack($serial); # unpack our serial into this sv
287 }
288 }
289  
290 # have these as PDL methods
291  
292 =head2 store
293  
294 =for ref
295  
296 store a piddle using L<Storable|Storable>
297  
298 =for example
299  
300 $a = random 12,10;
301 $a->store('myfile');
302  
303 =cut
304  
305 =head2 freeze
306  
307 =for ref
308  
309 freeze a piddle using L<Storable|Storable>
310  
311 =for example
312  
313 $a = random 12,10;
314 $frozen = $a->freeze;
315  
316 =cut
317  
318 0 ▶sub store { require Storable; Storable::store(@_) }
Subroutine store: not called CC=1 SCAR=6.9
Pod undocumented
319 1 ▶sub freeze { require Storable; Storable::freeze(@_) }
Subroutine freeze: called CC=1 SCAR=0.0
Pod undocumented
320 }
321  
322 =head1 AUTHOR
323  
324 Copyright (C) 2013 Dima Kogan <dima@secretsauce.net>
325 Copyright (C) 2002 Christian Soeller <c.soeller@auckland.ac.nz>
326 All rights reserved. There is no warranty. You are allowed
327 to redistribute this software / documentation under certain
328 conditions. For details, see the file COPYING in the PDL
329 distribution. If this file is separated from the PDL distribution,
330 the copyright notice should be included in the file.
331  
332 =cut
333  
334  
335  
336  
337 48 ;
338  
339  
340  
341 # Exit with OK status
342  
343 48 1;
344  
345
« blib/lib/PDL/Reduce.pm ↑ Summary blib/lib/PDL/Perldl2/Script.pm »