blib/lib/IO/Async/Internals/TimeQueue.pm

filter: Statementstmt 62.8% 61 / 97 Branchbran 68.1% 15 / 22 Conditioncond 50.0% 3 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 65.2% 15 / 23 Podpod 0.0% 0 / 11 totaltotal 58.9% 96 / 163 CC 16 SCAR 12.4
CC
16
Cov
63%
HEAP__fire
12.0
HEAP_next_time
6.0
new
6.0
CRAP 3.5
« blib/lib/IO/Async/OS/MSWin32.pm ↑ Summary blib/lib/IO/Async/Protocol/Stream.pm »
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
BranchTrueFalseTotal
if (eval { do { require Heap::Fibonacci } }) { } 0 76 76
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
BranchTrueFalseTotal
ref $code or croak("Expected 'code' to be a reference") 2 699 701
39  
40 699 ▶ defined $params{time} or croak "Expected 'time'";
Branch: 1 branch
BranchTrueFalseTotal
defined $params{"time"} or croak("Expected 'time'") 1 698 699
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
BranchTrueFalseTotal
exists $params{'now'} ? : 107 1150 1257
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
BranchTrueFalseTotal
@$self ? : 1219 29 1248
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
BranchTrueFalseTotal
$idx < @$self and $self->[$idx][0] <= $time and do { ++$idx } 5118 698 5816
Condition100%3/3
$idx < @$self and $self->[$idx][0] <= $time
!ll&&!rl&&r
662365118
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if (@$self) 1381 51 1432
103 1381 ▶ last if( $self->[0]->[TIME] > $now );
Branch: 1 branch
BranchTrueFalseTotal
if $self->[0][0] > $now 1204 177 1381
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
BranchTrueFalseTotal
defined $top ? : 0 0 0
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
BranchTrueFalseTotal
if (defined(my $top = $self->top)) 0 0 0
152 0 ▶ last if( $top->time > $now );
Branch: 1 branch
BranchTrueFalseTotal
if $top->time > $now 0 0 0
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
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
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;
« blib/lib/IO/Async/OS/MSWin32.pm ↑ Summary blib/lib/IO/Async/Protocol/Stream.pm »