blib/lib/Statistics/TopK.pm

filter: Statementstmt 100% 54 / 54 Branchbran 87.5% 7 / 8 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 90.8% 79 / 87 CC 7 SCAR 8.9
CC
7
Cov
93%
new
4.5
add
4.0
BEGIN
1.0
CRAP 2.4
↑ Summary
1 package Statistics::TopK;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.02';
9 3 $VERSION = eval $VERSION;
10  
11 3 ▶use constant _K => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use constant _COUNTS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use constant _ELEMS => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use constant _SIZE => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use constant _INDEX => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 5 ▶sub new {
Subroutine new: called CC=4 SCAR=15.0
Pod documented
18 5 my ($class, $k) = @_;
19  
20 5 ▶ croak 'expecting a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and $k =~ /^\d+$/ and $k > 0 0 5 5
Condition33%2/6
defined $k and $k =~ /^\d+$/ and $k > 0
!ll&&!rl&&r
005
defined $k and $k =~ /^\d+$/
!ll&&!rl&&r
005
MC/DC0%0/3
defined $k and $k =~ /^\d+$/ and $k > 0
defined $k $k =~ /^\d+$/ $k > 0
Truth table25%1/4
A and B and C
A= defined $kB= $k =~ /^\d+$/C= $k > 0
ABCresult
0XX0
10X0
1100
1111
21 unless defined $k and $k =~ /^\d+$/ and $k > 0;
22  
23 5 my $self = [
24 $k, # _K
25 {}, # _COUNTS
26 [], # _ELEMS
27 0, # _SIZE
28 0, # _INDEX
29 ];
30  
31 # Pre-extend the internal data structures, just in case $k is large.
32 5 keys %{ $self->[_COUNTS] } = $k;
33 5 $#{ $self->[_ELEMS] } = $k - 1;
34  
35 5 return bless $self, $class;
36 }
37  
38 1200 ▶sub add {
Subroutine add: called CC=4 SCAR=13.9
Pod documented
39 1200 my ($self, $elem) = @_;
40  
41 # Increment the element's counter if it is currently being counted.
42 1200 ▶ if (exists $self->[_COUNTS]{$elem}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[1]{$elem}) 957 243 1200
43 957 return $self->[_COUNTS]{$elem} += 1;
44 }
45  
46 # Add the element if it's not being counted and there are free slots.
47 243 ▶ if ($self->[_SIZE] < $self->[_K]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[3] < $self->[0]) 15 228 243
48 15 $self->[_ELEMS][ $self->[_SIZE]++ ] = $elem;
49 15 return $self->[_COUNTS]{$elem} = 1;
50 }
51  
52 # Decrement one of the currently counted elements.
53 228 my $index = $self->[_INDEX];
54 228 my $prev = $self->[_ELEMS][$index];
55 228 my $count = $self->[_COUNTS]{$prev} -= 1;
56  
57 # Advance the counter.
58 228 $self->[_INDEX] = ++$self->[_INDEX] % $self->[_K];
59  
60 # If the count of the decremented element reaches 0, replace it with the
61 # current element.
62 228 ▶ if (0 == $count) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $count) 103 125 228
63 103 delete $self->[_COUNTS]{$prev};
64  
65 103 $self->[_ELEMS][$index] = $elem;
66  
67 103 return $self->[_COUNTS]{$elem} = 1;
68 }
69  
70 # This element is not currently being counted.
71 125 return 0;
72 }
73  
74 4 ▶sub top {
Subroutine top: called CC=1 SCAR=0.0
Pod documented
75 4 return keys %{$_[0]->[_COUNTS]};
76 }
77  
78 1 ▶sub counts {
Subroutine counts: called CC=1 SCAR=0.0
Pod documented
79 1 return %{$_[0]->[_COUNTS]};
80 }
81  
82  
83 3 1;
84  
85 __END__
↑ Summary