Struct ClosedGenVec

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

Generationally indexed vector with an internal index allocator

Implementations§

Source§

impl<T> ClosedGenVec<T>

Source

pub fn new() -> ClosedGenVec<T>

Returns an empty ClosedGenVec

§Examples
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();
Source

pub fn with_capacity(capacity: usize) -> ClosedGenVec<T>

Returns a ClosedGenVec with initial capacity of capacity

Allows the ClosedGenVec to hold capacity elements before allocating more space

§Examples
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::with_capacity(5);
Source

pub fn len(&self) -> usize

Number of active Items within the vec

The internal item vec may actually be larger depending on the number of freed indices

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(42);
assert_eq!(vec.len(), 1);

vec.remove(index);
assert_eq!(vec.len(), 0);
Source

pub fn is_empty(&self) -> bool

Returns true if there are no active items

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();
assert!(vec.is_empty());

let index: Index = vec.insert(23);
assert!(!vec.is_empty());

vec.remove(index);
assert!(vec.is_empty());
Source

pub fn capacity(&self) -> usize

Reserved capacity within the vec

§Examples
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::with_capacity(5);
assert_eq!(vec.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::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();
assert_eq!(vec.capacity(), 0);

let index: Index = vec.insert(13);

vec.reserve(4);
assert!(vec.capacity() >= 4)
Source

pub fn insert(&mut self, value: T) -> Index

Insert value and return an associated Index

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(23);
Source

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

Returns true if the index points to a valid item within

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(124);
assert!(vec.contains(index));

vec.remove(index);
assert!(!vec.contains(index));
Source

pub fn remove(&mut self, index: Index) -> Option<T>

Returns the value of index if index is valid

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

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(124);
assert!(vec.contains(index));

vec.remove(index);
assert!(!vec.contains(index));
Source

pub fn clear(&mut self)

Free all items

Internal capacity will not change. Internally this is performed as all Some(_) being replaced with None

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(42);
assert!(vec.contains(index));

vec.clear();

assert!(!vec.contains(index));
Source

pub fn get(&self, index: Index) -> Option<&T>

Returns an immutable reference to the value of index if index is valid

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(23);

let value: Option<&i32> = vec.get(index);
assert_eq!(value, Some(&23));
Source

pub fn get_mut(&mut self, index: Index) -> Option<&mut T>

Returns a mutable reference to the value of index if index is valid

§Examples
use gen_vec::Index;
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();

let index: Index = vec.insert(23);

let mut value: Option<&mut i32> = vec.get_mut(index);
assert_eq!(value, Some(&mut 23));

if let Some(value) = value
{
    *value = 0;
}

let value: Option<&i32> = vec.get(index);
assert_eq!(value, Some(&0));
Source

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

Returns an iterator of immutable references to the vec elements

Each iterator step returns (Index, &T)

§Examples
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();
vec.insert(0);
vec.insert(1);

for (index, value) in vec // vec.iter() is valid too
{
    println!("Index: {:?}, Value: {}", index, value);
}
Source

pub fn iter_mut(&mut self) -> IterMut<'_, T>

Returns an iterator of mutable references to the vec elements

Each iterator step returns (Index, &mut T)

§Examples
use gen_vec::closed::ClosedGenVec;

let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();
vec.insert(0);
vec.insert(1);

for (index, value) in vec.iter_mut()
{
    *value = 0;
}

Trait Implementations§

Source§

impl<T: Debug> Debug for ClosedGenVec<T>

Source§

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

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

impl<T: Default> Default for ClosedGenVec<T>

Source§

fn default() -> ClosedGenVec<T>

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

impl<T> Index<Index> for ClosedGenVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: Index) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<T> IndexMut<Index> for ClosedGenVec<T>

Source§

fn index_mut(&mut self, index: Index) -> &mut Self::Output

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, T> IntoIterator for &'a ClosedGenVec<T>

Source§

type Item = (Index, &'a T)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, T>

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<'a, T> IntoIterator for &'a mut ClosedGenVec<T>

Source§

type Item = (Index, &'a mut T)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, T>

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<T> IntoIterator for ClosedGenVec<T>

Source§

type Item = (Index, T)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<T>

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§

§

impl<T> Freeze for ClosedGenVec<T>

§

impl<T> RefUnwindSafe for ClosedGenVec<T>
where T: RefUnwindSafe,

§

impl<T> Send for ClosedGenVec<T>
where T: Send,

§

impl<T> Sync for ClosedGenVec<T>
where T: Sync,

§

impl<T> Unpin for ClosedGenVec<T>
where T: Unpin,

§

impl<T> UnwindSafe for ClosedGenVec<T>
where T: UnwindSafe,

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.