Struct cml_cip36::OrderedHashMap

source ·
pub struct OrderedHashMap<K, V>(/* private fields */)
where
    K: Hash + Eq + Ord;

Implementations§

source§

impl<K, V> OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

source

pub fn new() -> OrderedHashMap<K, V>

source

pub fn take(self) -> LinkedHashMap<K, V>

Methods from Deref<Target = LinkedHashMap<K, V>>§

source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional more elements to be inserted into the map. The map may reserve more space to avoid frequent allocations.

§Panics

Panics if the new allocation size overflows usize.

source

pub fn shrink_to_fit(&mut self)

Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.

source

pub fn entry(&mut self, k: K) -> Entry<'_, K, V, S>

Gets the given key’s corresponding entry in the map for in-place manipulation.

§Examples
use linked_hash_map::LinkedHashMap;

let mut letters = LinkedHashMap::new();

for ch in "a short treatise on fungi".chars() {
    let counter = letters.entry(ch).or_insert(0);
    *counter += 1;
}

assert_eq!(letters[&'s'], 2);
assert_eq!(letters[&'t'], 3);
assert_eq!(letters[&'u'], 1);
assert_eq!(letters.get(&'y'), None);
source

pub fn entries(&mut self) -> Entries<'_, K, V, S>

Returns an iterator visiting all entries in insertion order. Iterator element type is OccupiedEntry<K, V, S>. Allows for removal as well as replacing the entry.

§Examples
use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

{
    let mut iter = map.entries();
    let mut entry = iter.next().unwrap();
    assert_eq!(&"a", entry.key());
    *entry.get_mut() = 17;
}

assert_eq!(&17, map.get(&"a").unwrap());
source

pub fn insert(&mut self, k: K, v: V) -> Option<V>

Inserts a key-value pair into the map. If the key already existed, the old value is returned.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
assert_eq!(map[&1], "a");
assert_eq!(map[&2], "b");
source

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

Checks if the map contains the given key.

source

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

Returns the value corresponding to the key in the map.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
map.insert(2, "c");
map.insert(3, "d");

assert_eq!(map.get(&1), Some(&"a"));
assert_eq!(map.get(&2), Some(&"c"));
source

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

Returns the mutable reference corresponding to the key in the map.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");

*map.get_mut(&1).unwrap() = "c";
assert_eq!(map.get(&1), Some(&"c"));
source

pub fn get_refresh<Q>(&mut self, k: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Returns the value corresponding to the key in the map.

If value is found, it is moved to the end of the list. This operation can be used in implemenation of LRU cache.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(1, "a");
map.insert(2, "b");
map.insert(3, "d");

assert_eq!(map.get_refresh(&2), Some(&mut "b"));

assert_eq!((&2, &"b"), map.iter().rev().next().unwrap());
source

pub fn remove<Q>(&mut self, k: &Q) -> Option<V>
where K: Borrow<Q>, Q: Eq + Hash + ?Sized,

Removes and returns the value corresponding to the key from the map.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();

map.insert(2, "a");

assert_eq!(map.remove(&1), None);
assert_eq!(map.remove(&2), Some("a"));
assert_eq!(map.remove(&2), None);
assert_eq!(map.len(), 0);
source

pub fn capacity(&self) -> usize

Returns the maximum number of key-value pairs the map can hold without reallocating.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map: LinkedHashMap<i32, &str> = LinkedHashMap::new();
let capacity = map.capacity();
source

pub fn pop_front(&mut self) -> Option<(K, V)>

Removes the first entry.

Can be used in implementation of LRU cache.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_front();
assert_eq!(map.get(&1), None);
assert_eq!(map.get(&2), Some(&20));
source

pub fn front(&self) -> Option<(&K, &V)>

Gets the first entry.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.front(), Some((&1, &10)));
source

pub fn pop_back(&mut self) -> Option<(K, V)>

Removes the last entry.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
map.pop_back();
assert_eq!(map.get(&1), Some(&10));
assert_eq!(map.get(&2), None);
source

pub fn back(&self) -> Option<(&K, &V)>

Gets the last entry.

§Examples
use linked_hash_map::LinkedHashMap;
let mut map = LinkedHashMap::new();
map.insert(1, 10);
map.insert(2, 20);
assert_eq!(map.back(), Some((&2, &20)));
source

pub fn len(&self) -> usize

Returns the number of key-value pairs in the map.

source

pub fn is_empty(&self) -> bool

Returns whether the map is currently empty.

source

pub fn hasher(&self) -> &S

Returns a reference to the map’s hasher.

source

pub fn clear(&mut self)

Clears the map of all key-value pairs.

source

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

Returns a double-ended iterator visiting all key-value pairs in order of insertion. Iterator element type is (&'a K, &'a V)

§Examples
use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

let mut iter = map.iter();
assert_eq!((&"a", &10), iter.next().unwrap());
assert_eq!((&"c", &30), iter.next().unwrap());
assert_eq!((&"b", &20), iter.next().unwrap());
assert_eq!(None, iter.next());
source

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

