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) -> usize

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>

An iterator visiting all elements in their order.

source

pub fn 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 const 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>

source

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

Creates a new NEIndexMap with a single element.

source

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

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

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

source§

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

Returns a copy 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, 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 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>
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' => 1, '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>

§

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

§

type Item = (K, V)

The type of the elements being iterated over.
§

type IntoIter = Chain<Once<(K, V)>, IntoIter<K, V>>

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

fn into_nonempty_iter(self) -> Self::IntoIter

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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 K: Freeze, V: Freeze, S: Freeze,

§

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

§

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

§

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

§

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

§

impl<K, V, S> UnwindSafe for NEIndexMap<K, V, S>
where K: UnwindSafe, V: UnwindSafe, S: 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<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> ToOwned for T
where T: Clone,

§

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

§

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

§

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.