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   22283 use strict;
  4         4  
  4         96  
4 4     4   12 use warnings;
  4         5  
  4         141  
5              
6             our $VERSION = '0.15';
7              
8 4     4   14 use Scalar::Util qw(blessed);
  4         6  
  4         225  
9              
10 4     4   13 use base qw(Tree::Simple::Visitor);
  4         6  
  4         1253  
11              
12             sub new {
13 6     6 1 4701 my ($_class) = @_;
14 6   33     32 my $class = ref($_class) || $_class;
15 6         8 my $visitor = {};
16 6         12 bless($visitor, $class);
17 6         15 $visitor->_init();
18 6         55 return $visitor;
19             }
20              
21             sub _init {
22 6     6   6 my ($self) = @_;
23 6         37 $self->SUPER::_init();
24             }
25              
26             sub visit {
27 11     11 1 2708 my ($self, $tree) = @_;
28 11 100 100     105 (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         9 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         54 my @queue;
37             # if we are to include the trunk
38 7 100       18 if ($self->includeTrunk()) {
39             # then enqueue that
40 3         13 @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         32 @queue = ($tree->getAllChildren());
47             }
48             # until our queue is empty
49 7         43 while (scalar(@queue) != 0){
50             # get the first item off the queue
51 78         256 my $current_tree = shift @queue;
52             # enqueue all the current tree's children
53 78         91 push @queue => $current_tree->getAllChildren();
54             # now collect the results
55 78 100       300 push @results => (($filter_function) ?
56             $filter_function->($current_tree)
57             :
58             $current_tree->getNodeValue());
59             }
60             # store our results
61 3         22 $self->setResults(@results);
62             }
63              
64             1;
65              
66             __END__