From 4bedc314597c20cbaf68ac094e42ba569fcc8973 Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Mon, 21 May 2018 19:16:26 +0200 Subject: [PATCH] Cleanup SortedMap by wrapping element lookup in a method. --- src/librustc_data_structures/sorted_map.rs | 37 ++++++++++------------ 1 file changed, 16 insertions(+), 21 deletions(-) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index fd2bf9272a7..cdce1e77c0f 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -42,9 +42,7 @@ impl SortedMap { #[inline] pub fn insert(&mut self, key: K, mut value: V) -> Option { - let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(&key)); - - match index { + match self.lookup_index_for(&key) { Ok(index) => { let mut slot = unsafe { self.data.get_unchecked_mut(index) @@ -61,9 +59,7 @@ impl SortedMap { #[inline] pub fn remove(&mut self, key: &K) -> Option { - let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key)); - - match index { + match self.lookup_index_for(key) { Ok(index) => { Some(self.data.remove(index).1) } @@ -75,9 +71,7 @@ impl SortedMap { #[inline] pub fn get(&self, key: &K) -> Option<&V> { - let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key)); - - match index { + match self.lookup_index_for(key) { Ok(index) => { unsafe { Some(&self.data.get_unchecked(index).1) @@ -91,9 +85,7 @@ impl SortedMap { #[inline] pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { - let index = self.data.binary_search_by(|&(ref x, _)| x.cmp(key)); - - match index { + match self.lookup_index_for(key) { Ok(index) => { unsafe { Some(&mut self.data.get_unchecked_mut(index).1) @@ -168,12 +160,9 @@ impl SortedMap { debug_assert!(elements.windows(2).all(|w| w[0].0 < w[1].0)); - let index = { - let first_element = &elements[0].0; - self.data.binary_search_by(|&(ref x, _)| x.cmp(first_element)) - }; + let start_index = self.lookup_index_for(&elements[0].0); - let drain = match index { + let drain = match start_index { Ok(index) => { let mut drain = elements.drain(..); self.data[index] = drain.next().unwrap(); @@ -200,18 +189,24 @@ impl SortedMap { } } + /// Looks up the key in `self.data` via `slice::binary_search()`. + #[inline(always)] + fn lookup_index_for(&self, key: &K) -> Result { + self.data.binary_search_by(|&(ref x, _)| x.cmp(key)) + } + #[inline] fn range_slice_indices(&self, range: R) -> (usize, usize) where R: RangeBounds { let start = match range.start() { Bound::Included(ref k) => { - match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) { + match self.lookup_index_for(k) { Ok(index) | Err(index) => index } } Bound::Excluded(ref k) => { - match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) { + match self.lookup_index_for(k) { Ok(index) => index + 1, Err(index) => index, } @@ -221,13 +216,13 @@ impl SortedMap { let end = match range.end() { Bound::Included(ref k) => { - match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) { + match self.lookup_index_for(k) { Ok(index) => index + 1, Err(index) => index, } } Bound::Excluded(ref k) => { - match self.data.binary_search_by(|&(ref x, _)| x.cmp(k)) { + match self.lookup_index_for(k) { Ok(index) | Err(index) => index, } }