| 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
| |||||||||||||||
| 55 | 273 | ▶ | @$keys or croak "Empty multi-key\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 62 | 153 | ▶ | @$keys or croak "Empty multi-key\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 69 | 68 | ▶ | @$keys or croak "Empty multi-key\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 76 | 103 | ▶ | @$keys or croak "Empty multi-key\n"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 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
| |||||||||||||||
| 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__ | ||||||||||||||