Skip to content

Commit 3b4a0df

Browse files
committed
Auto merge of rust-lang#86429 - JohnTitor:get-by-key-enum-part-2, r=oli-obk
Improve `get_by_key_enumerated` more Follow-up of rust-lang#86392, this applies the suggestions by `@m-ou-se.` r? `@m-ou-se`
2 parents 67b0300 + 6761826 commit 3b4a0df

File tree

4 files changed

+24
-50
lines changed

4 files changed

+24
-50
lines changed

compiler/rustc_data_structures/src/lib.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,26 @@
77
//! This API is completely unstable and subject to change.
88
99
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
10+
#![feature(allow_internal_unstable)]
1011
#![feature(array_windows)]
12+
#![feature(associated_type_bounds)]
13+
#![feature(auto_traits)]
14+
#![feature(bool_to_option)]
15+
#![feature(const_panic)]
1116
#![feature(control_flow_enum)]
17+
#![feature(core_intrinsics)]
18+
#![feature(extend_one)]
19+
#![feature(hash_raw_entry)]
1220
#![feature(in_band_lifetimes)]
21+
#![feature(iter_map_while)]
22+
#![feature(maybe_uninit_uninit_array)]
1323
#![feature(min_specialization)]
14-
#![feature(auto_traits)]
24+
#![feature(min_type_alias_impl_trait)]
25+
#![feature(new_uninit)]
1526
#![feature(nll)]
16-
#![feature(allow_internal_unstable)]
17-
#![feature(hash_raw_entry)]
18-
#![feature(core_intrinsics)]
27+
#![feature(once_cell)]
1928
#![feature(test)]
20-
#![feature(associated_type_bounds)]
2129
#![feature(thread_id_value)]
22-
#![feature(extend_one)]
23-
#![feature(const_panic)]
24-
#![feature(new_uninit)]
25-
#![feature(once_cell)]
26-
#![feature(maybe_uninit_uninit_array)]
27-
#![feature(min_type_alias_impl_trait)]
2830
#![allow(rustc::default_hash_types)]
2931
#![deny(unaligned_references)]
3032

compiler/rustc_data_structures/src/sorted_map/index_map.rs

+7-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! A variant of `SortedMap` that preserves insertion order.
22
3-
use std::borrow::Borrow;
43
use std::hash::{Hash, Hasher};
54
use std::iter::FromIterator;
65

@@ -76,11 +75,7 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
7675
///
7776
/// If there are multiple items that are equivalent to `key`, they will be yielded in
7877
/// insertion order.
79-
pub fn get_by_key<Q: 'a>(&'a self, key: &Q) -> impl 'a + Iterator<Item = &'a V>
80-
where
81-
Q: Ord + ?Sized,
82-
K: Borrow<Q>,
83-
{
78+
pub fn get_by_key(&'a self, key: K) -> impl 'a + Iterator<Item = &'a V> {
8479
self.get_by_key_enumerated(key).map(|(_, v)| v)
8580
}
8681

@@ -89,35 +84,12 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
8984
///
9085
/// If there are multiple items that are equivalent to `key`, they will be yielded in
9186
/// insertion order.
92-
pub fn get_by_key_enumerated<Q>(&self, key: &Q) -> impl '_ + Iterator<Item = (I, &V)>
93-
where
94-
Q: Ord + ?Sized,
95-
K: Borrow<Q>,
96-
{
97-
match self.binary_search_idx(key) {
98-
Err(_) => self.idxs_to_items_enumerated(&[]),
99-
100-
Ok(idx) => {
101-
let start = self.idx_sorted_by_item_key[..idx]
102-
.partition_point(|&i| self.items[i].0.borrow() != key);
103-
let end = idx
104-
+ self.idx_sorted_by_item_key[idx..]
105-
.partition_point(|&i| self.items[i].0.borrow() == key);
106-
self.idxs_to_items_enumerated(&self.idx_sorted_by_item_key[start..end])
107-
}
108-
}
109-
}
110-
111-
fn binary_search_idx<Q>(&self, key: &Q) -> Result<usize, usize>
112-
where
113-
Q: Ord + ?Sized,
114-
K: Borrow<Q>,
115-
{
116-
self.idx_sorted_by_item_key.binary_search_by(|&idx| self.items[idx].0.borrow().cmp(key))
117-
}
118-
119-
fn idxs_to_items_enumerated(&'a self, idxs: &'a [I]) -> impl 'a + Iterator<Item = (I, &'a V)> {
120-
idxs.iter().map(move |&idx| (idx, &self.items[idx].1))
87+
pub fn get_by_key_enumerated(&'a self, key: K) -> impl '_ + Iterator<Item = (I, &V)> {
88+
let lower_bound = self.idx_sorted_by_item_key.partition_point(|&i| self.items[i].0 < key);
89+
self.idx_sorted_by_item_key[lower_bound..].iter().map_while(move |&i| {
90+
let (k, v) = &self.items[i];
91+
(k == &key).then_some((i, v))
92+
})
12193
}
12294
}
12395

compiler/rustc_data_structures/src/sorted_map/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ fn test_sorted_index_multi_map() {
1414
}
1515

1616
// `get_by_key` works.
17-
assert_eq!(set.get_by_key(&3).copied().collect::<Vec<_>>(), vec![0]);
18-
assert!(set.get_by_key(&4).next().is_none());
17+
assert_eq!(set.get_by_key(3).copied().collect::<Vec<_>>(), vec![0]);
18+
assert!(set.get_by_key(4).next().is_none());
1919

2020
// `get_by_key` returns items in insertion order.
21-
let twos: Vec<_> = set.get_by_key_enumerated(&2).collect();
21+
let twos: Vec<_> = set.get_by_key_enumerated(2).collect();
2222
let idxs: Vec<usize> = twos.iter().map(|(i, _)| *i).collect();
2323
let values: Vec<usize> = twos.iter().map(|(_, &v)| v).collect();
2424

compiler/rustc_middle/src/ty/assoc.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl<'tcx> AssocItems<'tcx> {
124124
&self,
125125
name: Symbol,
126126
) -> impl '_ + Iterator<Item = &ty::AssocItem> {
127-
self.items.get_by_key(&name).copied()
127+
self.items.get_by_key(name).copied()
128128
}
129129

130130
/// Returns an iterator over all associated items with the given name.

0 commit comments

Comments
 (0)