pub struct OptVec<T, S> { /* private fields */ }Expand description
A vector containing optional values.
The vector would be useful when you need invariant index regardless of insertion and removal. If you remove an item from the vector, the slot remains vacant rather than removing the space by moving right items. Then the vacant slot can be filled when you insert an item into the vector.
For more understanding, see the diagram below.
vector -> [ None, Some, None ]
occupied No Yes No
vacant Yes No Yes
* length: 1
* number of slots: 3
* number of vacancies: 2Implementations§
Source§impl<T, S> OptVec<T, S>
impl<T, S> OptVec<T, S>
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns number of items, which is occupied slots in other words.
Returned value is equal to self.num_slots() - self.num_vacancies().
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true is the vector is empty.
Note that vector may have slots in it even if it’s empty.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<i32, RandomState>::new();
assert!(v.is_empty());Sourcepub fn num_slots(&self) -> usize
pub fn num_slots(&self) -> usize
Returns number of all slots including occupied and vacant slots.
Returned value is equal to self.len() + self.num_vacancies().
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.num_slots(), 1);Sourcepub fn num_vacancies(&self) -> usize
pub fn num_vacancies(&self) -> usize
Returns number of vacant slots.
Returned value is equal to self.num_slots() - self.len().
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
v.take(0);
assert_eq!(v.num_vacancies(), 1);Sourcepub fn is_occupied(&self, index: usize) -> bool
pub fn is_occupied(&self, index: usize) -> bool
Sourcepub fn as_slice(&self) -> &[Option<T>]
pub fn as_slice(&self) -> &[Option<T>]
Creates a slice from the vector.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
v.add(1);
assert_eq!(v.as_slice(), &[Some(0), Some(1)]);Sourcepub unsafe fn as_mut_slice(&mut self) -> &mut [Option<T>]
pub unsafe fn as_mut_slice(&mut self) -> &mut [Option<T>]
Creates a mutable slice from the vector.
Caller must not modify occupied/vacant status in the returned slice because the vector is tracking the status.
§Safety
Undefined behavior if caller take out a value from an occupied slot, or insert a value into a vacant slot.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
v.add(1);
assert_eq!(v.as_slice(), &[Some(0), Some(1)]);Sourcepub fn get(&self, index: usize) -> Option<&T>
pub fn get(&self, index: usize) -> Option<&T>
Returns shared reference to the value at the given index if the index is in bounds and the slot is occupied.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.get(0), Some(&0));Sourcepub unsafe fn get_unchecked(&self, index: usize) -> &T
pub unsafe fn get_unchecked(&self, index: usize) -> &T
Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut T>
pub fn get_mut(&mut self, index: usize) -> Option<&mut T>
Returns mutable reference to the value at the given index if the index is in bounds and the slot is occupied.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.get_mut(0), Some(&mut 0));Sourcepub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
pub unsafe fn get_unchecked_mut(&mut self, index: usize) -> &mut T
Returns shared reference to the value at the given index.
§Safety
Undefined behavior if
- The index is out of bounds.
- The slot is vacant.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(unsafe { v.get_unchecked_mut(0) }, &mut 0);Sourcepub fn pairs(&self) -> impl Iterator<Item = (usize, &T)>
pub fn pairs(&self) -> impl Iterator<Item = (usize, &T)>
Returns an iterator visiting all values with corresponding indices.
As return type says, vacant slots are filtered out from the iteration.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add('a');
v.add('b');
let pairs = v.pairs().collect::<Vec<_>>();
assert_eq!(pairs, [(0, &'a'), (1, &'b')]);Sourcepub fn pairs_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)>
pub fn pairs_mut(&mut self) -> impl Iterator<Item = (usize, &mut T)>
Returns a mutable iterator visiting all values with corresponding indices.
As return type says, vacant slots are filtered out from the iteration.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add('a');
v.add('b');
let pairs = v.pairs_mut().collect::<Vec<_>>();
assert_eq!(pairs, [(0, &mut 'a'), (1, &mut 'b')]);Sourcepub fn slots(&self) -> Iter<'_, Option<T>>
pub fn slots(&self) -> Iter<'_, Option<T>>
Returns an iterator visiting all slots regardless of whether the slot is occupied or not.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add('a');
v.add('b');
v.take(0);
let slots = v.slots().cloned().collect::<Vec<_>>();
assert_eq!(slots, [None, Some('b')]);Sourcepub fn iter(&self) -> impl Iterator<Item = &T> + Clone
pub fn iter(&self) -> impl Iterator<Item = &T> + Clone
Returns an iterator visiting all values.
As return type says, vacant slots are filtered out from the iteration.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add('a');
v.add('b');
let values = v.iter().collect::<Vec<_>>();
assert_eq!(values, [&'a', &'b']);Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut T>
Returns a mutable iterator visiting all values.
As return type says, vacant slots are filtered out from the iteration.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add('a');
v.add('b');
let values = v.iter_mut().collect::<Vec<_>>();
assert_eq!(values, [&mut 'a', &mut 'b']);Source§impl<T, S> OptVec<T, S>where
S: BuildHasher,
impl<T, S> OptVec<T, S>where
S: BuildHasher,
Sourcepub fn next_index(&self) -> usize
pub fn next_index(&self) -> usize
Returns the next index that will be returned on the next call to
OptVec::add.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
assert_eq!(v.next_index(), 0);
v.add(0);
v.add(1);
v.take(0);
assert_eq!(v.next_index(), 0);Sourcepub fn add(&mut self, value: T) -> usize
pub fn add(&mut self, value: T) -> usize
Inserts the given value into the vector.
The vector prefers to insert values into vacant slots if possible. But if the vector doesn’t have any vacant slots, then the value is appended to the end of the vector.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.len(), 1);Sourcepub fn push(&mut self, value: Option<T>)
pub fn push(&mut self, value: Option<T>)
Appends the given optional value to the end of the vector.
Note that this method won’t insert the value into a vacant slot. It just makes a new slot at the end of the vector then puts the value in the slot.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
v.take(0);
v.push(Some(0));
assert_eq!(v.as_slice(), &[None, Some(0)]);Sourcepub fn take(&mut self, index: usize) -> Option<T>
pub fn take(&mut self, index: usize) -> Option<T>
Takes value out of the slot at the given index.
After calling the method, the slot remains vacant.
§Panics
Panics if the given index is out of bounds.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.add(0);
assert_eq!(v.take(0), Some(0));
assert_eq!(v.take(0), None);Sourcepub fn resize(&mut self, new_len: usize, value: Option<T>)where
T: Clone,
pub fn resize(&mut self, new_len: usize, value: Option<T>)where
T: Clone,
Resizes the vector to the given length.
If the new length is greater than previous length of the vector, then the vector is extended with the given optional value. Otherwise, the vector is shrunk.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.resize(2, Some(0));
assert_eq!(v.as_slice(), &[Some(0), Some(0)]);Sourcepub fn resize_with<F>(&mut self, new_len: usize, f: F)
pub fn resize_with<F>(&mut self, new_len: usize, f: F)
Resizes the vector to the given length.
If the new length is greater than previous length of the vector, then the vector is extended with optional values the given function generates. In this case, generated values are appended in order. Otherwise, the vector is shrunk.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.resize_with(2, || Some(0));
assert_eq!(v.as_slice(), &[Some(0), Some(0)]);Sourcepub fn truncate(&mut self, len: usize)
pub fn truncate(&mut self, len: usize)
Shrinks the vector to the given length, and drops abandoned items.
If the given length is greater than or equal to the current length of the vector, nothing takes place.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.resize_with(4, || Some(0));
v.truncate(2);
assert_eq!(v.as_slice(), &[Some(0), Some(0)]);Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Removes vacant slots from the end of the vector, then shrinks capacity of the vector as much as possible.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.resize(5, Some(0));
v.resize(10, None);
v.shrink_to_fit();
assert_eq!(v.num_vacancies(), 0);Sourcepub fn extend_set(&mut self, index: usize, value: T) -> Option<T>
pub fn extend_set(&mut self, index: usize, value: T) -> Option<T>
Sets a slot with the given optional value and returns old value.
Unlike OptVec::set, this method doesn’t panic even if the index is
out of bounds, extending the vector with vacant slots instead.
§Examples
use my_ecs::ds::OptVec;
use std::hash::RandomState;
let mut v = OptVec::<_, RandomState>::new();
v.extend_set(2, 0);
assert_eq!(v.as_slice(), &[None, None, Some(0)]);Trait Implementations§
Source§impl<T, S> IntoIterator for OptVec<T, S>where
S: BuildHasher,
impl<T, S> IntoIterator for OptVec<T, S>where
S: BuildHasher,
impl<T, S> Resource for OptVec<T, S>
Auto Trait Implementations§
impl<T, S> Freeze for OptVec<T, S>where
S: Freeze,
impl<T, S> RefUnwindSafe for OptVec<T, S>where
S: RefUnwindSafe,
T: RefUnwindSafe,
impl<T, S> Send for OptVec<T, S>
impl<T, S> Sync for OptVec<T, S>
impl<T, S> Unpin for OptVec<T, S>
impl<T, S> UnwindSafe for OptVec<T, S>where
S: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more