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

[Merged by Bors] - bevy_utils: Re-introduce with_capacity(). #2393

Closed
Closed
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
80 changes: 80 additions & 0 deletions crates/bevy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ impl std::hash::BuildHasher for FixedState {
/// AHash is designed for performance and is NOT cryptographically secure.
pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;

pub trait AHashExt {
fn with_capacity(capacity: usize) -> Self;
}

impl<K, V> AHashExt for HashMap<K, V> {
/// Creates an empty `HashMap` with the specified capacity with AHash.
///
/// The hash map will be able to hold at least `capacity` elements without
/// reallocating. If `capacity` is 0, the hash map will not allocate.
///
/// # Examples
///
/// ```
/// use bevy_utils::{HashMap, AHashExt};
/// let mut map: HashMap<&str, i32> = HashMap::with_capacity(10);
/// assert!(map.capacity() >= 10);
/// ```
#[inline]
fn with_capacity(capacity: usize) -> Self {
HashMap::with_capacity_and_hasher(capacity, RandomState::default())
}
}

/// A stable std hash map implementing AHash, a high speed keyed hashing algorithm
/// intended for use in in-memory hashmaps.
///
Expand All @@ -46,12 +69,50 @@ pub type HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
/// AHash is designed for performance and is NOT cryptographically secure.
pub type StableHashMap<K, V> = std::collections::HashMap<K, V, FixedState>;

impl<K, V> AHashExt for StableHashMap<K, V> {
/// Creates an empty `StableHashMap` with the specified capacity with AHash.
///
/// The hash map will be able to hold at least `capacity` elements without
/// reallocating. If `capacity` is 0, the hash map will not allocate.
///
/// # Examples
///
/// ```
/// use bevy_utils::{StableHashMap, AHashExt};
/// let mut map: StableHashMap<&str, i32> = StableHashMap::with_capacity(10);
/// assert!(map.capacity() >= 10);
/// ```
#[inline]
fn with_capacity(capacity: usize) -> Self {
StableHashMap::with_capacity_and_hasher(capacity, FixedState::default())
}
}

/// A std hash set implementing AHash, a high speed keyed hashing algorithm
/// intended for use in in-memory hashmaps.
///
/// AHash is designed for performance and is NOT cryptographically secure.
pub type HashSet<K> = std::collections::HashSet<K, RandomState>;

impl<K> AHashExt for HashSet<K> {
/// Creates an empty `HashSet` with the specified capacity with AHash.
///
/// The hash set will be able to hold at least `capacity` elements without
/// reallocating. If `capacity` is 0, the hash set will not allocate.
///
/// # Examples
///
/// ```
/// use bevy_utils::{HashSet, AHashExt};
/// let set: HashSet<i32> = HashSet::with_capacity(10);
/// assert!(set.capacity() >= 10);
/// ```
#[inline]
fn with_capacity(capacity: usize) -> Self {
HashSet::with_capacity_and_hasher(capacity, RandomState::default())
}
}

/// A stable std hash set implementing AHash, a high speed keyed hashing algorithm
/// intended for use in in-memory hashmaps.
///
Expand All @@ -60,3 +121,22 @@ pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
///
/// AHash is designed for performance and is NOT cryptographically secure.
pub type StableHashSet<K> = std::collections::HashSet<K, FixedState>;

impl<K> AHashExt for StableHashSet<K> {
/// Creates an empty `StableHashSet` with the specified capacity with AHash.
///
/// The hash set will be able to hold at least `capacity` elements without
/// reallocating. If `capacity` is 0, the hash set will not allocate.
///
/// # Examples
///
/// ```
/// use bevy_utils::{StableHashSet, AHashExt};
/// let set: StableHashSet<i32> = StableHashSet::with_capacity(10);
/// assert!(set.capacity() >= 10);
/// ```
#[inline]
fn with_capacity(capacity: usize) -> Self {
StableHashSet::with_capacity_and_hasher(capacity, FixedState::default())
}
}