/usr/share/perl5/IO/Async/Internals/TimeQueue.pm is in libio-async-perl 0.61-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | # You may distribute under the terms of either the GNU General Public License
# or the Artistic License (the same terms as Perl itself)
#
# (C) Paul Evans, 2006-2012 -- leonerd@leonerd.org.uk
package # hide from CPAN
IO::Async::Internals::TimeQueue;
use strict;
use warnings;
use Carp;
use Time::HiRes qw( time );
BEGIN {
my @methods = qw( next_time _enqueue cancel _fire );
if( eval { require Heap::Fibonacci } ) {
unshift our @ISA, "Heap::Fibonacci";
require Heap::Elem;
no strict 'refs';
*$_ = \&{"HEAP_$_"} for @methods;
}
else {
no strict 'refs';
*$_ = \&{"ARRAY_$_"} for "new", @methods;
}
}
# High-level methods
sub enqueue
{
my $self = shift;
my ( %params ) = @_;
my $code = delete $params{code};
ref $code or croak "Expected 'code' to be a reference";
defined $params{time} or croak "Expected 'time'";
my $time = $params{time};
$self->_enqueue( $time, $code );
}
sub fire
{
my $self = shift;
my ( %params ) = @_;
my $now = exists $params{now} ? $params{now} : time;
$self->_fire( $now );
}
# Implementation using a Perl array
use constant {
TIME => 0,
CODE => 1,
};
sub ARRAY_new
{
my $class = shift;
return bless [], $class;
}
sub ARRAY_next_time
{
my $self = shift;
return @$self ? $self->[0]->[TIME] : undef;
}
sub ARRAY__enqueue
{
my $self = shift;
my ( $time, $code ) = @_;
# TODO: This could be more efficient maybe using a binary search
my $idx = 0;
$idx++ while $idx < @$self and $self->[$idx][TIME] < $time;
splice @$self, $idx, 0, ( my $elem = [ $time, $code ]);
return $elem;
}
sub ARRAY_cancel
{
my $self = shift;
my ( $id ) = @_;
@$self = grep { $_ != $id } @$self;
}
sub ARRAY__fire
{
my $self = shift;
my ( $now ) = @_;
my $count = 0;
while( @$self ) {
last if( $self->[0]->[TIME] > $now );
my $top = shift @$self;
$top->[CODE]->();
$count++;
}
return $count;
}
# Implementation using Heap::Fibonacci
sub HEAP_next_time
{
my $self = shift;
my $top = $self->top;
return defined $top ? $top->time : undef;
}
sub HEAP__enqueue
{
my $self = shift;
my ( $time, $code ) = @_;
my $elem = IO::Async::Internals::TimeQueue::Elem->new( $time, $code );
$self->add( $elem );
return $elem;
}
sub HEAP_cancel
{
my $self = shift;
my ( $id ) = @_;
$self->delete( $id );
}
sub HEAP__fire
{
my $self = shift;
my ( $now ) = @_;
my $count = 0;
while( defined( my $top = $self->top ) ) {
last if( $top->time > $now );
$self->extract_top;
$top->code->();
$count++;
}
return $count;
}
package # hide from CPAN
IO::Async::Internals::TimeQueue::Elem;
use strict;
our @ISA = qw( Heap::Elem );
sub new
{
my $self = shift;
my $class = ref $self || $self;
my ( $time, $code ) = @_;
my $new = $class->SUPER::new(
time => $time,
code => $code,
);
return $new;
}
sub time
{
my $self = shift;
return $self->val->{time};
}
sub code
{
my $self = shift;
return $self->val->{code};
}
# This only uses methods so is transparent to HASH or ARRAY
sub cmp
{
my $self = shift;
my $other = shift;
$self->time <=> $other->time;
}
0x55AA;
|