Example/Ordered.pm untested

filter: Statementstmt 0.0% 0 / 117 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 243 CC 7 SCAR 15.0
CC
7
Cov
0%
_cut_bucket
12.0
_locate
6.0
put
6.0
CRAP 4.5
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Ordered v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 use LinkedList::Single qw( v2 );
10  
11 ########################################################################
12 # an ordered map from numeric keys to values: a linked list of entries
13 # in key order, divided into buckets by permanent marker nodes, with a
14 # small array that indexes the markers.
15 #
16 # index[0] index[1] index[2]
17 # | | |
18 # marker -> e -> e -> marker -> e -> e -> e -> marker -> e -> sentinel
19 #
20 # the nodes are [ $next, $key, $value ] for an entry and [ $next, 'bucket' ]
21 # for a marker. each index element is
22 #
23 # low the smallest key the bucket may hold. every key in the bucket
24 # is >= low and < the next bucket's low. the first bucket has
25 # no low: it takes every key below the second's.
26 # mark a cursor on the bucket's marker node.
27 # count how many entries the bucket holds.
28 #
29 # a lookup is two levels: a binary search of the index finds the bucket,
30 # then a walk from the marker finds the entry. the walk is bounded by
31 # the bucket size (at most 2 * bucket entries), not by the size of the
32 # map.
33 #
34 # why markers: a singly linked list can remove or insert only from the
35 # predecessor, and in the LRU cache (example 01) the index had to hold a
36 # cursor on the node before every entry, and repair it whenever a
37 # neighbour went. here the index points only at markers, and a marker is
38 # never moved or removed while its bucket has entries, so nothing in the
39 # index ever needs repair except the counts. the walk from the marker
40 # supplies the predecessor for each insert and delete.
41 #
42 # an entry is added after its predecessor with 'append' (a pointer
43 # change, nothing else moves) and removed with 'drop'. a bucket that
44 # reaches twice its size is split in two by inserting a new marker; an
45 # empty bucket's marker is dropped. the index is an array, but it holds
46 # one element per bucket, so splicing it costs 1/bucket of the work an
47 # array of all the entries would.
48 #
49 # in-order traversal is a walk of the list. no recursion, no stack, no
50 # rebalancing.
51 ########################################################################
52  
53 sub new( $class, %arg )
54 {
55 # the default is the fastest found for lists of up to 100,000 entries
56 # (see the POD); LLS_BUCKET_SIZE overrides it.
57  
58 my $bucket
59 = $arg{ bucket }
60 // $ENV{ LLS_BUCKET_SIZE }
61 // 3
62 ;
63  
64 $bucket > 0
65 or croak "Botched new: bucket '$bucket' is not positive";
66  
67 my $list = LinkedList::Single->new;
68  
69 # an empty list: push fills the sentinel in place.
70  
71 $list->push( 'bucket' );
72  
73 bless
74 {
75 bucket => $bucket
76 , list => $list
77 , index =>
78 [
79 {
80 low => undef
81 , mark => $list->cursor
82 , count => 0
83 }
84 ]
85 , size => 0
86 }
87 => $class
88 }
89  
90 ########################################################################
91 # utility
92 ########################################################################
93  
94 # the position in the index of the bucket that holds, or would hold, a
95 # key: the last one whose low is not above it.
96  
97 sub _locate( $self, $key )
98 {
99 my $index = $self->{ index };
100 my( $lo, $hi ) = ( 0, $#$index );
101  
102 while( $lo < $hi )
103 {
104 my $mid = ( $lo + $hi + 1 ) >> 1;
105  
106 if( $index->[ $mid ]{ low } <= $key )
107 {
108 $lo = $mid;
109 }
110 else
111 {
112 $hi = $mid - 1;
113 }
114 }
115  
116 $lo
117 }
118  
119 # a cursor on the node before the first entry of bucket $at whose key is
120 # not below $key (the marker if there is none before it), and whether
121 # that entry's key is $key.
122 #
123 # a new cursor is made for the walk: the marker's own is never moved.
124  
125 sub _before( $self, $at, $key )
126 {
127 my $entry = $self->{ index }[ $at ];
128 my $curs = $entry->{ mark }->next( 0 );
129  
130 for( 1 .. $entry->{ count } )
131 {
132 my( $found ) = $curs->next_data;
133  
134 $found >= $key
135 and return( $curs, $found == $key );
136  
137 $curs->advance;
138 }
139  
140 ( $curs, 0 )
141 }
142  
143 # a bucket has outgrown its size: the second half becomes a bucket of its
144 # own, behind a new marker.
145  
146 sub _split( $self, $at )
147 {
148 my $entry = $self->{ index }[ $at ];
149 my $keep = $self->{ bucket };
150  
151 # the last entry that stays.
152  
153 my $last = $entry->{ mark }->next( $keep );
154  
155 $last->append( 'bucket' );
156  
157 my $mark = $last->next;
158  
159 splice $self->{ index }->@*, $at + 1, 0
160 , {
161 low => ( $mark->next_data )[ 0 ]
162 , mark => $mark
163 , count => $entry->{ count } - $keep
164 };
165  
166 $entry->{ count } = $keep;
167  
168 $self
169 }
170  
171 # drop the marker of bucket $at, which has no entries left. the node
172 # before a marker is the last node of the bucket before it.
173  
174 sub _remove_bucket( $self, $at )
175 {
176 my $index = $self->{ index };
177 my $before = $index->[ $at - 1 ];
178  
179 $before->{ mark }->next( $before->{ count } )->drop;
180  
181 splice $index->@*, $at, 1;
182  
183 $self
184 }
185  
186 ########################################################################
187 # public interface
188 ########################################################################
189  
190 # add an entry, or replace the value of an existing one.
191  
192 sub put( $self, $key, $value )
193 {
194 my $at = $self->_locate( $key );
195 my( $curs, $found ) = $self->_before( $at, $key );
196  
197 if( $found )
198 {
199 $curs->next->set_data( $key, $value );
200 }
201 else
202 {
203 $curs->append( $key, $value );
204  
205 ++$self->{ size };
206  
207 my $entry = $self->{ index }[ $at ];
208  
209 ++$entry->{ count } > 2 * $self->{ bucket }
210 and $self->_split( $at );
211 }
212  
213 $self
214 }
215  
216 # the value for a key, or undef.
217  
218 sub get( $self, $key )
219 {
220 my $at = $self->_locate( $key );
221 my( $curs, $found ) = $self->_before( $at, $key );
222  
223 $found
224 ? ( $curs->next_data )[ 1 ]
225 : undef
226 }
227  
228 # remove an entry; returns its value, or undef if there was none.
229  
230 sub delete( $self, $key )
231 {
232 my $at = $self->_locate( $key );
233 my( $curs, $found ) = $self->_before( $at, $key );
234  
235 $found
236 or return undef;
237  
238 my( undef, $value ) = $curs->drop;
239  
240 --$self->{ size };
241  
242 my $entry = $self->{ index }[ $at ];
243  
244 --$entry->{ count }
245 or $at
246 and $self->_remove_bucket( $at );
247  
248 $value
249 }
250  
251 # the entries with lo <= key <= hi, in order, as [ key, value ] pairs.
252  
253 sub range( $self, $lo, $hi )
254 {
255 my $at = $self->_locate( $lo );
256 my $curs = $self->{ index }[ $at ]{ mark }->next( 0 );
257 my @found = ();
258  
259 while( $curs->advance )
260 {
261 my @data = $curs->data;
262  
263 # a marker holds one datum, an entry two.
264  
265 @data > 1
266 or next;
267  
268 $data[ 0 ] > $hi
269 and last;
270  
271 $data[ 0 ] >= $lo
272 and push @found, \@data;
273 }
274  
275 \@found
276 }
277  
278 # remove and return the entries with lo <= key <= hi.
279 #
280 # buckets that lie wholly inside the range, markers and all, come out as
281 # one detached piece, without a look at the keys in them. the buckets at
282 # the two ends are cut entry by entry.
283  
284 sub cut( $self, $lo, $hi )
285 {
286 my $index = $self->{ index };
287 my @found = ();
288 my $at = $self->_locate( $lo );
289  
290 while( $at <= $#$index )
291 {
292 my $entry = $index->[ $at ];
293  
294 # nothing at or after a bucket whose low is above the range
295 # is in it.
296  
297 defined $entry->{ low }
298 and $entry->{ low } > $hi
299 and last;
300  
301 if( $self->_inside( $at, $lo, $hi ) )
302 {
303 # take this bucket and every whole bucket after it.
304  
305 my $stop = $at;
306  
307 ++$stop while $self->_inside( $stop + 1, $lo, $hi );
308  
309 push @found, $self->_detach( $at, $stop )->@*;
310 }
311 else
312 {
313 push @found, $self->_cut_bucket( $at, $lo, $hi )->@*;
314  
315 # an emptied bucket was removed, and the next one is here.
316  
317 $at <= $#$index
318 and $index->[ $at ] == $entry
319 and ++$at;
320 }
321 }
322  
323 \@found
324 }
325  
326 # is every key that bucket $at can hold within the range? the keys are
327 # all below the next bucket's low, so the last bucket never is.
328  
329 sub _inside( $self, $at, $lo, $hi )
330 {
331 my $index = $self->{ index };
332  
333 $at > 0
334 and $at < $#$index
335 and $index->[ $at ]{ low } >= $lo
336 and $index->[ $at + 1 ]{ low } <= $hi
337 }
338  
339 # detach buckets $first .. $last whole, and return their entries.
340  
341 sub _detach( $self, $first, $last )
342 {
343 my $index = $self->{ index };
344 my $before = $index->[ $first - 1 ];
345 my $nodes = 0;
346  
347 $nodes += 1 + $index->[ $_ ]{ count } for $first .. $last;
348  
349 my $piece = $before->{ mark }->next( $before->{ count } )->detach( $nodes );
350  
351 my @entry
352 = grep { @$_ > 1 }
353 $piece->extract->@*;
354  
355 $self->{ size } -= @entry;
356  
357 splice $index->@*, $first, $last - $first + 1;
358  
359 \@entry
360 }
361  
362 # cut the entries in range out of bucket $at, one by one.
363  
364 sub _cut_bucket( $self, $at, $lo, $hi )
365 {
366 my $entry = $self->{ index }[ $at ];
367 my $curs = $entry->{ mark }->next( 0 );
368 my @found = ();
369  
370 for( 1 .. $entry->{ count } )
371 {
372 my( $key ) = $curs->next_data;
373  
374 if( $key < $lo )
375 {
376 $curs->advance;
377 }
378 elsif( $key <= $hi )
379 {
380 push @found, [ $curs->drop ];
381 }
382 else
383 {
384 last;
385 }
386 }
387  
388 $entry->{ count } -= @found;
389 $self->{ size } -= @found;
390  
391 $entry->{ count }
392 or $at
393 and $self->_remove_bucket( $at );
394  
395 \@found
396 }
397  
398 sub size( $self )
399 {
400 $self->{ size }
401 }
402  
403 sub buckets( $self )
404 {
405 scalar $self->{ index }->@*
406 }
407  
408 # arrayref of [ key, value ] in key order.
409  
410 sub contents( $self )
411 {
412 [ grep { @$_ > 1 } $self->{ list }->extract->@* ]
413 }
414  
415 1
416 __END__