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   250154 use strict;
  2         29  
  2         79  
8 2     2   13 use warnings;
  2         3  
  2         88  
9              
10 2     2   934 use Array::RefElem ();
  2         1105  
  2         191  
11              
12             our $VERSION = '0.94';
13              
14              
15             # shared undef variable
16             my $UNDEF = undef;
17              
18             BEGIN {
19             # handy aliases
20 2     2   11 *has = \&exists;
21 2         3 *contains = \&exists;
22 2         4 *is_null = \&is_empty;
23 2         661 *remove = \&delete;
24             }
25              
26              
27             sub new {
28 3     3 1 985 my $class = shift;
29 3         8 my $x = bless {}, $class;
30              
31 3         5 my $opt;
32 3 50       12 $opt = shift if ref( $_[0] ) eq 'HASH';
33              
34 3 100       11 $x->insert(@_) if @_ != 0;
35              
36 3         18 $x;
37             }
38              
39              
40             sub insert {
41 12     12 1 3693 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         22 my $inserted = 0;
47 12         21 for (@_) {
48             $inserted++, Array::RefElem::hv_store( %$x, $_, $UNDEF )
49 72 100       212 unless CORE::exists $x->{$_};
50             }
51 12         45 $inserted;
52             }
53              
54              
55             sub is_empty {
56 16     16 1 2818 my ($x) = @_;
57              
58 16         76 scalar keys %$x == 0;
59             }
60              
61              
62             sub size {
63 9     9 1 523 my ($x) = @_;
64              
65 9         40 scalar keys %$x;
66             }
67              
68              
69             sub exists {
70 16     16 1 2238 my ( $x, $key ) = @_;
71              
72 16         66 CORE::exists $x->{$key};
73             }
74              
75              
76             sub delete {
77 4     4 1 10 my $x = shift;
78              
79 4         6 my $deleted = 0;
80 4         8 for (@_) {
81 6 100       18 $deleted++, delete $x->{$_} if CORE::exists $x->{$_};
82             }
83 4         16 $deleted;
84             }
85              
86              
87             sub members {
88 3     3 1 7 my ($x) = @_;
89 3         20 return keys %$x;
90             }
91              
92              
93             1;
94              
95             __END__