pub struct GenIndexAllocator<I: GenIndex = IndexF64> { /* private fields */ }Expand description
Allocator of generational indices.
Implementations§
Source§impl<I: GenIndex> GenIndexAllocator<I>
impl<I: GenIndex> GenIndexAllocator<I>
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Constructs a new, empty GenIndexAllocator with the specified capacity.
The allocator will be able to hold exactly capacity elements without reallocating.
If capacity is 0, it will not allocate.
§Panic
Panics if the capacity overflows.
§Examples
let allocator = <GenIndexAllocator>::with_capacity(10);
assert_eq!(allocator.capacity(), 10);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the allocator, also referred to as its ‘length’.
§Examples
let mut allocator = <GenIndexAllocator>::new();
assert_eq!(allocator.len(), 0);
allocator.create();
assert_eq!(allocator.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the allocator contains no elements.
§Examples
let mut allocator = <GenIndexAllocator>::new();
assert!(allocator.is_empty());
allocator.create();
assert!(!allocator.is_empty());Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the allocator can hold without reallocating.
§Examples
let allocator = <GenIndexAllocator>::with_capacity(10);
assert_eq!(allocator.capacity(), 10);Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional more elements to be inserted in the given allocator.
The collection may reserve more space to avoid frequent reallocations. After calling reserve, capacity
will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient.
§Panics
Panics if the capacity overflows.
§Examples
let mut allocator = <GenIndexAllocator>::new();
allocator.reserve(10);
assert!(allocator.capacity() >= 10);Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the allocator, removing all values. Note that this method has no effect on the allocated capacity of the allocator.
§Examples
allocator.create();
allocator.create();
allocator.clear();
assert!(allocator.is_empty());Sourcepub fn remove(&mut self, i: &I) -> bool
pub fn remove(&mut self, i: &I) -> bool
Removes index i from the allocator if exists.
Returns a bool indicating whether the allocator originally contains the index.
§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = &allocator.create();
assert!(allocator.remove(i));
assert!(!allocator.remove(i));Sourcepub fn contains(&self, i: &I) -> bool
pub fn contains(&self, i: &I) -> bool
Returns true if the allocator contains the index i .
§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = &allocator.create();
assert!(allocator.contains(i));
allocator.remove(i);
assert!(!allocator.contains(i));Sourcepub fn get(&self, idx: &I::Index) -> Option<&I>
pub fn get(&self, idx: &I::Index) -> Option<&I>
Given an index without a generation, get the GenIndex at that slot.
§Examples
let mut allocator = <GenIndexAllocator>::new();
let i = allocator.create();
assert_eq!(i, *allocator.get(&i.index()).unwrap());Sourcepub fn retain(&mut self, f: impl FnMut(&I) -> bool)
pub fn retain(&mut self, f: impl FnMut(&I) -> bool)
Retains only the indices specified by the predicate.
In other words, removes all indices such that f(index) returns false.
§Examples
let mut allocator = <GenIndexAllocator>::new();
let idx1 = &allocator.create();
let idx2 = &allocator.create();
allocator.retain(|idx| idx == idx1);
assert!(allocator.contains(idx1));
assert!(!allocator.contains(idx2));Trait Implementations§
Source§impl<I: Clone + GenIndex> Clone for GenIndexAllocator<I>
impl<I: Clone + GenIndex> Clone for GenIndexAllocator<I>
Source§fn clone(&self) -> GenIndexAllocator<I>
fn clone(&self) -> GenIndexAllocator<I>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more