blib/lib/Tie/LLHash.pm

filter: Statementstmt 84.4% 98 / 116 Branchbran 76.0% 35 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 68.1% 15 / 22 Podpod 100% 10 / 10 totaltotal 81.4% 158 / 194 CC 24 SCAR 12.8
CC
24
Cov
82%
insert
6.2
DELETE
5.4
first
5.0
CRAP 3.6
↑ Summary
1 package Tie::LLHash;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '1.004';
8  
9 5 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.0
10 5 my $pkg = shift;
11  
12 5 my $self = bless {}, $pkg;
13 5 ▶ %$self = ( %$self, %{shift()} ) if ref $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] 2 3 5
14 5 $self->CLEAR;
15  
16 # Initialize the hash if more arguments are given
17 5 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 5 16
18 11 $self->last( splice(@_, 0, 2) );
19 }
20  
21 5 return $self;
22 }
23  
24 # Standard access methods:
25  
26 24 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.0
27 24 my $self = shift;
28 24 my $key = shift;
29  
30 24 ▶ return undef unless $self->EXISTS($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXISTS($key) 0 24 24
31 24 return $self->{'nodes'}{$key}{'value'};
32 }
33  
34 4 ▶sub STORE {
Subroutine STORE: called CC=3 SCAR=11.6
35 4 my $self = shift;
36 4 my $name = shift;
37 4 my $value = shift;
38  
39 4 ▶ if (exists $self->{'nodes'}{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"nodes"}{$name}) 0 4 4
40 0 return $self->{'nodes'}{$name}{'value'} = $value;
41 }
42  
43 4 ▶ croak ("No such key '$name', use first() or insert() to add keys") unless $self->{lazy};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"lazy"} 0 4 4
44 4 return $self->last($name, $value);
45 }
46  
47  
48 12 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
49 12 my $self = shift;
50 12 return $self->{'current'} = $self->{'first'};
51 }
52  
53 57 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=6.9
54 57 my $self = shift;
55 return $self->{'current'} = (defined $self->{'current'}
56 ? $self->{'nodes'}{ $self->{'current'} }{'next'}
57 57 ▶ : $self->{'first'});
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'current'} ? : 55 2 57
58 }
59  
60 81 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
61 81 my $self = shift;
62 81 my $name = shift;
63 81 return exists $self->{'nodes'}{$name};
64 }
65  
66 6 ▶sub DELETE {
Subroutine DELETE: called CC=5 SCAR=16.9
67 6 my $self = shift;
68 6 my $key = shift;
69  
70 6 ▶ return unless $self->EXISTS($key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXISTS($key) 0 6 6
71 6 my $node = $self->{'nodes'}{$key};
72  
73 6 ▶ if ($self->{'first'} eq $self->{'last'}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->{'first'} eq $self->{'last'}) { } 2 4 6
elsif ($self->{'first'} eq $key) { } 3 1 4
elsif ($self->{'last'} eq $key) { } 1 0 1
74 2 $self->{'first'} = undef;
75 2 $self->{'current'} = undef;
76 2 $self->{'last'} = undef;
77  
78 } elsif ($self->{'first'} eq $key) {
79 3 $self->{'first'} = $node->{'next'};
80 3 $self->{'nodes'}{ $self->{'first'} }{'prev'} = undef;
81 3 $self->{'current'} = undef;
82  
83 } elsif ($self->{'last'} eq $key) {
84 1 $self->{'current'} = $self->{'last'} = $node->{'prev'};
85 1 $self->{'nodes'}{ $self->{'last'} }{'next'} = undef;
86  
87 } else {
88 0 my $key_one = $node->{'prev'};
89 0 my $key_three = $node->{'next'};
90 0 $self->{'nodes'}{$key_one }{'next'} = $key_three;
91 0 $self->{'nodes'}{$key_three}{'prev'} = $key_one;
92 0 $self->{'current'} = $key_one;
93 }
94  
95 6 return +(delete $self->{'nodes'}{$key})->{value};
96 }
97  
98 6 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
99 6 my $self = shift;
100  
101 6 $self->{'first'} = undef;
102 6 $self->{'last'} = undef;
103 6 $self->{'current'} = undef;
104 6 $self->{'nodes'} = {};
105 }
106  
107 3 ▶sub SCALAR {
Subroutine SCALAR: called CC=1 SCAR=0.0
108 3 my $self = shift;
109 3 return scalar %{$self->{'nodes'}};
110 }
111  
112 # Special access methods
113 # Use (tied %hash)->method to get at them
114  
115 3 ▶sub insert {
Subroutine insert: called CC=6 SCAR=18.3
Pod documented
116 3 my $self = shift;
117 3 my $two_key = shift;
118 3 my $two_value = shift;
119 3 my $one_key = shift;
120  
121 # insert(key,val) and insert(key,val,undef) == first(key,val)
122 3 ▶ return $self->first($two_key, $two_value) unless defined $one_key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $one_key 1 2 3
123  
124 2 ▶ croak ("No such key '$one_key'") unless $self->EXISTS($one_key);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->EXISTS($one_key) 0 2 2
125 2 ▶ croak ("'$two_key' already exists") if $self->EXISTS($two_key);
Branch: 1 branch
BranchTrueFalseTotal
if $self->EXISTS($two_key) 0 2 2
126  
127 2 my $three_key = $self->{'nodes'}{$one_key}{'next'};
128  
129 2 $self->{'nodes'}{$one_key}{'next'} = $two_key;
130  
131 2 $self->{'nodes'}{$two_key}{'prev'} = $one_key;
132 2 $self->{'nodes'}{$two_key}{'next'} = $three_key;
133 2 $self->{'nodes'}{$two_key}{'value'} = $two_value;
134  
135 2 ▶ if (defined $three_key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $three_key) 0 2 2
136 0 $self->{'nodes'}{$three_key}{'prev'} = $two_key;
137 }
138  
139 # If we're adding to the end of the hash, adjust the {last} pointer:
140 2 ▶ if ($one_key eq $self->{'last'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($one_key eq $self->{"last"}) 2 0 2
141 2 $self->{'last'} = $two_key;
142 }
143  
144 2 return $two_value;
145 }
146  
147 7 ▶sub first {
Subroutine first: called CC=5 SCAR=16.1
Pod documented
148 7 my $self = shift;
149  
150 7 ▶ if (@_) { # Set it
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 6 1 7
151 6 my $newkey = shift;
152 6 my $newvalue = shift;
153  
154 6 ▶ croak ("'$newkey' already exists") if $self->EXISTS($newkey);
Branch: 1 branch
BranchTrueFalseTotal
if $self->EXISTS($newkey) 0 6 6
155  
156 # Create the new node
157 6 $self->{'nodes'}{$newkey} =
158 {
159 'next' => undef,
160 'value' => $newvalue,
161 'prev' => undef,
162 };
163  
164 # Put it in its relative place
165 6 ▶ if (defined $self->{'first'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"first"}) 4 2 6
166 4 $self->{'nodes'}{$newkey}{'next'} = $self->{'first'};
167 4 $self->{'nodes'}{ $self->{'first'} }{'prev'} = $newkey;
168 }
169  
170 # Finally, make this node the first node
171 6 $self->{'first'} = $newkey;
172  
173 # If this is an empty hash, make it the last node too
174 6 ▶ $self->{'last'} = $newkey unless (defined $self->{'last'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"last"} 2 4 6
175 }
176 7 return $self->{'first'};
177 }
178  
179 30 ▶sub last {
Subroutine last: called CC=5 SCAR=16.1
Pod documented
180 30 my $self = shift;
181  
182 30 ▶ if (@_) { # Set it
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 29 1 30
183 29 my $newkey = shift;
184 29 my $newvalue = shift;
185  
186 29 ▶ croak ("'$newkey' already exists") if $self->EXISTS($newkey);
Branch: 1 branch
BranchTrueFalseTotal
if $self->EXISTS($newkey) 0 29 29
187  
188 # Create the new node
189 29 $self->{'nodes'}{$newkey} =
190 {
191 'next' => undef,
192 'value' => $newvalue,
193 'prev' => undef,
194 };
195  
196 # Put it in its relative place
197 29 ▶ if (defined $self->{'last'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"last"}) 24 5 29
198 24 $self->{'nodes'}{$newkey}{'prev'} = $self->{'last'};
199 24 $self->{'nodes'}{ $self->{'last'} }{'next'} = $newkey;
200 }
201  
202 # Finally, make this node the last node
203 29 $self->{'last'} = $newkey;
204  
205 # If this is an empty hash, make it the first node too
206 29 ▶ $self->{'first'} = $newkey unless (defined $self->{'first'});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"first"} 5 24 29
207 }
208 30 return $self->{'last'};
209 }
210  
211 0 ▶sub key_before {
Subroutine key_before: not called CC=1 SCAR=6.9
Pod documented
212 0 return $_[0]->{'nodes'}{$_[1]}{'prev'};
213 }
214  
215 0 ▶sub key_after {
Subroutine key_after: not called CC=1 SCAR=6.9
Pod documented
216 0 return $_[0]->{'nodes'}{$_[1]}{'next'};
217 }
218  
219 0 ▶sub current_key {
Subroutine current_key: not called CC=1 SCAR=6.9
Pod documented
220 0 return $_[0]->{'current'};
221 }
222  
223 0 ▶sub current_value {
Subroutine current_value: not called CC=1 SCAR=6.9
Pod documented
224 0 my $self = shift;
225 0 return $self->FETCH($self->{'current'});
226 }
227  
228 0 ▶sub next {
Subroutine next: not called CC=1 SCAR=6.9
Pod documented
229 0 my $self = shift;
230 0 return $self->NEXTKEY;
231 }
232  
233 0 ▶sub prev {
Subroutine prev: not called CC=1 SCAR=6.9
Pod documented
234 0 my $self = shift;
235 0 return $self->{'current'} = $self->{'nodes'}{ $self->{'current'} }{'prev'};
236 }
237  
238 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
239 0 my $self = shift;
240 0 return $self->FIRSTKEY;
241 }
242  
243 2 1;
244 __END__
↑ Summary