| 1 | //! A data structure for in place modification of vecs. |
| 2 | #![deny(missing_docs)] |
| 3 | use core::fmt::Debug; |
| 4 | |
| 5 | /// builds a SmallVec out of itself |
| 6 | pub struct InPlaceVecBuilder<'a, T> { |
| 7 | /// the underlying vector, possibly containing some uninitialized values in the middle! |
| 8 | v: &'a mut Vec<T>, |
| 9 | /// the end of the target area |
| 10 | t1: usize, |
| 11 | /// the start of the source area |
| 12 | s0: usize, |
| 13 | } |
| 14 | |
| 15 | impl<'a, T: Debug> Debug for InPlaceVecBuilder<'a, T> { |
| 16 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 17 | let InPlaceVecBuilder { s0, t1, v } = self; |
| 18 | let s1 = v.len(); |
| 19 | let cap = v.capacity(); |
| 20 | write!( |
| 21 | f, |
| 22 | "InPlaceSmallVecBuilder(0..{},{}..{},{})", |
| 23 | t1, s0, s1, cap |
| 24 | ) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | /// initializes the source part of this flip buffer with the given vector. |
| 29 | /// The target part is initially empty. |
| 30 | impl<'a, T> From<&'a mut Vec<T>> for InPlaceVecBuilder<'a, T> { |
| 31 | fn from(value: &'a mut Vec<T>) -> Self { |
| 32 | InPlaceVecBuilder { |
| 33 | v: value, |
| 34 | s0: 0, |
| 35 | t1: 0, |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl<'a, T> InPlaceVecBuilder<'a, T> { |
| 41 | /// The current target part as a slice |
| 42 | pub fn target_slice(&self) -> &[T] { |
| 43 | &self.v[..self.t1] |
| 44 | } |
| 45 | |
| 46 | /// The current source part as a slice |
| 47 | pub fn source_slice(&self) -> &[T] { |
| 48 | &self.v[self.s0..] |
| 49 | } |
| 50 | |
| 51 | /// The current source part as a slice |
| 52 | pub fn source_slice_mut(&mut self) -> &mut [T] { |
| 53 | &mut self.v[self.s0..] |
| 54 | } |
| 55 | |
| 56 | /// ensure that we have at least `capacity` space. |
| 57 | #[inline] |
| 58 | fn reserve(&mut self, capacity: usize) { |
| 59 | // ensure we have space! |
| 60 | if self.t1 + capacity > self.s0 { |
| 61 | let v = &mut self.v; |
| 62 | let s0 = self.s0; |
| 63 | let s1 = v.len(); |
| 64 | let sn = s1 - s0; |
| 65 | // delegate to the underlying vec for the grow logic |
| 66 | v.reserve(capacity); |
| 67 | // move the source to the end of the vec |
| 68 | let cap = v.capacity(); |
| 69 | // just move source to the end without any concern about dropping |
| 70 | unsafe { |
| 71 | copy(v.as_mut_ptr(), s0, cap - sn, sn); |
| 72 | v.set_len(cap); |
| 73 | } |
| 74 | // move s0 |
| 75 | self.s0 = cap - sn; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Take at most `n` elements from `iter` to the target |
| 80 | #[inline] |
| 81 | pub fn extend_from_iter<I: Iterator<Item = T>>(&mut self, mut iter: I, n: usize) { |
| 82 | if n > 0 { |
| 83 | self.reserve(n); |
| 84 | for _ in 0..n { |
| 85 | if let Some(value) = iter.next() { |
| 86 | self.push_unsafe(value) |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// Push a single value to the target |
| 93 | pub fn push(&mut self, value: T) { |
| 94 | // ensure we have space! |
| 95 | self.reserve(1); |
| 96 | self.push_unsafe(value); |
| 97 | } |
| 98 | |
| 99 | fn push_unsafe(&mut self, value: T) { |
| 100 | unsafe { std::ptr::write(self.v.as_mut_ptr().add(self.t1), value) } |
| 101 | self.t1 += 1; |
| 102 | } |
| 103 | |
| 104 | /// Consume `n` elements from the source. If `take` is true they will be added to the target, |
| 105 | /// else they will be dropped. |
| 106 | #[inline] |
| 107 | pub fn consume(&mut self, n: usize, take: bool) { |
| 108 | let n = std::cmp::min(n, self.source_slice().len()); |
| 109 | let v = self.v.as_mut_ptr(); |
| 110 | if take { |
| 111 | if self.t1 != self.s0 { |
| 112 | unsafe { |
| 113 | copy(v, self.s0, self.t1, n); |
| 114 | } |
| 115 | } |
| 116 | self.t1 += n; |
| 117 | self.s0 += n; |
| 118 | } else { |
| 119 | for _ in 0..n { |
| 120 | unsafe { |
| 121 | self.s0 += 1; |
| 122 | std::ptr::drop_in_place(v.add(self.s0 - 1)); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /// Skip up to `n` elements from source without adding them to the target. |
| 129 | /// They will be immediately dropped! |
| 130 | pub fn skip(&mut self, n: usize) { |
| 131 | let n = std::cmp::min(n, self.source_slice().len()); |
| 132 | let v = self.v.as_mut_ptr(); |
| 133 | for _ in 0..n { |
| 134 | unsafe { |
| 135 | self.s0 += 1; |
| 136 | std::ptr::drop_in_place(v.add(self.s0 - 1)); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Take up to `n` elements from source to target. |
| 142 | /// If n is larger than the size of the remaining source, this will only copy all remaining elements in source. |
| 143 | pub fn take(&mut self, n: usize) { |
| 144 | let n = std::cmp::min(n, self.source_slice().len()); |
| 145 | if self.t1 != self.s0 { |
| 146 | unsafe { |
| 147 | copy(self.v.as_mut_ptr(), self.s0, self.t1, n); |
| 148 | } |
| 149 | } |
| 150 | self.t1 += n; |
| 151 | self.s0 += n; |
| 152 | } |
| 153 | |
| 154 | /// Takes the next element from the source, if it exists |
| 155 | pub fn pop_front(&mut self) -> Option<T> { |
| 156 | if self.s0 < self.v.len() { |
| 157 | self.s0 += 1; |
| 158 | Some(unsafe { std::ptr::read(self.v.as_ptr().add(self.s0 - 1)) }) |
| 159 | } else { |
| 160 | None |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | fn drop_source(&mut self) { |
| 165 | // use truncate to get rid of the source part, if any, calling drop as needed |
| 166 | self.v.truncate(self.s0); |
| 167 | // use set_len to get rid of the gap part between t1 and s0, not calling drop! |
| 168 | unsafe { |
| 169 | self.v.set_len(self.t1); |
| 170 | } |
| 171 | self.s0 = self.t1; |
| 172 | // shorten the source part |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | #[inline] |
| 177 | unsafe fn copy<T>(v: *mut T, from: usize, to: usize, n: usize) { |
| 178 | // if to < from { |
| 179 | // for i in 0..n { |
| 180 | // std::ptr::write(v.add(to + i), std::ptr::read(v.add(from + i))); |
| 181 | // } |
| 182 | // } else { |
| 183 | // for i in (0..n).rev() { |
| 184 | // std::ptr::write(v.add(to + i), std::ptr::read(v.add(from + i))); |
| 185 | // } |
| 186 | // } |
| 187 | std::ptr::copy(v.add(from), v.add(to), n); |
| 188 | } |
| 189 | |
| 190 | /// the purpose of drop is to clean up and make the SmallVec that we reference into a normal |
| 191 | /// SmallVec again. |
| 192 | impl<'a, T> Drop for InPlaceVecBuilder<'a, T> { |
| 193 | fn drop(&mut self) { |
| 194 | // drop the source part. |
| 195 | self.drop_source(); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | #[cfg(test)] |
| 200 | mod tests { |
| 201 | extern crate testdrop; |
| 202 | use super::*; |
| 203 | use testdrop::{Item, TestDrop}; |
| 204 | |
| 205 | fn everything_dropped<'a, F>(td: &'a TestDrop, n: usize, f: F) |
| 206 | where |
| 207 | F: Fn(Vec<Item<'a>>, Vec<Item<'a>>), |
| 208 | { |
| 209 | let mut a: Vec<Item<'a>> = Vec::new(); |
| 210 | let mut b: Vec<Item<'a>> = Vec::new(); |
| 211 | let mut ids: Vec<usize> = Vec::new(); |
| 212 | for _ in 0..n { |
| 213 | let (id, item) = td.new_item(); |
| 214 | a.push(item); |
| 215 | ids.push(id); |
| 216 | } |
| 217 | for _ in 0..n { |
| 218 | let (id, item) = td.new_item(); |
| 219 | b.push(item); |
| 220 | ids.push(id); |
| 221 | } |
| 222 | f(a, b); |
| 223 | for id in ids { |
| 224 | td.assert_drop(id); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn drop_just_source() { |
| 230 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 231 | let _: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 232 | }) |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn target_push_gap() { |
| 237 | everything_dropped(&TestDrop::new(), 10, |mut a, b| { |
| 238 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 239 | for x in b.into_iter() { |
| 240 | res.push(x); |
| 241 | } |
| 242 | }) |
| 243 | } |
| 244 | |
| 245 | #[test] |
| 246 | fn source_move_some() { |
| 247 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 248 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 249 | res.take(3); |
| 250 | }) |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn source_move_all() { |
| 255 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 256 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 257 | res.take(10); |
| 258 | }) |
| 259 | } |
| 260 | |
| 261 | #[test] |
| 262 | fn source_drop_some() { |
| 263 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 264 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 265 | res.skip(3); |
| 266 | }) |
| 267 | } |
| 268 | |
| 269 | #[test] |
| 270 | fn source_drop_all() { |
| 271 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 272 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 273 | res.skip(10); |
| 274 | }) |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn source_pop_some() { |
| 279 | everything_dropped(&TestDrop::new(), 10, |mut a, _| { |
| 280 | let mut res: InPlaceVecBuilder<Item> = (&mut a).into(); |
| 281 | res.pop_front(); |
| 282 | res.pop_front(); |
| 283 | res.pop_front(); |
| 284 | }) |
| 285 | } |
| 286 | } |