blib/lib/Tree/Create/DepthFirst.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 90.0% 9 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 90.6% 58 / 64 CC 7 SCAR 13.0
CC
7
Cov
92%
addNode
6.0
new
2.0
BEGIN
1.0
CRAP 3.7
↑ Summary
1 #_{ Encoding and name
2 =encoding utf8
3 =head1 NAME
4 Tree::Create::DepthFirst - Create a Tree::Simple in the order as the created tree will traverse its nodes when traversing it depth first.
5 =cut
6 package Tree::Create::DepthFirst;
7  
8 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #_}
12 #_{ Version
13 =head1 VERSION
14  
15 Version 0.02
16 =cut
17 1 our $VERSION = '0.02';
18 #_}
19 #_{ Synopsis
20 =head1 SYNOPSIS
21  
22 =pod
23  
24 use Tree::Create::DepthFirst;
25  
26 my $tree_creator = Tree::Create::DepthFirst->new();
27 $tree_creator -> addNode(0, 'child 1');
28 $tree_creator -> addNode(0, 'child 2');
29 $tree_creator -> addNode(1, 'grand chhild 1');
30 $tree_creator -> addNode(1, 'grand chhild 2');
31 $tree_creator -> addNode(2, 'grand-grand chhild 2');
32  
33 my $tree_simple = $tree_creator->getTree();
34 =cut
35 #_}
36  
37 1 ▶use Tree::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 #_{ Methods
40 =head1 Methods
41 =cut
42  
43 1 ▶sub new { #_{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
44  
45 =head2 new()
46  
47 Creates the tree creator.
48  
49 my $tree_creator = Tree::Create::DepthFirst->new()
50  
51 From now on, you will want to call
52  
53 $tree_creator->addNode(…);
54  
55 until the tree is finished. Then you get the created tree with
56  
57 my $tree_simple = $tree_simple->getTree();
58  
59 =cut
60  
61 1 my ($_class, $input) = @_;
62 1 ▶ my $class = ref($_class) || $_class;
Condition33%1/3
ref $_class || $_class
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $_class || $_class
ref $_class $_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
ABresult
1X1
011
000
63  
64 1 my $self = {};
65 1 bless $self, $class;
66  
67 1 $self->{tree} = Tree::Simple->new('root', Tree::Simple->ROOT);
68 1 $self->{current_tree} = $self->{tree};
69  
70 1 return $self;
71 } #_}
72  
73 23 ▶sub addNode { #_{
Subroutine addNode: called CC=6 SCAR=17.9
Pod documented
74  
75 =head2 addNode($depth, $nodeValue)
76  
77 Add tree nodes and leaves in the same order as a depth first traversal would traverse
78 the tree.
79  
80 There are two restrictions on $depth: a) it must be greater or equal to 0. b) It must
81 not be greater than the last added node's $depth+1.
82  
83 =cut
84  
85 #
86 # Note the similarity to parts of Tree::Parser's sub _parse
87 #
88  
89 23 my $self = shift;
90 23 my $depth = shift;
91 23 my $nodeValue = shift;
92  
93 23 my $new_tree = Tree::Simple->new($nodeValue);
94  
95 23 ▶ if ($self->{current_tree}->isRoot()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"current_tree"}->isRoot) 1 22 23
96 1 $self->{current_tree}->addChild($new_tree);
97 1 $self->{current_tree}=$new_tree;
98 1 return $self->{tree};
99 }
100  
101 22 my $tree_depth = $self->{current_tree}->getDepth();
102 22 ▶ if ($depth == $tree_depth) {
Branch: 2 branches
BranchTrueFalseTotal
if ($depth == $tree_depth) { } 6 16 22
elsif ($depth > $tree_depth) { } 10 6 16
103 6 $self->{current_tree}->addSibling($new_tree);
104 6 $self->{current_tree} = $new_tree;
105 }
106 elsif ($depth > $tree_depth) {
107  
108 10 ▶ if ($depth - $tree_depth > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth - $tree_depth > 1) 0 10 10
109 0 die "Passed depth (=$depth) must not be greater than current current_tree depth (=$tree_depth) + 1";
110 }
111  
112 10 $self->{current_tree}->addChild($new_tree);
113 10 $self->{current_tree}=$new_tree;
114  
115 }
116 else {
117 6 ▶ $self->{current_tree} = $self->{current_tree}->getParent() while ($depth < $self->{current_tree}->getDepth());
Branch: 1 branch
BranchTrueFalseTotal
$depth < $self->{"current_tree"}->getDepth and do { $self->{"current_tree"} = $self->{"current_tree"}->getParent } 10 6 16
118  
119 6 $self->{current_tree}->addSibling($new_tree);
120 6 $self->{current_tree}=$new_tree;
121  
122 }
123 22 return $self->{tree};
124  
125 } #_}
126  
127 1 ▶sub getTree { #_{
Subroutine getTree: called CC=1 SCAR=0.0
Pod documented
128  
129 =head2 getTree()
130  
131 After building, getTree() returns the created tree (as a Tree::Simple) object.
132  
133 $tree_simple = $tree_creator->getTree();
134  
135 =cut
136  
137 1 my $self = shift;
138  
139 1 return $self->{tree};
140  
141 } #_}
142 #_}
143 #_{ More POD
144 =head1 AUTHOR
145  
146 René Nyffenegger, C<< <rene.nyffenegger at adp-gmbh.ch> >>
147  
148 =head1 LICENSE
149  
150 According to the C<LICENSE> file that comes with the package.
151  
152 =head1 LINKS
153  
154 The source code is in L<this Github repository|https://github.com/ReneNyffenegger/Tree-Create-DepthFirst>
155  
156 =cut
157  
158 #_}
159  
160 1 "tq84";
↑ Summary