1010
1111use std:: borrow:: Borrow ;
1212use std:: cmp:: Ordering ;
13- use std:: convert :: From ;
13+ use std:: iter :: FromIterator ;
1414use std:: mem;
1515use std:: ops:: { RangeBounds , Bound , Index , IndexMut } ;
1616
@@ -25,11 +25,10 @@ use std::ops::{RangeBounds, Bound, Index, IndexMut};
2525#[ derive( Clone , PartialEq , Eq , PartialOrd , Ord , Hash , Default , Debug , RustcEncodable ,
2626 RustcDecodable ) ]
2727pub struct SortedMap < K : Ord , V > {
28- data : Vec < ( K , V ) >
28+ data : Vec < ( K , V ) >
2929}
3030
3131impl < K : Ord , V > SortedMap < K , V > {
32-
3332 #[ inline]
3433 pub fn new ( ) -> SortedMap < K , V > {
3534 SortedMap {
@@ -82,7 +81,10 @@ impl<K: Ord, V> SortedMap<K, V> {
8281 }
8382
8483 #[ 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+ {
8688 match self . lookup_index_for ( key) {
8789 Ok ( index) => {
8890 unsafe {
@@ -96,7 +98,10 @@ impl<K: Ord, V> SortedMap<K, V> {
9698 }
9799
98100 #[ 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+ {
100105 match self . lookup_index_for ( key) {
101106 Ok ( index) => {
102107 unsafe {
@@ -122,13 +127,13 @@ impl<K: Ord, V> SortedMap<K, V> {
122127
123128 /// Iterate over the keys, sorted
124129 #[ inline]
125- pub fn keys ( & self ) -> impl Iterator < Item = & K > + ExactSizeIterator {
130+ pub fn keys ( & self ) -> impl Iterator < Item = & K > + ExactSizeIterator {
126131 self . data . iter ( ) . map ( |& ( ref k, _) | k)
127132 }
128133
129134 /// Iterate over values, sorted by key
130135 #[ inline]
131- pub fn values ( & self ) -> impl Iterator < Item = & V > + ExactSizeIterator {
136+ pub fn values ( & self ) -> impl Iterator < Item = & V > + ExactSizeIterator {
132137 self . data . iter ( ) . map ( |& ( _, ref v) | v)
133138 }
134139
@@ -137,6 +142,11 @@ impl<K: Ord, V> SortedMap<K, V> {
137142 self . data . len ( )
138143 }
139144
145+ #[ inline]
146+ pub fn is_empty ( & self ) -> bool {
147+ self . len ( ) == 0
148+ }
149+
140150 #[ inline]
141151 pub fn range < R > ( & self , range : R ) -> & [ ( K , V ) ]
142152 where R : RangeBounds < K >
@@ -207,8 +217,11 @@ impl<K: Ord, V> SortedMap<K, V> {
207217
208218 /// Looks up the key in `self.data` via `slice::binary_search()`.
209219 #[ 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) )
212225 }
213226
214227 #[ inline]
@@ -247,38 +260,54 @@ impl<K: Ord, V> SortedMap<K, V> {
247260
248261 ( start, end)
249262 }
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+ }
250271}
251272
252273impl < K : Ord , V > IntoIterator for SortedMap < K , V > {
253274 type Item = ( K , V ) ;
254275 type IntoIter = :: std:: vec:: IntoIter < ( K , V ) > ;
276+
255277 fn into_iter ( self ) -> Self :: IntoIter {
256278 self . data . into_iter ( )
257279 }
258280}
259281
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+ {
261286 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" )
265290 }
266291}
267292
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" )
272299 }
273300}
274301
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+
278306 data. sort_unstable_by ( |& ( ref k1, _) , & ( ref k2, _) | k1. cmp ( k2) ) ;
279307 data. dedup_by ( |& mut ( ref k1, _) , & mut ( ref k2, _) | {
280308 k1. cmp ( k2) == Ordering :: Equal
281309 } ) ;
310+
282311 SortedMap {
283312 data
284313 }
0 commit comments