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
use super::{
any,
growth_trait::{SplitVecGrowth, SplitVecGrowthWithFlexibleIndexAccess},
};
use crate::{Fragment, SplitVec};
/// Stategy which allows new fragments grow exponentially.
///
/// The capacity of the n-th fragment is computed as
/// `cap0 * pow(growth_coefficient, n)`
/// where `cap0` is the capacity of the first fragment.
///
/// Note that `DoublingGrowth` is a special case of `ExponentialGrowth`
/// with `growth_coefficient` equal to 2,
/// while providing a faster access by index.
///
/// On the other hand, exponential growth allows for fitting growth startegies
/// for fitting situations which could be a better choice when memory allocation
/// is more important than index access complexity.
///
/// As you may see in the example below, it is especially useful in providing
/// exponential growth rates slower than the doubling.
///
/// 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;
///
/// // SplitVec<usize, ExponentialGrowth>
/// let mut vec = SplitVec::with_exponential_growth(2, 1.5);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(2), 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![2, 3, 4, 6, 9, 13];
/// 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((13 as f32 * 1.5) as usize));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ExponentialGrowth {
growth_coefficient: f32,
}
impl ExponentialGrowth {
/// Creates a new exponential growth strategy with the given `growth_coefficient`.
///
/// The capacity of the n-th fragment is computed as
/// `cap0 * pow(growth_coefficient, n)`
/// where `cap0` is the capacity of the first fragment.
///
/// # Panics
/// Panics if `growth_coefficient` is less than 1.0.
pub fn new(growth_coefficient: f32) -> Self {
assert!(
growth_coefficient >= 1.0,
"Growth coefficient of exponential growth strategy must be greater than or equal to 1."
);
Self { growth_coefficient }
}
/// Returns the coefficient of the exponential growth strategy.
pub fn growth_coefficient(&self) -> f32 {
self.growth_coefficient
}
}
impl Default for ExponentialGrowth {
/// Creates a default exponential growth strategy with
/// `growth_coefficient` being equal to 1.5.
fn default() -> Self {
Self {
growth_coefficient: 1.5,
}
}
}
impl<T> SplitVecGrowth<T> for ExponentialGrowth {
fn new_fragment_capacity(&self, fragments: &[Fragment<T>]) -> usize {
fragments
.last()
.map(|f| (f.capacity() as f32 * self.growth_coefficient) as usize)
.unwrap_or(4)
}
fn get_fragment_and_inner_indices(
&self,
fragments: &[Fragment<T>],
element_index: usize,
) -> Option<(usize, usize)> {
any::get_fragment_and_inner_indices(fragments, element_index)
}
}
impl<T> SplitVecGrowthWithFlexibleIndexAccess<T> for ExponentialGrowth {}
impl<T> SplitVec<T, ExponentialGrowth> {
/// Stategy which allows new fragments grow exponentially.
///
/// The capacity of the n-th fragment is computed as
/// `cap0 * pow(growth_coefficient, n)`
/// where `cap0` is the capacity of the first fragment.
///
/// Note that `DoublingGrowth` is a special case of `ExponentialGrowth`
/// with `growth_coefficient` equal to 2,
/// while providing a faster access by index.
///
/// On the other hand, exponential growth allows for fitting growth startegies
/// for fitting situations which could be a better choice when memory allocation
/// is more important than index access complexity.
///
/// As you may see in the example below, it is especially useful in providing
/// exponential growth rates slower than the doubling.
///
/// 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,
/// or if `growth_coefficient` is less than 1.0.
///
/// # Examples
///
/// ```
/// use orx_split_vec::SplitVec;
///
/// // SplitVec<usize, ExponentialGrowth>
/// let mut vec = SplitVec::with_exponential_growth(2, 1.5);
///
/// assert_eq!(1, vec.fragments().len());
/// assert_eq!(Some(2), 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![2, 3, 4, 6, 9, 13];
/// 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((13 as f32 * 1.5) as usize));
/// assert_eq!(vec.fragments().last().map(|f| f.len()), Some(1));
/// ```
pub fn with_exponential_growth(
first_fragment_capacity: usize,
growth_coefficient: f32,
) -> Self {
assert!(first_fragment_capacity > 0);
assert!(growth_coefficient >= 1.0);
Self {
fragments: vec![Fragment::new(first_fragment_capacity)],
growth: ExponentialGrowth::new(growth_coefficient),
}
}
}