Forest/Tree/Pure.pm

filter: Statementstmt 90.0% 117 / 130 Branchbran 57.5% 23 / 40 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 85.7% 24 / 28 Podpod 100% 18 / 18 totaltotal 77.5% 187 / 241 CC 29 SCAR 12.2
CC
29
Cov
78%
insert_child_at
6.3
add_children
6.1
reconstruct_with_class
6.0
CRAP 3.4
« Forest/Tree.pm ↑ Summary Forest/Tree/Roles/LoadWithMetaData.pm »
1 package Forest::Tree::Pure;
2 16 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 16 our $VERSION = '0.10';
5 16 our $AUTHORITY = 'cpan:STEVAN';
6  
7 16 ▶use Scalar::Util 'reftype', 'refaddr';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use List::Util 'sum', 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 16 with qw(MooseX::Clone);
11  
12 16 has 'node' => (
13 is => 'ro',
14 isa => 'Item',
15 predicate => 'has_node',
16 );
17  
18 has 'uid' => (
19 is => 'rw',
20 isa => 'Value',
21 lazy => 1,
22 14 ▶ default => sub { (overload::StrVal($_[0]) =~ /\((.*?)\)$/)[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
23 16 );
24  
25 has 'children' => (
26 traits => ['Array'],
27 is => 'ro',
28 isa => 'ArrayRef[Forest::Tree::Pure]',
29 lazy => 1,
30 10 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 16 handles => {
32 get_child_at => 'get',
33 child_count => 'count',
34 },
35 );
36  
37 16 has 'size' => (
38 traits => [qw(NoClone)],
39 is => 'ro',
40 isa => 'Int',
41 lazy_build => 1,
42 );
43  
44 44 ▶sub _build_size {
Subroutine _build_size: called CC=2 SCAR=6.9
45 44 my $self = shift;
46  
47 44 ▶ if ( $self->is_leaf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_leaf) { } 18 26 44
48 18 return 1;
49 } else {
50 26 return 1 + sum map { $_->size } @{ $self->children };
51 }
52 }
53  
54 16 has 'height' => (
55 traits => [qw(NoClone)],
56 is => 'ro',
57 isa => 'Int',
58 lazy_build => 1,
59 );
60  
61 44 ▶sub _build_height {
Subroutine _build_height: called CC=2 SCAR=6.9
62 44 my $self = shift;
63  
64 44 ▶ if ( $self->is_leaf ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_leaf) { } 18 26 44
65 18 return 0;
66 } else {
67 26 return 1 + max map { $_->height } @{ $self->children };
68 }
69 }
70  
71 ## informational
72 114 ▶sub is_leaf { (shift)->child_count == 0 }
Subroutine is_leaf: called CC=1 SCAR=0.0
Pod documented
73  
74 ## traversal
75 6 ▶sub traverse {
Subroutine traverse: called CC=2 SCAR=6.9
Pod documented
76 6 my ($self, @args) = @_;
77  
78 6 $_->visit(@args) for @{ $self->children };
79 }
80  
81 13 ▶sub visit {
Subroutine visit: called CC=1 SCAR=0.0
Pod documented
82 13 my ( $self, $f, @args ) = @_;
83  
84 50 ▶ $self->fmap_cont(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
85 50 my ( $tree, $cont, @args ) = @_;
86 50 $tree->$f(@args);
87 50 $cont->();
88 13 });
89 }
90  
91 207 ▶sub fmap_cont {
Subroutine fmap_cont: called CC=5 SCAR=16.6
Pod documented
92 207 my ( $self, @args ) = @_;
93  
94 207 ▶ unshift @args, "callback" if @args % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @args % 2 == 1 30 177 207
95  
96 207 my %args = ( depth => 0, path => [], index_path => [], @args );
97  
98 207 my $f = $args{callback};
99  
100 207 ▶ (defined($f))
Branch: 1 branch
BranchTrueFalseTotal
defined $f or confess("Cannot traverse without traversal function") 207 0 207
101 || confess "Cannot traverse without traversal function";
102 207 ▶ (!ref($f) or reftype($f) eq "CODE")
Branch: 1 branch
BranchTrueFalseTotal
not ref $f or builtin::reftype($f) eq "CODE" or confess("Traversal function must be a CODE reference or method name, not: $f") 207 0 207
Condition33%1/3
not ref $f or builtin::reftype($f) eq "CODE"
l!l&&r!l&&!r
02070
MC/DC0%0/2
not ref $f or builtin::reftype($f) eq "CODE"
not ref $f builtin::reftype($f) eq "CODE"
Truth table33%1/3
not(A) or B
A= not ref $fB= builtin::reftype($f) eq "CODE"
ABresult
1X1
011
000
103 || confess "Traversal function must be a CODE reference or method name, not: $f";
104  
105 $self->$f(
106 182 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
107 182 my ( @inner_args ) = @_;
108 182 ▶ unshift @inner_args, "callback" if @inner_args % 2 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @inner_args % 2 == 1 0 182 182
109 182 ▶ my $children = $args{children} || $self->children;
Condition33%1/3
$args{'children'} || $self->children
l!l&&r!l&&!r
01820
MC/DC0%0/2
$args{'children'} || $self->children
$args{'children'} $self->children
Truth table33%1/3
A || B
A= $args{'children'}B= $self->children
ABresult
1X1
011
000
110  
111 182 my %child_args = ( %args, depth => $args{depth} + 1, path => [ @{ $args{path} }, $self ], parent => $self, @inner_args );
112  
113 182 my @index_path = @{ $args{index_path} };
114  
115 182 my $i = 0;
116 map {
117 182 my $index = $i++;
118 177 $_->fmap_cont(
119 %child_args,
120 index => $index,
121 index_path => [ @index_path, $index ],
122 )
123 } @$children;
124 },
125 207 %args,
126 );
127 }
128  
129 6 ▶sub locate {
Subroutine locate: called CC=1 SCAR=0.0
Pod documented
130 6 my ( $self, @path ) = @_;
131  
132 6 my @nodes = $self->descend(@path);
133  
134 6 return $nodes[-1];
135 }
136  
137 20 ▶sub descend {
Subroutine descend: called CC=3 SCAR=11.2
Pod documented
138 20 my ( $self, @path ) = @_;
139  
140 20 ▶ if ( @path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 14 6 20
141 14 my ( $head, @tail ) = @path;
142  
143 14 ▶ if ( my $child = $self->get_child_at($head) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = $self->get_child_at($head)) { } 14 0 14
144 14 return ( $self, $child->descend(@tail) );
145 } else {
146 0 confess "No such child $head";
147 }
148 } else {
149 6 return $self;
150 }
151 }
152  
153 16 ▶sub transform {
Subroutine transform: called CC=3 SCAR=11.0
Pod documented
154 16 my ( $self, $path, $method, @args ) = @_;
155  
156 16 ▶ if ( @$path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path) { } 10 6 16
157 10 my ( $i, @path ) = @$path;
158  
159 10 my $targ = $self->get_child_at($i);
160  
161 10 my $transformed = $targ->transform(\@path, $method, @args);
162  
163 10 ▶ if ( refaddr($transformed) == refaddr($targ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::refaddr($transformed) == builtin::refaddr($targ)) { } 4 6 10
164 4 return $self;
165 } else {
166 6 return $self->set_child_at( $i => $transformed );
167 }
168 } else {
169 6 return $self->$method(@args);
170 }
171 }
172  
173 0 ▶sub set_node {
Subroutine set_node: not called CC=1 SCAR=6.9
Pod documented
174 0 my ( $self, $node ) = @_;
175  
176 0 $self->clone( node => $node );
177 }
178  
179 1 ▶sub replace {
Subroutine replace: called CC=1 SCAR=0.0
Pod documented
180 1 my ( $self, $replacement ) = @_;
181  
182 1 return $replacement;
183 }
184  
185 4 ▶sub add_children {
Subroutine add_children: called CC=5 SCAR=18.1
Pod documented
186 4 my ( $self, @additional_children ) = @_;
187  
188 4 foreach my $child ( @additional_children ) {
189 4 ▶ (blessed($child) && $child->isa(ref $self))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa(ref $self) or confess('Child parameter must be a ' . ref($self) . ' not (' . (defined $child ? $child : "undef") . ')') 4 0 4
defined $child ? : 0 0 0
Condition33%1/3
builtin::blessed($child) and $child->isa(ref $self)
!ll&&!rl&&r
004
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)
ABresult
0X0
100
111
190 || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")";
191 }
192  
193 4 my @children = @{ $self->children };
194  
195 4 push @children, @additional_children;
196  
197 4 return $self->clone( children => \@children );
198 }
199  
200 4 ▶sub add_child {
Subroutine add_child: called CC=1 SCAR=0.0
Pod documented
201 4 my ( $self, $child ) = @_;
202  
203 4 $self->add_children($child);
204 }
205  
206 6 ▶sub set_child_at {
Subroutine set_child_at: called CC=4 SCAR=15.9
Pod documented
207 6 my ( $self, $index, $child ) = @_;
208  
209 6 ▶ (blessed($child) && $child->isa(ref $self))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa(ref $self) or confess('Child parameter must be a ' . ref($self) . ' not (' . (defined $child ? $child : "undef") . ')') 6 0 6
defined $child ? : 0 0 0
Condition33%1/3
builtin::blessed($child) and $child->isa(ref $self)
!ll&&!rl&&r
006
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)
ABresult
0X0
100
111
210 || confess "Child parameter must be a " . ref($self) . " not (" . (defined $child ? $child : 'undef') . ")";
211  
212 6 my @children = @{ $self->children };
213  
214 6 $children[$index] = $child;
215  
216 6 $self->clone( children => \@children );
217 }
218  
219 2 ▶sub remove_child_at {
Subroutine remove_child_at: called CC=2 SCAR=7.0
Pod documented
220 2 my ( $self, $index ) = @_;
221  
222 2 my @children = @{ $self->children };
223  
224 2 ▶ confess "No child at index '$index'" if @children <= $index;
Branch: 1 branch
BranchTrueFalseTotal
if @children <= $index 0 2 2
225  
226 2 splice @children, $index, 1;
227  
228 2 $self->clone( children => \@children );
229  
230 }
231  
232 3 ▶sub insert_child_at {
Subroutine insert_child_at: called CC=5 SCAR=18.4
Pod documented
233 3 my ( $self, $index, $child ) = @_;
234  
235 3 ▶ (blessed($child) && $child->isa('Forest::Tree::Pure'))
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($child) and $child->isa("Forest::Tree::Pure") or confess('Child parameter must be a Forest::Tree::Pure not (' . (defined $child ? $child : "undef") . ')') 3 0 3
defined $child ? : 0 0 0
Condition33%1/3
builtin::blessed($child) and $child->isa("Forest::Tree::Pure")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($child) and $child->isa("Forest::Tree::Pure")
builtin::blessed($child) $child->isa("Forest::Tree::Pure")
Truth table33%1/3
A and B
A= builtin::blessed($child)B= $child->isa("Forest::Tree::Pure")
ABresult
0X0
100
111
236 || confess "Child parameter must be a Forest::Tree::Pure not (" . (defined $child ? $child : 'undef') . ")";
237  
238 3 my @children = @{ $self->children };
239  
240 3 ▶ confess "'$index' is out of bounds" if @children < $index;
Branch: 1 branch
BranchTrueFalseTotal
if @children < $index 0 3 3
241  
242 3 splice @children, $index, 0, $child;
243  
244 3 $self->clone( children => \@children );
245 }
246  
247 23 ▶sub get_child_index {
Subroutine get_child_index: called CC=3 SCAR=11.0
Pod documented
248 23 my ( $self, $child ) = @_;
249  
250 23 my $index = 0;
251 23 foreach my $sibling (@{ $self->children }) {
252 37 ▶ (refaddr($sibling) eq refaddr($child)) && return $index;
Branch: 1 branch
BranchTrueFalseTotal
builtin::refaddr($sibling) eq builtin::refaddr($child) and return $index 23 14 37
253 14 $index++;
254 }
255  
256 0 return;
257 }
258  
259 0 ▶sub reconstruct_with_class {
Subroutine reconstruct_with_class: not called CC=2 SCAR=17.9
Pod documented
260 0 my ( $self, $class ) = @_;
261  
262 0 ▶ confess "No class provided" unless defined($class);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class 0 0 0
263  
264 return $class->new(
265 node => $self->node,
266 children => [
267 0 map { $_->reconstruct_with_class($class) } @{ $self->children },
268 ],
269 );
270 }
271  
272 0 ▶sub to_pure_tree {
Subroutine to_pure_tree: not called CC=1 SCAR=6.9
Pod documented
273 0 my $self = shift;
274  
275 0 return $self;
276 }
277  
278 0 ▶sub to_mutable_tree {
Subroutine to_mutable_tree: not called CC=1 SCAR=6.9
Pod documented
279 0 my $self = shift;
280  
281 0 $self->reconstruct_with_class("Forest::Tree");
282 }
283  
284 16 __PACKAGE__->meta->make_immutable;
285  
286 16 ▶no Moose; 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
287  
288 __END__
« Forest/Tree.pm ↑ Summary Forest/Tree/Roles/LoadWithMetaData.pm »