-
Notifications
You must be signed in to change notification settings - Fork 13.4k
data_structures: Add deterministic FxHashMap and FxHashSet wrappers #64131
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
pub use rustc_hash::FxHashMap; | ||
use std::borrow::Borrow; | ||
use std::collections::hash_map::Entry; | ||
use std::fmt; | ||
use std::hash::Hash; | ||
|
||
/// A deterministic wrapper around FxHashMap that does not provide iteration support. | ||
/// | ||
/// It supports insert, remove, get and get_mut functions from FxHashMap. | ||
/// It also allows to convert hashmap to a sorted vector with the method `into_sorted_vector()`. | ||
#[derive(Clone)] | ||
pub struct StableMap<K, V> { | ||
base: FxHashMap<K, V>, | ||
} | ||
|
||
impl<K, V> Default for StableMap<K, V> | ||
where | ||
K: Eq + Hash, | ||
{ | ||
fn default() -> StableMap<K, V> { | ||
StableMap::new() | ||
} | ||
} | ||
|
||
impl<K, V> fmt::Debug for StableMap<K, V> | ||
where | ||
K: Eq + Hash + fmt::Debug, | ||
V: fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self.base) | ||
} | ||
} | ||
|
||
impl<K, V> PartialEq for StableMap<K, V> | ||
where | ||
K: Eq + Hash, | ||
V: PartialEq, | ||
{ | ||
fn eq(&self, other: &StableMap<K, V>) -> bool { | ||
self.base == other.base | ||
} | ||
} | ||
|
||
impl<K, V> Eq for StableMap<K, V> | ||
where | ||
K: Eq + Hash, | ||
V: Eq, | ||
{} | ||
|
||
impl<K, V> StableMap<K, V> | ||
shivan1b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
K: Eq + Hash, | ||
{ | ||
pub fn new() -> StableMap<K, V> { | ||
StableMap { base: FxHashMap::default() } | ||
} | ||
|
||
pub fn into_sorted_vector(self) -> Vec<(K, V)> | ||
where | ||
K: Ord + Copy, | ||
{ | ||
let mut vector = self.base.into_iter().collect::<Vec<_>>(); | ||
vector.sort_unstable_by_key(|pair| pair.0); | ||
shivan1b marked this conversation as resolved.
Show resolved
Hide resolved
|
||
vector | ||
} | ||
|
||
pub fn entry(&mut self, k: K) -> Entry<'_, K, V> { | ||
self.base.entry(k) | ||
} | ||
|
||
pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V> | ||
where | ||
K: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.base.get(k) | ||
} | ||
|
||
pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V> | ||
where | ||
K: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.base.get_mut(k) | ||
} | ||
|
||
pub fn insert(&mut self, k: K, v: V) -> Option<V> { | ||
self.base.insert(k, v) | ||
} | ||
|
||
pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V> | ||
where | ||
K: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.base.remove(k) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
pub use rustc_hash::FxHashSet; | ||
use std::borrow::Borrow; | ||
use std::fmt; | ||
use std::hash::Hash; | ||
|
||
/// A deterministic wrapper around FxHashSet that does not provide iteration support. | ||
/// | ||
/// It supports insert, remove, get functions from FxHashSet. | ||
/// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`. | ||
#[derive(Clone)] | ||
pub struct StableSet<T> { | ||
base: FxHashSet<T>, | ||
} | ||
|
||
impl<T> Default for StableSet<T> | ||
where | ||
T: Eq + Hash, | ||
{ | ||
fn default() -> StableSet<T> { | ||
StableSet::new() | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for StableSet<T> | ||
where | ||
T: Eq + Hash + fmt::Debug, | ||
{ | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{:?}", self.base) | ||
} | ||
} | ||
|
||
impl<T> PartialEq<StableSet<T>> for StableSet<T> | ||
where | ||
T: Eq + Hash, | ||
{ | ||
fn eq(&self, other: &StableSet<T>) -> bool { | ||
self.base == other.base | ||
} | ||
} | ||
|
||
impl<T> Eq for StableSet<T> where T: Eq + Hash {} | ||
|
||
impl<T: Hash + Eq> StableSet<T> { | ||
pub fn new() -> StableSet<T> { | ||
StableSet { base: FxHashSet::default() } | ||
} | ||
|
||
pub fn into_sorted_vector(self) -> Vec<T> | ||
where | ||
T: Ord, | ||
{ | ||
let mut vector = self.base.into_iter().collect::<Vec<_>>(); | ||
vector.sort_unstable(); | ||
vector | ||
} | ||
|
||
pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> | ||
where | ||
T: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.base.get(value) | ||
} | ||
|
||
pub fn insert(&mut self, value: T) -> bool { | ||
self.base.insert(value) | ||
} | ||
|
||
pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool | ||
where | ||
T: Borrow<Q>, | ||
Q: Hash + Eq, | ||
{ | ||
self.base.remove(value) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.