blib/lib/PDL/Char.pm

filter: Statementstmt 97.0% 100 / 103 Branchbran 76.1% 32 / 42 Conditioncond 81.8% 9 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 90.0% 9 / 10 Podpod 80.0% 4 / 5 totaltotal 88.7% 158 / 178 CC 28 SCAR 18.6
CC
28
Cov
90%
_rcharpack
11.4
new
6.0
string
6.0
CRAP 6.4
« blib/lib/PDL/Fit/Polynomial.pm ↑ Summary blib/lib/PDL/Filter/LinPred.pm »
1 package PDL::Char;
2  
3 1 @ISA = qw (PDL);
4 1 ▶use overload ("\"\"" => \&PDL::Char::string);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use vars ('$level', '@dims'); # Global Vars used
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 =head1 NAME
10  
11 PDL::Char -- PDL subclass which allows reading and writing of fixed-length character strings as byte PDLs
12  
13 =head1 SYNOPSIS
14  
15 use PDL;
16 use PDL::Char;
17  
18 my $pchar = PDL::Char->new( [['abc', 'def', 'ghi'],['jkl', 'mno', 'pqr']] );
19
20 $pchar->setstr(1,0,'foo');
21
22 print $pchar; # 'string' bound to "", perl stringify function
23 # Prints:
24 # [
25 # ['abc' 'foo' 'ghi']
26 # ['jkl' 'mno' 'pqr']
27 # ]
28  
29 print $pchar->atstr(2,0);
30 # Prints:
31 # ghi
32  
33 =head1 DESCRIPTION
34  
35 This subclass of PDL allows one to manipulate PDLs of 'byte' type as if they were made of fixed
36 length strings, not just numbers.
37  
38 This type of behavior is useful when you want to work with charactar grids. The indexing is done
39 on a string level and not a character level for the 'setstr' and 'atstr' commands.
40  
41 This module is in particular useful for writing NetCDF files that include character data using the
42 PDL::NetCDF module.
43  
44 =head1 FUNCTIONS
45  
46 =head2 new
47  
48 =for ref
49  
50 Function to create a byte PDL from a string, list of strings, list of list of strings, etc.
51  
52 =for usage
53  
54 # create a new PDL::Char from a perl array of strings
55 $strpdl = PDL::Char->new( ['abc', 'def', 'ghij'] );
56  
57 # Convert a PDL of type 'byte' to a PDL::Char
58 $strpdl1 = PDL::Char->new (sequence (byte, 4, 5)+99);
59  
60 =for example
61  
62 $pdlchar3d = PDL::Char->new([['abc','def','ghi'],['jkl', 'mno', 'pqr']]);
63  
64 =cut
65  
66  
67 sub new {
68 3 ▶ my $type = shift;
Subroutine new: called CC=6 SCAR=18.0
Pod documented
69 3 ▶ my $value = (scalar(@_)>1 ? [@_] : shift); # ref thyself
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ > 1 ? : 0 3 3
70  
71 # re-bless byte PDLs as PDL::Char
72 3 ▶ if (ref($value) =~ /PDL/) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($value) =~ /PDL/) 1 2 3
73 1 ▶ PDL::Core::barf('Cannot convert a non-byte PDL to PDL::Char')
Branch: 1 branch
BranchTrueFalseTotal
if $value->get_datatype != $PDL::Types::PDL_B 0 1 1
74 if ($value->get_datatype != $PDL::Types::PDL_B);
75 1 return bless $value, $type;
76 }
77  
78 2 my $ptype = $PDL::Types::PDL_B;
79 2 my $self = PDL->initialize();
80 2 $self->set_datatype($ptype);
81 2 ▶ $value = 0 if !defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2 2
82 2 $level = 0; @dims = (); # package vars
83 2 my $maxlength; # max length seen for all character strings
84 2 my $samelen = 1; # Flag = 1 if all character strings are the same length
85  
86 # 1st Pass thru the perl array structure, assume all strings the same length
87 2 my $str = _rcharpack($value,\$maxlength,\$samelen);
88 2 ▶ unless( $samelen){ # Strings weren't the same length, go thru again and null pad to
Branch: 1 branch
BranchTrueFalseTotal
unless ($samelen) 1 1 2
89 # the max length.
90 1 $str = _rcharpack2($value,$maxlength);
91 }
92 2 $self->setdims([reverse @dims]);
93 2 ${$self->get_dataref} = $str;
94 2 $self->upd_data();
95 2 return bless $self, $type;
96 }
97
98 # Take an N-D perl array of strings and pack it into a single string,
99 # updating the $level and @dims package vars on the way.
100 # Used by the 'char' constructor
101 #
102 # References supplied so $maxlength and $samelen are updated along the way as well.
103 #
104 #
105 # This version (_rcharpack) is for the 1st pass thru the N-d string array.
106 # It assumes that all strings are the same length, but also checks to see if they aren't
107 sub _rcharpack {
108  
109 28 ▶ my $a = shift; # Input string
Subroutine _rcharpack: called CC=11 SCAR=24.3
110 28 my ($maxlenref, $samelenref) = @_; # reference to $maxlength, $samelen
111  
112 28 my ($ret,$type);
113
114 28 $ret = "";
115 28 ▶ if (ref($a) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $a eq 'ARRAY') { } 10 18 28
elsif (ref \$a eq 'SCALAR') { } 18 0 18
116  
117 10 ▶ PDL::Core::barf('Array is not rectangular') if (defined($dims[$level]) and
Branch: 1 branch
BranchTrueFalseTotal
if defined $PDL::Char::dims[$PDL::Char::level] and $PDL::Char::dims[$PDL::Char::level] != scalar @$a 0 10 10
Condition66%2/3
defined $PDL::Char::dims[$PDL::Char::level] and $PDL::Char::dims[$PDL::Char::level] != scalar @$a
!ll&&!rl&&r
550
MC/DC0%0/2
defined $PDL::Char::dims[$PDL::Char::level] and $PDL::Char::dims[$PDL::Char::level] != scalar @$a
defined $PDL::Char::dims[$PDL::Char::level] $PDL::Char::dims[$PDL::Char::level] != scalar @$a
Truth table66%2/3
A and B
A= defined $PDL::Char::dims[$PDL::Char::level]B= $PDL::Char::dims[$PDL::Char::level] != scalar @$a
ABresult
0X0
100
111
118 $dims[$level] != scalar(@$a));
119 10 $dims[$level] = scalar (@$a);
120 10 $level++;
121
122 10 $type = ref($$a[0]);
123 10 for(@$a) {
124 26 ▶ PDL::Core::barf('Array is not rectangular') unless $type eq ref($_); # Equal types
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq ref $_ 0 26 26
125 26 $ret .= _rcharpack($_,$maxlenref, $samelenref);
126 }
127
128 10 $level--;
129
130 }elsif (ref(\$a) eq "SCALAR") {
131 18 my $len = length($a);
132  
133 # Check for this length being different then the others:
134 18 ▶ $$samelenref = 0 if( defined($$maxlenref) && ($len != $$maxlenref) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $$maxlenref and $len != $$maxlenref 5 13 18
Condition100%3/3
defined $$maxlenref and $len != $$maxlenref
!ll&&!rl&&r
2115
MC/DC100%2/2
defined $$maxlenref and $len != $$maxlenref
defined $$maxlenref $len != $$maxlenref
Truth table100%3/3
A and B
A= defined $$maxlenrefB= $len != $$maxlenref
ABresult
0X0
100
111
135 # Save the max length:
136 18 ▶ $$maxlenref = $len if( !defined($$maxlenref) || $len > $$maxlenref); # see if this is the max length seen so far
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$maxlenref or $len > $$maxlenref 2 16 18
Condition66%2/3
not defined $$maxlenref or $len > $$maxlenref
l!l&&r!l&&!r
2016
MC/DC50%1/2
not defined $$maxlenref or $len > $$maxlenref
not defined $$maxlenref $len > $$maxlenref
Truth table66%2/3
not(A) or B
A= not defined $$maxlenrefB= $len > $$maxlenref
ABresult
1X1
011
000
137  
138 18 $dims[$level] = $len;
139 18 $ret = $a;
140
141 }else{
142 0 PDL::Core::barf("Don't know how to make a PDL object from passed argument");
143 }
144 28 return $ret;
145 }
146 #
147 #
148 # This version (_rcharpack2) is for the 2nd pass (if required) thru the N-d string array.
149 # If the 1st pass thru (_rcharpack) finds that all strings were not the same length,
150 # this routine will go thru and null-pad all strings to the max length seen.
151 # Note: For efficiency, the error checking is not repeated here, because any errors will
152 # already be detected in the 1st pass.
153 #
154 sub _rcharpack2 {
155  
156 9 ▶ my $a = shift; # Input string
Subroutine _rcharpack2: called CC=4 SCAR=13.9
157 9 my ($maxlen) = @_; # Length to pad strings to
158  
159 9 my ($ret,$type);
160
161 9 $ret = "";
162 9 ▶ if (ref($a) eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $a eq 'ARRAY') { } 3 6 9
elsif (ref \$a eq 'SCALAR') { } 6 0 6
163  
164 # Checks not needed the second time thru (removed)
165  
166 3 $dims[$level] = scalar (@$a);
167 3 $level++;
168
169 3 $type = ref($$a[0]);
170 3 for(@$a) {
171 8 $ret .= _rcharpack2($_,$maxlen);
172 }
173
174 3 $level--;
175
176 }elsif (ref(\$a) eq "SCALAR") {
177 6 my $len = length($a);
178  
179 6 $dims[$level] = $maxlen;
180 6 $ret = $a.("\00" x ($maxlen - $len));
181 }
182 9 return $ret;
183 }
184  
185  
186 #
187 #
188  
189 =head2 string
190  
191 =for ref
192  
193 Function to print a character PDL (created by 'char') in a pretty format.
194  
195 =for usage
196  
197 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
198 print $char; # 'string' bound to "", perl stringify function
199 # Prints:
200 # [
201 # ['abc' 'def' 'ghi']
202 # ['jkl' 'mno' 'pqr']
203 # ]
204  
205 # 'string' is overloaded to the "" operator, so:
206 # print $char;
207 # should have the same effect.
208  
209 =cut
210  
211 sub string {
212 34 ▶ my $self = shift;
Subroutine string: called CC=6 SCAR=17.9
Pod documented
213 34 ▶ my $level = shift || 0;
Condition100%2/2
shift() || 0
l!l
313
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
214  
215 34 ▶ my $sep = $PDL::use_commas ? "," : " ";
Branch: 1 branch
BranchTrueFalseTotal
$PDL::use_commas ? : 0 34 34
216  
217 34 ▶ if ($self->dims == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->dims == 1) { } 23 11 34
218 23 my $str = ${$self->get_dataref}; # get copy of string
219 23 $str =~ s/\00+$//g; # get rid of any null padding
220 23 return "\'". $str. "\'". $sep;
221 } else {
222 11 my @dims = reverse $self->dims;
223 11 my $ret = '';
224 11 ▶ $ret .= (" " x $level) . '[' . ((@dims == 2) ? ' ' : "\n");
Branch: 1 branch
BranchTrueFalseTotal
@dims == 2 ? : 7 4 11
225 11 ▶ for (my $i=0;$i<$dims[0];$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $dims[0]) 31 11 42
226 31 my $slicestr = ":," x (scalar(@dims)-1) . "($i)";
227 31 my $substr = $self->slice($slicestr);
228 31 $ret .= $substr->string($level+1);
229 }
230 11 $ret .= (" " x $level) . ']' . $sep . "\n";
231 11 return $ret;
232 }
233
234 }
235  
236  
237 =head2 setstr
238  
239 =for ref
240  
241 Function to set one string value in a character PDL. The input position is
242 the position of the string, not a character in the string. The first dimension
243 is assumed to be the length of the string.
244  
245 The input string will be null-padded if the string is shorter than the first
246 dimension of the PDL. It will be truncated if it is longer.
247  
248 =for usage
249  
250 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
251 $char->setstr(0,1, 'foobar');
252 print $char; # 'string' bound to "", perl stringify function
253 # Prints:
254 # [
255 # ['abc' 'def' 'ghi']
256 # ['foo' 'mno' 'pqr']
257 # ]
258 $char->setstr(2,1, 'f');
259 print $char; # 'string' bound to "", perl stringify function
260 # Prints:
261 # [
262 # ['abc' 'def' 'ghi']
263 # ['foo' 'mno' 'f'] -> note that this 'f' is stored "f\0\0"
264 # ]
265  
266 =cut
267  
268 sub setstr { # Sets a particular single value to a string.
269 3 ▶ PDL::Core::barf('Usage: setstr($pdl, $x, $y,.., $value)') if $#_<2;
Subroutine setstr: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 2 0 3 3
270 3 my $self = shift;
271 3 my $val = pop;
272  
273 3 my @dims = $self->dims;
274 3 my $n = $dims[0];
275  
276 3 ▶ for (my $i=0;$i<$n;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 9 3 12
277 9 ▶ my $chr = ($i >= length($val)) ? 0 : unpack ("C", substr ($val, $i, 1));
Branch: 1 branch
BranchTrueFalseTotal
$i >= length $val ? : 2 7 9
278 9 PDL::Core::set_c ($self, [$i, @_], $chr);
279 }
280
281 }
282  
283 =head2 atstr
284  
285 =for ref
286  
287 Function to fetch one string value from a PDL::Char type PDL, given a position within the PDL.
288 The input position of the string, not a character in the string. The length of the input
289 string is the implied first dimension.
290  
291 =for usage
292  
293 $char = PDL::Char->new( [['abc', 'def', 'ghi'], ['jkl', 'mno', 'pqr']] );
294 print $char->atstr(0,1);
295 # Prints:
296 # jkl
297  
298 =cut
299  
300 sub atstr { # Fetchs a string value from a PDL::Char
301 3 ▶ PDL::Core::barf('Usage: atstr($pdl, $x, $y,..,)') if (@_ < 2);
Subroutine atstr: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 3 3
302 3 my $self = shift;
303
304 3 my $str = ':,' . join (',', map {"($_)"} @_);
305 3 my $a = $self->slice($str);
306
307 3 my $val = ${$a->get_dataref}; # get the data
308 3 $val =~ s/\00+$//g; # get rid of any null padding
309 3 return $val;
310 }
311  
312 # yuck ;) this is a cool little accessor method
313 # rebless a slice into PDL; originally
314 # Marc's idea used in PDL::Complex
315 sub numeric {
316 0 ▶ my ($seq) = @_;
Subroutine numeric: not called CC=1 SCAR=6.9
Pod undocumented
317 0 return bless $seq->slice(''), 'PDL';
318 }
319  
320 1 1;
« blib/lib/PDL/Fit/Polynomial.pm ↑ Summary blib/lib/PDL/Filter/LinPred.pm »