@@ -94,13 +94,15 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
94
94
Q : Ord + ?Sized ,
95
95
K : Borrow < Q > ,
96
96
{
97
- // FIXME: This should be in the standard library as `equal_range`. See rust-lang/rfcs#2184.
98
97
match self . binary_search_idx ( key) {
99
98
Err ( _) => self . idxs_to_items_enumerated ( & [ ] ) ,
100
99
101
100
Ok ( idx) => {
102
- let start = self . find_lower_bound ( key, idx) ;
103
- let end = self . find_upper_bound ( key, 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) ;
104
106
self . idxs_to_items_enumerated ( & self . idx_sorted_by_item_key [ start..end] )
105
107
}
106
108
}
@@ -114,50 +116,6 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
114
116
self . idx_sorted_by_item_key . binary_search_by ( |& idx| self . items [ idx] . 0 . borrow ( ) . cmp ( key) )
115
117
}
116
118
117
- /// Returns the index into the `idx_sorted_by_item_key` array of the first item equal to
118
- /// `key`.
119
- ///
120
- /// `initial` must be an index into that same array for an item that is equal to `key`.
121
- fn find_lower_bound < Q > ( & self , key : & Q , initial : usize ) -> usize
122
- where
123
- Q : Ord + ?Sized ,
124
- K : Borrow < Q > ,
125
- {
126
- debug_assert ! ( self . items[ self . idx_sorted_by_item_key[ initial] ] . 0 . borrow( ) == key) ;
127
-
128
- // FIXME: At present, this uses linear search, meaning lookup is only `O(log n)` if duplicate
129
- // entries are rare. It would be better to start with a linear search for the common case but
130
- // fall back to an exponential search if many duplicates are found. This applies to
131
- // `upper_bound` as well.
132
- let mut start = initial;
133
- while start != 0 && self . items [ self . idx_sorted_by_item_key [ start - 1 ] ] . 0 . borrow ( ) == key {
134
- start -= 1 ;
135
- }
136
-
137
- start
138
- }
139
-
140
- /// Returns the index into the `idx_sorted_by_item_key` array of the first item greater than
141
- /// `key`, or `self.len()` if no such item exists.
142
- ///
143
- /// `initial` must be an index into that same array for an item that is equal to `key`.
144
- fn find_upper_bound < Q > ( & self , key : & Q , initial : usize ) -> usize
145
- where
146
- Q : Ord + ?Sized ,
147
- K : Borrow < Q > ,
148
- {
149
- debug_assert ! ( self . items[ self . idx_sorted_by_item_key[ initial] ] . 0 . borrow( ) == key) ;
150
-
151
- // See the FIXME for `find_lower_bound`.
152
- let mut end = initial + 1 ;
153
- let len = self . items . len ( ) ;
154
- while end < len && self . items [ self . idx_sorted_by_item_key [ end] ] . 0 . borrow ( ) == key {
155
- end += 1 ;
156
- }
157
-
158
- end
159
- }
160
-
161
119
fn idxs_to_items_enumerated ( & ' a self , idxs : & ' a [ I ] ) -> impl ' a + Iterator < Item = ( I , & ' a V ) > {
162
120
idxs. iter ( ) . map ( move |& idx| ( idx, & self . items [ idx] . 1 ) )
163
121
}
0 commit comments