blib/lib/Tie/IxHash.pm

filter: Statementstmt 72.7% 168 / 231 Branchbran 67.6% 46 / 68 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 74.1% 23 / 31 Podpod 75.0% 15 / 20 totaltotal 70.7% 254 / 359 CC 46 SCAR 20.7
CC
46
Cov
71%
Unshift
42.0
Shift
12.0
Splice
10.5
CRAP 7.9
↑ Summary
1 #
2 # Tie/IxHash.pm
3 #
4 # Indexed hash implementation for Perl
5 #
6 # See below for documentation.
7 #
8
9 2 require 5.005;
10
11 package Tie::IxHash;
12 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 require Tie::Hash;
15 2 ▶use vars qw/@ISA $VERSION/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 @ISA = qw(Tie::Hash);
17
18 2 $VERSION = $VERSION = '1.23';
19
20 #
21 # standard tie functions
22 #
23
24 2 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=2 SCAR=6.9
25 2 my($c) = shift;
26 2 my($s) = [];
27 2 $s->[0] = {}; # hashkey index
28 2 $s->[1] = []; # array of keys
29 2 $s->[2] = []; # array of data
30 2 $s->[3] = 0; # iter count
31
32 2 bless $s, $c;
33
34 2 ▶ $s->Push(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
35
36 2 return $s;
37 }
38
39 #sub DESTROY {} # costly if there's nothing to do
40
41 48 ▶sub FETCH {
Subroutine FETCH: called CC=2 SCAR=7.2
42 48 my($s, $k) = (shift, shift);
43 48 ▶ return exists( $s->[0]{$k} ) ? $s->[2][ $s->[0]{$k} ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $s->[0]{$k} ? : 48 0 48
44 }
45
46 20 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
47 20 my($s, $k, $v) = (shift, shift, shift);
48
49 20 ▶ if (exists $s->[0]{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$k}) { } 2 18 20
50 2 my($i) = $s->[0]{$k};
51 2 $s->[1][$i] = $k;
52 2 $s->[2][$i] = $v;
53 2 $s->[0]{$k} = $i;
54 }
55 else {
56 18 push(@{$s->[1]}, $k);
57 18 push(@{$s->[2]}, $v);
58 18 $s->[0]{$k} = $#{$s->[1]};
59 }
60 }
61
62 11 ▶sub DELETE {
Subroutine DELETE: called CC=4 SCAR=13.9
63 11 my($s, $k) = (shift, shift);
64
65 11 ▶ if (exists $s->[0]{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$k}) 11 0 11
66 11 my($i) = $s->[0]{$k};
67 11 for ($i+1..$#{$s->[1]}) { # reset higher elt indexes
68 16 $s->[0]{ $s->[1][$_] }--; # timeconsuming, is there is better way?
69 }
70 11 ▶ if ( $i == $s->[3]-1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $s->[3] - 1) 4 7 11
71 4 $s->[3]--;
72 }
73 11 delete $s->[0]{$k};
74 11 splice @{$s->[1]}, $i, 1;
75 11 return (splice(@{$s->[2]}, $i, 1))[0];
76 }
77 0 return undef;
78 }
79
80 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
81 0 exists $_[0]->[0]{ $_[1] };
82 }
83
84 18 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
85 18 $_[0][3] = 0;
86 18 &NEXTKEY;
87 }
88
89 74 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=6.9
90 74 ▶ return $_[0][1][ $_[0][3]++ ] if ($_[0][3] <= $#{ $_[0][1] } );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][3] <= $#{$_[0][1];} 56 18 74
91 18 return undef;
92 }
93
94
95
96 #
97 #
98 # class functions that provide additional capabilities
99 #
100 #
101
102 0 ▶sub new { TIEHASH(@_) }
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
103
104 1 ▶sub Clear {
Subroutine Clear: called CC=1 SCAR=0.0
Pod documented
105 1 my $s = shift;
106 1 $s->[0] = {}; # hashkey index
107 1 $s->[1] = []; # array of keys
108 1 $s->[2] = []; # array of data
109 1 $s->[3] = 0; # iter count
110 1 return;
111 }
112
113 #
114 # add pairs to end of indexed hash
115 # note that if a supplied key exists, it will not be reordered
116 #
117 5 ▶sub Push {
Subroutine Push: called CC=2 SCAR=6.9
Pod documented
118 5 my($s) = shift;
119 5 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 5 15
120 10 $s->STORE(shift, shift);
121 }
122 5 return scalar(@{$s->[1]});
123 }
124
125 0 ▶sub Push2 {
Subroutine Push2: not called CC=1 SCAR=6.9
Pod undocumented
126 0 my($s) = shift;
127 0 $s->Splice($#{$s->[1]}+1, 0, @_);
128 0 return scalar(@{$s->[1]});
129 }
130
131 #
132 # pop last k-v pair
133 #
134 3 ▶sub Pop {
Subroutine Pop: called CC=2 SCAR=7.0
Pod documented
135 3 my($s) = shift;
136 3 my($k, $v, $i);
137 3 $k = pop(@{$s->[1]});
138 3 $v = pop(@{$s->[2]});
139 3 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) 3 0 3
140 3 delete $s->[0]{$k};
141 3 return ($k, $v);
142 }
143 0 return undef;
144 }
145
146 0 ▶sub Pop2 {
Subroutine Pop2: not called CC=1 SCAR=6.9
Pod undocumented
147 0 return $_[0]->Splice(-1);
148 }
149
150 #
151 # shift
152 #
153 0 ▶sub Shift {
Subroutine Shift: not called CC=3 SCAR=24.8
Pod documented
154 0 my($s) = shift;
155 0 my($k, $v, $i);
156 0 $k = shift(@{$s->[1]});
157 0 $v = shift(@{$s->[2]});
158 0 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) 0 0 0
159 0 delete $s->[0]{$k};
160 0 for (keys %{$s->[0]}) {
161 0 $s->[0]{$_}--;
162 }
163 0 return ($k, $v);
164 }
165 0 return undef;
166 }
167
168 0 ▶sub Shift2 {
Subroutine Shift2: not called CC=1 SCAR=6.9
Pod undocumented
169 0 return $_[0]->Splice(0, 1);
170 }
171
172 #
173 # unshift
174 # if a supplied key exists, it will not be reordered
175 #
176 0 ▶sub Unshift {
Subroutine Unshift: not called CC=6 SCAR=37.4
Pod documented
177 0 my($s) = shift;
178 0 my($k, $v, @k, @v, $len, $i);
179
180 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
181 0 ($k, $v) = (shift, shift);
182 0 ▶ if (exists $s->[0]{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$k}) { } 0 0 0
183 0 $i = $s->[0]{$k};
184 0 $s->[1][$i] = $k;
185 0 $s->[2][$i] = $v;
186 0 $s->[0]{$k} = $i;
187 }
188 else {
189 0 push(@k, $k);
190 0 push(@v, $v);
191 0 $len++;
192 }
193 }
194 0 ▶ if (defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) 0 0 0
195 0 for (keys %{$s->[0]}) {
196 0 $s->[0]{$_} += $len;
197 }
198 0 $i = 0;
199 0 for (@k) {
200 0 $s->[0]{$_} = $i++;
201 }
202 0 unshift(@{$s->[1]}, @k);
203 0 return unshift(@{$s->[2]}, @v);
204 }
205 0 return scalar(@{$s->[1]});
206 }
207
208 0 ▶sub Unshift2 {
Subroutine Unshift2: not called CC=1 SCAR=6.9
Pod undocumented
209 0 my($s) = shift;
210 0 $s->Splice(0,0,@_);
211 0 return scalar(@{$s->[1]});
212 }
213
214 #
215 # splice
216 #
217 # any existing hash key order is preserved. the value is replaced for
218 # such keys, and the new keys are spliced in the regular fashion.
219 #
220 # supports -ve offsets but only +ve lengths
221 #
222 # always assumes a 0 start offset
223 #
224 2 ▶sub Splice {
Subroutine Splice: called CC=10 SCAR=23.6
Pod documented
225 2 my($s, $start, $len) = (shift, shift, shift);
226 2 my($k, $v, @k, @v, @r, $i, $siz);
227 2 my($end); # inclusive
228
229 # XXX inline this
230 2 ($start, $end, $len) = $s->_lrange($start, $len);
231
232 2 ▶ if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 2 0 2
233 2 ▶ if ($len > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len > 0) 1 1 2
234 1 my(@k) = splice(@{$s->[1]}, $start, $len);
235 1 my(@v) = splice(@{$s->[2]}, $start, $len);
236 1 ▶ while (@k) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k) 2 1 3
237 2 $k = shift(@k);
238 2 delete $s->[0]{$k};
239 2 push(@r, $k, shift(@v));
240 }
241 1 for ($start..$#{$s->[1]}) {
242 0 $s->[0]{$s->[1][$_]} -= $len;
243 }
244 }
245 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
246 3 ($k, $v) = (shift, shift);
247 3 ▶ if (exists $s->[0]{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$k}) { } 0 3 3
248 # $s->STORE($k, $v);
249 0 $i = $s->[0]{$k};
250 0 $s->[1][$i] = $k;
251 0 $s->[2][$i] = $v;
252 0 $s->[0]{$k} = $i;
253 }
254 else {
255 3 push(@k, $k);
256 3 push(@v, $v);
257 3 $siz++;
258 }
259 }
260 2 ▶ if (defined $siz) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $siz) 2 0 2
261 2 for ($start..$#{$s->[1]}) {
262 0 $s->[0]{$s->[1][$_]} += $siz;
263 }
264 2 $i = $start;
265 2 for (@k) {
266 3 $s->[0]{$_} = $i++;
267 }
268 2 splice(@{$s->[1]}, $start, 0, @k);
269 2 splice(@{$s->[2]}, $start, 0, @v);
270 }
271 }
272 2 return @r;
273 }
274
275 #
276 # delete elements specified by key
277 # other elements higher than the one deleted "slide" down
278 #
279 1 ▶sub Delete {
Subroutine Delete: called CC=2 SCAR=6.9
Pod documented
280 1 my($s) = shift;
281
282 1 for (@_) {
283 #
284 # XXX potential optimization: could do $s->DELETE only if $#_ < 4.
285 # otherwise, should reset all the hash indices in one loop
286 #
287 2 $s->DELETE($_);
288 }
289 }
290
291 #
292 # replace hash element at specified index
293 #
294 # if the optional key is not supplied the value at index will simply be
295 # replaced without affecting the order.
296 #
297 # if an element with the supplied key already exists, it will be deleted first.
298 #
299 # returns the key of replaced value if it succeeds.
300 #
301 2 ▶sub Replace {
Subroutine Replace: called CC=5 SCAR=16.8
Pod documented
302 2 my($s) = shift;
303 2 my($i, $v, $k) = (shift, shift, shift);
304 2 ▶ if (defined $i and $i <= $#{$s->[1]} and $i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $i and $i <= $#{$$s[1];} and $i >= 0) 2 0 2
Condition33%2/6
defined $i and $i <= $#{$$s[1];} and $i >= 0
!ll&&!rl&&r
002
defined $i and $i <= $#{$$s[1];}
!ll&&!rl&&r
002
MC/DC0%0/3
defined $i and $i <= $#{$$s[1];} and $i >= 0
defined $i $i <= $#{$$s[1];} $i >= 0
Truth table25%1/4
A and B and C
A= defined $iB= $i <= $#{$$s[1];}C= $i >= 0
ABCresult
0XX0
10X0
1100
1111
305 2 ▶ if (defined $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $k) { } 1 1 2
306 1 delete $s->[0]{ $s->[1][$i] };
307 1 $s->DELETE($k) ; #if exists $s->[0]{$k};
308 1 $s->[1][$i] = $k;
309 1 $s->[2][$i] = $v;
310 1 $s->[0]{$k} = $i;
311 1 return $k;
312 }
313 else {
314 1 $s->[2][$i] = $v;
315 1 return $s->[1][$i];
316 }
317 }
318 0 return undef;
319 }
320
321 #
322 # Given an $start and $len, returns a legal start and end (where start <= end)
323 # for the current hash.
324 # Legal range is defined as 0 to $#s+1
325 # $len defaults to number of elts upto end of list
326 #
327 # 0 1 2 ...
328 # | X | X | X ... X | X | X |
329 # -2 -1 (no -0 alas)
330 # X's above are the elements
331 #
332 2 ▶sub _lrange {
Subroutine _lrange: called CC=8 SCAR=22.8
333 2 my($s) = shift;
334 2 my($offset, $len) = @_;
335 2 my($start, $end); # both inclusive
336 2 my($size) = $#{$s->[1]}+1;
337
338 2 ▶ return undef unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 0 2 2
339 2 ▶ if($offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($offset < 0) { } 0 2 2
340 0 $start = $offset + $size;
341 0 ▶ $start = 0 if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 0 0 0
342 }
343 else {
344 2 ▶ ($offset > $size) ? ($start = $size) : ($start = $offset);
Branch: 1 branch
BranchTrueFalseTotal
$offset > $size ? : 1 1 2
345 }
346
347 2 ▶ if (defined $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len) { } 2 0 2
348 2 ▶ $len = -$len if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 2 2
349 2 ▶ $len = $size - $start if $len > $size - $start;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $size - $start 1 1 2
350 }
351 else {
352 0 $len = $size - $start;
353 }
354 2 $end = $start + $len - 1;
355
356 2 return ($start, $end, $len);
357 }
358
359 #
360 # Return keys at supplied indices
361 # Returns all keys if no args.
362 #
363 7 ▶sub Keys {
Subroutine Keys: called CC=3 SCAR=11.0
Pod documented
364 7 my($s) = shift;
365 return ( @_ == 1
366 ? $s->[1][$_[0]]
367 : ( @_
368 1 ? @{$s->[1]}[@_]
369 7 ▶ : @{$s->[1]} ) );
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 1 6 7
@_ ? : 1 5 6
370 }
371
372 #
373 # Returns values at supplied indices
374 # Returns all values if no args.
375 #
376 3 ▶sub Values {
Subroutine Values: called CC=3 SCAR=11.0
Pod documented
377 3 my($s) = shift;
378 return ( @_ == 1
379 ? $s->[2][$_[0]]
380 : ( @_
381 1 ? @{$s->[2]}[@_]
382 3 ▶ : @{$s->[2]} ) );
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 ? : 1 2 3
@_ ? : 1 1 2
383 }
384
385 #
386 # get indices of specified hash keys
387 #
388 2 ▶sub Indices {
Subroutine Indices: called CC=2 SCAR=6.9
Pod documented
389 2 my($s) = shift;
390 2 ▶ return ( @_ == 1 ? $s->[0]{$_[0]} : @{$s->[0]}{@_} );
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 1 2
391 }
392
393 #
394 # number of k-v pairs in the ixhash
395 # note that this does not equal the highest index
396 # owing to preextended arrays
397 #
398 2 ▶sub Length {
Subroutine Length: called CC=1 SCAR=0.0
Pod documented
399 2 return scalar @{$_[0]->[1]};
400 }
401
402 #
403 # Reorder the hash in the supplied key order
404 #
405 # warning: any unsupplied keys will be lost from the hash
406 # any supplied keys that dont exist in the hash will be ignored
407 #
408 4 ▶sub Reorder {
Subroutine Reorder: called CC=4 SCAR=13.9
Pod documented
409 4 my($s) = shift;
410 4 my(@k, @v, %x, $i);
411 4 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
412
413 4 $i = 0;
414 4 for (@_) {
415 14 ▶ if (exists $s->[0]{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $s->[0]{$_}) 13 1 14
416 13 push(@k, $_);
417 13 push(@v, $s->[2][ $s->[0]{$_} ] );
418 13 $x{$_} = $i++;
419 }
420 }
421 4 $s->[1] = \@k;
422 4 $s->[2] = \@v;
423 4 $s->[0] = \%x;
424 4 return $s;
425 }
426
427 1 ▶sub SortByKey {
Subroutine SortByKey: called CC=1 SCAR=0.0
Pod documented
428 1 my($s) = shift;
429 1 $s->Reorder(sort $s->Keys);
430 }
431
432 1 ▶sub SortByValue {
Subroutine SortByValue: called CC=1 SCAR=0.0
Pod documented
433 1 my($s) = shift;
434 1 $s->Reorder(sort { $s->FETCH($a) cmp $s->FETCH($b) } $s->Keys)
435 }
436
437 2 1;
438 __END__
↑ Summary