pub struct ListOrderedMultimap<Key, Value, State = RandomState> { /* private fields */ }
Expand description
A multimap that associates with each key a list of values.
§Ordering
The primary guarantee this type gives is that regardless of what you do to the multimap, you are always able to iterate through all keys and values in the order they were inserted. Values can be iterated by their insertion order either for a specific key or for the entire map.
§Allocations
Allocations may be performed on any key-value insertion.
Implementations§
source§impl<Key, Value> ListOrderedMultimap<Key, Value, RandomState>
impl<Key, Value> ListOrderedMultimap<Key, Value, RandomState>
sourcepub fn new() -> ListOrderedMultimap<Key, Value, RandomState>
pub fn new() -> ListOrderedMultimap<Key, Value, RandomState>
Creates a new multimap with no initial capacity.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
assert_eq!(map.get(&"key1"), Some(&"value1"));
sourcepub fn with_capacity(
key_capacity: usize,
value_capacity: usize
) -> ListOrderedMultimap<Key, Value, RandomState>
pub fn with_capacity( key_capacity: usize, value_capacity: usize ) -> ListOrderedMultimap<Key, Value, RandomState>
Creates a new multimap with the specified capacities.
The multimap will be able to hold at least key_capacity
keys and value_capacity
values without reallocating.
A capacity of 0 will result in no allocation for the respective container.
§Examples
use ordered_multimap::ListOrderedMultimap;
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
assert_eq!(map.keys_capacity(), 0);
assert_eq!(map.values_capacity(), 0);
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::with_capacity(5, 10);
assert_eq!(map.keys_capacity(), 5);
assert_eq!(map.values_capacity(), 10);
source§impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>where
State: BuildHasher,
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>where
State: BuildHasher,
sourcepub fn with_capacity_and_hasher(
key_capacity: usize,
value_capacity: usize,
state: State
) -> ListOrderedMultimap<Key, Value, State>
pub fn with_capacity_and_hasher( key_capacity: usize, value_capacity: usize, state: State ) -> ListOrderedMultimap<Key, Value, State>
Creates a new multimap with the specified capacities and the given hash builder to hash keys.
The multimap will be able to hold at least key_capacity
keys and value_capacity
values without reallocating. A
capacity of 0 will result in no allocation for the respective container.
The state
is normally randomly generated and is designed to allow multimaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack
vector.
§Examples
use ordered_multimap::ListOrderedMultimap;
use std::collections::hash_map::RandomState;
let state = RandomState::new();
let mut map = ListOrderedMultimap::with_capacity_and_hasher(10, 10, state);
map.insert("key", "value");
assert_eq!(map.keys_capacity(), 10);
assert_eq!(map.values_capacity(), 10);
sourcepub fn with_hasher(state: State) -> ListOrderedMultimap<Key, Value, State>
pub fn with_hasher(state: State) -> ListOrderedMultimap<Key, Value, State>
Creates a new multimap with no capacity which will use the given hash builder to hash keys.
The state
is normally randomly generated and is designed to allow multimaps to be resistant to attacks that
cause many collisions and very poor performance. Setting it manually using this function can expose a DoS attack
vector.
§Examples
use ordered_multimap::ListOrderedMultimap;
use std::collections::hash_map::RandomState;
let state = RandomState::new();
let mut map = ListOrderedMultimap::with_hasher(state);
map.insert("key", "value");
source§impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
sourcepub fn back(&self) -> Option<(&Key, &Value)>
pub fn back(&self) -> Option<(&Key, &Value)>
Returns an immutable reference to the first key-value pair in the multimap
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.back(), None);
map.insert("key", "value");
assert_eq!(map.back(), Some((&"key", &"value")));
sourcepub fn back_mut(&mut self) -> Option<(&Key, &mut Value)>
pub fn back_mut(&mut self) -> Option<(&Key, &mut Value)>
Returns an immutable reference to the first key-value pair in the multimap
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.back_mut(), None);
map.insert("key", "value");
assert_eq!(map.back_mut(), Some((&"key", &mut "value")));
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Removes all keys and values from the multimap.
Complexity: O(|K| + |V|) where |K| is the number of keys and |V| is the number of values.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
assert_eq!(map.keys_len(), 1);
assert_eq!(map.values_len(), 1);
map.clear();
assert_eq!(map.keys_len(), 0);
assert_eq!(map.values_len(), 0);
sourcepub fn front(&self) -> Option<(&Key, &Value)>
pub fn front(&self) -> Option<(&Key, &Value)>
Returns an immutable reference to the first key-value pair in the multimap
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.front(), None);
map.insert("key", "value");
assert_eq!(map.front(), Some((&"key", &"value")));
sourcepub fn front_mut(&mut self) -> Option<(&Key, &mut Value)>
pub fn front_mut(&mut self) -> Option<(&Key, &mut Value)>
Returns an immutable reference to the first key-value pair in the multimap
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.front_mut(), None);
map.insert("key", "value");
assert_eq!(map.front_mut(), Some((&"key", &mut "value")));
sourcepub fn hasher(&self) -> &State
pub fn hasher(&self) -> &State
Returns a reference to the multimap’s BuildHasher
.
§Examples
use ordered_multimap::ListOrderedMultimap;
let map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
let hasher = map.hasher();
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns whether the multimap is empty.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert!(map.is_empty());
map.insert("key1", "value");
assert!(!map.is_empty());
map.remove(&"key1");
assert!(map.is_empty());
sourcepub fn iter(&self) -> Iter<'_, Key, Value> ⓘ
pub fn iter(&self) -> Iter<'_, Key, Value> ⓘ
Returns an iterator that yields immutable references to all key-value pairs in the multimap by insertion order.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value1");
map.append(&"key1", "value2");
map.append(&"key2", "value2");
let mut iter = map.iter();
assert_eq!(iter.size_hint(), (4, Some(4)));
assert_eq!(iter.next(), Some((&"key1", &"value1")));
assert_eq!(iter.next(), Some((&"key2", &"value1")));
assert_eq!(iter.next(), Some((&"key1", &"value2")));
assert_eq!(iter.next(), Some((&"key2", &"value2")));
assert_eq!(iter.next(), None);
sourcepub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> ⓘ
pub fn iter_mut(&mut self) -> IterMut<'_, Key, Value> ⓘ
Returns an iterator that yields mutable references to all key-value pairs in the multimap by insertion order.
Only the values are mutable, the keys are immutable.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value1");
map.append(&"key1", "value2");
map.append(&"key2", "value2");
let mut iter = map.iter_mut();
assert_eq!(iter.size_hint(), (4, Some(4)));
let first = iter.next().unwrap();
assert_eq!(first, (&"key1", &mut "value1"));
*first.1 = "value3";
assert_eq!(iter.next(), Some((&"key2", &mut "value1")));
assert_eq!(iter.next(), Some((&"key1", &mut "value2")));
assert_eq!(iter.next(), Some((&"key2", &mut "value2")));
assert_eq!(iter.next(), None);
assert_eq!(map.get(&"key1"), Some(&"value3"));
sourcepub fn keys(&self) -> Keys<'_, Key> ⓘ
pub fn keys(&self) -> Keys<'_, Key> ⓘ
Returns an iterator that yields immutable references to all keys in the multimap by insertion order.
Insertion order of keys is determined by the order in which a given key is first inserted into the multimap with a value. Any subsequent insertions with that key without first removing it will not affect its ordering.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value");
map.insert("key2", "value");
map.insert("key3", "value");
let mut keys = map.keys();
assert_eq!(keys.next(), Some(&"key1"));
assert_eq!(keys.next(), Some(&"key2"));
assert_eq!(keys.next(), Some(&"key3"));
assert_eq!(keys.next(), None);
sourcepub fn keys_capacity(&self) -> usize
pub fn keys_capacity(&self) -> usize
Returns the number of keys the multimap can hold without reallocating.
This number is a lower bound, and the multimap may be able to hold more.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.keys_capacity(), 0);
map.insert("key", "value");
assert!(map.keys_capacity() > 0);
sourcepub fn keys_len(&self) -> usize
pub fn keys_len(&self) -> usize
Returns the number of keys in the multimap.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.keys_len(), 0);
map.insert("key1", "value");
map.insert("key2", "value");
map.insert("key3", "value");
assert_eq!(map.keys_len(), 3);
sourcepub fn pairs(&self) -> KeyValues<'_, Key, Value, State> ⓘ
pub fn pairs(&self) -> KeyValues<'_, Key, Value, State> ⓘ
Returns an iterator that yields immutable references to keys and all associated values with those keys as separate iterators. The order of yielded pairs will be the order in which the keys were first inserted into the multimap.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
let mut iter = map.pairs();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key");
assert_eq!(values.next(), Some(&"value1"));
assert_eq!(values.next(), Some(&"value2"));
assert_eq!(values.next(), None);
sourcepub fn pairs_mut(&mut self) -> KeyValuesMut<'_, Key, Value, State> ⓘ
pub fn pairs_mut(&mut self) -> KeyValuesMut<'_, Key, Value, State> ⓘ
Returns an iterator that yields immutable references to keys and mutable references to all associated values with those keys as separate iterators. The order of yielded pairs will be the order in which the keys were first inserted into the multimap.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
let mut iter = map.pairs_mut();
let (key, mut values) = iter.next().unwrap();
assert_eq!(key, &"key");
assert_eq!(values.next(), Some(&mut "value1"));
assert_eq!(values.next(), Some(&mut "value2"));
assert_eq!(values.next(), None);
sourcepub fn reserve_values(&mut self, additional_capacity: usize)
pub fn reserve_values(&mut self, additional_capacity: usize)
Reserves additional capacity such that more values can be stored in the multimap.
If the existing capacity minus the current length is enough to satisfy the additional capacity, the capacity will remain unchanged.
If the capacity is increased, the capacity may be increased by more than what was requested.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::with_capacity(1, 1);
map.insert("key", "value");
assert_eq!(map.values_capacity(), 1);
map.reserve_values(10);
assert!(map.values_capacity() >= 11);
sourcepub fn values(&self) -> Values<'_, Key, Value> ⓘ
pub fn values(&self) -> Values<'_, Key, Value> ⓘ
Returns an iterator that yields immutable references to all values in the multimap by insertion order.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value1");
map.append(&"key1", "value2");
map.append(&"key2", "value2");
let mut iter = map.values();
assert_eq!(iter.size_hint(), (4, Some(4)));
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), Some(&"value1"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), None);
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, Key, Value> ⓘ
Returns an iterator that yields mutable references to all values in the multimap by insertion order.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", "value1");
map.insert("key2", "value1");
map.append(&"key1", "value2");
map.append(&"key2", "value2");
let mut iter = map.values_mut();
assert_eq!(iter.size_hint(), (4, Some(4)));
let first = iter.next().unwrap();
assert_eq!(first, &mut "value1");
*first = "value3";
assert_eq!(iter.next(), Some(&mut "value1"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next(), None);
assert_eq!(map.get(&"key1"), Some(&"value3"));
sourcepub fn values_capacity(&self) -> usize
pub fn values_capacity(&self) -> usize
Returns the number of values the multimap can hold without reallocating.
This number is a lower bound, and the multimap may be able to hold more.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.values_capacity(), 0);
map.insert("key", "value");
assert!(map.values_capacity() > 0);
sourcepub fn values_len(&self) -> usize
pub fn values_len(&self) -> usize
Returns the total number of values in the multimap across all keys.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.values_len(), 0);
map.insert("key1", "value1");
assert_eq!(map.values_len(), 1);
map.append("key1", "value2");
assert_eq!(map.values_len(), 2);
source§impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> ListOrderedMultimap<Key, Value, State>
sourcepub fn append(&mut self, key: Key, value: Value) -> bool
pub fn append(&mut self, key: Key, value: Value) -> bool
Appends a value to the list of values associated with the given key.
If the key is not already in the multimap, this will be identical to an insert and the return value will be
false
. Otherwise, true
will be returned.
Complexity: amortized O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
let already_exists = map.append("key", "value");
assert!(!already_exists);
assert_eq!(map.values_len(), 1);
assert_eq!(map.get(&"key"), Some(&"value"));
let already_exists = map.append("key", "value2");
assert!(already_exists);
assert_eq!(map.values_len(), 2);
sourcepub fn contains_key<KeyQuery>(&self, key: &KeyQuery) -> bool
pub fn contains_key<KeyQuery>(&self, key: &KeyQuery) -> bool
Returns whether the given key is in the multimap.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert!(!map.contains_key(&"key"));
map.insert("key", "value");
assert!(map.contains_key(&"key"));
sourcepub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value, State>
pub fn entry(&mut self, key: Key) -> Entry<'_, Key, Value, State>
Returns whether the given key is in the multimap.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
let value = map.entry("key").or_insert("value");
assert_eq!(value, &"value");
assert_eq!(map.get(&"key"), Some(&"value"));
sourcepub fn entry_len<KeyQuery>(&self, key: &KeyQuery) -> usize
pub fn entry_len<KeyQuery>(&self, key: &KeyQuery) -> usize
Returns the number of values associated with a key.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.entry_len(&"key"), 0);
map.insert("key", "value1");
assert_eq!(map.entry_len(&"key"), 1);
map.append(&"key", "value2");
assert_eq!(map.entry_len(&"key"), 2);
sourcepub fn get<KeyQuery>(&self, key: &KeyQuery) -> Option<&Value>
pub fn get<KeyQuery>(&self, key: &KeyQuery) -> Option<&Value>
Returns an immutable reference to the first value, by insertion order, associated with the given key, or None
if
the key is not in the multimap.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map: ListOrderedMultimap<&str, &str> = ListOrderedMultimap::new();
assert_eq!(map.get(&"key"), None);
sourcepub fn get_all<KeyQuery>(&self, key: &KeyQuery) -> EntryValues<'_, Key, Value> ⓘ
pub fn get_all<KeyQuery>(&self, key: &KeyQuery) -> EntryValues<'_, Key, Value> ⓘ
Returns an iterator that yields immutable references to all values associated with the given key by insertion order.
If the key is not in the multimap, the iterator will yield no values.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value");
map.append("key", "value2");
let mut iter = map.get_all(&"key");
assert_eq!(iter.next(), Some(&"value"));
assert_eq!(iter.next(), Some(&"value2"));
assert_eq!(iter.next(), None);
sourcepub fn get_all_mut<KeyQuery>(
&mut self,
key: &KeyQuery
) -> EntryValuesMut<'_, Key, Value> ⓘ
pub fn get_all_mut<KeyQuery>( &mut self, key: &KeyQuery ) -> EntryValuesMut<'_, Key, Value> ⓘ
Returns an iterator that yields mutable references to all values associated with the given key by insertion order.
If the key is not in the multimap, the iterator will yield no values.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
let mut iter = map.get_all_mut(&"key");
let first = iter.next().unwrap();
assert_eq!(first, &mut "value1");
*first = "value3";
assert_eq!(iter.next(), Some(&mut "value2"));
assert_eq!(iter.next(), None);
assert_eq!(map.get(&"key"), Some(&"value3"));
sourcepub fn get_mut<KeyQuery>(&mut self, key: &KeyQuery) -> Option<&mut Value>
pub fn get_mut<KeyQuery>(&mut self, key: &KeyQuery) -> Option<&mut Value>
Returns a mutable reference to the first value, by insertion order, associated with the given key, or None
if
the key is not in the multimap.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert_eq!(map.get(&"key"), None);
map.insert("key", "value");
assert_eq!(map.get(&"key"), Some(&"value"));
let mut value = map.get_mut(&"key").unwrap();
*value = "value2";
assert_eq!(map.get(&"key"), Some(&"value2"));
sourcepub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
pub fn insert(&mut self, key: Key, value: Value) -> Option<Value>
Inserts the key-value pair into the multimap and returns the first value, by insertion order, that was already associated with the key.
If the key is not already in the multimap, None
will be returned. If the key is already in the multimap, the
insertion ordering of the keys will remain unchanged.
Complexity: O(1) amortized
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert!(map.is_empty());
let old_value = map.insert("key", "value");
assert!(old_value.is_none());
assert_eq!(map.values_len(), 1);
assert_eq!(map.get(&"key"), Some(&"value"));
let old_value = map.insert("key", "value2");
assert_eq!(old_value, Some("value"));
assert_eq!(map.values_len(), 1);
assert_eq!(map.get(&"key"), Some(&"value2"));
sourcepub fn insert_all(
&mut self,
key: Key,
value: Value
) -> EntryValuesDrain<'_, Key, Value> ⓘ
pub fn insert_all( &mut self, key: Key, value: Value ) -> EntryValuesDrain<'_, Key, Value> ⓘ
Inserts the key-value pair into the multimap and returns an iterator that yields all values previously associated with the key by insertion order.
If the key is not already in the multimap, the iterator will yield no values.If the key is already in the multimap, the insertion ordering of the keys will remain unchanged.
Complexity: O(1) amortized
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
assert!(map.is_empty());
{
let mut old_values = map.insert_all("key", "value");
assert_eq!(old_values.next(), None);
}
assert_eq!(map.values_len(), 1);
assert_eq!(map.get(&"key"), Some(&"value"));
map.append("key", "value2");
{
let mut old_values = map.insert_all("key", "value3");
assert_eq!(old_values.next(), Some("value"));
assert_eq!(old_values.next(), Some("value2"));
assert_eq!(old_values.next(), None);
}
assert_eq!(map.values_len(), 1);
assert_eq!(map.get(&"key"), Some(&"value3"));
sourcepub fn pack_to(
&mut self,
keys_minimum_capacity: usize,
values_minimum_capacity: usize
)where
State: Default,
pub fn pack_to(
&mut self,
keys_minimum_capacity: usize,
values_minimum_capacity: usize
)where
State: Default,
Reorganizes the multimap to ensure maximum spatial locality and changes the key and value capacities to the provided values.
This function can be used to actually increase the capacity of the multimap.
Complexity: O(|K| + |V|) where |K| is the number of keys and |V| is the number of values.
§Panics
Panics if either of the given minimum capacities are less than their current respective lengths.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::with_capacity(10, 10);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to(5, 5);
assert_eq!(map.keys_capacity(), 5);
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_capacity(), 5);
assert_eq!(map.values_len(), 4);
sourcepub fn pack_to_fit(&mut self)where
State: Default,
pub fn pack_to_fit(&mut self)where
State: Default,
Reorganizes the multimap to ensure maximum spatial locality and removes any excess key and value capacity.
Complexity: O(|K| + |V|) where |K| is the number of keys and |V| is the number of values.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::with_capacity(5, 5);
map.insert("key1", "value1");
map.insert("key2", "value2");
map.append("key2", "value3");
map.append("key1", "value4");
map.pack_to_fit();
assert_eq!(map.keys_capacity(), 2);
assert_eq!(map.keys_len(), 2);
assert_eq!(map.values_capacity(), 4);
assert_eq!(map.values_len(), 4);
sourcepub fn pop_back(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)>
pub fn pop_back(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)>
Removes the last key-value pair to have been inserted.
Because a single key can be associated with many values, the key returned by this function is a KeyWrapper
which can be either owned or borrowed. If the value removed was the only value associated with the key, then the
key will be returned. Otherwise, a reference to the key will be returned.
This function along with ListOrderedMultimap::pop_front
act as replacements for a drain iterator since an
iterator cannot be done over KeyWrapper
.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
use ordered_multimap::list_ordered_multimap::KeyWrapper;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key"));
assert_eq!(&value, &"value2");
let (key, value) = map.pop_back().unwrap();
assert_eq!(key, KeyWrapper::Owned("key"));
assert_eq!(&value, &"value1");
sourcepub fn pop_front(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)>
pub fn pop_front(&mut self) -> Option<(KeyWrapper<'_, Key>, Value)>
Removes the first key-value pair to have been inserted.
Because a single key can be associated with many values, the key returned by this function is a KeyWrapper
which can be either owned or borrowed. If the value removed was the only value associated with the key, then the
key will be returned. Otherwise, a reference to the key will be returned.
This function along with ListOrderedMultimap::pop_back
act as replacements for a drain iterator since an
iterator cannot be done over KeyWrapper
.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
use ordered_multimap::list_ordered_multimap::KeyWrapper;
let mut map = ListOrderedMultimap::new();
map.insert("key", "value1");
map.append("key", "value2");
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Borrowed(&"key"));
assert_eq!(&value, &"value1");
let (key, value) = map.pop_front().unwrap();
assert_eq!(key, KeyWrapper::Owned("key"));
assert_eq!(&value, &"value2");
sourcepub fn remove<KeyQuery>(&mut self, key: &KeyQuery) -> Option<Value>
pub fn remove<KeyQuery>(&mut self, key: &KeyQuery) -> Option<Value>
Removes all values associated with the given key from the map and returns the first value by insertion order.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
let removed_value = map.remove(&"key");
assert_eq!(removed_value, None);
map.insert("key", "value");
assert_eq!(map.get(&"key"), Some(&"value"));
let removed_value = map.remove(&"key");
assert_eq!(removed_value, Some("value"));
assert_eq!(map.get(&"key"), None);
sourcepub fn remove_all<KeyQuery>(
&mut self,
key: &KeyQuery
) -> EntryValuesDrain<'_, Key, Value> ⓘ
pub fn remove_all<KeyQuery>( &mut self, key: &KeyQuery ) -> EntryValuesDrain<'_, Key, Value> ⓘ
Removes all values associated with the given key from the map and returns an iterator that yields those values.
If the key is not already in the map, the iterator will yield no values.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
{
let mut removed_values = map.remove_all(&"key");
assert_eq!(removed_values.next(), None);
}
map.insert("key", "value1");
map.append("key", "value2");
assert_eq!(map.get(&"key"), Some(&"value1"));
{
let mut removed_values = map.remove_all(&"key");
assert_eq!(removed_values.next(), Some("value1"));
assert_eq!(removed_values.next(), Some("value2"));
assert_eq!(removed_values.next(), None);
}
assert_eq!(map.get(&"key"), None);
sourcepub fn remove_entry<KeyQuery>(&mut self, key: &KeyQuery) -> Option<(Key, Value)>
pub fn remove_entry<KeyQuery>(&mut self, key: &KeyQuery) -> Option<(Key, Value)>
Removes all values associated with the given key from the map and returns the key and first value.
If the key is not already in the map, then None
will be returned.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
let entry = map.remove_entry(&"key");
assert_eq!(entry, None);
map.insert("key", "value");
assert_eq!(map.get(&"key"), Some(&"value"));
let entry = map.remove_entry(&"key");
assert_eq!(entry, Some(("key", "value")));
assert_eq!(map.get(&"key"), None);
sourcepub fn remove_entry_all<KeyQuery>(
&mut self,
key: &KeyQuery
) -> Option<(Key, EntryValuesDrain<'_, Key, Value>)>
pub fn remove_entry_all<KeyQuery>( &mut self, key: &KeyQuery ) -> Option<(Key, EntryValuesDrain<'_, Key, Value>)>
Removes all values associated with the given key from the map and returns the key and an iterator that yields those values.
If the key is not already in the map, then None
will be returned.
Complexity: O(1)
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
{
let entry = map.remove_entry_all(&"key");
assert!(entry.is_none());
}
map.insert("key", "value1");
map.append("key", "value2");
assert_eq!(map.get(&"key"), Some(&"value1"));
{
let (key, mut iter) = map.remove_entry_all(&"key").unwrap();
assert_eq!(key, "key");
assert_eq!(iter.next(), Some("value1"));
assert_eq!(iter.next(), Some("value2"));
assert_eq!(iter.next(), None);
}
assert_eq!(map.get(&"key"), None);
sourcepub fn reserve_keys(&mut self, additional_capacity: usize)
pub fn reserve_keys(&mut self, additional_capacity: usize)
Reserves additional capacity such that more keys can be stored in the multimap.
If the existing capacity minus the current length is enough to satisfy the additional capacity, the capacity will remain unchanged.
If the capacity is increased, the capacity may be increased by more than what was requested.
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::with_capacity(1, 1);
map.insert("key", "value");
assert_eq!(map.keys_capacity(), 1);
map.reserve_keys(10);
assert!(map.keys_capacity() >= 11);
assert_eq!(map.get(&"key"), Some(&"value"));
sourcepub fn retain<Function>(&mut self, function: Function)
pub fn retain<Function>(&mut self, function: Function)
Keeps all key-value pairs that satisfy the given predicate function.
Complexity: O(|V|) where |V| is the number of values
§Examples
use ordered_multimap::ListOrderedMultimap;
let mut map = ListOrderedMultimap::new();
map.insert("key1", 1);
map.insert("key2", 5);
map.append("key1", -1);
map.insert("key3", -10);
map.retain(|_, &mut value| value >= 0);
let mut iter = map.iter();
assert_eq!(iter.next(), Some((&"key1", &1)));
assert_eq!(iter.next(), Some((&"key2", &5)));
assert_eq!(iter.next(), None);
Trait Implementations§
source§impl<Key: Clone, Value: Clone, State: Clone> Clone for ListOrderedMultimap<Key, Value, State>
impl<Key: Clone, Value: Clone, State: Clone> Clone for ListOrderedMultimap<Key, Value, State>
source§fn clone(&self) -> ListOrderedMultimap<Key, Value, State>
fn clone(&self) -> ListOrderedMultimap<Key, Value, State>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<Key, Value, State> Debug for ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> Debug for ListOrderedMultimap<Key, Value, State>
source§impl<Key, Value> Default for ListOrderedMultimap<Key, Value, RandomState>
impl<Key, Value> Default for ListOrderedMultimap<Key, Value, RandomState>
source§impl<'a, Key, Value, State> Extend<(&'a Key, &'a Value)> for ListOrderedMultimap<Key, Value, State>
impl<'a, Key, Value, State> Extend<(&'a Key, &'a Value)> for ListOrderedMultimap<Key, Value, State>
source§fn extend<Iter>(&mut self, iter: Iter)
fn extend<Iter>(&mut self, iter: Iter)
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<Key, Value, State> Extend<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> Extend<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
source§fn extend<Iter>(&mut self, iter: Iter)where
Iter: IntoIterator<Item = (Key, Value)>,
fn extend<Iter>(&mut self, iter: Iter)where
Iter: IntoIterator<Item = (Key, Value)>,
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<Key, Value, State> FromIterator<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> FromIterator<(Key, Value)> for ListOrderedMultimap<Key, Value, State>
source§fn from_iter<Iter>(iter: Iter) -> Selfwhere
Iter: IntoIterator<Item = (Key, Value)>,
fn from_iter<Iter>(iter: Iter) -> Selfwhere
Iter: IntoIterator<Item = (Key, Value)>,
source§impl<'map, Key, Value, State> IntoIterator for &'map ListOrderedMultimap<Key, Value, State>
impl<'map, Key, Value, State> IntoIterator for &'map ListOrderedMultimap<Key, Value, State>
source§impl<'map, Key, Value, State> IntoIterator for &'map mut ListOrderedMultimap<Key, Value, State>
impl<'map, Key, Value, State> IntoIterator for &'map mut ListOrderedMultimap<Key, Value, State>
source§impl<Key, Value, State> IntoIterator for ListOrderedMultimap<Key, Value, State>where
Key: Clone,
impl<Key, Value, State> IntoIterator for ListOrderedMultimap<Key, Value, State>where
Key: Clone,
source§impl<Key, Value, State> PartialEq for ListOrderedMultimap<Key, Value, State>
impl<Key, Value, State> PartialEq for ListOrderedMultimap<Key, Value, State>
source§fn eq(&self, other: &ListOrderedMultimap<Key, Value, State>) -> bool
fn eq(&self, other: &ListOrderedMultimap<Key, Value, State>) -> bool
self
and other
values to be equal, and is used
by ==
.