File Coverage

blib/lib/Tree/Simple/Visitor/BreadthFirstTraversal.pm
Criterion Covered Total %
statement 33 33 100.0
branch 6 6 100.0
condition 4 6 66.6
subroutine 7 7 100.0
pod 2 2 100.0
total 52 54 96.3


line stmt bran cond sub pod time code
1             package Tree::Simple::Visitor::BreadthFirstTraversal;
2              
3 4     4   24641 use strict;
  4         4  
  4         108  
4 4     4   16 use warnings;
  4         5  
  4         191  
5              
6             our $VERSION = '0.14';
7              
8 4     4   16 use Scalar::Util qw(blessed);
  4         4  
  4         256  
9              
10 4     4   16 use base qw(Tree::Simple::Visitor);
  4         3  
  4         1374  
11              
12             sub new {
13 6     6 1 4724 my ($_class) = @_;
14 6   33     31 my $class = ref($_class) || $_class;
15 6         10 my $visitor = {};
16 6         10 bless($visitor, $class);
17 6         14 $visitor->_init();
18 6         56 return $visitor;
19             }
20              
21             sub _init {
22 6     6   8 my ($self) = @_;
23 6         39 $self->SUPER::_init();
24             }
25              
26             sub visit {
27 11     11 1 4715 my ($self, $tree) = @_;
28 11 100 100     109 (blessed($tree) && $tree->isa("Tree::Simple"))
29             || die "Insufficient Arguments : You must supply a valid Tree::Simple object";
30             # create a holder for our results
31 7         8 my @results;
32             # get our filter function
33 7         26 my $filter_function = $self->getNodeFilter();
34             # now create a queue for
35             # processing depth first
36 7         24 my @queue;
37             # if we are to include the trunk
38 7 100       19 if ($self->includeTrunk()) {
39             # then enqueue that
40 3         16 @queue = ($tree);
41             }
42             # if we are not including the trunk
43             else {
44             # then we enqueue all the
45             # trunks children instead
46 4         55 @queue = ($tree->getAllChildren());
47             }
48             # until our queue is empty
49 7         50 while (scalar(@queue) != 0){
50             # get the first item off the queue
51 78         292 my $current_tree = shift @queue;
52             # enqueue all the current tree's children
53 78         107 push @queue => $current_tree->getAllChildren();
54             # now collect the results
55 78 100       348 push @results => (($filter_function) ?
56             $filter_function->($current_tree)
57             :
58             $current_tree->getNodeValue());
59             }
60             # store our results
61 3         52 $self->setResults(@results);
62             }
63              
64             1;
65              
66             __END__