Skip to main content

IndexAllocator

Struct IndexAllocator 

Source
pub struct IndexAllocator { /* private fields */ }
Expand description

Allocates and deallocates indices for a ExposedGenVec

Implementations§

Source§

impl IndexAllocator

Source

pub fn new() -> IndexAllocator

Returns a new empty IndexAllocator

§Examples
use gen_vec::exposed::IndexAllocator;
let mut allocator: IndexAllocator = IndexAllocator::new();
Source

pub fn with_capacity(capacity: usize) -> IndexAllocator

Returns a IndexAllocator with initial capacity of capacity

Allows the IndexAllocator to hold capacity elements before allocating more space

§Examples
use gen_vec::exposed::IndexAllocator;
let mut allocator: IndexAllocator = IndexAllocator::with_capacity(5);
Source

pub fn allocate(&mut self) -> Index

Allocates and returns a new Index

Activates a freed index if there are any, otherwise creates and adds a new index to active_indices

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
let index: Index = allocator.allocate();
Source

pub fn deallocate(&mut self, index: Index)

Frees index if it hasn’t been already.

Afterwards, index is added to the pool of free indices available for reuse

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
let index: Index = allocator.allocate();
allocator.deallocate(index);
Source

pub fn deallocate_all(&mut self)

Frees all active indices and adds them to the pool of free indices

§Examples
use gen_vec::exposed::IndexAllocator;
use gen_vec::Index;

let mut allocator: IndexAllocator = IndexAllocator::new();
for _ in 0..10
{
    allocator.allocate();
}
assert_eq!(allocator.num_active(), 10);
assert_eq!(allocator.num_free(), 0);

allocator.deallocate_all();
assert_eq!(allocator.num_active(), 0);
assert_eq!(allocator.num_free(), 10);
Source

pub fn capacity(&self) -> usize

Reserved capacity within the IndexAllocator

§Examples
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::with_capacity(5);
assert_eq!(allocator.capacity(), 5);
Source

pub fn reserve(&mut self, additional: usize)

Reserves extra space for at least additional more elements

More space may be allocated to avoid frequent re-allocations (as per the specifications of std::vec::Vec)

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
let index: Index = allocator.allocate();
allocator.reserve(4);
assert!(allocator.capacity() >= 4);
Source

pub fn is_active(&self, index: Index) -> bool

Returns if index is still active and hasn’t been deallocated

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
let index: Index = allocator.allocate();
assert!(allocator.is_active(index));
allocator.deallocate(index);
assert!(!allocator.is_active(index));
Source

pub fn num_free(&self) -> usize

Returns the number of free indices waiting to be allocated and reused

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
assert_eq!(allocator.num_free(), 0);

let index: Index = allocator.allocate();

allocator.deallocate(index);
assert_eq!(allocator.num_free(), 1);

let index: Index = allocator.allocate();
assert_eq!(allocator.num_free(), 0);
Source

pub fn num_active(&self) -> usize

Returns the number of active indices

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
assert_eq!(allocator.num_active(), 0);

let index: Index = allocator.allocate();
assert_eq!(allocator.num_active(), 1);

allocator.deallocate(index);
assert_eq!(allocator.num_active(), 0);
Source

pub fn iter(&self) -> Iter<'_>

Returns an iterator over an immutable IndexAllocator Each step returns an Index

§Examples
use gen_vec::Index;
use gen_vec::exposed::IndexAllocator;

let mut allocator: IndexAllocator = IndexAllocator::new();
allocator.allocate();
allocator.allocate();

for index in allocator
{
    println!("{:?}", index);
}

Trait Implementations§

Source§

impl Debug for IndexAllocator

Source§

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

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

impl Default for IndexAllocator

Source§

fn default() -> IndexAllocator

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

impl<'a> IntoIterator for &'a IndexAllocator

Source§

type Item = Index

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for IndexAllocator

Source§

type Item = Index

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

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> 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, 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.