Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more methods matching the standard HashMap #136

Merged
merged 1 commit into from
Jul 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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