Skip to content

Commit

Permalink
Merge pull request #136 from cuviper/std-parity
Browse files Browse the repository at this point in the history
Add more methods matching the standard HashMap
  • Loading branch information
cuviper authored Jul 16, 2020
2 parents 299d950 + 6e8614d commit 8973b0f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
68 changes: 68 additions & 0 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,22 @@ where
self.get_full(key).map(third)
}

/// Return references to the key-value pair stored for `key`,
/// if it is present, else `None`.
///
/// Computes in **O(1)** time (average).
pub fn get_key_value<Q: ?Sized>(&self, key: &Q) -> Option<(&K, &V)>
where
Q: Hash + Equivalent<K>,
{
if let Some(i) = self.get_index_of(key) {
let entry = &self.as_entries()[i];
Some((&entry.key, &entry.value))
} else {
None
}
}

/// Return item index, key and value
pub fn get_full<Q: ?Sized>(&self, key: &Q) -> Option<(usize, &K, &V)>
where
Expand Down Expand Up @@ -424,6 +440,20 @@ where
self.swap_remove(key)
}

/// Remove and return the key-value pair equivalent to `key`.
///
/// **NOTE:** This is equivalent to `.swap_remove_entry(key)`, if you need to
/// preserve the order of the keys in the map, use `.shift_remove_entry(key)`
/// instead.
///
/// Computes in **O(1)** time (average).
pub fn remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: Hash + Equivalent<K>,
{
self.swap_remove_entry(key)
}

/// Remove the key-value pair equivalent to `key` and return
/// its value.
///
Expand All @@ -441,6 +471,25 @@ where
self.swap_remove_full(key).map(third)
}

/// Remove and return the key-value pair equivalent to `key`.
///
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
/// last element of the map and popping it off. **This perturbs
/// the postion of what used to be the last element!**
///
/// Return `None` if `key` is not in map.
///
/// Computes in **O(1)** time (average).
pub fn swap_remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: Hash + Equivalent<K>,
{
match self.swap_remove_full(key) {
Some((_, key, value)) => Some((key, value)),
None => None,
}
}

/// Remove the key-value pair equivalent to `key` and return it and
/// the index it had.
///
Expand Down Expand Up @@ -480,6 +529,25 @@ where
self.shift_remove_full(key).map(third)
}

/// Remove and return the key-value pair equivalent to `key`.
///
/// Like `Vec::remove`, the pair is removed by shifting all of the
/// elements that follow it, preserving their relative order.
/// **This perturbs the index of all of those elements!**
///
/// Return `None` if `key` is not in map.
///
/// Computes in **O(n)** time (average).
pub fn shift_remove_entry<Q: ?Sized>(&mut self, key: &Q) -> Option<(K, V)>
where
Q: Hash + Equivalent<K>,
{
match self.shift_remove_full(key) {
Some((_, key, value)) => Some((key, value)),
None => None,
}
}

/// Remove the key-value pair equivalent to `key` and return it and
/// the index it had.
///
Expand Down
6 changes: 3 additions & 3 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ where
where
Q: Hash + Equivalent<T>,
{
self.map.get_full(value).map(|(_, x, &())| x)
self.map.get_key_value(value).map(|(x, &())| x)
}

/// Return item index and value
Expand Down Expand Up @@ -439,7 +439,7 @@ where
where
Q: Hash + Equivalent<T>,
{
self.map.swap_remove_full(value).map(|(_, x, ())| x)
self.map.swap_remove_entry(value).map(|(x, ())| x)
}

/// Removes and returns the value in the set, if any, that is equal to the
Expand All @@ -456,7 +456,7 @@ where
where
Q: Hash + Equivalent<T>,
{
self.map.shift_remove_full(value).map(|(_, x, ())| x)
self.map.shift_remove_entry(value).map(|(x, ())| x)
}

/// Remove the value from the set return it and the index it had.
Expand Down

0 comments on commit 8973b0f

Please sign in to comment.