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,
impl<K, V, S> IndexedMap<K, V, S, true>where
S: BuildHasher,
Sourcepub fn get_key(&self, index: usize) -> Option<&K>
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>
impl<K, V, S> IndexedMap<K, V, S, true>
Sourcepub fn remove(&mut self, index: usize) -> Option<V>
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"));Sourcepub fn remove_entry(&mut self, index: usize) -> Option<(K, V)>
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,
impl<K, V, S, const IMAP: bool> IndexedMap<K, V, S, IMAP>where
S: Default,
Source§impl<K, V, S, const IMAP: bool> IndexedMap<K, V, S, IMAP>
impl<K, V, S, const IMAP: bool> IndexedMap<K, V, S, IMAP>
Sourcepub fn len(&self) -> usize
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);Sourcepub fn is_empty(&self) -> bool
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());Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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'));Sourcepub fn contains_index(&self, index: usize) -> bool
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));Sourcepub fn next_index<Q>(&self, key: &Q) -> usize
pub fn next_index<Q>(&self, key: &Q) -> usize
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);Sourcepub fn insert(&mut self, key: K, value: V) -> (usize, Option<V>)
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"));Sourcepub fn remove2<Q>(&mut self, key: &Q) -> Option<V>
pub fn remove2<Q>(&mut self, key: &Q) -> Option<V>
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"));Sourcepub fn remove_entry2<Q>(&mut self, key: &Q) -> Option<(K, V)>
pub fn remove_entry2<Q>(&mut self, key: &Q) -> Option<(K, V)>
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")));Sourcepub fn get_index<Q>(&self, key: &Q) -> Option<usize>
pub fn get_index<Q>(&self, key: &Q) -> Option<usize>
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));Sourcepub fn get(&self, index: usize) -> Option<&V>
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"));Sourcepub fn get2<Q>(&self, key: &Q) -> Option<&V>
pub fn get2<Q>(&self, key: &Q) -> Option<&V>
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"));Sourcepub fn get_mut(&mut self, index: usize) -> Option<&mut V>
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"));Sourcepub fn get_mut2<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut2<Q>(&mut self, key: &Q) -> Option<&mut V>
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"));Sourcepub fn iter(&self) -> impl Iterator<Item = (&K, usize, &V)> + Clone
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}");
}Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = (&K, usize, &mut V)>
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}");
}Sourcepub fn values(&self) -> impl Iterator<Item = &V> + Clone
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}");
}Sourcepub fn values_mut(&mut self) -> impl Iterator<Item = &mut V>
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>
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>
fn clone(&self) -> IndexedMap<K, V, S, IMAP>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<'a, K, V, S, const IMAP: bool> From<&'a IndexedMap<K, V, S, IMAP>> for &'a [Option<V>]where
S: BuildHasher,
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
fn from(value: &'a IndexedMap<K, V, S, IMAP>) -> Self
impl<K, V, S, const IMAP: bool> Resource for IndexedMap<K, V, S, IMAP>
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>
impl<K, V, S, const IMAP: bool> Sync for IndexedMap<K, V, S, IMAP>
impl<K, V, S, const IMAP: bool> Unpin for IndexedMap<K, V, S, IMAP>
impl<K, V, S, const IMAP: bool> UnwindSafe for IndexedMap<K, V, S, IMAP>
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