Ahash constructor extensions and HashMap / HashSet (#790)

Ahash constructor extensions and HashMap / HashSet
This commit is contained in:
Joshua J. Bouw 2020-11-11 08:06:55 +07:00 committed by GitHub
parent 0c30762ab7
commit c9acef04e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 7 deletions

View file

@ -1,4 +1,5 @@
use std::{collections::HashMap, hash::Hash};
use bevy_utils::{AHashExt, HashMap};
use std::hash::Hash;
#[derive(Debug)]
pub struct Axis<T> {

View file

@ -9,15 +9,89 @@ pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
#[cfg(target_arch = "wasm32")]
pub type BoxedFuture<'a, T> = Pin<Box<dyn Future<Output = T> + 'a>>;
/// A std hash map 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 HashMap<K, V> = std::collections::HashMap<K, V, RandomState>;
pub type HashSet<K> = std::collections::HashSet<K, RandomState>;
pub trait HashMapExt {
fn with_capacity(cap: usize) -> Self;
pub trait AHashExt {
fn new() -> Self;
fn with_capacity(capacity: usize) -> Self;
}
impl<K, V> HashMapExt for HashMap<K, V> {
fn with_capacity(cap: usize) -> Self {
HashMap::with_capacity_and_hasher(cap, RandomState::default())
impl<K, V> AHashExt for HashMap<K, V> {
/// Creates an empty `HashMap` with AHash.
///
/// The hash map is initially created with a capacity of 0, so it will not
/// allocate until it is first inserted into.
///
/// # Examples
///
/// ```
/// use bevy_utils::{HashMap, AHashExt};
/// let mut map: HashMap<&str, i32> = HashMap::new();
/// ```
#[inline]
fn new() -> Self {
Default::default()
}
/// 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);
/// ```
#[inline]
fn with_capacity(capacity: usize) -> Self {
HashMap::with_capacity_and_hasher(capacity, RandomState::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 AHash.
///
/// The hash set is initially created with a capacity of 0, so it will not
/// allocate until it is first inserted into.
///
/// # Examples
///
/// ```
/// use bevy_utils::{HashSet, AHashExt};
/// let set: HashSet<i32> = HashSet::new();
/// ```
#[inline]
fn new() -> Self {
Default::default()
}
/// 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())
}
}