| 1 | package Tie::Hash::Rank; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 1 | my $VERSION='1.0.1'; | |||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | sub TIEHASH { | ||||||||
|
Subroutine
TIEHASH: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | my $class = shift; | |||||||||
| 9 | 2 | my $self = { | |||||||||
| 10 | ALGORITHM => '$DATA{$b} <=> $DATA{$a}', # rev numeric sort | ||||||||||
| 11 | EQUALITYSUFFIX => '', | ||||||||||
| 12 | EQUALITYPREFIX => '', | ||||||||||
| 13 | RECALCULATE => 'onstore', | ||||||||||
| 14 | @_, | ||||||||||
| 15 | _RANKS => {}, # yes, these go AFTER the parameters | ||||||||||
| 16 | DATA => {} | ||||||||||
| 17 | }; | ||||||||||
| 18 | |||||||||||
| 19 | 2 | return bless $self, $class; | |||||||||
| 20 | } | ||||||||||
| 21 | |||||||||||
| 22 | 8 | ▶ | sub _recalculate { | ||||||||
|
Subroutine
_recalculate: called CC=3 SCAR=11.0
| |||||||||||
| 23 | 8 | my $self = shift; | |||||||||
| 24 | 8 | my $sort_function = $self->{ALGORITHM}; | |||||||||
| 25 | 8 | $sort_function=~s/\$DATA\{(\$[ab])\}/\$self->{DATA}->{$1}/g; | |||||||||
| 26 | 8 | $self->{_RANKS} = {}; | |||||||||
| 27 | 8 | my $i=1; | |||||||||
| 28 | 8 | my $prevkey; | |||||||||
| 29 | 8 | foreach my $key ( | |||||||||
| 30 | eval("sort { $sort_function } keys %{\$self->{DATA}}") | ||||||||||
| 31 | ) { | ||||||||||
| 32 | 26 | $self->{_RANKS}->{$key}=$i++; | |||||||||
| 33 | |||||||||||
| 34 | 1 | ▶ | no warnings; # to avoid 'use of uninitialised value' errors | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | # in next line | ||||||||||
| 36 | |||||||||||
| 37 | 26 | ▶ | if($self->{DATA}->{$key} eq $self->{DATA}->{$prevkey}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | $self->{_RANKS}->{$key} = | ||||||||||
| 39 | $self->{_RANKS}->{$prevkey} = | ||||||||||
| 40 | $self->{EQUALITYPREFIX}. | ||||||||||
| 41 | $self->{_RANKS}->{$prevkey}. | ||||||||||
| 42 | 5 | $self->{EQUALITYSUFFIX}; | |||||||||
| 43 | } | ||||||||||
| 44 | 26 | $prevkey = $key; | |||||||||
| 45 | } | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 2 | ▶ | sub CLEAR { my $self=shift; $self->{DATA}={}; $self->{_RANKS}={}; } | ||||||||
|
Subroutine
CLEAR: called CC=1 SCAR=0.0
| |||||||||||
| 49 | 8 | ▶ | sub STORE { | ||||||||
|
Subroutine
STORE: called CC=2 SCAR=6.9
| |||||||||||
| 50 | 8 | my($self, $key, $value)=@_; | |||||||||
| 51 | 8 | $self->{DATA}->{$key}=$value; | |||||||||
| 52 | 8 | ▶ | $self->_recalculate() if($self->{RECALCULATE} eq 'onstore'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | } | ||||||||||
| 54 | 8 | ▶ | sub FETCH { | ||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||
| 55 | 8 | my $self=shift; | |||||||||
| 56 | 8 | my $key=shift; | |||||||||
| 57 | 8 | ▶ | $self->_recalculate() if($self->{RECALCULATE} eq 'onfetch'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 8 | return $self->{_RANKS}->{$key}; | |||||||||
| 59 | } | ||||||||||
| 60 | 2 | ▶ | sub FIRSTKEY { | ||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 61 | 2 | my $self = shift; | |||||||||
| 62 | 2 | scalar keys %{$self->{DATA}}; | |||||||||
| 63 | 2 | return scalar each %{$self->{DATA}}; | |||||||||
| 64 | } | ||||||||||
| 65 | 8 | ▶ | sub NEXTKEY { | ||||||||
|
Subroutine
NEXTKEY: called CC=1 SCAR=0.0
| |||||||||||
| 66 | 8 | my $self = shift; | |||||||||
| 67 | 8 | return scalar each %{$self->{DATA}}; | |||||||||
| 68 | } | ||||||||||
| 69 | 0 | ▶ | sub DELETE { | ||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||
| 70 | 0 | my $self = shift; | |||||||||
| 71 | 0 | my $key = shift; | |||||||||
| 72 | 0 | delete $self->{_RANKS}->{$key}; | |||||||||
| 73 | 0 | delete $self->{DATA}->{$key}; | |||||||||
| 74 | 0 | ▶ | $self->_recalculate() if($self->{RECALCULATE} eq 'onstore'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | } | ||||||||||
| 76 | 0 | ▶ | sub EXISTS { | ||||||||
|
Subroutine
EXISTS: not called CC=1 SCAR=6.9
| |||||||||||
| 77 | 0 | my $self = shift; | |||||||||
| 78 | 0 | my $key = shift; | |||||||||
| 79 | 0 | return exists($self->{DATA}->{$key}); | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | 1 | 1; | |||||||||
| 83 | __END__ | ||||||||||