pub struct ClosedGenVec<T> { /* private fields */ }Expand description
Generationally indexed vector with an internal index allocator
Implementations§
Source§impl<T> ClosedGenVec<T>
impl<T> ClosedGenVec<T>
Sourcepub fn new() -> ClosedGenVec<T>
pub fn new() -> ClosedGenVec<T>
Returns an empty ClosedGenVec
§Examples
use gen_vec::closed::ClosedGenVec;
let mut vec: ClosedGenVec<i32> = ClosedGenVec::new();Sourcepub fn with_capacity(capacity: usize) -> ClosedGenVec<T>
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);Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn capacity(&self) -> usize
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);Sourcepub fn reserve(&mut self, additional: usize)
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)Sourcepub fn insert(&mut self, value: T) -> Index
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);Sourcepub fn contains(&self, index: Index) -> bool
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));Sourcepub fn remove(&mut self, index: Index) -> Option<T>
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));Sourcepub fn clear(&mut self)
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));Sourcepub fn get(&self, index: Index) -> Option<&T>
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));Sourcepub fn get_mut(&mut self, index: Index) -> Option<&mut T>
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));Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
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);
}Sourcepub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ
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;
}