BV.pm

filter: Statementstmt 100% 73 / 73 Branchbran 100% 12 / 12 Conditioncond 91.6% 10 / 12 (1 uncoverable) MC/DCmcdc 87.5% 5 / 8 (2 uncoverable) Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 98.4% 124 / 129 (3 uncoverable) CC 13 SCAR 9.2
CC
13
Cov
99%
similarity
5.0
new
4.0
intersection
3.0
CRAP 2.5
↑ Summary BV/Cosine.pm »
1 package Set::Similarity::BV;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 our $VERSION = '0.06';
7  
8 5 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 5 our $width = int 0.999+log(~0)/log(2);
12 5 our $chunk_size = int($width/4);
13  
14 # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetParallel
15 5 our $final_shift = (($width/8) - 1) * 8;
16 5 ▶no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 our $x01 = hex(substr('01'x16,0,$chunk_size));
18 5 our $x33 = hex(substr('33'x16,0,$chunk_size));
19 5 our $x55 = hex(substr('55'x16,0,$chunk_size));
20 5 our $x0f = hex(substr('0f'x16,0,$chunk_size));
21  
22  
23 7 ▶sub new {
Subroutine new: called CC=4 SCAR=13.9
Pod documented
24 7 my $class = shift;
25  
26 # uncoverable condition false
27 7 ▶ bless @_ ? @_ > 1 ? {@_} : {%{$_[0]}} : {}, ref $class || $class;
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 3 4 7
@_ > 1 ? : 1 2 3
Condition100%2/3
ref $class || $class
l!l&&r!l&&!r
430
MC/DC100%0/2
ref $class || $class
ref $class $class
Truth table100%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
28 }
29  
30 44 ▶sub similarity {
Subroutine similarity: called CC=5 SCAR=16.1
Pod documented
31 44 my ($self, $hex1, $hex2) = @_;
32  
33 44 ▶ return 1 if (!($hex1 || $hex2)); # both zero
Branch: 1 branch
BranchTrueFalseTotal
unless $hex1 or $hex2 8 36 44
Condition100%3/3
$hex1 or $hex2
l!l&&r!l&&!r
3248
MC/DC100%2/2
$hex1 or $hex2
$hex1 $hex2
Truth table100%3/3
A or B
A= $hex1B= $hex2
ABresult
1X1
011
000
34 36 ▶ return 0 unless ($hex1 && $hex2); # one is zero
Branch: 1 branch
BranchTrueFalseTotal
unless $hex1 and $hex2 12 24 36
Condition100%3/3
$hex1 and $hex2
!ll&&!rl&&r
4824
MC/DC100%2/2
$hex1 and $hex2
$hex1 $hex2
Truth table100%3/3
A and B
A= $hex1B= $hex2
ABresult
0X0
100
111
35  
36 #return 1 if (!(hex($hex1) || hex($hex2))); # both zero
37 #return 0 unless (hex($hex1) && hex($hex2) ); # one is zero
38  
39 5 ▶ no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 24 return $self->from_integers(
42 $self->_integers($hex1),
43 $self->_integers($hex2),
44 );
45 }
46  
47  
48 1 ▶sub from_integers { croak 'Method "from_integers" not implemented in subclass' }
Subroutine from_integers: called CC=1 SCAR=0.0
Pod documented
49  
50 30 ▶sub intersection {
Subroutine intersection: called CC=3 SCAR=11.0
Pod documented
51 30 my ($self,$v1,$v2) = @_;
52  
53 5 ▶ no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 30 my $bits = 0;
56 30 my $max1 = scalar(@{$v1}) - 1;
57 30 my $max2 = scalar(@{$v2}) - 1;
58  
59 30 ▶ for (my $i=0; ($i <= $max1) && ($i <= $max2); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $max1 and $i <= $max2) 33 30 63
Condition66%2/3
$i <= $max1 and $i <= $max2
!ll&&!rl&&r
30033
MC/DC50%1/2
$i <= $max1 and $i <= $max2
$i <= $max1 $i <= $max2
Truth table66%2/3
A and B
A= $i <= $max1B= $i <= $max2
ABresult
0X0
100
111
60 33 $bits += $self->bits([ ($v1->[$i] & $v2->[$i]) ]);
61 }
62 30 return $bits;
63 }
64  
65 62 ▶sub _integers {
Subroutine _integers: called CC=2 SCAR=6.9
66 62 my ($self,$hex_string) = @_;
67  
68 62 my @chunks = $hex_string =~ m/([0-9a-f]{1,$chunk_size})/gi;
69  
70 5 ▶ no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
71 62 my $result = [];
72 62 for my $chunk (@chunks) {
73 71 push @{$result},hex($chunk);
74 }
75 62 return $result;
76 }
77  
78 87 ▶sub bits {
Subroutine bits: called CC=2 SCAR=6.9
Pod documented
79 87 my ($self,$array_of_integers) = @_;
80  
81 5 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 5 ▶ no warnings 'portable'; # for 0xffffffffffffffff
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 87 my $bits = 0;
85  
86 87 for my $i (@{$array_of_integers}) {
87 95 my $v = $i; # don't use (and change) $i directly
88 95 $v = $v - (($v >> 1) & $x55);
89 95 $v = ($v & $x33) + (($v >> 2) & $x33);
90 # (bytesof($v) -1) * bitsofbyte = (8-1)*8 = --vvvvvvv
91 95 $v = (($v + ($v >> 4) & $x0f) * $x01) >> $final_shift;
92 95 $bits += $v;
93 }
94  
95 87 return $bits;
96 }
97  
98 10 ▶sub combined_length {
Subroutine combined_length: called CC=1 SCAR=0.0
Pod documented
99 10 my ($self,$v1,$v2) = @_;
100  
101 10 return $self->bits($v1) + $self->bits($v2);
102 }
103  
104 8 ▶sub min {
Subroutine min: called CC=2 SCAR=6.9
Pod documented
105 8 ▶ ($_[1] < $_[2])
Branch: 1 branch
BranchTrueFalseTotal
$_[1] < $_[2] ? : 1 7 8
106 ? $_[1] : $_[2];
107 }
108  
109 5 1;
110  
111 __END__
↑ Summary BV/Cosine.pm »