Paranoid/Data/AVLTree.pm

filter: Statementstmt 83.7% 464 / 554 Branchbran 60.1% 125 / 208 Conditioncond 52.9% 36 / 68 MC/DCmcdc 25.5% 11 / 43 Subroutinesub 90.6% 58 / 64 Podpod 100% 14 / 14 totaltotal 74.4% 708 / 951 CC 133 SCAR 25.8
CC
133
Cov
75%
_readRecord
47.7
_printKeys
30.0
delNode
22.3
CRAP 13.1
« Paranoid/Input.pm ↑ Summary Paranoid/IO.pm »
1 # Paranoid::Data::AVLTree -- AVL-Balanced Tree Class
2 #
3 # $Id: lib/Paranoid/Data/AVLTree.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Data::AVLTree;
33  
34 2 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Paranoid::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶use Paranoid::Data::AVLTree::AVLNode;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶use Paranoid::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶use Fcntl qw(:DEFAULT :flock :mode :seek);
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
49  
50 2 ▶use constant AVLROOT => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 2 ▶use constant AVLKEYS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2 ▶use constant AVLPROF => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 ▶use constant AVLSTATS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 2 ▶use constant STAT_INSERTS => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use constant STAT_DELETES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use constant STAT_REBALANCE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 2 ▶use constant STAT_ROTATIONS => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 2 ▶use constant AVLZEROLS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2 ▶use constant AVLUNDEF => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 # Record signature format:
64 # PDAVL KFLAG VFLAG KLEN VLEN
65 # Z6 Cx Cx NNx NNx
66 # 28 bytes
67 2 ▶use constant SIGNATURE => 'Z6CxCxNNxNNx';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 2 ▶use constant SIG_LEN => 28;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 2 ▶use constant SIG_TYPE => 'PDAVL';
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 #####################################################################
72 #
73 # Module code follows
74 #
75 #####################################################################
76  
77 4 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
78  
79 # Purpose: instantiates an AVLTree object
80 # Returns: Object reference if successful, undef otherwise
81 # Usage: $obj = Paranoid::Data::AVLTree->new();
82  
83 4 my ( $class, %args ) = splice @_;
84 4 my $self = [undef];
85  
86 4 subPreamble( PDLEVEL1, '%', %args );
87  
88 4 bless $self, $class;
89  
90 4 subPostamble( PDLEVEL1, '$', $self );
91  
92 4 return $self;
93 }
94  
95 1 ▶sub profile {
Subroutine profile: called CC=2 SCAR=6.9
Pod documented
96  
97 # Purpose: Enables/disables performance profiling
98 # Returns: Boolean
99 # Usage: $rv = $obj->profile(1);
100  
101 1 my $self = shift;
102 1 my $enable = shift;
103  
104 1 $$self[AVLPROF] = $enable;
105 1 ▶ if ($enable) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enable) 1 0 1
106  
107 # Reset counters
108 1 $$self[AVLSTATS] = [];
109 1 $$self[AVLSTATS][STAT_INSERTS] = 0;
110 1 $$self[AVLSTATS][STAT_DELETES] = 0;
111 1 $$self[AVLSTATS][STAT_REBALANCE] = 0;
112 1 $$self[AVLSTATS][STAT_ROTATIONS] = 0;
113 }
114  
115 1 return 1;
116 }
117  
118 1 ▶sub stats {
Subroutine stats: called CC=2 SCAR=7.2
Pod documented
119  
120 # Purpose: Returns the values of the current perf counters
121 # Returns: Hash
122 # Usage: %stats = $obj->stats;
123  
124 1 my $self = shift;
125 1 my %stats;
126  
127 1 ▶ if ( defined $$self[AVLSTATS] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[3]) { } 1 0 1
128 1 %stats = (
129 insertions => $$self[AVLSTATS][STAT_INSERTS],
130 deletions => $$self[AVLSTATS][STAT_DELETES],
131 rebalances => $$self[AVLSTATS][STAT_REBALANCE],
132 rotations => $$self[AVLSTATS][STAT_ROTATIONS],
133 );
134 } else {
135 0 %stats = (
136 insertions => 0,
137 deletions => 0,
138 rebalances => 0,
139 rotations => 0,
140 );
141 }
142  
143 1 return %stats;
144 }
145  
146 9 ▶sub count {
Subroutine count: called CC=2 SCAR=6.9
Pod documented
147  
148 # Purpose: Returns the number of nodes in the tree
149 # Returns: Integer
150 # Usage: $count = $obj->count;
151  
152 9 my $self = shift;
153  
154 9 ▶ return defined $$self[AVLROOT] ? $$self[AVLROOT]->count : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[0] ? : 7 2 9
155 }
156  
157 13 ▶sub height {
Subroutine height: called CC=2 SCAR=6.9
Pod documented
158  
159 # Purpose: Returns the height of the tree based on the longest branch
160 # Returns: Integer
161 # Usage: $height = $obj->height;
162  
163 13 my $self = shift;
164  
165 13 ▶ return defined $$self[AVLROOT] ? $$self[AVLROOT]->height : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[0] ? : 10 3 13
166 }
167  
168 0 ▶sub _printKeys {
Subroutine _printKeys: not called CC=5 SCAR=34.0
169  
170 # Purpose: Prints the key structure of a tree starting with the node
171 # passed, and includes all children
172 # Returns: String
173 # Usage: $output = _printKeys($root);
174  
175 0 my $i = shift;
176 0 my $node = shift;
177 0 my $side = shift;
178 0 my $h = $node->height;
179 0 my $b = $node->balance;
180 0 my $line = '';
181  
182 0 ▶ $line = ' ' x $i if $i;
Branch: 1 branch
BranchTrueFalseTotal
if $i 0 0 0
183 0 ▶ $line .= defined $side ? "($side/$h/$b) " : "($h/$b) ";
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 0 0 0
184 0 $line .= $node->key;
185 0 $line .= "\n";
186  
187 0 $i++;
188 0 ▶ $line .= _printKeys( $i, $node->left, 'l' ) if defined $node->left;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->left 0 0 0
189 0 ▶ $line .= _printKeys( $i, $node->right, 'r' ) if defined $node->right;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->right 0 0 0
190  
191 0 return $line;
192 }
193  
194 0 ▶sub dumpKeys {
Subroutine dumpKeys: not called CC=2 SCAR=17.9
Pod documented
195  
196 # Purpose: A wrapper method that calls _printKeys() with the root node
197 # stored in the object
198 # Returns: String
199 # Usage: $obj->dumpKeys;
200  
201 0 my $self = shift;
202 0 my $line = '<empty>';
203  
204 0 ▶ $line = _printKeys( 1, $$self[AVLROOT] ) if defined $$self[AVLROOT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[0] 0 0 0
205  
206 0 pderror($line);
207  
208 0 return 1;
209 }
210  
211 23 ▶sub _keys {
Subroutine _keys: called CC=3 SCAR=11.0
212  
213 # Purpose: Returns an array containing all the keys in tree starting
214 # with the passed node
215 # Returns: List of Strings
216 # Usage: @keys = _keys($rootNode);
217  
218 23 my $node = shift;
219 23 my @stack = $node->key;
220  
221 23 ▶ push @stack, _keys( $node->left ) if defined $node->left;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->left 9 14 23
222 23 ▶ push @stack, _keys( $node->right ) if defined $node->right;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node->right 10 13 23
223  
224 23 return @stack;
225 }
226  
227 6 ▶sub nodeKeys {
Subroutine nodeKeys: called CC=2 SCAR=6.9
Pod documented
228  
229 # Purpose: A wrapper method that calles _keys with the root node
230 # stored in the object
231 # Returns: List of Strings
232 # Usage: @keys = $obj->nodeKeys;
233  
234 6 my $self = shift;
235 6 my @k;
236  
237 6 ▶ @k = _keys( $$self[AVLROOT] ) if defined $$self[AVLROOT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[0] 4 2 6
238  
239 6 return @k;
240 }
241  
242 147 ▶sub _findNode {
Subroutine _findNode: called CC=5 SCAR=16.1
243  
244 # Purpose: Checks for the existence of a matching node in the tree
245 # and updates the passed references for the path to where
246 # the node would be positioned, as well as the node, if
247 # there is one.
248 # Returns: Boolean
249 # Usage: $rv = $obj->_findNode($key, $node, @path);
250  
251 147 my $self = shift;
252 147 my $key = shift;
253 147 my $nref = shift;
254 147 my $pref = shift;
255 147 my $root = $$self[AVLROOT];
256 147 my $rv = 0;
257 147 my ( @path, $node );
258  
259 147 subPreamble( PDLEVEL4, '$$$', $key, $nref, $pref );
260  
261 147 $$nref = undef;
262 147 @$pref = ();
263 147 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) 143 4 147
264 143 $node = $root;
265 143 ▶ while ( defined $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $node) 389 20 409
266 389 ▶ if ( $node->key eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->key eq $key) { } 123 266 389
267 123 $rv = 1;
268 123 $$nref = $node;
269 123 pdebug( 'node found: %s', PDLEVEL4, $$nref );
270 123 last;
271 } else {
272 266 push @path, $node;
273 266 ▶ $node = $key gt $node->key ? $node->right : $node->left;
Branch: 1 branch
BranchTrueFalseTotal
$key gt $node->key ? : 167 99 266
274 }
275 }
276 143 @$pref = @path;
277 143 pdebug( 'path to node position: %s', PDLEVEL4, @$pref );
278 }
279  
280 147 subPostamble( PDLEVEL4, '$', $rv );
281  
282 147 return $rv;
283 }
284  
285 11 ▶sub nodeExists {
Subroutine nodeExists: called CC=2 SCAR=6.9
Pod documented
286  
287 # Purpose: Checks for the existence of a matching node
288 # Returns: Boolean
289 # Usage: $rv = $obj->nodeExists($key):
290  
291 11 my $self = shift;
292 11 my $key = shift;
293 11 my $rv = 0;
294 11 my ( @path, $node );
295  
296 11 pdebug( 'entering w/%s', PDLEVEL3, $key );
297 11 pIn();
298  
299 11 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 11 0 11
300 11 $self->_findNode( $key, \$node, \@path );
301 11 $rv = defined $node;
302 }
303  
304 11 pOut();
305 11 pdebug( 'leaving w/rv: %s', PDLEVEL3, $rv );
306  
307 11 return $self->_findNode( $key, \$node, \@path );
308 }
309  
310 12 ▶sub fetchVal {
Subroutine fetchVal: called CC=3 SCAR=11.1
Pod documented
311  
312 # Purpose: Returns the associated value for the key, if it's present
313 # Returns: Scalar
314 # Usage: $val = obj->fetchVal($key);
315  
316 12 my $self = shift;
317 12 my $key = shift;
318 12 my ( @path, $node, $val );
319  
320 12 subPreamble( PDLEVEL3, '$', $key );
321  
322 12 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 12 0 12
323 12 $self->_findNode( $key, \$node, \@path );
324 12 pdebug( 'node is %s', PDLEVEL4, $node );
325 12 ▶ $val = $node->val if defined $node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node 12 0 12
326 }
327  
328 12 subPostamble( PDLEVEL3, 'b', $val );
329  
330 12 return $val;
331 }
332  
333 25 ▶sub _addNode {
Subroutine _addNode: called CC=9 SCAR=22.2
334  
335 # Purpose: Adds or updates a node for the key/value passed
336 # Returns: Boolean
337 # Usage: $rv = $obj->_addNode($key, $value);
338  
339 25 my $self = shift;
340 25 my $key = shift;
341 25 my $val = shift;
342 25 my $root = $$self[AVLROOT];
343 25 my $rv = 1;
344 25 my ( @path, $nn, $node, $parent );
345  
346 25 subPreamble( PDLEVEL3, '$b', $key, $val );
347  
348 # Validation check
349 25 $nn = Paranoid::Data::AVLTree::AVLNode->new( $key, $val );
350  
351 25 ▶ if ( defined $nn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $nn) { } 25 0 25
352 25 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 20 5 25
353 20 ▶ if ( $self->_findNode( $key, \$node, \@path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_findNode($key, \$node, \@path)) { } 0 20 20
354 0 $node->setVal($val);
355 0 pdebug( 'updating existing node', PDLEVEL4 );
356 } else {
357  
358 # Attach the new node
359 20 foreach (@path) {
360 48 ▶ if ( $key gt $_->key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key gt $_->key) { } 30 18 48
361 30 $_->incrRHeight;
362 } else {
363 18 $_->incrLHeight;
364 }
365 }
366 20 ▶ if ( $key gt $path[-1]->key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key gt $path[-1]->key) { } 16 4 20
367 16 $path[-1]->setRight($nn);
368 } else {
369 4 $path[-1]->setLeft($nn);
370 }
371 20 pdebug( 'added node at the end of the branch', PDLEVEL4 );
372 20 ▶ $$self[AVLSTATS][STAT_INSERTS]++ if $$self[AVLPROF];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] 4 16 20
373  
374 }
375  
376 } else {
377 5 $$self[AVLROOT] = $nn;
378 5 pdebug( 'adding node as the tree root: %s',
379 PDLEVEL4, $$self[AVLROOT] );
380 5 ▶ $$self[AVLSTATS][STAT_INSERTS]++ if $$self[AVLPROF];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] 1 4 5
381 }
382  
383 } else {
384 0 $rv = 0;
385 0 pdebug( 'invalid key submitted: %s', PDLEVEL1, $key );
386 }
387  
388 25 subPostamble( PDLEVEL3, '$', $rv );
389  
390 25 return $rv;
391 }
392  
393 19 ▶sub _updtRootRef {
Subroutine _updtRootRef: called CC=6 SCAR=18.3
394  
395 # Purpose: Updates the parent link matching the original ref
396 # with the new ref. Link can be on either side of
397 # the branch, or could be root of the tree, entirely.
398 # Returns: Boolean
399 # Usage: $rv = $obj->_updtRootRef($root, $oldref, $newref);
400  
401 19 my $self = shift;
402 19 my $root = shift;
403 19 my $oref = shift;
404 19 my $nref = shift;
405 19 my $rv = 1;
406  
407 19 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 10 9 19
408  
409 # Update the parent link (could be on either side) to the child
410 10 ▶ if ( defined $root->left and $root->left == $oref ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $root->left and $root->left == $oref) { } 6 4 10
elsif (defined $root->right and $root->right == $oref) { } 4 0 4
Condition100%3/3
defined $root->left and $root->left == $oref
!ll&&!rl&&r
136
MC/DC100%2/2
defined $root->left and $root->left == $oref
defined $root->left $root->left == $oref
Truth table100%3/3
A and B
A= defined $root->leftB= $root->left == $oref
ABresult
0X0
100
111
411 6 pdebug( 'updating link on the root\'s right side', PDLEVEL4 );
412 6 $root->setLeft($nref);
413 ▶ } elsif ( defined $root->right and $root->right == $oref ) {
Condition33%1/3
defined $root->right and $root->right == $oref
!ll&&!rl&&r
004
MC/DC0%0/2
defined $root->right and $root->right == $oref
defined $root->right $root->right == $oref
Truth table33%1/3
A and B
A= defined $root->rightB= $root->right == $oref
ABresult
0X0
100
111
414 4 pdebug( 'updating link on the root\'s left side', PDLEVEL4 );
415 4 $root->setRight($nref);
416 } else {
417 0 pdebug( 'ERROR: old ref not linked to root!', PDLEVEL1 );
418 0 $rv = 0;
419 }
420  
421 } else {
422  
423 # No parent means we're rotating the root
424 9 pdebug( 'updating root node link', PDLEVEL4 );
425 9 $$self[AVLROOT] = $nref;
426 }
427  
428 19 return $rv;
429 }
430  
431 6 ▶sub _rrr {
Subroutine _rrr: called CC=6 SCAR=18.1
432  
433 # Purpose: Performs a single rotate right
434 # Returns: Boolean
435 # Usage: $rv = $self->_rrr($root, $node);
436  
437 6 my $self = shift;
438 6 my $root = shift;
439 6 my $x = shift;
440 6 my $z = $x->left;
441 6 my $rv;
442  
443 6 subPreamble( PDLEVEL4, '$$', $root, $x );
444  
445 # Update root node as a prerequisite to continuing
446 6 ▶ $rv = defined $x and defined $z and $self->_updtRootRef( $root, $x, $z );
Branch: 1 branch
BranchTrueFalseTotal
$rv = defined $x and defined $z and $self->_updtRootRef($root, $x, $z) 6 0 6
Condition33%1/3
$rv = defined $x and defined $z
!ll&&!rl&&r
006
MC/DC0%0/2
$rv = defined $x and defined $z
$rv = defined $x defined $z
Truth table33%1/3
A and B
A= $rv = defined $xB= defined $z
ABresult
0X0
100
111
447  
448 # Update x & z refs
449 6 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 6 0 6
450 6 $x->setLeft( $z->right );
451 6 $z->setRight($x);
452  
453 # Update heights
454 6 $x->updtHeights;
455 6 $z->updtHeights;
456 6 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 2 4 6
457 2 $root->updtHeights;
458 } else {
459 4 $$self[AVLROOT]->updtHeights;
460 }
461 6 ▶ $$self[AVLSTATS][STAT_ROTATIONS]++ if $$self[AVLPROF];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] 1 5 6
462 }
463  
464 6 subPostamble( PDLEVEL4, '$', $rv );
465  
466 6 return $rv;
467 }
468  
469 8 ▶sub _rll {
Subroutine _rll: called CC=6 SCAR=18.2
470  
471 # Purpose: Performs a single rotate left
472 # Returns: Boolean
473 # Usage: $rv = $self->_rll($root, $node);
474  
475 8 my $self = shift;
476 8 my $root = shift;
477 8 my $x = shift;
478 8 my $z = $x->right;
479 8 my $rv;
480  
481 8 subPreamble( PDLEVEL4, '$$', $root, $x );
482  
483 # Update root node as a prerequisite to continuing
484 8 ▶ $rv = defined $x and defined $z and $self->_updtRootRef( $root, $x, $z );
Branch: 1 branch
BranchTrueFalseTotal
$rv = defined $x and defined $z and $self->_updtRootRef($root, $x, $z) 8 0 8
Condition33%1/3
$rv = defined $x and defined $z
!ll&&!rl&&r
008
MC/DC0%0/2
$rv = defined $x and defined $z
$rv = defined $x defined $z
Truth table33%1/3
A and B
A= $rv = defined $xB= defined $z
ABresult
0X0
100
111
485  
486 # Update x & z refs
487 8 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 8 0 8
488 8 $x->setRight( $z->left );
489 8 $z->setLeft($x);
490  
491 # Update heights
492 8 $x->updtHeights;
493 8 $z->updtHeights;
494 8 ▶ if ( defined $root ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $root) { } 4 4 8
495 4 $root->updtHeights;
496 } else {
497 4 $$self[AVLROOT]->updtHeights;
498 }
499 8 ▶ $$self[AVLSTATS][STAT_ROTATIONS]++ if $$self[AVLPROF];
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] 0 8 8
500 }
501  
502 8 subPostamble( PDLEVEL4, '$', $rv );
503  
504 8 return $rv;
505 }
506  
507 0 ▶sub _rrl {
Subroutine _rrl: not called CC=3 SCAR=24.8
508  
509 # Purpose: Performs a double rotation of right-left
510 # Returns: Boolean
511 # Usage: $rv = $self->_rrl($root, $node);
512  
513 0 my $self = shift;
514 0 my $root = shift;
515 0 my $x = shift;
516 0 my $z = $x->right;
517 0 my $y = $z->left;
518 0 my $rv = 0;
519  
520 0 subPreamble( PDLEVEL4, '$$', $root, $x );
521  
522 0 $rv = $self->_rrr( $x, $z );
523 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
524 0 $z = $x->right;
525 0 ▶ if ( $z == $y ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z == $y) { } 0 0 0
526 0 $rv = $self->_rll( $root, $x );
527 } else {
528 0 pdebug( 'double rotation incorrect results on first rotation',
529 PDLEVEL1 );
530 }
531 } else {
532 0 pdebug( 'double rotation failed on first rotation', PDLEVEL1 );
533 }
534  
535 0 subPostamble( PDLEVEL4, '$', $rv );
536  
537 0 return $rv;
538 }
539  
540 0 ▶sub _rlr {
Subroutine _rlr: not called CC=3 SCAR=24.8
541  
542 # Purpose: Performs a double rotation of left-right
543 # Returns: Boolean
544 # Usage: $rv = $self->_rlr($root, $node);
545  
546 0 my $self = shift;
547 0 my $root = shift;
548 0 my $x = shift;
549 0 my $z = $x->left;
550 0 my $y = $z->right;
551 0 my $rv = 0;
552  
553 0 subPreamble( PDLEVEL4, '$$', $root, $x );
554  
555 0 $rv = $self->_rll( $x, $z );
556 0 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) { } 0 0 0
557 0 $z = $x->left;
558 0 ▶ if ( $z == $y ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z == $y) { } 0 0 0
559 0 $rv = $self->_rrr( $root, $x );
560 } else {
561 0 pdebug( 'double rotation incorrect results on first rotation',
562 PDLEVEL1 );
563 }
564 } else {
565 0 pdebug( 'double rotation failed on first rotation', PDLEVEL1 );
566 }
567  
568 0 subPostamble( PDLEVEL4, '$', $rv );
569  
570 0 return $rv;
571 }
572  
573 14 ▶sub _rotate {
Subroutine _rotate: called CC=5 SCAR=16.7
574  
575 # Purpose: Performs the appropriate rotation for the specified node under
576 # the specified root. This includes not only what direction to
577 # rotate, but whether to perform a single or double rotation.
578 # Returns: Boolean
579 # Usage: $rv = $obj->_rotate($root, $node);
580  
581 14 my $self = shift;
582 14 my $root = shift;
583 14 my $x = shift;
584 14 my $rv = 1;
585  
586 14 subPreamble( PDLEVEL4, '$$', $root, $x );
587  
588 14 ▶ if ( $x->balance > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->balance > 1) { } 8 6 14
elsif ($x->balance < -1) { } 6 0 6
589  
590 # Rotate left
591 8 ▶ if ( $x->right->balance < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->right->balance < 0) { } 0 8 8
592  
593 # Perform a double rotation
594 0 $rv = $self->_rrl( $root, $x );
595  
596 } else {
597  
598 # Perform a single rotation
599 8 $rv = $self->_rll( $root, $x );
600 }
601  
602 } elsif ( $x->balance < -1 ) {
603  
604 # Rotate right
605 6 ▶ if ( $x->left->balance > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->left->balance > 0) { } 0 6 6
606  
607 # Perform a double rotation
608 0 $rv = $self->_rlr( $root, $x );
609  
610 } else {
611  
612 # Perform a single rotation
613 6 $rv = $self->_rrr( $root, $x );
614 }
615 }
616  
617 14 subPostamble( PDLEVEL4, '$', $rv );
618  
619 14 return $rv;
620 }
621  
622 30 ▶sub _rebalance {
Subroutine _rebalance: called CC=7 SCAR=19.5
623  
624 # Purpose: Rebalances the tree for the branch extending to the specified
625 # node by performing rotations on all nodes that are unbalanced
626 # Returns: Boolean
627 # Usage: $rv = $obj->($node);
628  
629 30 my $self = shift;
630 30 my $node = shift;
631 30 my $key = $node->key;
632 30 my ( @path, $parent, $n );
633  
634 30 subPreamble( PDLEVEL4, '$', $node );
635  
636 30 ▶ if ( $self->_findNode( $key, \$node, \@path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_findNode($key, \$node, \@path)) 30 0 30
637  
638 # Start at the bottom of the chain
639 30 push @path, $node;
640 30 $key = $node->key;
641  
642 # Find number of nodes that are unbalanced
643 30 $n = scalar grep { abs( $_->balance ) > 1 } @path;
644 30 ▶ $$self[AVLSTATS][STAT_REBALANCE]++ if $$self[AVLPROF] and $n;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[2] and $n 1 29 30
Condition100%3/3
$self->[2] and $n
!ll&&!rl&&r
2541
MC/DC100%2/2
$self->[2] and $n
$self->[2] $n
Truth table100%3/3
A and B
A= $self->[2]B= $n
ABresult
0X0
100
111
645 30 ▶ while ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 14 30 44
646 14 pdebug( 'found %s node(s) in the branch that are unbalanced',
647 PDLEVEL4, $n );
648 14 $node = $parent = undef;
649  
650 14 foreach (@path) {
651 20 $node = $_;
652 20 ▶ if ( abs( $node->balance ) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs $node->balance > 1) { } 14 6 20
653 14 pdebug( 'key: %s balance: %s',
654 PDLEVEL4, $node->key, $node->balance );
655  
656 # Determine type of rotation and execute it
657 14 $self->_rotate( $parent, $node );
658 14 $self->_findNode( $key, \$node, \@path );
659 14 push @path, $node;
660 14 $n = scalar grep { abs( $_->balance ) > 1 } @path;
661 14 last;
662 } else {
663 6 $parent = $node;
664 }
665 }
666 }
667 }
668  
669 30 subPostamble( PDLEVEL4, '$', 1 );
670  
671 30 return 1;
672 }
673  
674 25 ▶sub addPair {
Subroutine addPair: called CC=2 SCAR=6.9
Pod documented
675  
676 # Purpose: Adds or updates a node for the key/value passed
677 # Returns: Boolean
678 # Usage: $rv = $obj->addPair($key, $value);
679  
680 25 my $self = shift;
681 25 my $key = shift;
682 25 my $val = shift;
683 25 my $rv = 1;
684 25 my ( @path, $node );
685  
686 25 subPreamble( PDLEVEL3, '$b', $key, $val );
687  
688 25 $rv = $self->_addNode( $key, $val );
689 25 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 25 0 25
690 25 $self->_findNode( $key, \$node, \@path );
691 25 $rv = $self->_rebalance($node);
692 }
693  
694 25 subPostamble( PDLEVEL3, '$', $rv );
695  
696 25 return $rv;
697 }
698  
699 1 ▶sub _splice {
Subroutine _splice: called CC=10 SCAR=29.0
700  
701 # Purpose: Splices off the requested node and reattaches any
702 # sub-branches, and returns a new target key useful
703 # for tracing for rebalancing purposes.
704 # Returns: String
705 # Usage: $key = $obj->_splice($root, $node);
706  
707 1 my $self = shift;
708 1 my $root = shift;
709 1 my $node = shift;
710 1 my ( $rv, $ln, $rn, $cn, @path, $height );
711  
712 1 subPreamble( PDLEVEL4, '$$', $root, $node );
713  
714 1 $ln = $node->left;
715 1 $rn = $node->right;
716  
717 1 ▶ if ( defined $ln and defined $rn ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ln and defined $rn) { } 1 0 1
Condition33%1/3
defined $ln and defined $rn
!ll&&!rl&&r
001
MC/DC0%0/2
defined $ln and defined $rn
defined $ln defined $rn
Truth table33%1/3
A and B
A= defined $lnB= defined $rn
ABresult
0X0
100
111
718  
719 # Attach the longer branch underneath the shorter branch
720 1 ▶ if ( $ln->height < $rn->height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ln->height < $rn->height) { } 0 1 1
721  
722 # Right branch is longer
723 #
724 # Find a place to attach on the left branch
725 0 push @path, $ln;
726 0 $cn = $ln;
727 0 ▶ while ( defined $cn->right ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cn->right) 0 0 0
728 0 $cn = $cn->right;
729 0 push @path, $cn;
730 }
731 0 $cn->setRight($rn);
732  
733 # Update the height back up to the root of the left branch
734 0 $height = $cn->height;
735 0 foreach ( reverse @path ) {
736 0 ▶ if ( $_->rHeight < $height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->rHeight < $height) 0 0 0
737 0 $_->addRHeight( $height - $_->rHeight );
738 }
739 0 $height++;
740 }
741  
742 # Now, attach the left branch to the root
743 0 $self->_updtRootRef( $root, $node, $ln );
744  
745 # Hand back the node key that we're going to seek to in the
746 # calling function
747 0 $rv = $rn->key;
748  
749 } else {
750  
751 # Left branch is longer
752 #
753 # Find a place to attach on the right branch
754 1 push @path, $rn;
755 1 $cn = $rn;
756 1 ▶ while ( defined $cn->left ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cn->left) 1 1 2
757 1 $cn = $cn->left;
758 1 push @path, $cn;
759 }
760 1 $cn->setLeft($ln);
761  
762 # Update the height back up to the root of the left branch
763 1 $height = $cn->height;
764 1 foreach ( reverse @path ) {
765 2 ▶ if ( $_->lHeight < $height ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->lHeight < $height) 2 0 2
766 2 $_->addLHeight( $height - $_->lHeight );
767 }
768 2 $height++;
769 }
770  
771 # Now, attach the left branch to the root
772 1 $self->_updtRootRef( $root, $node, $rn );
773  
774 # Hand back the node key that we're going to seek to in the
775 # calling function
776 1 $rv = $ln->key;
777  
778 }
779  
780 } else {
781 0 pdebug( 'this function shouldn\'t be called without two branches',
782 PDLEVEL4 );
783 }
784  
785 1 subPostamble( PDLEVEL4, '$', $rv );
786  
787 1 return $rv;
788 }
789  
790 5 ▶sub delNode {
Subroutine delNode: called CC=17 SCAR=31.0
Pod documented
791  
792 # Purpose: Removes the specifed node
793 # Returns: Boolean
794 # Usage: $rv = $obj->delNode($key);
795  
796 5 my $self = shift;
797 5 my $key = shift;
798 5 my $rv = 0;
799 5 my ( $root, $node, @path, $height );
800  
801 5 subPreamble( PDLEVEL3, '$', $key );
802  
803 5 ▶ if ( $self->_findNode( $key, \$node, \@path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_findNode($key, \$node, \@path)) 5 0 5
804 5 $root = $path[-1];
805  
806 # Test for simplest deletion conditions
807 5 ▶ if ( scalar $node->children <= 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar $node->children <= 1) { } 4 1 5
808  
809 # Node for deletion only has one or zero children
810 4 ▶ $rv =
Branch: 2 branches
BranchTrueFalseTotal
defined $node->left ? : 0 4 4
defined $node->right ? : 0 4 4
811 defined $node->left
812 ? $self->_updtRootRef( $root, $node, $node->left )
813 : defined $node->right
814 ? $self->_updtRootRef( $root, $node, $node->right )
815 : $self->_updtRootRef( $root, $node, undef );
816  
817 # Adjust heights
818 4 foreach ( reverse @path ) {
819 11 ▶ if ( $key lt $_->key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key lt $_->key) { } 2 9 11
820 2 ▶ $_->decrLHeight
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->left and $_->left->height < $_->lHeight 0 2 2
Condition33%1/3
defined $_->left and $_->left->height < $_->lHeight
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_->left and $_->left->height < $_->lHeight
defined $_->left $_->left->height < $_->lHeight
Truth table33%1/3
A and B
A= defined $_->leftB= $_->left->height < $_->lHeight
ABresult
0X0
100
111
821 if defined $_->left
822 and $_->left->height < $_->lHeight;
823 } else {
824 9 ▶ $_->decrRHeight
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->right and $_->right->height < $_->rHeight 2 7 9
Condition100%3/3
defined $_->right and $_->right->height < $_->rHeight
!ll&&!rl&&r
252
MC/DC100%2/2
defined $_->right and $_->right->height < $_->rHeight
defined $_->right $_->right->height < $_->rHeight
Truth table100%3/3
A and B
A= defined $_->rightB= $_->right->height < $_->rHeight
ABresult
0X0
100
111
825 if defined $_->right
826 and $_->right->height < $_->rHeight;
827 }
828 }
829  
830 } else {
831  
832 # Splice the node out
833 1 $key = $self->_splice( $root, $node );
834  
835 1 ▶ if ( $self->_findNode( $key, \$node, \@path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_findNode($key, \$node, \@path)) { } 1 0 1
836 1 $rv = 1;
837 1 $root = $node;
838  
839 } else {
840 0 pdebug( 'something went horribly wrong', PDLEVEL1 );
841 }
842  
843 }
844 5 ▶ $$self[AVLSTATS][STAT_DELETES]++ if $$self[AVLSTATS] and $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[3] and $rv 0 5 5
Condition33%1/3
$self->[3] and $rv
!ll&&!rl&&r
500
MC/DC0%0/2
$self->[3] and $rv
$self->[3] $rv
Truth table33%1/3
A and B
A= $self->[3]B= $rv
ABresult
0X0
100
111
845  
846 # Rebalance
847 5 ▶ $root = $$self[AVLROOT] unless defined $root;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $root 0 5 5
848 5 ▶ $rv = $self->_rebalance($root) if defined $root and $rv;
Branch: 1 branch
BranchTrueFalseTotal
if defined $root and $rv 5 0 5
Condition33%1/3
defined $root and $rv
!ll&&!rl&&r
005
MC/DC0%0/2
defined $root and $rv
defined $root $rv
Truth table33%1/3
A and B
A= defined $rootB= $rv
ABresult
0X0
100
111
849 }
850  
851 5 subPostamble( PDLEVEL3, '$', $rv );
852  
853 5 return $rv;
854 }
855  
856 3 ▶sub purgeNodes {
Subroutine purgeNodes: called CC=1 SCAR=0.0
Pod documented
857  
858 # Purpose: Deletes the root reference, essentially purging the entire
859 # tree
860 # Returns: Boolean
861 # Usage: $rv = $obj->purgeNodes;
862  
863 3 my $self = shift;
864  
865 3 subPreamble(PDLEVEL3);
866  
867 3 $$self[AVLROOT] = undef;
868  
869 3 subPostamble( PDLEVEL3, '$', 1 );
870  
871 3 return 1;
872 }
873  
874 5 ▶sub _writeRecord {
Subroutine _writeRecord: called CC=11 SCAR=25.9
875  
876 # Purpose: Writes the passed node to file
877 # Returns: Boolean
878 # Usage: $rv = _writeRecord($filename, $node);
879  
880 5 my $file = shift;
881 5 my $node = shift;
882 5 my ( $rv, $rec, $k, $v, $kf, $vf );
883  
884 # Get key/val
885 5 $k = $node->key;
886 5 $v = $node->val;
887  
888 # Set flag values
889 5 ▶ $kf =
Branch: 2 branches
BranchTrueFalseTotal
!defined($k) ? : 0 5 5
length $k ? : 5 0 5
890 !defined $k ? AVLUNDEF
891 : length $k ? 0
892 : AVLZEROLS;
893 5 ▶ $vf =
Branch: 2 branches
BranchTrueFalseTotal
!defined($v) ? : 0 5 5
length $v ? : 5 0 5
894 !defined $v ? AVLUNDEF
895 : length $v ? 0
896 : AVLZEROLS;
897  
898 {
899 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
900 5 ▶ $rec = pack SIGNATURE, SIG_TYPE, $kf, $vf,
Branch: 2 branches
BranchTrueFalseTotal
$kf ? : 0 5 5
$vf ? : 0 5 5
901 quad2Longs( $kf ? 0 : length $k ),
902 quad2Longs( $vf ? 0 : length $v );
903 5 ▶ $rec .= $k unless $kf;
Branch: 1 branch
BranchTrueFalseTotal
unless $kf 5 0 5
904 5 ▶ $rec .= $v unless $vf;
Branch: 1 branch
BranchTrueFalseTotal
unless $vf 5 0 5
905  
906 5 ▶ $rv = pwrite( $file, $rec ) == length $rec ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
pwrite($file, $rec) == length $rec ? : 5 0 5
907 }
908  
909 5 ▶ pdebug( 'failed to write record', PDLEVEL1 ) unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 5 5
910  
911 5 return $rv;
912 }
913  
914 1 ▶sub save2File {
Subroutine save2File: called CC=18 SCAR=30.2
Pod documented
915  
916 # Purpose: Saves binary tree to a file
917 # Returns: Boolean
918 # Usage: $rv = $obj->save($file);
919  
920 1 my $self = shift;
921 1 my $file = shift;
922 1 my $rv;
923 1 my ( @lc, @rc, @ln, @rn, $node );
924  
925 1 subPreamble( PDLEVEL1, '$', $file );
926  
927 1 ▶ if ( defined $file and length $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and length $file) 1 0 1
Condition33%1/3
defined $file and length $file
!ll&&!rl&&r
001
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table33%1/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
928 1 ▶ if ( popen( $file, O_RDWR | O_CREAT ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (popen($file, 66)) 1 0 1
929 1 pseek( $file, 0, SEEK_SET );
930 1 ptruncate($file);
931  
932 # Start descending the tree one level at a time
933 1 ▶ if ( defined $$self[AVLROOT] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[0]) { } 1 0 1
934 1 $rv = _writeRecord( $file, $$self[AVLROOT] );
935 1 @lc = ( grep {defined} $$self[AVLROOT]->left );
936 1 @rc = ( grep {defined} $$self[AVLROOT]->right );
937  
938 # Note: the whole point of this is to attempt to retrieve
939 # nodes from both sides of the tree in a way that, when read,
940 # will require minimal rebalances.
941  
942 # Start descending and writing
943 1 ▶ while ( $rv and ( @lc or @rc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and @lc || @rc) 2 1 3
Condition66%4/6
$rv and @lc || @rc
!ll&&!rl&&r
012
@lc or @rc
l!l&&r!l&&!r
201
MC/DC33%1/3
$rv and @lc || @rc
$rv @lc @rc
Truth table50%2/4
A and B or C
A= $rvB= @lcC= @rc
ABCresult
0XX0
1000
11X1
1011
944  
945 # Extract a list of all left and right nodes
946 2 @ln = grep {defined} map { $_->left } @lc;
947 2 push @ln, grep {defined} map { $_->right } @lc;
948 2 @rn = grep {defined} map { $_->left } @rc;
949 2 push @rn, grep {defined} map { $_->right } @rc;
950  
951 # Record all of the current level of children
952 2 ▶ while ( $rv and ( @lc or @rc ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and @lc || @rc) 2 2 4
Condition66%4/6
$rv and @lc || @rc
!ll&&!rl&&r
022
@lc or @rc
l!l&&r!l&&!r
202
MC/DC33%1/3
$rv and @lc || @rc
$rv @lc @rc
Truth table50%2/4
A and B or C
A= $rvB= @lcC= @rc
ABCresult
0XX0
1000
11X1
1011
953  
954 # Shift off of the left side
955 2 $node = shift @lc;
956 2 ▶ $rv = _writeRecord( $file, $node ) if defined $node;
Branch: 1 branch
BranchTrueFalseTotal
if defined $node 2 0 2
957  
958 # Shift off of the right side
959 2 $node = shift @rc;
960 2 ▶ $rv = _writeRecord( $file, $node )
Branch: 1 branch
BranchTrueFalseTotal
if $rv and defined $node 1 1 2
Condition66%2/3
$rv and defined $node
!ll&&!rl&&r
011
MC/DC50%1/2
$rv and defined $node
$rv defined $node
Truth table66%2/3
A and B
A= $rvB= defined $node
ABresult
0X0
100
111
961 if $rv and defined $node;
962  
963 # Pop off of the left side
964 2 $node = pop @lc;
965 2 ▶ $rv = _writeRecord( $file, $node )
Branch: 1 branch
BranchTrueFalseTotal
if $rv and defined $node 1 1 2
Condition66%2/3
$rv and defined $node
!ll&&!rl&&r
011
MC/DC50%1/2
$rv and defined $node
$rv defined $node
Truth table66%2/3
A and B
A= $rvB= defined $node
ABresult
0X0
100
111
966 if $rv and defined $node;
967  
968 # Pop off of the right side
969 2 $node = pop @rc;
970 2 ▶ $rv = _writeRecord( $file, $node )
Branch: 1 branch
BranchTrueFalseTotal
if $rv and defined $node 0 2 2
Condition33%1/3
$rv and defined $node
!ll&&!rl&&r
020
MC/DC0%0/2
$rv and defined $node
$rv defined $node
Truth table33%1/3
A and B
A= $rvB= defined $node
ABresult
0X0
100
111
971 if $rv and defined $node;
972  
973 }
974  
975 # Start with the next level
976 2 @lc = @ln;
977 2 @rc = @rn;
978 }
979  
980 } else {
981 0 pdebug( 'nothing in the tree to write', PDLEVEL2 );
982 0 $rv = 1;
983 }
984  
985 1 pclose($file);
986 }
987 }
988  
989 1 subPostamble( PDLEVEL1, '$', $rv );
990  
991 1 return $rv;
992 }
993  
994 5 ▶sub _readRecord {
Subroutine _readRecord: called CC=18 SCAR=38.6
995  
996 # Purpose: Reads the node from the file
997 # Returns: Boolean
998 # Usage: $rv = _readRecord($self, $filename);
999  
1000 5 my $self = shift;
1001 5 my $file = shift;
1002 5 my ( $rv, $node, $sig, $content );
1003 5 my ( $stype, $kf, $vf, $kl, $vl, $kv, $vv );
1004 5 my ( $kl1, $kl2, $vl1, $vl2 );
1005  
1006 # Read Signature
1007 5 ▶ if ( pread( $file, $sig, SIG_LEN ) == SIG_LEN ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&pread($file, \$sig, 28) == 28) { } 5 0 5
1008 5 ( $stype, $kf, $vf, $kl1, $kl2, $vl1, $vl2 ) = unpack SIGNATURE, $sig;
1009 5 ▶ if ( $stype eq SIG_TYPE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stype eq 'PDAVL') { } 5 0 5
1010 5 $rv = 1;
1011 5 $kl = longs2Quad( $kl1, $kl2 );
1012 5 $vl = longs2Quad( $vl1, $vl2 );
1013  
1014 5 ▶ if ( !defined $kl or !defined $vl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $kl and defined $vl) 0 5 5
Condition33%1/3
defined $kl and defined $vl
!ll&&!rl&&r
005
MC/DC0%0/2
defined $kl and defined $vl
defined $kl defined $vl
Truth table33%1/3
A and B
A= defined $klB= defined $vl
ABresult
0X0
100
111
1015 0 $rv = 0;
1016 0 pdebug( '64-bit values not supported on 32-bit platforms',
1017 PDLEVEL1 );
1018 }
1019 } else {
1020 0 pdebug( 'PDAVL signature failed basic validation: %s',
1021 PDLEVEL1, $sig );
1022 }
1023 } else {
1024 0 pdebug( 'failed to read PDAVL signature', PDLEVEL1 );
1025 }
1026  
1027 # Extract key/val lengths/values
1028 5 ▶ if ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv) 5 0 5
1029 5 ▶ if ($kf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kf) 0 5 5
1030 0 ▶ $kv = '' if $kf == AVLZEROLS;
Branch: 1 branch
BranchTrueFalseTotal
if $kf == 1 0 0 0
1031 }
1032 5 ▶ if ($vf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vf) 0 5 5
1033 0 ▶ $vv = '' if $vf == AVLZEROLS;
Branch: 1 branch
BranchTrueFalseTotal
if $vf == 1 0 0 0
1034 }
1035 }
1036  
1037 # Read key
1038 5 ▶ if ( $rv and $kl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $kl) 5 0 5
Condition33%1/3
$rv and $kl
!ll&&!rl&&r
005
MC/DC0%0/2
$rv and $kl
$rv $kl
Truth table33%1/3
A and B
A= $rvB= $kl
ABresult
0X0
100
111
1039 5 ▶ if ( pread( $file, $content, $kl ) == $kl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&pread($file, \$content, $kl) == $kl) { } 5 0 5
1040 5 $kv = $content;
1041 } else {
1042 0 pdebug( 'failed to read full length of key content', PDLEVEL1 );
1043 0 $rv = 0;
1044 }
1045 }
1046  
1047 # Read value
1048 5 ▶ if ( $rv and $vl ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rv and $vl) 5 0 5
Condition33%1/3
$rv and $vl
!ll&&!rl&&r
005
MC/DC0%0/2
$rv and $vl
$rv $vl
Truth table33%1/3
A and B
A= $rvB= $vl
ABresult
0X0
100
111
1049 5 ▶ if ( pread( $file, $content, $vl ) == $vl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&pread($file, \$content, $vl) == $vl) { } 5 0 5
1050 5 $vv = $content;
1051 } else {
1052 0 pdebug( 'failed to read full length of key content', PDLEVEL1 );
1053 0 $rv = 0;
1054 }
1055 }
1056  
1057 # Add the key/pair
1058 5 ▶ $rv = $self->addPair( $kv, $vv ) if $rv;
Branch: 1 branch
BranchTrueFalseTotal
if $rv 5 0 5
1059  
1060 5 ▶ pdebug( 'failed to read record', PDLEVEL1 ) unless $rv;
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 5 5
1061  
1062 5 return $rv;
1063 }
1064  
1065 1 ▶sub loadFromFile {
Subroutine loadFromFile: called CC=4 SCAR=13.9
Pod documented
1066  
1067 # Purpose: Loads content from file
1068 # Returns: Boolean
1069 # Usage: $rv = $obj->loadFromFile($file);
1070  
1071 1 my $self = shift;
1072 1 my $file = shift;
1073 1 my ( $rv, $eof );
1074  
1075 1 subPreamble( PDLEVEL1, '$', $file );
1076  
1077 # Purge current hash contents
1078 1 $self->purgeNodes;
1079  
1080 # Make sure file is open and at the beginning
1081 1 ▶ if ( defined popen( $file, O_RDWR ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined popen($file, 2)) 1 0 1
1082 1 $eof = pseek( $file, 0, SEEK_END );
1083 1 pseek( $file, 0, SEEK_SET );
1084  
1085 # Read records
1086 1 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$rv and ptell($file) != $eof and do { do { $rv = _readRecord($self, $file) } } 4 1 5
Condition66%2/3
$rv and ptell($file) != $eof
!ll&&!rl&&r
014
MC/DC50%1/2
$rv and ptell($file) != $eof
$rv ptell($file) != $eof
Truth table66%2/3
A and B
A= $rvB= ptell($file) != $eof
ABresult
0X0
100
111
1087 5 $rv = _readRecord( $self, $file );
1088 } while $rv and ptell($file) != $eof;
1089  
1090 1 pclose($file);
1091 }
1092  
1093 1 subPostamble( PDLEVEL1, '$', $rv );
1094  
1095 1 return $rv;
1096 }
1097  
1098 1 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
1099 1 return new Paranoid::Data::AVLTree;
1100 }
1101  
1102 1 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
1103 1 my $self = shift;
1104 1 my $key = shift;
1105 1 my $rv;
1106  
1107 1 return $self->fetchVal($key);
1108 }
1109  
1110 10 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
1111 10 my $self = shift;
1112 10 my $key = shift;
1113 10 my $val = shift;
1114  
1115 10 return $self->addPair( $key, $val );
1116 }
1117  
1118 3 ▶sub EXISTS {
Subroutine EXISTS: called CC=1 SCAR=0.0
1119 3 my $self = shift;
1120 3 my $key = shift;
1121  
1122 3 return $self->nodeExists($key);
1123 }
1124  
1125 1 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
1126 1 my $self = shift;
1127 1 my $key = shift;
1128  
1129 1 return $self->delNode($key);
1130 }
1131  
1132 1 ▶sub CLEAR {
Subroutine CLEAR: called CC=1 SCAR=0.0
1133 1 my $self = shift;
1134  
1135 1 return $self->purgeNodes;
1136 }
1137  
1138 5 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=2 SCAR=6.9
1139 5 my $self = shift;
1140 5 my @k = $self->nodeKeys();
1141 5 my ( $key, $node, @path, %rv );
1142  
1143 5 ▶ if (@k) {
Branch: 1 branch
BranchTrueFalseTotal
if (@k) 3 2 5
1144 3 $key = shift @k;
1145 3 $self->_findNode( $key, \$node, \@path );
1146 3 %rv = ( $node->key() => $node->val() );
1147 3 $$self[AVLKEYS] = [@k];
1148 }
1149  
1150 5 return each %rv;
1151 }
1152  
1153 18 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=3 SCAR=11.0
1154 18 my $self = shift;
1155 18 my ( $key, $node, @path, %rv );
1156  
1157 18 ▶ if ( defined $$self[AVLKEYS] and scalar @{ $$self[AVLKEYS] } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->[1] and scalar @{$self->[1];}) 15 3 18
Condition50%1/2
defined $self->[1] and scalar @{$self->[1];}
!ll
018
MC/DC0%0/1
defined $self->[1] and scalar @{$self->[1];}
defined $self->[1]
Truth table50%1/2
A
A= defined $self->[1]
Aresult
00
11
1158 15 $key = shift @{ $$self[AVLKEYS] };
1159 15 $self->_findNode( $key, \$node, \@path );
1160 15 %rv = ( $node->key() => $node->val() );
1161 }
1162  
1163 18 return each %rv;
1164 }
1165  
1166 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=1 SCAR=6.9
1167 0 my $self = shift;
1168  
1169 0 return $self->count;
1170 }
1171  
1172 0 ▶sub UNTIE {
Subroutine UNTIE: not called CC=1 SCAR=6.9
1173 0 return 1;
1174 }
1175  
1176 2 1;
1177  
1178 __END__
« Paranoid/Input.pm ↑ Summary Paranoid/IO.pm »