Example/SlidingWindow/Array.pm untested

filter: Statementstmt 0.0% 0 / 25 Branchbran n/a Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 48 CC 1 SCAR 6.9
CC
1
Cov
0%
new
2.0
record
2.0
expire
2.0
CRAP 2.0
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::SlidingWindow::Array v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 ########################################################################
10 # methods
11 ########################################################################
12  
13 sub new( $class, %arg )
14 {
15 my $span = $arg{ span }
16 // croak 'Botched new: missing span';
17  
18 bless
19 {
20 span => $span
21 , window => []
22 }
23 => $class
24 }
25  
26 sub record( $self, $time, @payload )
27 {
28 push $self->{ window }->@*, [ $time, @payload ];
29  
30 $self
31 }
32  
33 sub expire( $self, $now, $handler = sub {} )
34 {
35 my $window = $self->{ window };
36 my $cutoff = $now - $self->{ span };
37  
38 # count the expired prefix, then splice it off the front.
39  
40 my $count = 0;
41  
42 ++$count
43 while $count < $window->@*
44 && $window->[ $count ][ 0 ] < $cutoff;
45  
46 $count
47 or return 0;
48  
49 my @dead = splice $window->@*, 0, $count;
50  
51 $handler->( $_->@* ) for @dead;
52  
53 $count
54 }
55  
56 sub contents( $self )
57 {
58 [ map { [ $_->@* ] } $self->{ window }->@* ]
59 }
60  
61 sub size( $self )
62 {
63 scalar $self->{ window }->@*
64 }
65  
66 1
67 __END__