Struct IndexedMap

Source
pub struct IndexedMap<K, V, S, const IMAP: bool = true> { /* private fields */ }
Expand description

A hash map that you can find a value by an index as well.

It’s encouraged to prefer accessing item by an index over using a key because it is simpler in terms of required operations. Plus, values are laid on sequential memory block, so that we can expect faster iteration as well. However, the map allocates more memory than usual hash map.

If you want to use an index as key interchangeably, then set IMAP to true. Then, the map keeps index->key relation as well so that the map can find a corresponding key from an index.

Implementations§

Source§

impl<K, V, S> IndexedMap<K, V, S, true>
where S: BuildHasher,

Source

pub fn get_key(&self, index: usize) -> Option<&K>

Retrieves a shared reference to a key corresponding to the given index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get_key(index), Some(&'a'));
Source§

impl<K, V, S> IndexedMap<K, V, S, true>
where K: Hash + Eq + Clone, S: BuildHasher,

Source

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

Removes a value at the given index then returns it.

Consider using IndexedMap::remove2 if you need to remove a value by a key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.remove(index), Some("alpha"));
Source

pub fn remove_entry(&mut self, index: usize) -> Option<(K, V)>

Removes a value at the given index then returns it with the corresponding key.

Consider using IndexedMap::remove_entry2 if you need to remove a value by a key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.remove_entry(index), Some(('a', "alpha")));
Source§

impl<K, V, S, const IMAP: bool> IndexedMap<K, V, S, IMAP>
where S: Default,

Source

pub fn new() -> Self

Creates a new empty map.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let map = IndexedMap::<char, &'static str, RandomState>::new();
Source§

impl<K, V, S, const IMAP: bool> IndexedMap<K, V, S, IMAP>
where K: Hash + Eq + Clone, S: BuildHasher,

Source

pub fn len(&self) -> usize

Returns number of items.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
assert_eq!(map.len(), 1);
Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let map = IndexedMap::<char, &'static str, RandomState>::new();
assert!(map.is_empty());
Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns true if the map contains the given key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
assert!(map.contains_key(&'a'));
Source

pub fn contains_index(&self, index: usize) -> bool

Returns true if the map contains value corresponding to the given index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert!(map.contains_index(index));
Source

pub fn next_index<Q>(&self, key: &Q) -> usize
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Returns the next index that will be returned on the next call to IndexedMap::insert with the given key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let next_index = map.next_index(&'a');
let (index, _) = map.insert('a', "alpha");
assert_eq!(next_index, index);
Source

pub fn insert(&mut self, key: K, value: V) -> (usize, Option<V>)

Inserts the given key-value pair into the map and returns corresponding index.

If the map contains the key, then replaces values and returns old value.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
let (_, old) = map.insert('a', "ah");
assert_eq!(old, Some("alpha"));
Source

pub fn remove2<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a value corresponding to the given key then returns it.

Consider using IndexedMap::remove if you need to remove a value by an index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
assert_eq!(map.remove2(&'a'), Some("alpha"));
Source

pub fn remove_entry2<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes a value corresponding to the given key then returns it with the key.

Consider using IndexedMap::remove_entry if you need to remove a value by an index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
assert_eq!(map.remove_entry2(&'a'), Some(('a', "alpha")));
Source

pub fn get_index<Q>(&self, key: &Q) -> Option<usize>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves an index corresponding to the given key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get_index(&'a'), Some(index));
Source

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

Retrieves a shared reference to a value at the given index.

Consider using IndexedMap::get2 if you need to get a value by a key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get(index), Some(&"alpha"));
Source

pub fn get2<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves a shared reference to a value corresponding to the given key.

Consider using IndexedMap::get if you need to get a value by an index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get2(&'a'), Some(&"alpha"));
Source

pub fn get_mut(&mut self, index: usize) -> Option<&mut V>

Retrieves a mutable reference to a value at the given index.

Consider using IndexedMap::get_mut2 if you need to get a value by a key.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get_mut(index), Some(&mut "alpha"));
Source

pub fn get_mut2<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Retrieves a mutable reference to a value corresponding to the given key.

Consider using IndexedMap::get_mut if you need to get a value by an index.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
let (index, _) = map.insert('a', "alpha");
assert_eq!(map.get_mut2(&'a'), Some(&mut "alpha"));
Source

pub fn iter(&self) -> impl Iterator<Item = (&K, usize, &V)> + Clone

Returns an iterator visiting key-index-value pairs in the map in arbitrary order.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
map.insert('b', "beta");
for (key, index, value) in map.iter() {
    println!("{key}, {index}, {value}");
}
Source

pub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, usize, &mut V)>

Returns a mutable iterator visiting key-index-value pairs in the map in arbitrary order.

You can modify values only through the iterator.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha".to_owned());
map.insert('b', "beta".to_owned());
for (key, index, value) in map.iter_mut() {
    value.push('*');
    println!("{key}, {index}, {value}");
}
Source

pub fn values(&self) -> impl Iterator<Item = &V> + Clone

Returns an iterator visiting all values.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha");
map.insert('b', "beta");
for v in map.values() {
    println!("{v}");
}
Source

pub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>

Returns a mutable iterator visiting all values.

§Examples
use my_ecs::ds::IndexedMap;
use std::hash::RandomState;

let mut map = IndexedMap::<_, _, RandomState>::new();
map.insert('a', "alpha".to_owned());
map.insert('b', "beta".to_owned());
for v in map.values_mut() {
    v.push('*');
    println!("{v}");
}

Trait Implementations§

Source§

impl<K: Clone, V: Clone, S: Clone, const IMAP: bool> Clone for IndexedMap<K, V, S, IMAP>

Source§

fn clone(&self) -> IndexedMap<K, V, S, IMAP>

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<K: Debug, V: Debug, S: Debug, const IMAP: bool> Debug for IndexedMap<K, V, S, IMAP>

Source§

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

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

impl<K, V, S, const IMAP: bool> Default for IndexedMap<K, V, S, IMAP>
where S: Default,

Source§

fn default() -> Self

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

impl<'a, K, V, S, const IMAP: bool> From<&'a IndexedMap<K, V, S, IMAP>> for &'a [Option<V>]
where S: BuildHasher,

Source§

fn from(value: &'a IndexedMap<K, V, S, IMAP>) -> Self

Converts to this type from the input type.
Source§

impl<K, V, S, const IMAP: bool> Resource for IndexedMap<K, V, S, IMAP>
where K: Send + 'static, V: Send + 'static, S: Send + 'static,

Auto Trait Implementations§

§

impl<K, V, S, const IMAP: bool> Freeze for IndexedMap<K, V, S, IMAP>
where S: Freeze,

§

impl<K, V, S, const IMAP: bool> RefUnwindSafe for IndexedMap<K, V, S, IMAP>

§

impl<K, V, S, const IMAP: bool> Send for IndexedMap<K, V, S, IMAP>
where S: Send, K: Send, V: Send,

§

impl<K, V, S, const IMAP: bool> Sync for IndexedMap<K, V, S, IMAP>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S, const IMAP: bool> Unpin for IndexedMap<K, V, S, IMAP>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S, const IMAP: bool> UnwindSafe for IndexedMap<K, V, S, IMAP>
where K: UnwindSafe, S: UnwindSafe, V: 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.