Skip to content

Commit 94c3c84

Browse files
committed
38880 hashmap check size=0, not just capacity=0
1 parent a295ec1 commit 94c3c84

File tree

1 file changed

+24
-30
lines changed
  • src/libstd/collections/hash

1 file changed

+24
-30
lines changed

src/libstd/collections/hash/map.rs

+24-30
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, is_match: F) -> InternalE
414414
search_hashed_nonempty(table, hash, is_match)
415415
}
416416

417-
418417
/// Search for a pre-hashed key when the hash map is known to be non-empty.
419418
#[inline]
420419
fn search_hashed_nonempty<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F)
@@ -557,32 +556,36 @@ impl<K, V, S> HashMap<K, V, S>
557556
}
558557

559558
/// Search for a key, yielding the index if it's found in the hashtable.
560-
/// If you already have the hash for the key lying around, use
561-
/// search_hashed.
559+
/// If you already have the hash for the key lying around, or if you need an
560+
/// InternalEntry, use search_hashed or search_hashed_nonempty.
562561
#[inline]
563-
fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> InternalEntry<K, V, &'a RawTable<K, V>>
562+
fn search<'a, Q: ?Sized>(&'a self, q: &Q)
563+
-> Option<FullBucket<K, V, &'a RawTable<K, V>>>
564564
where K: Borrow<Q>,
565565
Q: Eq + Hash
566566
{
567-
if self.table.capacity() != 0 {
568-
let hash = self.make_hash(q);
569-
search_hashed_nonempty(&self.table, hash, |k| q.eq(k.borrow()))
570-
} else {
571-
InternalEntry::TableIsEmpty
567+
if !self.is_empty() {
568+
return None;
572569
}
570+
571+
let hash = self.make_hash(q);
572+
search_hashed_nonempty(&self.table, hash, |k| q.eq(k.borrow()))
573+
.into_occupied_bucket()
573574
}
574575

575576
#[inline]
576-
fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> InternalEntry<K, V, &'a mut RawTable<K, V>>
577+
fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q)
578+
-> Option<FullBucket<K, V, &'a mut RawTable<K, V>>>
577579
where K: Borrow<Q>,
578580
Q: Eq + Hash
579581
{
580-
if self.table.capacity() != 0 {
581-
let hash = self.make_hash(q);
582-
search_hashed_nonempty(&mut self.table, hash, |k| q.eq(k.borrow()))
583-
} else {
584-
InternalEntry::TableIsEmpty
582+
if self.is_empty() {
583+
return None;
585584
}
585+
586+
let hash = self.make_hash(q);
587+
search_hashed_nonempty(&mut self.table, hash, |k| q.eq(k.borrow()))
588+
.into_occupied_bucket()
586589
}
587590

588591
// The caller should ensure that invariants by Robin Hood Hashing hold
@@ -1140,7 +1143,7 @@ impl<K, V, S> HashMap<K, V, S>
11401143
where K: Borrow<Q>,
11411144
Q: Hash + Eq
11421145
{
1143-
self.search(k).into_occupied_bucket().map(|bucket| bucket.into_refs().1)
1146+
self.search(k).map(|bucket| bucket.into_refs().1)
11441147
}
11451148

11461149
/// Returns true if the map contains a value for the specified key.
@@ -1167,7 +1170,7 @@ impl<K, V, S> HashMap<K, V, S>
11671170
where K: Borrow<Q>,
11681171
Q: Hash + Eq
11691172
{
1170-
self.search(k).into_occupied_bucket().is_some()
1173+
self.search(k).is_some()
11711174
}
11721175

11731176
/// Returns a mutable reference to the value corresponding to the key.
@@ -1196,7 +1199,7 @@ impl<K, V, S> HashMap<K, V, S>
11961199
where K: Borrow<Q>,
11971200
Q: Hash + Eq
11981201
{
1199-
self.search_mut(k).into_occupied_bucket().map(|bucket| bucket.into_mut_refs().1)
1202+
self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
12001203
}
12011204

12021205
/// Inserts a key-value pair into the map.
@@ -1256,11 +1259,7 @@ impl<K, V, S> HashMap<K, V, S>
12561259
where K: Borrow<Q>,
12571260
Q: Hash + Eq
12581261
{
1259-
if self.table.size() == 0 {
1260-
return None;
1261-
}
1262-
1263-
self.search_mut(k).into_occupied_bucket().map(|bucket| pop_internal(bucket).1)
1262+
self.search_mut(k).map(|bucket| pop_internal(bucket).1)
12641263
}
12651264

12661265
/// Removes a key from the map, returning the stored key and value if the
@@ -1296,7 +1295,6 @@ impl<K, V, S> HashMap<K, V, S>
12961295
}
12971296

12981297
self.search_mut(k)
1299-
.into_occupied_bucket()
13001298
.map(|bucket| {
13011299
let (k, v, _) = pop_internal(bucket);
13021300
(k, v)
@@ -2654,15 +2652,11 @@ impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
26542652

26552653
#[inline]
26562654
fn get(&self, key: &Q) -> Option<&K> {
2657-
self.search(key).into_occupied_bucket().map(|bucket| bucket.into_refs().0)
2655+
self.search(key).map(|bucket| bucket.into_refs().0)
26582656
}
26592657

26602658
fn take(&mut self, key: &Q) -> Option<K> {
2661-
if self.table.size() == 0 {
2662-
return None;
2663-
}
2664-
2665-
self.search_mut(key).into_occupied_bucket().map(|bucket| pop_internal(bucket).0)
2659+
self.search_mut(key).map(|bucket| pop_internal(bucket).0)
26662660
}
26672661

26682662
#[inline]

0 commit comments

Comments
 (0)