Skip to content

Commit

Permalink
Made with_capacity_and_hasher generic over N shards. Previously, in
Browse files Browse the repository at this point in the history
error, it used the crate's DEFAULT_SHARD_COUNT.
  • Loading branch information
nkconnor committed Oct 26, 2024
1 parent 197f955 commit c417335
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@ impl<K, V, S: BuildHasher, const N: usize> ConcurrentHashMap<K, V, S, N> {
S: Clone,
{
// per shard capacity
let capacity = (capacity + DEFAULT_SHARD_COUNT - 1) / DEFAULT_SHARD_COUNT;
let capacity = (capacity + N - 1) / N;

let shards: Vec<RwLock<Shard<K, V, S>>> =
std::iter::repeat(|| RawTable::with_capacity(capacity))
.map(|f| f())
.take(DEFAULT_SHARD_COUNT)
.take(N)
.map(|inner| {
RwLock::new(Shard {
inner,
Expand Down Expand Up @@ -273,7 +273,13 @@ impl<K, V, S: BuildHasher, const N: usize> ConcurrentHashMap<K, V, S, N> {
/// ```
#[inline]
pub fn capacity(&self) -> usize {
self.shards.first().unwrap().read().inner.capacity()
self.shards
.first()
.expect("at least one shard present")
.read()
.inner
.capacity()
* N
}

/// Returns a guarded reference for the value corresponding to the
Expand Down Expand Up @@ -508,6 +514,12 @@ mod tests {
use std::sync::Arc;
use std::time::Duration;

#[test]
fn test_apprx_capacity() {
let _map: ConcurrentHashMap<usize, usize, RandomState, 120> =
ConcurrentHashMap::with_capacity_and_hasher(1000, RandomState::new());
}

#[test]
fn test_insert_values() {
let map = ConcurrentHashMap::new();
Expand Down

0 comments on commit c417335

Please sign in to comment.