| 1 | # ============================================================================= | ||||||||||||||||||||||||||||
| 2 | package Tie::TimeSeries; | ||||||||||||||||||||||||||||
| 3 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 4 | 4 | $Tie::TimeSeries::VERSION = '0.01'; | |||||||||||||||||||||||||||
| 5 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | Tie::TimeSeries - Convenient hash tyng for time series data. | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use Tie::TimeSeries; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Using with tie() | ||||||||||||||||||||||||||||
| 19 | tie %data, 'Tie::TimeSeries'; | ||||||||||||||||||||||||||||
| 20 | $data{ time() } = [ 0.14, 0.06, 0.01 ]; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | %data = (); | ||||||||||||||||||||||||||||
| 23 | $data{1361970900} = 10; | ||||||||||||||||||||||||||||
| 24 | $data{1361971200} = 20; | ||||||||||||||||||||||||||||
| 25 | $data{1361971500} = 30; | ||||||||||||||||||||||||||||
| 26 | $,=','; | ||||||||||||||||||||||||||||
| 27 | print values %data; # 10,20,30 | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | $data{1361971050} = 5; | ||||||||||||||||||||||||||||
| 30 | print values %data; # 10,5,20,30 | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # OO | ||||||||||||||||||||||||||||
| 33 | $data = Tie::TimeSeries->new( | ||||||||||||||||||||||||||||
| 34 | 1361970900 => 10, | ||||||||||||||||||||||||||||
| 35 | 1361971200 => 20, | ||||||||||||||||||||||||||||
| 36 | 1361971500 => 30, | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | print $data->values(); # 10,20,30 | ||||||||||||||||||||||||||||
| 39 | $data->store( 1361971050 => 15 ); | ||||||||||||||||||||||||||||
| 40 | print $data->values(); # 15,20,30 | ||||||||||||||||||||||||||||
| 41 | $data->store( 1361971800 => 40 ); | ||||||||||||||||||||||||||||
| 42 | print $data->values(); # 15,20,30,40 | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | When using time series data like throughput, statistics or so, this module is | ||||||||||||||||||||||||||||
| 48 | convenient that key will be sorted automatically. | ||||||||||||||||||||||||||||
| 49 | And this also is able to provide instance for OO using. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | # ============================================================================= | ||||||||||||||||||||||||||||
| 55 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | use Search::Binary; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 4 | ▶ | use base qw/ Tie::Hash /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # ============================================================================= | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 STANDARD TIE HASH | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | There are standard interfaces usage with tie(). | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | tie %yourhash, 'Tie::TimeSeries' [, $time => $value [, ... ]] | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | On this way, use C<%yourhash> as regular Perl hashes, but keys and values will be stored in order internally. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | With several arguments given, initialization stores them. C<$time> must be | ||||||||||||||||||||||||||||
| 71 | integer number. C<$value> can be any type of scalar. | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 76 | 20 | ▶ | sub TIEHASH { | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 20 | my $class = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 20 | my $self = { | |||||||||||||||||||||||||||
| 80 | h => {}, # Hash for fetching which has same data of $self->{d} has. | ||||||||||||||||||||||||||||
| 81 | t => [], # Array of key means time; [ t1, t2, ..., tx ] | ||||||||||||||||||||||||||||
| 82 | d => [], # Array of data warpped array;[ [v1],[v2], ..., [vx] ] | ||||||||||||||||||||||||||||
| 83 | c => 0, # for each() | ||||||||||||||||||||||||||||
| 84 | _i => 0, # for _readindex(), last read index | ||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 20 | bless $self, $class; | |||||||||||||||||||||||||||
| 88 | 20 | ▶ | while ( @_ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 8267 | $self->STORE( shift, shift ); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 20 | return $self; | |||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 8281 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||
| 96 | 8281 | my ($self, $key) = (@_); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 8281 | ▶ | return (defined $key && CORE::exists($self->{h}{$key}))? $self->{h}{$key}->[0]: undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and exists $self->{"h"}{$key}
MC/DC50%1/2
defined $key and exists $self->{"h"}{$key}
defined $key
exists $self->{"h"}{$key}
Truth table66%2/3
A and B
A= defined $keyB= exists $self->{"h"}{$key}
| |||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8287 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 103 | 8287 | my ($self, $key, $val) = (@_); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Validation | ||||||||||||||||||||||||||||
| 106 | 8287 | ▶ | unless ( defined($key) && $key =~ /^\d+$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $key and $key =~ /^\d+$/
MC/DC50%1/2
defined $key and $key =~ /^\d+$/
defined $key
$key =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $keyB= $key =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 107 | 4 | carp("Not a number given as key of hash."); | |||||||||||||||||||||||||||
| 108 | 4 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # Storing | ||||||||||||||||||||||||||||
| 112 | 8283 | ▶ | if ( CORE::exists $self->{h}{$key} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 10 | push @{ $self->{h}{$key} }, $val; | |||||||||||||||||||||||||||
| 114 | 10 | shift @{ $self->{h}{$key} }; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | } else { | ||||||||||||||||||||||||||||
| 117 | 8273 | my $d = [ $val ]; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # In the case of inserting just ordered data by number as key, | ||||||||||||||||||||||||||||
| 120 | # this cheking will be effective... | ||||||||||||||||||||||||||||
| 121 | 8273 | my $nums = $#{$self->{t}}; | |||||||||||||||||||||||||||
| 122 | 8273 | ▶ | if ( $nums >= 0 && $key > $self->{t}[ $nums ] ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$nums >= 0 and $key > $self->{"t"}[$nums]
MC/DC100%2/2
$nums >= 0 and $key > $self->{"t"}[$nums]
$nums >= 0
$key > $self->{"t"}[$nums]
Truth table100%3/3
A and B
A= $nums >= 0B= $key > $self->{"t"}[$nums]
| |||||||||||||||||||||||||||||
| 123 | 4160 | push @{ $self->{t} }, $key+0; | |||||||||||||||||||||||||||
| 124 | 4160 | push @{ $self->{d} }, $d; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | else { | ||||||||||||||||||||||||||||
| 127 | # Caluculation prefer index of array to insert | ||||||||||||||||||||||||||||
| 128 | 4113 | my $posi = binary_search(0, $#{$self->{t}}, $key, \&_readindex, $self); | |||||||||||||||||||||||||||
| 129 | 4113 | splice @{ $self->{t} }, $posi, 0, $key+0; | |||||||||||||||||||||||||||
| 130 | 4113 | splice @{ $self->{d} }, $posi, 0, $d; | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 8273 | $self->{h}{$key} = $d; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 11 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 138 | 11 | my ($self, $key) = (@_); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 11 | ▶ | if ( CORE::exists $self->{h}{$key} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | # Seek index | ||||||||||||||||||||||||||||
| 142 | 8 | my $posi = binary_search(0, $#{$self->{t}}, $key, \&_readindex, $self); | |||||||||||||||||||||||||||
| 143 | 8 | splice @{ $self->{t} }, $posi, 1; | |||||||||||||||||||||||||||
| 144 | 8 | splice @{ $self->{d} }, $posi, 1; | |||||||||||||||||||||||||||
| 145 | 8 | my $val = $self->{h}{$key}->[0]; | |||||||||||||||||||||||||||
| 146 | 8 | CORE::delete $self->{h}{$key}; | |||||||||||||||||||||||||||
| 147 | 8 | return $val; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 3 | return undef; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | ▶ | sub EXISTS { | ||||||||||||||||||||||||||
|
Subroutine
EXISTS: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 153 | 5 | my ($self, $key) = (@_); | |||||||||||||||||||||||||||
| 154 | 5 | ▶ | return CORE::exists( $self->{h}{$key} )? 1: undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 25 | ▶ | sub FIRSTKEY { | ||||||||||||||||||||||||||
|
Subroutine
FIRSTKEY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 159 | 25 | my ($self, $key) = (@_); | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 25 | $self->{c} = 0; | |||||||||||||||||||||||||||
| 162 | 25 | $self->NEXTKEY(); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 16503 | ▶ | sub NEXTKEY { | ||||||||||||||||||||||||||
|
Subroutine
NEXTKEY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 167 | 16503 | my ($self) = (@_); | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 16503 | ▶ | return $self->{c} <= $#{$self->{t}}? $self->{t}[ $self->{c}++ ]: undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # ============================================================================= | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =head1 OBJECTIVE USAGE | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | This modules provides object instance and methods. | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | =head2 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | $tied = tie %yourhash, 'Tie::TimeSeries' [, $time => $value [, ... ]] | ||||||||||||||||||||||||||||
| 182 | $tied = Tie::TimeSeries->new( $time => $value [, ... ] ); | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Call method C<new()> to get instance or get return value of tie(). | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 190 | # OO Methods | ||||||||||||||||||||||||||||
| 191 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 192 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 193 | 7 | TIEHASH( @_ ); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # ============================================================================= | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =head2 METHODS | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | =head3 fetch() | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | Method C<fetch()> will fetch a value bound specified key. | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | $tied->fetch( $time [, ... ] ); | ||||||||||||||||||||||||||||
| 206 | $tied->fetch( \@keys_array ); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 211 | 17 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 17 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 17 | my @ret = (); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 17 | ▶ | if ( ref($_[0]) eq 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 217 | 1 | foreach ( @{$_[0]} ){ | |||||||||||||||||||||||||||
| 218 | 3 | push @ret, $self->FETCH( $_ ); | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | else { | ||||||||||||||||||||||||||||
| 222 | 16 | ▶ | while ( @_ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 18 | push @ret, $self->FETCH( shift ); | |||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | 17 | ▶ | if ( wantarray ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 3 | return @ret; | |||||||||||||||||||||||||||
| 228 | } else { | ||||||||||||||||||||||||||||
| 229 | 14 | ▶ | if ( @ret == 1 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 14 | return $ret[0]; | |||||||||||||||||||||||||||
| 231 | } else { | ||||||||||||||||||||||||||||
| 232 | 0 | return \@ret; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | # ============================================================================= | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head3 store() | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Method C<store()> will store keys of time and values to the object. | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | $tied->store( $time => $value [, ... ] ); | ||||||||||||||||||||||||||||
| 245 | $tied->store( \%pairs_hash ); | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =cut | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 250 | 5 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 251 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | 5 | ▶ | if ( ref($_[0]) eq 'HASH' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 254 | 3 | ▶ | while ( my ($k, $v) = each %{$_[0]} ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 255 | 9 | $self->STORE( $k, $v ); | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | else { | ||||||||||||||||||||||||||||
| 259 | 2 | ▶ | while ( @_ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 260 | 4 | $self->STORE( shift, shift ); | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | # ============================================================================= | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head3 delete() | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Method C<delete()> will remove key and value from the object. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | $tied->delete( $time [, ... ] ); | ||||||||||||||||||||||||||||
| 273 | $tied->delete( \@keys_array ); | ||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | And this method will return deleted value(s). | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =cut | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 280 | 4 | ▶ | sub delete { | ||||||||||||||||||||||||||
|
Subroutine
delete: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||
| 281 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 4 | my @deleted = (); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 4 | ▶ | if ( ref($_[0]) eq 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 1 | foreach ( @{$_[0]} ){ | |||||||||||||||||||||||||||
| 287 | 2 | push @deleted, $self->DELETE( $_ ); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||
| 290 | else { | ||||||||||||||||||||||||||||
| 291 | 3 | ▶ | while ( @_ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 292 | 4 | push @deleted, $self->DELETE( shift ); | |||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | 4 | ▶ | if ( wantarray ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 296 | 2 | return @deleted; | |||||||||||||||||||||||||||
| 297 | } else { | ||||||||||||||||||||||||||||
| 298 | 2 | ▶ | if ( @deleted == 1 ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 2 | return $deleted[0]; | |||||||||||||||||||||||||||
| 300 | } else { | ||||||||||||||||||||||||||||
| 301 | 0 | return \@deleted; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | # ============================================================================= | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | =head3 exists() | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | Method C<exists()> returns boolean value. | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | $tied->exists( $time ); | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | =cut | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 318 | 3 | ▶ | sub exists { | ||||||||||||||||||||||||||
|
Subroutine
exists: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 319 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 3 | return $self->EXISTS(shift); | |||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | # ============================================================================= | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | =head3 keys() | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | Method C<keys()> returns keys list of the object. | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | $tied->keys(); | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | =cut | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 336 | 3 | ▶ | sub keys { | ||||||||||||||||||||||||||
|
Subroutine
keys: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 337 | 3 | return @{(shift)->{t}}; | |||||||||||||||||||||||||||
| 338 | } | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # ============================================================================= | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =head3 values() | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | Method C<values()> returns values list of the object. | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | $tied->values(); | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =cut | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 352 | 6 | ▶ | sub values { | ||||||||||||||||||||||||||
|
Subroutine
values: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 353 | 6 | return map { $_->[0] } @{(shift)->{d}}; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | # ============================================================================= | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head3 iterate() | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | Method C<iterate()> execute a routine for each keys and values. | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | $tied->iterate(\&subroutine); | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | Given subroutine will call by iterator with two argument, key and value. | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | # Iterator example | ||||||||||||||||||||||||||||
| 368 | $obj->iterate(sub { | ||||||||||||||||||||||||||||
| 369 | ($key, $val) = @_; | ||||||||||||||||||||||||||||
| 370 | $obj->{$key} = $val * 100; | ||||||||||||||||||||||||||||
| 371 | }); | ||||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | =cut | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 376 | 1 | ▶ | sub iterate { | ||||||||||||||||||||||||||
|
Subroutine
iterate: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||
| 377 | 1 | my ($self, $func) = @_; | |||||||||||||||||||||||||||
| 378 | 1 | ▶ | unless ( ref($func) eq 'CODE' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | croak("Not a subrotine was given to iterate()."); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | 1 | foreach my $key ( @{$self->{t}} ){ | |||||||||||||||||||||||||||
| 383 | 5 | $func->( $key, $self->FETCH($key) ); | |||||||||||||||||||||||||||
| 384 | } | ||||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # ============================================================================= | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 392 | # Private functions | ||||||||||||||||||||||||||||
| 393 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 394 | 62239 | ▶ | sub _readindex { | ||||||||||||||||||||||||||
|
Subroutine
_readindex: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 395 | 62239 | my ($self, $val, $posi ) = @_; | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 62239 | ▶ | if ( defined $posi ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 54061 | $self->{_i} = $posi; | |||||||||||||||||||||||||||
| 399 | 54061 | return ( $val <=> $self->{t}[$posi], $posi ); | |||||||||||||||||||||||||||
| 400 | } else { | ||||||||||||||||||||||||||||
| 401 | 8178 | return $self->{_i} <= $#{$self->{t}}? | |||||||||||||||||||||||||||
| 402 | ( $val <=> $self->{t}[ $self->{_i} ], $self->{_i}++ ): | ||||||||||||||||||||||||||||
| 403 | 8178 | ▶ | ( -1, $self->{_i}++ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | } | ||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | See L<<Tie::IxHash>> - The great module brings many hints to this module. | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | Takahiro Onodera, C<< <ong at garakuta.net> >> | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Copyright 2010 T.Onodera. | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||
| 427 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =cut | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 4 | 1; | |||||||||||||||||||||||||||