scanmut 0.2.0

Insert/remove multiple items from Vecs in O(n) time
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use alloc::vec::Vec;
use core as std;

/// An object that can remove multiple elements from a [Vec] in a single scan through the [Vec].
///
/// The [Remover] has a position in the [Vec] at which it inserts, called the index. Initially, the
/// index is at the **start** of the [Vec]. This index can only be moved further towards end of the
/// [Vec], meaning that the indices of the removed objects must be monotonically increasing
/// (i.e. the next index must be larger than the previous index).
///
/// Dropping the [Remover] without using the entire insertion capacity has a runtime cost
/// proportional to the size of the merged elements. Dropping after using the entire insertion
/// capacity has no runtime cost.
///
/// Leaking the [Remover] (through [std::mem::forget] or similar) may leak some items of the [Vec]
/// and will leave the [Vec] in an unspecified but valid state
///
/// # Example
///
/// ```
/// use scanmut::Remover;
///
/// let mut items = vec!['a', 'b', 'c'];
///
/// let mut remover = Remover::new(&mut items);
///
/// assert_eq!(remover.index(), 0); // Initial index is at start of the vec
/// assert_eq!(remover.remove(), 'a');
///
/// assert_eq!(remover.index(), 1);  // Removing an element increments the index
/// remover.move_to(2);
///
/// assert_eq!(remover.index(), 2);
/// assert_eq!(remover.remove(), 'c');
///
/// assert_eq!(remover.index(), 3);
///
/// drop(remover);
///
/// assert_eq!(items, ['b']);
/// ```
///
/// As calling [Remover::remove] will increment the index, it can be used to remove items in a row:
///
/// ```
/// use scanmut::Remover;
///
/// let mut items = vec!['a', 'b', 'c', 'd'];
///
/// let mut remover = Remover::new(&mut items);
///
/// remover.move_to(1);
/// assert_eq!(remover.remove(), 'b');
/// assert_eq!(remover.remove(), 'c');
/// drop(remover);
///
/// assert_eq!(items, ['a', 'd']);
/// ```
#[derive(Debug)]
pub struct Remover<'a, T> {
    // The remover works by keeping a "gap" of uninitialized values. This gap is initially
    // of length zero and at the start of the vec, and moves up through the vec as we remove elements,
    // increasing in size by one each time we remove an element.
    //
    // The fields have the following invariant:
    //     vec.len() <= unfiltered_start <= unfiltered_end <= vec.capacity()
    //
    // Where
    //     [0,vec.len()[                      are the filtered elements
    //     [vec.len(),unfiltered_start[       is a gap of uninitialized values
    //     [unfiltered_start,unfiltered_end[  are the unfiltered elements
    vec: &'a mut Vec<T>,
    unfiltered_start: usize,
    unfiltered_end: usize,
}

