3939//! # }
4040//! ```
4141
42- use std:: collections:: HashMap ;
43- use std:: collections:: hash_map:: Iter ;
44- use std:: hash:: Hash ;
4542use std:: borrow:: Borrow ;
43+ use std:: collections:: hash_map:: Iter ;
44+ use std:: collections:: HashMap ;
4645use std:: fmt:: { self , Debug } ;
46+ use std:: hash:: Hash ;
4747
4848#[ derive( Eq ) ]
4949pub struct MultiMap < K1 : Eq + Hash , K2 : Eq + Hash , V > {
@@ -137,8 +137,9 @@ impl<K1: Eq + Hash + Clone, K2: Eq + Hash + Clone, V> MultiMap<K1, K2, V> {
137137 /// given key is returned (if it exists), just like a HashMap. This removes
138138 /// an item from the main HashMap, and the second `<K2, K1>` HashMap.
139139 pub fn remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
140- where K1 : Borrow < Q > ,
141- Q : Hash + Eq
140+ where
141+ K1 : Borrow < Q > ,
142+ Q : Hash + Eq ,
142143 {
143144 let mut result = None ;
144145 if let Some ( pair) = self . value_map . remove ( key) {
@@ -148,13 +149,68 @@ impl<K1: Eq + Hash + Clone, K2: Eq + Hash + Clone, V> MultiMap<K1, K2, V> {
148149 result
149150 }
150151
152+ /// Returns true if the map contains a value for the specified key. The key may be any borrowed
153+ /// form of the map's key type, but Hash and Eq on the borrowed form must match those for the
154+ /// key type
155+ ///
156+ /// ## Example
157+ /// ```
158+ /// #[macro_use]
159+ /// extern crate multi_map;
160+ /// use multi_map::MultiMap;
161+ /// # fn main() {
162+ /// let map = multimap! {
163+ /// 1, "One" => String::from("Eins"),
164+ /// 2, "Two" => String::from("Zwei"),
165+ /// 3, "Three" => String::from("Drei"),
166+ /// };
167+ /// assert!(map.contains_key(&1));
168+ /// assert!(!map.contains_key(&4));
169+ /// # }
170+ /// ```
171+ pub fn contains_key < Q : ?Sized > ( & self , key : & Q ) -> bool
172+ where
173+ K1 : Borrow < Q > ,
174+ Q : Hash + Eq ,
175+ {
176+ self . value_map . contains_key ( key)
177+ }
178+
179+ /// Returns true if the map contains a value for the specified alternative key. The key may be
180+ /// any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match
181+ /// those for the key type
182+ ///
183+ /// ## Example
184+ /// ```
185+ /// #[macro_use]
186+ /// extern crate multi_map;
187+ /// use multi_map::MultiMap;
188+ /// # fn main() {
189+ /// let map = multimap! {
190+ /// 1, "One" => String::from("Eins"),
191+ /// 2, "Two" => String::from("Zwei"),
192+ /// 3, "Three" => String::from("Drei"),
193+ /// };
194+ /// assert!(map.contains_key_alt(&"One"));
195+ /// assert!(!map.contains_key_alt(&"Four"));
196+ /// # }
197+ /// ```
198+ pub fn contains_key_alt < Q : ?Sized > ( & self , key : & Q ) -> bool
199+ where
200+ K2 : Borrow < Q > ,
201+ Q : Hash + Eq ,
202+ {
203+ self . key_map . contains_key ( key)
204+ }
205+
151206 /// Remove an item from the HashMap using the secondary key. The value for
152207 /// the given key is returned (if it exists). Ordinary HashMaps can't do
153208 /// this. This removes an item from both the main HashMap and the second
154209 /// `<K2, K1>` HashMap.
155210 pub fn remove_alt < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
156- where K2 : Borrow < Q > ,
157- Q : Hash + Eq
211+ where
212+ K2 : Borrow < Q > ,
213+ Q : Hash + Eq ,
158214 {
159215 let mut result = None ;
160216 if let Some ( key_a) = self . key_map . remove ( key) {
@@ -180,7 +236,13 @@ impl<K1: Eq + Hash, K2: Eq + Hash, V: Eq> PartialEq for MultiMap<K1, K2, V> {
180236
181237impl < K1 : Eq + Hash + Debug , K2 : Eq + Hash + Debug , V : Debug > fmt:: Debug for MultiMap < K1 , K2 , V > {
182238 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
183- f. debug_map ( ) . entries ( self . value_map . iter ( ) . map ( |( key_one, & ( ref key_two, ref value) ) | ( ( key_one, key_two) , value) ) ) . finish ( )
239+ f. debug_map ( )
240+ . entries (
241+ self . value_map
242+ . iter ( )
243+ . map ( |( key_one, & ( ref key_two, ref value) ) | ( ( key_one, key_two) , value) ) ,
244+ )
245+ . finish ( )
184246 }
185247}
186248
@@ -232,7 +294,7 @@ mod test {
232294
233295 #[ test]
234296 fn big_test ( ) {
235- use :: MultiMap ;
297+ use MultiMap ;
236298
237299 let mut map = MultiMap :: new ( ) ;
238300
@@ -243,6 +305,10 @@ mod test {
243305 assert ! ( * map. get( & 1 ) . unwrap( ) == String :: from( "Ein" ) ) ;
244306 assert ! ( * map. get( & 2 ) . unwrap( ) == String :: from( "Zwei" ) ) ;
245307 assert ! ( * map. get( & 3 ) . unwrap( ) == String :: from( "Drei" ) ) ;
308+ assert ! ( map. contains_key( & 1 ) ) ;
309+ assert ! ( !map. contains_key( & 4 ) ) ;
310+ assert ! ( map. contains_key_alt( & "One" ) ) ;
311+ assert ! ( !map. contains_key_alt( & "Four" ) ) ;
246312
247313 map. get_mut_alt ( & "One" ) . unwrap ( ) . push_str ( "s" ) ;
248314
@@ -270,38 +336,50 @@ mod test {
270336
271337 #[ test]
272338 fn macro_test ( ) {
273- use :: MultiMap ;
339+ use MultiMap ;
274340
275341 let map: MultiMap < i32 , & str , String > = MultiMap :: new ( ) ;
276342
277- assert_eq ! ( map, multimap!{ } ) ;
343+ assert_eq ! ( map, multimap! { } ) ;
278344
279345 let mut map = MultiMap :: new ( ) ;
280346 map. insert ( 1 , "One" , String :: from ( "Eins" ) ) ;
281347
282- assert_eq ! ( map, multimap!{
283- 1 , "One" => String :: from( "Eins" ) ,
284- } ) ;
348+ assert_eq ! (
349+ map,
350+ multimap! {
351+ 1 , "One" => String :: from( "Eins" ) ,
352+ }
353+ ) ;
285354
286- assert_eq ! ( map, multimap!{
287- 1 , "One" => String :: from( "Eins" )
288- } ) ;
355+ assert_eq ! (
356+ map,
357+ multimap! {
358+ 1 , "One" => String :: from( "Eins" )
359+ }
360+ ) ;
289361
290362 let mut map = MultiMap :: new ( ) ;
291363 map. insert ( 1 , "One" , String :: from ( "Eins" ) ) ;
292364 map. insert ( 2 , "Two" , String :: from ( "Zwei" ) ) ;
293365 map. insert ( 3 , "Three" , String :: from ( "Drei" ) ) ;
294366
295- assert_eq ! ( map, multimap!{
296- 1 , "One" => String :: from( "Eins" ) ,
297- 2 , "Two" => String :: from( "Zwei" ) ,
298- 3 , "Three" => String :: from( "Drei" ) ,
299- } ) ;
300-
301- assert_eq ! ( map, multimap!{
302- 1 , "One" => String :: from( "Eins" ) ,
303- 2 , "Two" => String :: from( "Zwei" ) ,
304- 3 , "Three" => String :: from( "Drei" )
305- } ) ;
367+ assert_eq ! (
368+ map,
369+ multimap! {
370+ 1 , "One" => String :: from( "Eins" ) ,
371+ 2 , "Two" => String :: from( "Zwei" ) ,
372+ 3 , "Three" => String :: from( "Drei" ) ,
373+ }
374+ ) ;
375+
376+ assert_eq ! (
377+ map,
378+ multimap! {
379+ 1 , "One" => String :: from( "Eins" ) ,
380+ 2 , "Two" => String :: from( "Zwei" ) ,
381+ 3 , "Three" => String :: from( "Drei" )
382+ }
383+ ) ;
306384 }
307385}
0 commit comments