|
| 1 | +use crate::{ |
| 2 | + GrowthWithConstantTimeAccess, |
| 3 | + fragment::transformations::fragment_from_raw, |
| 4 | + range_helpers::{range_end, range_start}, |
| 5 | +}; |
| 6 | +use alloc::vec::Vec; |
| 7 | +use core::cmp::min; |
| 8 | +use core::{cell::UnsafeCell, iter::FusedIterator, ops::Range}; |
| 9 | + |
| 10 | +#[derive(Default)] |
| 11 | +pub(super) struct IntoIterPtrOfConSlices<T, G> |
| 12 | +where |
| 13 | + G: GrowthWithConstantTimeAccess, |
| 14 | +{ |
| 15 | + fragments: Vec<UnsafeCell<*mut T>>, |
| 16 | + growth: G, |
| 17 | + sf: usize, |
| 18 | + si: usize, |
| 19 | + si_end: usize, |
| 20 | + ef: usize, |
| 21 | + ei: usize, |
| 22 | + f: usize, |
| 23 | +} |
| 24 | + |
| 25 | +impl<T, G> Drop for IntoIterPtrOfConSlices<T, G> |
| 26 | +where |
| 27 | + G: GrowthWithConstantTimeAccess, |
| 28 | +{ |
| 29 | + fn drop(&mut self) { |
| 30 | + Self::drop_fragments(&self.growth, &mut self.fragments); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl<T, G> IntoIterPtrOfConSlices<T, G> |
| 35 | +where |
| 36 | + G: GrowthWithConstantTimeAccess, |
| 37 | +{ |
| 38 | + fn empty() -> Self { |
| 39 | + Self { |
| 40 | + fragments: Default::default(), |
| 41 | + growth: G::pseudo_default(), |
| 42 | + sf: 0, |
| 43 | + si: 0, |
| 44 | + si_end: 0, |
| 45 | + ef: 0, |
| 46 | + ei: 0, |
| 47 | + f: 1, |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + fn single_slice( |
| 52 | + fragments: Vec<UnsafeCell<*mut T>>, |
| 53 | + growth: G, |
| 54 | + f: usize, |
| 55 | + begin: usize, |
| 56 | + end: usize, |
| 57 | + ) -> Self { |
| 58 | + Self { |
| 59 | + fragments, |
| 60 | + growth, |
| 61 | + sf: f, |
| 62 | + si: begin, |
| 63 | + si_end: end, |
| 64 | + ef: f, |
| 65 | + ei: 0, |
| 66 | + f, |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + pub fn new( |
| 71 | + capacity: usize, |
| 72 | + mut fragments: Vec<UnsafeCell<*mut T>>, |
| 73 | + growth: G, |
| 74 | + range: Range<usize>, |
| 75 | + ) -> Self { |
| 76 | + let fragment_and_inner_indices = |i| growth.get_fragment_and_inner_indices_unchecked(i); |
| 77 | + |
| 78 | + let a = range_start(&range); |
| 79 | + let b = min(capacity, range_end(&range, capacity)); |
| 80 | + |
| 81 | + match b.saturating_sub(a) { |
| 82 | + 0 => { |
| 83 | + Self::drop_fragments(&growth, &mut fragments); |
| 84 | + Self::empty() |
| 85 | + } |
| 86 | + _ => { |
| 87 | + let (sf, si) = fragment_and_inner_indices(a); |
| 88 | + let (ef, ei) = fragment_and_inner_indices(b - 1); |
| 89 | + |
| 90 | + match sf == ef { |
| 91 | + true => Self::single_slice(fragments, growth, sf, si, ei + 1), |
| 92 | + false => { |
| 93 | + let si_end = growth.fragment_capacity_of(sf); |
| 94 | + Self { |
| 95 | + fragments, |
| 96 | + growth, |
| 97 | + sf, |
| 98 | + si, |
| 99 | + si_end, |
| 100 | + ef, |
| 101 | + ei, |
| 102 | + f: sf, |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fn drop_fragments(growth: &G, fragments: &mut [UnsafeCell<*mut T>]) { |
| 111 | + for (f, cell) in fragments.iter().enumerate() { |
| 112 | + let ptr = unsafe { *cell.get() }; |
| 113 | + match ptr.is_null() { |
| 114 | + true => continue, |
| 115 | + false => { |
| 116 | + let capacity = growth.fragment_capacity_of(f); |
| 117 | + let _fragment_to_drop = unsafe { fragment_from_raw(ptr, 0, capacity) }; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + #[inline(always)] |
| 124 | + fn remaining_len(&self) -> usize { |
| 125 | + (1 + self.ef).saturating_sub(self.f) |
| 126 | + } |
| 127 | + |
| 128 | + #[inline(always)] |
| 129 | + fn get_ptr_fi(&self, f: usize, i: usize) -> *mut T { |
| 130 | + let p = unsafe { *self.fragments[f].get() }; |
| 131 | + unsafe { p.add(i) } |
| 132 | + } |
| 133 | + |
| 134 | + #[inline(always)] |
| 135 | + fn capacity_of(&self, f: usize) -> usize { |
| 136 | + self.growth.fragment_capacity_of(f) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +impl<T, G> Iterator for IntoIterPtrOfConSlices<T, G> |
| 141 | +where |
| 142 | + G: GrowthWithConstantTimeAccess, |
| 143 | +{ |
| 144 | + type Item = (*mut T, usize); |
| 145 | + |
| 146 | + fn next(&mut self) -> Option<Self::Item> { |
| 147 | + match self.f { |
| 148 | + f if f == self.sf => { |
| 149 | + self.f += 1; |
| 150 | + let len = self.si_end - self.si; |
| 151 | + let p = self.get_ptr_fi(self.sf, self.si); |
| 152 | + Some((p, len)) |
| 153 | + } |
| 154 | + f if f < self.ef => { |
| 155 | + self.f += 1; |
| 156 | + let len = self.capacity_of(f); |
| 157 | + let p = self.get_ptr_fi(f, 0); |
| 158 | + Some((p, len)) |
| 159 | + } |
| 160 | + f if f == self.ef => { |
| 161 | + self.f += 1; |
| 162 | + let len = self.ei + 1; |
| 163 | + let p = self.get_ptr_fi(self.ef, 0); |
| 164 | + Some((p, len)) |
| 165 | + } |
| 166 | + _ => None, |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + fn size_hint(&self) -> (usize, Option<usize>) { |
| 171 | + let len = self.remaining_len(); |
| 172 | + (len, Some(len)) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +impl<T, G> FusedIterator for IntoIterPtrOfConSlices<T, G> where G: GrowthWithConstantTimeAccess {} |
| 177 | + |
| 178 | +impl<T, G> ExactSizeIterator for IntoIterPtrOfConSlices<T, G> |
| 179 | +where |
| 180 | + G: GrowthWithConstantTimeAccess, |
| 181 | +{ |
| 182 | + fn len(&self) -> usize { |
| 183 | + self.remaining_len() |
| 184 | + } |
| 185 | +} |
0 commit comments