Struct orx_split_vec::ExponentialGrowth
source · pub struct ExponentialGrowth { /* private fields */ }Expand description
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 strategies 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));Implementations§
source§impl ExponentialGrowth
impl ExponentialGrowth
sourcepub fn new(growth_coefficient: f32) -> Self
pub fn new(growth_coefficient: f32) -> Self
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.
sourcepub fn growth_coefficient(&self) -> f32
pub fn growth_coefficient(&self) -> f32
Returns the coefficient of the exponential growth strategy.
Trait Implementations§
source§impl Clone for ExponentialGrowth
impl Clone for ExponentialGrowth
source§fn clone(&self) -> ExponentialGrowth
fn clone(&self) -> ExponentialGrowth
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moresource§impl Debug for ExponentialGrowth
impl Debug for ExponentialGrowth
source§impl Default for ExponentialGrowth
impl Default for ExponentialGrowth
source§impl PartialEq<ExponentialGrowth> for ExponentialGrowth
impl PartialEq<ExponentialGrowth> for ExponentialGrowth
source§fn eq(&self, other: &ExponentialGrowth) -> bool
fn eq(&self, other: &ExponentialGrowth) -> bool
self and other values to be equal, and is used
by ==.source§impl<T> SplitVecGrowth<T> for ExponentialGrowth
impl<T> SplitVecGrowth<T> for ExponentialGrowth
source§fn new_fragment_capacity(&self, fragments: &[Fragment<T>]) -> usize
fn new_fragment_capacity(&self, fragments: &[Fragment<T>]) -> usize
fragments,
returns the capacity of the next fragment.