File Coverage

blib/lib/App/Sets/Operations.pm
Criterion Covered Total %
statement 50 51 98.0
branch 12 12 100.0
condition 12 16 75.0
subroutine 11 11 100.0
pod 0 3 0.0
total 85 93 91.4


line stmt bran cond sub pod time code
1             package App::Sets::Operations;
2             $App::Sets::Operations::VERSION = '0.976';
3              
4              
5 3     3   10 use strict;
  3         5  
  3         78  
6 3     3   10 use warnings;
  3         3  
  3         131  
7              
8             # ABSTRACT: set operations in Perl
9              
10 3     3   12 use English qw( -no_match_vars );
  3         3  
  3         20  
11 3     3   1134 use 5.010;
  3         8  
12 3     3   12 use App::Sets::Iterator;
  3         3  
  3         918  
13              
14             sub intersect {
15 20     20 0 38 my ($l, $r) = @_;
16 20         25 my ($lh, $rh);
17             return App::Sets::Iterator->new(
18             sub {
19 160     160   138 while ('necessary') {
20 280   100     664 $lh //= $l->drop() // last;
      100        
21 240   50     543 $rh //= $r->drop() // last;
      66        
22 240 100       477 if ($lh eq $rh) {
    100          
23 120         100 my $retval = $lh;
24 120         105 $lh = $rh = undef;
25 120         262 return $retval;
26             }
27             elsif ($lh gt $rh) {
28 40         46 $rh = undef;
29             }
30             else {
31 80         77 $lh = undef;
32             }
33             } ## end while ('necessary')
34 40         64 return undef;
35             }
36 20         123 );
37             } ## end sub intersect
38              
39             sub union {
40 28     28 0 44 my ($l, $r) = @_;
41 28         33 my ($lh, $rh);
42             return App::Sets::Iterator->new(
43             sub {
44 420   66 420   657 while (defined($lh = $l->head()) && defined($rh = $r->head())) {
45 336 100       584 if ($lh eq $rh) {
    100          
46 168         281 $r->drop();
47 168         278 return $l->drop();
48             }
49             elsif ($lh lt $rh) {
50 112         196 return $l->drop();
51             }
52             else {
53 56         142 return $r->drop();
54             }
55             } ## end while (defined($lh = $l->head...
56 84         146 while (defined($lh = $l->drop())) {
57 0         0 return $lh;
58             }
59 84         174 while (defined($rh = $r->drop())) {
60 28         55 return $rh;
61             }
62 56         88 return undef;
63             }
64 28         177 );
65             } ## end sub union
66              
67             sub minus {
68 16     16 0 24 my ($l, $r) = @_;
69 16         13 my ($lh, $rh);
70             return App::Sets::Iterator->new(
71             sub {
72 96   66 96   163 while (defined($lh = $l->head()) && defined($rh = $r->head())) {
73 192 100       339 if ($lh eq $rh) { # shared, drop both
    100          
74 96         205 $r->drop();
75 96         156 $l->drop();
76             }
77             elsif ($lh lt $rh) { # only in left, OK!
78 64         113 return $l->drop();
79             }
80             else { # only in right, go on
81 32         72 $r->drop();
82             }
83             } ## end while (defined($lh = $l->head...
84 32         58 return $l->drop();
85             }
86 16         94 );
87             } ## end sub minus
88              
89             1;
90              
91             __END__