line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Sort::XS; |
2
|
4
|
|
|
4
|
|
91892
|
use strict; |
|
4
|
|
|
|
|
8
|
|
|
4
|
|
|
|
|
165
|
|
3
|
4
|
|
|
4
|
|
21
|
use warnings; |
|
4
|
|
|
|
|
9
|
|
|
4
|
|
|
|
|
126
|
|
4
|
4
|
|
|
4
|
|
32
|
use base Exporter::; |
|
4
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
674
|
|
5
|
|
|
|
|
|
|
our @EXPORT = qw(xsort ixsort sxsort); |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.30'; |
8
|
|
|
|
|
|
|
require XSLoader; |
9
|
|
|
|
|
|
|
XSLoader::load( 'Sort::XS', $VERSION ); |
10
|
4
|
|
|
4
|
|
21
|
use Carp qw/croak/; |
|
4
|
|
|
|
|
6
|
|
|
4
|
|
|
|
|
339
|
|
11
|
|
|
|
|
|
|
|
12
|
4
|
|
|
4
|
|
25
|
use constant ERR_MSG_NOLIST => 'Need to provide a list'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
287
|
|
13
|
4
|
|
|
4
|
|
20
|
use constant ERR_MSG_UNKNOWN_ALGO => 'Unknown algorithm : '; |
|
4
|
|
|
|
|
13
|
|
|
4
|
|
|
|
|
306
|
|
14
|
4
|
|
|
4
|
|
18
|
use constant ERR_MSG_NUMBER_ARGUMENTS => 'Bad number of arguments'; |
|
4
|
|
|
|
|
7
|
|
|
4
|
|
|
|
|
2805
|
|
15
|
|
|
|
|
|
|
my $_mapping = { |
16
|
|
|
|
|
|
|
quick => \&Sort::XS::quick_sort, |
17
|
|
|
|
|
|
|
heap => \&Sort::XS::heap_sort, |
18
|
|
|
|
|
|
|
merge => \&Sort::XS::merge_sort, |
19
|
|
|
|
|
|
|
insertion => \&Sort::XS::insertion_sort, |
20
|
|
|
|
|
|
|
perl => \&_perl_sort, |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# string sorting |
23
|
|
|
|
|
|
|
quick_str => \&Sort::XS::quick_sort_str, |
24
|
|
|
|
|
|
|
heap_str => \&Sort::XS::heap_sort_str, |
25
|
|
|
|
|
|
|
merge_str => \&Sort::XS::merge_sort_str, |
26
|
|
|
|
|
|
|
insertion_str => \&Sort::XS::insertion_sort_str, |
27
|
|
|
|
|
|
|
perl_str => \&_perl_sort_str, |
28
|
|
|
|
|
|
|
}; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# API to call XS subs |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub xsort { |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# shortcut to speedup API usage, we first advantage preferred usage |
35
|
|
|
|
|
|
|
# ( we could avoid it... but we want to provide an api as fast as possible ) |
36
|
121
|
|
|
121
|
1
|
24081
|
my $argc = scalar @_; |
37
|
121
|
100
|
|
|
|
303
|
if ( $argc == 1 ) { |
38
|
6
|
100
|
|
|
|
288
|
croak ERR_MSG_NOLIST unless ref $_[0] eq ref []; |
39
|
4
|
|
|
|
|
49
|
return Sort::XS::quick_sort( $_[0] ); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# default parameters |
43
|
115
|
|
|
|
|
122
|
my %params; |
44
|
115
|
|
|
|
|
199
|
$params{algorithm} = 'quick'; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# default list |
47
|
115
|
|
|
|
|
206
|
$params{list} = $_[0]; |
48
|
|
|
|
|
|
|
|
49
|
115
|
100
|
|
|
|
384
|
croak ERR_MSG_NOLIST unless $params{list}; |
50
|
114
|
|
|
|
|
120
|
my %args; |
51
|
114
|
100
|
|
|
|
351
|
unless ( ref $params{list} eq ref [] ) { |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# hash input |
54
|
25
|
50
|
|
|
|
59
|
croak ERR_MSG_NUMBER_ARGUMENTS if $argc % 2; |
55
|
25
|
|
|
|
|
70
|
(%args) = @_; |
56
|
25
|
50
|
33
|
|
|
144
|
croak ERR_MSG_NOLIST |
57
|
|
|
|
|
|
|
unless defined $args{list} && ref $args{list} eq ref []; |
58
|
25
|
|
|
|
|
51
|
$params{list} = $args{list}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
else { |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# first element was the array, then hash option |
63
|
89
|
50
|
|
|
|
220
|
croak ERR_MSG_NUMBER_ARGUMENTS unless scalar @_ % 2; |
64
|
89
|
|
|
|
|
88
|
my $void; |
65
|
89
|
|
|
|
|
312
|
( $void, %args ) = @_; |
66
|
|
|
|
|
|
|
} |
67
|
114
|
|
100
|
|
|
208
|
map { $params{$_} = $args{$_} || $params{$_}; } qw/algorithm type/; |
|
228
|
|
|
|
|
809
|
|
68
|
|
|
|
|
|
|
|
69
|
114
|
100
|
100
|
|
|
577
|
my $type = |
70
|
|
|
|
|
|
|
( defined $params{type} && $params{type} eq 'string' ) ? '_str' : ''; |
71
|
114
|
|
|
|
|
247
|
my $sub = $_mapping->{ $params{algorithm} . $type }; |
72
|
114
|
100
|
|
|
|
533
|
croak( ERR_MSG_UNKNOWN_ALGO, $params{algorithm} ) unless defined $sub; |
73
|
|
|
|
|
|
|
|
74
|
112
|
|
|
|
|
1060
|
return $sub->( $params{list} ); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
# shortcut to xsort with integers |
78
|
|
|
|
|
|
|
sub ixsort { |
79
|
24
|
|
|
24
|
1
|
14645
|
xsort(@_); |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# shortcut to xsort with strings |
83
|
|
|
|
|
|
|
sub sxsort { |
84
|
48
|
|
|
48
|
1
|
21779
|
xsort( @_, type => 'string' ); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _perl_sort { |
88
|
16
|
|
|
16
|
|
25
|
my $list = shift; |
89
|
16
|
|
|
|
|
21
|
my @sorted = sort { $a <=> $b } @{$list}; |
|
304
|
|
|
|
|
387
|
|
|
16
|
|
|
|
|
62
|
|
90
|
16
|
|
|
|
|
121
|
return \@sorted; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _perl_sort_str { |
94
|
16
|
|
|
16
|
|
22
|
my $list = shift; |
95
|
16
|
|
|
|
|
18
|
my @sorted = sort { $a cmp $b } @{$list}; |
|
256
|
|
|
|
|
316
|
|
|
16
|
|
|
|
|
60
|
|
96
|
16
|
|
|
|
|
104
|
return \@sorted; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
1; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
__END__ |