Struct orx_split_vec::Doubling
source · pub struct Doubling;
Expand description
Stategy 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::prelude::*;
// SplitVec<usize, DoublingGrowth>
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));
Trait Implementations§
source§impl Growth for Doubling
impl Growth for Doubling
source§fn new_fragment_capacity<T>(&self, fragments: &[Fragment<T>]) -> usize
fn new_fragment_capacity<T>(&self, fragments: &[Fragment<T>]) -> usize
Given that the split vector contains the given
fragments
,
returns the capacity of the next fragment.source§impl PartialEq for Doubling
impl PartialEq for Doubling
impl StructuralPartialEq for Doubling
Auto Trait Implementations§
impl RefUnwindSafe for Doubling
impl Send for Doubling
impl Sync for Doubling
impl Unpin for Doubling
impl UnwindSafe for Doubling
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more