pub struct TotalOrderMultiMap<K, V>{ /* private fields */ }Expand description
A multi map with keeps the total ordering of inserted elements
The map is meant to contain values implementing StableDeref,
methods like get and iter will iterate over the inner values
referred to when dereferencing the values.
The key is currently limited to values implementing Copy.
See the module/crate level documentation for more details.
§Unwind Safety
This type is unwind + resume safe.
Through in the unlikely case that a panic happens inside of
a function like insert,get the resulting state might be
inconsistent in a safe way, i.e. some values might be in the map,
accessible during iteration but hey won’t appear when using get.
Note that this can only happen in a few rare cases:
Vec::pop/Vec::reservepanicsHashMap::insert/HashMap::reservepanics- for some reason
oomdoes panic instead of aborting
Which mainly can happen in mainly following cases:
- the vector of key-value pairs tries to contain more then
isize::MAXbytes - you use zero-sized values and overflow
usize(which can’t really happen as we store at last some data per inserting, i.e. you would overflow the vec first)
Generally speaking you won’t run into any of this in normal circumstances and if
you do it’s likely that you are close to a system wide oom anyway.
Implementations§
Source§impl<K, V> TotalOrderMultiMap<K, V>
impl<K, V> TotalOrderMultiMap<K, V>
Sourcepub fn iter(&self) -> Iter<'_, K, V> ⓘ
pub fn iter(&self) -> Iter<'_, K, V> ⓘ
Returns a iterator over the key value pairs in insertion order.
As this is a multi map a key can appear more than one time.
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Source§impl<K, V> TotalOrderMultiMap<K, V>
impl<K, V> TotalOrderMultiMap<K, V>
Source§impl<K, V> TotalOrderMultiMap<K, V>
impl<K, V> TotalOrderMultiMap<K, V>
Source§impl<K, V> TotalOrderMultiMap<K, V>
impl<K, V> TotalOrderMultiMap<K, V>
Sourcepub fn keys(&self) -> Keys<'_, K, V::Target> ⓘ
pub fn keys(&self) -> Keys<'_, K, V::Target> ⓘ
Return an iterator the keys of this multi map.
each key will only be returned once, there is no specific order in which they keys are returned
Sourcepub fn group_iter(&self) -> GroupedValues<'_, K, V::Target> ⓘ
pub fn group_iter(&self) -> GroupedValues<'_, K, V::Target> ⓘ
Returns a iterator over all values grouped by key.
Sourcepub fn group_iter_mut(&mut self) -> GroupedValuesMut<'_, K, V::Target> ⓘ
pub fn group_iter_mut(&mut self) -> GroupedValuesMut<'_, K, V::Target> ⓘ
Returns a iterator over all values grouped by key
Sourcepub fn values(&self) -> Values<'_, K, V> ⓘ
pub fn values(&self) -> Values<'_, K, V> ⓘ
Returns a iterator over the inner-values in this multi map.
Inner-Values are returned in the order they where inserted into the map. Note that
Sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ
Returns a iterator over the values in this multi map.
Source§impl<K, V> TotalOrderMultiMap<K, V>
impl<K, V> TotalOrderMultiMap<K, V>
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new empty map.
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Crate a new map with a given capacity.
Note that this method will reserve the given
capacity for the internal Vec and HashMap,
but as it’s a multi map it can not know how
elements will be distributed, as such using
with_capacity does not guarantee that there
are no allocations if less than capacity elements
are inserted. Through it still can reduce the
number of allocations needed.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the capacity (unreliable).
This is not reliable as it only returns
the capacity of the underlying Vec not
considering the underlying HashMap or
the Vec used to turn the map into a
multi map.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves space for n additional elements.
The reservation is done in both the internal
Vec and HashMap but as the map is a multi
map this method is less helpful as e.g. on a
pure Vec or HashMap
§Panics
if the new allocation size overflows usize
Sourcepub fn reverse(&mut self)
pub fn reverse(&mut self)
Reverses insertion order.
After calling this the map will contains values as if they had been inserted in reversed order.
This will affect both the iteration order of
the fill map as well as the iteration order of
values returned by get.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrinks all internal containers to not contains any additional capacity.
Whether or not memory is freed depends in the dens on the shrink_to_fit
implementation of Vec and HashMap
Sourcepub fn contains_key(&self, k: K) -> bool
pub fn contains_key(&self, k: K) -> bool
Returns true if the key is contained in the map.
Does not state how many values are associated with it.
Sourcepub fn get(&self, k: K) -> EntryValues<'_, V::Target> ⓘ
pub fn get(&self, k: K) -> EntryValues<'_, V::Target> ⓘ
Returns values associated with the given key.
If the key is not in the map this will return None.
This also means that EntryValues has at last one
element.
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Sourcepub fn get_mut(&mut self, k: K) -> EntryValuesMut<'_, V::Target> ⓘ
pub fn get_mut(&mut self, k: K) -> EntryValuesMut<'_, V::Target> ⓘ
Returns mutable references associated with the given key.
If the key is not in the map this will return None.
This means the EntryValuesMut has at last one element.
Sourcepub fn add(&mut self, key: K, value: V) -> EntryValuesMut<'_, V::Target> ⓘ
pub fn add(&mut self, key: K, value: V) -> EntryValuesMut<'_, V::Target> ⓘ
Adds a value for a given key to the multi map.
Returns access the all values already added to
the key previously and this now added value
through EntryValuesMut
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Sourcepub fn set(&mut self, key: K, value: V) -> Vec<V>
pub fn set(&mut self, key: K, value: V) -> Vec<V>
Sets the value associated with the given key.
Values previously associated with the key are removed and returned.
Sourcepub fn pop(&mut self) -> Option<(K, V)>
pub fn pop(&mut self) -> Option<(K, V)>
Remove and return the element last inserted.
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Sourcepub fn truncate(&mut self, to_len: usize)
pub fn truncate(&mut self, to_len: usize)
Keeps the first to_len inserted headers, removing the remaining ones.
If to_len is equal or larger the current length nothing will happen.
This won’t affect the capacity.
Which headers are keeps/removed depends on the insertion order of the headers in the map and is independent of the headers name or if there had been other headers with the same name inserted before/after.
Sourcepub fn remove_all(&mut self, key_to_remove: K) -> bool
pub fn remove_all(&mut self, key_to_remove: K) -> bool
removes all values associated with the given key
I.e. this removes all key-value pairs which key is equal to the given key.
Returns true if at last one value was removed
Examples found in repository?
15fn main() {
16 let mut map = TotalOrderMultiMap::<Key, Value>::new();
17
18 map.add("key1", mk_box_str("val1"));
19 map.add("key1", mk_my_thingy("val2"));
20 map.add("key2", mk_box_str("val3"));
21 map.add("key1", mk_my_thingy("val4"));
22 map.add("key0", mk_box_str("val5"));
23
24 let stringed = map
25 .iter()
26 // val is of type `&Display`
27 .map(|(key, val)| format!("{}:{}", key, val))
28 .collect::<Vec<_>>();
29
30 // as it can be seen the total insertion order was kept
31 assert_eq!(stringed, &[
32 "key1:val1".to_owned(),
33 "key1:val2".to_owned(),
34 "key2:val3".to_owned(),
35 "key1:val4".to_owned(),
36 "key0:val5".to_owned()
37 ]);
38
39 // It's also still a multi map.
40 // Note that the get operation has roughly the same
41 // performance as in a hash map (through you then
42 // iterate over the elements associated with the key
43 // roughly with the speed of iterating over an `Vec`).
44 {
45 let values = map.get("key1");
46 let stringed = values
47 .map(|val| format!("{}", val))
48 .collect::<Vec<_>>();
49
50 assert_eq!(stringed, &[ "val1", "val2", "val4" ]);
51 }
52
53 // but as we have an order we can remove
54 // "the last inserted" element
55 let (key, val) = map.pop().unwrap();
56 assert_eq!(format!("{}:{}", key, val), "key0:val5");
57
58 // or remove all for an specific key as it's
59 // an multi map
60 map.remove_all("key1");
61 assert_eq!(0, map.get("key1").len(), "expected no values for key1");
62
63 println!("DONE (I only contain asserts)")
64}Trait Implementations§
Source§impl<K, V> Clone for TotalOrderMultiMap<K, V>
impl<K, V> Clone for TotalOrderMultiMap<K, V>
Source§impl<K, V> Debug for TotalOrderMultiMap<K, V>
impl<K, V> Debug for TotalOrderMultiMap<K, V>
Source§impl<K, V> Default for TotalOrderMultiMap<K, V>
impl<K, V> Default for TotalOrderMultiMap<K, V>
Source§impl<K, V> Extend<(K, V)> for TotalOrderMultiMap<K, V>
impl<K, V> Extend<(K, V)> for TotalOrderMultiMap<K, V>
Source§fn extend<I>(&mut self, src: I)where
I: IntoIterator<Item = (K, V)>,
fn extend<I>(&mut self, src: I)where
I: IntoIterator<Item = (K, V)>,
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> FromIterator<(K, V)> for TotalOrderMultiMap<K, V>
impl<K, V> FromIterator<(K, V)> for TotalOrderMultiMap<K, V>
Source§impl<K, V> IntoIterator for TotalOrderMultiMap<K, V>
impl<K, V> IntoIterator for TotalOrderMultiMap<K, V>
Source§impl<K, V> PartialEq for TotalOrderMultiMap<K, V>
Compares for equality which does consider the insertion order
impl<K, V> PartialEq for TotalOrderMultiMap<K, V>
Compares for equality which does consider the insertion order
impl<K, V> Eq for TotalOrderMultiMap<K, V>
Compares for equality which does consider the insertion order
impl<K, V> Send for TotalOrderMultiMap<K, V>
see SendSyncHelper
impl<K, V> Sync for TotalOrderMultiMap<K, V>
see SendSyncHelper