Skip to content

Commit d6ecdde

Browse files
committed
fixes, tests
1 parent eda6780 commit d6ecdde

File tree

1 file changed

+33
-10
lines changed
  • src/libstd/collections/hash

1 file changed

+33
-10
lines changed

src/libstd/collections/hash/map.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ fn test_resize_policy() {
9696
}
9797

9898
// The main performance trick in this hashmap is called Robin Hood linear
99-
// probing. It gains its excellent performance from one essential operation:
99+
// probing hashing. It gains its excellent performance from one essential
100+
// operation:
100101
//
101102
// If an insertion collides with an existing element, and that element's
102103
// "displacement" (how far away the element is from its ideal location)
@@ -226,11 +227,12 @@ fn test_resize_policy() {
226227
///
227228
/// Relevant papers/articles:
228229
///
229-
/// 1. Emmanuel Goossaert. ["Robin Hood
230+
/// 1. Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
231+
/// 2. Emmanuel Goossaert. ["Robin Hood
230232
/// hashing"](http://codecapsule.com/2013/11/11/robin-hood-hashing/)
231-
/// 2. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
233+
/// 3. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
232234
/// deletion"](http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
233-
/// 3. Alfredo Viola (2005). Distributional analysis of Robin Hood linear probing
235+
/// 4. Alfredo Viola (2005). Distributional analysis of Robin Hood linear probing
234236
/// hashing with buckets.
235237
///
236238
/// # Example
@@ -324,10 +326,9 @@ fn search_hashed<K, V, M, F>(table: M,
324326
{
325327
// Worst case, we'll find one empty bucket among `size + 1` buckets.
326328
let size = table.size();
327-
let mut probe = if let Some(probe) = Bucket::new(table, hash) {
328-
probe
329-
} else {
330-
return InternalEntry::TableIsEmpty;
329+
let mut probe = match Bucket::new(table, hash) {
330+
Some(probe) => probe,
331+
None => return InternalEntry::TableIsEmpty,
331332
};
332333
let ib = probe.index();
333334

@@ -1468,7 +1469,7 @@ impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
14681469
#[inline] fn len(&self) -> usize { self.inner.len() }
14691470
}
14701471

1471-
1472+
#[stable(feature = "rust1", since = "1.0.0")]
14721473
impl<'a, K, V> Iterator for Drain<'a, K, V> {
14731474
type Item = (K, V);
14741475

@@ -1490,6 +1491,7 @@ impl<'a, K, V> Iterator for Drain<'a, K, V> {
14901491
}
14911492
}
14921493

1494+
#[stable(feature = "rust1", since = "1.0.0")]
14931495
impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
14941496
fn len(&self) -> usize { self.inner.table().size() }
14951497
}
@@ -1874,11 +1876,32 @@ mod test_map {
18741876
}
18751877

18761878
#[test]
1877-
fn test_empty_pop() {
1879+
fn test_empty_remove() {
18781880
let mut m: HashMap<int, bool> = HashMap::new();
18791881
assert_eq!(m.remove(&0), None);
18801882
}
18811883

1884+
#[test]
1885+
fn test_empty_entry() {
1886+
let mut m: HashMap<int, bool> = HashMap::new();
1887+
assert!(m.entry(&0).is_err());
1888+
assert!(*m.entry(&0).get().unwrap_or_else(|e| e.insert(true)));
1889+
assert_eq!(m.len(), 1);
1890+
}
1891+
1892+
#[test]
1893+
fn test_empty_iter() {
1894+
let m: HashMap<int, bool> = HashMap::new();
1895+
assert_eq!(m.drain().next(), None);
1896+
assert_eq!(m.keys().next(), None);
1897+
assert_eq!(m.values().next(), None);
1898+
assert_eq!(m.iter().next(), None);
1899+
assert_eq!(m.iter_mut().next(), None);
1900+
assert_eq!(m.len(), 0);
1901+
assert!(m.is_empty());
1902+
assert_eq!(m.into_iter().next(), None);
1903+
}
1904+
18821905
#[test]
18831906
fn test_lots_of_insertions() {
18841907
let mut m = HashMap::new();

0 commit comments

Comments
 (0)