| 1 | package Forest::Tree; | ||||||||||||||||||||||||||||||||
| 2 | 15 | ▶ | use Moose; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use Scalar::Util 'reftype', 'refaddr'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 15 | ▶ | use List::Util 'sum', 'max'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||
| 7 | 15 | our $VERSION = '0.10'; | |||||||||||||||||||||||||||||||
| 8 | 15 | our $AUTHORITY = 'cpan:STEVAN'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 15 | extends qw(Forest::Tree::Pure); | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | #has '+node' => ( is => 'rw' ); | ||||||||||||||||||||||||||||||||
| 13 | 15 | has 'node' => ( | |||||||||||||||||||||||||||||||
| 14 | traits => [qw(StorableClone)], | ||||||||||||||||||||||||||||||||
| 15 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 16 | isa => 'Item', | ||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | sub set_node { | ||||||||||||||||||||||||||||||
|
Subroutine
set_node: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 20 | 0 | my ( $self, $new ) = @_; | |||||||||||||||||||||||||||||||
| 21 | 0 | $self->node($new); | |||||||||||||||||||||||||||||||
| 22 | 0 | $self; | |||||||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 15 | has 'parent' => ( | |||||||||||||||||||||||||||||||
| 26 | traits => [qw(NoClone)], | ||||||||||||||||||||||||||||||||
| 27 | reader => 'parent', | ||||||||||||||||||||||||||||||||
| 28 | writer => '_set_parent', | ||||||||||||||||||||||||||||||||
| 29 | predicate => 'has_parent', | ||||||||||||||||||||||||||||||||
| 30 | clearer => 'clear_parent', | ||||||||||||||||||||||||||||||||
| 31 | isa => 'Maybe[Forest::Tree]', | ||||||||||||||||||||||||||||||||
| 32 | weak_ref => 1, | ||||||||||||||||||||||||||||||||
| 33 | handles => { | ||||||||||||||||||||||||||||||||
| 34 | 'add_sibling' => 'add_child', | ||||||||||||||||||||||||||||||||
| 35 | 'get_sibling_at' => 'get_child_at', | ||||||||||||||||||||||||||||||||
| 36 | 'insert_sibling_at' => 'insert_child_at', | ||||||||||||||||||||||||||||||||
| 37 | }, | ||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||
| 40 | #has '+children' => ( | ||||||||||||||||||||||||||||||||
| 41 | # is => 'rw', | ||||||||||||||||||||||||||||||||
| 42 | has 'children' => ( | ||||||||||||||||||||||||||||||||
| 43 | traits => [qw(Array Clone)], | ||||||||||||||||||||||||||||||||
| 44 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 45 | isa => 'ArrayRef[Forest::Tree]', | ||||||||||||||||||||||||||||||||
| 46 | lazy => 1, | ||||||||||||||||||||||||||||||||
| 47 | 74 | ▶ | default => sub { [] }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 48 | handles => { | ||||||||||||||||||||||||||||||||
| 49 | get_child_at => 'get', | ||||||||||||||||||||||||||||||||
| 50 | child_count => 'count', | ||||||||||||||||||||||||||||||||
| 51 | }, | ||||||||||||||||||||||||||||||||
| 52 | 51 | ▶ | trigger => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||
| 53 | 51 | my ($self, $children) = @_; | |||||||||||||||||||||||||||||||
| 54 | 51 | foreach my $child (@$children) { | |||||||||||||||||||||||||||||||
| 55 | 71 | $child->_set_parent($self); | |||||||||||||||||||||||||||||||
| 56 | 71 | ▶ | $self->clear_height if $self->has_height; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | 71 | ▶ | $self->clear_size if $self->has_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||
| 60 | 15 | ); | |||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 13 | ▶ | after 'clear_size' => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||
| 63 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 64 | 13 | ▶ | $self->parent->clear_size | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_parent and $self->parent->has_size
MC/DC50%1/2
$self->has_parent and $self->parent->has_size
$self->has_parent
$self->parent->has_size
Truth table66%2/3
A and B
A= $self->has_parentB= $self->parent->has_size
| |||||||||||||||||||||||||||||||||
| 65 | if $self->has_parent && $self->parent->has_size; | ||||||||||||||||||||||||||||||||
| 66 | 15 | }; | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | after 'clear_height' => sub { | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||
| 69 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||
| 70 | 13 | ▶ | $self->parent->clear_height | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->has_parent and $self->parent->has_height
MC/DC50%1/2
$self->has_parent and $self->parent->has_height
$self->has_parent
$self->parent->has_height
Truth table66%2/3
A and B
A= $self->has_parentB= $self->parent->has_height
| |||||||||||||||||||||||||||||||||
| 71 | if $self->has_parent && $self->parent->has_height; | ||||||||||||||||||||||||||||||||
| 72 | 15 | }; | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | ## informational | ||||||||||||||||||||||||||||||||
| 75 | 76 | ▶ | sub is_root { !(shift)->has_parent } | ||||||||||||||||||||||||||||||
|
Subroutine
is_root: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||
| 77 | ## depth | ||||||||||||||||||||||||||||||||
| 78 | 118 | ▶ | sub depth { ((shift)->parent || return -1)->depth + 1 } | ||||||||||||||||||||||||||||||
|
Subroutine
depth: called CC=2 SCAR=6.9
Pod
documented
Condition100%2/2
(shift())->parent || (return -1)
MC/DC100%1/1
(shift())->parent || (return -1)
(shift())->parent
Truth table100%2/2
A
A= (shift())->parent
| |||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | ## child management | ||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||
| 82 | 61 | ▶ | sub add_child { | ||||||||||||||||||||||||||||||
|
Subroutine
add_child: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 83 | 61 | my ($self, $child) = @_; | |||||||||||||||||||||||||||||||
| 84 | 61 | ▶ | (blessed($child) && $child->isa(ref $self)) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
builtin::blessed($child) and $child->isa(ref $self)
MC/DC100%2/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child)
$child->isa(ref $self)
Truth table100%3/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
| |||||||||||||||||||||||||||||||||
| 85 | || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")"; | ||||||||||||||||||||||||||||||||
| 86 | 57 | $child->_set_parent($self); | |||||||||||||||||||||||||||||||
| 87 | 57 | ▶ | $self->clear_height if $self->has_height; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 88 | 57 | ▶ | $self->clear_size if $self->has_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 57 | push @{ $self->children } => $child; | |||||||||||||||||||||||||||||||
| 90 | 57 | $self; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 1 | ▶ | sub replace { | ||||||||||||||||||||||||||||||
|
Subroutine
replace: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 94 | 1 | my ( $self, $replacement ) = @_; | |||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | confess "Can't replace root" if $self->is_root; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | 1 | $self->parent->set_child_at( $self->get_index_in_siblings, $replacement ); | |||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 1 | return $replacement; | |||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | 6 | ▶ | sub add_children { | ||||||||||||||||||||||||||||||
|
Subroutine
add_children: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 104 | 6 | my ($self, @children) = @_; | |||||||||||||||||||||||||||||||
| 105 | 6 | $self->add_child($_) for @children; | |||||||||||||||||||||||||||||||
| 106 | 6 | return $self; | |||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub set_child_at { | ||||||||||||||||||||||||||||||
|
Subroutine
set_child_at: called CC=6 SCAR=18.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 110 | 2 | my ( $self, $index, $child ) = @_; | |||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | 2 | ▶ | (blessed($child) && $child->isa(ref $self)) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
builtin::blessed($child) and $child->isa(ref $self)
MC/DC0%0/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child)
$child->isa(ref $self)
Truth table33%1/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
| |||||||||||||||||||||||||||||||||
| 113 | || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")"; | ||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||
| 115 | 2 | ▶ | $self->clear_height if $self->has_height; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 116 | 2 | ▶ | $self->clear_size if $self->has_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | 2 | my $children = $self->children; | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 2 | $children->[$index]->clear_parent; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 2 | $children->[$index] = $child; | |||||||||||||||||||||||||||||||
| 123 | 2 | $child->_set_parent($self); | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 2 | $self; | |||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||
| 128 | 7 | ▶ | sub insert_child_at { | ||||||||||||||||||||||||||||||
|
Subroutine
insert_child_at: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 129 | 7 | my ($self, $index, $child) = @_; | |||||||||||||||||||||||||||||||
| 130 | 7 | ▶ | (blessed($child) && $child->isa(ref $self)) | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
builtin::blessed($child) and $child->isa(ref $self)
MC/DC50%1/2
builtin::blessed($child) and $child->isa(ref $self)
builtin::blessed($child)
$child->isa(ref $self)
Truth table66%2/3
A and B
A= builtin::blessed($child)B= $child->isa(ref $self)
| |||||||||||||||||||||||||||||||||
| 131 | || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")"; | ||||||||||||||||||||||||||||||||
| 132 | 3 | $child->_set_parent($self); | |||||||||||||||||||||||||||||||
| 133 | 3 | ▶ | $self->clear_height if $self->has_height; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | 3 | ▶ | $self->clear_size if $self->has_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 135 | 3 | splice @{ $self->children }, $index, 0, $child; | |||||||||||||||||||||||||||||||
| 136 | 3 | $self; | |||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||
| 139 | 2 | ▶ | sub remove_child_at { | ||||||||||||||||||||||||||||||
|
Subroutine
remove_child_at: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 140 | 2 | my ($self, $index) = @_; | |||||||||||||||||||||||||||||||
| 141 | 2 | ▶ | $self->clear_height if $self->has_height; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | $self->clear_size if $self->has_size; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 143 | 2 | my $child = splice @{ $self->children }, $index, 1; | |||||||||||||||||||||||||||||||
| 144 | 2 | $child->clear_parent; | |||||||||||||||||||||||||||||||
| 145 | 2 | $child; | |||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||
| 148 | ##siblings | ||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 2 | ▶ | sub siblings { | ||||||||||||||||||||||||||||||
|
Subroutine
siblings: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 151 | 2 | my $self = shift; | |||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | return [] unless $self->has_parent; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 153 | 2 | [ grep { $self->uid ne $_->uid } @{ $self->parent->children } ]; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 23 | ▶ | sub get_index_in_siblings { | ||||||||||||||||||||||||||||||
|
Subroutine
get_index_in_siblings: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 157 | 23 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 158 | 23 | ▶ | return -1 if $self->is_root; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 23 | $self->parent->get_child_index($self); | |||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | ## cloning | ||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | sub clone_and_detach { shift->clone(@_) } | ||||||||||||||||||||||||||||||
|
Subroutine
clone_and_detach: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | sub to_pure_tree { | ||||||||||||||||||||||||||||||
|
Subroutine
to_pure_tree: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 168 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||
| 170 | 0 | $self->reconstruct_with_class("Forest::Tree::Pure"); | |||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | sub to_mutable_tree { | ||||||||||||||||||||||||||||||
|
Subroutine
to_mutable_tree: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||
| 176 | 0 | return $self; | |||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||
| 179 | 15 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||
| 181 | 15 | ▶ | no Moose; 1; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||||||||||||||