File Coverage

lib/Set/Light.pm
Criterion Covered Total %
statement 37 37 100.0
branch 7 8 87.5
condition n/a
subroutine 11 11 100.0
pod 7 7 100.0
total 62 63 98.4


line stmt bran cond sub pod time code
1             package Set::Light;
2              
3             # ABSTRACT: (memory efficient) unordered set of strings
4              
5             require 5.006;
6              
7 2     2   353068 use strict;
  2         6  
  2         111  
8 2     2   12 use warnings;
  2         4  
  2         117  
9              
10 2     2   932 use Array::RefElem ();
  2         1366  
  2         156  
11              
12             our $VERSION = '0.96';
13              
14              
15             # shared undef variable
16             my $UNDEF = undef;
17              
18             BEGIN {
19             # handy aliases
20 2     2   6 *has = \&exists;
21 2         3 *contains = \&exists;
22 2         4 *is_null = \&is_empty;
23 2         793 *remove = \&delete;
24             }
25              
26              
27             sub new {
28 3     3 1 409104 my $class = shift;
29 3         11 my $x = bless {}, $class;
30              
31 3         6 my $opt;
32 3 50       14 $opt = shift if ref( $_[0] ) eq 'HASH';
33              
34 3 100       16 $x->insert(@_) if @_ != 0;
35              
36 3         21 $x;
37             }
38              
39              
40             sub insert {
41 12     12 1 5641 my $x = shift;
42              
43             # Note: this trick may no longer be necessesary for modern perls,
44             # when storing an undef value.
45              
46 12         18 my $inserted = 0;
47 12         31 for (@_) {
48             $inserted++, Array::RefElem::hv_store( %$x, $_, $UNDEF )
49 72 100       380 unless CORE::exists $x->{$_};
50             }
51 12         79 $inserted;
52             }
53              
54              
55             sub is_empty {
56 16     16 1 2931 my ($x) = @_;
57              
58 16         87 scalar keys %$x == 0;
59             }
60              
61              
62             sub size {
63 9     9 1 896 my ($x) = @_;
64              
65 9         43 scalar keys %$x;
66             }
67              
68              
69             sub exists {
70 16     16 1 3703 my ( $x, $key ) = @_;
71              
72 16         85 CORE::exists $x->{$key};
73             }
74              
75              
76             sub delete {
77 4     4 1 10 my $x = shift;
78              
79 4         7 my $deleted = 0;
80 4         8 for (@_) {
81 6 100       23 $deleted++, delete $x->{$_} if CORE::exists $x->{$_};
82             }
83 4         18 $deleted;
84             }
85              
86              
87             sub members {
88 3     3 1 8 my ($x) = @_;
89 3         23 return keys %$x;
90             }
91              
92              
93             1;
94              
95             __END__