line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Tree::Simple::Visitor::Sort; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
19776
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
4
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.14'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Scalar::Util qw(blessed); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
87
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Tree::Simple::Visitor); |
|
1
|
|
|
|
|
0
|
|
|
1
|
|
|
|
|
412
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
5
|
|
|
5
|
1
|
3314
|
my ($_class) = @_; |
14
|
5
|
|
33
|
|
|
20
|
my $class = ref($_class) || $_class; |
15
|
5
|
|
|
|
|
7
|
my $visitor = {}; |
16
|
5
|
|
|
|
|
7
|
bless($visitor, $class); |
17
|
5
|
|
|
|
|
8
|
$visitor->_init(); |
18
|
5
|
|
|
|
|
24
|
return $visitor; |
19
|
|
|
|
|
|
|
} |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub _init { |
22
|
5
|
|
|
5
|
|
6
|
my ($self) = @_; |
23
|
5
|
|
|
|
|
8
|
$self->{sort_function} = undef; |
24
|
5
|
|
|
|
|
13
|
$self->SUPER::_init(); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
2
|
|
|
2
|
1
|
848
|
sub REVERSE { sub ($$) { $_[1]->getNodeValue() cmp $_[0]->getNodeValue() }}; |
|
10
|
|
|
10
|
|
14
|
|
28
|
2
|
|
|
2
|
1
|
43
|
sub NUMERIC { sub ($$) { $_[0]->getNodeValue() <=> $_[1]->getNodeValue() }}; |
|
1
|
|
|
1
|
|
3
|
|
29
|
2
|
|
|
2
|
1
|
302
|
sub REVERSE_NUMERIC { sub ($$) { $_[1]->getNodeValue() <=> $_[0]->getNodeValue() }}; |
|
1
|
|
|
1
|
|
3
|
|
30
|
2
|
|
|
2
|
1
|
301
|
sub ALPHABETICAL { sub ($$) { lc($_[0]->getNodeValue()) cmp lc($_[1]->getNodeValue()) }}; |
|
1
|
|
|
1
|
|
3
|
|
31
|
2
|
|
|
2
|
1
|
301
|
sub REVERSE_ALPHABETICAL { sub ($$) { lc($_[1]->getNodeValue()) cmp lc($_[0]->getNodeValue()) }}; |
|
1
|
|
|
1
|
|
2
|
|
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub setSortFunction { |
34
|
4
|
|
|
4
|
1
|
527
|
my ($self, $sort_function) = @_; |
35
|
4
|
100
|
100
|
|
|
33
|
(defined($sort_function) && ref($sort_function) eq "CODE") |
36
|
|
|
|
|
|
|
|| die "Insufficient Arguments : You must supply a CODE reference for the sort function"; |
37
|
1
|
|
|
|
|
3
|
$self->{sort_function} = $sort_function; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub visit { |
41
|
8
|
|
|
8
|
1
|
2896
|
my ($self, $tree) = @_; |
42
|
8
|
100
|
100
|
|
|
69
|
(blessed($tree) && $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
|
100
|
|
|
|
14
|
return if $tree->isLeaf(); |
47
|
|
|
|
|
|
|
|
48
|
3
|
|
|
|
|
18
|
my $sort_function; |
49
|
3
|
100
|
|
|
|
6
|
if ($self->{sort_function}) { |
50
|
1
|
|
|
|
|
1
|
$sort_function = $self->{sort_function}; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
else { |
53
|
|
|
|
|
|
|
# get the node filter |
54
|
2
|
|
|
|
|
6
|
my $filter_func = $self->getNodeFilter(); |
55
|
2
|
100
|
|
|
|
9
|
if ($filter_func) { |
56
|
1
|
|
|
5
|
|
2
|
$sort_function = sub { $filter_func->($a) cmp $filter_func->($b) }; |
|
5
|
|
|
|
|
6
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
else { |
59
|
1
|
|
|
11
|
|
3
|
$sort_function = sub { $a->getNodeValue() cmp $b->getNodeValue() }; |
|
11
|
|
|
|
|
14
|
|
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# otherwise sort them |
64
|
3
|
|
|
|
|
6
|
$self->_sortTree($sort_function, $tree); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _sortTree { |
68
|
16
|
|
|
16
|
|
63
|
my ($self, $sort_function, $tree) = @_; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# sort children, using the sort filter |
71
|
16
|
|
|
|
|
20
|
my @childs = sort { $sort_function->($a, $b) } $tree->getAllChildren(); |
|
26
|
|
|
|
|
96
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# Create the new sequence |
74
|
16
|
|
|
|
|
76
|
foreach my $child (@childs) { |
75
|
|
|
|
|
|
|
# get the removed child |
76
|
39
|
|
|
|
|
130
|
$child = $tree->removeChild($child); |
77
|
|
|
|
|
|
|
# and be sure that is the one |
78
|
|
|
|
|
|
|
# we re-insert |
79
|
39
|
|
|
|
|
2478
|
$tree->addChild($child); |
80
|
|
|
|
|
|
|
# only sort the child if |
81
|
|
|
|
|
|
|
# it is not a leaf |
82
|
39
|
100
|
|
|
|
1841
|
$self->_sortTree($sort_function, $child) unless $child->isLeaf(); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
1; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
__END__ |