| 1 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 2 | # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de> | ||||||||||
| 3 | # File: GX/Tie/Hash/Ordered.pm | ||||||||||
| 4 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 5 | |||||||||||
| 6 | package GX::Tie::Hash::Ordered; | ||||||||||
| 7 | |||||||||||
| 8 | 106 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 106 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 106 | ▶ | use base 'Tie::Hash'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | |||||||||||
| 14 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 15 | # Standard tie interface | ||||||||||
| 16 | # ---------------------------------------------------------------------------------------------------------------------- | ||||||||||
| 17 | |||||||||||
| 18 | 149 | ▶ | sub CLEAR { | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 149 | @{$_[0]} = ( {}, [], [], 0 ); | |||||||||
| 21 | |||||||||||
| 22 | 149 | return; | |||||||||
| 23 | |||||||||||
| 24 | } | ||||||||||
| 25 | |||||||||||
| 26 | 34 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: called CC=3 SCAR=11.0
| |||||||||||
| 27 | |||||||||||
| 28 | 34 | my $self = shift; | |||||||||
| 29 | |||||||||||
| 30 | 34 | ▶ | if ( defined( my $i = $self->[0]{$_[0]} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | |||||||||||
| 32 | 25 | for ( $i + 1 .. $#{$self->[1]} ) { | |||||||||
| 33 | 51 | $self->[0]{$self->[1][$_]}--; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 25 | delete $self->[0]{$_[0]}; | |||||||||
| 37 | |||||||||||
| 38 | 25 | splice @{$self->[1]}, $i, 1; | |||||||||
| 39 | |||||||||||
| 40 | 25 | return ( splice( @{$self->[2]}, $i, 1 ) )[0]; | |||||||||
| 41 | |||||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | 9 | return undef; | |||||||||
| 45 | |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 344 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | 344 | return exists $_[0]->[0]{$_[1]}; | |||||||||
| 51 | |||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | 9956 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||
| 55 | |||||||||||
| 56 | 9956 | ▶ | return exists $_[0]->[0]{$_[1]} ? $_[0]->[2][ $_[0]->[0]{$_[1]} ] : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 830 | ▶ | sub FIRSTKEY { | ||||||||
|
Subroutine
FIRSTKEY: called CC=2 SCAR=6.9
| |||||||||||
| 61 | |||||||||||
| 62 | 830 | ▶ | if ( @{$_[0][1]} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 757 | $_[0][3] = 1; | |||||||||
| 64 | 757 | return $_[0][1][0]; | |||||||||
| 65 | } | ||||||||||
| 66 | else { | ||||||||||
| 67 | 73 | $_[0][3] = 0; | |||||||||
| 68 | 73 | return undef; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 2267 | ▶ | sub NEXTKEY { | ||||||||
|
Subroutine
NEXTKEY: called CC=2 SCAR=6.9
| |||||||||||
| 74 | |||||||||||
| 75 | 2267 | ▶ | return $_[0][3] <= $#{$_[0][1]} ? $_[0][1][$_[0][3]++] : undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | 5 | ▶ | sub SCALAR { | ||||||||
|
Subroutine
SCALAR: called CC=1 SCAR=0.0
| |||||||||||
| 80 | |||||||||||
| 81 | 5 | return scalar @{$_[0][1]}; | |||||||||
| 82 | |||||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 3765 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=2 SCAR=6.9
| |||||||||||
| 86 | |||||||||||
| 87 | 3765 | my $self = shift; | |||||||||
| 88 | |||||||||||
| 89 | 3765 | ▶ | if ( defined( my $i = $self->[0]{$_[0]} ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 90 | |||||||||||
| 91 | 44 | $self->[1][$i] = $_[0]; | |||||||||
| 92 | 44 | $self->[2][$i] = $_[1]; | |||||||||
| 93 | |||||||||||
| 94 | 44 | $self->[0]{$_[0]} = $i; | |||||||||
| 95 | |||||||||||
| 96 | } | ||||||||||
| 97 | else { | ||||||||||
| 98 | |||||||||||
| 99 | 3721 | push @{$self->[1]}, $_[0]; | |||||||||
| 100 | 3721 | push @{$self->[2]}, $_[1]; | |||||||||
| 101 | |||||||||||
| 102 | 3721 | $self->[0]{$_[0]} = $#{$self->[1]}; | |||||||||
| 103 | |||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | 3765 | return; | |||||||||
| 107 | |||||||||||
| 108 | } | ||||||||||
| 109 | |||||||||||
| 110 | 1908 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 111 | |||||||||||
| 112 | 1908 | return bless( | |||||||||
| 113 | [ | ||||||||||
| 114 | {}, # hash key => index map | ||||||||||
| 115 | [], # hash keys | ||||||||||
| 116 | [], # hash values | ||||||||||
| 117 | 0 # iterator offet | ||||||||||
| 118 | ], | ||||||||||
| 119 | $_[0] | ||||||||||
| 120 | ); | ||||||||||
| 121 | |||||||||||
| 122 | } | ||||||||||
| 123 | |||||||||||
| 124 | |||||||||||
| 125 | 106 | 1; | |||||||||
| 126 | |||||||||||
| 127 | __END__ | ||||||||||