Struct orx_concurrent_vec::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 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§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)>
O(fragments.len()) Returns the location of the element with the given
element_index on the split vector as a tuple of (fragment-index, index-within-fragment). Read moresource§impl GrowthWithConstantTimeAccess for Doubling
impl GrowthWithConstantTimeAccess for Doubling
source§impl PartialEq for Doubling
impl PartialEq for Doubling
impl StructuralPartialEq for Doubling
Auto Trait Implementations§
impl Freeze for Doubling
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