PartialOrder/Caching.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 57.1% 8 / 14 Conditioncond 45.4% 5 / 11 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 91.6% 22 / 24 Podpod 100% 10 / 10 totaltotal 85.0% 114 / 134 CC 12 SCAR 9.6
CC
12
Cov
85%
has_ancestor
8.9
_get_cached_in
2.1
_get_cached_op
2.1
CRAP 2.6
« PartialOrder/Std.pm ↑ Summary PartialOrder/LRUCaching.pm »
1 # -*- Mode: Perl -*-
2  
3 #
4 # Copyright (c) 2001, Bryan Jurish. All rights reserved.
5 #
6 # This package is free software. You may redistribute it
7 # and/or modify it under the same terms as Perl itself.
8 #
9  
10 ###############################################################
11 #
12 # File: Math::PartialOrder::Caching.pm
13 # Author: Bryan Jurish <jurish@ling.uni-potsdam.de>
14 #
15 # Description: PartialOrder class using hashrefs
16 # to store hierarchy information which
17 # caches inheritance- and operation-lookups
18 #
19 ###############################################################
20  
21  
22 package Math::PartialOrder::Caching;
23 # System modules
24 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 7 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # 3rd party exstensions
27 7 ▶use Tie::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 # user extension modules
29 7 ▶use Math::PartialOrder::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 7 @ISA = qw(Math::PartialOrder::Std);
31 7 @EXPORT = qw();
32 7 @EXPORT_OK = qw($CACHE_KEY_SEP);
33  
34  
35 ###############################################################
36 # Package-Globals
37 ###############################################################
38  
39 7 our $VERSION = 0.01;
40  
41 7 our $CACHE_KEY_SEP = ',';
42  
43  
44 ###############################################################
45 # Initialization
46 # + object structure:
47 # {
48 # # --- INHERITED ---
49 # types => Set::Hashed
50 # root => scalar
51 # parents => { type1 => {p1a=>p1a,...}, ... }
52 # children => { type1 => {c1a=>c1a,...}, ... }
53 # attributes => { type1 => { attr1.1 => val1.1, ... }, ... }
54 # # --- NEW ---
55 # incache => { 'type1,type2' => $has_anc_bool, ... }
56 # opcache => { 'op,type1,type2' => \@result }
57 # }
58 ###############################################################
59 #----------------------------------------------------------------------
60 # new( {root=>$r} )
61 # + initialization routine: returns the object
62 #----------------------------------------------------------------------
63 22 ▶sub new ($;$) {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
64 22 my $class = shift;
65 22 my $args = shift;
66 22 my $self = bless {
67 types => {},
68 root => '',
69 parents => {},
70 children => {},
71 attrs => {},
72 # --- NEW ---
73 incache => {},
74 opcache => {},
75 }, $class;
76 # root node
77 22 ▶ $self->_root($args->{root}||'BOTTOM');
Condition100%2/2
$args->{'root'} || 'BOTTOM'
l!l
1012
MC/DC100%1/1
$args->{'root'} || 'BOTTOM'
$args->{'root'}
Truth table100%2/2
A
A= $args->{'root'}
Aresult
11
00
78  
79 22 return $self;
80 }
81  
82  
83  
84 ###############################################################
85 # Hierarchy Maintainance: Type Operations
86 ###############################################################
87 #--------------------------------------------------------------
88 # types : inherited from Math::PartialOrder::Std
89  
90 #--------------------------------------------------------------
91 216 ▶sub add ($$@) {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
92 216 my $self = shift;
93 216 $self->_clear_cached();
94 216 return $self->SUPER::add(@_);
95 }
96  
97 #--------------------------------------------------------------
98 # has_type : inherited from Math::PartialOrder::Std
99  
100 #--------------------------------------------------------------
101 4 ▶sub add_parents ($$@) {
Subroutine add_parents: called CC=1 SCAR=0.0
Pod documented
102 4 my $self = shift;
103 4 $self->_clear_cached();
104 4 return $self->SUPER::add_parents(@_);
105 }
106  
107 #--------------------------------------------------------------
108 54 ▶sub move ($$@) {
Subroutine move: called CC=1 SCAR=0.0
Pod documented
109 54 my $self = shift;
110 54 $self->_clear_cached();
111 54 return $self->SUPER::move(@_);
112 }
113  
114 #--------------------------------------------------------------
115 2 ▶sub remove ($@) {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
116 2 my $self = shift;
117 2 ▶ return $self unless (@_); # not really deleting anything
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
118 2 $self->_clear_cached();
119 2 return $self->SUPER::remove(@_);
120 }
121  
122 #--------------------------------------------------------------
123 # parents : inherited from Math::PartialOrder::Std
124  
125 #--------------------------------------------------------------
126 # children : inherited from Math::PartialOrder::Std
127  
128 #--------------------------------------------------------------
129 # has_parent : inherited from Math::PartialOrder::Std
130  
131 #--------------------------------------------------------------
132 # has_child : inherited from Math::PartialOrder::Std
133  
134 #--------------------------------------------------------------
135 12 ▶sub has_ancestor ($$$) {
Subroutine has_ancestor: called CC=5 SCAR=21.9
Pod documented
136 12 my ($cached);
137 12 ▶ return (defined($_[1]) && defined($_[2]) &&
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] && defined $_[2] && $_[0]->has_types($_[1], $_[2]) && defined($cached = $_[0]->_get_cached_in($_[1], $_[2])) ? : 0 12 12
Condition33%3/9
defined $_[1] and defined $_[2] and $_[0]->has_types($_[1], $_[2]) and defined($cached = $_[0]->_get_cached_in($_[1], $_[2]))
!ll&&!rl&&r
0120
defined $_[1] and defined $_[2] and $_[0]->has_types($_[1], $_[2])
!ll&&!rl&&r
0012
defined $_[1] and defined $_[2]
!ll&&!rl&&r
0012
MC/DC0%0/4
defined $_[1] and defined $_[2] and $_[0]->has_types($_[1], $_[2]) and defined($cached = $_[0]->_get_cached_in($_[1], $_[2]))
defined $_[1] defined $_[2] $_[0]->has_types($_[1], $_[2]) defined($cached = $_[0]->_get_cached_in($_[1], $_[2]))
Truth table20%1/5
A and B and C and D
A= defined $_[1]B= defined $_[2]C= $_[0]->has_types($_[1], $_[2])D= defined($cached = $_[0]->_get_cached_in($_[1], $_[2]))
ABCDresult
0XXX0
10XX0
110X0
11100
11111
138 $_[0]->has_types($_[1],$_[2])
139 &&
140 defined($cached = $_[0]->_get_cached_in($_[1],$_[2]))
141 ? $cached
142 : $_[0]->_set_cached_in($_[1], $_[2],
143 $_[0]->SUPER::has_ancestor($_[1],$_[2])));
144 }
145  
146  
147 #--------------------------------------------------------------
148 2 ▶sub has_descendant ($$$) { return $_[0]->has_ancestor(@_[2,1]); }
Subroutine has_descendant: called CC=1 SCAR=0.0
Pod documented
149  
150 #--------------------------------------------------------------
151 # get_attributes : inherited from Math::PartialOrder::Std
152  
153 #--------------------------------------------------------------
154 # get_attribute : inherited from Math::PartialOrder::Std
155  
156 #--------------------------------------------------------------
157 # set_attribute : inherited from Math::PartialOrder::Std
158  
159 #--------------------------------------------------------------
160 12 ▶sub assign ($$) {
Subroutine assign: called CC=1 SCAR=0.0
Pod documented
161 12 my ($h1,$h2) = @_;
162 12 $h1->_clear_cache();
163 12 return $h1->SUPER::assign($h2);
164 }
165  
166  
167  
168 #--------------------------------------------------------------
169 10 ▶sub merge ($@) {
Subroutine merge: called CC=1 SCAR=0.0
Pod documented
170 10 my $h1 = shift;
171 10 $h1->_clear_cache();
172 10 return $h1->SUPER::merge(@_);
173 }
174  
175 #--------------------------------------------------------------
176 14 ▶sub clear ($) {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
177 14 my $self = shift;
178 14 $self->_clear_cache();
179 14 return $self->SUPER::clear();
180 }
181  
182 ###############################################################
183 # Additional Hierarchy Maintainence Operations
184 ###############################################################
185 #--------------------------------------------------------------
186 # ensure_types : inherited from Math::PartialOrder
187  
188 #--------------------------------------------------------------
189 # _ancestors($type) => $hashref : inherited from Math::PartialOrder::Std
190  
191 #--------------------------------------------------------------
192 # _descendants($type) => $hashref : inherited from Math::PartialOrder::Std
193  
194 #--------------------------------------------------------------
195 # _minimize : inherited from Math::PartialOrder::Std
196  
197 #--------------------------------------------------------------
198 # _maximize : inherited from Math::PartialOrder::Std
199  
200  
201 ###############################################################
202 # Hierarchy Operations
203 ###############################################################
204  
205 #--------------------------------------------------------------
206 # lub
207 25 ▶sub _lub ($$$) {
Subroutine _lub: called CC=2 SCAR=7.1
208 25 my ($cached);
209 return
210 (defined($cached = $_[0]->_get_cached_op('lub',$_[1],$_[2]))
211 ? (@$cached)
212 25 ▶ : (@{$_[0]->_set_cached_op('lub', $_[1], $_[2],
Branch: 1 branch
BranchTrueFalseTotal
defined($cached = $_[0]->_get_cached_op('lub', $_[1], $_[2])) ? : 0 25 25
213 [$_[0]->SUPER::_lub($_[1],$_[2])])}));
214 }
215  
216  
217 #--------------------------------------------------------------
218 # glb
219 2 ▶sub _glb ($$$) {
Subroutine _glb: called CC=2 SCAR=7.1
220 2 my ($cached);
221 return
222 (defined($cached = $_[0]->_get_cached_op('glb',$_[1],$_[2]))
223 ? (@$cached)
224 2 ▶ : (@{$_[0]->_set_cached_op('glb', $_[1], $_[2],
Branch: 1 branch
BranchTrueFalseTotal
defined($cached = $_[0]->_get_cached_op('glb', $_[1], $_[2])) ? : 0 2 2
225 [$_[0]->SUPER::_glb($_[1],$_[2])])}));
226 }
227  
228  
229 ###############################################################
230 # Hierarchy operation utilities
231 ###############################################################
232  
233 ###############################################################
234 # Accessors/manipulators
235 ###############################################################
236  
237 #--------------------------------------------------------------
238 # _types : inherited from Math::PartialOrder::Std
239  
240 #--------------------------------------------------------------
241 158 ▶sub _root ($;$) {
Subroutine _root: called CC=2 SCAR=6.9
242 158 my $self = shift;
243 158 ▶ return $self->{root} unless (@_);
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 108 50 158
244 50 $self->_clear_cache();
245 50 return $self->SUPER::_root(@_);
246 }
247 7 *root = \&_root;
248  
249 #--------------------------------------------------------------
250 # _parents : inherited from Math::PartialOrder::Std
251  
252 #--------------------------------------------------------------
253 # _children : inherited from Math::PartialOrder::Std
254  
255 #--------------------------------------------------------------
256 # _attributes : inherited from Math::PartialOrder::Std
257  
258  
259 #--------------------------------------------------------------
260 0 ▶sub _incache ($) { return $_[0]->{incache}; }
Subroutine _incache: not called CC=1 SCAR=6.9
261 12 ▶sub _get_cached_in ($$$) {
Subroutine _get_cached_in: called CC=2 SCAR=7.6
262 return
263 exists($_[0]->{incache}{$_[1].$CACHE_KEY_SEP.$_[2]})
264 12 ▶ ? $_[0]->{incache}{$_[1].$CACHE_KEY_SEP.$_[2]}
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'incache'}{$_[1] . $CACHE_KEY_SEP . $_[2]} ? : 0 12 12
265 : undef;
266 }
267 12 ▶sub _set_cached_in ($$$$) {
Subroutine _set_cached_in: called CC=1 SCAR=0.0
268 return
269 12 $_[0]->{incache}{$_[1].$CACHE_KEY_SEP.$_[2]} = $_[3];
270 }
271  
272  
273  
274 #--------------------------------------------------------------
275 0 ▶sub _opcache ($) { return $_[0]->{opcache}; }
Subroutine _opcache: not called CC=1 SCAR=6.9
276 27 ▶sub _get_cached_op ($$$$) {
Subroutine _get_cached_op: called CC=2 SCAR=7.6
277 return
278 exists($_[0]->{opcache}{$_[1].$CACHE_KEY_SEP.$_[2].$CACHE_KEY_SEP.$_[3]})
279 27 ▶ ? $_[0]->{opcache}{$_[1].$CACHE_KEY_SEP.$_[2].$CACHE_KEY_SEP.$_[3]}
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{'opcache'}{$_[1] . $CACHE_KEY_SEP . $_[2] . $CACHE_KEY_SEP . $_[3]} ? : 0 27 27
280 : undef;
281 }
282 27 ▶sub _set_cached_op ($$$$$) {
Subroutine _set_cached_op: called CC=1 SCAR=0.0
283 return
284 27 $_[0]->{opcache}{$_[1].$CACHE_KEY_SEP.$_[2].$CACHE_KEY_SEP.$_[3]} = $_[4];
285 }
286  
287  
288  
289  
290 #--------------------------------------------------------------
291 7 *_clear_cached = \&_clear_cache;
292 362 ▶sub _clear_cache ($) {
Subroutine _clear_cache: called CC=1 SCAR=0.0
293 362 %{$_[0]->{incache}} = ();
294 362 %{$_[0]->{opcache}} = ();
295 }
296  
297  
298  
299  
300 7 1;
301 __END__
« PartialOrder/Std.pm ↑ Summary PartialOrder/LRUCaching.pm »