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

feat(variadics): add variadic hash set #1454

Closed
Closed
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
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions variadics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ workspace = true

[dependencies]
sealed = "0.5.0"
hashbrown = "0.14.0"

[dev-dependencies]
trybuild = "1.0"
75 changes: 75 additions & 0 deletions variadics/src/hash_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use std::hash::{BuildHasher, Hash, Hasher, RandomState};

use hashbrown::hash_table::{Entry, HashTable};

use crate::{PartialEqVariadic, VariadicExt};

pub struct VariadicHashSet<T, S = RandomState> {

Check warning on line 7 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a struct

Check failure on line 7 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a struct

Check warning on line 7 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a struct
table: HashTable<T>,
hasher: S,
}
impl<T> VariadicHashSet<T> {
pub fn new() -> Self {

Check warning on line 12 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check failure on line 12 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check warning on line 12 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for an associated function
Self {
table: HashTable::new(),
hasher: RandomState::default(),
}
}
}
impl<T, S> VariadicHashSet<T, S>
where
T: VariadicExt + PartialEqVariadic,
for<'a> T::AsRefVar<'a>: Hash,
S: BuildHasher,
{
fn get_hash(hasher: &S, ref_var: T::AsRefVar<'_>) -> u64 {
let mut hasher = hasher.build_hasher();
ref_var.hash(&mut hasher);
hasher.finish()
}

pub fn get<'a>(&'a self, ref_var: T::AsRefVar<'_>) -> Option<&'a T> {

Check warning on line 31 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check failure on line 31 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check warning on line 31 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a method
let hash = Self::get_hash(&self.hasher, ref_var);
self.table.find(hash, |item| {
<T as PartialEqVariadic>::eq_ref(ref_var, item.as_ref_var())
})
}

pub fn insert(&mut self, element: T) -> bool {

Check warning on line 38 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check failure on line 38 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check warning on line 38 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a method
let hash = Self::get_hash(&self.hasher, element.as_ref_var());
let entry = self.table.entry(
hash,
|item| <T as PartialEqVariadic>::eq(&element, &item),
|item| Self::get_hash(&self.hasher, item.as_ref_var()),
);
match entry {
Entry::Occupied(_occupied_entry) => false,
Entry::Vacant(vacant_entry) => {
vacant_entry.insert(element);
true
}
}
}

pub fn len(&self) -> usize {

Check warning on line 54 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check failure on line 54 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check warning on line 54 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a method
self.table.len()
}

pub fn iter<'a>(&'a self) -> impl Iterator<Item = T::AsRefVar<'a>> {

Check warning on line 58 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check failure on line 58 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a method

Check warning on line 58 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a method
self.table.iter().map(|item| item.as_ref_var())
}
}
impl<T, S> VariadicHashSet<T, S> {
pub fn with_hasher(hasher: S) -> Self {

Check warning on line 63 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check failure on line 63 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check warning on line 63 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for an associated function
Self {
table: HashTable::new(),
hasher,
}
}
pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self {

Check warning on line 69 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check failure on line 69 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for an associated function

Check warning on line 69 in variadics/src/hash_set.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for an associated function
Self {
table: HashTable::with_capacity(capacity),
hasher,
}
}
}
4 changes: 3 additions & 1 deletion variadics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
//! ## [`var_args!`]
#![doc = include_str!("../var_args.md")]

pub mod hash_set;

Check warning on line 14 in variadics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a module

Check failure on line 14 in variadics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Docs (rustdoc)

missing documentation for a module

Check warning on line 14 in variadics/src/lib.rs

View workflow job for this annotation

GitHub Actions / Test Suite (WebAssembly) (pinned-nightly)

missing documentation for a module

use std::any::Any;

use sealed::sealed;
Expand Down Expand Up @@ -481,7 +483,7 @@
fn clone_var(&self) -> Self::UnRefVar {}
}

/// A variadic where all item implement `PartialEq`.
/// A variadic where all item implement [`PartialEq`].
#[sealed]
pub trait PartialEqVariadic: VariadicExt {
/// `PartialEq` between a referenced variadic and a variadic of references, of the same types.
Expand Down
Loading