line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Types::PDL; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: PDL types using Type::Tiny |
4
|
|
|
|
|
|
|
|
5
|
7
|
|
|
7
|
|
1529434
|
use strict; |
|
7
|
|
|
|
|
50
|
|
|
7
|
|
|
|
|
200
|
|
6
|
7
|
|
|
7
|
|
37
|
use warnings; |
|
7
|
|
|
|
|
12
|
|
|
7
|
|
|
|
|
256
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
9
|
|
|
|
|
|
|
|
10
|
7
|
|
|
7
|
|
37
|
use Carp; |
|
7
|
|
|
|
|
17
|
|
|
7
|
|
|
|
|
520
|
|
11
|
|
|
|
|
|
|
|
12
|
7
|
|
|
|
|
77
|
use Type::Library -base, |
13
|
|
|
|
|
|
|
-declare => qw[ |
14
|
|
|
|
|
|
|
Piddle |
15
|
|
|
|
|
|
|
Piddle0D |
16
|
|
|
|
|
|
|
Piddle1D |
17
|
|
|
|
|
|
|
Piddle2D |
18
|
|
|
|
|
|
|
Piddle3D |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
PiddleFromAny |
21
|
7
|
|
|
7
|
|
3172
|
]; |
|
7
|
|
|
|
|
163364
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
7
|
|
|
7
|
|
16574
|
use Types::Standard -types, 'is_Int'; |
|
7
|
|
|
|
|
348653
|
|
|
7
|
|
|
|
|
75
|
|
25
|
7
|
|
|
7
|
|
36761
|
use Type::Utils; |
|
7
|
|
|
|
|
33897
|
|
|
7
|
|
|
|
|
100
|
|
26
|
7
|
|
|
7
|
|
13600
|
use Type::TinyX::Facets; |
|
7
|
|
|
|
|
16149
|
|
|
7
|
|
|
|
|
83
|
|
27
|
7
|
|
|
7
|
|
5335
|
use String::Errf 'errf'; |
|
7
|
|
|
|
|
187179
|
|
|
7
|
|
|
|
|
56
|
|
28
|
7
|
|
|
7
|
|
2406
|
use B qw(perlstring); |
|
7
|
|
|
|
|
15
|
|
|
7
|
|
|
|
|
5532
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
facet 'empty', sub { |
32
|
|
|
|
|
|
|
my ( $o, $var ) = @_; |
33
|
|
|
|
|
|
|
return unless exists $o->{empty}; |
34
|
|
|
|
|
|
|
errf '%{not}s%{var}s->isempty', |
35
|
|
|
|
|
|
|
{ var => $var, not => ( !!delete( $o->{empty} ) ? '' : '!' ) }; |
36
|
|
|
|
|
|
|
}; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
facet 'null', sub { |
39
|
|
|
|
|
|
|
my ( $o, $var ) = @_; |
40
|
|
|
|
|
|
|
return unless exists $o->{null}; |
41
|
|
|
|
|
|
|
errf '%{not}s%{var}s->isnull', |
42
|
|
|
|
|
|
|
{ var => $var, not => ( !!delete( $o->{null} ) ? '' : '!' ) }; |
43
|
|
|
|
|
|
|
}; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
facet ndims => sub { |
46
|
|
|
|
|
|
|
my ( $o, $var ) = @_; |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my %o = map { ( $_ => delete $o->{$_} ) } |
49
|
|
|
|
|
|
|
grep { exists $o->{$_} } qw[ ndims ndims_min ndims_max ]; |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
return unless keys %o; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
croak( "'$_' must be an integer\n" ) |
54
|
|
|
|
|
|
|
for grep { !is_Int( $o{$_} ) } keys %o; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
if ( exists $o{ndims_max} and exists $o{ndims_min} ) { |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
if ( $o{ndims_max} < $o{ndims_min} ) { |
60
|
|
|
|
|
|
|
croak( "'ndims_min' must be <= 'ndims_max'\n" ); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
elsif ( $o{ndims_min} == $o{ndims_max} ) { |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
croak( |
66
|
|
|
|
|
|
|
"cannot mix 'ndims' facet with either 'ndims_min' or 'ndims_max'\n" |
67
|
|
|
|
|
|
|
) if exists $o{ndims}; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
$o{ndims} = delete $o{ndims_min}; |
70
|
|
|
|
|
|
|
delete $o{ndims_max}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
my @code; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
if ( exists $o{ndims_max} or exists $o{ndims_min} ) { |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
if ( exists $o{ndims_min} ) { |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
push @code, errf '%{var}s->ndims >= %{value}i', |
81
|
|
|
|
|
|
|
{ |
82
|
|
|
|
|
|
|
var => $var, |
83
|
|
|
|
|
|
|
value => delete $o{ndims_min} }; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
if ( exists $o{ndims_max} ) { |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
push @code, errf '%{var}s->ndims <= %{value}i', |
89
|
|
|
|
|
|
|
{ |
90
|
|
|
|
|
|
|
var => $var, |
91
|
|
|
|
|
|
|
value => delete $o{ndims_max} }; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
elsif ( exists $o{ndims} ) { |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
push @code, errf '%{var}s->ndims == %{value}i', |
98
|
|
|
|
|
|
|
{ var => $var, value => delete $o{ndims} }; |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
else { |
102
|
|
|
|
|
|
|
return; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
croak( "cannot mix 'ndims' facet with either 'ndims_min' or 'ndims_max'\n" ) |
106
|
|
|
|
|
|
|
if keys %o; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
return join( ' and ', @code ); |
109
|
|
|
|
|
|
|
}; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
facet 'type', sub { |
113
|
|
|
|
|
|
|
my ( $o, $var ) = @_; |
114
|
|
|
|
|
|
|
return unless exists $o->{type}; |
115
|
|
|
|
|
|
|
my $type = eval { PDL::Type->new( delete $o->{type} )->ioname }; |
116
|
|
|
|
|
|
|
croak( "type must be a valid type name or a PDL::Type object: $@\n" ) |
117
|
|
|
|
|
|
|
if $@; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
errf '%{var}s->type->ioname eq q[%{type}s]', |
120
|
|
|
|
|
|
|
{ var => $var, type => $type }; |
121
|
|
|
|
|
|
|
}; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
facetize qw[ empty null ndims type ], class_type Piddle, { class => 'PDL' }; |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
facetize qw[ null ], declare Piddle0D, as Piddle [ ndims => 0 ]; |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
facetize qw[ empty null ], declare Piddle1D, as Piddle [ ndims => 1 ]; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
facetize qw[ empty null ], declare Piddle2D, as Piddle [ ndims => 2 ]; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
facetize qw[ empty null ], declare Piddle3D, as Piddle [ ndims => 3 ]; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
declare_coercion PiddleFromAny, to_type Piddle, from Any, q[ do { local $@; |
134
|
|
|
|
|
|
|
require PDL::Core; |
135
|
|
|
|
|
|
|
my $new = eval { PDL::Core::topdl( $_ ) }; |
136
|
|
|
|
|
|
|
$@ ? $_ : $new |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
]; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1; |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
# |
144
|
|
|
|
|
|
|
# This file is part of Types-PDL |
145
|
|
|
|
|
|
|
# |
146
|
|
|
|
|
|
|
# This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory. |
147
|
|
|
|
|
|
|
# |
148
|
|
|
|
|
|
|
# This is free software, licensed under: |
149
|
|
|
|
|
|
|
# |
150
|
|
|
|
|
|
|
# The GNU General Public License, Version 3, June 2007 |
151
|
|
|
|
|
|
|
# |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=pod |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 NAME |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Types::PDL - PDL types using Type::Tiny |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 VERSION |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
version 0.02 |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 SYNOPSIS |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
use Types::PDL -types; |
166
|
|
|
|
|
|
|
use Type::Params qw[ validate ]; |
167
|
|
|
|
|
|
|
use PDL; |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
validate( [ pdl ], Piddle ); |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=head1 DESCRIPTION |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
This module provides L compatible types for L. |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 Types |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Types which accept parameters (see L) will list them. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head3 C |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Allows an object blessed into the class C, e.g. |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
validate( [pdl], Piddle ); |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
It accepts the following parameters: |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
null |
188
|
|
|
|
|
|
|
empty |
189
|
|
|
|
|
|
|
ndims |
190
|
|
|
|
|
|
|
ndims_min |
191
|
|
|
|
|
|
|
ndims_max |
192
|
|
|
|
|
|
|
type |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
=head3 C |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Allows an object blessed into the class C with C = 0. |
197
|
|
|
|
|
|
|
It accepts the following parameters: |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
null |
200
|
|
|
|
|
|
|
type |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
=head3 C |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
Allows an object blessed into the class C with C = 1. |
205
|
|
|
|
|
|
|
It accepts the following parameters: |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
null |
208
|
|
|
|
|
|
|
empty |
209
|
|
|
|
|
|
|
type |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head3 C |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Allows an object blessed into the class C with C = 2. |
214
|
|
|
|
|
|
|
It accepts the following parameters: |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
null |
217
|
|
|
|
|
|
|
empty |
218
|
|
|
|
|
|
|
type |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head3 C |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
Allows an object blessed into the class C with C = 3. |
223
|
|
|
|
|
|
|
It accepts the following parameters: |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
null |
226
|
|
|
|
|
|
|
empty |
227
|
|
|
|
|
|
|
type |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head2 Coercions |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
The following coercions are provided, and may be applied via a type |
232
|
|
|
|
|
|
|
object's L or |
233
|
|
|
|
|
|
|
L methods, e.g. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
Piddle->plus_coercions( PiddleFromAny ); |
236
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head3 C |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Uses L to coerce the value into a piddle. |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
=head2 Parameters |
242
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
Some types take optional parameters which add additional constraints |
244
|
|
|
|
|
|
|
on the object. For example, to indicate that only empty piddles are |
245
|
|
|
|
|
|
|
accepted: |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
validate( [pdl], Piddle[ empty => 1 ] ); |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
The available parameters are: |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head3 C |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
This accepts a boolean value; if true the piddle must be empty |
254
|
|
|
|
|
|
|
(i.e. the C method returns true), if false, it must not be |
255
|
|
|
|
|
|
|
empty. |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=head3 C |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
This accepts a boolean value; if true the piddle must be a null |
260
|
|
|
|
|
|
|
piddle, if false, it must not be null. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=head3 C |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
This specifies a fixed number of dimensions which the piddle must |
265
|
|
|
|
|
|
|
have. Don't mix use this with C or C. |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
=head3 C |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
The minimum number of dimensions the piddle may have. Don't specify |
270
|
|
|
|
|
|
|
this with C. |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
=head3 C |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
The maximum number of dimensions the piddle may have. Don't specify |
275
|
|
|
|
|
|
|
this with C. |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=head3 C |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
The type of the piddle. The value may be a L object or a |
280
|
|
|
|
|
|
|
string containing the name of a type (e.g., C). For a complete |
281
|
|
|
|
|
|
|
list of types, run this command: |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
perl -MPDL::Types=mapfld,ppdefs \ |
284
|
|
|
|
|
|
|
-E 'say mapfld( $_ => 'ppsym' => 'ioname' ) for ppdefs' |
285
|
|
|
|
|
|
|
|
286
|
|
|
|
|
|
|
=head1 BUGS AND LIMITATIONS |
287
|
|
|
|
|
|
|
|
288
|
|
|
|
|
|
|
You can make new bug reports, and view existing ones, through the |
289
|
|
|
|
|
|
|
web interface at L. |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
=head1 AUTHOR |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
Diab Jerius |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory. |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
This is free software, licensed under: |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
The GNU General Public License, Version 3, June 2007 |
302
|
|
|
|
|
|
|
|
303
|
|
|
|
|
|
|
=cut |
304
|
|
|
|
|
|
|
|
305
|
|
|
|
|
|
|
__END__ |