Returns a double-ended iterator visiting all key-value pairs in order of insertion. Iterator element type is (&'a K, &'a mut V)

§Examples
use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert("a", 10);
map.insert("c", 30);
map.insert("b", 20);

{
    let mut iter = map.iter_mut();
    let mut entry = iter.next().unwrap();
    assert_eq!(&"a", entry.0);
    *entry.1 = 17;
}

assert_eq!(&17, map.get(&"a").unwrap());
source

pub fn drain(&mut self) -> Drain<'_, K, V>

Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.

If the returned iterator is dropped before being fully consumed, it drops the remaining key-value pairs. The returned iterator keeps a mutable borrow on the vector to optimize its implementation.

Current performance implications (why to use this over into_iter()):

  • Clears the inner HashMap instead of dropping it
  • Puts all drained nodes in the free-list instead of deallocating them
  • Avoids deallocating the sentinel node
source

pub fn keys(&self) -> Keys<'_, K, V>

Returns a double-ended iterator visiting all key in order of insertion.

§Examples
use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);

let mut keys = map.keys();
assert_eq!(&'a', keys.next().unwrap());
assert_eq!(&'c', keys.next().unwrap());
assert_eq!(&'b', keys.next().unwrap());
assert_eq!(None, keys.next());
source

pub fn values(&self) -> Values<'_, K, V>

Returns a double-ended iterator visiting all values in order of insertion.

§Examples
use linked_hash_map::LinkedHashMap;

let mut map = LinkedHashMap::new();
map.insert('a', 10);
map.insert('c', 30);
map.insert('b', 20);

let mut values = map.values();
assert_eq!(&10, values.next().unwrap());
assert_eq!(&30, values.next().unwrap());
assert_eq!(&20, values.next().unwrap());
assert_eq!(None, values.next());

Trait Implementations§

source§

impl<K, V> Clone for OrderedHashMap<K, V>
where K: Clone + Hash + Eq + Ord, V: Clone,

source§

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

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, V> Debug for OrderedHashMap<K, V>
where K: Debug + Hash + Eq + Ord, V: Debug,

source§

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

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

impl<K, V> Default for OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

source§

fn default() -> OrderedHashMap<K, V>

Returns the “default value” for a type. Read more
source§

impl<K, V> Deref for OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

§

type Target = LinkedHashMap<K, V>

The resulting type after dereferencing.
source§

fn deref(&self) -> &<OrderedHashMap<K, V> as Deref>::Target

Dereferences the value.
source§

impl<K, V> DerefMut for OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

source§

fn deref_mut(&mut self) -> &mut <OrderedHashMap<K, V> as Deref>::Target

Mutably dereferences the value.
source§

impl<'de, K, V> Deserialize<'de> for OrderedHashMap<K, V>
where K: Hash + Eq + Ord + Deserialize<'de>, V: Deserialize<'de>,

source§

fn deserialize<D>( deserializer: D ) -> Result<OrderedHashMap<K, V>, <D as Deserializer<'de>>::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<K, V> FromIterator<(K, V)> for OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

source§

fn from_iter<T>(iter: T) -> OrderedHashMap<K, V>
where T: IntoIterator<Item = (K, V)>,

Creates a value from an iterator. Read more
source§

impl<K, V> Hash for OrderedHashMap<K, V>
where K: Hash + Eq + Ord, V: Hash,

source§

fn hash<H>(&self, h: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<K, V> JsonSchema for OrderedHashMap<K, V>
where K: Hash + Eq + Ord + JsonSchema, V: JsonSchema,

source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
source§

fn json_schema(gen: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
source§

impl<K, V> Ord for OrderedHashMap<K, V>
where K: Ord + Hash + Eq, V: Ord,

source§

fn cmp(&self, other: &OrderedHashMap<K, V>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl<K, V> PartialEq for OrderedHashMap<K, V>
where K: PartialEq + Hash + Eq + Ord, V: PartialEq,

source§

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

source§

fn partial_cmp(&self, other: &OrderedHashMap<K, V>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl<K, V> Serialize for OrderedHashMap<K, V>
where K: Hash + Eq + Ord + Serialize, V: Serialize,

source§

fn serialize<S>( &self, serializer: S ) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<K, V> Eq for OrderedHashMap<K, V>
where K: Eq + Hash + Ord, V: Eq,

source§

impl<K, V> StructuralPartialEq for OrderedHashMap<K, V>
where K: Hash + Eq + Ord,

Auto Trait Implementations§

§

impl<K, V> Freeze for OrderedHashMap<K, V>

§

impl<K, V> RefUnwindSafe for OrderedHashMap<K, V>

§

impl<K, V> Send for OrderedHashMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for OrderedHashMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for OrderedHashMap<K, V>

§

impl<K, V> UnwindSafe for OrderedHashMap<K, V>

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> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,