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
use super::constants::*;
use crate::growth::growth_trait::{Growth, GrowthWithConstantTimeAccess};
use crate::{Fragment, SplitVec};
/// Strategy which allows creates a fragment with double the capacity
/// of the prior fragment every time the split vector needs to expand.
///
/// Assuming it is the common case compared to empty vector scenarios,
/// it immediately allocates the first fragment to keep the `SplitVec` struct smaller.
///
/// # Examples
///
/// ```
/// use orx_split_vec::*;
///
/// // SplitVec<usize, Doubling>
/// let mut vec = SplitVec::with_doubling_growth();
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(4), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // fill the first 5 fragments
/// let expected_fragment_capacities = vec![4, 8, 16, 32];
/// let num_items: usize = expected_fragment_capacities.iter().sum();
/// for i in 0..num_items {
/// vec.push(i);
/// }
///
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments()
/// .iter()
/// .map(|f| f.capacity())
/// .collect::<Vec<_>>()
/// );
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments().iter().map(|f| f.len()).collect::<Vec<_>>()
/// );
///
/// // create the 6-th fragment doubling the capacity
/// vec.push(42);
/// assert_eq!(
/// vec.fragments().len(),
/// expected_fragment_capacities.len() + 1
/// );
///
/// assert_eq!(vec.fragments().last().map(|f| f.capacity()), Some(32 * 2));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Doubling;
impl Growth for Doubling {
fn new_fragment_capacity<T>(&self, fragments: &[Fragment<T>]) -> usize {
fragments.last().map(|f| f.capacity() * 2).unwrap_or(4)
}
#[inline(always)]
fn get_fragment_and_inner_indices<T>(
&self,
vec_len: usize,
_fragments: &[Fragment<T>],
element_index: usize,
) -> Option<(usize, usize)> {
if element_index < vec_len {
let element_index_offset = element_index + FIRST_FRAGMENT_CAPACITY;
let leading_zeros = usize::leading_zeros(element_index_offset) as usize;
let f = OFFSET_FRAGMENT_IDX - leading_zeros;
Some((f, element_index - CUMULATIVE_CAPACITIES[f]))
} else {
None
}
}
/// ***O(1)*** Returns a mutable reference to the `index`-th element of the split vector of the `fragments`.
///
/// Returns `None` if `index`-th position does not belong to the split vector; i.e., if `index` is out of cumulative capacity of fragments.
///
/// # Safety
///
/// This method allows to write to a memory which is greater than the split vector's length.
/// On the other hand, it will never return a pointer to a memory location that the vector does not own.
#[inline(always)]
unsafe fn get_ptr_mut<T>(&self, fragments: &mut [Fragment<T>], index: usize) -> Option<*mut T> {
<Self as GrowthWithConstantTimeAccess>::get_ptr_mut(self, fragments, index)
}
}
impl GrowthWithConstantTimeAccess for Doubling {
fn get_fragment_and_inner_indices_unchecked(&self, element_index: usize) -> (usize, usize) {
let element_index_offset = element_index + FIRST_FRAGMENT_CAPACITY;
let leading_zeros = usize::leading_zeros(element_index_offset) as usize;
let f = OFFSET_FRAGMENT_IDX - leading_zeros;
(f, element_index - CUMULATIVE_CAPACITIES[f])
}
}
impl<T> SplitVec<T, Doubling> {
/// Strategy which allows to create a fragment with double the capacity
/// of the prior fragment every time the split vector needs to expand.
///
/// Assuming it is the common case compared to empty vector scenarios,
/// it immediately allocates the first fragment to keep the `SplitVec` struct smaller.
///
/// # Panics
/// Panics if `first_fragment_capacity` is zero.
///
/// # Examples
///
/// ```
/// use orx_split_vec::*;
///
/// // SplitVec<usize, Doubling>
/// let mut vec = SplitVec::with_doubling_growth();
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(4), vec.fragments().first().map(|f| f.capacity()));
/// assert_eq!(Some(0), vec.fragments().first().map(|f| f.len()));
///
/// // fill the first 5 fragments
/// let expected_fragment_capacities = vec![4, 8, 16, 32];
/// let num_items: usize = expected_fragment_capacities.iter().sum();
/// for i in 0..num_items {
/// vec.push(i);
/// }
///
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments()
/// .iter()
/// .map(|f| f.capacity())
/// .collect::<Vec<_>>()
/// );
/// assert_eq!(
/// expected_fragment_capacities,
/// vec.fragments().iter().map(|f| f.len()).collect::<Vec<_>>()
/// );
///
/// // create the 6-th fragment doubling the capacity
/// vec.push(42);
/// assert_eq!(
/// vec.fragments().len(),
/// expected_fragment_capacities.len() + 1
/// );
///
/// assert_eq!(vec.fragments().last().map(|f| f.capacity()), Some(32 * 2));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
pub fn with_doubling_growth() -> Self {
Self {
fragments: vec![Fragment::new(FIRST_FRAGMENT_CAPACITY)],
growth: Doubling,
len: 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn get_fragment_and_inner_indices() {
let growth = Doubling;
let get = |index| growth.get_fragment_and_inner_indices::<char>(usize::MAX, &[], index);
let get_none = |index| growth.get_fragment_and_inner_indices::<char>(index, &[], index);
assert_eq!((0, 0), growth.get_fragment_and_inner_indices_unchecked(0));
assert_eq!((0, 1), growth.get_fragment_and_inner_indices_unchecked(1));
assert_eq!((1, 0), growth.get_fragment_and_inner_indices_unchecked(4));
assert_eq!((1, 5), growth.get_fragment_and_inner_indices_unchecked(9));
assert_eq!((2, 0), growth.get_fragment_and_inner_indices_unchecked(12));
assert_eq!(Some((0, 0)), get(0));
assert_eq!(Some((0, 1)), get(1));
assert_eq!(Some((1, 0)), get(4));
assert_eq!(Some((1, 5)), get(9));
assert_eq!(Some((2, 0)), get(12));
assert_eq!(None, get_none(0));
assert_eq!(None, get_none(1));
assert_eq!(None, get_none(4));
assert_eq!(None, get_none(9));
assert_eq!(None, get_none(12));
}
#[test]
fn get_fragment_and_inner_indices_exhaustive() {
let growth = Doubling;
let get = |index| growth.get_fragment_and_inner_indices::<char>(usize::MAX, &[], index);
let get_none = |index| growth.get_fragment_and_inner_indices::<char>(index, &[], index);
let mut f = 0;
let mut prev_cumulative_capacity = 0;
let mut curr_capacity = 4;
let mut cumulative_capacity = 4;
for index in 0..1111111 {
if index == cumulative_capacity {
prev_cumulative_capacity = cumulative_capacity;
curr_capacity *= 2;
cumulative_capacity += curr_capacity;
f += 1;
}
let (f, i) = (f, index - prev_cumulative_capacity);
assert_eq!(
(f, i),
growth.get_fragment_and_inner_indices_unchecked(index)
);
assert_eq!(Some((f, i)), get(index));
assert_eq!(None, get_none(index));
}
}
}