line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# ABSTRACT: IO::Async timer based sorting algorithm |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package Acme::Sort::Sleep; |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
695
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
43
|
|
6
|
2
|
|
|
2
|
|
5
|
use warnings; |
|
2
|
|
|
|
|
1
|
|
|
2
|
|
|
|
|
34
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
1171
|
use IO::Async::Loop; |
|
2
|
|
|
|
|
71333
|
|
|
2
|
|
|
|
|
48
|
|
9
|
2
|
|
|
2
|
|
800
|
use IO::Async::Timer::Countdown; |
|
2
|
|
|
|
|
22912
|
|
|
2
|
|
|
|
|
42
|
|
10
|
2
|
|
|
2
|
|
9
|
use Scalar::Util qw( looks_like_number ); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
100
|
|
11
|
|
|
|
|
|
|
|
12
|
2
|
|
|
2
|
|
8
|
use base 'Exporter'; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
140
|
|
13
|
|
|
|
|
|
|
our @EXPORT_OK = qw( sleepsort ); |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
2
|
|
8
|
use constant ERROR_STR => "Only positive numbers accepted."; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
311
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub sleepsort { |
18
|
|
|
|
|
|
|
|
19
|
5
|
|
|
5
|
0
|
983
|
my @unsorted = @_; |
20
|
5
|
|
|
|
|
5
|
my @sorted = (); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# handle empty list |
23
|
5
|
100
|
|
|
|
13
|
return () unless @unsorted; |
24
|
|
|
|
|
|
|
|
25
|
4
|
|
|
|
|
19
|
my $loop = IO::Async::Loop->new; |
26
|
|
|
|
|
|
|
|
27
|
4
|
|
|
|
|
6360
|
for my $num ( @unsorted ) { |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# only allow positive numbers |
30
|
7
|
100
|
|
|
|
1951
|
die ERROR_STR unless defined $num; |
31
|
6
|
100
|
|
|
|
28
|
die ERROR_STR unless looks_like_number $num; |
32
|
5
|
100
|
|
|
|
17
|
die ERROR_STR unless $num >= 0; |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
my $timer = IO::Async::Timer::Countdown->new( |
35
|
|
|
|
|
|
|
delay => $num, |
36
|
|
|
|
|
|
|
remove_on_expire => 1, |
37
|
|
|
|
|
|
|
on_expire => sub { |
38
|
|
|
|
|
|
|
|
39
|
4
|
|
|
4
|
|
3337462
|
push @sorted, $num; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# no more timers/numbers left to sort |
42
|
4
|
100
|
|
|
|
15
|
$loop->stop unless $loop->notifiers; |
43
|
|
|
|
|
|
|
}, |
44
|
4
|
|
|
|
|
27
|
); |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
183
|
$timer->start; |
47
|
4
|
|
|
|
|
33
|
$loop->add( $timer ); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
1
|
|
|
|
|
65
|
$loop->run; |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
60
|
return @sorted; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
1; |