Skip to content

Commit 4bedc31

Browse files
Cleanup SortedMap by wrapping element lookup in a method.
1 parent e850d78 commit 4bedc31

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

src/librustc_data_structures/sorted_map.rs

+16-21
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ impl<K: Ord, V> SortedMap<K, V> {
4242

4343
#[inline]
4444
pub fn insert(&mut self, key: K, mut value: V) -> Option<V> {
45-
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(&key));
46-
47-
match index {
45+
match self.lookup_index_for(&key) {
4846
Ok(index) => {
4947
let mut slot = unsafe {
5048
self.data.get_unchecked_mut(index)
@@ -61,9 +59,7 @@ impl<K: Ord, V> SortedMap<K, V> {
6159

6260
#[inline]
6361
pub fn remove(&mut self, key: &K) -> Option<V> {
64-
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
65-
66-
match index {
62+
match self.lookup_index_for(key) {
6763
Ok(index) => {
6864
Some(self.data.remove(index).1)
6965
}
@@ -75,9 +71,7 @@ impl<K: Ord, V> SortedMap<K, V> {
7571

7672
#[inline]
7773
pub fn get(&self, key: &K) -> Option<&V> {
78-
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
79-
80-
match index {
74+
match self.lookup_index_for(key) {
8175
Ok(index) => {
8276
unsafe {
8377
Some(&self.data.get_unchecked(index).1)
@@ -91,9 +85,7 @@ impl<K: Ord, V> SortedMap<K, V> {
9185

9286
#[inline]
9387
pub fn get_mut(&mut self, key: &K) -> Option<&mut V> {
94-
let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key));
95-
96-
match index {
88+
match self.lookup_index_for(key) {
9789
Ok(index) => {
9890
unsafe {
9991
Some(&mut self.data.get_unchecked_mut(index).1)
@@ -168,12 +160,9 @@ impl<K: Ord, V> SortedMap<K, V> {
168160

169161
debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0));
170162

171-
let index = {
172-
let first_element = &elements[0].0;
173-
self.data.binary_search_by(|&(ref x, _)| x.cmp(first_element))
174-
};
163+
let start_index = self.lookup_index_for(&elements[0].0);
175164

176-
let drain = match index {
165+
let drain = match start_index {
177166
Ok(index) => {
178167
let mut drain = elements.drain(..);
179168
self.data[index] = drain.next().unwrap();
@@ -200,18 +189,24 @@ impl<K: Ord, V> SortedMap<K, V> {
200189
}
201190
}
202191

192+
/// Looks up the key in `self.data` via `slice::binary_search()`.
193+
#[inline(always)]
194+
fn lookup_index_for(&self, key: &K) -> Result<usize, usize> {
195+
self.data.binary_search_by(|&(ref x, _)| x.cmp(key))
196+
}
197+
203198
#[inline]
204199
fn range_slice_indices<R>(&self, range: R) -> (usize, usize)
205200
where R: RangeBounds<K>
206201
{
207202
let start = match range.start() {
208203
Bound::Included(ref k) => {
209-
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
204+
match self.lookup_index_for(k) {
210205
Ok(index) | Err(index) => index
211206
}
212207
}
213208
Bound::Excluded(ref k) => {
214-
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
209+
match self.lookup_index_for(k) {
215210
Ok(index) => index + 1,
216211
Err(index) => index,
217212
}
@@ -221,13 +216,13 @@ impl<K: Ord, V> SortedMap<K, V> {
221216

222217
let end = match range.end() {
223218
Bound::Included(ref k) => {
224-
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
219+
match self.lookup_index_for(k) {
225220
Ok(index) => index + 1,
226221
Err(index) => index,
227222
}
228223
}
229224
Bound::Excluded(ref k) => {
230-
match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) {
225+
match self.lookup_index_for(k) {
231226
Ok(index) | Err(index) => index,
232227
}
233228
}

0 commit comments

Comments
 (0)