10
10
11
11
use std:: borrow:: Borrow ;
12
12
use std:: cmp:: Ordering ;
13
- use std:: convert :: From ;
13
+ use std:: iter :: FromIterator ;
14
14
use std:: mem;
15
15
use std:: ops:: { RangeBounds , Bound , Index , IndexMut } ;
16
16
@@ -25,11 +25,10 @@ use std::ops::{RangeBounds, Bound, Index, IndexMut};
25
25
#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default , Debug , RustcEncodable ,
26
26
RustcDecodable ) ]
27
27
pub struct SortedMap < K : Ord , V > {
28
- data : Vec < ( K , V ) >
28
+ data : Vec < ( K , V ) >
29
29
}
30
30
31
31
impl < K : Ord , V > SortedMap < K , V > {
32
-
33
32
#[ inline]
34
33
pub fn new ( ) -> SortedMap < K , V > {
35
34
SortedMap {
@@ -82,7 +81,10 @@ impl<K: Ord, V> SortedMap<K, V> {
82
81
}
83
82
84
83
#[ inline]
85
- pub fn get ( & self , key : & K ) -> Option < & V > {
84
+ pub fn get < Q > ( & self , key : & Q ) -> Option < & V >
85
+ where K : Borrow < Q > ,
86
+ Q : Ord + ?Sized
87
+ {
86
88
match self . lookup_index_for ( key) {
87
89
Ok ( index) => {
88
90
unsafe {
@@ -96,7 +98,10 @@ impl<K: Ord, V> SortedMap<K, V> {
96
98
}
97
99
98
100
#[ inline]
99
- pub fn get_mut ( & mut self , key : & K ) -> Option < & mut V > {
101
+ pub fn get_mut < Q > ( & mut self , key : & Q ) -> Option < & mut V >
102
+ where K : Borrow < Q > ,
103
+ Q : Ord + ?Sized
104
+ {
100
105
match self . lookup_index_for ( key) {
101
106
Ok ( index) => {
102
107
unsafe {
@@ -122,13 +127,13 @@ impl<K: Ord, V> SortedMap<K, V> {
122
127
123
128
/// Iterate over the keys, sorted
124
129
#[ inline]
125
- pub fn keys ( & self ) -> impl Iterator < Item = & K > + ExactSizeIterator {
130
+ pub fn keys ( & self ) -> impl Iterator < Item = & K > + ExactSizeIterator {
126
131
self . data . iter ( ) . map ( |& ( ref k, _) | k)
127
132
}
128
133
129
134
/// Iterate over values, sorted by key
130
135
#[ inline]
131
- pub fn values ( & self ) -> impl Iterator < Item = & V > + ExactSizeIterator {
136
+ pub fn values ( & self ) -> impl Iterator < Item = & V > + ExactSizeIterator {
132
137
self . data . iter ( ) . map ( |& ( _, ref v) | v)
133
138
}
134
139
@@ -137,6 +142,11 @@ impl<K: Ord, V> SortedMap<K, V> {
137
142
self . data . len ( )
138
143
}
139
144
145
+ #[ inline]
146
+ pub fn is_empty ( & self ) -> bool {
147
+ self . len ( ) == 0
148
+ }
149
+
140
150
#[ inline]
141
151
pub fn range < R > ( & self , range : R ) -> & [ ( K , V ) ]
142
152
where R : RangeBounds < K >
@@ -207,8 +217,11 @@ impl<K: Ord, V> SortedMap<K, V> {
207
217
208
218
/// Looks up the key in `self.data` via `slice::binary_search()`.
209
219
#[ inline( always) ]
210
- fn lookup_index_for ( & self , key : & K ) -> Result < usize , usize > {
211
- self . data . binary_search_by ( |& ( ref x, _) | x. cmp ( key) )
220
+ fn lookup_index_for < Q > ( & self , key : & Q ) -> Result < usize , usize >
221
+ where K : Borrow < Q > ,
222
+ Q : Ord + ?Sized
223
+ {
224
+ self . data . binary_search_by ( |& ( ref x, _) | x. borrow ( ) . cmp ( key) )
212
225
}
213
226
214
227
#[ inline]
@@ -247,38 +260,54 @@ impl<K: Ord, V> SortedMap<K, V> {
247
260
248
261
( start, end)
249
262
}
263
+
264
+ #[ inline]
265
+ pub fn contains_key < Q > ( & self , key : & Q ) -> bool
266
+ where K : Borrow < Q > ,
267
+ Q : Ord + ?Sized
268
+ {
269
+ self . get ( key) . is_some ( )
270
+ }
250
271
}
251
272
252
273
impl < K : Ord , V > IntoIterator for SortedMap < K , V > {
253
274
type Item = ( K , V ) ;
254
275
type IntoIter = :: std:: vec:: IntoIter < ( K , V ) > ;
276
+
255
277
fn into_iter ( self ) -> Self :: IntoIter {
256
278
self . data . into_iter ( )
257
279
}
258
280
}
259
281
260
- impl < K : Ord , V , Q : Borrow < K > > Index < Q > for SortedMap < K , V > {
282
+ impl < ' a , K , Q , V > Index < & ' a Q > for SortedMap < K , V >
283
+ where K : Ord + Borrow < Q > ,
284
+ Q : Ord + ?Sized
285
+ {
261
286
type Output = V ;
262
- fn index ( & self , index : Q ) -> & Self :: Output {
263
- let k : & K = index . borrow ( ) ;
264
- self . get ( k ) . unwrap ( )
287
+
288
+ fn index ( & self , key : & Q ) -> & Self :: Output {
289
+ self . get ( key ) . expect ( "no entry found for key" )
265
290
}
266
291
}
267
292
268
- impl < K : Ord , V , Q : Borrow < K > > IndexMut < Q > for SortedMap < K , V > {
269
- fn index_mut ( & mut self , index : Q ) -> & mut Self :: Output {
270
- let k: & K = index. borrow ( ) ;
271
- self . get_mut ( k) . unwrap ( )
293
+ impl < ' a , K , Q , V > IndexMut < & ' a Q > for SortedMap < K , V >
294
+ where K : Ord + Borrow < Q > ,
295
+ Q : Ord + ?Sized
296
+ {
297
+ fn index_mut ( & mut self , key : & Q ) -> & mut Self :: Output {
298
+ self . get_mut ( key) . expect ( "no entry found for key" )
272
299
}
273
300
}
274
301
275
- impl < K : Ord , V , I : Iterator < Item =( K , V ) > > From < I > for SortedMap < K , V > {
276
- fn from ( data : I ) -> Self {
277
- let mut data: Vec < ( K , V ) > = data. collect ( ) ;
302
+ impl < K : Ord , V > FromIterator < ( K , V ) > for SortedMap < K , V > {
303
+ fn from_iter < T : IntoIterator < Item = ( K , V ) > > ( iter : T ) -> Self {
304
+ let mut data: Vec < ( K , V ) > = iter. into_iter ( ) . collect ( ) ;
305
+
278
306
data. sort_unstable_by ( |& ( ref k1, _) , & ( ref k2, _) | k1. cmp ( k2) ) ;
279
307
data. dedup_by ( |& mut ( ref k1, _) , & mut ( ref k2, _) | {
280
308
k1. cmp ( k2) == Ordering :: Equal
281
309
} ) ;
310
+
282
311
SortedMap {
283
312
data
284
313
}
0 commit comments