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: add DashSet::with_shard_amount and it's kin to mirror DashMap::with_shard_amount #277

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,46 @@ impl<'a, K: 'a + Eq + Hash> DashSet<K, RandomState> {
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_and_hasher(capacity, RandomState::default())
}

/// Creates a new DashSet with a specified shard amount
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashSet;
///
/// let numbers = DashSet::with_shard_amount(32);
/// numbers.insert(2);
/// numbers.insert(8);
/// ```
pub fn with_shard_amount(shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(0, RandomState::default(), shard_amount)
}

/// Creates a new DashSet with a specified capacity and shard amount.
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashSet;
///
/// let numbers = DashSet::with_capacity_and_shard_amount(32, 32);
/// numbers.insert(2);
/// numbers.insert(8);
/// ```
pub fn with_capacity_and_shard_amount(capacity: usize, shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(
capacity,
RandomState::default(),
shard_amount,
)
}
}

impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
Expand Down Expand Up @@ -115,6 +155,56 @@ impl<'a, K: 'a + Eq + Hash, S: BuildHasher + Clone> DashSet<K, S> {
}
}

/// Creates a new DashSet with a specified hasher and shard amount
///
/// shard_amount should be greater than 0 and a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashSet;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let numbers = DashSet::with_hasher_and_shard_amount(s, 32);
/// numbers.insert(2);
/// numbers.insert(8);
/// ```
pub fn with_hasher_and_shard_amount(hasher: S, shard_amount: usize) -> Self {
Self::with_capacity_and_hasher_and_shard_amount(0, hasher, shard_amount)
}

/// Creates a new DashSet with a specified starting capacity, hasher and shard_amount.
///
/// shard_amount should greater than 0 and be a power of two.
/// If a shard_amount which is not a power of two is provided, the function will panic.
///
/// # Examples
///
/// ```
/// use dashmap::DashSet;
/// use std::collections::hash_map::RandomState;
///
/// let s = RandomState::new();
/// let numbers = DashSet::with_capacity_and_hasher_and_shard_amount(2, s, 32);
/// numbers.insert(2);
/// numbers.insert(8);
/// ```
pub fn with_capacity_and_hasher_and_shard_amount(
capacity: usize,
hasher: S,
shard_amount: usize,
) -> Self {
Self {
inner: DashMap::with_capacity_and_hasher_and_shard_amount(
capacity,
hasher,
shard_amount,
),
}
}

/// Hash a given item to produce a usize.
/// Uses the provided or default HashBuilder.
pub fn hash_usize<T: Hash>(&self, item: &T) -> usize {
Expand Down