File Coverage

blib/lib/Set/Tiny.pm
Criterion Covered Total %
statement 68 68 100.0
branch 17 18 94.4
condition 5 5 100.0
subroutine 27 27 100.0
pod 25 25 100.0
total 142 143 99.3


line stmt bran cond sub pod time code
1             package Set::Tiny;
2              
3 2     2   1204 use 5.004;
  2         6  
4 2     2   7 use strict;
  2         2  
  2         1557  
5              
6             require Exporter;
7             @Set::Tiny::ISA = qw(Exporter);
8             @Set::Tiny::EXPORT_OK = qw(set);
9              
10             $Set::Tiny::VERSION = '0.04';
11              
12             sub new {
13 26     26 1 664 my $class = shift;
14 26         33 my %self;
15 26         55 @self{@_} = ();
16 26         46 return bless \%self, $class;
17             }
18              
19             sub set {
20 4 100   4 1 677 if (ref($_[0]) eq "Set::Tiny") {
    100          
21 1         4 return $_[0]->clone();
22             }
23             elsif (ref($_[0]) eq 'ARRAY') {
24 1         1 return Set::Tiny->new(@{$_[0]});
  1         5  
25             }
26             else {
27 2         5 return Set::Tiny->new(@_);
28             }
29             }
30              
31 98     98 1 1298 sub as_string { "(" . join(" ", sort keys %{$_[0]}) . ")" }
  98         470  
32              
33 30     30 1 360 sub size { scalar keys %{$_[0]} }
  30         118  
34              
35 3 100   3 1 345 sub element { exists $_[0]->{$_[1]} ? $_[1] : () }
36              
37 3     3 1 4 sub elements { keys %{$_[0]} }
  3         17  
38              
39             sub contains {
40 19     19 1 528 my $self = shift;
41 19   100     63 exists $self->{$_} or return for @_;
42 15         54 return 1;
43             }
44              
45             sub clone {
46 10     10 1 10 my $class = ref $_[0];
47 10         11 return $class->new( keys %{$_[0]} );
  10         21  
48             }
49              
50             sub clear {
51 2     2 1 3 %{$_[0]} = ();
  2         5  
52 2         3 return $_[0];
53             }
54              
55             sub insert {
56 1     1 1 2 my $self = shift;
57 1         2 @{$self}{@_} = ();
  1         3  
58 1         1 return $self;
59             }
60              
61             sub remove {
62 7     7 1 6 my $self = shift;
63 7         6 delete @{$self}{@_};
  7         12  
64 7         8 return $self;
65             }
66              
67             sub invert {
68 3     3 1 3 my $self = shift;
69 3 100       18 exists $self->{$_} ? delete $self->{$_} : ($self->{$_} = undef) for @_;
70 3         3 return $self;
71             }
72              
73 3     3 1 768 sub is_null { ! %{$_[0]} }
  3         15  
74              
75 15     15 1 18 sub is_subset { $_[1]->contains( keys %{$_[0]} ) }
  15         31  
76              
77 4 100   4 1 10 sub is_proper_subset { $_[0]->size < $_[1]->size && $_[0]->is_subset($_[1]) }
78              
79 4     4 1 8 sub is_superset { $_[1]->is_subset($_[0]) }
80              
81 4 100   4 1 9 sub is_proper_superset { $_[0]->size > $_[1]->size && $_[1]->is_subset($_[0]) }
82              
83 3 100   3 1 7 sub is_equal { $_[1]->is_subset($_[0]) && $_[0]->is_subset($_[1]) }
84              
85 7     7 1 537 sub is_disjoint { ! $_[0]->intersection($_[1])->size }
86              
87             sub is_properly_intersecting {
88 4 100 100 4 1 278 ! $_[0]->is_disjoint($_[1])
89             && $_[0]->difference($_[1])->size
90             && $_[1]->difference($_[0])->size
91             }
92              
93 5     5 1 9 sub difference { $_[0]->clone->remove(keys %{$_[1]}) }
  5         10  
94              
95             sub union {
96 1     1 1 2 my $class = ref $_[0];
97 1         2 return $class->new( keys %{$_[0]}, keys %{$_[1]} );
  1         2  
  1         4  
98             }
99              
100             sub intersection {
101 8     8 1 12 my $class = ref $_[0];
102 8         6 return $class->new( grep { exists($_[0]->{$_}) } keys %{$_[1]} );
  18         27  
  8         19  
103             }
104              
105             sub intersection2 {
106 1     1 1 4 my $class = ref $_[0];
107 1 50       3 my ($a, $b) = $_[0]->size > $_[1]->size ? ($_[0], $_[1]) : ($_[1], $_[0]);
108 1         2 return $class->new( grep { exists($a->{$_}) } keys %{$b} );
  3         4  
  1         3  
109             }
110              
111 2     2 1 7 sub symmetric_difference { $_[0]->clone->invert(keys %{$_[1]}) }
  2         7  
112              
113             {
114             *copy = \&clone;
115             *has = \&contains;
116             *member = \&element;
117             *members = \&elements;
118             *delete = \&remove;
119             *is_empty = \&is_null;
120             *unique = \&symmetric_difference;
121             }
122              
123             1;
124              
125             __END__