line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Articulate::Role::Sortation::AllYouNeedIsCmp; |
2
|
3
|
|
|
3
|
|
9114
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
122
|
|
3
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
66
|
|
4
|
3
|
|
|
3
|
|
9
|
use Moo::Role; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
17
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Articulate::Role::Sortation::AllYouNeedIsCmp - provides sortation methods derived from you cmp method |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 SYNOPSIS |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
package My::Sortation::Class; |
13
|
|
|
|
|
|
|
use Moo; |
14
|
|
|
|
|
|
|
with 'Articulate::Role::Sortation::AllYouNeedIsCmp'; |
15
|
|
|
|
|
|
|
sub cmp { ... } |
16
|
|
|
|
|
|
|
sub decorate { ... } # optional |
17
|
|
|
|
|
|
|
1; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
Provides the C and C funtions. These call on the C method (which you are exected to write) and, optionally, the C method (if it exists). |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=cut |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 METHODS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head3 order |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Convenience method which inspects the C value of the C hash and returns it (or returns C) if undefined). |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=cut |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub order { |
32
|
3
|
|
|
3
|
1
|
5
|
my $self = shift; |
33
|
3
|
100
|
50
|
|
|
31
|
return ( $self->options->{order} // 'asc' ) if $self->can('options'); |
34
|
1
|
|
|
|
|
3
|
return 'asc'; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
=head3 sort |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$sorter->sort( [items] ) |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
Performs a simple sort, using C and C. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub sort { |
47
|
3
|
|
|
3
|
1
|
86
|
my $self = shift; |
48
|
3
|
|
|
|
|
4
|
my $items = shift; |
49
|
|
|
|
|
|
|
my $dec = sub { |
50
|
48
|
|
|
48
|
|
38
|
my $orig = shift; |
51
|
48
|
100
|
|
|
|
144
|
$self->can('decorate') |
52
|
|
|
|
|
|
|
? $self->decorate($orig) |
53
|
|
|
|
|
|
|
: $orig |
54
|
3
|
|
|
|
|
13
|
}; |
55
|
3
|
50
|
|
|
|
8
|
return [ sort { $self->cmp( $dec->($b), $dec->($a) ) } @$items ] if 'desc' eq $self->order; |
|
0
|
|
|
|
|
0
|
|
56
|
3
|
|
|
|
|
474
|
return [ sort { $self->cmp( $dec->($a), $dec->($b) ) } @$items ]; |
|
24
|
|
|
|
|
40
|
|
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head3 schwartz |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$sorter->schwartz( [items] ); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
Performs a schwartxian transform using C, and sorts the decorated items using C, then returns the originals in the sorted order. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=cut |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub schwartz { |
69
|
2
|
|
|
2
|
1
|
41
|
my $self = shift; |
70
|
2
|
|
|
|
|
3
|
my $items = shift; |
71
|
2
|
50
|
|
|
|
6
|
if ( $self->can('decorate') ) { |
72
|
|
|
|
|
|
|
return [ |
73
|
6
|
|
|
|
|
16
|
map { $_->[0] } |
|
5
|
|
|
|
|
9
|
|
74
|
6
|
|
|
|
|
14
|
sort { $self->cmp( $a->[1], $b->[1] ) } |
75
|
2
|
|
|
|
|
4
|
map { [$_, $self->decorate($_)] } |
76
|
|
|
|
|
|
|
@$items |
77
|
|
|
|
|
|
|
]; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
else { |
80
|
0
|
|
|
|
|
|
return $self->sort($items); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |