| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::SlidingWindow v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use Carp qw( croak ); | ||
| 8 | |||
| 9 | use LinkedList::Single qw( v2 ); | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | # methods | ||
| 13 | ######################################################################## | ||
| 14 | |||
| 15 | sub new( $class, %arg ) | ||
| 16 | { | ||
| 17 | my $span = $arg{ span } | ||
| 18 | // croak 'Botched new: missing span'; | ||
| 19 | |||
| 20 | my $list = LinkedList::Single->new; | ||
| 21 | |||
| 22 | my $self | ||
| 23 | = bless | ||
| 24 | { | ||
| 25 | span => $span | ||
| 26 | , list => $list | ||
| 27 | |||
| 28 | # the tail is a cursor parked on the sentinel at the end of | ||
| 29 | # the list, so recording an event never walks the list. | ||
| 30 | |||
| 31 | , tail => $list->cursor | ||
| 32 | } | ||
| 33 | => $class; | ||
| 34 | |||
| 35 | $self | ||
| 36 | } | ||
| 37 | |||
| 38 | ######################################################################## | ||
| 39 | # add an event at the tail | ||
| 40 | ######################################################################## | ||
| 41 | |||
| 42 | sub record( $self, $time, @payload ) | ||
| 43 | { | ||
| 44 | # push at the sentinel fills it and hangs a new sentinel | ||
| 45 | # off the end; advance moves the cursor onto the new one. | ||
| 46 | # | ||
| 47 | # Note: $list->push would walk the whole list to find the tail | ||
| 48 | # on every call, which makes building a long window quadratic. | ||
| 49 | |||
| 50 | $self->{ tail }->push( $time, @payload )->advance; | ||
| 51 | |||
| 52 | $self | ||
| 53 | } | ||
| 54 | |||
| 55 | ######################################################################## | ||
| 56 | # discard events older than the span | ||
| 57 | # | ||
| 58 | # the expired events are always a prefix of the list. they are | ||
| 59 | # cut off whole with two fragments rather than shifted one by one, | ||
| 60 | # and then consumed: each node is released, and each payload | ||
| 61 | # destroyed, as the fragment advances past it. | ||
| 62 | ######################################################################## | ||
| 63 | |||
| 64 | sub expire( $self, $now, $handler = sub {} ) | ||
| 65 | { | ||
| 66 | my $list = $self->{ list }; | ||
| 67 | my $cutoff = $now - $self->{ span }; | ||
| 68 | |||
| 69 | # the usual case in a busy window is that nothing has expired. | ||
| 70 | |||
| 71 | $list | ||
| 72 | and ( $list->data )[ 0 ] < $cutoff | ||
| 73 | or return 0; | ||
| 74 | |||
| 75 | # take every node: the list is left empty. | ||
| 76 | |||
| 77 | my $dead = $list->fragment; | ||
| 78 | |||
| 79 | # the first node still inside the window, if there is one. | ||
| 80 | |||
| 81 | my $live = $dead->first( sub( $time, @ ) { $time >= $cutoff } ); | ||
| 82 | |||
| 83 | if( $live ) | ||
| 84 | { | ||
| 85 | # cutting at the boundary leaves $dead ending there and | ||
| 86 | # returns the live nodes as a fragment, which goes | ||
| 87 | # straight back onto the (empty) list. | ||
| 88 | # | ||
| 89 | # the sentinel at the end of the chain never moves, so | ||
| 90 | # the tail cursor is still parked on it. | ||
| 91 | |||
| 92 | $live->fragment->push_onto( $list ); | ||
| 93 | } | ||
| 94 | else | ||
| 95 | { | ||
| 96 | # everything expired: the old sentinel went off with $dead | ||
| 97 | # so the tail needs a new place to stand. | ||
| 98 | |||
| 99 | $self->{ tail } = $list->cursor; | ||
| 100 | } | ||
| 101 | |||
| 102 | my $count = 0; | ||
| 103 | |||
| 104 | $dead->consume | ||
| 105 | ( | ||
| 106 | sub( @event ) | ||
| 107 | { | ||
| 108 | ++$count; | ||
| 109 | |||
| 110 | $handler->( @event ); | ||
| 111 | } | ||
| 112 | ); | ||
| 113 | |||
| 114 | $count | ||
| 115 | } | ||
| 116 | |||
| 117 | ######################################################################## | ||
| 118 | # introspection | ||
| 119 | ######################################################################## | ||
| 120 | |||
| 121 | # arrayref of [ $time, @payload ], oldest first. | ||
| 122 | |||
| 123 | sub contents( $self ) | ||
| 124 | { | ||
| 125 | ( scalar $self->{ list }->extract ) // [] | ||
| 126 | } | ||
| 127 | |||
| 128 | sub size( $self ) | ||
| 129 | { | ||
| 130 | $self->{ list }->count | ||
| 131 | } | ||
| 132 | |||
| 133 | 1 | ||
| 134 | __END__ |