Struct FixedIndexVec

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

A fixed-size indexed vector that maps indices to values.

It provides a fixed-size vector-like data structure that can store values based on its associated index. Each value is associated with a unique index in the map. The values can be accessed, inserted, and removed using the index as the identifier.

§Examples

use fixed_index_vec::FixedIndexVec;

let mut vec = FixedIndexVec::new();

vec.insert("value1".to_string());
vec.insert("value2".to_string());

assert_eq!(vec.get(0), Some(&"value1".to_string()));
assert_eq!(vec.get(1), Some(&"value2".to_string()));

vec.remove(1);

assert_eq!(vec.get(1), None);

§Notes

  • The FixedIndexVec is backed by a BTreeMap, so it is not as fast as a Vec.
  • Index notations are supported (eg. vec[0]), however, accessing an index that does not exist will panic.

Implementations§

Source§

impl<T> FixedIndexVec<T>

Source

pub fn new() -> FixedIndexVec<T>

Creates an empty FixedIndexVec.

The internal storage will not allocate until elements are pushed onto it.

§Examples
use fixed_index_vec::FixedIndexVec;
let mut vec: FixedIndexVec<i32> = FixedIndexVec::new();
Source

pub fn push(&mut self, value: T)

Inserts an element at the end of the FixedIndexVec.

§Panics

Panics if the FixedIndexVec is at capacity.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec = FixedIndexVec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
Source

pub fn insert(&mut self, value: T)

Alias for push. Inserts an element at the end of the FixedIndexVec.

§Panics

Panics if the FixedIndexVec is at capacity.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec = FixedIndexVec::new();
vec.insert(1);
vec.insert(2);
assert_eq!(vec[0], 1);
assert_eq!(vec[1], 2);
Source

pub fn remove(&mut self, index: usize) -> Option<T>

Removes the element at the given index, if it exists, returning it or None if it does not exist.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec = FixedIndexVec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec.remove(0), Some(1));
assert_eq!(vec.remove(0), None);
§Notes

Unlike Vec::remove, this does not shift elements after the removed element. If index >= length, this returns None, the same as if the element did not exist.

Source

pub fn get(&self, index: usize) -> Option<&T>

Returns a reference to the element at the given index, if it exists, or None if it does not exist.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec = FixedIndexVec::new();
vec.push(1);
vec.push(2);
assert_eq!(vec.get(0), Some(&1));
assert_eq!(vec.get(2), None);
Source

pub fn iter(&self) -> impl Iterator<Item = (usize, &T)>

An iterator visiting all elements in ascending order of their indices. The index is returned along with the value. The iterator skips indices that do not have a corresponding value.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(1);
let mut iter = vec.iter();
assert_eq!(iter.next(), Some((0, &1)));
assert_eq!(iter.next(), Some((2, &3)));
assert_eq!(iter.next(), None);
Source

pub fn len(&self) -> usize

Returns the number of elements in the FixedIndexVec. This is not the same as the value of the largest index, unless no elements have been removed.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(1);
assert_eq!(vec.len(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the FixedIndexVec contains no elements.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(1);
assert_eq!(vec.is_empty(), false);
use fixed_index_vec::FixedIndexVec;
let vec: FixedIndexVec<i32> = FixedIndexVec::new();
assert_eq!(vec.is_empty(), true);
Source

pub fn clear(&mut self)

Clears the FixedIndexVec, removing all values. Keeps the allocated memory for reuse. This is equivalent to calling remove on every index. The next index will not be reset to 0.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.clear();
assert_eq!(vec.len(), 0);
assert_eq!(vec.next_index(), 3);
Source

pub fn reset(&mut self)

Clears the FixedIndexVec, removing all values and resetting the next index to 0. Keeps the allocated memory for reuse.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.reset();
assert_eq!(vec.len(), 0);
assert_eq!(vec.next_index(), 0);
Source

pub fn next_index(&self) -> usize

Returns the next index that will be used when inserting an element.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(1);
assert_eq!(vec.next_index(), 3);
Source

pub fn first(&self) -> Option<(usize, &T)>

Returns the index and a reference to the element at the smallest populated index, or None if the FixedIndexVec is empty.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(0);
assert_eq!(vec.first(), Some((1, &2)));
use fixed_index_vec::FixedIndexVec;

let vec: FixedIndexVec<i32> = FixedIndexVec::new();
assert_eq!(vec.first(), None);
Source

pub fn last(&self) -> Option<(usize, &T)>

Returns the index and a reference to the element at the largest populated index, or None if the FixedIndexVec is empty.

§Examples
use fixed_index_vec::FixedIndexVec;

let mut vec: FixedIndexVec<i32> = vec![1, 2, 3].into();
vec.remove(2);
assert_eq!(vec.last(), Some((1, &2)));
use fixed_index_vec::FixedIndexVec;

let vec: FixedIndexVec<i32> = FixedIndexVec::new();
assert_eq!(vec.last(), None);

Trait Implementations§

Source§

impl<T: Clone> Clone for FixedIndexVec<T>

Source§

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

Returns a copy 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> Debug for FixedIndexVec<T>

Source§

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

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

impl<T: Default> Default for FixedIndexVec<T>

Source§

fn default() -> FixedIndexVec<T>

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

impl<T: Display> Display for FixedIndexVec<T>

Source§

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

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

impl<T> Extend<T> for FixedIndexVec<T>

Source§

fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, A> From<BTreeMap<A, T>> for FixedIndexVec<T>

Source§

fn from(map: BTreeMap<A, T>) -> FixedIndexVec<T>

Converts to this type from the input type.
Source§

impl<T, A> From<HashMap<A, T>> for FixedIndexVec<T>

Source§

fn from(map: HashMap<A, T>) -> FixedIndexVec<T>

Converts to this type from the input type.
Source§

impl<T> From<Vec<T>> for FixedIndexVec<T>

Source§

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

Converts to this type from the input type.
Source§

impl<T> FromIterator<T> for FixedIndexVec<T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for FixedIndexVec<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Index<usize> for FixedIndexVec<T>

Source§

type Output = T

The returned type after indexing.
Source§

fn index(&self, index: usize) -> &T

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

impl<T: Ord> Ord for FixedIndexVec<T>

Source§

fn cmp(&self, other: &FixedIndexVec<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for FixedIndexVec<T>

Source§

fn eq(&self, other: &FixedIndexVec<T>) -> 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: PartialOrd> PartialOrd for FixedIndexVec<T>

Source§

fn partial_cmp(&self, other: &FixedIndexVec<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Eq> Eq for FixedIndexVec<T>

Source§

impl<T> StructuralPartialEq for FixedIndexVec<T>

Auto Trait Implementations§

§

impl<T> Freeze for FixedIndexVec<T>

§

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

§

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

§

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

§

impl<T> Unpin for FixedIndexVec<T>

§

impl<T> UnwindSafe for FixedIndexVec<T>
where T: RefUnwindSafe,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.