| 1 | # Copyright 2001 Simon Wistow <simon@twosortplanks.com> | ||||||||||||||||||||||||||||
| 2 | # Distributed under the same terms as Perl itself | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | package Tie::Hash::Layered; | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | require 5.005_62; | |||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use AutoLoader qw(AUTOLOAD); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '0.9'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | # -- Tie ourselves into a hash | ||||||||||||||||||||||||||||
| 20 | # We set up a list of other hashes | ||||||||||||||||||||||||||||
| 21 | # so that we can check from L to R | ||||||||||||||||||||||||||||
| 22 | sub TIEHASH | ||||||||||||||||||||||||||||
| 23 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | # get our class | ||||||||||||||||||||||||||||
| 25 | 1 | my $class = shift; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 1 | my $self = {}; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # we assume the rest of our arguments are arrays | ||||||||||||||||||||||||||||
| 31 | 1 | $self->{_HASHES} = \@_; | |||||||||||||||||||||||||||
| 32 | 1 | $self->{_OFFSET} = 0; | |||||||||||||||||||||||||||
| 33 | 1 | $self->{_KEYSSEEN} = {}; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | return bless $self, $class; | |||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub FETCH | ||||||||||||||||||||||||||||
| 40 | 14 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 41 | 14 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # right, we have the key we need to check for | ||||||||||||||||||||||||||||
| 44 | # so we iterate down our list of arrays and | ||||||||||||||||||||||||||||
| 45 | # check to see if the value exists | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 14 | foreach my $hashref (reverse @{$self->{_HASHES}}) | |||||||||||||||||||||||||||
| 51 | { | ||||||||||||||||||||||||||||
| 52 | 22 | ▶ | return $hashref->{$key} if defined $hashref->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # hmm, none of our hashes have that key, | ||||||||||||||||||||||||||||
| 57 | # /shurg/ (sic), return undef; | ||||||||||||||||||||||||||||
| 58 | 3 | return undef; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub STORE | ||||||||||||||||||||||||||||
| 64 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 3 | my ($self,$key, $value) = @_; | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # this is dead easy, we just store it in the first | ||||||||||||||||||||||||||||
| 69 | 3 | return $self->{_HASHES}->[_get_max($self)]->{$key} = $value; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub DELETE | ||||||||||||||||||||||||||||
| 76 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 77 | 2 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # just delete the first key in the the first hash | ||||||||||||||||||||||||||||
| 80 | 2 | my $res = delete $self->{_HASHES}->[_get_max($self)]->{$key}; | |||||||||||||||||||||||||||
| 81 | 2 | return $res; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | sub CLEAR | ||||||||||||||||||||||||||||
| 85 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||
| 87 | 1 | my %hash = (); | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 1 | pop @{$self->{_HASHES}}; | |||||||||||||||||||||||||||
| 91 | 1 | push @{$self->{_HASHES}}, \%hash; | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub EXISTS | ||||||||||||||||||||||||||||
| 96 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 97 | 2 | my ($self, $key) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # again with the iteration :) | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 2 | foreach my $hashref (reverse @{$self->{_HASHES}}) | |||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||
| 104 | 4 | ▶ | return 1 if (exists $hashref->{$key}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | return 0; | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | sub FIRSTKEY | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 2 | $self->{_OFFSET} = 0; | |||||||||||||||||||||||||||
| 117 | 2 | $self->{_KEYSSEEN} = {}; | |||||||||||||||||||||||||||
| 118 | 2 | my $a = keys %{$self->{_HASHES}->[0]}; | |||||||||||||||||||||||||||
| 119 | 2 | each (%{$self->{_HASHES}->[0]}); | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | sub NEXTKEY | ||||||||||||||||||||||||||||
| 127 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 128 | 4 | my ($self) = @_; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 4 | my $key; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 4 | my %keys = %{$self->{_KEYSSEEN}}; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | do | ||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||
| 137 | 5 | $key = each %{ $self->{_HASHES}->[$self->{_OFFSET}] }; | |||||||||||||||||||||||||||
| 138 | 5 | ▶ | if (defined $key) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | 3 | ▶ | $key = undef if ($self->{_KEYSSEEN}->{$key}++); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | #print "Offset : ".$self->{_OFFSET}; | ||||||||||||||||||||||||||||
| 144 | #print " Key = $key" if (defined $key); | ||||||||||||||||||||||||||||
| 145 | #print "\n"; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 4 | ▶ | until (defined $key || ++$self->{_OFFSET} > _get_max($self)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $key or ++$self->{"_OFFSET"} > _get_max($self)
MC/DC100%2/2
defined $key or ++$self->{"_OFFSET"} > _get_max($self)
defined $key
++$self->{"_OFFSET"} > _get_max($self)
Truth table100%3/3
A or B
A= defined $keyB= ++$self->{"_OFFSET"} > _get_max($self)
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | #return undef if ($self->{_OFFSET}==_get_max($self)); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | return $key; | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | sub _get_max | ||||||||||||||||||||||||||||
| 158 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_get_max: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 7 | my $arrref = $self->{_HASHES}; | |||||||||||||||||||||||||||
| 162 | 7 | my @arr = @$arrref; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 7 | return $#arr; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | sub push | ||||||||||||||||||||||||||||
| 170 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 3 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 172 | 3 | return push @{$self->{_HASHES}}, $val; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | sub pop | ||||||||||||||||||||||||||||
| 176 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
pop: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 177 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||
| 178 | 2 | return pop @{$self->{_HASHES}}; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub shift | ||||||||||||||||||||||||||||
| 182 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
shift: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 183 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 184 | 3 | return shift @{$self->{_HASHES}}; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | sub unshift | ||||||||||||||||||||||||||||
| 188 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
unshift: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 189 | 1 | my ($self, $val) = @_; | |||||||||||||||||||||||||||
| 190 | 1 | return unshift @{$self->{_HASHES}}, $val; | |||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 1 | 1; | |||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||