[][src]Struct gvec::DenseVec

pub struct DenseVec<T> { /* fields omitted */ }

A Generational vector

A vector with a so-called Generational scheme, akin to a versioning scheme, for its indices. This is done in order to prevent use-after-free scenarios, where an attempt to access a specific position with a given index is denied after the original item is removed and its position (or index) has been freed. It also promotes memory reuse, since the memory allocated is not released until the vector goes out of scope or the program ends, and it might lead to lower overall memory use.

Note

Due to the functionality of avoiding invalid access (use-after-free), this type of vector consumes much more memory than a simple vector itself.

Methods

impl<T> DenseVec<T>[src]

pub fn with_capacity(capacity: usize) -> DenseVec<T>[src]

Creates a GenVec with a specific capacity

Creates a GenerationalVec with a pre-allocated memory in order to reduce the overhead of future reallocations.

pub fn get(&self, index: GenerationalIndex) -> Option<&T>[src]

Get a value by an index

Returns the value if both the index and the generation do match. Otherwise returns None. A safe alternative to the index [] operator, which can panic if index out-of-bounds or generation does not match.

pub fn insert(&mut self, value: T) -> GenerationalIndex[src]

Insert a value on the vector

Returns a GenerationalIndex with the index and the generation in which it was added.

Example

use gvec::DenseVec as GenVec;

let mut example = GenVec::with_capacity(2);
let first = example.insert(2);
assert_eq!(example[first], 2);

pub fn remove(&mut self, index: GenerationalIndex) -> Result<(), Error>[src]

Remove a value of the vector by a given index

Upon the removal of the value, the index is liberated for use, in order to reuse the space already allocated, and the generation is upped by one, in order to prevent use-after-free scenarios or "address violation" (access another value allocated in the same space (i.e. same index), but with a different generation)

Example

use gvec::DenseVec as GenVec;

let mut example = GenVec::with_capacity(2);
let first = example.insert(1);
assert_eq!(example[first], 1);
example.remove(first);
assert_eq!(example[first], 1); // produces a panic!

pub fn iter(&self) -> impl Iterator<Item = &T>[src]

Trait Implementations

impl<T> IntoIterator for DenseVec<T>[src]

type Item = T

The type of the elements being iterated over.

type IntoIter = IntoIter<T>

Which kind of iterator are we turning this into?

impl<T> Index<GenerationalIndex> for DenseVec<T>[src]

type Output = T

The returned type after indexing.

fn index(&self, index: GenerationalIndex) -> &Self::Output[src]

Panics

Indexing will panic! when the generation of the given index does not match the generation in the vector. Use GenVec::get() instead as a non-panic version that will return an Option.

Auto Trait Implementations

impl<T> Send for DenseVec<T> where
    T: Send

impl<T> Sync for DenseVec<T> where
    T: Sync

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<I> IntoIterator for I where
    I: Iterator
[src]

type Item = <I as Iterator>::Item

The type of the elements being iterated over.

type IntoIter = I

Which kind of iterator are we turning this into?

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]