| 1 | # | ||||||||||||||
| 2 | # InsertOrderHash.pm - insert-order-preserving tied hash | ||||||||||||||
| 3 | # | ||||||||||||||
| 4 | # $Id$ | ||||||||||||||
| 5 | # | ||||||||||||||
| 6 | |||||||||||||||
| 7 | package Tie::InsertOrderHash; | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | ▶ | use v5.6.1; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | our $VERSION = '0.01'; | |||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | ▶ | use base qw(Tie::Hash); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 2 | ▶ | sub TIEHASH { my $c = shift; | ||||||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 2 | bless [[@_[grep { $_ % 2 == 0 } (0..$#_)]], {@_}, 0], $c } | |||||||||||||
| 19 | |||||||||||||||
| 20 | 0 | ▶ | sub STORE { @{$_[0]->[0]} = grep { $_ ne $_[1] } @{$_[0]->[0]}; | ||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | 1 | push @{$_[0]->[0]}, $_[1]; | |||||||||||||
| 22 | 1 | $_[0]->[2] = -1; | |||||||||||||
| 23 | 1 | $_[0]->[1]->{$_[1]} = $_[2] } | |||||||||||||
| 24 | |||||||||||||||
| 25 | 1 | ▶ | sub FETCH { $_[0]->[1]->{$_[1]} } | ||||||||||||
|
Subroutine
FETCH: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | sub FIRSTKEY { return wantarray ? () : undef | ||||||||||||
|
Subroutine
FIRSTKEY: called CC=4 SCAR=16.9
Branch: 2 branches
| |||||||||||||||
| 28 | unless exists $_[0]->[0]->[$_[0]->[2] = 0]; | ||||||||||||||
| 29 | 1 | my $key = $_[0]->[0]->[0]; | |||||||||||||
| 30 | 1 | ▶ | return wantarray ? ($key, $_[0]->[1]->{$key}) : $key } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 31 | |||||||||||||||
| 32 | # Guard against deletion (see perldoc -f each) | ||||||||||||||
| 33 | 5 | ▶ | sub NEXTKEY { my $i = $_[0]->[2]; | ||||||||||||
|
Subroutine
NEXTKEY: called CC=7 SCAR=21.2
| |||||||||||||||
| 34 | 5 | ▶ | return wantarray ? () : undef unless exists $_[0]->[0]->[$i]; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 35 | 5 | ▶ | if ($_[0]->[0]->[$i] eq $_[1]) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 36 | 5 | $i = ++$_[0]->[2] ; | |||||||||||||
| 37 | 5 | ▶ | return wantarray ? () : undef | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 38 | unless exists $_[0]->[0]->[$i]; | ||||||||||||||
| 39 | } | ||||||||||||||
| 40 | 4 | my $key = ${$_[0]->[0]}[$i]; | |||||||||||||
| 41 | 4 | ▶ | return wantarray ? ($key, $_[0]->[1]->{$key}) : $key } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub EXISTS { exists $_[0]->[1]->{$_[1]} } | ||||||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||||||
| 44 | |||||||||||||||
| 45 | 0 | ▶ | sub DELETE { @{$_[0]->[0]} = grep { $_ ne $_[1] } @{$_[0]->[0]}; | ||||||||||||
|
Subroutine
DELETE: not called CC=1 SCAR=6.9
| |||||||||||||||
| 46 | 0 | delete $_[0]->[1]->{$_[1]} } | |||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | ▶ | sub CLEAR { @{$_[0]->[0]} = (); | ||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||
| 49 | 0 | %{$_[0]->[1]} = () } | |||||||||||||
| 50 | |||||||||||||||
| 51 | 1 | 1; | |||||||||||||
| 52 | |||||||||||||||
| 53 | __END__ | ||||||||||||||