pub struct IndexAllocator { /* private fields */ }Expand description
Allocates and deallocates indices for a ExposedGenVec
Implementations§
Source§impl IndexAllocator
impl IndexAllocator
Sourcepub fn new() -> IndexAllocator
pub fn new() -> IndexAllocator
Returns a new empty IndexAllocator
§Examples
use gen_vec::exposed::IndexAllocator;
let mut allocator: IndexAllocator = IndexAllocator::new();Sourcepub fn with_capacity(capacity: usize) -> IndexAllocator
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);Sourcepub fn allocate(&mut self) -> Index
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();Sourcepub fn deallocate(&mut self, index: Index)
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);Sourcepub fn deallocate_all(&mut self)
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);Sourcepub fn capacity(&self) -> usize
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);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::exposed::IndexAllocator;
let mut allocator: IndexAllocator = IndexAllocator::new();
let index: Index = allocator.allocate();
allocator.reserve(4);
assert!(allocator.capacity() >= 4);Sourcepub fn is_active(&self, index: Index) -> bool
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));Sourcepub fn num_free(&self) -> usize
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);Sourcepub fn num_active(&self) -> usize
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);Sourcepub fn iter(&self) -> Iter<'_> ⓘ
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);
}