pub struct CustomGrowth<T> { /* private fields */ }
Expand description

Stategy which allows to define a custom growth rate with a function of priorly created fragments.

Examples

use orx_split_vec::prelude::*;
use std::rc::Rc;

// vec: SplitVec<usize, CustomGrowth<usize>>
let mut vec =
    SplitVec::with_custom_growth_function(Rc::new(|fragments: &[Fragment<_>]| {
        if fragments.len() % 2 == 0 {
            2
        } else {
            8
        }
    }));

    for i in 0..100 {
        vec.push(i);
    }

    vec.into_iter().zip(0..100).all(|(l, r)| *l == r);
     
    for (f, fragment) in vec.fragments().iter().enumerate() {
        if f % 2 == 0 {
            assert_eq!(2, fragment.capacity());
        } else {
            assert_eq!(8, fragment.capacity());
        }
    }

Implementations§

§

impl<T> CustomGrowth<T>

pub fn new( get_capacity_of_new_fragment: Rc<dyn Fn(&[Fragment<T>]) -> usize, Global> ) -> CustomGrowth<T>

Creates a new custom growth where the growth strategy is defined by the function get_capacity_of_new_fragment.

Panics

Panics if the function returns 0 as the capacity of the new fragment for any given set of already created fragments.

Trait Implementations§

§

impl<T> Clone for CustomGrowth<T>

§

fn clone(&self) -> CustomGrowth<T>

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
§

impl<T> Debug for CustomGrowth<T>

§

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

Formats the value using the given formatter. Read more
§

impl<T> Default for CustomGrowth<T>where T: 'static,

§

fn default() -> CustomGrowth<T>

Creates the default custom growth which doubles the initial capacity every 4 fragments.

Let c be the initial capacity of the split vector; i.e., capacity of the first fragment. Then, fragment capacities with the default function will be computed as:

  • capacities of fragments 0..4 = c
  • capacities of fragments 4..8 = 2c
  • capacities of fragments 8..12 = 3c
§

impl<T> SplitVecGrowth<T> for CustomGrowth<T>

§

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

Given that the split vector contains the given fragments, returns the capacity of the next fragment.
§

fn get_fragment_and_inner_indices( &self, fragments: &[Fragment<T>], element_index: usize ) -> Option<(usize, usize)>

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

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for CustomGrowth<T>

§

impl<T> !Send for CustomGrowth<T>

§

impl<T> !Sync for CustomGrowth<T>

§

impl<T> Unpin for CustomGrowth<T>

§

impl<T> !UnwindSafe for CustomGrowth<T>

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.