Example/LRU.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 143 CC 6 SCAR 19.6
CC
6
Cov
0%
put
20.0
_promote
12.0
new
2.0
CRAP 7.1
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::LRU v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8 use Scalar::Util qw( refaddr );
9  
10 use LinkedList::Single qw( v2 );
11  
12 ########################################################################
13 # the cache is a list that begins with a permanent anchor node:
14 #
15 # anchor -> newest -> ... -> oldest -> sentinel
16 #
17 # every entry is a node [ $key, $value ]. the hash finds an entry, the
18 # list keeps the order, and moving an entry to the front, or dropping
19 # the oldest one, is a few pointer changes whatever the size.
20 #
21 # things kept beside the list, all of them cursors:
22 #
23 # anchor the first node, never removed. it gives the newest entry a
24 # predecessor, which is what the index needs.
25 # index key => cursor on the node *before* that entry. a singly
26 # linked list can only unlink a node from its predecessor,
27 # so this is what makes a hit O(1).
28 # last cursor on the node before the *oldest* entry. eviction
29 # unlinks the oldest entry from it.
30 #
31 # the cursors are only ever used to step off of ('next' and 'detach'
32 # return new objects), never advanced, so one cursor can safely be
33 # stored in several places.
34 #
35 # only operations that leave every other node where it is are used
36 # next to a stored cursor: 'append' after a non-sentinel, 'detach', and
37 # the pointer 'append_to'. 'fragment', 'delete', 'insert', 'shift' and
38 # 'unshift' move the *contents* of a node, which would leave a stored
39 # cursor looking at some other entry's data.
40 #
41 # the price of a singly linked list is the fix-ups: when an entry is
42 # unlinked, the entry behind it has a new predecessor and its index
43 # cursor has to follow. a doubly linked list would not need them.
44 ########################################################################
45  
46 sub new( $class, %arg )
47 {
48 my $limit = $arg{ limit }
49 // croak 'Botched new: missing limit';
50  
51 $limit > 0
52 or croak "Botched new: limit '$limit' is not positive";
53  
54 my $list = LinkedList::Single->new;
55  
56 # an empty list: push fills the sentinel in place.
57  
58 $list->push( 'anchor' );
59  
60 my $anchor = $list->cursor;
61  
62 bless
63 {
64 limit => $limit
65 , on_evict => $arg{ on_evict } // sub {}
66 , list => $list
67 , anchor => $anchor
68 , last => $anchor
69 , index => {}
70 , size => 0
71 }
72 => $class
73 }
74  
75 ########################################################################
76 # utility
77 ########################################################################
78  
79 # the key of the entry that follows a cursor.
80  
81 sub _key_after( $cursor )
82 {
83 ( $cursor->next_data )[ 0 ]
84 }
85  
86 # the key of the entry a cursor stands on.
87  
88 sub _key_at( $cursor )
89 {
90 ( $cursor->data )[ 0 ]
91 }
92  
93 ########################################################################
94 # move an entry to the front.
95 #
96 # A anchor, P the entry's predecessor, M the entry, T the node after M,
97 # S the node after A (the newest entry before the move).
98 #
99 # before: A -> S ... P -> M -> T
100 # after: A -> M -> S ... P -> T
101 ########################################################################
102  
103 sub _promote( $self, $key )
104 {
105 my $anchor = $self->{ anchor };
106 my $index = $self->{ index };
107 my $pred = $index->{ $key };
108  
109 # already the newest: nothing moves.
110  
111 refaddr $pred == refaddr $anchor
112 and return $self;
113  
114 my $newest = _key_after $anchor;
115  
116 # M comes out whole, in a fragment of its own: the nodes on either
117 # side are joined by the detach.
118  
119 my $entry = $pred->detach( 1 );
120  
121 # T, if there is one, now follows P.
122  
123 my $after = $pred->next;
124  
125 $after
126 and $index->{ _key_at $after } = $pred;
127  
128 # M goes in behind the anchor, S behind M.
129  
130 $entry->append_to( $anchor );
131  
132 $index->{ $key } = $anchor;
133 $index->{ $newest } = $anchor->next;
134  
135 # 'last' stands on the node before the oldest entry, and that
136 # may be a node that just moved:
137 #
138 # M was the oldest: P is, and its predecessor is whatever the
139 # index says it is after the moves above.
140 #
141 # T is the oldest: M was the node before it, and P is now.
142  
143 if( ! $after )
144 {
145 $self->{ last } = $index->{ _key_at $pred };
146 }
147 elsif( ! $pred->next( 2 ) )
148 {
149 $self->{ last } = $pred;
150 }
151  
152 $self
153 }
154  
155 ########################################################################
156 # drop the oldest entry, handing it to the eviction handler.
157 ########################################################################
158  
159 sub _evict( $self )
160 {
161 my $last = $self->{ last };
162 my $index = $self->{ index };
163  
164 my $entry = $last->detach( 1 );
165  
166 delete $index->{ _key_at $entry };
167  
168 --$self->{ size };
169  
170 # the entry before the one removed is the oldest now; with nothing
171 # left that is the anchor, and 'last' stays where it is.
172  
173 $self->{ size }
174 and $self->{ last } = $index->{ _key_at $last };
175  
176 # consuming releases the node and destroys the payload; the handler
177 # sees ( $key, $value ).
178  
179 $entry->consume( $self->{ on_evict } );
180  
181 $self
182 }
183  
184 ########################################################################
185 # public interface
186 ########################################################################
187  
188 # the value for a key and the entry becomes the newest; undef on a miss.
189  
190 sub get( $self, $key )
191 {
192 my $pred = $self->{ index }{ $key }
193 or return undef;
194  
195 $self->_promote( $key );
196  
197 ( $self->{ anchor }->next_data )[ 1 ]
198 }
199  
200 # add or replace an entry; it is now the newest. the oldest is evicted
201 # if the cache is over its limit.
202  
203 sub put( $self, $key, $value )
204 {
205 my $anchor = $self->{ anchor };
206 my $index = $self->{ index };
207  
208 if( my $pred = $index->{ $key } )
209 {
210 $self->_promote( $key );
211  
212 $anchor->next->set_data( $key, $value );
213 }
214 else
215 {
216 my $newest = $self->{ size } ? _key_after $anchor : undef;
217 my $alone = $self->{ size } == 1;
218  
219 # append after a non-sentinel links a new node in; no other
220 # node moves.
221  
222 $anchor->append( $key, $value );
223  
224 $index->{ $key } = $anchor;
225  
226 if( defined $newest )
227 {
228 $index->{ $newest } = $anchor->next;
229  
230 # the lone entry was the oldest: the new one is before it.
231  
232 $alone
233 and $self->{ last } = $anchor->next;
234 }
235 else
236 {
237 $self->{ last } = $anchor;
238 }
239  
240 ++$self->{ size };
241  
242 $self->{ size } > $self->{ limit }
243 and $self->_evict;
244 }
245  
246 $self
247 }
248  
249 sub exists( $self, $key )
250 {
251 exists $self->{ index }{ $key }
252 }
253  
254 sub size( $self )
255 {
256 $self->{ size }
257 }
258  
259 sub limit( $self )
260 {
261 $self->{ limit }
262 }
263  
264 # arrayref of [ $key, $value ], newest first.
265  
266 sub contents( $self )
267 {
268 my $all = $self->{ list }->extract;
269  
270 # the anchor is not an entry.
271  
272 [ $all->@[ 1 .. $#$all ] ]
273 }
274  
275 1
276 __END__