NEIndexMap

Struct 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>

Source

pub fn capacity(&self) -> NonZeroUsize

Returns the number of elements the map can hold without reallocating.

Source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

Source

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().

Source

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().

Source

pub fn nonempty_iter(&self) -> Iter<'_, K, V>

An iterator visiting all elements in their order.

Source

pub fn nonempty_iter_mut(&mut self) -> IterMut<'_, K, V>

An iterator visiting all elements in their order.

Source

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);
Source

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());
Source

pub const fn is_empty(&self) -> bool

👎Deprecated: A NEIndexMap is never empty.

A NEIndexMap is never empty.

Source

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<_>>());
Source

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

pub fn first(&self) -> (&K, &V)

Get the first element. Never fails.

Source

pub fn last(&self) -> (&K, &V)

Get the last element. Never fails.

Source§

impl<K, V> NEIndexMap<K, V>
where K: Eq + Hash,

Source

pub fn new(k: K, v: V) -> Self

Creates a new NEIndexMap with a single element.

Source

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>
where K: Eq + Hash, S: BuildHasher,

Source

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));
Source

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

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"));
Source

pub fn get<Q>(&self, k: &Q) -> Option<&V>
where Q: Hash + Equivalent<K> + ?Sized,

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"));
Source

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

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"));
Source

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

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"));
Source

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

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"));
Source

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<_>>()
);
Source

pub fn shrink_to_fit(&mut self)

Shrink the capacity of the map as much as possible.

Source

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.

Source

pub fn with_hasher(hasher: S, k: K, v: V) -> NEIndexMap<K, V, S>

Source

pub fn swap_indices(&mut self, a: usize, b: usize)

Swaps the position of two key-value pairs in the map.

§Panics

If a or b are out of bounds.

Trait Implementations§

Source§

impl<K, V, S> AsMut<IndexMap<K, V, S>> for NEIndexMap<K, V, S>

Source§

fn as_mut(&mut self) -> &mut IndexMap<K, V, S>

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<K, V, S> AsRef<IndexMap<K, V, S>> for NEIndexMap<K, V, S>

Source§

fn as_ref(&self) -> &IndexMap<K, V, S>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<K: Clone, V: Clone, S: Clone> Clone for NEIndexMap<K, V, S>

Source§

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

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 for NEIndexMap<K, V, S>

Source§

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

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

impl<K, V> Extend<(K, V)> for NEIndexMap<K, V>
where K: Eq + Hash,

Source§

fn extend<I: IntoIterator<Item = (K, V)>>(&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<K, V, S> From<NEIndexMap<K, V, S>> for IndexMap<K, V, S>
where K: Eq + Hash, S: BuildHasher,

Source§

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>
where K: Eq + Hash, S: BuildHasher + Default,

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) -> Self
where I: IntoNonEmptyIterator<Item = (K, V)>,

Creates a value from a NonEmptyIterator.
Source§

impl<K, V> Index<usize> for NEIndexMap<K, V>

Source§

type Output = V

The returned type after indexing.
Source§

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

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

impl<'a, K, V, S> IntoIterator for &'a NEIndexMap<K, V, S>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a mut NEIndexMap<K, V, S>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for NEIndexMap<K, V, S>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a, K, V, S> IntoNonEmptyIterator for &'a NEIndexMap<K, V, S>

Source§

type IntoNEIter = Iter<'a, K, V>

Which kind of NonEmptyIterator are we turning this into?
Source§

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

impl<K, V, S> IntoNonEmptyIterator for NEIndexMap<K, V, S>

Source§

type IntoNEIter = IntoIter<K, V>

Which kind of NonEmptyIterator are we turning this into?
Source§

fn into_nonempty_iter(self) -> Self::IntoNEIter

Creates a NonEmptyIterator from a value.
Source§

impl<K, V, S> PartialEq for NEIndexMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

Source§

fn eq(&self, other: &Self) -> 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<K, V> Singleton for NEIndexMap<K, V>
where K: Eq + Hash,

Source§

fn singleton((k, v): Self::Item) -> Self

use nonempty_collections::{NEIndexMap, Singleton, ne_indexmap};

let m = NEIndexMap::singleton(('a', 1));
assert_eq!(ne_indexmap!['a' => 1], m);
Source§

type Item = (K, V)

Source§

impl<K, V, S> Eq for NEIndexMap<K, V, S>
where K: Eq + Hash, V: Eq, S: BuildHasher,

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>
where S: Send, K: Send, V: Send,

§

impl<K, V, S> Sync for NEIndexMap<K, V, S>
where S: Sync, K: Sync, V: Sync,

§

impl<K, V, S> Unpin for NEIndexMap<K, V, S>
where S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, S> UnwindSafe for NEIndexMap<K, V, S>
where S: UnwindSafe, K: 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> IntoIteratorExt for T
where T: IntoIterator,

Source§

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

The type of the elements being iterated over.
Source§

type IntoIter = NonEmptyIterAdapter<Peekable<<T as IntoIterator>::IntoIter>>

Which kind of NonEmptyIterator are we turning this into?
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.