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