Rollup merge of #108571 - Jesse-Bakker:sorted_index_multi_map_contains_key, r=Nilstrieb

Add contains_key to SortedIndexMultiMap
This commit is contained in:
Matthias Krüger 2023-03-01 01:21:59 +01:00 committed by GitHub
commit 168589426c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View file

@ -100,6 +100,11 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
(k == &key).then_some((i, v))
})
}
#[inline]
pub fn contains_key(&self, key: K) -> bool {
self.get_by_key(key).next().is_some()
}
}
impl<I: Idx, K: Eq, V: Eq> Eq for SortedIndexMultiMap<I, K, V> {}

View file

@ -17,6 +17,10 @@ fn test_sorted_index_multi_map() {
assert_eq!(set.get_by_key(3).copied().collect::<Vec<_>>(), vec![0]);
assert!(set.get_by_key(4).next().is_none());
// `contains_key` works
assert!(set.contains_key(3));
assert!(!set.contains_key(4));
// `get_by_key` returns items in insertion order.
let twos: Vec<_> = set.get_by_key_enumerated(2).collect();
let idxs: Vec<usize> = twos.iter().map(|(i, _)| *i).collect();