Add contains_key to SortedIndexMultiMap

This commit is contained in:
Jesse Bakker 2023-02-28 17:08:04 +01:00
parent 5157d938c4
commit edf053036f
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();