Skip to content

Commit

Permalink
use Global for with_capacity, and add with_capacity_in
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Jan 18, 2021
1 parent ae86b4e commit dac885a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ impl<K, V, S> HashMap<K, V, S> {
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self {
Self {
hash_builder,
table: RawTable::with_capacity(Global, capacity),
table: RawTable::with_capacity(capacity),
}
}
}
Expand Down Expand Up @@ -464,7 +464,7 @@ impl<K, V, S, A: Allocator + Clone> HashMap<K, V, S, A> {
pub fn with_capacity_and_hasher_in(capacity: usize, hash_builder: S, alloc: A) -> Self {
Self {
hash_builder,
table: RawTable::with_capacity(alloc, capacity),
table: RawTable::with_capacity_in(capacity, alloc),
}
}

Expand Down
27 changes: 20 additions & 7 deletions src/raw/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,19 @@ impl<T> RawTable<T, Global> {
alloc: Global,
}
}

/// Attempts to allocate a new hash table with at least enough capacity
/// for inserting the given number of elements without reallocating.
#[cfg(feature = "raw")]
pub fn try_with_capacity(capacity: usize) -> Result<Self, TryReserveError> {
Self::try_with_capacity_in(capacity, Global)
}

/// Allocates a new hash table with at least enough capacity for inserting
/// the given number of elements without reallocating.
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
}
}

impl<T, A: Allocator + Clone> RawTable<T, A> {
Expand Down Expand Up @@ -483,16 +496,16 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
}
}

/// Attempts to allocate a new hash table with at least enough capacity
/// for inserting the given number of elements without reallocating.
/// Attempts to allocate a new hash table using the given allocator, with at least enough
/// capacity for inserting the given number of elements without reallocating.
#[cfg(feature = "raw")]
pub fn try_with_capacity(alloc: A, capacity: usize) -> Result<Self, TryReserveError> {
pub fn try_with_capacity_in(capacity: usize, alloc: A) -> Result<Self, TryReserveError> {
Self::fallible_with_capacity(alloc, capacity, Fallibility::Fallible)
}

/// Allocates a new hash table with at least enough capacity for inserting
/// the given number of elements without reallocating.
pub fn with_capacity(alloc: A, capacity: usize) -> Self {
/// Allocates a new hash table using the given allocator, with at least enough capacity for
/// inserting the given number of elements without reallocating.
pub fn with_capacity_in(capacity: usize, alloc: A) -> Self {
// Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
match Self::fallible_with_capacity(alloc, capacity, Fallibility::Infallible) {
Ok(capacity) => capacity,
Expand Down Expand Up @@ -748,7 +761,7 @@ impl<T, A: Allocator + Clone> RawTable<T, A> {
if min_buckets < self.buckets() {
// Fast path if the table is empty
if self.items == 0 {
*self = Self::with_capacity(self.alloc.clone(), min_size)
*self = Self::with_capacity_in(min_size, self.alloc.clone())
} else {
// Avoid `Result::unwrap_or_else` because it bloats LLVM IR.
if self
Expand Down

0 comments on commit dac885a

Please sign in to comment.