blib/lib/Zuzu/Value/Bag.pm

filter: Statementstmt 62.9% 114 / 181 Branchbran 52.0% 25 / 48 Conditioncond 27.2% 3 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 59.5% 25 / 42 Podpod 53.3% 16 / 30 totaltotal 58.1% 185 / 318 CC 38 SCAR 17.1
CC
38
Cov
59%
remove_if
20.0
any
12.0
all
12.0
CRAP 5.5
« blib/lib/Zuzu/Test/ZPathFacelessPortDiagnostics.pm ↑ Summary blib/lib/Zuzu/Value/Set.pm »
1 package Zuzu::Value::Bag;
2  
3 70 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 70 our $VERSION = '0.007002';
6  
7 70 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 70 ▶use Zuzu::Value::Array;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 70 ▶use Zuzu::Value::Set;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 70 ▶use Zuzu::Value::Equality qw( value_equal );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 70 ▶use Zuzu::Weak qw( slot_value store_value );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 70 ▶use List::Util qw( sum0 );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 0 ▶has 'items' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 32 ▶has 'weak' => ( is => 'rw', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 15 ▶sub _store_at {
Subroutine _store_at: called CC=2 SCAR=6.9
19 15 my ( $self, $index, $value, $weak ) = @_;
20  
21 15 ▶ $self->weak->[$index] = $weak ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$weak ? : 6 9 15
22 15 store_value( \$self->items->[$index], $value, $weak );
23  
24 15 return $value;
25 }
26  
27 58 ▶sub _value_at {
Subroutine _value_at: called CC=1 SCAR=0.0
28 58 my ( $self, $index ) = @_;
29  
30 58 return slot_value( \$self->items->[$index] );
31 }
32  
33 21 ▶sub resolved_items {
Subroutine resolved_items: called CC=2 SCAR=6.9
Pod undocumented
34 21 my ( $self ) = @_;
35  
36 21 my @out;
37 21 ▶ for ( my $i = 0; $i < @{ $self->items }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->items;}) 38 21 59
38 38 push @out, $self->_value_at($i);
39 }
40  
41 21 return @out;
42 }
43  
44 5 ▶sub copy {
Subroutine copy: called CC=3 SCAR=11.0
Pod undocumented
45 5 my ( $self ) = @_;
46 5 my $copy = Zuzu::Value::Bag->new( items => [] );
47  
48 5 ▶ for ( my $i = 0; $i < @{ $self->items }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->items;}) 8 5 13
49 8 ▶ $copy->_store_at(
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 1 7 8
50 $i,
51 $self->_value_at($i),
52 $self->weak->[$i] ? 1 : 0,
53 );
54 }
55  
56 5 return $copy;
57 }
58  
59 2 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
60 2 my ( $self, @vals ) = @_;
61  
62 2 for my $value ( @vals ) {
63 2 my $index = scalar @{ $self->items };
64 2 $self->_store_at( $index, $value, 0 );
65 }
66  
67 2 return $self;
68 }
69  
70 4 ▶sub add_weak {
Subroutine add_weak: called CC=2 SCAR=6.9
Pod undocumented
71 4 my ( $self, @vals ) = @_;
72  
73 4 for my $value ( @vals ) {
74 4 my $index = scalar @{ $self->items };
75 4 $self->_store_at( $index, $value, 1 );
76 }
77  
78 4 return $self;
79 }
80  
81 0 ▶sub push_weak {
Subroutine push_weak: not called CC=1 SCAR=6.9
Pod undocumented
82 0 my ( $self, @vals ) = @_;
83  
84 0 return $self->add_weak(@vals);
85 }
86  
87 3 ▶sub remove {
Subroutine remove: called CC=4 SCAR=13.9
Pod documented
88 3 my ( $self, $needle ) = @_;
89 3 my @out;
90 my @weak;
91 3 ▶ for ( my $i = 0; $i < @{ $self->items }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->items;}) 8 3 11
92 8 my $v = $self->_value_at($i);
93 8 ▶ next if value_equal( $v, $needle );
Branch: 1 branch
BranchTrueFalseTotal
if value_equal($v, $needle) 3 5 8
94 5 push @out, undef;
95 5 ▶ push @weak, $self->weak->[$i] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 1 4 5
96 5 store_value( \$out[-1], $v, $weak[-1] );
97 }
98 3 $self->items( \@out );
99 3 $self->weak( \@weak );
100  
101 3 return $self;
102 }
103  
104 1 ▶sub remove_first {
Subroutine remove_first: called CC=5 SCAR=16.1
Pod documented
105 1 my ( $self, $needle ) = @_;
106 1 my @out;
107 my @weak;
108 1 my $removed = 0;
109 1 ▶ for ( my $i = 0; $i < @{ $self->items }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->items;}) 3 1 4
110 3 my $v = $self->_value_at($i);
111 3 ▶ if ( ! $removed and value_equal( $v, $needle ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $removed and value_equal($v, $needle)) 1 2 3
Condition100%3/3
not $removed and value_equal($v, $needle)
!ll&&!rl&&r
111
MC/DC100%2/2
not $removed and value_equal($v, $needle)
not $removed value_equal($v, $needle)
Truth table100%3/3
not(A) and B
A= not $removedB= value_equal($v, $needle)
ABresult
0X0
100
111
112 1 $removed = 1;
113 1 next;
114 }
115 2 push @out, undef;
116 2 ▶ push @weak, $self->weak->[$i] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 0 2 2
117 2 store_value( \$out[-1], $v, $weak[-1] );
118 }
119 1 $self->items( \@out );
120 1 $self->weak( \@weak );
121  
122 1 return $self;
123 }
124  
125  
126 12 ▶sub contains {
Subroutine contains: called CC=3 SCAR=11.1
Pod documented
127 12 my ( $self, $needle ) = @_;
128  
129 12 for my $item ( $self->resolved_items ) {
130 16 ▶ return 1 if value_equal( $item, $needle );
Branch: 1 branch
BranchTrueFalseTotal
if value_equal($item, $needle) 12 4 16
131 }
132  
133 0 return 0;
134 }
135  
136 1 ▶sub to_Array {
Subroutine to_Array: called CC=1 SCAR=0.0
Pod documented
137 1 my ( $self ) = @_;
138  
139 1 return Zuzu::Value::Array->new( items => [ $self->resolved_items ] );
140 }
141  
142 1 ▶sub to_Set {
Subroutine to_Set: called CC=1 SCAR=0.0
Pod documented
143 1 my ( $self ) = @_;
144 1 my $set = Zuzu::Value::Set->new( items => [] );
145 1 $set->add( $self->resolved_items );
146  
147 1 return $set;
148 }
149  
150 0 ▶sub to_Iterator {
Subroutine to_Iterator: not called CC=1 SCAR=6.9
Pod undocumented
151 0 my ( $self ) = @_;
152  
153 0 my @items = $self->resolved_items;
154 0 my $index = 0;
155  
156 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
157 0 ▶ die 'ExhaustedException' if $index >= @items;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= @items 0 0 0
158 0 my $value = $items[$index];
159 0 $index++;
160  
161 0 return $value;
162 0 };
163 }
164  
165 0 ▶sub map {
Subroutine map: not called CC=1 SCAR=6.9
Pod documented
166 0 my ( $self, $mapper ) = @_;
167 0 my @out = map { $mapper->($_) } $self->resolved_items;
168  
169 0 return Zuzu::Value::Bag->new( items => \@out );
170 }
171  
172 0 ▶sub grep {
Subroutine grep: not called CC=1 SCAR=6.9
Pod documented
173 0 my ( $self, $pred ) = @_;
174 0 my @out = grep { $pred->($_) } $self->resolved_items;
175  
176 0 return Zuzu::Value::Bag->new( items => \@out );
177 }
178  
179 0 ▶sub any {
Subroutine any: not called CC=3 SCAR=24.8
Pod documented
180 0 my ( $self, $pred ) = @_;
181 0 for my $v ( $self->resolved_items ) {
182 0 ▶ return 1 if $pred->($v);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($v) 0 0 0
183 }
184  
185 0 return 0;
186 }
187  
188 0 ▶sub all {
Subroutine all: not called CC=3 SCAR=24.8
Pod documented
189 0 my ( $self, $pred ) = @_;
190 0 for my $v ( $self->resolved_items ) {
191 0 ▶ return 0 if !$pred->($v);
Branch: 1 branch
BranchTrueFalseTotal
unless &$pred($v) 0 0 0
192 }
193  
194 0 return 1;
195 }
196  
197 0 ▶sub first {
Subroutine first: not called CC=3 SCAR=24.8
Pod documented
198 0 my ( $self, $pred ) = @_;
199 0 for my $v ( $self->resolved_items ) {
200 0 ▶ return $v if $pred->($v);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($v) 0 0 0
201 }
202  
203 0 return undef;
204 }
205  
206 0 ▶sub remove_if {
Subroutine remove_if: not called CC=4 SCAR=30.0
Pod documented
207 0 my ( $self, $pred ) = @_;
208 0 my @out;
209 my @weak;
210 0 ▶ for ( my $i = 0; $i < @{ $self->items }; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->items;}) 0 0 0
211 0 my $value = $self->_value_at($i);
212 0 ▶ next if $pred->($value);
Branch: 1 branch
BranchTrueFalseTotal
if &$pred($value) 0 0 0
213 0 push @out, undef;
214 0 ▶ push @weak, $self->weak->[$i] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->weak->[$i] ? : 0 0 0
215 0 store_value( \$out[-1], $value, $weak[-1] );
216 }
217 0 $self->items( \@out );
218 0 $self->weak( \@weak );
219  
220 0 return $self;
221 }
222  
223 2 ▶sub count {
Subroutine count: called CC=4 SCAR=14.1
Pod undocumented
224 2 my ( $self, $needle ) = @_;
225  
226 0 ▶ return scalar @{ $self->items } if @_ < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 2 0 2 2
227  
228 2 my $count = 0;
229 2 for my $v ( $self->resolved_items ) {
230 6 ▶ $count++ if value_equal( $v, $needle );
Branch: 1 branch
BranchTrueFalseTotal
if value_equal($v, $needle) 4 2 6
231 }
232  
233 2 return $count;
234 }
235  
236 1 ▶sub uniq {
Subroutine uniq: called CC=1 SCAR=0.0
Pod undocumented
237 1 my ( $self ) = @_;
238 1 my $set = Zuzu::Value::Set->new( items => [] );
239 1 $set->add( $self->resolved_items );
240  
241 1 return $set;
242 }
243  
244 0 ▶sub sum {
Subroutine sum: not called CC=2 SCAR=17.9
Pod undocumented
245 0 my ( $self ) = @_;
246 0 ▶ my @nums = map { 0 + ( $_ // 0 ) } $self->resolved_items;
Condition0%0/2
$_ // 0
l!l
00
MC/DC0%0/1
$_ // 0
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
247  
248 0 return sum0 @nums;
249 }
250  
251 0 ▶sub product {
Subroutine product: not called CC=2 SCAR=17.9
Pod undocumented
252 0 my ( $self ) = @_;
253 0 ▶ my @nums = map { 0 + ( $_ // 0 ) } $self->resolved_items;
Condition0%0/2
$_ // 0
l!l
00
MC/DC0%0/1
$_ // 0
$_
Truth table0%0/2
A
A= $_
Aresult
11
00
254  
255 0 return List::Util::product @nums;
256 }
257  
258 1 ▶sub for_each_value {
Subroutine for_each_value: called CC=2 SCAR=6.9
Pod undocumented
259 1 my ( $self, $callback ) = @_;
260  
261 1 for my $v ( $self->resolved_items ) {
262 2 $callback->($v);
263 }
264  
265 1 return $self;
266 }
267  
268 1 ▶sub sort {
Subroutine sort: called CC=1 SCAR=0.0
Pod undocumented
269 1 my ( $self, $cmp ) = @_;
270 1 my @out = CORE::sort { $cmp->( $a, $b ) } $self->resolved_items;
271  
272 1 return Zuzu::Value::Array->new( items => \@out );
273 }
274  
275 0 ▶sub sortstr {
Subroutine sortstr: not called CC=3 SCAR=24.8
Pod undocumented
276 0 my ( $self ) = @_;
277 my @out = CORE::sort {
278 0 ▶ ( defined $a ? "$a" : '' ) cmp ( defined $b ? "$b" : '' )
Branch: 2 branches
BranchTrueFalseTotal
defined $a ? : 0 0 0
defined $b ? : 0 0 0
279 } $self->resolved_items;
280  
281 0 return Zuzu::Value::Array->new( items => \@out );
282 }
283  
284 0 ▶sub sortnum {
Subroutine sortnum: not called CC=3 SCAR=24.8
Pod undocumented
285 0 my ( $self ) = @_;
286 my @out = CORE::sort {
287 0 ▶ ( 0 + ( $a // 0 ) ) <=> ( 0 + ( $b // 0 ) )
Condition0%0/4
$a // 0
l!l
00
$b // 0
l!l
00
MC/DC0%0/1
$a // 0
$a
MC/DC0%0/1
$b // 0
$b
Truth table0%0/2
A
A= $a
Aresult
11
00
Truth table0%0/2
A
A= $b
Aresult
11
00
288 } $self->resolved_items;
289  
290 0 return Zuzu::Value::Array->new( items => \@out );
291 }
292  
293 10 ▶sub length { scalar @{ $_[0]->items } }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
294  
295 0 ▶sub empty { @{ $_[0]->items } ? 0 : 1 }
Subroutine empty: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]->items;} ? : 0 0 0
296  
297 0 ▶sub is_empty { $_[0]->empty }
Subroutine is_empty: not called CC=1 SCAR=6.9
Pod undocumented
298  
299 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
300 0 $_[0]->items( [] );
301 0 $_[0]->weak( [] );
302  
303 0 return $_[0];
304 }
305  
306 1 ▶sub is_truthy { @{$_[0]->items} ? 1 : 0 }
Subroutine is_truthy: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0]->items;} ? : 0 1 1
307  
308 =pod
309  
310 =head1 NAME
311  
312 Zuzu::Value::Bag - runtime value class for bag values
313  
314 =head1 DESCRIPTION
315  
316 Wraps runtime bag values that keep duplicates without ordering guarantees.
317  
318 =head1 INHERITANCE
319  
320 Inherits from C<Moo::Object>.
321  
322 =head1 ROLES
323  
324 None.
325  
326 =head1 ATTRIBUTES
327  
328 =head2 items
329  
330 Type: B<ArrayRef>.
331  
332 Bag members, including duplicate entries.
333  
334 =head1 METHODS
335  
336 =head2 add
337  
338 Adds one or more members.
339  
340 =head2 remove
341  
342 Removes a single matching member, if present.
343  
344 =head2 remove_first
345  
346 Alias for C<remove>.
347  
348 =head2 contains
349  
350 Returns true when the bag contains a matching value.
351  
352 =head2 to_Array
353  
354 Returns this bag as an array value.
355  
356 =head2 to_Set
357  
358 Returns this bag as a set value.
359  
360 =head2 map
361  
362 Returns a new bag by mapping each member through a callback.
363  
364 =head2 grep
365  
366 Returns a new bag containing members where callback is true.
367  
368 =head2 any
369  
370 Returns true if any member satisfies the callback.
371  
372 =head2 all
373  
374 Returns true if all members satisfy the callback.
375  
376 =head2 first
377  
378 Returns the first matching member or undef when none match.
379  
380 =head2 remove_if
381  
382 Removes members where callback returns true.
383  
384 =head2 length
385  
386 Returns the current member count.
387  
388 =head2 empty
389  
390 Returns true when the bag has no members.
391  
392 =head2 clear
393  
394 Removes all members from the bag.
395  
396 =head2 is_truthy
397  
398 Returns this runtime value's truthiness in ZuzuScript.
399  
400 =head1 COPYRIGHT AND LICENCE
401  
402 B<< Zuzu::Value::Bag >> is copyright Toby Inkster.
403  
404 It is free software; you may redistribute it and/or modify it under
405 the terms of either the Artistic License 1.0 or the GNU General Public
406 License version 2.
407  
408 =cut
409  
410 70 1;
« blib/lib/Zuzu/Test/ZPathFacelessPortDiagnostics.pm ↑ Summary blib/lib/Zuzu/Value/Set.pm »