impl<'a, T> Remover<'a, T> {
    /// Create a new new `Remover` for removing elements from a `Vec`.
    #[inline]
    pub fn new(vec: &'a mut Vec<T>) -> Remover<'a, T> {
        let unfiltered_end = vec.len();

        unsafe {
            vec.set_len(0);
        }

        Remover {
            vec,
            unfiltered_start: 0,
            unfiltered_end,
        }
    }

    /// The current index of this remover.
    #[inline]
    pub fn index(&self) -> usize {
        self.unfiltered_start
    }

    /// Returns a reference to the item at the current index or `None` if the remover is past the
    /// end of the underlying `Vec`.
    #[inline]
    pub fn current(&self) -> Option<&T> {
        if self.unfiltered_start < self.unfiltered_end {
            unsafe { Some(&*self.vec.as_ptr().add(self.unfiltered_start)) }
        } else {
            None
        }
    }

    /// Returns a mutable reference to the item at the current index or `None` if the remover is
    /// past the end of the underlying `Vec`.
    #[inline]
    pub fn current_mut(&mut self) -> Option<&mut T> {
        if self.unfiltered_start < self.unfiltered_end {
            unsafe { Some(&mut *self.vec.as_mut_ptr().add(self.unfiltered_start)) }
        } else {
            None
        }
    }

    /// Returns a pair of slices representing the current state of the `Vec` being removed from.
    ///
    /// The first slice is the part of the `Vec` below the index. The second slice is the part of
    /// the `Vec` above or equal to the index.
    #[inline]
    pub fn as_slices(&self) -> (&[T], &[T]) {
        unsafe {
            (
                &self.vec[..],
                std::slice::from_raw_parts(
                    self.vec.as_ptr().add(self.unfiltered_start),
                    self.unfiltered_end - self.unfiltered_start,
                ),
            )
        }
    }

    /// Returns a pair of mutable slices representing the current state of the `Vec` being removed
    /// from.
    ///
    /// The first slice is the part of the `Vec` below the index. The second slice is the part of
    /// the `Vec` above or equal to the index.
    #[inline]
    pub fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
        unsafe {
            (
                std::slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.vec.len()),
                std::slice::from_raw_parts_mut(
                    self.vec.as_mut_ptr().add(self.unfiltered_start),
                    self.unfiltered_end - self.unfiltered_start,
                ),
            )
        }
    }

    /// Moves this `Remover` to the given index in the `Vec`.
    ///
    /// # Panics
    ///
    /// Panics if the index is less than or equal to the current index.
    #[inline]
    pub fn move_to(&mut self, index: usize) {
        assert!(index <= self.unfiltered_end, "Index out of bounds");

        let copy_len = index
            .checked_sub(self.unfiltered_start)
            .expect("Index must be larger than previous index");

        unsafe {
            let ptr = self.vec.as_mut_ptr();

            ptr.add(self.unfiltered_start)
                .copy_to(ptr.add(self.vec.len()), copy_len);

            self.vec.set_len(self.vec.len() + copy_len);
            self.unfiltered_start = index;
        }
    }

    /// Removes an element from the `Vec` at the current index.
    ///
    /// This also increments the index to the next position.
    ///
    /// # Panics
    ///
    /// Panics if the index is at the end of the vec.
    #[inline]
    pub fn remove(&mut self) -> T {
        assert!(
            self.unfiltered_start < self.unfiltered_end,
            "Removing out of bounds"
        );

        unsafe {
            let item = self.vec.as_mut_ptr().add(self.unfiltered_start).read();
            self.unfiltered_start += 1;
            item
        }
    }
}

