| 1 | use core::ops::{Range, RangeBounds}; |
| 2 | use core::{ptr, slice}; |
| 3 | |
| 4 | use super::Vec; |
| 5 | use crate::alloc::{Allocator, Global}; |
| 6 | |
| 7 | /// An iterator which uses a closure to determine if an element should be removed. |
| 8 | /// |
| 9 | /// This struct is created by [`Vec::extract_if`]. |
| 10 | /// See its documentation for more. |
| 11 | /// |
| 12 | /// # Example |
| 13 | /// |
| 14 | /// ``` |
| 15 | /// let mut v = vec![0, 1, 2]; |
| 16 | /// let iter: std::vec::ExtractIf<'_, _, _> = v.extract_if(.., |x| *x % 2 == 0); |
| 17 | /// ``` |
| 18 | #[stable (feature = "extract_if" , since = "1.87.0" )] |
| 19 | #[derive (Debug)] |
| 20 | #[must_use = "iterators are lazy and do nothing unless consumed" ] |
| 21 | pub struct ExtractIf< |
| 22 | 'a, |
| 23 | T, |
| 24 | F, |
| 25 | #[unstable (feature = "allocator_api" , issue = "32838" )] A: Allocator = Global, |
| 26 | > { |
| 27 | vec: &'a mut Vec<T, A>, |
| 28 | /// The index of the item that will be inspected by the next call to `next`. |
| 29 | idx: usize, |
| 30 | /// Elements at and beyond this point will be retained. Must be equal or smaller than `old_len`. |
| 31 | end: usize, |
| 32 | /// The number of items that have been drained (removed) thus far. |
| 33 | del: usize, |
| 34 | /// The original length of `vec` prior to draining. |
| 35 | old_len: usize, |
| 36 | /// The filter test predicate. |
| 37 | pred: F, |
| 38 | } |
| 39 | |
| 40 | impl<'a, T, F, A: Allocator> ExtractIf<'a, T, F, A> { |
| 41 | pub(super) fn new<R: RangeBounds<usize>>(vec: &'a mut Vec<T, A>, pred: F, range: R) -> Self { |
| 42 | let old_len: usize = vec.len(); |
| 43 | let Range { start: usize, end: usize } = slice::range(range, ..old_len); |
| 44 | |
| 45 | // Guard against the vec getting leaked (leak amplification) |
| 46 | unsafe { |
| 47 | vec.set_len(new_len:0); |
| 48 | } |
| 49 | ExtractIf { vec, idx: start, del: 0, end, old_len, pred } |
| 50 | } |
| 51 | |
| 52 | /// Returns a reference to the underlying allocator. |
| 53 | #[unstable (feature = "allocator_api" , issue = "32838" )] |
| 54 | #[inline ] |
| 55 | pub fn allocator(&self) -> &A { |
| 56 | self.vec.allocator() |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | #[stable (feature = "extract_if" , since = "1.87.0" )] |
| 61 | impl<T, F, A: Allocator> Iterator for ExtractIf<'_, T, F, A> |
| 62 | where |
| 63 | F: FnMut(&mut T) -> bool, |
| 64 | { |
| 65 | type Item = T; |
| 66 | |
| 67 | fn next(&mut self) -> Option<T> { |
| 68 | unsafe { |
| 69 | while self.idx < self.end { |
| 70 | let i = self.idx; |
| 71 | let v = slice::from_raw_parts_mut(self.vec.as_mut_ptr(), self.old_len); |
| 72 | let drained = (self.pred)(&mut v[i]); |
| 73 | // Update the index *after* the predicate is called. If the index |
| 74 | // is updated prior and the predicate panics, the element at this |
| 75 | // index would be leaked. |
| 76 | self.idx += 1; |
| 77 | if drained { |
| 78 | self.del += 1; |
| 79 | return Some(ptr::read(&v[i])); |
| 80 | } else if self.del > 0 { |
| 81 | let del = self.del; |
| 82 | let src: *const T = &v[i]; |
| 83 | let dst: *mut T = &mut v[i - del]; |
| 84 | ptr::copy_nonoverlapping(src, dst, 1); |
| 85 | } |
| 86 | } |
| 87 | None |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 92 | (0, Some(self.end - self.idx)) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | #[stable (feature = "extract_if" , since = "1.87.0" )] |
| 97 | impl<T, F, A: Allocator> Drop for ExtractIf<'_, T, F, A> { |
| 98 | fn drop(&mut self) { |
| 99 | unsafe { |
| 100 | if self.idx < self.old_len && self.del > 0 { |
| 101 | let ptr: *mut T = self.vec.as_mut_ptr(); |
| 102 | let src: *mut T = ptr.add(self.idx); |
| 103 | let dst: *mut T = src.sub(self.del); |
| 104 | let tail_len: usize = self.old_len - self.idx; |
| 105 | src.copy_to(dest:dst, count:tail_len); |
| 106 | } |
| 107 | self.vec.set_len(self.old_len - self.del); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |