| 1 | package Tree::Simple::Visitor::Sort; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.16'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use base qw(Tree::Simple::Visitor); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 5 | my ($_class) = @_; | |||||||||||||||||||||||||||
| 14 | 5 | ▶ | my $class = ref($_class) || $_class; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_class || $_class
MC/DC0%0/2
ref $_class || $_class
ref $_class
$_class
Truth table33%1/3
A || B
A= ref $_classB= $_class
| |||||||||||||||||||||||||||||
| 15 | 5 | my $visitor = {}; | |||||||||||||||||||||||||||
| 16 | 5 | bless($visitor, $class); | |||||||||||||||||||||||||||
| 17 | 5 | $visitor->_init(); | |||||||||||||||||||||||||||
| 18 | 5 | return $visitor; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | my ($self) = @_; | |||||||||||||||||||||||||||
| 23 | 5 | $self->{sort_function} = undef; | |||||||||||||||||||||||||||
| 24 | 5 | $self->SUPER::_init(); | |||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 2 | ▶ | sub REVERSE { sub ($$) { $_[1]->getNodeValue() cmp $_[0]->getNodeValue() }}; | ||||||||||||||||||||||||||
|
Subroutine
REVERSE: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 2 | ▶ | sub NUMERIC { sub ($$) { $_[0]->getNodeValue() <=> $_[1]->getNodeValue() }}; | ||||||||||||||||||||||||||
|
Subroutine
NUMERIC: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | sub REVERSE_NUMERIC { sub ($$) { $_[1]->getNodeValue() <=> $_[0]->getNodeValue() }}; | ||||||||||||||||||||||||||
|
Subroutine
REVERSE_NUMERIC: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 2 | ▶ | sub ALPHABETICAL { sub ($$) { lc($_[0]->getNodeValue()) cmp lc($_[1]->getNodeValue()) }}; | ||||||||||||||||||||||||||
|
Subroutine
ALPHABETICAL: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 2 | ▶ | sub REVERSE_ALPHABETICAL { sub ($$) { lc($_[1]->getNodeValue()) cmp lc($_[0]->getNodeValue()) }}; | ||||||||||||||||||||||||||
|
Subroutine
REVERSE_ALPHABETICAL: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub setSortFunction { | ||||||||||||||||||||||||||
|
Subroutine
setSortFunction: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 34 | 4 | my ($self, $sort_function) = @_; | |||||||||||||||||||||||||||
| 35 | 4 | ▶ | (defined($sort_function) && ref($sort_function) eq "CODE") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $sort_function and ref $sort_function eq "CODE"
MC/DC100%2/2
defined $sort_function and ref $sort_function eq "CODE"
defined $sort_function
ref $sort_function eq "CODE"
Truth table100%3/3
A and B
A= defined $sort_functionB= ref $sort_function eq "CODE"
| |||||||||||||||||||||||||||||
| 36 | || die "Insufficient Arguments : You must supply a CODE reference for the sort function"; | ||||||||||||||||||||||||||||
| 37 | 1 | $self->{sort_function} = $sort_function; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 8 | ▶ | sub visit { | ||||||||||||||||||||||||||
|
Subroutine
visit: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 8 | my ($self, $tree) = @_; | |||||||||||||||||||||||||||
| 42 | 8 | ▶ | (blessed($tree) && $tree->isa("Tree::Simple")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
builtin::blessed($tree) and $tree->isa("Tree::Simple")
MC/DC100%2/2
builtin::blessed($tree) and $tree->isa("Tree::Simple")
builtin::blessed($tree)
$tree->isa("Tree::Simple")
Truth table100%3/3
A and B
A= builtin::blessed($tree)B= $tree->isa("Tree::Simple")
| |||||||||||||||||||||||||||||
| 43 | || die "Insufficient Arguments : You must supply a valid Tree::Simple object"; | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # No childs, nothing to sort | ||||||||||||||||||||||||||||
| 46 | 4 | ▶ | return if $tree->isLeaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 3 | my $sort_function; | |||||||||||||||||||||||||||
| 49 | 3 | ▶ | if ($self->{sort_function}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 1 | $sort_function = $self->{sort_function}; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | else { | ||||||||||||||||||||||||||||
| 53 | # get the node filter | ||||||||||||||||||||||||||||
| 54 | 2 | my $filter_func = $self->getNodeFilter(); | |||||||||||||||||||||||||||
| 55 | 2 | ▶ | if ($filter_func) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 1 | ▶ | $sort_function = sub { $filter_func->($a) cmp $filter_func->($b) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | else { | ||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $sort_function = sub { $a->getNodeValue() cmp $b->getNodeValue() }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | # otherwise sort them | ||||||||||||||||||||||||||||
| 64 | 3 | $self->_sortTree($sort_function, $tree); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 16 | ▶ | sub _sortTree { | ||||||||||||||||||||||||||
|
Subroutine
_sortTree: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 68 | 16 | my ($self, $sort_function, $tree) = @_; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # sort children, using the sort filter | ||||||||||||||||||||||||||||
| 71 | 16 | my @childs = sort { $sort_function->($a, $b) } $tree->getAllChildren(); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Create the new sequence | ||||||||||||||||||||||||||||
| 74 | 16 | foreach my $child (@childs) { | |||||||||||||||||||||||||||
| 75 | # get the removed child | ||||||||||||||||||||||||||||
| 76 | 39 | $child = $tree->removeChild($child); | |||||||||||||||||||||||||||
| 77 | # and be sure that is the one | ||||||||||||||||||||||||||||
| 78 | # we re-insert | ||||||||||||||||||||||||||||
| 79 | 39 | $tree->addChild($child); | |||||||||||||||||||||||||||
| 80 | # only sort the child if | ||||||||||||||||||||||||||||
| 81 | # it is not a leaf | ||||||||||||||||||||||||||||
| 82 | 39 | ▶ | $self->_sortTree($sort_function, $child) unless $child->isLeaf(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 1 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||