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) -> NonZeroUsize
pub fn capacity(&self) -> NonZeroUsize
Returns the number of elements the map can hold without reallocating.
Sourcepub fn iter(&self) -> Iter<'_, K, V>
pub fn iter(&self) -> Iter<'_, K, V>
Returns a regular iterator over the entries in this non-empty index map.
For a NonEmptyIterator see Self::nonempty_iter().
Sourcepub fn iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn iter_mut(&mut self) -> IterMut<'_, K, V>
Returns a regular mutable iterator over the entries in this non-empty index map.
For a NonEmptyIterator see Self::nonempty_iter_mut().
Sourcepub fn nonempty_iter(&self) -> Iter<'_, K, V>
pub fn nonempty_iter(&self) -> Iter<'_, K, V>
An iterator visiting all elements in their order.
Sourcepub fn nonempty_iter_mut(&mut self) -> IterMut<'_, K, V>
pub fn nonempty_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(capacity: NonZeroUsize, k: K, v: V) -> NEIndexMap<K, V>
pub fn with_capacity(capacity: NonZeroUsize, k: K, v: V) -> NEIndexMap<K, V>
Creates a new NEIndexMap with a single element and specified
heap capacity.
Source§impl<K, V, S> NEIndexMap<K, V, S>
impl<K, V, S> NEIndexMap<K, V, S>
Sourcepub fn try_from_map(map: IndexMap<K, V, S>) -> Option<Self>
pub fn try_from_map(map: IndexMap<K, V, S>) -> Option<Self>
Attempt a conversion from IndexMap, consuming the given IndexMap.
Will return None if the IndexMap is empty.
use indexmap::*;
use nonempty_collections::*;
assert_eq!(
Some(ne_indexmap! {"a" => 1, "b" => 2}),
NEIndexMap::try_from_map(indexmap! {"a" => 1, "b" => 2})
);
let m: IndexMap<(), ()> = indexmap! {};
assert_eq!(None, NEIndexMap::try_from_map(m));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(
capacity: NonZeroUsize,
hasher: S,
k: K,
v: V,
) -> NEIndexMap<K, V, S>
pub fn with_capacity_and_hasher( capacity: NonZeroUsize, hasher: S, k: K, v: V, ) -> NEIndexMap<K, V, S>
Creates a new NEIndexMap with a single element and specified
heap capacity and hasher.
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, V, S> AsMut<IndexMap<K, V, S>> for NEIndexMap<K, V, S>
impl<K, V, S> AsMut<IndexMap<K, V, S>> for NEIndexMap<K, V, S>
Source§impl<K, V, S> AsRef<IndexMap<K, V, S>> for NEIndexMap<K, V, S>
impl<K, V, S> AsRef<IndexMap<K, V, S>> for NEIndexMap<K, V, S>
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> Extend<(K, V)> for NEIndexMap<K, V>
impl<K, V> Extend<(K, V)> for NEIndexMap<K, V>
Source§fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (K, V)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§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 indexmap::IndexMap;
use nonempty_collections::*;
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>
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' => 4, 'b' => 2, 'c' => 3};
assert_eq!(m0, m1);
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' => 4, '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<'a, K, V, S> IntoIterator for &'a NEIndexMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a NEIndexMap<K, V, S>
Source§impl<'a, K, V, S> IntoIterator for &'a mut NEIndexMap<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut NEIndexMap<K, V, S>
Source§impl<K, V, S> IntoIterator for NEIndexMap<K, V, S>
impl<K, V, S> IntoIterator for NEIndexMap<K, V, S>
Source§impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEIndexMap<K, V, S>
impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEIndexMap<K, V, S>
Source§type IntoNEIter = Iter<'a, K, V>
type IntoNEIter = Iter<'a, K, V>
NonEmptyIterator are we turning this into?Source§fn into_nonempty_iter(self) -> Self::IntoNEIter
fn into_nonempty_iter(self) -> Self::IntoNEIter
NonEmptyIterator from a value.Source§impl<K, V, S> IntoNonEmptyIterator for NEIndexMap<K, V, S>
impl<K, V, S> IntoNonEmptyIterator for NEIndexMap<K, V, S>
Source§type IntoNEIter = IntoIter<K, V>
type IntoNEIter = IntoIter<K, V>
NonEmptyIterator are we turning this into?Source§fn into_nonempty_iter(self) -> Self::IntoNEIter
fn into_nonempty_iter(self) -> Self::IntoNEIter
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>
Source§impl<K, V> Singleton for NEIndexMap<K, V>
impl<K, V> Singleton for NEIndexMap<K, V>
impl<K, V, S> Eq for NEIndexMap<K, V, S>
Auto Trait Implementations§
impl<K, V, S> Freeze for NEIndexMap<K, V, S>where
S: Freeze,
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<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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.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 moreSource§impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
impl<T> IntoIteratorExt for Twhere
T: IntoIterator,
Source§fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
fn try_into_nonempty_iter(self) -> Option<<T as IntoIteratorExt>::IntoIter>
Converts self into a non-empty iterator or returns None if
the iterator is empty.
Source§type Item = <T as IntoIterator>::Item
type Item = <T as IntoIterator>::Item
Source§type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>
type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>
NonEmptyIterator are we turning this into?