IdVec

Struct IdVec 

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

Inserting elements into this map yields a persistent, type-safe Index to that new element. It does not try to preserve the order of the inserted items.

The IdVec does not actively try to preserve order of inserted elements, but a packed IdVec will append elements to the end of the internal vector.

Implementations§

Source§

impl<T> IdVec<T>

Source

pub fn new() -> Self

Does not allocate heap memory

Source

pub fn with_capacity(capacity: usize) -> Self

Source

pub fn from_vec(elements: Vec<T>) -> Self

Create a map containing these elements. Directly uses the specified vector, so no heap allocation is made calling this function.

Source

pub fn len(&self) -> usize

Source

pub fn id_index_limit(&self) -> usize

Used to estimate the maximal index_value() of all ids inside this IdVec. This IdVec will not contain an id with an index value greater than or equal to this value.

Source

pub fn capacity(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn contains_id(&self, element: Id<T>) -> bool

Excludes deleted elements, and indices out of range

Source

pub fn is_packed(&self) -> bool

Returns if the internal vector does not contain any deleted elements

Source

pub fn remove(&mut self, element: Id<T>)

Enable the specified id to be overwritten when a new element is inserted. This does not directly deallocate the element. Make sure that no ids pointing to that element exist after this call. Ignores invalid and deleted ids.

Source

pub fn pop(&mut self) -> Option<(Id<T>, T)>

Removes an id and the associated element. See pop_element for more information.

Source

pub fn pop_element(&mut self) -> Option<T>

Removes an element from this map, returns the element: Removes the one element which is the least work to remove, the one with the highest id. May deallocate unused elements. Returns None if this map is empty.

Source

pub fn insert(&mut self, element: T) -> Id<T>

Associate the specified element with a currently unused id. This may overwrite (thus drop) unused elements.

Source

pub fn get(&self, element: Id<T>) -> Option<&T>

Return a reference to the element that this id points to

Source

pub fn get_mut<'s>(&'s mut self, element: Id<T>) -> Option<&'s mut T>

Return a mutable reference to the element that this id points to

Source

pub fn swap_elements(&mut self, id1: Id<T>, id2: Id<T>)

Swap the elements pointed to. Panic on invalid Id parameter.

Source

pub fn clear(&mut self)

Removes all elements, instantly deallocating

Source

pub fn shrink_to_fit(&mut self)

Shrinks the internal vector itself

Source

pub fn reserve(&mut self, additional: usize)

Reserve space for more elements, avoiding frequent reallocation

Source

pub fn retain<F>(&mut self, predicate: F)
where F: Fn(Id<T>, &T) -> bool,

Retain only the elements specified by the predicate. May deallocate unused elements.

Source

pub fn pack<F>(&mut self, remap: F)
where F: FnMut(Id<T>, Id<T>),

Make this map have a continuous flow of indices, having no wasted allocation and calling remap(old_id, new_id) for every element that has been moved to a new Id It does not preserve order of the inserted items.

Source

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

Used for immutable access to ids and elements

Source

pub fn into_elements(self) -> IntoElements<T>

Iterate over the elements, consuming this IdVec

Source

pub fn drain_elements(&mut self) -> DrainElements<'_, T>

Iterate over the elements, clearing this IdVec

Source

pub fn elements<'s>(&'s self) -> ElementIter<'s, T>

Used for immutable direct access to all used elements

Source

pub fn ids<'s>(&'s self) -> IdIter<'s, T>

Used for immutable indirect access

Source

pub fn get_ids(&self) -> OwnedIdIter<T>

Used for full mutable access, while allowing inserting and deleting while iterating. The iterator will keep an independent state, in order to un-borrow the underlying map. This may be more expensive than iter, because it needs to clone the internal set of unused ids.

Source

pub fn ids_eq(&self, other: &Self) -> bool

Compares if two id-maps contain the same ids, ignoring elements. Complexity of O(n)

Source

pub fn elements_eq(&self, other: &Self) -> bool
where T: PartialEq,

Compares if two id-maps contain the same elements, ignoring ids. Worst case complexity of O(n^2)

Source

pub fn contains_element(&self, element: &T) -> bool
where T: PartialEq,

Worst case complexity of O(n)

Source

pub fn find_id_of_element(&self, element: &T) -> Option<Id<T>>
where T: PartialEq,

Worst case complexity of O(n)

Trait Implementations§

Source§

impl<T: Clone> Clone for IdVec<T>

Source§

fn clone(&self) -> IdVec<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Debug for IdVec<T>
where T: Debug,

Source§

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

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

impl<T: Default> Default for IdVec<T>

Source§

fn default() -> IdVec<T>

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

impl<T> From<Vec<T>> for IdVec<T>

Source§

fn from(vec: Vec<T>) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for IdVec<T>

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<T> Index<Id<T>> for IdVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, element: Id<T>) -> &T

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

impl<T> IndexMut<Id<T>> for IdVec<T>

Source§

fn index_mut(&mut self, element: Id<T>) -> &mut T

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

impl<T> IntoIterator for IdVec<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = IntoElements<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> PartialEq for IdVec<T>
where T: PartialEq,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Eq for IdVec<T>
where T: Eq,

Equality means: The same Ids pointing to the same elements, ignoring deleted elements. Complexity of O(n)

Auto Trait Implementations§

§

impl<T> Freeze for IdVec<T>

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for IdVec<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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.