App/Chart/Tie/Hash/Union.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 117 CC 10 SCAR 19.7
CC
10
Cov
0%
STORE
12.0
NEXTKEY
12.0
SCALAR
12.0
CRAP 7.2
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__
131  
132 =head1 NAME
133  
134 App::Chart::Tie::Hash::Union -- union of multiple hashes
135  
136 =for test_synopsis my ($pid)
137  
138 =head1 SYNOPSIS
139  
140 use App::Chart::Tie::Hash::Union;
141 my %h1 = (a => 1, b => 2);
142 my %h2 = (x => 3, y => 3);
143 my %union;
144 tie %union, \%h1, \%h2;
145 print $union{a},"\n"; # entry in %h1
146 print $union{y},"\n"; # entry in %h2
147  
148 =head1 DESCRIPTION
149  
150 C<App::Chart::Tie::Hash::Union> makes a hash present the keys and values of a given set
151 of other underlying hash tables. Accessing the tied hash looks in each of
152 those hashes for the desired key. The tied hash hold nothing of it's own
153 but looks dynamically at the underlying hashes, so it reflects their current
154 contents at any given time.
155  
156 =over
157  
158 =item C<$tiedhash{$key}>
159  
160 Fetching looks in each unioned hash for the first with C<exists $h->{$key}>.
161  
162 =item C<$tiedhash{$key} = $value>
163  
164 Storing looks in each unioned hash for one with C<exists $h->{$key}> and
165 stores to that entry. If none have C<$key> already then a new entry is made
166 in the first unioned hash. If there are no unioned hashes the store croaks.
167  
168 =item C<delete $tiedhash{$key}>
169  
170 Deleting deletes C<$key> from each of the unioned hashes.
171  
172 =item C<clear %tiedhash>
173  
174 Clearing clears each unioned hash.
175  
176 =item C<keys %tiedhash>
177  
178 Returns the keys of all the unioned hashes.
179  
180 =item C<each %tiedhash>
181  
182 Iterates the unioned hashes. Currently this is implemented using a
183 corresponding C<each> on those underly hashes, so don't call C<each>,
184 C<keys> or C<values> on those or it will upset the iterated position of the
185 tied hash.
186  
187 =item C<scalar %tiedhash>
188  
189 In scalar context the tied hash returns bucket usage counts like C<3/64>
190 like an ordinary hash, made by adding up the unioned hashes. If a unioned
191 hash returns something other than a bucket count in scalar context (which
192 can happen if it in turn is also a tied hash) then it's counted as 1/1 if
193 true or 0/0 if false.
194  
195 In Perl 5.8 and earlier tied hashes which don't implement the C<SCALAR>
196 method always return 0 as if it's empty, when it may not be. This of course
197 will propagate up through a C<App::Chart::Tie::Hash::Union> to make it appear empty when
198 it may not be.
199  
200 =back
201  
202 =head1 FUNCTIONS
203  
204 =over 4
205  
206 =item C<tie %hash, 'App::Chart::Tie::Hash::Union', \%h1, \%h2, ...>
207  
208 Tie hash C<%hash> to present the contents of the given C<%h1>, C<%h2>, etc.
209  
210 =item C<< $hashref = App::Chart::Tie::Hash::Union->new (\%h1, \%h2, ...) >>
211  
212 Return a ref to a newly created hash table tied to the given C<%h1>, C<%h2>,
213 etc. For example
214  
215 my $href = App::Chart::Tie::Hash::Union->new (\%h1, \%h2);
216  
217 is the same as
218  
219 tie (my %hash, 'App::Chart::Tie::Hash::Union', \%h1, \%h2);
220 my $href = \%hash;
221  
222 If you want your own C<%hash> as such then the plain C<tie> is easier. If
223 you want an hashref to pass around to other funcs then C<new> saves a line
224 of code.
225  
226 =back
227  
228 =head2 Object Methods
229  
230 The tie object associated with the tied hash, as returned by the C<tie> or
231 obtained later with C<tied>, has the following methods.
232  
233 =over 4
234  
235 =item C<< $thobj->add (\%h3, ...) >>
236  
237 Add further hash tables to the union.
238  
239 =item C<< @list = $thobj->hashes() >>
240  
241 Return a list of hashrefs which are currently being unioned.
242  
243 =back
244  
245 =head1 SEE ALSO
246  
247 L<Hash::Union>, L<Hash::Merge>
248  
249 =cut