blib/lib/Zuzu/Value/Set.pm

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