blib/lib/Enum/Declare/Set.pm

filter: Statementstmt 99.2% 135 / 136 Branchbran 93.7% 30 / 32 Conditioncond 58.8% 10 / 17 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 30 / 30 Podpod 0.0% 0 / 18 totaltotal 85.5% 207 / 242 CC 31 SCAR 10.7
CC
31
Cov
95%
_val_to_index
7.4
equals
4.3
_assert_same_enum
3.0
CRAP 2.9
↑ Summary blib/lib/Enum/Declare/Meta.pm »
1 package Enum::Declare::Set;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Object::Proto;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 use overload
10 11 ▶ '""' => \&_stringify,
Subroutine BEGIN: called CC=1 SCAR=0.0
11 '==' => \&equals,
12 1 ▶ '!=' => sub { !$_[0]->equals($_[1]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
13 3 ▶ 'bool' => sub { !$_[0]->is_empty },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 11 fallback => 1;
15  
16 11 object 'Enum::Declare::Set',
17 'meta:required',
18 'name:Str:default()',
19 'frozen:Bool:default(0)',
20 'bits:Str:default()',
21 'init_values:ArrayRef:default([]):arg(values)';
22  
23 45 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
24 45 my ($self) = @_;
25 45 my $count = $self->meta->count;
26 45 my $bits = '';
27 45 ▶ vec($bits, $count - 1, 1) = 0 if $count;
Branch: 1 branch
BranchTrueFalseTotal
if $count 45 0 45
28  
29 45 for my $val (@{$self->init_values}) {
30 6 my $idx = _val_to_index($self, $val);
31 6 vec($bits, $idx, 1) = 1;
32 }
33  
34 45 $self->bits($bits);
35 }
36  
37 46 ▶sub has {
Subroutine has: called CC=3 SCAR=11.0
Pod undocumented
38 46 my ($self, $val) = @_;
39 46 my $idx = eval { $self->_val_to_index($val) };
40 46 ▶ return 0 unless defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $idx 2 44 46
41 44 ▶ return vec($self->bits, $idx, 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
vec($self->bits, $idx, 1) ? : 27 17 44
42 }
43  
44 34 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod undocumented
45 34 my ($self, @vals) = @_;
46 34 $self->_assert_mutable;
47 33 my $bits = $self->bits;
48 33 for my $val (@vals) {
49 53 my $idx = $self->_val_to_index($val);
50 52 vec($bits, $idx, 1) = 1;
51 }
52 32 $self->bits($bits);
53 32 return $self;
54 }
55  
56 4 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod undocumented
57 4 my ($self, @vals) = @_;
58 4 $self->_assert_mutable;
59 3 my $bits = $self->bits;
60 3 for my $val (@vals) {
61 3 my $idx = $self->_val_to_index($val);
62 3 vec($bits, $idx, 1) = 0;
63 }
64 3 $self->bits($bits);
65 3 return $self;
66 }
67  
68 3 ▶sub toggle {
Subroutine toggle: called CC=3 SCAR=11.0
Pod undocumented
69 3 my ($self, @vals) = @_;
70 3 $self->_assert_mutable;
71 2 my $bits = $self->bits;
72 2 for my $val (@vals) {
73 2 my $idx = $self->_val_to_index($val);
74 2 ▶ vec($bits, $idx, 1) = vec($bits, $idx, 1) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
vec($bits, $idx, 1) ? : 1 1 2
75 }
76 2 $self->bits($bits);
77 2 return $self;
78 }
79  
80 1 ▶sub members {
Subroutine members: called CC=3 SCAR=11.0
Pod undocumented
81 1 my ($self) = @_;
82 1 my $names = $self->meta->names;
83 1 my $n2v = $self->meta->name2val;
84 1 my $bits = $self->bits;
85 1 my @out;
86 1 for my $i (0 .. $#$names) {
87 3 ▶ push @out, $n2v->{$names->[$i]} if vec($bits, $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $bits, $i, 1 2 1 3
88 }
89 1 return @out;
90 }
91  
92 5 ▶sub names {
Subroutine names: called CC=3 SCAR=11.0
Pod undocumented
93 5 my ($self) = @_;
94 5 my $all_names = $self->meta->names;
95 5 my $bits = $self->bits;
96 5 my @out;
97 5 for my $i (0 .. $#$all_names) {
98 15 ▶ push @out, $all_names->[$i] if vec($bits, $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $bits, $i, 1 7 8 15
99 }
100 5 return @out;
101 }
102  
103 17 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod undocumented
104 17 my ($self) = @_;
105 17 my $n = 0;
106 17 my $total = $self->meta->count;
107 17 my $bits = $self->bits;
108 17 for my $i (0 .. $total - 1) {
109 52 ▶ $n++ if vec($bits, $i, 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $bits, $i, 1 20 32 52
110 }
111 17 return $n;
112 }
113  
114 5 ▶sub is_empty {
Subroutine is_empty: called CC=2 SCAR=6.9
Pod undocumented
115 5 my ($self) = @_;
116 5 ▶ return $self->count == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->count == 0 ? : 4 1 5
117 }
118  
119 34 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
120 34 my ($self) = @_;
121 34 my $new = Enum::Declare::Set->new(
122 meta => $self->meta,
123 name => $self->name,
124 );
125 34 $new->bits($self->bits);
126 34 return $new;
127 }
128  
129 # Set algebra — return new mutable Set
130  
131 2 ▶sub union {
Subroutine union: called CC=1 SCAR=0.0
Pod undocumented
132 2 my ($self, $other) = @_;
133 2 $self->_assert_same_enum($other);
134 1 my $new = $self->clone;
135 1 $new->bits($self->bits | $other->bits);
136 1 return $new;
137 }
138  
139 1 ▶sub intersection {
Subroutine intersection: called CC=1 SCAR=0.0
Pod undocumented
140 1 my ($self, $other) = @_;
141 1 $self->_assert_same_enum($other);
142 1 my $new = $self->clone;
143 1 $new->bits($self->bits & $other->bits);
144 1 return $new;
145 }
146  
147 1 ▶sub difference {
Subroutine difference: called CC=1 SCAR=0.0
Pod undocumented
148 1 my ($self, $other) = @_;
149 1 $self->_assert_same_enum($other);
150 1 my $new = $self->clone;
151 1 $new->bits($self->bits & ~$other->bits);
152 1 return $new;
153 }
154  
155 1 ▶sub symmetric_difference {
Subroutine symmetric_difference: called CC=1 SCAR=0.0
Pod undocumented
156 1 my ($self, $other) = @_;
157 1 $self->_assert_same_enum($other);
158 1 my $new = $self->clone;
159 1 $new->bits($self->bits ^ $other->bits);
160 1 return $new;
161 }
162  
163 # Comparisons
164  
165 3 ▶sub is_subset {
Subroutine is_subset: called CC=2 SCAR=6.9
Pod undocumented
166 3 my ($self, $other) = @_;
167 3 $self->_assert_same_enum($other);
168 3 ▶ return ($self->bits & $other->bits) eq $self->bits ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($self->bits & $other->bits) eq $self->bits ? : 2 1 3
169 }
170  
171 1 ▶sub is_superset {
Subroutine is_superset: called CC=1 SCAR=0.0
Pod undocumented
172 1 my ($self, $other) = @_;
173 1 $self->_assert_same_enum($other);
174 1 return $other->is_subset($self);
175 }
176  
177 2 ▶sub is_disjoint {
Subroutine is_disjoint: called CC=2 SCAR=6.9
Pod undocumented
178 2 my ($self, $other) = @_;
179 2 $self->_assert_same_enum($other);
180 2 my $inter = $self->bits & $other->bits;
181 2 ▶ return $inter eq ("\0" x length($inter)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$inter eq "\000" x length($inter) ? : 1 1 2
182 }
183  
184 4 ▶sub equals {
Subroutine equals: called CC=4 SCAR=14.6
Pod undocumented
185 4 my ($self, $other) = @_;
186 4 ▶ return 0 unless ref($other) && $other->isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $other and $other->isa(__PACKAGE__) 0 4 4
Condition33%1/3
ref $other and $other->isa(__PACKAGE__)
!ll&&!rl&&r
004
MC/DC0%0/2
ref $other and $other->isa(__PACKAGE__)
ref $other $other->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= ref $otherB= $other->isa(__PACKAGE__)
ABresult
0X0
100
111
187 4 $self->_assert_same_enum($other);
188 4 ▶ return $self->bits eq $other->bits ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->bits eq $other->bits ? : 2 2 4
189 }
190  
191 110 ▶sub _val_to_index {
Subroutine _val_to_index: called CC=7 SCAR=20.0
192 110 my ($self, $val) = @_;
193 110 my $v2n = $self->meta->val2name;
194 croak("Invalid enum value '$val' for " . $self->meta->enum_name)
195 110 ▶ unless exists $v2n->{$val};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $v2n->{$val} 3 107 110
196 107 my $names = $self->meta->names;
197 107 for my $i (0 .. $#$names) {
198 return $i if $self->meta->name2val->{$names->[$i]} eq $val
199 || (looks_like_number($val)
200 && looks_like_number($self->meta->name2val->{$names->[$i]})
201 203 ▶ && $self->meta->name2val->{$names->[$i]} == $val);
Branch: 1 branch
BranchTrueFalseTotal
if $self->meta->name2val->{$names->[$i]} eq $val or &looks_like_number($val) and &looks_like_number($self->meta->name2val->{$names->[$i]}) and $self->meta->name2val->{$names->[$i]} == $val 107 96 203
Condition66%6/9
$self->meta->name2val->{$names->[$i]} eq $val or &looks_like_number($val) and &looks_like_number($self->meta->name2val->{$names->[$i]}) and $self->meta->name2val->{$names->[$i]} == $val
l!l&&r!l&&!r
107096
&looks_like_number($val) and &looks_like_number($self->meta->name2val->{$names->[$i]}) and $self->meta->name2val->{$names->[$i]} == $val
!ll&&!rl&&r
5910
&looks_like_number($val) and &looks_like_number($self->meta->name2val->{$names->[$i]})
!ll&&!rl&&r
5091
MC/DC25%1/4
$self->meta->name2val->{$names->[$i]} eq $val or &looks_like_number($val) and &looks_like_number($self->meta->name2val->{$names->[$i]}) and $self->meta->name2val->{$names->[$i]} == $val
$self->meta->name2val->{$names->[$i]} eq $val &looks_like_number($val) &looks_like_number($self->meta->name2val->{$names->[$i]}) $self->meta->name2val->{$names->[$i]} == $val
Truth table60%3/5
A or B and C and D
A= $self->meta->name2val->{$names->[$i]} eq $valB= &looks_like_number($val)C= &looks_like_number($self->meta->name2val->{$names->[$i]})D= $self->meta->name2val->{$names->[$i]} == $val
ABCDresult
1XXX1
01111
00XX0
010X0
01100
202 }
203 0 croak("Value '$val' not found in " . $self->meta->enum_name);
204 }
205  
206 41 ▶sub _assert_mutable {
Subroutine _assert_mutable: called CC=2 SCAR=6.9
207 41 my ($self) = @_;
208 41 ▶ croak("cannot modify a frozen set") if $self->frozen;
Branch: 1 branch
BranchTrueFalseTotal
if $self->frozen 3 38 41
209 }
210  
211 15 ▶sub _assert_same_enum {
Subroutine _assert_same_enum: called CC=3 SCAR=11.1
212 15 my ($self, $other) = @_;
213 15 ▶ croak("sets belong to different enums")
Branch: 1 branch
BranchTrueFalseTotal
unless $self->meta->enum_name eq $other->meta->enum_name and $self->meta->package eq $other->meta->package 1 14 15
Condition66%2/3
$self->meta->enum_name eq $other->meta->enum_name and $self->meta->package eq $other->meta->package
!ll&&!rl&&r
1014
MC/DC50%1/2
$self->meta->enum_name eq $other->meta->enum_name and $self->meta->package eq $other->meta->package
$self->meta->enum_name eq $other->meta->enum_name $self->meta->package eq $other->meta->package
Truth table66%2/3
A and B
A= $self->meta->enum_name eq $other->meta->enum_nameB= $self->meta->package eq $other->meta->package
ABresult
0X0
100
111
214 unless $self->meta->enum_name eq $other->meta->enum_name
215 && $self->meta->package eq $other->meta->package;
216 }
217  
218 # Stringification
219  
220 2 ▶sub _stringify {
Subroutine _stringify: called CC=2 SCAR=7.1
221 2 my ($self) = @_;
222 2 ▶ my $label = $self->name || 'Set';
Condition50%1/2
$self->name || 'Set'
l!l
20
MC/DC0%0/1
$self->name || 'Set'
$self->name
Truth table50%1/2
A
A= $self->name
Aresult
11
00
223 2 return $label . '(' . join(', ', $self->names) . ')';
224 }
225  
226 11 1;
↑ Summary blib/lib/Enum/Declare/Meta.pm »