Code/Includable/Tree/NodeMethods.pm

filter: Statementstmt 92.5% 174 / 188 Branchbran 69.5% 57 / 82 Conditioncond 80.0% 4 / 5 MC/DCmcdc 66.6% 2 / 3 Subroutinesub 87.1% 27 / 31 Podpod 100% 26 / 26 totaltotal 86.5% 290 / 335 CC 54 SCAR 13.4
CC
54
Cov
85%
check
8.0
_children_as_list
6.1
walk
6.0
CRAP 3.8
« Code/Includable/Tree/FromObjArray.pm ↑ Summary Test/Role/TinyCommons/Tree.pm »
1 package Code::Includable::Tree::NodeMethods;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 7 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
6 7 our $DATE = '2021-10-07'; # DATE
7 7 our $DIST = 'RoleBundle-TinyCommons-Tree'; # DIST
8 7 our $VERSION = '0.129'; # VERSION
9  
10 7 our $IGNORE_NO_CHILDREN_METHOD = 1;
11  
12 7 our $GET_PARENT_METHOD = 'parent';
13 7 our $GET_CHILDREN_METHOD = 'children';
14 7 our $SET_PARENT_METHOD = 'parent';
15 7 our $SET_CHILDREN_METHOD = 'children';
16  
17 # we must contain no other functions
18  
19 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # like children, but always return list
22 320 ▶sub _children_as_list {
Subroutine _children_as_list: called CC=6 SCAR=18.1
23 320 my $self = shift;
24 320 my @children;
25 320 ▶ if ($IGNORE_NO_CHILDREN_METHOD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Code::Includable::Tree::NodeMethods::IGNORE_NO_CHILDREN_METHOD) { } 320 0 320
26 320 eval {
27 320 @children = $self->$GET_CHILDREN_METHOD;
28 };
29 320 ▶ return () if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 320 320
30 } else {
31 0 @children = $self->$GET_CHILDREN_METHOD;
32 }
33  
34 320 ▶ if (@children == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@children == 1) 243 77 320
35 243 ▶ return () unless defined($children[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $children[0] 36 207 243
36 207 ▶ return @{$children[0]} if ref($children[0]) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $children[0] eq "ARRAY" 199 8 207
37 }
38 85 @children;
39 }
40  
41 # direct children first
42 80 ▶sub _descendants {
Subroutine _descendants: called CC=2 SCAR=6.9
43 80 my ($self, $res) = @_;
44 80 my @children = _children_as_list($self);
45 80 push @$res, @children;
46 80 for (@children) { _descendants($_, $res) }
47 }
48  
49 8 ▶sub descendants {
Subroutine descendants: called CC=1 SCAR=0.0
Pod documented
50 8 my $self = shift;
51 8 my $res = [];
52 8 _descendants($self, $res);
53 8 @$res;
54 }
55  
56 40 ▶sub _descendants_depth_first {
Subroutine _descendants_depth_first: called CC=2 SCAR=6.9
57 40 my ($self, $res) = @_;
58 40 my @children = _children_as_list($self);
59 40 for (@children) {
60 36 push @$res, $_;
61 36 _descendants_depth_first($_, $res);
62 }
63 }
64  
65 4 ▶sub descendants_depth_first {
Subroutine descendants_depth_first: called CC=1 SCAR=0.0
Pod documented
66 4 my $self = shift;
67 4 my $res = [];
68 4 _descendants_depth_first($self, $res);
69 4 @$res;
70 }
71  
72 9 ▶sub ancestors {
Subroutine ancestors: called CC=2 SCAR=6.9
Pod documented
73 9 my $self = shift;
74 9 my @res;
75 9 my $parent = $self->$GET_PARENT_METHOD;
76 9 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 14 9 23
77 14 push @res, $parent;
78 14 $parent = $parent->$GET_PARENT_METHOD;
79 }
80 9 @res;
81 }
82  
83 4 ▶sub retrieve_parent {
Subroutine retrieve_parent: called CC=1 SCAR=0.0
Pod documented
84 4 my $self = shift;
85 4 $self->$GET_PARENT_METHOD;
86 }
87  
88 0 ▶sub walk {
Subroutine walk: not called CC=2 SCAR=17.9
Pod documented
89 0 my ($self, $code) = @_;
90 0 for (descendants($self)) {
91 0 $code->($_);
92 }
93 }
94  
95 4 ▶sub first_node {
Subroutine first_node: called CC=3 SCAR=11.1
Pod documented
96 4 my ($self, $code) = @_;
97 4 for (descendants($self)) {
98 20 ▶ return $_ if $code->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$code($_) 4 16 20
99 }
100 0 undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
101 }
102  
103 12 ▶sub is_first_child {
Subroutine is_first_child: called CC=3 SCAR=11.0
Pod documented
104 12 my $self = shift;
105 12 my $parent = $self->$GET_PARENT_METHOD;
106 12 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 4 8 12
107 8 my @siblings = _children_as_list($parent);
108 8 ▶ @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[0]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings and builtin::refaddr($self) == builtin::refaddr($siblings[0]) 8 0 8
109 }
110  
111 12 ▶sub is_last_child {
Subroutine is_last_child: called CC=3 SCAR=11.0
Pod documented
112 12 my $self = shift;
113 12 my $parent = $self->$GET_PARENT_METHOD;
114 12 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 4 8 12
115 8 my @siblings = _children_as_list($parent);
116 8 ▶ @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[-1]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings and builtin::refaddr($self) == builtin::refaddr($siblings[-1]) 8 0 8
117 }
118  
119 12 ▶sub is_only_child {
Subroutine is_only_child: called CC=2 SCAR=6.9
Pod documented
120 12 my $self = shift;
121 12 my $parent = $self->$GET_PARENT_METHOD;
122 12 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 4 8 12
123 8 my @siblings = _children_as_list($parent);
124 8 @siblings==1;# && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[0]);
125 }
126  
127 12 ▶sub is_nth_child {
Subroutine is_nth_child: called CC=3 SCAR=11.3
Pod documented
128 12 my ($self, $n) = @_;
129 12 my $parent = $self->$GET_PARENT_METHOD;
130 12 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 12 12
131 12 my @siblings = _children_as_list($parent);
132 12 ▶ @siblings >= $n && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[$n-1]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings >= $n and builtin::refaddr($self) == builtin::refaddr($siblings[$n - 1]) 12 0 12
133 }
134  
135 12 ▶sub is_nth_last_child {
Subroutine is_nth_last_child: called CC=3 SCAR=11.3
Pod documented
136 12 my ($self, $n) = @_;
137 12 my $parent = $self->$GET_PARENT_METHOD;
138 12 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 12 12
139 12 my @siblings = _children_as_list($parent);
140 12 ▶ @siblings >= $n && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[-$n]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings >= $n and builtin::refaddr($self) == builtin::refaddr($siblings[-$n]) 12 0 12
141 }
142  
143 20 ▶sub is_first_child_of_type {
Subroutine is_first_child_of_type: called CC=3 SCAR=11.2
Pod documented
144 20 my $self = shift;
145 20 my $parent = $self->$GET_PARENT_METHOD;
146 20 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 20 20
147 20 my $type = ref($self);
148 20 my @siblings = grep { ref($_) eq $type } _children_as_list($parent);
149 20 ▶ @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[0]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings and builtin::refaddr($self) == builtin::refaddr($siblings[0]) 20 0 20
150 }
151  
152 40 ▶sub is_last_child_of_type {
Subroutine is_last_child_of_type: called CC=3 SCAR=11.2
Pod documented
153 40 my $self = shift;
154 40 my $parent = $self->$GET_PARENT_METHOD;
155 40 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 40 40
156 40 my $type = ref($self);
157 40 my @siblings = grep { ref($_) eq $type } _children_as_list($parent);
158 40 ▶ @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[-1]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings and builtin::refaddr($self) == builtin::refaddr($siblings[-1]) 40 0 40
159 }
160  
161 8 ▶sub is_only_child_of_type {
Subroutine is_only_child_of_type: called CC=2 SCAR=7.0
Pod documented
162 8 my $self = shift;
163 8 my $parent = $self->$GET_PARENT_METHOD;
164 8 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 8 8
165 8 my $type = ref($self);
166 8 my @siblings = grep { ref($_) eq $type } _children_as_list($parent);
167 8 @siblings == 1; # && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[0]);
168 }
169  
170 16 ▶sub is_nth_child_of_type {
Subroutine is_nth_child_of_type: called CC=3 SCAR=11.2
Pod documented
171 16 my ($self, $n) = @_;
172 16 my $parent = $self->$GET_PARENT_METHOD;
173 16 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 16 16
174 16 my $type = ref($self);
175 16 my @siblings = grep { ref($_) eq $type } _children_as_list($parent);
176 16 ▶ @siblings >= $n && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[$n-1]);
Branch: 1 branch
BranchTrueFalseTotal
@siblings >= $n and builtin::refaddr($self) == builtin::refaddr($siblings[$n - 1]) 16 0 16
177 }
178  
179 16 ▶sub is_nth_last_child_of_type {
Subroutine is_nth_last_child_of_type: called CC=3 SCAR=11.2
Pod documented
180 16 my ($self, $n) = @_;
181 16 my $parent = $self->$GET_PARENT_METHOD;
182 16 ▶ return 0 unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 16 16
183 16 my $type = ref($self);
184 16 my @children = grep { ref($_) eq $type } _children_as_list($parent);
185 16 ▶ @children >= $n && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($children[-$n]);
Branch: 1 branch
BranchTrueFalseTotal
@children >= $n and builtin::refaddr($self) == builtin::refaddr($children[-$n]) 16 0 16
186 }
187  
188 12 ▶sub prev_sibling {
Subroutine prev_sibling: called CC=4 SCAR=13.9
Pod documented
189 12 my $self = shift;
190 12 ▶ my $parent = $self->$GET_PARENT_METHOD or return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD or return undef 12 0 12
191 12 my $refaddr = Scalar::Util::refaddr($self);
192 12 my @siblings = _children_as_list($parent);
193 12 for my $i (1..$#siblings) {
194 40 ▶ if (Scalar::Util::refaddr($siblings[$i]) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($siblings[$i]) == $refaddr) 8 32 40
195 8 return $siblings[$i-1];
196 }
197 }
198 4 undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
199 }
200  
201 8 ▶sub prev_siblings {
Subroutine prev_siblings: called CC=4 SCAR=13.9
Pod documented
202 8 my $self = shift;
203 8 ▶ my $parent = $self->$GET_PARENT_METHOD or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD or return () 8 0 8
204 8 my $refaddr = Scalar::Util::refaddr($self);
205 8 my @siblings = _children_as_list($parent);
206 8 for my $i (1..$#siblings) {
207 24 ▶ if (Scalar::Util::refaddr($siblings[$i]) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($siblings[$i]) == $refaddr) 4 20 24
208 4 return @siblings[0..$i-1];
209 }
210 }
211 4 ();
212 }
213  
214 12 ▶sub next_sibling {
Subroutine next_sibling: called CC=4 SCAR=13.9
Pod documented
215 12 my $self = shift;
216 12 ▶ my $parent = $self->$GET_PARENT_METHOD or return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD or return undef 12 0 12
217 12 my $refaddr = Scalar::Util::refaddr($self);
218 12 my @siblings = _children_as_list($parent);
219 12 for my $i (0..$#siblings-1) {
220 32 ▶ if (Scalar::Util::refaddr($siblings[$i]) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($siblings[$i]) == $refaddr) 8 24 32
221 8 return $siblings[$i+1];
222 }
223 }
224 4 undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
225 }
226  
227 8 ▶sub next_siblings {
Subroutine next_siblings: called CC=4 SCAR=13.9
Pod documented
228 8 my $self = shift;
229 8 ▶ my $parent = $self->$GET_PARENT_METHOD or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD or return () 8 0 8
230 8 my $refaddr = Scalar::Util::refaddr($self);
231 8 my @siblings = _children_as_list($parent);
232 8 for my $i (0..$#siblings-1) {
233 28 ▶ if (Scalar::Util::refaddr($siblings[$i]) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($siblings[$i]) == $refaddr) 4 24 28
234 4 return @siblings[$i+1 .. $#siblings];
235 }
236 }
237 4 ();
238 }
239  
240 4 ▶sub is_root {
Subroutine is_root: called CC=2 SCAR=6.9
Pod documented
241 4 my ($self, $n) = @_;
242 4 my $parent = $self->$GET_PARENT_METHOD;
243 4 ▶ return $parent ? 0:1;
Branch: 1 branch
BranchTrueFalseTotal
$parent ? : 2 2 4
244 }
245  
246 0 ▶sub has_min_children {
Subroutine has_min_children: not called CC=1 SCAR=6.9
Pod documented
247 0 my ($self, $m) = @_;
248 0 my @children = _children_as_list($self);
249 0 @children >= $m;
250 }
251  
252 0 ▶sub has_max_children {
Subroutine has_max_children: not called CC=1 SCAR=6.9
Pod documented
253 0 my ($self, $n) = @_;
254 0 my @children = _children_as_list($self);
255 0 @children <= $n;
256 }
257  
258 0 ▶sub has_children_between {
Subroutine has_children_between: not called CC=2 SCAR=17.9
Pod documented
259 0 my ($self, $m, $n) = @_;
260 0 my @children = _children_as_list($self);
261 0 ▶ @children >= $m && @children <= $n;
Branch: 1 branch
BranchTrueFalseTotal
@children >= $m and @children <= $n 0 0 0
262 }
263  
264 # remove self from parent
265 4 ▶sub remove {
Subroutine remove: called CC=4 SCAR=13.9
Pod documented
266 4 my $self = shift;
267 4 ▶ my $parent = $self->$GET_PARENT_METHOD or return;
Branch: 1 branch
BranchTrueFalseTotal
my $parent = $self->$Code::Includable::Tree::NodeMethods::GET_PARENT_METHOD or return 4 0 4
268 4 my $refaddr = Scalar::Util::refaddr($self);
269 4 my @remaining_siblings;
270 4 for my $sibling (_children_as_list($parent)) {
271 12 ▶ if (Scalar::Util::refaddr($sibling) == $refaddr) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($sibling) == $refaddr) 4 8 12
272 4 $sibling->$SET_PARENT_METHOD(undef);
273 4 next;
274 }
275 8 push @remaining_siblings, $sibling;
276 }
277 4 $parent->$SET_CHILDREN_METHOD(\@remaining_siblings);
278 }
279  
280 # check references
281 10 ▶sub check {
Subroutine check: called CC=8 SCAR=20.8
Pod documented
282 10 my $self = shift;
283 10 ▶ my $opts = shift // {};
Condition100%2/2
shift() // {}
l!l
64
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
284  
285 10 ▶ if ($opts->{check_root}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"check_root"}) 4 6 10
286 4 my $parent = $self->$GET_PARENT_METHOD;
287 4 ▶ defined $parent and die "check: parent is not undef";
Branch: 1 branch
BranchTrueFalseTotal
defined $parent and die "check: parent is not undef" 2 2 4
288 }
289  
290 # check that all children refers back to me in their parent
291 8 my $refaddr = Scalar::Util::refaddr($self);
292 8 my $i = 0;
293 8 for my $child (_children_as_list($self)) {
294 12 my $childs_parent = $child->$GET_PARENT_METHOD;
295 12 ▶ unless (defined $childs_parent &&
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $childs_parent and builtin::refaddr($childs_parent) == $refaddr) 4 8 12
Condition66%2/3
defined $childs_parent and builtin::refaddr($childs_parent) == $refaddr
!ll&&!rl&&r
048
MC/DC50%1/2
defined $childs_parent and builtin::refaddr($childs_parent) == $refaddr
defined $childs_parent builtin::refaddr($childs_parent) == $refaddr
Truth table66%2/3
A and B
A= defined $childs_parentB= builtin::refaddr($childs_parent) == $refaddr
ABresult
0X0
100
111
296 Scalar::Util::refaddr($childs_parent) == $refaddr) {
297 4 die "check: Child #$i of $self does not refer back to its parent";
298 }
299 check($child, {
300 recurse=>1,
301 #check_root=>0,
302 8 ▶ }) if $opts->{recurse};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"recurse"} 0 8 8
303 }
304 }
305  
306  
307 7 1;
308 # ABSTRACT: Tree node routines
309  
310 __END__
« Code/Includable/Tree/FromObjArray.pm ↑ Summary Test/Role/TinyCommons/Tree.pm »