blib/lib/Hash/MultiKey.pm

filter: Statementstmt 100% 44 / 44 Branchbran 61.5% 16 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod n/a totaltotal 88.1% 74 / 84 CC 14 SCAR 11.1
CC
14
Cov
86%
STORE
4.4
DELETE
4.4
EXISTS
4.4
CRAP 3.0
↑ Summary
1 package Hash::MultiKey;
2  
3 17 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 17 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 $VERSION = '0.06';
11  
12 # ---[ Implementation Overview ]----------------------------------------
13 #
14 # The first implementation of this module was based in an explicit tree.
15 # Right after its announcement in news:comp.lang.perl.modules Benjamin
16 # Goldberg suggested a radically different approach, far much simple and
17 # efficient. The current code is entirely based on his idea.
18 #
19 # Multi-key hashes are implemented now with a plain hash. There is no
20 # nesting involved.
21 #
22 # Lists of keys are converted to strings with pack():
23 #
24 # $key = pack 'N' . ('w/a*' x @$keys), scalar(@$keys), @$keys;
25 #
26 # and that $key is what's used in the underlying hash. The first chunk
27 # stores the number of keys, to be used afterwards when we decode it.
28 # Then, pairs length_of_key/key follow.
29 #
30 # Conversely, to retrieve the original list of keys from a real key we
31 # use unpack():
32 #
33 # $n = unpack 'N', $key;
34 # [ unpack 'x4' . ('w/a*' x $n), $key ];
35 #
36 # Iteration is delegated to the iterator of the very hash.
37 #
38 # Knowing that the following code is crystal clear, so comments have
39 # been removed altogether.
40 #
41 # ----------------------------------------------------------------------
42  
43  
44 17 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
45 17 bless {}, shift;
46 }
47  
48 2 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
49 2 %{ shift() } = ();
50 }
51  
52 273 ▶sub FETCH {
Subroutine FETCH: called CC=4 SCAR=13.9
53 273 my ($self, $keys) = @_;
54 273 ▶ $keys = [$keys eq '' ? ('') : split /$;/, $keys, -1] unless ref $keys eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 1 272 273
$keys eq '' ? : 0 1 1
55 273 ▶ @$keys or croak "Empty multi-key\n";
Branch: 1 branch
BranchTrueFalseTotal
@$keys or croak("Empty multi-key\n") 272 1 273
56 272 $self->{pack 'N' . ('w/a*' x @$keys), scalar(@$keys), @$keys};
57 }
58  
59 153 ▶sub STORE {
Subroutine STORE: called CC=4 SCAR=14.9
60 153 my ($self, $keys, $value) = @_;
61 153 ▶ $keys = [$keys eq '' ? ('') : split /$;/, $keys, -1] unless ref $keys eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 0 153 153
$keys eq '' ? : 0 0 0
62 153 ▶ @$keys or croak "Empty multi-key\n";
Branch: 1 branch
BranchTrueFalseTotal
@$keys or croak("Empty multi-key\n") 152 1 153
63 152 $self->{pack 'N' . ('w/a*' x @$keys), scalar(@$keys), @$keys} = $value;
64 }
65  
66 68 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=14.9
67 68 my ($self, $keys) = @_;
68 68 ▶ $keys = [$keys eq '' ? ('') : split /$;/, $keys, -1] unless ref $keys eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 0 68 68
$keys eq '' ? : 0 0 0
69 68 ▶ @$keys or croak "Empty multi-key\n";
Branch: 1 branch
BranchTrueFalseTotal
@$keys or croak("Empty multi-key\n") 67 1 68
70 67 delete $self->{pack 'N' . ('w/a*' x @$keys), scalar(@$keys), @$keys};
71 }
72  
73 103 ▶sub EXISTS {
Subroutine EXISTS: called CC=4 SCAR=14.9
74 103 my ($self, $keys) = @_;
75 103 ▶ $keys = [$keys eq '' ? ('') : split /$;/, $keys, -1] unless ref $keys eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $keys eq "ARRAY" 0 103 103
$keys eq '' ? : 0 0 0
76 103 ▶ @$keys or croak "Empty multi-key\n";
Branch: 1 branch
BranchTrueFalseTotal
@$keys or croak("Empty multi-key\n") 102 1 103
77 102 exists $self->{pack 'N' . ('w/a*' x @$keys), scalar(@$keys), @$keys};
78 }
79  
80 95 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
81 95 my ($self) = @_;
82 95 keys %$self; # reset iterator
83 95 $self->NEXTKEY;
84 }
85  
86 425 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=6.9
87 425 my ($self) = @_;
88 425 ▶ defined(my $key = each %$self) or return;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $key = each %$self) or return 330 95 425
89 330 my $n = unpack 'N', $key;
90 330 [ unpack 'x4' . ('w/a*' x $n), $key ];
91 }
92  
93 4 ▶sub SCALAR {
Subroutine SCALAR: called CC=1 SCAR=0.0
94 4 my ($self) = @_;
95 4 scalar %$self;
96 }
97  
98 17 1;
99  
100  
101 __END__
↑ Summary