| 1 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Hash::Ordered; | ||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A fast, pure-Perl ordered hash class | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | our $VERSION = '0.014'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | _DATA => 0, # unordered data | |||||||||||||||||||||||||||
| 14 | _KEYS => 1, # ordered keys | ||||||||||||||||||||||||||||
| 15 | _INDX => 2, # index into _KEYS (on demand) | ||||||||||||||||||||||||||||
| 16 | _OFFS => 3, # index offset for optimized shift/unshift | ||||||||||||||||||||||||||||
| 17 | _GCNT => 4, # garbage count | ||||||||||||||||||||||||||||
| 18 | _ITER => 5, # for tied hash support | ||||||||||||||||||||||||||||
| 19 | 3 | }; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 3 | _INDEX_THRESHOLD => 25, # max size before indexing/tombstone deletion | |||||||||||||||||||||||||||
| 23 | _TOMBSTONE => \1, # ref to arbitrary scalar | ||||||||||||||||||||||||||||
| 24 | 3 | }; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # 'overloading.pm' not available until 5.10.1 so emulate with Scalar::Util | ||||||||||||||||||||||||||||
| 27 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=4 SCAR=14.2
| |||||||||||||||||||||||||||||
| 28 | 3 | ▶ | if ( $] gt '5.010000' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | ## no critic | ||||||||||||||||||||||||||||
| 30 | 3 | ▶ | eval q{ | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
_numify: called CC=1 SCAR=0.0
Subroutine
_stringify: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | sub _stringify { no overloading; "$_[0]" } | ||||||||||||||||||||||||||||
| 32 | sub _numify { no overloading; 0+$_[0] } | ||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | 3 | ▶ | die $@ if $@; # uncoverable branch true | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | else { | ||||||||||||||||||||||||||||
| 37 | ## no critic | ||||||||||||||||||||||||||||
| 38 | 0 | eval q{ | |||||||||||||||||||||||||||
| 39 | require Scalar::Util; | ||||||||||||||||||||||||||||
| 40 | sub _stringify { sprintf("%s=ARRAY(0x%x)",ref($_[0]),Scalar::Util::refaddr($_[0])) } | ||||||||||||||||||||||||||||
| 41 | sub _numify { Scalar::Util::refaddr($_[0]) } | ||||||||||||||||||||||||||||
| 42 | }; | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | die $@ if $@; # uncoverable branch true | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use overload | ||||||||||||||||||||||||||||
| 48 | 3 | ▶ | q{""} => \&_stringify, | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 49 | q{0+} => \&_numify, | ||||||||||||||||||||||||||||
| 50 | 2 | ▶ | q{bool} => sub { !!scalar %{ $_[0]->[_DATA] } }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 3 | fallback => 1; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | #pod =method new | ||||||||||||||||||||||||||||
| 54 | #pod | ||||||||||||||||||||||||||||
| 55 | #pod $oh = Hash::Ordered->new; | ||||||||||||||||||||||||||||
| 56 | #pod $oh = Hash::Ordered->new( @pairs ); | ||||||||||||||||||||||||||||
| 57 | #pod | ||||||||||||||||||||||||||||
| 58 | #pod Constructs an object, with an optional list of key-value pairs. | ||||||||||||||||||||||||||||
| 59 | #pod | ||||||||||||||||||||||||||||
| 60 | #pod The position of a key corresponds to the first occurrence in the list, but | ||||||||||||||||||||||||||||
| 61 | #pod the value will be updated if the key is seen more than once. | ||||||||||||||||||||||||||||
| 62 | #pod | ||||||||||||||||||||||||||||
| 63 | #pod Current API available since 0.009. | ||||||||||||||||||||||||||||
| 64 | #pod | ||||||||||||||||||||||||||||
| 65 | #pod =cut | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 29 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 68 | 29 | my $class = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 29 | ▶ | Carp::croak("new() requires key-value pairs") unless @_ % 2 == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 28 | my ( %data, @keys, $k ); | |||||||||||||||||||||||||||
| 73 | 28 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | # must stringify keys for _KEYS array | ||||||||||||||||||||||||||||
| 75 | 3284 | $k = shift; | |||||||||||||||||||||||||||
| 76 | 3284 | ▶ | push @keys, "$k" unless exists $data{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 3284 | $data{$k} = shift; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | 28 | return bless [ \%data, \@keys, undef, 0, 0 ], $class; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | #pod =method clone | ||||||||||||||||||||||||||||
| 83 | #pod | ||||||||||||||||||||||||||||
| 84 | #pod $oh2 = $oh->clone; | ||||||||||||||||||||||||||||
| 85 | #pod $oh2 = $oh->clone( @keys ); | ||||||||||||||||||||||||||||
| 86 | #pod | ||||||||||||||||||||||||||||
| 87 | #pod Creates a shallow copy of an ordered hash object. If no arguments are | ||||||||||||||||||||||||||||
| 88 | #pod given, it produces an exact copy. If a list of keys is given, the new | ||||||||||||||||||||||||||||
| 89 | #pod object includes only those keys in the given order. Keys that aren't | ||||||||||||||||||||||||||||
| 90 | #pod in the original will have the value C<undef>. | ||||||||||||||||||||||||||||
| 91 | #pod | ||||||||||||||||||||||||||||
| 92 | #pod =cut | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 15 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 15 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 96 | 15 | my $clone; | |||||||||||||||||||||||||||
| 97 | 15 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 98 | 9 | my %subhash; | |||||||||||||||||||||||||||
| 99 | 9 | @subhash{@_} = @{ $self->[_DATA] }{@_}; | |||||||||||||||||||||||||||
| 100 | 9 | $clone = [ \%subhash, [ map "$_", @_ ], undef, 0, 0 ]; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | elsif ( $self->[_INDX] ) { | ||||||||||||||||||||||||||||
| 103 | $clone = | ||||||||||||||||||||||||||||
| 104 | 3 | [ { %{ $self->[_DATA] } }, [ grep !ref($_), @{ $self->[_KEYS] } ], undef, 0, 0 ]; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | else { | ||||||||||||||||||||||||||||
| 107 | $clone = | ||||||||||||||||||||||||||||
| 108 | 3 | [ { %{ $self->[_DATA] } }, [ @{ $self->[_KEYS] } ], undef, 0, 0 ]; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | 15 | return bless $clone, ref $self; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | #pod =method keys | ||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||
| 116 | #pod @keys = $oh->keys; | ||||||||||||||||||||||||||||
| 117 | #pod $size = $oh->keys; | ||||||||||||||||||||||||||||
| 118 | #pod | ||||||||||||||||||||||||||||
| 119 | #pod In list context, returns the ordered list of keys. In scalar context, returns | ||||||||||||||||||||||||||||
| 120 | #pod the number of elements. | ||||||||||||||||||||||||||||
| 121 | #pod | ||||||||||||||||||||||||||||
| 122 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 123 | #pod | ||||||||||||||||||||||||||||
| 124 | #pod =cut | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 27 | ▶ | sub keys { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 27 | my ($self) = @_; | |||||||||||||||||||||||||||
| 128 | return wantarray | ||||||||||||||||||||||||||||
| 129 | 25 | ? ( grep !ref($_), @{ $self->[_KEYS] } ) | |||||||||||||||||||||||||||
| 130 | 27 | ▶ | : @{ $self->[_KEYS] } - $self->[_GCNT]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | #pod =method values | ||||||||||||||||||||||||||||
| 134 | #pod | ||||||||||||||||||||||||||||
| 135 | #pod @values = $oh->values; | ||||||||||||||||||||||||||||
| 136 | #pod @values = $oh->values( @keys ); | ||||||||||||||||||||||||||||
| 137 | #pod | ||||||||||||||||||||||||||||
| 138 | #pod Returns an ordered list of values. If no arguments are given, returns | ||||||||||||||||||||||||||||
| 139 | #pod the ordered values of the entire hash. If a list of keys is given, returns | ||||||||||||||||||||||||||||
| 140 | #pod values in order corresponding to those keys. If a key does not exist, C<undef> | ||||||||||||||||||||||||||||
| 141 | #pod will be returned for that value. | ||||||||||||||||||||||||||||
| 142 | #pod | ||||||||||||||||||||||||||||
| 143 | #pod In scalar context, returns the number of elements. | ||||||||||||||||||||||||||||
| 144 | #pod | ||||||||||||||||||||||||||||
| 145 | #pod Current API available since 0.006. | ||||||||||||||||||||||||||||
| 146 | #pod | ||||||||||||||||||||||||||||
| 147 | #pod =cut | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 16 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | 16 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 151 | return | ||||||||||||||||||||||||||||
| 152 | wantarray | ||||||||||||||||||||||||||||
| 153 | 3049 | ▶ | ? ( map { $self->[_DATA]{$_} } ( @_ ? @_ : grep !ref($_), @{ $self->[_KEYS] } ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 16 | ▶ | : @{ $self->[_KEYS] } - $self->[_GCNT]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | #pod =method get | ||||||||||||||||||||||||||||
| 158 | #pod | ||||||||||||||||||||||||||||
| 159 | #pod $value = $oh->get("some key"); | ||||||||||||||||||||||||||||
| 160 | #pod | ||||||||||||||||||||||||||||
| 161 | #pod Returns the value associated with the key, or C<undef> if it does not exist in | ||||||||||||||||||||||||||||
| 162 | #pod the hash. | ||||||||||||||||||||||||||||
| 163 | #pod | ||||||||||||||||||||||||||||
| 164 | #pod =cut | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 1068 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 167 | 1068 | return $_[0]->[_DATA]{ $_[1] }; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | #pod =method set | ||||||||||||||||||||||||||||
| 171 | #pod | ||||||||||||||||||||||||||||
| 172 | #pod $oh->set("some key" => "some value"); | ||||||||||||||||||||||||||||
| 173 | #pod | ||||||||||||||||||||||||||||
| 174 | #pod Associates a value with a key and returns the value. If the key does not | ||||||||||||||||||||||||||||
| 175 | #pod already exist in the hash, it will be added at the end. | ||||||||||||||||||||||||||||
| 176 | #pod | ||||||||||||||||||||||||||||
| 177 | #pod =cut | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 537 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 537 | my ( $self, $key ) = @_; # don't copy $_[2] in case it's large | |||||||||||||||||||||||||||
| 181 | 537 | ▶ | if ( !exists $self->[_DATA]{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 324 | my $keys = $self->[_KEYS]; | |||||||||||||||||||||||||||
| 183 | 324 | ▶ | if ( my $indx = $self->[_INDX] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | 4 | ▶ | $indx->{$key} = @$keys ? $indx->{ $keys->[-1] } + 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | 324 | CORE::push @{ $self->[_KEYS] }, "$key"; # stringify key | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 537 | return $self->[_DATA]{$key} = $_[2]; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | #pod =method exists | ||||||||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||||||||
| 193 | #pod if ( $oh->exists("some key") ) { ... } | ||||||||||||||||||||||||||||
| 194 | #pod | ||||||||||||||||||||||||||||
| 195 | #pod Test if some key exists in the hash (without creating it). | ||||||||||||||||||||||||||||
| 196 | #pod | ||||||||||||||||||||||||||||
| 197 | #pod =cut | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | 6 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 200 | 6 | return exists $_[0]->[_DATA]{ $_[1] }; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | #pod =method delete | ||||||||||||||||||||||||||||
| 204 | #pod | ||||||||||||||||||||||||||||
| 205 | #pod $value = $oh->delete("some key"); | ||||||||||||||||||||||||||||
| 206 | #pod | ||||||||||||||||||||||||||||
| 207 | #pod Removes a key-value pair from the hash and returns the value. | ||||||||||||||||||||||||||||
| 208 | #pod | ||||||||||||||||||||||||||||
| 209 | #pod =cut | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 468 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=13 SCAR=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 468 | my ( $self, $key ) = @_; | |||||||||||||||||||||||||||
| 213 | 468 | ▶ | if ( exists $self->[_DATA]{$key} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 466 | my $keys = $self->[_KEYS]; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # JIT an index if hash is "large" | ||||||||||||||||||||||||||||
| 217 | 466 | ▶ | if ( !$self->[_INDX] && @$keys > _INDEX_THRESHOLD ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->[2] and @$keys > 25
MC/DC100%2/2
not $self->[2] and @$keys > 25
not $self->[2]
@$keys > 25
Truth table100%3/3
not(A) and B
A= not $self->[2]B= @$keys > 25
| |||||||||||||||||||||||||||||
| 218 | 12 | my %indx; | |||||||||||||||||||||||||||
| 219 | 12 | $indx{ $keys->[$_] } = $_ for 0 .. $#{$keys}; | |||||||||||||||||||||||||||
| 220 | 12 | $self->[_INDX] = \%indx; | |||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 466 | ▶ | if ( $self->[_INDX] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | # tombstone | ||||||||||||||||||||||||||||
| 226 | 354 | $keys->[ delete( $self->[_INDX]{$key} ) + $self->[_OFFS] ] = _TOMBSTONE; | |||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | # GC keys and remove index if more than half keys are tombstone. | ||||||||||||||||||||||||||||
| 229 | # Index will be recreated if needed on next delete | ||||||||||||||||||||||||||||
| 230 | 354 | ▶ | if ( ++$self->[_GCNT] > @$keys / 2 ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 231 | 4 | @{ $self->[_KEYS] } = grep !ref($_), @{ $self->[_KEYS] }; | |||||||||||||||||||||||||||
| 232 | 4 | $self->[_INDX] = undef; | |||||||||||||||||||||||||||
| 233 | 4 | $self->[_OFFS] = 0; | |||||||||||||||||||||||||||
| 234 | 4 | $self->[_GCNT] = 0; | |||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | # or maybe garbage collect start of list | ||||||||||||||||||||||||||||
| 237 | elsif ( ref( $keys->[0] ) ) { | ||||||||||||||||||||||||||||
| 238 | 182 | my $i = 0; | |||||||||||||||||||||||||||
| 239 | 182 | ▶ | $i++ while ref( $keys->[$i] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 182 | splice @$keys, 0, $i; | |||||||||||||||||||||||||||
| 241 | 182 | $self->[_GCNT] -= $i; | |||||||||||||||||||||||||||
| 242 | 182 | $self->[_OFFS] -= $i; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | # or maybe garbage collect end of list | ||||||||||||||||||||||||||||
| 245 | elsif ( ref( $keys->[-1] ) ) { | ||||||||||||||||||||||||||||
| 246 | 83 | my $i = $#{$keys}; | |||||||||||||||||||||||||||
| 247 | 83 | ▶ | $i-- while ref( $keys->[$i] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | 83 | $self->[_GCNT] -= $#{$keys} - $i; | |||||||||||||||||||||||||||
| 249 | 83 | splice @$keys, $i + 1; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||
| 252 | else { | ||||||||||||||||||||||||||||
| 253 | 112 | my $i; | |||||||||||||||||||||||||||
| 254 | 112 | for ( 0 .. $#{$keys} ) { | |||||||||||||||||||||||||||
| 255 | 800 | ▶ | if ( $keys->[$_] eq $key ) { $i = $_; last; } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | 112 | splice @$keys, $i, 1; | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 466 | return delete $self->[_DATA]{$key}; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | 2 | return undef; ## no critic | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | #pod =method clear | ||||||||||||||||||||||||||||
| 266 | #pod | ||||||||||||||||||||||||||||
| 267 | #pod $oh->clear; | ||||||||||||||||||||||||||||
| 268 | #pod | ||||||||||||||||||||||||||||
| 269 | #pod Removes all key-value pairs from the hash. Returns undef in scalar context | ||||||||||||||||||||||||||||
| 270 | #pod or an empty list in list context. | ||||||||||||||||||||||||||||
| 271 | #pod | ||||||||||||||||||||||||||||
| 272 | #pod Current API available since 0.003. | ||||||||||||||||||||||||||||
| 273 | #pod | ||||||||||||||||||||||||||||
| 274 | #pod =cut | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 20 | ▶ | sub clear { | ||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 277 | 20 | my ($self) = @_; | |||||||||||||||||||||||||||
| 278 | 20 | @$self = ( {}, [], undef, 0, 0 ); | |||||||||||||||||||||||||||
| 279 | 20 | return; | |||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | #pod =method push | ||||||||||||||||||||||||||||
| 283 | #pod | ||||||||||||||||||||||||||||
| 284 | #pod $oh->push( one => 1, two => 2); | ||||||||||||||||||||||||||||
| 285 | #pod | ||||||||||||||||||||||||||||
| 286 | #pod Add a list of key-value pairs to the end of the ordered hash. If a key already | ||||||||||||||||||||||||||||
| 287 | #pod exists in the hash, it will be deleted and re-inserted at the end with the new | ||||||||||||||||||||||||||||
| 288 | #pod value. | ||||||||||||||||||||||||||||
| 289 | #pod | ||||||||||||||||||||||||||||
| 290 | #pod Returns the number of keys after the push is complete. | ||||||||||||||||||||||||||||
| 291 | #pod | ||||||||||||||||||||||||||||
| 292 | #pod =cut | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 237 | ▶ | sub push { | ||||||||||||||||||||||||||
|
Subroutine
push: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 295 | 237 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 296 | 237 | my ( $data, $keys ) = @$self; | |||||||||||||||||||||||||||
| 297 | 237 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 298 | 245 | my ( $k, $v ) = splice( @_, 0, 2 ); | |||||||||||||||||||||||||||
| 299 | 245 | ▶ | $self->delete($k) if exists $data->{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 300 | 245 | $data->{$k} = $v; | |||||||||||||||||||||||||||
| 301 | 245 | ▶ | if ( my $indx = $self->[_INDX] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 302 | 115 | ▶ | $indx->{$k} = @$keys ? $indx->{ $keys->[-1] } + 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | 245 | CORE::push @$keys, "$k"; # stringify keys | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | 237 | return @$keys - $self->[_GCNT]; | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | #pod =method pop | ||||||||||||||||||||||||||||
| 310 | #pod | ||||||||||||||||||||||||||||
| 311 | #pod ($key, $value) = $oh->pop; | ||||||||||||||||||||||||||||
| 312 | #pod $value = $oh->pop; | ||||||||||||||||||||||||||||
| 313 | #pod | ||||||||||||||||||||||||||||
| 314 | #pod Removes and returns the last key-value pair in the ordered hash. | ||||||||||||||||||||||||||||
| 315 | #pod In scalar context, only the value is returned. If the hash is empty, | ||||||||||||||||||||||||||||
| 316 | #pod the returned key and value will be C<undef>. | ||||||||||||||||||||||||||||
| 317 | #pod | ||||||||||||||||||||||||||||
| 318 | #pod =cut | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | 1028 | ▶ | sub pop { | ||||||||||||||||||||||||||
|
Subroutine
pop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 321 | 1028 | my ($self) = @_; | |||||||||||||||||||||||||||
| 322 | 1028 | ▶ | if ( $self->[_INDX] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 323 | 2 | my $key = $self->[_KEYS][-1]; | |||||||||||||||||||||||||||
| 324 | 2 | return $key, $self->delete($key); | |||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||
| 326 | else { | ||||||||||||||||||||||||||||
| 327 | 1026 | my $key = CORE::pop @{ $self->[_KEYS] }; | |||||||||||||||||||||||||||
| 328 | 1026 | ▶ | return defined($key) ? ( $key, delete $self->[_DATA]{$key} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | #pod =method unshift | ||||||||||||||||||||||||||||
| 333 | #pod | ||||||||||||||||||||||||||||
| 334 | #pod $oh->unshift( one => 1, two => 2 ); | ||||||||||||||||||||||||||||
| 335 | #pod | ||||||||||||||||||||||||||||
| 336 | #pod Adds a list of key-value pairs to the beginning of the ordered hash. If a key | ||||||||||||||||||||||||||||
| 337 | #pod already exists, it will be deleted and re-inserted at the beginning with the | ||||||||||||||||||||||||||||
| 338 | #pod new value. | ||||||||||||||||||||||||||||
| 339 | #pod | ||||||||||||||||||||||||||||
| 340 | #pod Returns the number of keys after the unshift is complete. | ||||||||||||||||||||||||||||
| 341 | #pod | ||||||||||||||||||||||||||||
| 342 | #pod =cut | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | 215 | ▶ | sub unshift { | ||||||||||||||||||||||||||
|
Subroutine
unshift: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 345 | 215 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 346 | 215 | my ( $data, $keys ) = @$self; | |||||||||||||||||||||||||||
| 347 | 215 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 226 | my ( $k, $v ) = splice( @_, -2, 2 ); | |||||||||||||||||||||||||||
| 349 | 226 | ▶ | $self->delete($k) if exists $data->{$k}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 226 | $data->{$k} = $v; | |||||||||||||||||||||||||||
| 351 | 226 | CORE::unshift @$keys, "$k"; # stringify keys | |||||||||||||||||||||||||||
| 352 | 226 | ▶ | $self->[_INDX]{$k} = -( ++$self->[_OFFS] ) if $self->[_INDX]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||
| 354 | 215 | return @$keys - $self->[_GCNT]; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | #pod =method shift | ||||||||||||||||||||||||||||
| 358 | #pod | ||||||||||||||||||||||||||||
| 359 | #pod ($key, $value) = $oh->shift; | ||||||||||||||||||||||||||||
| 360 | #pod $value = $oh->shift; | ||||||||||||||||||||||||||||
| 361 | #pod | ||||||||||||||||||||||||||||
| 362 | #pod Removes and returns the first key-value pair in the ordered hash. | ||||||||||||||||||||||||||||
| 363 | #pod In scalar context, only the value is returned. If the hash is empty, | ||||||||||||||||||||||||||||
| 364 | #pod the returned key and value will be C<undef>. | ||||||||||||||||||||||||||||
| 365 | #pod | ||||||||||||||||||||||||||||
| 366 | #pod =cut | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | 1028 | ▶ | sub shift { | ||||||||||||||||||||||||||
|
Subroutine
shift: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 369 | 1028 | my ($self) = @_; | |||||||||||||||||||||||||||
| 370 | 1028 | ▶ | if ( $self->[_INDX] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 371 | 2 | my $key = $self->[_KEYS][0]; | |||||||||||||||||||||||||||
| 372 | 2 | return $key, $self->delete($key); | |||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | else { | ||||||||||||||||||||||||||||
| 375 | 1026 | my $key = CORE::shift @{ $self->[_KEYS] }; | |||||||||||||||||||||||||||
| 376 | 1026 | ▶ | return defined($key) ? ( $key, delete $self->[_DATA]{$key} ) : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | #pod =method merge | ||||||||||||||||||||||||||||
| 381 | #pod | ||||||||||||||||||||||||||||
| 382 | #pod $oh->merge( one => 1, two => 2 ); | ||||||||||||||||||||||||||||
| 383 | #pod | ||||||||||||||||||||||||||||
| 384 | #pod Merges a list of key-value pairs into the ordered hash. If a key already | ||||||||||||||||||||||||||||
| 385 | #pod exists, its value is replaced. Otherwise, the key-value pair is added at | ||||||||||||||||||||||||||||
| 386 | #pod the end of the hash. | ||||||||||||||||||||||||||||
| 387 | #pod | ||||||||||||||||||||||||||||
| 388 | #pod =cut | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 2 | ▶ | sub merge { | ||||||||||||||||||||||||||
|
Subroutine
merge: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 391 | 2 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 392 | 2 | ▶ | while (@_) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | 6 | my ( $k, $v ) = splice( @_, 0, 2 ); | |||||||||||||||||||||||||||
| 394 | 6 | ▶ | if ( !exists $self->[_DATA]{$k} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 4 | my $size = CORE::push @{ $self->[_KEYS] }, "$k"; # stringify key | |||||||||||||||||||||||||||
| 396 | 4 | ▶ | $self->[_INDX]{$k} = $size - 1 if $self->[_INDX]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | 6 | $self->[_DATA]{$k} = $v; | |||||||||||||||||||||||||||
| 399 | } | ||||||||||||||||||||||||||||
| 400 | 2 | return @{ $self->[_KEYS] } - $self->[_GCNT]; | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | #pod =method as_list | ||||||||||||||||||||||||||||
| 404 | #pod | ||||||||||||||||||||||||||||
| 405 | #pod @pairs = $oh->as_list; | ||||||||||||||||||||||||||||
| 406 | #pod @pairs = $oh->as_list( @keys ); | ||||||||||||||||||||||||||||
| 407 | #pod | ||||||||||||||||||||||||||||
| 408 | #pod Returns an ordered list of key-value pairs. If no arguments are given, all | ||||||||||||||||||||||||||||
| 409 | #pod pairs in the hash are returned. If a list of keys is given, the returned list | ||||||||||||||||||||||||||||
| 410 | #pod includes only those key-value pairs in the given order. Keys that aren't in | ||||||||||||||||||||||||||||
| 411 | #pod the original will have the value C<undef>. | ||||||||||||||||||||||||||||
| 412 | #pod | ||||||||||||||||||||||||||||
| 413 | #pod =cut | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 34 | ▶ | sub as_list { | ||||||||||||||||||||||||||
|
Subroutine
as_list: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 416 | 34 | my $self = CORE::shift; | |||||||||||||||||||||||||||
| 417 | return | ||||||||||||||||||||||||||||
| 418 | 12180 | ▶ | map { ; $_ => $self->[_DATA]{$_} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | 34 | ( @_ ? @_ : grep !ref($_), @{ $self->[_KEYS] } ); | |||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||
| 422 | #pod =method iterator | ||||||||||||||||||||||||||||
| 423 | #pod | ||||||||||||||||||||||||||||
| 424 | #pod $iter = $oh->iterator; | ||||||||||||||||||||||||||||
| 425 | #pod $iter = $oh->iterator( reverse $oh->keys ); # reverse | ||||||||||||||||||||||||||||
| 426 | #pod | ||||||||||||||||||||||||||||
| 427 | #pod while ( my ($key,$value) = $iter->() ) { ... } | ||||||||||||||||||||||||||||
| 428 | #pod | ||||||||||||||||||||||||||||
| 429 | #pod Returns a code reference that returns a single key-value pair (in order) on | ||||||||||||||||||||||||||||
| 430 | #pod each invocation, or the empty list if all keys are visited. | ||||||||||||||||||||||||||||
| 431 | #pod | ||||||||||||||||||||||||||||
| 432 | #pod If no arguments are given, the iterator walks the entire hash in order. If a | ||||||||||||||||||||||||||||
| 433 | #pod list of keys is provided, the iterator walks the hash in that order. Unknown | ||||||||||||||||||||||||||||
| 434 | #pod keys will return C<undef>. | ||||||||||||||||||||||||||||
| 435 | #pod | ||||||||||||||||||||||||||||
| 436 | #pod The list of keys to return is set when the iterator is generator. Keys added | ||||||||||||||||||||||||||||
| 437 | #pod later will not be returned. Subsequently deleted keys will return C<undef> | ||||||||||||||||||||||||||||
| 438 | #pod for the value. | ||||||||||||||||||||||||||||
| 439 | #pod | ||||||||||||||||||||||||||||
| 440 | #pod =cut | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | # usually we avoid copying keys in @_; here we must for the closure | ||||||||||||||||||||||||||||
| 443 | 2 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 444 | 2 | my ( $self, @keys ) = @_; | |||||||||||||||||||||||||||
| 445 | 2 | ▶ | @keys = grep !ref($_), @{ $self->[_KEYS] } unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | 2 | my $data = $self->[_DATA]; | |||||||||||||||||||||||||||
| 447 | 28 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 448 | 28 | ▶ | return unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 449 | 26 | my $key = CORE::shift(@keys); | |||||||||||||||||||||||||||
| 450 | 26 | return ( $key => $data->{$key} ); | |||||||||||||||||||||||||||
| 451 | 2 | }; | |||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | #pod =method preinc | ||||||||||||||||||||||||||||
| 455 | #pod | ||||||||||||||||||||||||||||
| 456 | #pod $oh->preinc($key); # like ++$hash{$key} | ||||||||||||||||||||||||||||
| 457 | #pod | ||||||||||||||||||||||||||||
| 458 | #pod This method is sugar for incrementing a key without having to call C<set> and | ||||||||||||||||||||||||||||
| 459 | #pod C<get> explicitly. It returns the new value. | ||||||||||||||||||||||||||||
| 460 | #pod | ||||||||||||||||||||||||||||
| 461 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 462 | #pod | ||||||||||||||||||||||||||||
| 463 | #pod =cut | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | 1 | ▶ | sub preinc { | ||||||||||||||||||||||||||
|
Subroutine
preinc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 466 | 1 | return ++$_[0]->[_DATA]{ $_[1] }; | |||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | #pod =method postinc | ||||||||||||||||||||||||||||
| 470 | #pod | ||||||||||||||||||||||||||||
| 471 | #pod $oh->postinc($key); # like $hash{$key}++ | ||||||||||||||||||||||||||||
| 472 | #pod | ||||||||||||||||||||||||||||
| 473 | #pod This method is sugar for incrementing a key without having to call C<set> and | ||||||||||||||||||||||||||||
| 474 | #pod C<get> explicitly. It returns the old value. | ||||||||||||||||||||||||||||
| 475 | #pod | ||||||||||||||||||||||||||||
| 476 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 477 | #pod | ||||||||||||||||||||||||||||
| 478 | #pod =cut | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 1 | ▶ | sub postinc { | ||||||||||||||||||||||||||
|
Subroutine
postinc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 481 | 1 | return $_[0]->[_DATA]{ $_[1] }++; | |||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||
| 484 | #pod =method predec | ||||||||||||||||||||||||||||
| 485 | #pod | ||||||||||||||||||||||||||||
| 486 | #pod $oh->predec($key); # like --$hash{$key} | ||||||||||||||||||||||||||||
| 487 | #pod | ||||||||||||||||||||||||||||
| 488 | #pod This method is sugar for decrementing a key without having to call C<set> and | ||||||||||||||||||||||||||||
| 489 | #pod C<get> explicitly. It returns the new value. | ||||||||||||||||||||||||||||
| 490 | #pod | ||||||||||||||||||||||||||||
| 491 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 492 | #pod | ||||||||||||||||||||||||||||
| 493 | #pod =cut | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 1 | ▶ | sub predec { | ||||||||||||||||||||||||||
|
Subroutine
predec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 1 | return --$_[0]->[_DATA]{ $_[1] }; | |||||||||||||||||||||||||||
| 497 | } | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | #pod =method postdec | ||||||||||||||||||||||||||||
| 500 | #pod | ||||||||||||||||||||||||||||
| 501 | #pod $oh->postdec($key); # like $hash{$key}-- | ||||||||||||||||||||||||||||
| 502 | #pod | ||||||||||||||||||||||||||||
| 503 | #pod This method is sugar for decrementing a key without having to call C<set> and | ||||||||||||||||||||||||||||
| 504 | #pod C<get> explicitly. It returns the old value. | ||||||||||||||||||||||||||||
| 505 | #pod | ||||||||||||||||||||||||||||
| 506 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 507 | #pod | ||||||||||||||||||||||||||||
| 508 | #pod =cut | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | 1 | ▶ | sub postdec { | ||||||||||||||||||||||||||
|
Subroutine
postdec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 511 | 1 | return $_[0]->[_DATA]{ $_[1] }--; | |||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | #pod =method add | ||||||||||||||||||||||||||||
| 515 | #pod | ||||||||||||||||||||||||||||
| 516 | #pod $oh->add($key, $n); # like $hash{$key} += $n | ||||||||||||||||||||||||||||
| 517 | #pod | ||||||||||||||||||||||||||||
| 518 | #pod This method is sugar for adding a value to a key without having to call | ||||||||||||||||||||||||||||
| 519 | #pod C<set> and C<get> explicitly. With no value to add, it is treated as "0". | ||||||||||||||||||||||||||||
| 520 | #pod It returns the new value. | ||||||||||||||||||||||||||||
| 521 | #pod | ||||||||||||||||||||||||||||
| 522 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 523 | #pod | ||||||||||||||||||||||||||||
| 524 | #pod =cut | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | 2 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 527 | 2 | ▶ | return $_[0]->[_DATA]{ $_[1] } += $_[2] || 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_[2] || 0
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table50%1/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | #pod =method subtract | ||||||||||||||||||||||||||||
| 531 | #pod | ||||||||||||||||||||||||||||
| 532 | #pod $oh->subtract($key, $n); # like $hash{$key} -= $n | ||||||||||||||||||||||||||||
| 533 | #pod | ||||||||||||||||||||||||||||
| 534 | #pod This method is sugar for subtracting a value from a key without having to call | ||||||||||||||||||||||||||||
| 535 | #pod C<set> and C<get> explicitly. With no value to subtract, it is treated as "0". | ||||||||||||||||||||||||||||
| 536 | #pod It returns the new value. | ||||||||||||||||||||||||||||
| 537 | #pod | ||||||||||||||||||||||||||||
| 538 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 539 | #pod | ||||||||||||||||||||||||||||
| 540 | #pod =cut | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | 0 | ▶ | sub subtract { | ||||||||||||||||||||||||||
|
Subroutine
subtract: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 543 | 0 | ▶ | return $_[0]->[_DATA]{ $_[1] } -= $_[2] || 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$_[2] || 0
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
| |||||||||||||||||||||||||||||
| 544 | } | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | #pod =method concat | ||||||||||||||||||||||||||||
| 547 | #pod | ||||||||||||||||||||||||||||
| 548 | #pod $oh->concat($key, $str); # like $hash{$key} .= $str | ||||||||||||||||||||||||||||
| 549 | #pod | ||||||||||||||||||||||||||||
| 550 | #pod This method is sugar for concatenating a string onto the value of a key without | ||||||||||||||||||||||||||||
| 551 | #pod having to call C<set> and C<get> explicitly. It returns the new value. If the | ||||||||||||||||||||||||||||
| 552 | #pod value to append is not defined, no concatenation is done and no warning is | ||||||||||||||||||||||||||||
| 553 | #pod given. | ||||||||||||||||||||||||||||
| 554 | #pod | ||||||||||||||||||||||||||||
| 555 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 556 | #pod | ||||||||||||||||||||||||||||
| 557 | #pod =cut | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | 2 | ▶ | sub concat { | ||||||||||||||||||||||||||
|
Subroutine
concat: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 560 | 2 | ▶ | if ( defined $_[2] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | 1 | return $_[0]->[_DATA]{ $_[1] } .= $_[2]; | |||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | else { | ||||||||||||||||||||||||||||
| 564 | 1 | return $_[0]->[_DATA]{ $_[1] }; | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | #pod =method or_equals | ||||||||||||||||||||||||||||
| 569 | #pod | ||||||||||||||||||||||||||||
| 570 | #pod $oh->or_equals($key, $str); # like $hash{$key} ||= $str | ||||||||||||||||||||||||||||
| 571 | #pod | ||||||||||||||||||||||||||||
| 572 | #pod This method is sugar for assigning to a key if the existing value is false | ||||||||||||||||||||||||||||
| 573 | #pod without having to call C<set> and C<get> explicitly. It returns the new value. | ||||||||||||||||||||||||||||
| 574 | #pod | ||||||||||||||||||||||||||||
| 575 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 576 | #pod | ||||||||||||||||||||||||||||
| 577 | #pod =cut | ||||||||||||||||||||||||||||
| 578 | |||||||||||||||||||||||||||||
| 579 | 5 | ▶ | sub or_equals { | ||||||||||||||||||||||||||
|
Subroutine
or_equals: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 580 | 5 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 581 | |||||||||||||||||||||||||||||
| 582 | 5 | ▶ | if ( my $val = $self->get($key) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 583 | 2 | return $val; | |||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | 3 | return $self->set($key,$_[2]); | |||||||||||||||||||||||||||
| 587 | } | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | #pod =method dor_equals | ||||||||||||||||||||||||||||
| 590 | #pod | ||||||||||||||||||||||||||||
| 591 | #pod $oh->dor_equals($key, $str); # like $hash{$key} //= $str | ||||||||||||||||||||||||||||
| 592 | #pod | ||||||||||||||||||||||||||||
| 593 | #pod This method is sugar for assigning to a key if the existing value is not | ||||||||||||||||||||||||||||
| 594 | #pod defined without having to call C<set> and C<get> explicitly. It returns the new | ||||||||||||||||||||||||||||
| 595 | #pod value. | ||||||||||||||||||||||||||||
| 596 | #pod | ||||||||||||||||||||||||||||
| 597 | #pod Current API available since 0.005. | ||||||||||||||||||||||||||||
| 598 | #pod | ||||||||||||||||||||||||||||
| 599 | #pod =cut | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | 5 | ▶ | sub dor_equals { | ||||||||||||||||||||||||||
|
Subroutine
dor_equals: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 602 | 5 | my ($self,$key) = @_; | |||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | 5 | ▶ | if ( defined( my $val = $self->get($key) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 605 | 3 | return $val; | |||||||||||||||||||||||||||
| 606 | } | ||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||
| 608 | 2 | return $self->set($key,$_[2]); | |||||||||||||||||||||||||||
| 609 | } | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 612 | # tied hash support -- slower, but I maybe some thing are more succinct | ||||||||||||||||||||||||||||
| 613 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | { | ||||||||||||||||||||||||||||
| 616 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | 3 | *{ __PACKAGE__ . '::TIEHASH' } = \&new; | |||||||||||||||||||||||||||
| 619 | 3 | *{ __PACKAGE__ . '::STORE' } = \&set; | |||||||||||||||||||||||||||
| 620 | 3 | *{ __PACKAGE__ . '::FETCH' } = \&get; | |||||||||||||||||||||||||||
| 621 | 3 | *{ __PACKAGE__ . '::EXISTS' } = \&exists; | |||||||||||||||||||||||||||
| 622 | 3 | *{ __PACKAGE__ . '::DELETE' } = \&delete; | |||||||||||||||||||||||||||
| 623 | 3 | *{ __PACKAGE__ . '::CLEAR' } = \&clear; | |||||||||||||||||||||||||||
| 624 | } | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | 3 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 627 | 3 | my ($self) = @_; | |||||||||||||||||||||||||||
| 628 | 3 | my @keys = grep !ref($_), @{ $self->[_KEYS] }; | |||||||||||||||||||||||||||
| 629 | 44 | ▶ | $self->[_ITER] = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 630 | 44 | ▶ | return unless @keys; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | 41 | return CORE::shift(@keys); | |||||||||||||||||||||||||||
| 632 | 3 | }; | |||||||||||||||||||||||||||
| 633 | 3 | return $self->[_ITER]->(); | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 41 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=2 SCAR=7.6
| |||||||||||||||||||||||||||||
| 637 | 41 | ▶ | return defined( $_[0]->[_ITER] ) ? $_[0]->[_ITER]->() : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | 2 | ▶ | sub SCALAR { | ||||||||||||||||||||||||||
|
Subroutine
SCALAR: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 641 | 2 | return scalar %{ $_[0]->[_DATA] }; | |||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | 3 | 1; | |||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | # vim: ts=4 sts=4 sw=4 et: | ||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||
| 649 | __END__ | ||||||||||||||||||||||||||||