Skip to content

Commit

Permalink
Merge pull request #316 from cuviper/trivial-search
Browse files Browse the repository at this point in the history
Avoid hashing for single-entry maps
  • Loading branch information
cuviper committed Feb 26, 2024
2 parents 406bbdb + 8b98253 commit e0a7f23
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,13 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
None
} else {
let hash = self.hash(key);
self.core.get_index_of(hash, key)
match self.as_entries() {
[] => None,
[x] => key.equivalent(&x.key).then_some(0),
_ => {
let hash = self.hash(key);
self.core.get_index_of(hash, key)
}
}
}

Expand Down Expand Up @@ -676,11 +678,17 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
return None;
match self.as_entries() {
[x] if key.equivalent(&x.key) => {
let (k, v) = self.core.pop()?;
Some((0, k, v))
}
[_] | [] => None,
_ => {
let hash = self.hash(key);
self.core.swap_remove_full(hash, key)
}
}
let hash = self.hash(key);
self.core.swap_remove_full(hash, key)
}

/// Remove the key-value pair equivalent to `key` and return
Expand Down Expand Up @@ -733,11 +741,17 @@ where
where
Q: Hash + Equivalent<K>,
{
if self.is_empty() {
return None;
match self.as_entries() {
[x] if key.equivalent(&x.key) => {
let (k, v) = self.core.pop()?;
Some((0, k, v))
}
[_] | [] => None,
_ => {
let hash = self.hash(key);
self.core.shift_remove_full(hash, key)
}
}
let hash = self.hash(key);
self.core.shift_remove_full(hash, key)
}
}

Expand Down

0 comments on commit e0a7f23

Please sign in to comment.