File Coverage

blib/lib/Hash/Util/Set/PP.pm
Criterion Covered Total %
statement 64 64 100.0
branch 8 8 100.0
condition 9 9 100.0
subroutine 26 26 100.0
pod 0 14 0.0
total 107 121 88.4


line stmt bran cond sub pod time code
1             package Hash::Util::Set::PP;
2 7     7   273407 use strict;
  7         23  
  7         306  
3 7     7   39 use warnings;
  7         15  
  7         420  
4              
5 7     7   39 use Exporter qw[import];
  7         14  
  7         316  
6 7     7   41 use List::Util qw[all any];
  7         24  
  7         8987  
7              
8             our $VERSION = '0.07';
9             our @EXPORT_OK = qw[ keys_union
10             keys_intersection
11             keys_difference
12             keys_symmetric_difference
13             keys_disjoint
14             keys_equal
15             keys_subset
16             keys_proper_subset
17             keys_superset
18             keys_proper_superset
19             keys_any
20             keys_all
21             keys_none
22             keys_partition ];
23              
24             our %EXPORT_TAGS = ( all => \@EXPORT_OK );
25              
26             sub keys_union(\%\%) {
27 4027     4027 0 8067670 my ($x, $y) = @_;
28 4027         6777 my %z; @z{
29 4027         79896 keys %$x,
30             keys %$y
31             } = ();
32 4027         143164 return keys %z;
33             }
34              
35             sub keys_intersection(\%\%) {
36 6044     6044 0 2275548 my ($x, $y) = @_;
37 6044 100       24709 ($x, $y) = ($y, $x) if (keys %$x > keys %$y);
38 6044         147222 return grep { exists $y->{$_} } keys %$x;
  181869         388208  
39             }
40              
41             sub keys_difference(\%\%) {
42 6019     6019 0 2138859 my ($x, $y) = @_;
43 6019         44511 return grep { not exists $y->{$_} } keys %$x;
  192292         412803  
44             }
45              
46             sub keys_symmetric_difference(\%\%) {
47 4012     4012 0 2350600 my ($x, $y) = @_;
48             my @k = (
49 128053         290187 (grep { not exists $y->{$_} } keys %$x),
50 4012         31864 (grep { not exists $x->{$_} } keys %$y),
  127734         273422  
51             );
52 4012         50227 return @k;
53             }
54              
55             sub keys_disjoint(\%\%) {
56 1009     1009 0 3125167 my ($x, $y) = @_;
57 1009 100       5908 ($x, $y) = ($y, $x) if (keys %$x > keys %$y);
58 1009     1865   38611 return not any { exists $y->{$_} } keys %$x;
  1865         20791  
59             }
60              
61             sub keys_equal(\%\%) {
62 1009     1009 0 5186 my ($x, $y) = @_;
63 1009   100 166   7832 return keys %$x == keys %$y && all { exists $y->{$_} } keys %$x;
  166         2310  
64             }
65              
66             sub keys_subset(\%\%) {
67 2013     2013 0 9294 my ($x, $y) = @_;
68 2013   100 2251   24589 return keys %$x <= keys %$y && all { exists $y->{$_} } keys %$x;
  2251         38502  
69             }
70              
71             sub keys_proper_subset(\%\%) {
72 2010     2010 0 5873 my ($x, $y) = @_;
73 2010   100 1923   23598 return keys %$x < keys %$y && all { exists $y->{$_} } keys %$x;
  1923         34148  
74             }
75              
76             sub keys_superset(\%\%) {
77 1006     1006 0 3462 my ($x, $y) = @_;
78 1006         2950 return &keys_subset($y, $x);
79             }
80              
81             sub keys_proper_superset(\%\%) {
82 1005     1005 0 3324 my ($x, $y) = @_;
83 1005         2812 return &keys_proper_subset($y, $x);
84             }
85              
86             sub keys_any(\%@) {
87 1013     1013 0 1017628 my $x = shift;
88 1013     1816   8054 return any { exists $x->{$_} } @_;
  1816         9662  
89             }
90              
91             sub keys_all(\%@) {
92 1012     1012 0 990487 my $x = shift;
93 1012     1764   8225 return all { exists $x->{$_} } @_;
  1764         8696  
94             }
95              
96             sub keys_none(\%@) {
97 1010     1010 0 999040 my $x = shift;
98 1010     1814   7607 return not any { exists $x->{$_} } @_;
  1814         8333  
99             }
100              
101             sub keys_partition(\%\%) {
102 1004     1004 0 1103359 my ($x, $y) = @_;
103              
104 1004         2536 my (@only_x, @both, @only_y);
105 1004         10136 foreach my $k (keys %$x) {
106 32113 100       65562 if (exists $y->{$k}) {
107 15852         31603 push @both, $k;
108             } else {
109 16261         32150 push @only_x, $k;
110             }
111             }
112              
113 1004         9149 foreach my $k (keys %$y) {
114 31751 100       78865 push @only_y, $k unless exists $x->{$k};
115             }
116              
117 1004         8324 return (\@only_x, \@both, \@only_y);
118             }
119              
120             BEGIN {
121 7     7   368 delete @Hash::Util::Set::PP::{qw(all any)};
122             }
123              
124             1;