From cb3b3cf6abcd29f408b28cf3d59489a22ccc8897 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Fri, 18 Jun 2021 18:22:41 +0900 Subject: Improve `get_by_key_enumerated` more --- .../src/sorted_map/index_map.rs | 42 ++++------------------ .../rustc_data_structures/src/sorted_map/tests.rs | 6 ++-- 2 files changed, 10 insertions(+), 38 deletions(-) (limited to 'compiler/rustc_data_structures') 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 65689ab769c..2054f5eebf5 100644 --- a/compiler/rustc_data_structures/src/sorted_map/index_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map/index_map.rs @@ -1,6 +1,5 @@ //! A variant of `SortedMap` that preserves insertion order. -use std::borrow::Borrow; use std::hash::{Hash, Hasher}; use std::iter::FromIterator; @@ -76,11 +75,7 @@ impl SortedIndexMultiMap { /// /// If there are multiple items that are equivalent to `key`, they will be yielded in /// insertion order. - pub fn get_by_key(&'a self, key: &Q) -> impl 'a + Iterator - where - Q: Ord + ?Sized, - K: Borrow, - { + pub fn get_by_key(&'a self, key: K) -> impl 'a + Iterator { self.get_by_key_enumerated(key).map(|(_, v)| v) } @@ -89,35 +84,12 @@ impl SortedIndexMultiMap { /// /// If there are multiple items that are equivalent to `key`, they will be yielded in /// insertion order. - pub fn get_by_key_enumerated(&self, key: &Q) -> impl '_ + Iterator - where - Q: Ord + ?Sized, - K: Borrow, - { - match self.binary_search_idx(key) { - Err(_) => self.idxs_to_items_enumerated(&[]), - - Ok(idx) => { - let start = self.idx_sorted_by_item_key[..idx] - .partition_point(|&i| self.items[i].0.borrow() != key); - let end = idx - + self.idx_sorted_by_item_key[idx..] - .partition_point(|&i| self.items[i].0.borrow() == key); - self.idxs_to_items_enumerated(&self.idx_sorted_by_item_key[start..end]) - } - } - } - - fn binary_search_idx(&self, key: &Q) -> Result - where - Q: Ord + ?Sized, - K: Borrow, - { - self.idx_sorted_by_item_key.binary_search_by(|&idx| self.items[idx].0.borrow().cmp(key)) - } - - fn idxs_to_items_enumerated(&'a self, idxs: &'a [I]) -> impl 'a + Iterator { - idxs.iter().map(move |&idx| (idx, &self.items[idx].1)) + pub fn get_by_key_enumerated(&'a self, key: K) -> impl '_ + Iterator { + let lower_bound = self.idx_sorted_by_item_key.partition_point(|&i| self.items[i].0 < key); + self.idx_sorted_by_item_key[lower_bound..] + .iter() + .take_while(move |&&i| self.items[i].0.eq(&key)) + .map(move |&idx| (idx, &self.items[idx].1)) } } diff --git a/compiler/rustc_data_structures/src/sorted_map/tests.rs b/compiler/rustc_data_structures/src/sorted_map/tests.rs index 7d91e1fdcef..1e977d709f1 100644 --- a/compiler/rustc_data_structures/src/sorted_map/tests.rs +++ b/compiler/rustc_data_structures/src/sorted_map/tests.rs @@ -14,11 +14,11 @@ fn test_sorted_index_multi_map() { } // `get_by_key` works. - assert_eq!(set.get_by_key(&3).copied().collect::>(), vec![0]); - assert!(set.get_by_key(&4).next().is_none()); + assert_eq!(set.get_by_key(3).copied().collect::>(), vec![0]); + assert!(set.get_by_key(4).next().is_none()); // `get_by_key` returns items in insertion order. - let twos: Vec<_> = set.get_by_key_enumerated(&2).collect(); + let twos: Vec<_> = set.get_by_key_enumerated(2).collect(); let idxs: Vec = twos.iter().map(|(i, _)| *i).collect(); let values: Vec = twos.iter().map(|(_, &v)| v).collect(); -- cgit 1.4.1-3-g733a5