From 875ce5f851c3773cea3fb4af8d9e771e3e68dbc5 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 18 Nov 2018 10:41:40 +0100 Subject: [PATCH 1/5] sorted_map: change From> to FromIterator --- src/librustc_data_structures/sorted_map.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index 29d99a6aef3f2..212c83894a619 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -10,7 +10,7 @@ use std::borrow::Borrow; use std::cmp::Ordering; -use std::convert::From; +use std::iter::FromIterator; use std::mem; use std::ops::{RangeBounds, Bound, Index, IndexMut}; @@ -272,9 +272,9 @@ impl> IndexMut for SortedMap { } } -impl> From for SortedMap { - fn from(data: I) -> Self { - let mut data: Vec<(K, V)> = data.collect(); +impl FromIterator<(K, V)> for SortedMap { + fn from_iter>(iter: T) -> Self { + let mut data: Vec<(K, V)> = iter.into_iter().collect(); data.sort_unstable_by(|&(ref k1, _), &(ref k2, _)| k1.cmp(k2)); data.dedup_by(|&mut (ref k1, _), &mut (ref k2, _)| { k1.cmp(k2) == Ordering::Equal From 61de47dd259811e23fc85e3250974ba5b583e02b Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 18 Nov 2018 10:43:50 +0100 Subject: [PATCH 2/5] sorted_map: make the impls of Index and get match ones from BTreeMap --- src/librustc_data_structures/sorted_map.rs | 38 +++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index 212c83894a619..5f45f430183bc 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -82,7 +82,10 @@ impl SortedMap { } #[inline] - pub fn get(&self, key: &K) -> Option<&V> { + pub fn get(&self, key: &Q) -> Option<&V> + where K: Borrow, + Q: Ord + ?Sized + { match self.lookup_index_for(key) { Ok(index) => { unsafe { @@ -96,7 +99,10 @@ impl SortedMap { } #[inline] - pub fn get_mut(&mut self, key: &K) -> Option<&mut V> { + pub fn get_mut(&mut self, key: &Q) -> Option<&mut V> + where K: Borrow, + Q: Ord + ?Sized + { match self.lookup_index_for(key) { Ok(index) => { unsafe { @@ -207,8 +213,11 @@ 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)) + fn lookup_index_for(&self, key: &Q) -> Result + where K: Borrow, + Q: Ord + ?Sized + { + self.data.binary_search_by(|&(ref x, _)| x.borrow().cmp(key)) } #[inline] @@ -257,18 +266,23 @@ impl IntoIterator for SortedMap { } } -impl> Index for SortedMap { +impl<'a, K, Q, V> Index<&'a Q> for SortedMap + where K: Ord + Borrow, + Q: Ord + ?Sized +{ type Output = V; - fn index(&self, index: Q) -> &Self::Output { - let k: &K = index.borrow(); - self.get(k).unwrap() + + fn index(&self, key: &Q) -> &Self::Output { + self.get(key).expect("no entry found for key") } } -impl> IndexMut for SortedMap { - fn index_mut(&mut self, index: Q) -> &mut Self::Output { - let k: &K = index.borrow(); - self.get_mut(k).unwrap() +impl<'a, K, Q, V> IndexMut<&'a Q> for SortedMap + where K: Ord + Borrow, + Q: Ord + ?Sized +{ + fn index_mut(&mut self, key: &Q) -> &mut Self::Output { + self.get_mut(key).expect("no entry found for key") } } From eb772045f88ba8af8555f086b425225fbc891aa0 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 18 Nov 2018 10:44:25 +0100 Subject: [PATCH 3/5] sorted_map: add is_empty --- src/librustc_data_structures/sorted_map.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index 5f45f430183bc..c8f8579719108 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -143,6 +143,11 @@ impl SortedMap { self.data.len() } + #[inline] + pub fn is_empty(&self) -> bool { + self.len() == 0 + } + #[inline] pub fn range(&self, range: R) -> &[(K, V)] where R: RangeBounds From 08c6bda3eed64f3a7e767f9e3792a6e94d395839 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 18 Nov 2018 10:44:49 +0100 Subject: [PATCH 4/5] sorted_map: readability/whitespace fixes --- src/librustc_data_structures/sorted_map.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index c8f8579719108..2fa8453c4b55c 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -25,11 +25,10 @@ use std::ops::{RangeBounds, Bound, Index, IndexMut}; #[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Debug, RustcEncodable, RustcDecodable)] pub struct SortedMap { - data: Vec<(K,V)> + data: Vec<(K, V)> } impl SortedMap { - #[inline] pub fn new() -> SortedMap { SortedMap { @@ -128,13 +127,13 @@ impl SortedMap { /// Iterate over the keys, sorted #[inline] - pub fn keys(&self) -> impl Iterator + ExactSizeIterator { + pub fn keys(&self) -> impl Iterator + ExactSizeIterator { self.data.iter().map(|&(ref k, _)| k) } /// Iterate over values, sorted by key #[inline] - pub fn values(&self) -> impl Iterator + ExactSizeIterator { + pub fn values(&self) -> impl Iterator + ExactSizeIterator { self.data.iter().map(|&(_, ref v)| v) } @@ -266,6 +265,7 @@ impl SortedMap { impl IntoIterator for SortedMap { type Item = (K, V); type IntoIter = ::std::vec::IntoIter<(K, V)>; + fn into_iter(self) -> Self::IntoIter { self.data.into_iter() } @@ -294,10 +294,12 @@ impl<'a, K, Q, V> IndexMut<&'a Q> for SortedMap impl FromIterator<(K, V)> for SortedMap { fn from_iter>(iter: T) -> Self { let mut data: Vec<(K, V)> = iter.into_iter().collect(); + data.sort_unstable_by(|&(ref k1, _), &(ref k2, _)| k1.cmp(k2)); data.dedup_by(|&mut (ref k1, _), &mut (ref k2, _)| { k1.cmp(k2) == Ordering::Equal }); + SortedMap { data } From 5b6401f09db502b9e596bf7d756ccc97029dfe31 Mon Sep 17 00:00:00 2001 From: ljedrz Date: Sun, 18 Nov 2018 19:00:22 +0100 Subject: [PATCH 5/5] sorted_map: add contains_key function --- src/librustc_data_structures/sorted_map.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/librustc_data_structures/sorted_map.rs b/src/librustc_data_structures/sorted_map.rs index 2fa8453c4b55c..3bd3d11660797 100644 --- a/src/librustc_data_structures/sorted_map.rs +++ b/src/librustc_data_structures/sorted_map.rs @@ -260,6 +260,14 @@ impl SortedMap { (start, end) } + + #[inline] + pub fn contains_key(&self, key: &Q) -> bool + where K: Borrow, + Q: Ord + ?Sized + { + self.get(key).is_some() + } } impl IntoIterator for SortedMap {