diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-03-01 01:21:59 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-01 01:21:59 +0100 |
| commit | 168589426c10dc0672ffdec58de0c3c9eca067d2 (patch) | |
| tree | bd58a7bb4c8d0a5c3bc2c76270502390ba59053c /compiler | |
| parent | 41eb28dc18a57001a9b9e9df8c87d83c8a39c850 (diff) | |
| parent | edf053036f7db0839f54b8b05d159551f3eacc3b (diff) | |
| download | rust-168589426c10dc0672ffdec58de0c3c9eca067d2.tar.gz rust-168589426c10dc0672ffdec58de0c3c9eca067d2.zip | |
Rollup merge of #108571 - Jesse-Bakker:sorted_index_multi_map_contains_key, r=Nilstrieb
Add contains_key to SortedIndexMultiMap
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_data_structures/src/sorted_map/index_map.rs | 5 | ||||
| -rw-r--r-- | compiler/rustc_data_structures/src/sorted_map/tests.rs | 4 |
2 files changed, 9 insertions, 0 deletions
diff --git a/compiler/rustc_data_structures/src/sorted_map/index_map.rs b/compiler/rustc_data_structures/src/sorted_map/index_map.rs index 814e7c7fb9b..7d23ff51948 100644 --- a/compiler/rustc_data_structures/src/sorted_map/index_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map/index_map.rs @@ -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> {} diff --git a/compiler/rustc_data_structures/src/sorted_map/tests.rs b/compiler/rustc_data_structures/src/sorted_map/tests.rs index 3cc250862df..def7a7112fb 100644 --- a/compiler/rustc_data_structures/src/sorted_map/tests.rs +++ b/compiler/rustc_data_structures/src/sorted_map/tests.rs @@ -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(); |