impl<'a, T> Drop for Remover<'a, T> {
    #[inline]
    fn drop(&mut self) {
        let ptr = self.vec.as_mut_ptr();
        let unfiltered_len = self.unfiltered_end - self.unfiltered_start;

        unsafe {
            ptr.add(self.unfiltered_start)
                .copy_to(ptr.add(self.vec.len()), unfiltered_len);

            self.vec.set_len(self.vec.len() + unfiltered_len)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::Remover;
    use crate::proputils::prop_eq;
    use alloc::{format, vec};
    use proptest::prelude::*;
    use std::fmt::Debug;
    use std::vec::Vec;

    #[test]
    fn remover_empty() {
        let mut items: Vec<usize> = Vec::new();
        Remover::new(&mut items);
        assert_eq!(items, vec![]);
    }

    #[test]
    fn remover_single() {
        let mut items = vec![1];
        assert_eq!(Remover::new(&mut items).remove(), 1);
        assert_eq!(items, vec![]);
    }

    #[test]
    fn remover_two_first() {
        let mut items = vec![1, 2];
        assert_eq!(Remover::new(&mut items).remove(), 1);
        assert_eq!(items, vec![2]);
    }

    #[test]
    fn remover_two_second() {
        let mut items = vec![1, 2];
        let mut rem = Remover::new(&mut items);
        rem.move_to(1);
        assert_eq!(rem.remove(), 2);
        drop(rem);
        assert_eq!(items, vec![1]);
    }

    #[test]
    fn remover_two_both() {
        let mut items = vec![1, 2];
        let mut rem = Remover::new(&mut items);
        rem.move_to(0);
        assert_eq!(rem.remove(), 1);
        rem.move_to(1);
        assert_eq!(rem.remove(), 2);
        drop(rem);
        assert_eq!(items, vec![]);
    }

    #[test]
    #[should_panic]
    fn remover_two_out_of_order() {
        let mut items = vec![1, 2];
        let mut remover = Remover::new(&mut items);
        remover.move_to(1);
        assert_eq!(remover.remove(), 2);
        remover.move_to(0)
    }

    #[test]
    #[should_panic]
    fn remover_out_of_bounds() {
        drop(Remover::new(&mut vec![1, 2]).move_to(3));
    }

    #[test]
    #[should_panic]
    fn remover_advance_out_of_bounds() {
        Remover::new(&mut vec![1, 2]).move_to(4);
    }

    #[test]
    fn remover_advance_one_past() {
        let mut items = vec![1, 2];
        let mut rem = Remover::new(&mut items);
        rem.move_to(2);
        assert_eq!(rem.index(), 2);
    }

    #[test]
    fn remover_slices() {
        let mut items = vec![1, 2, 3, 4];
        let mut rem = Remover::new(&mut items);
        rem.move_to(2);
        assert_eq!(rem.remove(), 3);
        assert_eq!(rem.as_slices(), (&[1, 2][..], &[4][..]));
        assert_eq!(rem.as_mut_slices(), (&mut [1, 2][..], &mut [4][..]));
    }

    #[test]
    fn remover_index() {
        let mut items = vec![1, 2, 3, 4];
        let mut remover = Remover::new(&mut items);
        assert_eq!(remover.index(), 0);
        remover.move_to(1);
        assert_eq!(remover.remove(), 2);
        assert_eq!(remover.index(), 2);
        remover.move_to(3);
        assert_eq!(remover.index(), 3);
    }

    struct NaiveRemover<'a, T> {
        vec: &'a mut Vec<T>,
        old_index: usize,
        new_index: usize,
    }

    impl<'a, T> NaiveRemover<'a, T> {
        fn new(vec: &mut Vec<T>) -> NaiveRemover<T> {
            NaiveRemover {
                vec,
                old_index: 0,
                new_index: 0,
            }
        }

        fn index(&self) -> usize {
            self.old_index
        }

        fn current(&self) -> Option<&T> {
            self.vec.get(self.new_index)
        }

        fn remove(&mut self) -> T {
            let item = self.vec.remove(self.new_index);
            self.old_index += 1;
            item
        }

        fn move_to(&mut self, index: usize) {
            self.new_index += index.checked_sub(self.old_index).unwrap();
            self.old_index = index;
        }

        fn as_mut_slices(&mut self) -> (&mut [T], &mut [T]) {
            self.vec.split_at_mut(self.new_index)
        }

        fn as_slices(&self) -> (&[T], &[T]) {
            self.vec.split_at(self.new_index)
        }
    }

    #[derive(Debug, Clone)]
    enum Op {
        MoveTo(usize),
        Remove,
    }

    fn prop_strategy() -> impl Strategy<Value = (Vec<u8>, Vec<Op>)> {
        proptest::collection::vec(any::<u8>(), 0..100).prop_flat_map(|base| {
            proptest::collection::vec(
                prop_oneof![(0..base.len() + 1).prop_map(Op::MoveTo), Just(Op::Remove)],
                0..100,
            )
            .prop_map(move |ops| (base.clone(), ops))
        })
    }

    fn check_equals_naive_remover<T: Eq + Clone + Debug>(
        base: Vec<T>,
        ops: &[Op],
    ) -> Result<(), TestCaseError> {
        let mut model_vec = base.clone();
        let mut model = NaiveRemover::new(&mut model_vec);
        let mut tested_vec = base;
        let mut tested = Remover::new(&mut tested_vec);

        ops.iter().try_for_each(|op| {
            match op {
                &Op::MoveTo(index) => {
                    prop_eq(|| model.move_to(index), || tested.move_to(index))
                }
                Op::Remove => prop_eq(|| model.remove(), || tested.remove()),
            }?;

            prop_assert_eq!(model.current(), tested.current());
            prop_assert_eq!(model.index(), tested.index());
            prop_assert_eq!(model.as_slices(), tested.as_slices());
            prop_assert_eq!(model.as_mut_slices(), tested.as_mut_slices());

            Ok(())
        })?;

        drop(tested);
        prop_assert_eq!(model_vec, tested_vec);

        Ok(())
    }

    proptest! {
        #[test]
        fn equals_naive_remover((base, ops) in prop_strategy()) {
            check_equals_naive_remover(base, &ops[..])?
        }
    }
}