| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::FromNestedArray; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19640
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
35
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
53
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
5
|
use Scalar::Util qw(blessed); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
129
|
|
|
9
|
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
7
|
use base qw(Tree::Simple::Visitor); |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
644
|
|
|
11
|
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
|
13
|
4
|
|
|
4
|
1
|
11052
|
my ($_class) = @_; |
|
14
|
4
|
|
33
|
|
|
20
|
my $class = ref($_class) || $_class; |
|
15
|
4
|
|
|
|
|
6
|
my $visitor = {}; |
|
16
|
4
|
|
|
|
|
4
|
bless($visitor, $class); |
|
17
|
4
|
|
|
|
|
8
|
$visitor->_init(); |
|
18
|
4
|
|
|
|
|
22
|
return $visitor; |
|
19
|
|
|
|
|
|
|
} |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _init { |
|
22
|
4
|
|
|
4
|
|
5
|
my ($self) = @_; |
|
23
|
4
|
|
|
|
|
9
|
$self->{array_tree} = undef; |
|
24
|
4
|
|
|
|
|
12
|
$self->SUPER::_init(); |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub setArrayTree { |
|
28
|
9
|
|
|
9
|
1
|
4824
|
my ($self, $array_tree) = @_; |
|
29
|
9
|
100
|
100
|
|
|
67
|
(defined($array_tree) && ref($array_tree) eq 'ARRAY') |
|
30
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a valid ARRAY reference"; |
|
31
|
|
|
|
|
|
|
# validate the tree ... |
|
32
|
|
|
|
|
|
|
# it must not be empty |
|
33
|
7
|
100
|
|
|
|
6
|
(scalar @{$array_tree} != 0) |
|
|
7
|
|
|
|
|
21
|
|
|
34
|
|
|
|
|
|
|
|| die "Insufficient Arguments : The array tree provided is empty"; |
|
35
|
|
|
|
|
|
|
# it's first element must not be an array |
|
36
|
6
|
100
|
|
|
|
21
|
(ref($array_tree->[0]) ne 'ARRAY') |
|
37
|
|
|
|
|
|
|
|| die "Incorrect Object Type : The first value in the array tree is an array reference"; |
|
38
|
|
|
|
|
|
|
# and it must be a single rooted tree |
|
39
|
5
|
50
|
100
|
|
|
23
|
(ref($array_tree->[1]) eq 'ARRAY') |
|
40
|
|
|
|
|
|
|
|| die "Incorrect Object Type : The second value in the array tree must be an array reference" |
|
41
|
|
|
|
|
|
|
if defined($array_tree->[1]); |
|
42
|
4
|
|
|
|
|
10
|
$self->{array_tree} = $array_tree; |
|
43
|
|
|
|
|
|
|
} |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub visit { |
|
46
|
8
|
|
|
8
|
1
|
3431
|
my ($self, $tree) = @_; |
|
47
|
8
|
100
|
100
|
|
|
79
|
(blessed($tree) && $tree->isa("Tree::Simple")) |
|
48
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a valid Tree::Simple object"; |
|
49
|
|
|
|
|
|
|
$self->_buildTree( |
|
50
|
|
|
|
|
|
|
$tree, |
|
51
|
|
|
|
|
|
|
# our array tree |
|
52
|
|
|
|
|
|
|
$self->{array_tree}, |
|
53
|
|
|
|
|
|
|
# get a node filter if we have one |
|
54
|
4
|
|
|
|
|
15
|
$self->getNodeFilter(), |
|
55
|
|
|
|
|
|
|
# pass the value of includeTrunk too |
|
56
|
|
|
|
|
|
|
$self->includeTrunk() |
|
57
|
|
|
|
|
|
|
); |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
sub _buildTree { |
|
61
|
11
|
|
|
11
|
|
35
|
my ($self, $tree, $array, $node_filter, $include_trunk) = @_; |
|
62
|
11
|
|
|
|
|
10
|
my $i = 0; |
|
63
|
11
|
|
|
|
|
9
|
while ($i < scalar @{$array}) { |
|
|
26
|
|
|
|
|
49
|
|
|
64
|
17
|
|
|
|
|
18
|
my $node = $array->[$i]; |
|
65
|
|
|
|
|
|
|
# check to make sure we have a well formed tree |
|
66
|
17
|
100
|
|
|
|
33
|
(ref($node) ne 'ARRAY') |
|
67
|
|
|
|
|
|
|
|| die "Incorrect Object Type : The node value should never be an array reference"; |
|
68
|
|
|
|
|
|
|
# filter the node if necessary |
|
69
|
16
|
100
|
|
|
|
27
|
$node = $node_filter->($node) if defined($node_filter); |
|
70
|
|
|
|
|
|
|
# create the new tree |
|
71
|
16
|
|
|
|
|
25
|
my $new_tree; |
|
72
|
16
|
100
|
|
|
|
18
|
if ($include_trunk) { |
|
73
|
1
|
|
|
|
|
13
|
$tree->setNodeValue($node); |
|
74
|
1
|
|
|
|
|
7
|
$new_tree = $tree; |
|
75
|
|
|
|
|
|
|
} |
|
76
|
|
|
|
|
|
|
else { |
|
77
|
15
|
|
|
|
|
37
|
$new_tree = Tree::Simple->new($node); |
|
78
|
15
|
|
|
|
|
257
|
$tree->addChild($new_tree); |
|
79
|
|
|
|
|
|
|
} |
|
80
|
|
|
|
|
|
|
# increment the index value |
|
81
|
16
|
|
|
|
|
740
|
$i++; |
|
82
|
|
|
|
|
|
|
# NOTE: |
|
83
|
|
|
|
|
|
|
# the value of include trunk is only |
|
84
|
|
|
|
|
|
|
# passed in the recursion, so that |
|
85
|
|
|
|
|
|
|
# the trunk/root can be populated, |
|
86
|
|
|
|
|
|
|
# we have no more need for it after |
|
87
|
|
|
|
|
|
|
# that time. |
|
88
|
16
|
100
|
|
|
|
50
|
$self->_buildTree($new_tree, $array->[$i++], $node_filter) |
|
89
|
|
|
|
|
|
|
if ref($array->[$i]) eq 'ARRAY'; |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
} |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
|
94
|
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |