| 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
Condition100%2/3
ref $class || $class
MC/DC100%0/2
ref $class || $class
ref $class
$class
Truth table100%2/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$hex1 or $hex2
MC/DC100%2/2
$hex1 or $hex2
$hex1
$hex2
Truth table100%3/3
A or B
A= $hex1B= $hex2
| |||||||||||||||||||||||||||||||||
| 34 | 36 | ▶ | return 0 unless ($hex1 && $hex2); # one is zero | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$hex1 and $hex2
MC/DC100%2/2
$hex1 and $hex2
$hex1
$hex2
Truth table100%3/3
A and B
A= $hex1B= $hex2
| |||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$i <= $max1 and $i <= $max2
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
| |||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||
| 106 | ? $_[1] : $_[2]; | ||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 5 | 1; | |||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||