Use UnordSet instead of FxHashSet in define_id_collections!().

This commit is contained in:
Michael Woerister 2022-12-02 16:27:25 +01:00
parent 65d2f2a5f9
commit 8a1de57a4a
6 changed files with 49 additions and 18 deletions

View file

@ -12,7 +12,7 @@ pub type IndexEntry<'a, K, V> = indexmap::map::Entry<'a, K, V>;
macro_rules! define_id_collections {
($map_name:ident, $set_name:ident, $entry_name:ident, $key:ty) => {
pub type $map_name<T> = $crate::fx::FxHashMap<$key, T>;
pub type $set_name = $crate::fx::FxHashSet<$key>;
pub type $set_name = $crate::unord::UnordSet<$key>;
pub type $entry_name<'a, T> = $crate::fx::StdEntry<'a, $key, T>;
};
}

View file

@ -38,17 +38,17 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
}
#[inline]
pub fn all<U, F: Fn(T) -> bool>(mut self, f: F) -> bool {
pub fn all<F: Fn(T) -> bool>(mut self, f: F) -> bool {
self.0.all(f)
}
#[inline]
pub fn any<U, F: Fn(T) -> bool>(mut self, f: F) -> bool {
pub fn any<F: Fn(T) -> bool>(mut self, f: F) -> bool {
self.0.any(f)
}
#[inline]
pub fn filter<U, F: Fn(&T) -> bool>(self, f: F) -> UnordItems<T, impl Iterator<Item = T>> {
pub fn filter<F: Fn(&T) -> bool>(self, f: F) -> UnordItems<T, impl Iterator<Item = T>> {
UnordItems(self.0.filter(f))
}
@ -96,6 +96,15 @@ impl<T, I: Iterator<Item = T>> UnordItems<T, I> {
pub fn count(self) -> usize {
self.0.count()
}
#[inline]
pub fn flat_map<U, F, O>(self, f: F) -> UnordItems<O, impl Iterator<Item = O>>
where
U: IntoIterator<Item = O>,
F: Fn(T) -> U,
{
UnordItems(self.0.flat_map(f))
}
}
impl<'a, T: Clone + 'a, I: Iterator<Item = &'a T>> UnordItems<&'a T, I> {
@ -193,6 +202,11 @@ impl<V: Eq + Hash> UnordSet<V> {
pub fn extend<I: Iterator<Item = V>>(&mut self, items: UnordItems<V, I>) {
self.inner.extend(items.0)
}
#[inline]
pub fn clear(&mut self) {
self.inner.clear();
}
}
impl<V: Hash + Eq> Extend<V> for UnordSet<V> {
@ -201,6 +215,12 @@ impl<V: Hash + Eq> Extend<V> for UnordSet<V> {
}
}
impl<V: Hash + Eq> FromIterator<V> for UnordSet<V> {
fn from_iter<T: IntoIterator<Item = V>>(iter: T) -> Self {
UnordSet { inner: FxHashSet::from_iter(iter) }
}
}
impl<HCX, V: Hash + Eq + HashStable<HCX>> HashStable<HCX> for UnordSet<V> {
#[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {