| 1 | # Copyright 2010 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Tie::Hash::Union; | ||
| 18 | use 5.004; | ||
| 19 | # use strict; | ||
| 20 | # use warnings; | ||
| 21 | #use Carp; | ||
| 22 | |||
| 23 | # uncomment this to run the ### lines | ||
| 24 | #use Smart::Comments; | ||
| 25 | |||
| 26 | sub new { | ||
| 27 | tie (my %h, @_); | ||
| 28 | return \%h; | ||
| 29 | } | ||
| 30 | sub add { | ||
| 31 | my $self = shift; | ||
| 32 | push @{$self->{'hashes'}}, @_; | ||
| 33 | } | ||
| 34 | sub hashes { | ||
| 35 | my $self = shift; | ||
| 36 | return @{$self->{'hashes'}}; | ||
| 37 | } | ||
| 38 | |||
| 39 | sub TIEHASH { | ||
| 40 | ### Tie-Hash-Union TIEHASH: @_ | ||
| 41 | my $class = shift; | ||
| 42 | return bless { iterpos => 0, | ||
| 43 | hashes => \@_ }, $class; | ||
| 44 | } | ||
| 45 | sub FETCH { | ||
| 46 | my ($self, $key) = @_; | ||
| 47 | #### Tie-Hash-Union FETCH: $key | ||
| 48 | foreach my $h (@{$self->{'hashes'}}) { | ||
| 49 | if (exists $h->{$key}) { | ||
| 50 | return $h->{$key}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | return undef; | ||
| 54 | } | ||
| 55 | sub STORE { | ||
| 56 | my ($self, $key, $value) = @_; | ||
| 57 | foreach my $h (@{$self->{'hashes'}}) { | ||
| 58 | if (exists $h->{$key}) { | ||
| 59 | $h->{$key} = $value; | ||
| 60 | return; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | if (my $h = $self->{'hashes'}->[0]) { | ||
| 64 | $h->{$key} = $value; | ||
| 65 | } else { | ||
| 66 | # croak "No hashes to store to for App::Chart::Tie::Hash::Union"; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | sub FIRSTKEY { | ||
| 71 | my ($self) = @_; | ||
| 72 | $self->{'iterpos'} = 0; | ||
| 73 | if (my $h = $self->{'hashes'}->[0]) { | ||
| 74 | keys %$h; # reset iterator | ||
| 75 | } | ||
| 76 | $self->NEXTKEY (undef); | ||
| 77 | } | ||
| 78 | sub NEXTKEY { | ||
| 79 | my ($self) = @_; | ||
| 80 | for (;;) { | ||
| 81 | my $iterpos; | ||
| 82 | if (($iterpos = $self->{'iterpos'}) >= @{$self->{'hashes'}}) { | ||
| 83 | return; | ||
| 84 | } | ||
| 85 | if (my ($key) = each %{$self->{'hashes'}->[$iterpos]}) { | ||
| 86 | return $key; | ||
| 87 | } | ||
| 88 | $self->{'iterpos'}++; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub EXISTS { | ||
| 93 | my ($self, $key) = @_; | ||
| 94 | return List::Util::first {exists $_->{$key}} @{$self->{'hashes'}}; | ||
| 95 | } | ||
| 96 | sub DELETE { | ||
| 97 | my ($self, $key) = @_; | ||
| 98 | foreach my $h (@{$self->{'hashes'}}) { | ||
| 99 | if (exists $h->{$key}) { | ||
| 100 | return delete $h->{$key}; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | return undef; | ||
| 104 | } | ||
| 105 | sub CLEAR { | ||
| 106 | my ($self) = @_; | ||
| 107 | foreach my $h (@{$self->{'hashes'}}) { | ||
| 108 | %$h = (); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub SCALAR { | ||
| 113 | my ($self) = @_; | ||
| 114 | my $used = 0; | ||
| 115 | my $avail = 0; | ||
| 116 | foreach my $h (@{$self->{'hashes'}}) { | ||
| 117 | my $s = scalar(%$h); | ||
| 118 | if ($s =~ m{^(\d+)/(\d+)$}) { | ||
| 119 | $used += $1; | ||
| 120 | $avail += $2; | ||
| 121 | } elsif ($s) { | ||
| 122 | $used++; | ||
| 123 | $avail++; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | return "$used/$avail"; | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; | ||
| 130 | __END__ |