| 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
| |||||||||||||||||||||||||||||
| 26 | 320 | eval { | |||||||||||||||||||||||||||
| 27 | 320 | @children = $self->$GET_CHILDREN_METHOD; | |||||||||||||||||||||||||||
| 28 | }; | ||||||||||||||||||||||||||||
| 29 | 320 | ▶ | return () if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | } else { | ||||||||||||||||||||||||||||
| 31 | 0 | @children = $self->$GET_CHILDREN_METHOD; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 320 | ▶ | if (@children == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 243 | ▶ | return () unless defined($children[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 207 | ▶ | return @{$children[0]} if ref($children[0]) eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 107 | 8 | my @siblings = _children_as_list($parent); | |||||||||||||||||||||||||||
| 108 | 8 | ▶ | @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 115 | 8 | my @siblings = _children_as_list($parent); | |||||||||||||||||||||||||||
| 116 | 8 | ▶ | @siblings && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[-1]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 139 | 12 | my @siblings = _children_as_list($parent); | |||||||||||||||||||||||||||
| 140 | 12 | ▶ | @siblings >= $n && Scalar::Util::refaddr($self) == Scalar::Util::refaddr($siblings[-$n]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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() // {}
MC/DC100%1/1
shift() // {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 10 | ▶ | if ($opts->{check_root}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | 4 | my $parent = $self->$GET_PARENT_METHOD; | |||||||||||||||||||||||||||
| 287 | 4 | ▶ | defined $parent and die "check: parent is not undef"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $childs_parent and builtin::refaddr($childs_parent) == $refaddr
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 7 | 1; | |||||||||||||||||||||||||||
| 308 | # ABSTRACT: Tree node routines | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | __END__ | ||||||||||||||||||||||||||||