Struct orx_split_vec::prelude::Doubling
source · pub struct Doubling;Expand description
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));Trait Implementations§
source§impl Growth for Doubling
impl Growth for Doubling
source§unsafe fn get_ptr_mut<T>(
&self,
fragments: &mut [Fragment<T>],
index: usize
) -> Option<*mut T>
unsafe fn get_ptr_mut<T>( &self, fragments: &mut [Fragment<T>], index: usize ) -> Option<*mut T>
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.
source§fn required_fragments_len<T>(
&self,
_: &[Fragment<T>],
maximum_capacity: usize
) -> usize
fn required_fragments_len<T>( &self, _: &[Fragment<T>], maximum_capacity: usize ) -> usize
Returns the number of fragments with this growth strategy in order to be able to reach a capacity of maximum_capacity of elements.
This method is relevant and useful for concurrent programs, which helps in avoiding the fragments to allocate.
§Panics
Panics if maximum_capacity is greater than sum { 2^f | for f in 2..34 }.
source§fn new_fragment_capacity<T>(&self, fragments: &[Fragment<T>]) -> usize
fn new_fragment_capacity<T>(&self, fragments: &[Fragment<T>]) -> usize
fragments,
returns the capacity of the next fragment.source§fn get_fragment_and_inner_indices<T>(
&self,
vec_len: usize,
_fragments: &[Fragment<T>],
element_index: usize
) -> Option<(usize, usize)>
fn get_fragment_and_inner_indices<T>( &self, vec_len: usize, _fragments: &[Fragment<T>], element_index: usize ) -> Option<(usize, usize)>
element_index on the split vector as a tuple of (fragment-index, index-within-fragment). Read more