File Coverage

blib/lib/Array/OverlapFinder.pm
Criterion Covered Total %
statement 43 43 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 7 7 100.0
pod 2 2 100.0
total 65 65 100.0


line stmt bran cond sub pod time code
1             package Array::OverlapFinder;
2              
3             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
4             our $DATE = '2020-12-25'; # DATE
5             our $DIST = 'Array-OverlapFinder'; # DIST
6             our $VERSION = '0.002'; # VERSION
7              
8 1     1   71888 use 5.010001;
  1         13  
9 1     1   7 use strict;
  1         2  
  1         32  
10 1     1   9 use warnings;
  1         1  
  1         29  
11              
12 1     1   5 use Exporter qw(import);
  1         2  
  1         405  
13             our @EXPORT_OK = qw(find_overlap combine_overlap);
14              
15             sub _find_or_combine_overlap {
16 24     24   56 my ($action, $seq1, $seq2, $detail) = @_;
17              
18 24         36 my @overlap_items;
19             my $index_at_seq1;
20              
21             L1:
22 24         41 for my $i (0 .. $#{$seq1}) {
  24         67  
23 60         80 my $j = $i;
24 60   100     84 while ($j <= $#{$seq1} && ($j-$i) <= $#{$seq2}) {
  116         253  
  104         258  
25 100 100       250 if ($seq1->[$j] ne $seq2->[$j - $i]) {
26 44         84 next L1;
27             }
28 56         79 $j++;
29             }
30 16         26 @overlap_items = @{$seq1}[$i .. $#{$seq1}];
  16         43  
  16         40  
31 16         27 $index_at_seq1 = $i;
32 16         29 last L1;
33             }
34              
35 24 100       57 if ($action eq 'find') {
36             # find
37 12 100       26 if ($detail) { return (\@overlap_items, $index_at_seq1) } else { return @overlap_items }
  6         52  
  6         41  
38             } else {
39             # combine
40 12         14 my @combined;
41 12 100       26 if (defined $index_at_seq1) {
42 8         13 @combined = (@$seq1, @{$seq2}[ ($#{$seq1} - $index_at_seq1 + 1) .. $#{$seq2} ]);
  8         19  
  8         16  
  8         14  
43             } else {
44 4         13 @combined = (@$seq1, @$seq2);
45             }
46 12 100       23 if ($detail) { return (\@combined, \@overlap_items, $index_at_seq1) } else { return @combined }
  6         65  
  6         48  
47             }
48             }
49              
50 12     12 1 1218 sub find_overlap { _find_or_combine_overlap('find', @_) }
51              
52 12     12 1 2863 sub combine_overlap { _find_or_combine_overlap('combine', @_) }
53              
54             1;
55             # ABSTRACT: Find/remove overlapping items between two ordered sequences
56              
57             __END__