Paranoid/Data/AVLTree/AVLNode.pm

filter: Statementstmt 89.6% 104 / 116 Branchbran 95.0% 19 / 20 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 91.1% 31 / 34 Podpod 100% 21 / 21 totaltotal 89.8% 176 / 196 CC 12 SCAR 5.5
CC
12
Cov
89%
new
3.1
count
3.0
updtHeights
3.0
CRAP 1.7
« Paranoid/Log/Buffer.pm ↑ Summary
1 # Paranoid::Data::AVLTree::AVLNode -- AVL Tree Node Object Class
2 #
3 # $Id: lib/Paranoid/Data/AVLTree/AVLNode.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::AVLNode;
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 Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 2 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
44  
45 2 ▶use constant AVLKEY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶use constant AVLVAL => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 2 ▶use constant AVLRIGHT => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 2 ▶use constant AVLLEFT => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 2 ▶use constant AVLRHEIGHT => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use constant AVLLHEIGHT => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 #####################################################################
53 #
54 # Module code follows
55 #
56 #####################################################################
57  
58 25 ▶sub new {
Subroutine new: called CC=3 SCAR=11.4
Pod documented
59  
60 # Purpose: Instantiates an AVLNode object
61 # Returns: Object reference if successful, undef otherwise
62 # Usage: $obj = Paranoid::Data::AVLTree::AVLNode->new(
63 # $key, $val
64 # );
65  
66 25 my $class = shift;
67 25 my $key = shift;
68 25 my $val = shift;
69 25 my $self = [];
70  
71 25 bless $self, $class;
72 25 ▶ if ( defined $key and length $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and length $key) { } 25 0 25
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
0025
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
73 25 $$self[AVLKEY] = $key;
74 25 $$self[AVLVAL] = $val;
75 25 $$self[AVLRHEIGHT] = 0;
76 25 $$self[AVLLHEIGHT] = 0;
77 } else {
78 0 $self = undef;
79 }
80  
81 25 return $self;
82 }
83  
84 855 ▶sub key {
Subroutine key: called CC=1 SCAR=0.0
Pod documented
85  
86 # Purpose: Returns the node key
87 # Returns: String
88 # Usage: $key = $obj->key;
89  
90 855 my $self = shift;
91 855 return $$self[AVLKEY];
92 }
93  
94 35 ▶sub val {
Subroutine val: called CC=1 SCAR=0.0
Pod documented
95  
96 # Purpose: Returns the node value
97 # Returns: Scalar/undef
98 # Usage: $val = $node->val;
99  
100 35 my $self = shift;
101 35 return $$self[AVLVAL];
102 }
103  
104 0 ▶sub setVal {
Subroutine setVal: not called CC=1 SCAR=6.9
Pod documented
105  
106 # Purpose: Sets the node value
107 # Returns: Boolean
108 # Usage: $rv = $obj->setVal($val);
109  
110 0 my $self = shift;
111 0 my $val = shift;
112  
113 0 $$self[AVLVAL] = $val;
114  
115 0 return 1;
116 }
117  
118 256 ▶sub right {
Subroutine right: called CC=1 SCAR=0.0
Pod documented
119  
120 # Purpose: Returns the right-side node reference
121 # Returns: AVLNode ref/undef
122 # Usage: $ref = $obj->right;
123  
124 256 my $self = shift;
125 256 return $$self[AVLRIGHT];
126 }
127  
128 34 ▶sub setRight {
Subroutine setRight: called CC=2 SCAR=6.9
Pod documented
129  
130 # Purpose: Sets the right-side node reference
131 # Returns: Boolean
132 # Usage: $rv = $obj->setRight($node);
133  
134 34 my $self = shift;
135 34 my $val = shift;
136  
137 34 $$self[AVLRIGHT] = $val;
138 34 ▶ $$self[AVLRHEIGHT] = defined $val ? $val->height : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 24 10 34
139  
140 34 return 1;
141 }
142  
143 185 ▶sub left {
Subroutine left: called CC=1 SCAR=0.0
Pod documented
144  
145 # Purpose: Returns the left-side node reference
146 # Returns: AVLNode ref/undef
147 # Usage: $ref = $obj->left;
148  
149 185 my $self = shift;
150 185 return $$self[AVLLEFT];
151 }
152  
153 25 ▶sub setLeft {
Subroutine setLeft: called CC=2 SCAR=6.9
Pod documented
154  
155 # Purpose: Sets the left-side node reference
156 # Returns: Boolean
157 # Usage: $rv = $obj->setLeft($node);
158  
159 25 my $self = shift;
160 25 my $val = shift;
161  
162 25 $$self[AVLLEFT] = $val;
163 25 ▶ $$self[AVLLHEIGHT] = defined $val ? $val->height : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 20 5 25
164  
165 25 return 1;
166 }
167  
168 30 ▶sub incrRHeight {
Subroutine incrRHeight: called CC=1 SCAR=0.0
Pod documented
169  
170 # Purpose: Increments the right-side branch height
171 # Returns: Boolean
172 # Usage: $rv = $obj->incrRHeight;
173  
174 30 my $self = shift;
175  
176 30 $$self[AVLRHEIGHT]++;
177  
178 30 return 1;
179 }
180  
181 18 ▶sub incrLHeight {
Subroutine incrLHeight: called CC=1 SCAR=0.0
Pod documented
182  
183 # Purpose: Increments the left-side branch height
184 # Returns: Boolean
185 # Usage: $rv = $obj->incrLHeight;
186  
187 18 my $self = shift;
188  
189 18 $$self[AVLLHEIGHT]++;
190  
191 18 return 1;
192 }
193  
194 0 ▶sub addRHeight {
Subroutine addRHeight: not called CC=1 SCAR=6.9
Pod documented
195  
196 # Purpose: Adds the passed value to the right-side height
197 # Returns: Boolean
198 # Usage: $rv = $obj->addRHeight($n);
199  
200 0 my $self = shift;
201 0 my $n = shift;
202  
203 0 $$self[AVLRHEIGHT] += $n;
204  
205 0 return 1;
206 }
207  
208 2 ▶sub addLHeight {
Subroutine addLHeight: called CC=1 SCAR=0.0
Pod documented
209  
210 # Purpose: Adds the passed value to the left-side height
211 # Returns: Boolean
212 # Usage: $rv = $obj->addLHeight($n);
213  
214 2 my $self = shift;
215 2 my $n = shift;
216  
217 2 $$self[AVLLHEIGHT] += $n;
218  
219 2 return 1;
220 }
221  
222 2 ▶sub decrRHeight {
Subroutine decrRHeight: called CC=1 SCAR=0.0
Pod documented
223  
224 # Purpose: Decrements the right-side branch height
225 # Returns: Boolean
226 # Usage: $rv = $obj->decrRHeight;
227  
228 2 my $self = shift;
229  
230 2 $$self[AVLRHEIGHT]--;
231  
232 2 return 1;
233 }
234  
235 0 ▶sub decrLHeight {
Subroutine decrLHeight: not called CC=1 SCAR=6.9
Pod documented
236  
237 # Purpose: Decrements the left-side branch height
238 # Returns: Boolean
239 # Usage: $rv = $obj->decrLHeight;
240  
241 0 my $self = shift;
242  
243 0 $$self[AVLLHEIGHT]--;
244  
245 0 return 1;
246 }
247  
248 196 ▶sub balance {
Subroutine balance: called CC=1 SCAR=0.0
Pod documented
249  
250 # Purpose: Returns the current balance of right/left-side branch heights
251 # Returns: Integer
252 # Usage: $balance = $obj->balance;
253  
254 196 my $self = shift;
255  
256 196 return $$self[AVLRHEIGHT] - $$self[AVLLHEIGHT];
257 }
258  
259 41 ▶sub count {
Subroutine count: called CC=3 SCAR=11.0
Pod documented
260  
261 # Purpose: Returns the count of nodes from this node and its sub-branches
262 # Returns: Integer
263 # Usage: $count = $obj->count;
264  
265 41 my $self = shift;
266 41 my $rv = 1;
267  
268 41 ▶ $rv += $$self[AVLRIGHT]->count if defined $$self[AVLRIGHT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[2] 17 24 41
269 41 ▶ $rv += $$self[AVLLEFT]->count if defined $$self[AVLLEFT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[3] 17 24 41
270  
271 41 return $rv;
272 }
273  
274 128 ▶sub height {
Subroutine height: called CC=2 SCAR=6.9
Pod documented
275  
276 # Purpose: Returns the height of this node and its longest sub-branch
277 # Returns: Integer
278 # Usage: $height = $obj->height;
279  
280 128 my $self = shift;
281  
282 128 ▶ return 1 + (
Branch: 1 branch
BranchTrueFalseTotal
$self->[4] > $self->[5] ? : 29 99 128
283 $$self[AVLRHEIGHT] > $$self[AVLLHEIGHT]
284 ? $$self[AVLRHEIGHT]
285 : $$self[AVLLHEIGHT] );
286 }
287  
288 7 ▶sub rHeight {
Subroutine rHeight: called CC=1 SCAR=0.0
Pod documented
289  
290 # Purpose: Returns the height of the right-side sub-branch
291 # Returns: Integer
292 # Usage: $height = $obj->rHeight;
293  
294 7 my $self = shift;
295  
296 7 return $$self[AVLRHEIGHT];
297 }
298  
299 4 ▶sub lHeight {
Subroutine lHeight: called CC=1 SCAR=0.0
Pod documented
300  
301 # Purpose: Returns the height of the left-side sub-branch
302 # Returns: Integer
303 # Usage: $height = $obj->lHeight;
304  
305 4 my $self = shift;
306  
307 4 return $$self[AVLLHEIGHT];
308 }
309  
310 42 ▶sub updtHeights {
Subroutine updtHeights: called CC=3 SCAR=11.0
Pod documented
311  
312 # Purpose: Brute force method of recalculating all sub-branch heights
313 # Returns: Boolean
314 # Usage: $rv = $obj->updtHeights;
315  
316 42 my $self = shift;
317  
318 42 ▶ $$self[AVLRHEIGHT] =
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[2] ? : 31 11 42
319 defined $$self[AVLRIGHT]
320 ? $$self[AVLRIGHT]->height
321 : 0;
322 42 ▶ $$self[AVLLHEIGHT] =
Branch: 1 branch
BranchTrueFalseTotal
defined $self->[3] ? : 33 9 42
323 defined $$self[AVLLEFT] ? $$self[AVLLEFT]->height : 0;
324  
325 42 return 1;
326 }
327  
328 5 ▶sub children {
Subroutine children: called CC=3 SCAR=11.0
Pod documented
329  
330 # Purpose: Returns all nodes linked to from this node
331 # Returns: Array of AVLNode refs
332 # Usage: @crefs = $obj->children;
333  
334 5 my $self = shift;
335 5 my @rv;
336  
337 5 ▶ push @rv, $$self[AVLRIGHT] if defined $$self[AVLRIGHT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[2] 1 4 5
338 5 ▶ push @rv, $$self[AVLLEFT] if defined $$self[AVLLEFT];
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->[3] 1 4 5
339  
340 5 return @rv;
341 }
342  
343 2 1;
344  
345 __END__
« Paranoid/Log/Buffer.pm ↑ Summary