From fd0d317f2035b34bd64ffca6305ddf1cb707fb49 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Mon, 20 Jan 2014 12:03:28 +0100 Subject: [PATCH 1/3] Implement pop_equiv() for HashMap --- src/libstd/hashmap.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index 5c3b18caa069f..e7a5e4f7c8d01 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -252,7 +252,7 @@ impl HashMap { } } - fn pop_internal(&mut self, hash: uint, k: &K) -> Option { + fn pop_internal(&mut self, searchresult: SearchResult) -> Option { // Removing from an open-addressed hashtable // is, well, painful. The problem is that // the entry may lie on the probe path for other @@ -267,7 +267,7 @@ impl HashMap { // // I found this explanation elucidating: // http://www.maths.lse.ac.uk/Courses/MA407/del-hash.pdf - let mut idx = match self.bucket_for_key_with_hash(hash, k) { + let mut idx = match searchresult { TableFull | FoundHole(_) => return None, FoundEntry(idx) => idx }; @@ -349,8 +349,17 @@ impl MutableMap for HashMap { /// Removes a key from the map, returning the value at the key if the key /// was previously in the map. fn pop(&mut self, k: &K) -> Option { - let hash = k.hash_keyed(self.k0, self.k1) as uint; - self.pop_internal(hash, k) + let s = self.bucket_for_key(k); + self.pop_internal(s) + } +} + +impl HashMap { + /// Removes a key from the map using equivalence, returning the value + /// at the key if the key was previously in the map. + pub fn pop_equiv>(&mut self, k: &Q) -> Option { + let s = self.bucket_for_key_equiv(k); + self.pop_internal(s) } } @@ -968,6 +977,16 @@ mod test_map { assert_eq!(m.pop(&1), None); } + #[test] + fn test_pop_equiv() { + let mut m = HashMap::new(); + m.insert(~"key1", 1); + m.insert(~"key2", 2); + assert_eq!(m.pop("key1"), Some(1)); + assert_eq!(m.pop("key1"), None); + assert_eq!(m.pop(&~"key2"), Some(2)); + } + #[test] fn test_swap() { let mut m = HashMap::new(); From e8e98b80353c6bc57b6be392622f887b28f7362d Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Mon, 20 Jan 2014 15:17:14 +0100 Subject: [PATCH 2/3] Fix test case --- src/libstd/hashmap.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index e7a5e4f7c8d01..e827d4a27f310 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -355,7 +355,7 @@ impl MutableMap for HashMap { } impl HashMap { - /// Removes a key from the map using equivalence, returning the value + /// Removes a key from the map using equivalence, returning the value /// at the key if the key was previously in the map. pub fn pop_equiv>(&mut self, k: &Q) -> Option { let s = self.bucket_for_key_equiv(k); @@ -982,9 +982,9 @@ mod test_map { let mut m = HashMap::new(); m.insert(~"key1", 1); m.insert(~"key2", 2); - assert_eq!(m.pop("key1"), Some(1)); - assert_eq!(m.pop("key1"), None); - assert_eq!(m.pop(&~"key2"), Some(2)); + assert_eq!(m.pop_equiv("key1"), Some(1)); + assert_eq!(m.pop_equiv("key1"), None); + assert_eq!(m.pop_equiv(&~"key2"), Some(2)); } #[test] From b084527f55fdff699653d42cbf76f74f1498e4b5 Mon Sep 17 00:00:00 2001 From: Michael Neumann Date: Mon, 20 Jan 2014 22:35:33 +0100 Subject: [PATCH 3/3] Make it compile --- src/libstd/hashmap.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libstd/hashmap.rs b/src/libstd/hashmap.rs index e827d4a27f310..4542efa0aa5b7 100644 --- a/src/libstd/hashmap.rs +++ b/src/libstd/hashmap.rs @@ -354,7 +354,7 @@ impl MutableMap for HashMap { } } -impl HashMap { +impl HashMap { /// Removes a key from the map using equivalence, returning the value /// at the key if the key was previously in the map. pub fn pop_equiv>(&mut self, k: &Q) -> Option { @@ -982,9 +982,9 @@ mod test_map { let mut m = HashMap::new(); m.insert(~"key1", 1); m.insert(~"key2", 2); - assert_eq!(m.pop_equiv("key1"), Some(1)); - assert_eq!(m.pop_equiv("key1"), None); - assert_eq!(m.pop_equiv(&~"key2"), Some(2)); + assert_eq!(m.pop_equiv(&("key1")), Some(1)); + assert_eq!(m.pop_equiv(&("key1")), None); + assert_eq!(m.pop_equiv(&(~"key2")), Some(2)); } #[test]