Struct nonempty_collections::index_map::NEIndexMap
source · pub struct NEIndexMap<K, V, S = RandomState> { /* private fields */ }
Expand description
A non-empty, growable IndexMap
.
Unlike HashMap
and crate::NEMap
, these feature a predictable iteration
order.
use nonempty_collections::*;
let m = ne_indexmap!{"Netherlands" => 18, "Canada" => 40};
assert_eq!(2, m.len().get());
Implementations§
source§impl<K, V, S> NEIndexMap<K, V, S>
impl<K, V, S> NEIndexMap<K, V, S>
sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of elements the map can hold without reallocating.
sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
An iterator visiting all elements in their order.
sourcepub fn keys(&self) -> Keys<'_, K, V>
pub fn keys(&self) -> Keys<'_, K, V>
An iterator visiting all keys in arbitrary order. The iterator element
type is &'a K
.
use nonempty_collections::*;
let m = ne_indexmap!{"Duke" => "Leto", "Doctor" => "Yueh", "Planetologist" => "Kynes"};
let v = m.keys().collect::<NEVec<_>>();
assert_eq!(nev![&"Duke", &"Doctor", &"Planetologist"], v);
sourcepub fn len(&self) -> NonZeroUsize
pub fn len(&self) -> NonZeroUsize
Returns the number of elements in the map. Always 1 or more.
use nonempty_collections::*;
let m = ne_indexmap!{"a" => 1, "b" => 2};
assert_eq!(2, m.len().get());
sourcepub const fn is_empty(&self) -> bool
👎Deprecated: A NEIndexMap is never empty.
pub const fn is_empty(&self) -> bool
A NEIndexMap
is never empty.
sourcepub fn values(&self) -> Values<'_, K, V>
pub fn values(&self) -> Values<'_, K, V>
An iterator visiting all values in order.
use nonempty_collections::*;
let m = ne_indexmap!["Caladan" => "Atreides", "Giedi Prime" => "Harkonnen", "Kaitain" => "Corrino"];
assert_eq!(vec![&"Atreides", &"Harkonnen", &"Corrino"], m.values().collect::<Vec<_>>());
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V>
Return an iterator visiting all mutable values in order.
use nonempty_collections::*;
let mut m = ne_indexmap![0 => "Fremen".to_string(), 1 => "Crysknife".to_string(), 2 => "Water of Life".to_string()];
m.values_mut().into_iter().for_each(|v| v.truncate(3));
assert_eq!(vec![&mut "Fre".to_string(), &mut "Cry".to_string(),&mut "Wat".to_string()], m.values_mut().collect::<Vec<_>>());
source§impl<K, V> NEIndexMap<K, V>
impl<K, V> NEIndexMap<K, V>
sourcepub fn with_capacity(heap_capacity: usize, k: K, v: V) -> NEIndexMap<K, V>
pub fn with_capacity(heap_capacity: usize, k: K, v: V) -> NEIndexMap<K, V>
Creates a new NEIndexMap
with a single element and specified
heap capacity.
Note that the effective capacity of this map is always heap_capacity + 1
because the first element is stored inline.
source§impl<K, V, S> NEIndexMap<K, V, S>
impl<K, V, S> NEIndexMap<K, V, S>
sourcepub fn contains_key<Q>(&self, k: &Q) -> bool
pub fn contains_key<Q>(&self, k: &Q) -> bool
Returns true if the map contains a value.
use nonempty_collections::*;
let m = ne_indexmap!{"Paul" => ()};
assert!(m.contains_key("Paul"));
assert!(!m.contains_key("Atreides"));
sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>
pub fn get<Q>(&self, k: &Q) -> Option<&V>
Return a reference to the value stored for key
, if it is present,
else None
.
use nonempty_collections::*;
let m = ne_indexmap!{"Arrakis" => 3};
assert_eq!(Some(&3), m.get("Arrakis"));
assert_eq!(None, m.get("Caladan"));
sourcepub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
Return references to the key-value pair stored for key
,
if it is present, else None
.
use nonempty_collections::*;
let m = ne_indexmap!{"Year" => 1963, "Pages" => 896};
assert_eq!(Some((&"Year", &1963)), m.get_key_value(&"Year"));
assert_eq!(Some((&"Pages", &896)), m.get_key_value(&"Pages"));
assert_eq!(None, m.get_key_value(&"Title"));
sourcepub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Return a mutable reference to the value stored for key
, if it is
present, else None
.
use nonempty_collections::*;
let mut m = ne_indexmap!{"Mentat" => 3, "Bene Gesserit" => 14};
let v = m.get_mut(&"Mentat");
assert_eq!(Some(&mut 3), v);
*v.unwrap() += 1;
assert_eq!(Some(&mut 4), m.get_mut(&"Mentat"));
let v = m.get_mut(&"Bene Gesserit");
assert_eq!(Some(&mut 14), v);
*v.unwrap() -= 1;
assert_eq!(Some(&mut 13), m.get_mut(&"Bene Gesserit"));
assert_eq!(None, m.get_mut(&"Sandworm"));
sourcepub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
Return item index, if it exists in the map.
use nonempty_collections::*;
let m = ne_indexmap!{"Title" => "Dune", "Author" => "Frank Herbert", "Language" => "English"};
assert_eq!(Some(0), m.get_index_of(&"Title"));
assert_eq!(Some(1), m.get_index_of(&"Author"));
assert_eq!(None, m.get_index_of(&"Genre"));
sourcepub fn insert(&mut self, k: K, v: V) -> Option<V>
pub fn insert(&mut self, k: K, v: V) -> Option<V>
Insert a key-value pair into the map.
If an equivalent key already exists in the map: the key remains and
retains in its place in the order, its corresponding value is updated
with value
, and the older value is returned inside Some(_)
.
If no equivalent key existed in the map: the new key-value pair is
inserted, last in order, and None
is returned.
use nonempty_collections::*;
let mut m = ne_indexmap!{"Duke" => "Leto", "Doctor" => "Yueh"};
assert_eq!(None, m.insert("Lady", "Jessica"));
assert_eq!(vec!["Duke", "Doctor", "Lady"], m.keys().copied().collect::<Vec<_>>());
// Spoiler alert: there is a different duke at some point
assert_eq!(Some("Leto"), m.insert("Duke", "Paul"));
assert_eq!(vec!["Paul", "Yueh", "Jessica"], m.values().copied().collect::<Vec<_>>());
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the capacity of the map as much as possible.
sourcepub fn with_capacity_and_hasher(
heap_capacity: usize,
hasher: S,
k: K,
v: V
) -> NEIndexMap<K, V, S>
pub fn with_capacity_and_hasher( heap_capacity: usize, hasher: S, k: K, v: V ) -> NEIndexMap<K, V, S>
Creates a new NEIndexMap
with a single element and specified
heap capacity and hasher.
Note that the effective capacity of this map is always heap_capacity + 1
because the first element is stored inline.
sourcepub fn with_hasher(hasher: S, k: K, v: V) -> NEIndexMap<K, V, S>
pub fn with_hasher(hasher: S, k: K, v: V) -> NEIndexMap<K, V, S>
sourcepub fn swap_indices(&mut self, a: usize, b: usize)
pub fn swap_indices(&mut self, a: usize, b: usize)
Trait Implementations§
source§impl<K: Clone, V: Clone, S: Clone> Clone for NEIndexMap<K, V, S>
impl<K: Clone, V: Clone, S: Clone> Clone for NEIndexMap<K, V, S>
source§fn clone(&self) -> NEIndexMap<K, V, S>
fn clone(&self) -> NEIndexMap<K, V, S>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<K, V, S> From<NEIndexMap<K, V, S>> for IndexMap<K, V, S>
impl<K, V, S> From<NEIndexMap<K, V, S>> for IndexMap<K, V, S>
source§fn from(m: NEIndexMap<K, V, S>) -> Self
fn from(m: NEIndexMap<K, V, S>) -> Self
use nonempty_collections::*;
use indexmap::IndexMap;
let m: IndexMap<&str, usize> = ne_indexmap!{"population" => 1000}.into();
assert!(m.contains_key("population"));
source§impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEIndexMap<K, V, S>
impl<K, V, S> FromNonEmptyIterator<(K, V)> for NEIndexMap<K, V, S>
use nonempty_collections::*;
let v = nev![('a', 1), ('b', 2), ('c', 3), ('a', 4)];
let m0 = v.into_nonempty_iter().collect::<NEIndexMap<_, _>>();
let m1 = ne_indexmap!{'a' => 1, 'b' => 2, 'c' => 3};
assert_eq!(m0, m1);
source§fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = (K, V)>,
fn from_nonempty_iter<I>(iter: I) -> Selfwhere
I: IntoNonEmptyIterator<Item = (K, V)>,
NonEmptyIterator
.source§impl<K, V> Index<usize> for NEIndexMap<K, V>
impl<K, V> Index<usize> for NEIndexMap<K, V>
source§impl<K, V, S> IntoNonEmptyIterator for NEIndexMap<K, V, S>
impl<K, V, S> IntoNonEmptyIterator for NEIndexMap<K, V, S>
§type IntoIter = Chain<Once<(K, V)>, IntoIter<K, V>>
type IntoIter = Chain<Once<(K, V)>, IntoIter<K, V>>
NonEmptyIterator
are we turning this into?source§fn into_nonempty_iter(self) -> Self::IntoIter
fn into_nonempty_iter(self) -> Self::IntoIter
NonEmptyIterator
from a value.source§impl<K, V, S> PartialEq for NEIndexMap<K, V, S>
impl<K, V, S> PartialEq for NEIndexMap<K, V, S>
impl<K, V, S> Eq for NEIndexMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for NEIndexMap<K, V, S>
impl<K, V, S> RefUnwindSafe for NEIndexMap<K, V, S>
impl<K, V, S> Send for NEIndexMap<K, V, S>
impl<K, V, S> Sync for NEIndexMap<K, V, S>
impl<K, V, S> Unpin for NEIndexMap<K, V, S>
impl<K, V, S> UnwindSafe for NEIndexMap<K, V, S>
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.