This commit is contained in:
Tom Karwowski 2023-08-30 08:05:11 +09:00 committed by GitHub
commit abd2e751cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 90 additions and 0 deletions

View File

@ -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> {
@ -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 {