blib/lib/List/Sliding/Changes.pm

filter: Statementstmt 100% 44 / 44 Branchbran 75.0% 12 / 16 Conditioncond 66.6% 6 / 9 MC/DCmcdc 40.0% 2 / 5 Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 86.0% 74 / 86 CC 12 SCAR 17.5
CC
12
Cov
90%
find_new_indices
10.3
find_new_elements
3.1
__ANON__
1.1
CRAP 5.7
↑ Summary
1 package List::Sliding::Changes;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw ($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 $VERSION = 0.03;
9 3 @ISA = qw (Exporter);
10  
11 3 @EXPORT_OK = qw ( &find_new_indices &find_new_elements );
12  
13 =head1 NAME
14  
15 List::Sliding::Changes - Extract new elements from a sliding window
16  
17 =head1 SYNOPSIS
18  
19 =for example begin
20  
21 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶ use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use List::Sliding::Changes qw(find_new_elements);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 my $filename = 'log.txt';
26 my @log;
27 tie @log, 'Tie::File', $filename
28 or die "Couldn't tie to $filename : $!\n";
29  
30 # See what has happened since we last polled
31 my @status = get_last_20_status_messages();
32  
33 # Find out what we did not already communicate
34 my (@new) = find_new_elements(\@log,\@status);
35 print "New log messages : $_\n"
36 for (@new);
37  
38 # And update our log with what we have seen
39 push @log, @new;
40  
41 =for example end
42  
43 =head1 DESCRIPTION
44  
45 This module allows you to easily find elements that were appended
46 to one of two lists. It is intended to faciliate processing wherever
47 you don't have a log but only a sliding window for events, such as
48 a status window which only displays the 20 most recent events,
49 without timestamp.
50  
51 The module assumes that the update frequency is high and will always
52 respect the longest overlap between the two sequences. To be a bit
53 faster with long lists, it searches the first list from the end,
54 assuming that the first list will be much longer than the second list.
55  
56 =head1 PUBLIC METHODS
57  
58 find_new_indices( \@OLDLIST, \@NEWLIST [, EQUALITY] )
59  
60 Returns the list of indices that were added since the last time @OLDLIST
61 was updated. This is convenient if you want to modify @NEWLIST afterwards.
62 The function accepts an optional third parameter, which should be a
63 reference to a function that takes two list elements and compares them
64 for equality.
65  
66 find_new_elements( \@OLDLIST, \@NEWLIST [, EQUALITY] )
67  
68 Returns the list of the elements that were added since the last time @OLDLIST
69 was updated.
70  
71 =cut
72  
73 32 ▶sub find_new_indices {
Subroutine find_new_indices: called CC=10 SCAR=23.3
Pod undocumented
74 32 my ($old,$new,$equal) = @_;
75 32 ▶ croak "First parameter to find_new_elements() must be a reference, not $old" unless ref $old;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $old 0 32 32
76 32 ▶ croak "Second parameter to find_new_elements() must be a reference, not $new" unless ref $new;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new 0 32 32
77 32 ▶ $equal ||= sub { $_[0] eq $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$equal ||= sub { $_[0] eq $_[1]; }
l!l&&r!l&&!r
0320
MC/DC0%0/2
$equal ||= sub { $_[0] eq $_[1]; }
$equal sub { $_[0] eq $_[1]; }
Truth table33%1/3
A ||= B
A= $equalB= sub { $_[0] eq $_[1]; }
ABresult
1X1
011
000
78  
79 32 my ($new_offset,$old_offset) = (0,scalar @$old - scalar @$new);
80 32 ▶ $old_offset = 0 if $old_offset < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $old_offset < 0 22 10 32
81  
82 32 ▶ while ($old_offset < scalar @$old) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_offset < scalar @$old) 40 4 44
83 40 $new_offset = 0;
84 40 ▶ while (($old_offset+$new_offset < scalar @$old)
Branch: 1 branch
BranchTrueFalseTotal
if ($old_offset + $new_offset < scalar @$old and $new_offset < scalar @$new and &$equal($$old[$old_offset + $new_offset], $new->[$new_offset])) 68 40 108
Condition83%5/6
$old_offset + $new_offset < scalar @$old and $new_offset < scalar @$new and &$equal($$old[$old_offset + $new_offset], $new->[$new_offset])
!ll&&!rl&&r
281268
$old_offset + $new_offset < scalar @$old and $new_offset < scalar @$new
!ll&&!rl&&r
28080
MC/DC66%2/3
$old_offset + $new_offset < scalar @$old and $new_offset < scalar @$new and &$equal($$old[$old_offset + $new_offset], $new->[$new_offset])
$old_offset + $new_offset < scalar @$old $new_offset < scalar @$new &$equal($$old[$old_offset + $new_offset], $new->[$new_offset])
Truth table75%3/4
A and B and C
A= $old_offset + $new_offset < scalar @$oldB= $new_offset < scalar @$newC= &$equal($$old[$old_offset + $new_offset], $new->[$new_offset])
ABCresult
0XX0
10X0
1100
1111
85 and ($new_offset < scalar @$new)
86 and ($equal->($old->[$old_offset+$new_offset],$new->[$new_offset]))) {
87 68 $new_offset++;
88 };
89 40 ▶ last if ($old_offset+$new_offset == scalar @$old);
Branch: 1 branch
BranchTrueFalseTotal
if $old_offset + $new_offset == scalar @$old 28 12 40
90 12 $old_offset++;
91 };
92  
93 32 ($new_offset .. $#$new)
94 };
95  
96 20 ▶sub find_new_elements {
Subroutine find_new_elements: called CC=3 SCAR=11.3
Pod undocumented
97 20 my ($old,$new,$equal) = @_;
98 20 ▶ croak "First parameter to find_new_elements() must be a reference, not $old" unless ref $old;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $old 0 20 20
99 20 ▶ croak "Second parameter to find_new_elements() must be a reference, not $new" unless ref $new;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new 0 20 20
100  
101 20 (@{$new}[ find_new_indices($old,$new,$equal) ])
102 };
103  
104 3 1;
105 __END__
↑ Summary