blib/lib/Array/AsObject.pm

filter: Statementstmt 86.7% 411 / 474 Branchbran 78.5% 157 / 200 Conditioncond 78.5% 33 / 42 MC/DCmcdc 66.6% 18 / 27 Subroutinesub 91.4% 43 / 47 Podpod 100% 41 / 41 totaltotal 84.6% 703 / 831 CC 138 SCAR 19.8
CC
138
Cov
84%
splice
15.9
as_hash
12.5
delete_at
11.3
CRAP 7.2
↑ Summary
1 package Array::AsObject;
2 # Copyright (c) 2009-2010 Sullivan Beck. All rights reserved.
3 # This program is free software; you can redistribute it and/or modify it
4 # under the same terms as Perl itself.
5  
6 ###############################################################################
7  
8 6 require 5.004;
9  
10 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 6 ▶use Sort::DataTypes qw(sort_by_method sort_valid_method);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 $VERSION = "1.02";
16  
17 ###############################################################################
18 # BASE METHODS
19 ###############################################################################
20  
21 30 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
22 30 my($class,@array) = @_;
23  
24 30 my $self = {
25 "set" => [],
26 "err" => "",
27 };
28 30 bless $self, $class;
29  
30 30 ▶ $self->list(@array) if (@array);
Branch: 1 branch
BranchTrueFalseTotal
if @array 14 16 30
31 30 return $self;
32 }
33  
34 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod documented
35 0 my($self) = @_;
36  
37 0 return $VERSION;
38 }
39  
40 246 ▶sub err {
Subroutine err: called CC=2 SCAR=6.9
Pod documented
41 246 my($self) = @_;
42  
43 246 ▶ return 1 if ($$self{"err"});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"err"} 9 237 246
44 237 return 0;
45 }
46  
47 0 ▶sub errmsg {
Subroutine errmsg: not called CC=1 SCAR=6.9
Pod documented
48 0 my($self) = @_;
49  
50 0 return $$self{"err"};
51 }
52  
53 ###############################################################################
54 # LIST EXAMINATION METHODS
55 ###############################################################################
56  
57 6 ▶sub as_hash {
Subroutine as_hash: called CC=12 SCAR=25.2
Pod documented
58 6 my($self,$full) = @_;
59 6 $$self{"err"} = "";
60  
61 6 ▶ if ($full) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full) { } 3 3 6
62  
63 3 my %count;
64 my %vals;
65 3 my %refs;
66 3 my %scal;
67 3 my $undef;
68 3 my $label = 1;
69 3 foreach my $ele (@{ $$self{"set"} }) {
70 15 ▶ if (! defined $ele) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ele) { } 0 15 15
elsif (ref $ele) { } 4 11 15
71 0 ▶ if ($undef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef) { } 0 0 0
72 0 $count{$undef}++;
73 } else {
74 0 $undef = $label++;
75 0 $vals{$undef} = undef;
76 0 $count{$undef} = 1;
77 }
78 } elsif (ref($ele)) {
79 4 my $s = scalar($ele);
80 4 my $l;
81 4 ▶ if (exists $refs{$s}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $refs{$s}) { } 1 3 4
82 1 $l = $refs{$s};
83 1 $count{$l}++;
84 } else {
85 3 $l = $label++;
86 3 $refs{$s} = $l;
87 3 $vals{$l} = $ele;
88 3 $count{$l} = 1;
89 }
90 } else {
91 11 my $l;
92 11 ▶ if (exists $scal{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $scal{$ele}) { } 4 7 11
93 4 $l = $scal{$ele};
94 4 $count{$l}++;
95 } else {
96 7 $l = $label++;
97 7 $scal{$ele} = $l;
98 7 $vals{$l} = $ele;
99 7 $count{$l} = 1;
100 }
101 }
102 }
103  
104 3 return (\%count, \%vals);
105  
106 } else {
107  
108 3 my %tmp;
109 3 foreach my $ele (@{ $$self{"set"} }) {
110 15 ▶ next if (! defined $ele || ref($ele));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $ele or ref $ele 4 11 15
Condition66%2/3
not defined $ele or ref $ele
l!l&&r!l&&!r
0411
MC/DC50%1/2
not defined $ele or ref $ele
not defined $ele ref $ele
Truth table66%2/3
not(A) or B
A= not defined $eleB= ref $ele
ABresult
1X1
011
000
111 11 ▶ if (exists $tmp{$ele}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tmp{$ele}) { } 4 7 11
112 4 $tmp{$ele}++;
113 } else {
114 7 $tmp{$ele} = 1;
115 }
116 }
117  
118 3 return %tmp;
119  
120 }
121 }
122  
123 14 ▶sub at {
Subroutine at: called CC=10 SCAR=23.3
Pod documented
124 14 my($self,@n) = @_;
125 14 $$self{"err"} = "";
126  
127 14 ▶ if (! @n) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @n) { } 0 14 14
elsif ($#n > 0 and not wantarray) { } 2 22 24
128 0 $$self{"err"} = "Index required";
129 0 return undef;
130 ▶ } elsif ($#n > 0 && ! wantarray) {
Condition100%3/3
$#n > 0 and not wantarray
!ll&&!rl&&r
1022
MC/DC100%2/2
$#n > 0 and not wantarray
$#n > 0 not wantarray
Truth table100%3/3
A and not(B)
A= $#n > 0B= not wantarray
ABresult
0X0
100
111
131 2 $$self{"err"} = "In scalar context, only a single index allowed";
132 2 return undef;
133 }
134  
135 12 my @list = @{ $$self{"set"} };
136 12 ▶ if (! @list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 0 12 12
137 0 $$self{"err"} = "Operation (at) invalid with empty list";
138 0 return undef;
139 }
140  
141 12 my(@ret);
142 12 my $len = $#list + 1;
143  
144 12 foreach my $n (@n) {
145 14 ▶ if ($n =~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ /^[+-]?\d+$/) { } 12 2 14
146 12 ▶ if ($n < -$len || $n > $len-1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < -$len or $n > $len - 1) 4 8 12
Condition100%3/3
$n < -$len or $n > $len - 1
l!l&&r!l&&!r
228
MC/DC100%2/2
$n < -$len or $n > $len - 1
$n < -$len $n > $len - 1
Truth table100%3/3
A or B
A= $n < -$lenB= $n > $len - 1
ABresult
1X1
011
000
147 4 $$self{"err"} = "Index out of range";
148 4 return undef;
149 }
150 8 CORE::push(@ret,$list[$n]);
151 } else {
152 2 $$self{"err"} = "Index must be an integer";
153 2 return undef;
154 }
155 }
156  
157 6 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 4 2 6
158 4 return @ret;
159 } else {
160 2 return $ret[0];
161 }
162 }
163  
164 36 ▶sub count {
Subroutine count: called CC=2 SCAR=7.0
Pod documented
165 36 my($self,$val) = @_;
166 36 my @idx = $self->index($val);
167 36 ▶ return undef if ($self->err());
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 36 36
168  
169 36 return $#idx + 1;
170 }
171  
172 74 ▶sub exists {
Subroutine exists: called CC=5 SCAR=16.1
Pod documented
173 74 my($self,@val) = @_;
174 74 ▶ @val = (undef) if (! @val);
Branch: 1 branch
BranchTrueFalseTotal
unless @val 1 73 74
175  
176 74 foreach my $val (@val) {
177 76 my @idx = $self->index($val);
178 76 ▶ return undef if ($self->err());
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 76 76
179 76 ▶ return 0 if (! @idx);
Branch: 1 branch
BranchTrueFalseTotal
unless @idx 25 51 76
180 }
181 49 return 1;
182 }
183  
184 1 ▶sub first {
Subroutine first: called CC=2 SCAR=7.5
Pod documented
185 1 my($self) = @_;
186 1 $$self{"err"} = "";
187  
188 1 my @list = @{ $$self{"set"} };
189 1 ▶ if (! @list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 0 1 1
190 0 $$self{"err"} = "Operation (first) invalid with empty list";
191 0 return undef;
192 }
193  
194 1 return $list[0];
195 }
196  
197 1 ▶sub last {
Subroutine last: called CC=2 SCAR=7.5
Pod documented
198 1 my($self) = @_;
199 1 $$self{"err"} = "";
200  
201 1 my @list = @{ $$self{"set"} };
202 1 ▶ if (! @list) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@list) 0 1 1
203 0 $$self{"err"} = "Operation (first) invalid with empty list";
204 0 return undef;
205 }
206  
207 1 return $list[$#list];
208 }
209  
210 185 ▶sub index {
Subroutine index: called CC=5 SCAR=16.1
Pod documented
211 185 my($self,$val) = @_;
212 185 $$self{"err"} = "";
213  
214 185 my @idx = ();
215 185 my @list = @{ $$self{"set"} };
216  
217 185 ▶ for (my $i=0; $i<=$#list; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#list) 625 185 810
218 625 my $ele = $list[$i];
219 625 ▶ CORE::push(@idx,$i) if (_eq($self,$val,$ele));
Branch: 1 branch
BranchTrueFalseTotal
if _eq($self, $val, $ele) 211 414 625
220 }
221  
222 185 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 156 29 185
elsif (@idx) { } 23 6 29
223 156 return @idx;
224 } elsif (@idx) {
225 23 return $idx[0];
226 } else {
227 6 return -1;
228 }
229 }
230  
231 33 ▶sub rindex {
Subroutine rindex: called CC=3 SCAR=11.0
Pod documented
232 33 my($self,$val) = @_;
233 33 my @idx = $self->index($val);
234  
235 33 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 25 8 33
elsif (@idx) { } 5 3 8
236 25 return CORE::reverse(@idx);
237 } elsif (@idx) {
238 5 return $idx[$#idx];
239 } else {
240 3 return -1;
241 }
242 }
243  
244 6 ▶sub is_empty {
Subroutine is_empty: called CC=5 SCAR=16.1
Pod documented
245 6 my($self,$undef) = @_;
246 6 $$self{"err"} = "";
247  
248 6 my @list = @{ $$self{"set"} };
249 6 ▶ return 1 if ($#list == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $#list == -1 2 4 6
250  
251 4 foreach my $ele (@list) {
252 5 ▶ next if ($undef && ! defined $ele);
Branch: 1 branch
BranchTrueFalseTotal
if $undef and not defined $ele 2 3 5
Condition100%3/3
$undef and not defined $ele
!ll&&!rl&&r
212
MC/DC100%2/2
$undef and not defined $ele
$undef not defined $ele
Truth table100%3/3
A and not(B)
A= $undefB= not defined $ele
ABresult
0X0
100
111
253 3 return 0;
254 }
255  
256 1 return 1;
257 }
258  
259 5 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
260 5 my($self) = @_;
261 5 $$self{"err"} = "";
262  
263 5 return $#{ $$self{"set"} } + 1;
264 }
265  
266 81 ▶sub list {
Subroutine list: called CC=2 SCAR=6.9
Pod documented
267 81 my($self,@list) = @_;
268 81 $$self{"err"} = "";
269  
270 81 ▶ if (@list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list) { } 40 41 81
271 40 $$self{"set"} = [@list];
272 40 return;
273 } else {
274 41 return @{ $$self{"set"} };
275 }
276 }
277  
278 625 ▶sub _eq {
Subroutine _eq: called CC=11 SCAR=24.0
279 625 my($self,$val1,$val2) = @_;
280  
281 625 ▶ if (! defined $val1 && ! defined $val2) {
Branch: 5 branches
BranchTrueFalseTotal
if (not defined $val1 || defined $val2) { } 20 605 625
elsif (not defined $val1 && defined $val2) { } 121 484 605
elsif (ref $val1 and ref $val2 and scalar $val1 eq scalar $val2) { } 8 476 484
elsif (ref $val1 or ref $val2) { } 28 448 476
elsif ($val1 eq $val2) { } 183 265 448
Condition100%3/3
defined $val1 || defined $val2
l!l&&r!l&&!r
5258020
MC/DC100%2/2
defined $val1 || defined $val2
defined $val1 defined $val2
Truth table100%3/3
A || B
A= defined $val1B= defined $val2
ABresult
1X1
011
000
282 20 return 1;
283 ▶ } elsif (! defined $val1 || ! defined $val2) {
Condition100%3/3
defined $val1 && defined $val2
!ll&&!rl&&r
8041484
MC/DC100%2/2
defined $val1 && defined $val2
defined $val1 defined $val2
Truth table100%3/3
A && B
A= defined $val1B= defined $val2
ABresult
0X0
100
111
284 121 return 0;
285  
286 ▶ } elsif (ref($val1) && ref($val2) && scalar($val1) eq scalar($val2)) {
Condition83%5/6
ref $val1 and ref $val2 and scalar $val1 eq scalar $val2
!ll&&!rl&&r
47608
ref $val1 and ref $val2
!ll&&!rl&&r
464128
MC/DC66%2/3
ref $val1 and ref $val2 and scalar $val1 eq scalar $val2
ref $val1 ref $val2 scalar $val1 eq scalar $val2
Truth table75%3/4
A and B and C
A= ref $val1B= ref $val2C= scalar $val1 eq scalar $val2
ABCresult
0XX0
10X0
1100
1111
287 8 return 1;
288 ▶ } elsif (ref($val1) || ref($val2)) {
Condition100%3/3
ref $val1 or ref $val2
l!l&&r!l&&!r
1216448
MC/DC100%2/2
ref $val1 or ref $val2
ref $val1 ref $val2
Truth table100%3/3
A or B
A= ref $val1B= ref $val2
ABresult
1X1
011
000
289 28 return 0;
290  
291 } elsif ($val1 eq $val2) {
292 183 return 1;
293 } else {
294 265 return 0;
295 }
296 }
297  
298 ###############################################################################
299 # SIMPLE LIST MODIFICATION METHODS
300 ###############################################################################
301  
302 3 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
303 3 my($self,$undef) = @_;
304 3 $$self{"err"} = "";
305  
306 3 ▶ if ($undef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef) { } 1 2 3
307 1 foreach my $ele (@{ $$self{"set"} }) {
308 2 $ele = undef;
309 }
310  
311 } else {
312 2 $$self{"set"} = [];
313 }
314  
315 3 return;
316 }
317  
318 1 ▶sub compact {
Subroutine compact: called CC=3 SCAR=11.0
Pod documented
319 1 my($self) = @_;
320 1 $$self{"err"} = "";
321  
322 1 my @list = ();
323 1 foreach my $ele (@{ $$self{"set"} }) {
324 5 ▶ CORE::push(@list,$ele) if (defined $ele);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ele 3 2 5
325 }
326 1 $$self{"set"} = [@list];
327 1 return;
328 }
329  
330 32 ▶sub delete {
Subroutine delete: called CC=8 SCAR=20.8
Pod documented
331 32 my($self,$all,$undef,@val) = @_;
332 32 $$self{"err"} = "";
333  
334 32 foreach my $val (@val) {
335 36 my(@idx);
336 36 ▶ if ($all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all) { } 15 21 36
337 15 @idx = $self->rindex($val);
338 15 ▶ next if (! @idx);
Branch: 1 branch
BranchTrueFalseTotal
unless @idx 3 12 15
339 } else {
340 21 my $idx = $self->index($val);
341 21 ▶ next if ($idx == -1);
Branch: 1 branch
BranchTrueFalseTotal
if $idx == -1 3 18 21
342 18 @idx = ($idx);
343 }
344  
345 30 ▶ if ($undef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef) { } 4 26 30
346 4 foreach my $idx (@idx) {
347 7 $$self{"set"}[$idx] = undef;
348 }
349 } else {
350 26 foreach my $idx (@idx) {
351 32 CORE::splice(@{ $$self{"set"} },$idx,1);
352 }
353 }
354 }
355 32 return;
356 }
357  
358 2 ▶sub delete_at {
Subroutine delete_at: called CC=9 SCAR=24.2
Pod documented
359 2 my($self,$undef,@idx) = @_;
360 2 $$self{"err"} = "";
361  
362 2 my @list = @{ $$self{"set"} };
363 2 foreach my $idx (@idx) {
364 6 ▶ if ($idx !~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($idx =~ /^[+-]?\d+$/) 0 6 6
365 0 $$self{"err"} = "Index must be an integer";
366 0 return undef;
367 }
368 6 ▶ if ($idx < -($#list + 1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < -($#list + 1) or $idx > $#list) 0 6 6
Condition33%1/3
$idx < -($#list + 1) or $idx > $#list
l!l&&r!l&&!r
006
MC/DC0%0/2
$idx < -($#list + 1) or $idx > $#list
$idx < -($#list + 1) $idx > $#list
Truth table33%1/3
A or B
A= $idx < -($#list + 1)B= $idx > $#list
ABresult
1X1
011
000
369 $idx > $#list) {
370 0 $$self{"err"} = "Index out of bounds";
371 0 return undef;
372 }
373 6 ▶ if ($idx < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < 0) 0 6 6
374 0 $idx = $#list + 1 + $idx;
375 }
376 }
377 2 @idx = sort { $b <=> $a } @idx;
378  
379 2 ▶ if ($undef) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undef) { } 1 1 2
380 1 foreach my $idx (@idx) {
381 3 $$self{"set"}[$idx] = undef;
382 }
383 } else {
384 1 foreach my $idx (@idx) {
385 3 CORE::splice(@{ $$self{"set"} },$idx,1);
386 }
387 }
388 2 return;
389 }
390  
391 7 ▶sub fill {
Subroutine fill: called CC=10 SCAR=23.5
Pod documented
392 7 my($self,$val,$start,$length) = @_;
393 7 $$self{"err"} = "";
394  
395 7 my @list = @{ $$self{"set"} };
396  
397 7 ▶ $start = 0 if (! $start);
Branch: 1 branch
BranchTrueFalseTotal
unless $start 1 6 7
398 7 ▶ if ($start !~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start =~ /^[+-]?\d+$/) 0 7 7
399 0 $$self{"err"} = "Start must be an integer";
400 0 return undef;
401 }
402 7 ▶ if ($start < -($#list + 1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($start < -($#list + 1) or $start > $#list + 1) 1 6 7
Condition66%2/3
$start < -($#list + 1) or $start > $#list + 1
l!l&&r!l&&!r
016
MC/DC50%1/2
$start < -($#list + 1) or $start > $#list + 1
$start < -($#list + 1) $start > $#list + 1
Truth table66%2/3
A or B
A= $start < -($#list + 1)B= $start > $#list + 1
ABresult
1X1
011
000
403 $start > $#list + 1) {
404 1 $$self{"err"} = "Start out of bounds";
405 1 return undef;
406 }
407 6 ▶ if ($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 1 5 6
408 1 $start = $#list + 1 + $start;
409 }
410  
411 6 ▶ if (! defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 4 2 6
412 4 ▶ if ($start > $#list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $#list) { } 1 3 4
413 1 $length = 1;
414 } else {
415 3 $length = ($#list + 1 - $start);
416 }
417 }
418  
419 6 ▶ if ($length !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($length =~ /^\d+$/) 0 6 6
420 0 $$self{"err"} = "Length must be an unsigned integer";
421 0 return undef;
422 }
423 6 my $end = $start + $length - 1;
424  
425 6 foreach my $i ($start..$end) {
426 15 $list[$i] = $val;
427 }
428  
429 6 $$self{"set"} = [@list];
430 6 return;
431 }
432  
433 2 ▶sub min {
Subroutine min: called CC=3 SCAR=11.0
Pod documented
434 2 my($self,$method,@args) = @_;
435  
436 2 ▶ if (! defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 1 1 2
437 1 $method = "numerical";
438 }
439  
440 2 my(@list) = _sort($self,$method,@args);
441 2 ▶ return undef if ($self->err());
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 2 2
442  
443 2 return $list[0];
444 }
445  
446 2 ▶sub max {
Subroutine max: called CC=3 SCAR=11.0
Pod documented
447 2 my($self,$method,@args) = @_;
448  
449 2 ▶ if (! defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 1 1 2
450 1 $method = "numerical";
451 }
452  
453 2 my(@list) = _sort($self,$method,@args);
454 2 ▶ return undef if ($self->err());
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 2 2
455  
456 2 return $list[$#list];
457 }
458  
459 1 ▶sub pop {
Subroutine pop: called CC=1 SCAR=0.0
Pod documented
460 1 my($self) = @_;
461 1 $$self{"err"} = "";
462  
463 1 my $val = CORE::pop @{ $$self{"set"} };
464 1 return $val;
465 }
466  
467 1 ▶sub shift {
Subroutine shift: called CC=1 SCAR=0.0
Pod documented
468 1 my($self) = @_;
469 1 $$self{"err"} = "";
470  
471 1 my $val = CORE::shift @{ $$self{"set"} };
472 1 return $val;
473 }
474  
475 11 ▶sub push {
Subroutine push: called CC=1 SCAR=0.0
Pod documented
476 11 my($self,@list) = @_;
477 11 $$self{"err"} = "";
478  
479 11 CORE::push(@{ $$self{"set"} },@list);
480 11 return;
481 }
482  
483 1 ▶sub unshift {
Subroutine unshift: called CC=1 SCAR=0.0
Pod documented
484 1 my($self,@list) = @_;
485 1 $$self{"err"} = "";
486  
487 1 CORE::unshift(@{ $$self{"set"} },@list);
488 1 return;
489 }
490  
491 0 ▶sub randomize {
Subroutine randomize: not called CC=1 SCAR=6.9
Pod documented
492 0 my($self) = @_;
493 0 $self->sort("random");
494 }
495  
496 1 ▶sub reverse {
Subroutine reverse: called CC=1 SCAR=0.0
Pod documented
497 1 my($self) = @_;
498 1 $$self{"err"} = "";
499  
500 1 my @list = @{ $$self{"set"} };
501 1 $$self{"set"} = [ CORE::reverse(@list) ];
502 1 return;
503 }
504  
505 5 ▶sub rotate {
Subroutine rotate: called CC=7 SCAR=19.7
Pod documented
506 5 my($self,$n) = @_;
507 5 ▶ $n = 1 if (! defined $n);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 1 4 5
508 5 $$self{"err"} = "";
509  
510 5 ▶ if ($n !~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n =~ /^[+-]?\d+$/) 0 5 5
511 0 $$self{"err"} = "Rotation number must be an integer";
512 0 return undef;
513 }
514  
515 5 my @list = @{ $$self{"set"} };
516 5 ▶ if ($n > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n > 0) { } 3 2 5
elsif ($n < 0) { } 2 0 2
517 3 ▶ for (my $i=1; $i<=$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 5 3 8
518 5 CORE::push(@list,CORE::shift(@list));
519 }
520 } elsif ($n < 0) {
521 2 $n *= -1;
522 2 ▶ for (my $i=1; $i<=$n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $n) 4 2 6
523 4 CORE::unshift(@list,CORE::pop(@list));
524 }
525 }
526  
527 5 $$self{"set"} = [@list];
528 5 return;
529 }
530  
531 2 ▶sub set {
Subroutine set: called CC=5 SCAR=20.0
Pod documented
532 2 my($self,$index,$val) = @_;
533 2 $$self{"err"} = "";
534  
535 2 ▶ if (! defined $index) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $index) 0 2 2
536 0 $$self{"err"} = "Index required";
537 0 return undef;
538 }
539  
540 2 my @list = @{ $$self{"set"} };
541  
542 2 ▶ if ($index !~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($index =~ /^[+-]?\d+$/) 0 2 2
543 0 $$self{"err"} = "Index must be an integer";
544 0 return undef;
545 }
546 2 ▶ if ($index < -($#list + 1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($index < -($#list + 1) or $index > $#list) 0 2 2
Condition33%1/3
$index < -($#list + 1) or $index > $#list
l!l&&r!l&&!r
002
MC/DC0%0/2
$index < -($#list + 1) or $index > $#list
$index < -($#list + 1) $index > $#list
Truth table33%1/3
A or B
A= $index < -($#list + 1)B= $index > $#list
ABresult
1X1
011
000
547 $index > $#list) {
548 0 $$self{"err"} = "Index out of bounds";
549 0 return undef;
550 }
551  
552 2 $$self{"set"}[$index] = $val;
553 2 return;
554 }
555  
556 2 ▶sub sort {
Subroutine sort: called CC=3 SCAR=11.0
Pod documented
557 2 my($self,$method,@args) = @_;
558  
559 2 ▶ if (! defined $method) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $method) 1 1 2
560 1 $method = "alphabetic";
561 }
562  
563 2 my(@list) = _sort($self,$method,@args);
564 2 ▶ return undef if ($self->err());
Branch: 1 branch
BranchTrueFalseTotal
if $self->err 0 2 2
565  
566 2 $$self{"set"} = [@list];
567 2 return;
568 }
569  
570 6 ▶sub _sort {
Subroutine _sort: called CC=2 SCAR=7.3
571 6 my($self,$method,@args) = @_;
572 6 $$self{"err"} = "";
573  
574 6 ▶ if (! sort_valid_method($method)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sort_valid_method($method)) 0 6 6
575 0 $$self{"err"} = "Invalid sort method";
576 0 return undef;
577 }
578  
579 6 my @list = @{ $$self{"set"} };
580 6 sort_by_method($method,\@list,@args);
581 6 return @list;
582 }
583  
584 3 ▶sub splice {
Subroutine splice: called CC=9 SCAR=27.6
Pod documented
585 3 my($self,$start,$length,@vals) = @_;
586 3 $$self{"err"} = "";
587  
588 3 my @list = @{ $$self{"set"} };
589  
590 3 ▶ $start = 0 if (! $start);
Branch: 1 branch
BranchTrueFalseTotal
unless $start 0 3 3
591 3 ▶ if ($start !~ /^[+-]?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start =~ /^[+-]?\d+$/) 0 3 3
592 0 $$self{"err"} = "Start must be an integer";
593 0 return undef;
594 }
595 3 ▶ if ($start < -($#list + 1) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($start < -($#list + 1) or $start > $#list) 0 3 3
Condition33%1/3
$start < -($#list + 1) or $start > $#list
l!l&&r!l&&!r
003
MC/DC0%0/2
$start < -($#list + 1) or $start > $#list
$start < -($#list + 1) $start > $#list
Truth table33%1/3
A or B
A= $start < -($#list + 1)B= $start > $#list
ABresult
1X1
011
000
596 $start > $#list) {
597 0 $$self{"err"} = "Start out of bounds";
598 0 return undef;
599 }
600 3 ▶ if ($start < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start < 0) 1 2 3
601 1 $start = $#list + 1 + $start;
602 }
603  
604 3 ▶ if (! defined $length) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $length) 0 3 3
605 0 ▶ if ($start > $#list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start > $#list) { } 0 0 0
606 0 $length = 1;
607 } else {
608 0 $length = ($#list + 1 - $start);
609 }
610 }
611  
612 3 ▶ if ($length !~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($length =~ /^\d+$/) 0 3 3
613 0 $$self{"err"} = "Length must be an unsigned integer";
614 0 return undef;
615 }
616 3 my $end = $start + $length - 1;
617  
618 3 my @ret = CORE::splice(@list,$start,$length,@vals);
619  
620 3 $$self{"set"} = [@list];
621 3 return @ret;
622 }
623  
624 2 ▶sub unique {
Subroutine unique: called CC=5 SCAR=17.2
Pod documented
625 2 my($self) = @_;
626 2 $$self{"err"} = "";
627  
628 2 my @list = ();
629 2 my %list = ();
630 2 my $undef = 0;
631  
632 2 foreach my $ele (@{ $$self{"set"} }) {
633 12 ▶ if (! defined($ele)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $ele) { } 0 12 12
elsif (not exists $list{$ele}) { } 7 5 12
634 0 ▶ if (! $undef) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($undef) 0 0 0
635 0 CORE::push(@list,$ele);
636 0 $undef = 1;
637 }
638 } elsif (! CORE::exists $list{$ele}) {
639 7 CORE::push(@list,$ele);
640 7 $list{$ele} = 1;
641 }
642 }
643 2 $$self{"set"} = [@list];
644 2 return;
645 }
646  
647 ###############################################################################
648 # SET METHODS
649 ###############################################################################
650  
651 2 ▶sub difference {
Subroutine difference: called CC=4 SCAR=14.0
Pod documented
652 2 my($obj1,$obj2,$unique) = @_;
653  
654 2 my @list = @{ $$obj1{"set"} };
655 2 my $class = ref($obj1);
656 2 my $ret = new $class;
657  
658 2 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 2 2
659 0 $$ret{"err"} = "Obj2 not of the right class";
660 0 return $ret;
661 }
662  
663 # $ret starts as identical to $obj1
664 # remove every element in $obj2 from $ret
665  
666 2 $ret->list(@list);
667 2 ▶ my $all = ($unique ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 1 1 2
668 2 foreach my $ele (@{ $$obj2{"set"} }) {
669 10 $ret->delete($all,0,$ele);
670 }
671  
672 2 return $ret;
673 }
674  
675 2 ▶sub intersection {
Subroutine intersection: called CC=5 SCAR=16.2
Pod documented
676 2 my($obj1,$obj2,$unique) = @_;
677  
678 2 my $class = ref($obj1);
679 2 my $ret = new $class;
680  
681 2 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 2 2
682 0 $$ret{"err"} = "Obj2 not of the right class";
683 0 return $ret;
684 }
685  
686 # $tmp is identical to $obj2
687 # foreach element in $obj1
688 # if it's in $tmp
689 # add it to $ret
690 # remove it from $tmp
691  
692 2 my $tmp = new $class;
693 2 $tmp->list(@{ $$obj2{"set"} });
694 2 ▶ my $all = ($unique ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 1 1 2
695  
696 2 my @list = @{ $$obj1{"set"} };
697 2 foreach my $ele (@list) {
698 8 ▶ if ($tmp->exists($ele)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp->exists($ele)) 5 3 8
699 5 $ret->push($ele);
700 5 $tmp->delete($all,0,$ele);
701 }
702 }
703  
704 2 return $ret;
705 }
706  
707 8 ▶sub is_equal {
Subroutine is_equal: called CC=9 SCAR=22.3
Pod documented
708 8 my($obj1,$obj2,$unique) = @_;
709  
710 8 my $class = ref($obj1);
711  
712 8 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 8 8
713 0 return undef;
714 }
715  
716 8 my @list1 = @{ $$obj1{"set"} };
717 8 my @list2 = @{ $$obj2{"set"} };
718  
719 8 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 4 4 8
720 4 foreach my $ele (@list1) {
721 10 ▶ return 0 if (! $obj2->exists($ele));
Branch: 1 branch
BranchTrueFalseTotal
unless $obj2->exists($ele) 0 10 10
722 }
723 4 foreach my $ele (@list2) {
724 8 ▶ return 0 if (! $obj1->exists($ele));
Branch: 1 branch
BranchTrueFalseTotal
unless $obj1->exists($ele) 0 8 8
725 }
726 4 return 1;
727 }
728  
729 4 foreach my $ele (@list1,@list2) {
730 10 ▶ return 0 if ($obj1->count($ele) != $obj2->count($ele));
Branch: 1 branch
BranchTrueFalseTotal
if $obj1->count($ele) != $obj2->count($ele) 2 8 10
731 }
732 2 return 1;
733 }
734  
735 4 ▶sub not_equal {
Subroutine not_equal: called CC=1 SCAR=0.0
Pod documented
736 4 return 1 - is_equal(@_);
737 }
738  
739 5 ▶sub is_subset {
Subroutine is_subset: called CC=7 SCAR=19.7
Pod documented
740 5 my($obj1,$obj2,$unique) = @_;
741  
742 5 my $class = ref($obj1);
743  
744 5 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 5 5
745 0 return undef;
746 }
747  
748 5 my @list = @{ $$obj2{"set"} };
749  
750 5 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 2 3 5
751 2 foreach my $ele (@list) {
752 5 ▶ return 0 if (! $obj1->exists($ele));
Branch: 1 branch
BranchTrueFalseTotal
unless $obj1->exists($ele) 0 5 5
753 }
754 2 return 1;
755 }
756  
757 3 foreach my $ele (@list) {
758 6 ▶ return 0 if ($obj2->count($ele) > $obj1->count($ele));
Branch: 1 branch
BranchTrueFalseTotal
if $obj2->count($ele) > $obj1->count($ele) 1 5 6
759 }
760 2 return 1;
761 }
762  
763 0 ▶sub not_subset {
Subroutine not_subset: not called CC=1 SCAR=6.9
Pod documented
764 0 return 1 - is_subset(@_);
765 }
766  
767 2 ▶sub symmetric_difference {
Subroutine symmetric_difference: called CC=8 SCAR=21.1
Pod documented
768 2 my($obj1,$obj2,$unique) = @_;
769  
770 2 my $class = ref($obj1);
771 2 my $ret = new $class;
772  
773 2 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 2 2
774 0 $$ret{"err"} = "Obj2 not of the right class";
775 0 return $ret;
776 }
777  
778 2 my $tmp1 = new $class;
779 2 my @list1 = @{ $$obj1{"set"} };
780 2 $tmp1->list(@list1);
781  
782 2 my $tmp2 = new $class;
783 2 my @list2 = @{ $$obj2{"set"} };
784 2 $tmp2->list(@list2);
785  
786 2 ▶ my $all = ($unique ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$unique ? : 1 1 2
787  
788 2 foreach my $ele (@list1,@list2) {
789 14 ▶ if ($tmp1->exists($ele) && $tmp2->exists($ele)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($tmp1->exists($ele) and $tmp2->exists($ele)) { } 4 10 14
elsif ($tmp1->exists($ele)) { } 5 5 10
elsif ($tmp2->exists($ele)) { } 0 5 5
Condition100%3/3
$tmp1->exists($ele) and $tmp2->exists($ele)
!ll&&!rl&&r
554
MC/DC100%2/2
$tmp1->exists($ele) and $tmp2->exists($ele)
$tmp1->exists($ele) $tmp2->exists($ele)
Truth table100%3/3
A and B
A= $tmp1->exists($ele)B= $tmp2->exists($ele)
ABresult
0X0
100
111
790 4 $tmp1->delete($all,0,$ele);
791 4 $tmp2->delete($all,0,$ele);
792 } elsif ($tmp1->exists($ele)) {
793 5 $ret->push($ele);
794 5 $tmp1->delete(0,0,$ele);
795 } elsif ($tmp2->exists($ele)) {
796 0 $ret->push($ele);
797 0 $tmp2->delete(0,0,$ele);
798 }
799 }
800  
801 2 return $ret;
802 }
803  
804 2 ▶sub union {
Subroutine union: called CC=3 SCAR=11.1
Pod documented
805 2 my($obj1,$obj2,$unique) = @_;
806  
807 2 my $class = ref($obj1);
808 2 my $ret = new $class;
809  
810 2 ▶ if (ref($obj2) ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj2 ne $class) 0 2 2
811 0 $$ret{"err"} = "Obj2 not of the right class";
812 0 return $ret;
813 }
814  
815 2 my @list1 = @{ $$obj1{"set"} };
816 2 my @list2 = @{ $$obj2{"set"} };
817  
818 2 $ret->list(@list1,@list2);
819 2 ▶ if ($unique) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unique) 1 1 2
820 1 $ret->unique();
821 }
822  
823 2 return $ret;
824 }
825  
826 6 1;
827 # Local Variables:
828 # mode: cperl
829 # indent-tabs-mode: nil
830 # cperl-indent-level: 3
831 # cperl-continued-statement-offset: 2
832 # cperl-continued-brace-offset: 0
833 # cperl-brace-offset: 0
834 # cperl-brace-imaginary-offset: 0
835 # cperl-label-offset: -2
836 # End:
↑ Summary