@@ -16,13 +16,6 @@ pub struct RangeMap<T> {
16
16
map : BTreeMap < Range , T > ,
17
17
}
18
18
19
- impl < T > Default for RangeMap < T > {
20
- #[ inline( always) ]
21
- fn default ( ) -> Self {
22
- RangeMap :: new ( )
23
- }
24
- }
25
-
26
19
// The derived `Ord` impl sorts first by the first field, then, if the fields are the same,
27
20
// by the second field.
28
21
// This is exactly what we need for our purposes, since a range query on a BTReeSet/BTreeMap will give us all
@@ -73,9 +66,15 @@ impl Range {
73
66
}
74
67
75
68
impl < T > RangeMap < T > {
69
+ /// Create a new RangeMap for the given size, and with the given initial value used for
70
+ /// the entire range.
76
71
#[ inline( always) ]
77
- pub fn new ( ) -> RangeMap < T > {
78
- RangeMap { map : BTreeMap :: new ( ) }
72
+ pub fn new ( size : Size , init : T ) -> RangeMap < T > {
73
+ let mut map = RangeMap { map : BTreeMap :: new ( ) } ;
74
+ if size. bytes ( ) > 0 {
75
+ map. map . insert ( Range { start : 0 , end : size. bytes ( ) } , init) ;
76
+ }
77
+ map
79
78
}
80
79
81
80
fn iter_with_range < ' a > (
@@ -95,6 +94,9 @@ impl<T> RangeMap<T> {
95
94
)
96
95
}
97
96
97
+ /// Provide read-only iteration over everything in the given range. This does
98
+ /// *not* split items if they overlap with the edges. Do not use this to mutate
99
+ /// through interior mutability.
98
100
pub fn iter < ' a > ( & ' a self , offset : Size , len : Size ) -> impl Iterator < Item = & ' a T > + ' a {
99
101
self . iter_with_range ( offset. bytes ( ) , len. bytes ( ) ) . map ( |( _, data) | data)
100
102
}
@@ -140,8 +142,7 @@ impl<T> RangeMap<T> {
140
142
/// Provide mutable iteration over everything in the given range. As a side-effect,
141
143
/// this will split entries in the map that are only partially hit by the given range,
142
144
/// to make sure that when they are mutated, the effect is constrained to the given range.
143
- /// If there are gaps, leave them be.
144
- pub fn iter_mut_with_gaps < ' a > (
145
+ pub fn iter_mut < ' a > (
145
146
& ' a mut self ,
146
147
offset : Size ,
147
148
len : Size ,
@@ -174,93 +175,34 @@ impl<T> RangeMap<T> {
174
175
} ,
175
176
)
176
177
}
177
-
178
- /// Provide a mutable iterator over everything in the given range, with the same side-effects as
179
- /// iter_mut_with_gaps. Furthermore, if there are gaps between ranges, fill them with the given default
180
- /// before yielding them in the iterator.
181
- /// This is also how you insert.
182
- pub fn iter_mut < ' a > ( & ' a mut self , offset : Size , len : Size ) -> impl Iterator < Item = & ' a mut T > + ' a
183
- where
184
- T : Clone + Default ,
185
- {
186
- if len. bytes ( ) > 0 {
187
- let offset = offset. bytes ( ) ;
188
- let len = len. bytes ( ) ;
189
-
190
- // Do a first iteration to collect the gaps
191
- let mut gaps = Vec :: new ( ) ;
192
- let mut last_end = offset;
193
- for ( range, _) in self . iter_with_range ( offset, len) {
194
- if last_end < range. start {
195
- gaps. push ( Range {
196
- start : last_end,
197
- end : range. start ,
198
- } ) ;
199
- }
200
- last_end = range. end ;
201
- }
202
- if last_end < offset + len {
203
- gaps. push ( Range {
204
- start : last_end,
205
- end : offset + len,
206
- } ) ;
207
- }
208
-
209
- // Add default for all gaps
210
- for gap in gaps {
211
- let old = self . map . insert ( gap, Default :: default ( ) ) ;
212
- assert ! ( old. is_none( ) ) ;
213
- }
214
- }
215
-
216
- // Now provide mutable iteration
217
- self . iter_mut_with_gaps ( offset, len)
218
- }
219
-
220
- pub fn retain < F > ( & mut self , mut f : F )
221
- where
222
- F : FnMut ( & T ) -> bool ,
223
- {
224
- let mut remove = Vec :: new ( ) ;
225
- for ( range, data) in & self . map {
226
- if !f ( data) {
227
- remove. push ( * range) ;
228
- }
229
- }
230
-
231
- for range in remove {
232
- self . map . remove ( & range) ;
233
- }
234
- }
235
178
}
236
179
237
180
#[ cfg( test) ]
238
181
mod tests {
239
182
use super :: * ;
240
183
241
184
/// Query the map at every offset in the range and collect the results.
242
- fn to_vec < T : Copy > ( map : & RangeMap < T > , offset : u64 , len : u64 , default : Option < T > ) -> Vec < T > {
185
+ fn to_vec < T : Copy > ( map : & RangeMap < T > , offset : u64 , len : u64 ) -> Vec < T > {
243
186
( offset..offset + len)
244
187
. into_iter ( )
245
188
. map ( |i| map
246
189
. iter ( Size :: from_bytes ( i) , Size :: from_bytes ( 1 ) )
247
190
. next ( )
248
191
. map ( |& t| t)
249
- . or ( default)
250
192
. unwrap ( )
251
193
)
252
194
. collect ( )
253
195
}
254
196
255
197
#[ test]
256
198
fn basic_insert ( ) {
257
- let mut map = RangeMap :: < i32 > :: new ( ) ;
199
+ let mut map = RangeMap :: < i32 > :: new ( Size :: from_bytes ( 20 ) , - 1 ) ;
258
200
// Insert
259
201
for x in map. iter_mut ( Size :: from_bytes ( 10 ) , Size :: from_bytes ( 1 ) ) {
260
202
* x = 42 ;
261
203
}
262
204
// Check
263
- assert_eq ! ( to_vec( & map, 10 , 1 , None ) , vec![ 42 ] ) ;
205
+ assert_eq ! ( to_vec( & map, 10 , 1 ) , vec![ 42 ] ) ;
264
206
265
207
// Insert with size 0
266
208
for x in map. iter_mut ( Size :: from_bytes ( 10 ) , Size :: from_bytes ( 0 ) ) {
@@ -269,34 +211,42 @@ mod tests {
269
211
for x in map. iter_mut ( Size :: from_bytes ( 11 ) , Size :: from_bytes ( 0 ) ) {
270
212
* x = 19 ;
271
213
}
272
- assert_eq ! ( to_vec( & map, 10 , 2 , Some ( - 1 ) ) , vec![ 42 , -1 ] ) ;
214
+ assert_eq ! ( to_vec( & map, 10 , 2 ) , vec![ 42 , -1 ] ) ;
273
215
}
274
216
275
217
#[ test]
276
218
fn gaps ( ) {
277
- let mut map = RangeMap :: < i32 > :: new ( ) ;
219
+ let mut map = RangeMap :: < i32 > :: new ( Size :: from_bytes ( 20 ) , - 1 ) ;
278
220
for x in map. iter_mut ( Size :: from_bytes ( 11 ) , Size :: from_bytes ( 1 ) ) {
279
221
* x = 42 ;
280
222
}
281
223
for x in map. iter_mut ( Size :: from_bytes ( 15 ) , Size :: from_bytes ( 1 ) ) {
282
224
* x = 43 ;
283
225
}
284
226
assert_eq ! (
285
- to_vec( & map, 10 , 10 , Some ( - 1 ) ) ,
227
+ to_vec( & map, 10 , 10 ) ,
286
228
vec![ -1 , 42 , -1 , -1 , -1 , 43 , -1 , -1 , -1 , -1 ]
287
229
) ;
288
230
289
- // Now request a range that needs three gaps filled
290
231
for x in map. iter_mut ( Size :: from_bytes ( 10 ) , Size :: from_bytes ( 10 ) ) {
291
232
if * x < 42 {
292
233
* x = 23 ;
293
234
}
294
235
}
295
236
296
237
assert_eq ! (
297
- to_vec( & map, 10 , 10 , None ) ,
238
+ to_vec( & map, 10 , 10 ) ,
298
239
vec![ 23 , 42 , 23 , 23 , 23 , 43 , 23 , 23 , 23 , 23 ]
299
240
) ;
300
- assert_eq ! ( to_vec( & map, 13 , 5 , None ) , vec![ 23 , 23 , 43 , 23 , 23 ] ) ;
241
+ assert_eq ! ( to_vec( & map, 13 , 5 ) , vec![ 23 , 23 , 43 , 23 , 23 ] ) ;
242
+
243
+ // Now request a range that goes beyond the initial size
244
+ for x in map. iter_mut ( Size :: from_bytes ( 15 ) , Size :: from_bytes ( 10 ) ) {
245
+ * x = 19 ;
246
+ }
247
+ assert_eq ! ( map. iter( Size :: from_bytes( 19 ) , Size :: from_bytes( 1 ) )
248
+ . map( |& t| t) . collect:: <Vec <_>>( ) , vec![ 19 ] ) ;
249
+ assert_eq ! ( map. iter( Size :: from_bytes( 20 ) , Size :: from_bytes( 1 ) )
250
+ . map( |& t| t) . collect:: <Vec <_>>( ) , vec![ ] ) ;
301
251
}
302
252
}
0 commit comments