Struct btreemultimap::BTreeMultiMap
source · pub struct BTreeMultiMap<K, V> { /* private fields */ }
Implementations§
source§impl<K, V> BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> BTreeMultiMap<K, V>where K: Ord,
sourcepub fn new() -> BTreeMultiMap<K, V>
pub fn new() -> BTreeMultiMap<K, V>
Creates an empty BTreeMultiMap
Examples
use btreemultimap::BTreeMultiMap;
let mut map: BTreeMultiMap<&str, isize> = BTreeMultiMap::new();
source§impl<K, V> BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> BTreeMultiMap<K, V>where K: Ord,
sourcepub fn insert(&mut self, k: K, v: V)
pub fn insert(&mut self, k: K, v: V)
Inserts a key-value pair into the btreemultimap. If the key does exist in the map then the value is pushed to that key’s vector. If the key doesn’t exist in the map a new vector with the given value is inserted.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert("key", 42);
sourcepub fn insert_many<I: IntoIterator<Item = V>>(&mut self, k: K, v: I)
pub fn insert_many<I: IntoIterator<Item = V>>(&mut self, k: K, v: I)
Inserts multiple key-value pairs into the btree multimap. If the key does exist in the map then the values are extended into that key’s vector. If the key doesn’t exist in the map a new vector collected from the given values is inserted.
This may be more efficient than inserting values independently.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::<&str, &usize>::new();
map.insert_many("key", &[42, 43]);
sourcepub fn insert_many_from_slice(&mut self, k: K, v: &[V])where
V: Clone,
pub fn insert_many_from_slice(&mut self, k: K, v: &[V])where V: Clone,
Inserts multiple key-value pairs into the btree multimap. If the key does exist in the map then the values are extended into that key’s vector. If the key doesn’t exist in the map a new vector collected from the given values is inserted.
This may be more efficient than inserting values independently.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::<&str, usize>::new();
map.insert_many_from_slice("key", &[42, 43]);
sourcepub fn contains_key<Q>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn contains_key<Q>(&self, k: &Q) -> boolwhere K: Borrow<Q>, Q: Ord + ?Sized,
Returns true if the map contains a value for the specified key.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
assert_eq!(map.contains_key(&1), true);
assert_eq!(map.contains_key(&2), false);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the map.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 52);
map.insert(2, 1337);
assert_eq!(map.len(), 3);
sourcepub fn remove<Q>(&mut self, k: &Q) -> Option<Vec<V>>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn remove<Q>(&mut self, k: &Q) -> Option<Vec<V>>where K: Borrow<Q>, Q: Ord + ?Sized,
Removes a key from the map, returning the vector of values at the key if the key was previously in the map.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.remove(&1), Some(vec![42, 1337]));
assert_eq!(map.remove(&1), None);
sourcepub fn get<Q>(&self, k: &Q) -> Option<&V>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn get<Q>(&self, k: &Q) -> Option<&V>where K: Borrow<Q>, Q: Ord + ?Sized,
Returns a reference to the first item in the vector corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.get(&1), Some(&42));
sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where K: Borrow<Q>, Q: Ord + ?Sized,
Returns a mutable reference to the first item in the vector corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
if let Some(v) = map.get_mut(&1) {
*v = 99;
}
assert_eq!(map[&1], 99);
sourcepub fn get_vec<Q>(&self, k: &Q) -> Option<&Vec<V>>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn get_vec<Q>(&self, k: &Q) -> Option<&Vec<V>>where K: Borrow<Q>, Q: Ord + ?Sized,
Returns a reference to the vector corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
assert_eq!(map.get_vec(&1), Some(&vec![42, 1337]));
sourcepub fn get_key_values<Q>(&self, k: &Q) -> Option<(&K, &Vec<V>)>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn get_key_values<Q>(&self, k: &Q) -> Option<(&K, &Vec<V>)>where K: Borrow<Q>, Q: Ord + ?Sized,
Returns the key-value pair corresponding to the supplied key.
The supplied key may be any borrowed form of the map’s key type, but the ordering on the borrowed form must match the ordering on the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, "a");
assert_eq!(map.get_key_values(&1), Some((&1, &vec!["a"])));
assert_eq!(map.get_key_values(&2), None);
sourcepub fn get_vec_mut<Q>(&mut self, k: &Q) -> Option<&mut Vec<V>>where
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn get_vec_mut<Q>(&mut self, k: &Q) -> Option<&mut Vec<V>>where K: Borrow<Q>, Q: Ord + ?Sized,
Returns a mutable reference to the vector corresponding to the key.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
if let Some(v) = map.get_vec_mut(&1) {
(*v)[0] = 1991;
(*v)[1] = 2332;
}
assert_eq!(map.get_vec(&1), Some(&vec![1991, 2332]));
sourcepub fn is_vec<Q>(&self, k: &Q) -> boolwhere
K: Borrow<Q>,
Q: Ord + ?Sized,
pub fn is_vec<Q>(&self, k: &Q) -> boolwhere K: Borrow<Q>, Q: Ord + ?Sized,
Returns true if the key is multi-valued.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1, 42);
map.insert(1, 1337);
map.insert(2, 2332);
assert_eq!(map.is_vec(&1), true); // key is multi-valued
assert_eq!(map.is_vec(&2), false); // key is single-valued
assert_eq!(map.is_vec(&3), false); // key not in map
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the map contains no elements.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
assert!(map.is_empty());
map.insert(1,42);
assert!(!map.is_empty());
sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1,42);
map.clear();
assert!(map.is_empty());
sourcepub fn keys(&self) -> Keys<'_, K, Vec<V>>
pub fn keys(&self) -> Keys<'_, K, Vec<V>>
An iterator visiting all keys in arbitrary order. Iterator element type is &’a K.
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1,42);
map.insert(2,1337);
map.insert(4,1991);
for key in map.keys() {
println!("{:?}", key);
}
sourcepub fn iter(&self) -> MultiIter<'_, K, V> ⓘ
pub fn iter(&self) -> MultiIter<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a V).
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);
for (key, value) in map.iter() {
println!("key: {:?}, val: {:?}", key, value);
}
sourcepub fn iter_mut(&mut self) -> MultiIterMut<'_, K, V> ⓘ
pub fn iter_mut(&mut self) -> MultiIterMut<'_, K, V> ⓘ
An iterator visiting all key-value pairs in arbitrary order. The iterator returns a reference to the key and a mutable reference to the first element in the corresponding key’s vector. Iterator element type is (&’a K, &’a mut V).
Examples
use btreemultimap::BTreeMultiMap;
let mut map = BTreeMultiMap::new();
map.insert(1,42);
map.insert(1,1337);
map.insert(3,2332);
map.insert(4,1991);
for (_, value) in map.iter_mut() {
*value *= *value;
}
for (key, value) in map.iter() {
println!("key: {:?}, val: {:?}", key, value);
}
sourcepub fn entry(&mut self, k: K) -> Entry<'_, K, V>
pub fn entry(&mut self, k: K) -> Entry<'_, K, V>
Gets the specified key’s corresponding entry in the map for in-place manipulation. It’s possible to both manipulate the vector and the ‘value’ (the first value in the vector).
Examples
use btreemultimap::BTreeMultiMap;
let mut m = BTreeMultiMap::new();
m.insert(1, 42);
{
let mut v = m.entry(1).or_insert(43);
assert_eq!(v, &42);
*v = 44;
}
assert_eq!(m.entry(2).or_insert(666), &666);
{
let mut v = m.entry(1).or_insert_vec(vec![43]);
assert_eq!(v, &vec![44]);
v.push(50);
}
assert_eq!(m.entry(2).or_insert_vec(vec![666]), &vec![666]);
assert_eq!(m.get_vec(&1), Some(&vec![44, 50]));
sourcepub fn retain<F>(&mut self, f: F)where
F: FnMut(&K, &V) -> bool,
pub fn retain<F>(&mut self, f: F)where F: FnMut(&K, &V) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v)
such that f(&k,&mut v)
returns false
.
Examples
use btreemultimap::BTreeMultiMap;
let mut m = BTreeMultiMap::new();
m.insert(1, 42);
m.insert(1, 99);
m.insert(2, 42);
m.retain(|&k, &v| { k == 1 && v == 42 });
assert_eq!(1, m.len());
assert_eq!(Some(&42), m.get(&1));
sourcepub fn range<T, R>(&self, range: R) -> MultiRange<'_, K, V> ⓘwhere
T: Ord + ?Sized,
K: Borrow<T>,
R: RangeBounds<T>,
pub fn range<T, R>(&self, range: R) -> MultiRange<'_, K, V> ⓘwhere T: Ord + ?Sized, K: Borrow<T>, R: RangeBounds<T>,
Constructs a double-ended iterator over a sub-range of elements in the map.
The simplest way is to use the range syntax min..max
, thus range(min..max)
will
yield elements from min (inclusive) to max (exclusive).
The range may also be entered as (Bound<T>, Bound<T>)
, so for example
range((Excluded(4), Included(10)))
will yield a left-exclusive, right-inclusive
range from 4 to 10.
Panics
Panics if range start > end
.
Panics if range start == end
and both bounds are Excluded
.
Examples
Basic usage:
use btreemultimap::BTreeMultiMap;
use std::ops::Bound::Included;
let mut map = BTreeMultiMap::new();
map.insert(3, "a");
map.insert(5, "b");
map.insert(5, "c");
map.insert(8, "c");
map.insert(9, "d");
for (&key, &value) in map.range((Included(&4), Included(&8))) {
println!("{}: {}", key, value);
}
let mut iter = map.range(4..=8);
assert_eq!(Some((&5, &"b")), iter.next());
assert_eq!(Some((&5, &"c")), iter.next());
assert_eq!(Some((&8, &"c")), iter.next());
assert_eq!(None, iter.next());
sourcepub fn range_mut<T, R>(&mut self, range: R) -> MultiRangeMut<'_, K, V> ⓘwhere
T: Ord + ?Sized,
K: Borrow<T>,
R: RangeBounds<T>,
pub fn range_mut<T, R>(&mut self, range: R) -> MultiRangeMut<'_, K, V> ⓘwhere T: Ord + ?Sized, K: Borrow<T>, R: RangeBounds<T>,
Constructs a mutable double-ended iterator over a sub-range of elements in the map.
The simplest way is to use the range syntax min..max
, thus range(min..max)
will
yield elements from min (inclusive) to max (exclusive).
The range may also be entered as (Bound<T>, Bound<T>)
, so for example
range((Excluded(4), Included(10)))
will yield a left-exclusive, right-inclusive
range from 4 to 10.
Panics
Panics if range start > end
.
Panics if range start == end
and both bounds are Excluded
.
Examples
Basic usage:
use btreemultimap::BTreeMultiMap;
let mut map: BTreeMultiMap<&str, i32> = ["Alice", "Bob", "Carol", "Cheryl"]
.iter()
.map(|&s| (s, 0))
.collect();
for (_, balance) in map.range_mut("B".."Cheryl") {
*balance += 100;
}
for (name, balance) in &map {
println!("{} => {}", name, balance);
}
Trait Implementations§
source§impl<K: Clone, V: Clone> Clone for BTreeMultiMap<K, V>
impl<K: Clone, V: Clone> Clone for BTreeMultiMap<K, V>
source§fn clone(&self) -> BTreeMultiMap<K, V>
fn clone(&self) -> BTreeMultiMap<K, V>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<K, V> Default for BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> Default for BTreeMultiMap<K, V>where K: Ord,
source§fn default() -> BTreeMultiMap<K, V>
fn default() -> BTreeMultiMap<K, V>
source§impl<'a, K, V> Deserialize<'a> for BTreeMultiMap<K, V>where
K: Deserialize<'a> + Ord,
V: Deserialize<'a>,
impl<'a, K, V> Deserialize<'a> for BTreeMultiMap<K, V>where K: Deserialize<'a> + Ord, V: Deserialize<'a>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'a>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'a>,
source§impl<'a, K, V> Extend<(&'a K, &'a V)> for BTreeMultiMap<K, V>where
K: Ord + Copy,
V: Copy,
impl<'a, K, V> Extend<(&'a K, &'a V)> for BTreeMultiMap<K, V>where K: Ord + Copy, V: Copy,
source§fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a V)>>(&mut self, iter: T)
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<'a, K, V> Extend<(&'a K, &'a Vec<V, Global>)> for BTreeMultiMap<K, V>where
K: Ord + Copy,
V: Copy,
impl<'a, K, V> Extend<(&'a K, &'a Vec<V, Global>)> for BTreeMultiMap<K, V>where K: Ord + Copy, V: Copy,
source§fn extend<T: IntoIterator<Item = (&'a K, &'a Vec<V>)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (&'a K, &'a Vec<V>)>>(&mut self, iter: T)
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> Extend<(K, V)> for BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> Extend<(K, V)> for BTreeMultiMap<K, V>where K: Ord,
source§fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
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> Extend<(K, Vec<V, Global>)> for BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> Extend<(K, Vec<V, Global>)> for BTreeMultiMap<K, V>where K: Ord,
source§fn extend<T: IntoIterator<Item = (K, Vec<V>)>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = (K, Vec<V>)>>(&mut self, iter: T)
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 BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> FromIterator<(K, V)> for BTreeMultiMap<K, V>where K: Ord,
source§fn from_iter<T: IntoIterator<Item = (K, V)>>(iterable: T) -> BTreeMultiMap<K, V>
fn from_iter<T: IntoIterator<Item = (K, V)>>(iterable: T) -> BTreeMultiMap<K, V>
source§impl<'a, K, V, Q> Index<&'a Q> for BTreeMultiMap<K, V>where
K: Ord + Borrow<Q>,
Q: Ord + ?Sized,
impl<'a, K, V, Q> Index<&'a Q> for BTreeMultiMap<K, V>where K: Ord + Borrow<Q>, Q: Ord + ?Sized,
source§impl<'a, K, V> IntoIterator for &'a BTreeMultiMap<K, V>where
K: Ord,
impl<'a, K, V> IntoIterator for &'a BTreeMultiMap<K, V>where K: Ord,
source§impl<'a, K, V> IntoIterator for &'a mut BTreeMultiMap<K, V>where
K: Ord,
impl<'a, K, V> IntoIterator for &'a mut BTreeMultiMap<K, V>where K: Ord,
source§impl<K, V> IntoIterator for BTreeMultiMap<K, V>where
K: Ord,
impl<K, V> IntoIterator for BTreeMultiMap<K, V>where K: Ord,
source§impl<K, V> PartialEq<BTreeMultiMap<K, V>> for BTreeMultiMap<K, V>where
K: Ord,
V: PartialEq,
impl<K, V> PartialEq<BTreeMultiMap<K, V>> for BTreeMultiMap<K, V>where K: Ord, V: PartialEq,
source§fn eq(&self, other: &BTreeMultiMap<K, V>) -> bool
fn eq(&self, other: &BTreeMultiMap<K, V>) -> bool
self
and other
values to be equal, and is used
by ==
.