Example/Ordered/Buckets.pm untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 45 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 184 CC 7 SCAR 16.9
CC
7
Cov
0%
put
12.0
_locate
6.0
_lower
6.0
CRAP 5.4
1 ########################################################################
2 # housekeeping
3 ########################################################################
4 package LinkedList::Single::Example::Ordered::Buckets v2.0.0;
5 use v5.40;
6  
7 use Carp qw( croak );
8  
9 ########################################################################
10 # the array programmer's version of the same two-level design: an array
11 # of buckets, each a small sorted array of [ key, value ] pairs, with
12 # the bucket's low key beside it. binary search over the lows finds the
13 # bucket, binary search inside it finds the pair, a splice moves at
14 # most 2 * bucket pairs. a bucket at twice its size is split in two; an
15 # empty one is removed.
16 ########################################################################
17  
18 sub new( $class, %arg )
19 {
20 # the default is the fastest found for lists of up to 100,000 entries
21 # (see the POD); LLS_BUCKET_SIZE overrides it.
22  
23 my $bucket
24 = $arg{ bucket }
25 // $ENV{ LLS_BUCKET_SIZE }
26 // 3
27 ;
28  
29 $bucket > 0
30 or croak "Botched new: bucket '$bucket' is not positive";
31  
32 bless
33 {
34 bucket => $bucket
35 , index => [ { low => undef, item => [] } ]
36 }
37 => $class
38 }
39  
40 # the position of the bucket that holds, or would hold, a key.
41  
42 sub _locate( $self, $key )
43 {
44 my $index = $self->{ index };
45 my( $lo, $hi ) = ( 0, $#$index );
46  
47 while( $lo < $hi )
48 {
49 my $mid = ( $lo + $hi + 1 ) >> 1;
50  
51 if( $index->[ $mid ]{ low } <= $key )
52 {
53 $lo = $mid;
54 }
55 else
56 {
57 $hi = $mid - 1;
58 }
59 }
60  
61 $lo
62 }
63  
64 # the position in a bucket of the first pair not below $key.
65  
66 sub _lower( $item, $key )
67 {
68 my( $lo, $hi ) = ( 0, scalar @$item );
69  
70 while( $lo < $hi )
71 {
72 my $mid = ( $lo + $hi ) >> 1;
73  
74 if( $item->[ $mid ][ 0 ] < $key )
75 {
76 $lo = $mid + 1;
77 }
78 else
79 {
80 $hi = $mid;
81 }
82 }
83  
84 $lo
85 }
86  
87 sub put( $self, $key, $value )
88 {
89 my $at = $self->_locate( $key );
90 my $item = $self->{ index }[ $at ]{ item };
91 my $pos = _lower $item, $key;
92  
93 if( $pos < @$item && $item->[ $pos ][ 0 ] == $key )
94 {
95 $item->[ $pos ][ 1 ] = $value;
96 }
97 else
98 {
99 splice $item->@*, $pos, 0, [ $key, $value ];
100  
101 if( @$item > 2 * $self->{ bucket } )
102 {
103 my @second = splice $item->@*, $self->{ bucket };
104  
105 splice $self->{ index }->@*, $at + 1, 0
106 , { low => $second[ 0 ][ 0 ], item => \@second };
107 }
108 }
109  
110 $self
111 }
112  
113 sub get( $self, $key )
114 {
115 my $item = $self->{ index }[ $self->_locate( $key ) ]{ item };
116 my $pos = _lower $item, $key;
117  
118 $pos < @$item && $item->[ $pos ][ 0 ] == $key
119 ? $item->[ $pos ][ 1 ]
120 : undef
121 }
122  
123 sub delete( $self, $key )
124 {
125 my $at = $self->_locate( $key );
126 my $item = $self->{ index }[ $at ]{ item };
127 my $pos = _lower $item, $key;
128  
129 $pos < @$item && $item->[ $pos ][ 0 ] == $key
130 or return undef;
131  
132 my $value = ( splice $item->@*, $pos, 1 )[ 0 ][ 1 ];
133  
134 @$item
135 or $at
136 and splice $self->{ index }->@*, $at, 1;
137  
138 $value
139 }
140  
141 sub range( $self, $lo, $hi )
142 {
143 my $index = $self->{ index };
144 my $at = $self->_locate( $lo );
145 my @found = ();
146  
147 for my $position ( $at .. $#$index )
148 {
149 my $bucket = $index->[ $position ];
150  
151 defined $bucket->{ low }
152 and $bucket->{ low } > $hi
153 and last;
154  
155 for my $pair ( $bucket->{ item }->@* )
156 {
157 $pair->[ 0 ] > $hi
158 and return \@found;
159  
160 $pair->[ 0 ] >= $lo
161 and push @found, [ $pair->@* ];
162 }
163 }
164  
165 \@found
166 }
167  
168 sub cut( $self, $lo, $hi )
169 {
170 my $index = $self->{ index };
171 my $at = $self->_locate( $lo );
172 my @found = ();
173  
174 while( $at <= $#$index )
175 {
176 my $bucket = $index->[ $at ];
177  
178 defined $bucket->{ low }
179 and $bucket->{ low } > $hi
180 and last;
181  
182 my $item = $bucket->{ item };
183 my $from = _lower $item, $lo;
184 my $to = $from;
185  
186 ++$to while $to < @$item && $item->[ $to ][ 0 ] <= $hi;
187  
188 push @found, splice $item->@*, $from, $to - $from;
189  
190 if( @$item || ! $at )
191 {
192 ++$at;
193 }
194 else
195 {
196 splice $index->@*, $at, 1;
197 }
198 }
199  
200 \@found
201 }
202  
203 sub size( $self )
204 {
205 my $size = 0;
206  
207 $size += @{ $_->{ item } } for $self->{ index }->@*;
208  
209 $size
210 }
211  
212 sub contents( $self )
213 {
214 [ map { [ $_->@* ] } map { $_->{ item }->@* } $self->{ index }->@* ]
215 }
216  
217 1
218 __END__