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