Struct orx_split_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 Clone for Doubling

source§

fn clone(&self) -> Doubling

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Doubling

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Doubling

source§

fn default() -> Doubling

Returns the “default value” for a type. Read more
source§

impl Growth for Doubling

source§

fn get_ptr<T>( &self, fragments: &[Fragment<T>], index: usize, ) -> Option<*const T>

O(1) Returns a pointer 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 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 get_ptr_mut_and_indices<T>( &self, fragments: &mut [Fragment<T>], index: usize, ) -> Option<(*mut T, usize, usize)>

O(1) Returns a mutable reference to the index-th element of the split vector of the fragments together with the index of the fragment that the element belongs to and index of the element withing the respective fragment.

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, ) -> Result<usize, String>

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_from( &self, fragment_capacities: impl ExactSizeIterator<Item = usize>, ) -> usize

Given that the split vector contains fragments with the given fragment_capacities, 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)>

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 more
source§

fn maximum_concurrent_capacity<T>( &self, fragments: &[Fragment<T>], fragments_capacity: usize, ) -> usize

Returns the maximum number of elements that can safely be stored in a concurrent program. Read more
source§

fn first_fragment_capacity(&self) -> usize

Given that the split vector has no fragments yet, returns the capacity of the first fragment.
source§

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_ptr_and_indices<T>( &self, fragments: &[Fragment<T>], index: usize, ) -> Option<(*const T, usize, usize)>

O(fragments.len()) Returns a mutable reference to the index-th element of the split vector of the fragments together with the index of the fragment that the element belongs to and index of the element withing the respective fragment. Read more
source§

impl GrowthWithConstantTimeAccess for Doubling

source§

fn get_fragment_and_inner_indices_unchecked( &self, element_index: usize, ) -> (usize, usize)

O(1) 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 more
source§

fn fragment_capacity_of(&self, fragment_index: usize) -> usize

O(1) Returns the capacity of the fragment with the given fragment_index.
source§

fn get_ptr<T>( &self, fragments: &[Fragment<T>], index: usize, ) -> Option<*const T>

O(1) Returns a pointer to the index-th element of the split vector of the fragments. Read more
source§

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. Read more
source§

fn get_ptr_mut_and_indices<T>( &self, fragments: &mut [Fragment<T>], index: usize, ) -> Option<(*mut T, usize, usize)>

O(1) Returns a mutable reference to the index-th element of the split vector of the fragments together with the index of the fragment that the element belongs to and index of the element withing the respective fragment. Read more
source§

impl PartialEq for Doubling

source§

fn eq(&self, other: &Doubling) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PseudoDefault for Doubling

source§

fn pseudo_default() -> Self

PseudoDefault trait allows to create a cheap default instance of a type, which does not claim to be useful. Read more
source§

impl StructuralPartialEq for Doubling

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.