pub struct FragmentGrowth { /* private fields */ }
Expand description

Growth policy of fragments in a split vector.

A policy can be defined by one of the three constructors.

  • FragmentGrowth::exponential(initial_capacity, capacity_multiplier)

    • capacity of the f-th fragment will be computed as initial_capacity * capacity_multiplier^f.
  • FragmentGrowth::constant(constant_fragment_length)

    • capacity of all fragments will be equal to constant_fragment_length, leading to a linear growth.
  • FragmentGrowth::by_function(get_capacity_of_fragment)

    • capacity of the f-th fragment will be computed as get_capacity_of_fragment(f);
    • any custom growth policy can be represented by this functional form.

Implementations§

source§

impl FragmentGrowth

source

pub fn exponential(initial_capacity: usize, capacity_multiplier: f32) -> Self

Creates an exponential growth policy with the given positive coefficients.

The capacity of the f-th fragment will be computed as:

initial_capacity * capacity_multiplier^f

Panics

Panics when:

  • initial_capacity is nonpositive, or
  • capacity_multiplier is nonpositive;

as either of these cases would lead to zero capacity allocations for growth.

Examples

Below example demonstrates an exponential growth with a multiplier of 1.5.

use orx_split_vec::{FragmentGrowth, SplitVec};

let growth = FragmentGrowth::exponential(4, 1.5);
let mut vec = SplitVec::with_growth(growth);

assert_eq!(4, vec.capacity());

vec.extend_from_slice(&[0, 1, 2, 3, 4]);
assert_eq!(5, vec.len());
assert_eq!(4 + 6, vec.capacity());

vec.extend_from_slice(&[0, 1, 2, 3, 4]);
assert_eq!(10, vec.len());
assert_eq!(4 + 6, vec.capacity());

vec.push(42);
assert_eq!(11, vec.len());
assert_eq!(4 + 6 + 9, vec.capacity());
assert_eq!(4, vec.fragments()[0].capacity());
assert_eq!(6, vec.fragments()[1].capacity());
assert_eq!(9, vec.fragments()[2].capacity());

One can also allocate the same amount of memory every time new capacity is required.

use orx_split_vec::{FragmentGrowth, SplitVec};

let growth = FragmentGrowth::exponential(10, 1.0);
let mut vec = SplitVec::with_growth(growth);

assert_eq!(10, vec.capacity());

for x in 0..10 {
    vec.push(x);
}

assert_eq!(10, vec.len());
assert_eq!(10, vec.capacity());

vec.push(42);
assert_eq!(11, vec.len());
assert_eq!(20, vec.capacity());
for fragment in vec.fragments() {
    assert_eq!(10, fragment.capacity());
}
source

pub fn constant(constant_fragment_length: usize) -> Self

Creates a constant growth policy where every fragment will have the same length; i.e., constant_fragment_length.

Note that FragmentGrowth::constant(10) is a shorthand for FragmentGrowth::exponential(10, 1.0).

Panics

Panics when:

  • constant_fragment_length is nonpositive

as ot would lead to zero capacity allocations for growth.

Examples
use orx_split_vec::{FragmentGrowth, SplitVec};

let growth = FragmentGrowth::constant(10);
let mut vec = SplitVec::with_growth(growth);

assert_eq!(10, vec.capacity());

for x in 0..10 {
    vec.push(x);
}

assert_eq!(10, vec.len());
assert_eq!(10, vec.capacity());

vec.push(42);
assert_eq!(11, vec.len());
assert_eq!(20, vec.capacity());
for fragment in vec.fragments() {
    assert_eq!(10, fragment.capacity());
}
source

pub fn by_function(get_capacity_of_fragment: Rc<dyn Fn(usize) -> usize>) -> Self

Creates a growth policy function where the capacities are computed by the given function.

The capacity of the f-th fragment will be computed as:

get_capacity_by_fragment(f)

Examples

One interesting policy could be to increase the fragment lengths until it reaches a particular level. Then, each expansion could be a constant expansion.

use orx_split_vec::{FragmentGrowth, SplitVec};
use std::rc::Rc;

fn get_fragment_capacity(fragment: usize) -> usize {
    let exp = (4.0 * f32::powf(1.5, fragment as f32)) as usize;
    exp.min(10)
}
let growth = FragmentGrowth::by_function(Rc::new(get_fragment_capacity));
let mut vec = SplitVec::with_growth(growth);

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

assert_eq!(4, vec.fragments()[0].capacity());
assert_eq!(6, vec.fragments()[1].capacity());
assert_eq!(9, vec.fragments()[2].capacity());
assert_eq!(10, vec.fragments()[3].capacity());
for fragment in vec.fragments().iter().skip(4) {
    assert_eq!(10, fragment.capacity());
}
source

pub fn get_capacity(&self, fragment_index: usize) -> usize

Returns the capacity of the fragment_index-th fragment.

This method returns the maximum of 4 and the computed capacity with the defined strategy; i.e., capacities 0, 1, 2 and 3 are not allowed.

Trait Implementations§

source§

impl Clone for FragmentGrowth

source§

fn clone(&self) -> FragmentGrowth

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 FragmentGrowth

source§

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

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

impl Default for FragmentGrowth

source§

fn default() -> Self

Creates the default growth with an initial capacity of 4 and capacity multiplier of 1.5.

Fragment capacities with the default function will be:

  • fragment-0: capacity=4 -> total-capacity=4
  • fragment-1: capacity=6 -> total-capacity=10
  • fragment-2: capacity=9 -> total-capacity=19
  • fragment-3: capacity=13 -> total-capacity=32

Auto Trait Implementations§

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.