use core::{any::TypeId, hash::Hash};
use bevy_platform::{
collections::{hash_map::Entry, HashMap},
hash::{Hashed, NoOpHash, PassHash},
};
pub type PreHashMap<K, V> = HashMap<Hashed<K>, V, PassHash>;
pub trait PreHashMapExt<K, V> {
fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V;
}
impl<K: Hash + Eq + PartialEq + Clone, V> PreHashMapExt<K, V> for PreHashMap<K, V> {
#[inline]
fn get_or_insert_with<F: FnOnce() -> V>(&mut self, key: &Hashed<K>, func: F) -> &mut V {
use bevy_platform::collections::hash_map::RawEntryMut;
let entry = self
.raw_entry_mut()
.from_key_hashed_nocheck(key.hash(), key);
match entry {
RawEntryMut::Occupied(entry) => entry.into_mut(),
RawEntryMut::Vacant(entry) => {
let (_, value) = entry.insert_hashed_nocheck(key.hash(), key.clone(), func());
value
}
}
}
}
pub type TypeIdMap<V> = HashMap<TypeId, V, NoOpHash>;
pub trait TypeIdMapExt<V> {
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V>;
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V>;
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V>;
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V>;
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash>;
}
impl<V> TypeIdMapExt<V> for TypeIdMap<V> {
#[inline]
fn insert_type<T: ?Sized + 'static>(&mut self, v: V) -> Option<V> {
self.insert(TypeId::of::<T>(), v)
}
#[inline]
fn get_type<T: ?Sized + 'static>(&self) -> Option<&V> {
self.get(&TypeId::of::<T>())
}
#[inline]
fn get_type_mut<T: ?Sized + 'static>(&mut self) -> Option<&mut V> {
self.get_mut(&TypeId::of::<T>())
}
#[inline]
fn remove_type<T: ?Sized + 'static>(&mut self) -> Option<V> {
self.remove(&TypeId::of::<T>())
}
#[inline]
fn entry_type<T: ?Sized + 'static>(&mut self) -> Entry<'_, TypeId, V, NoOpHash> {
self.entry(TypeId::of::<T>())
}
}
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::assert_impl_all;
assert_impl_all!(PreHashMap::<u64, usize>: Clone);
#[test]
fn fast_typeid_hash() {
struct Hasher;
impl core::hash::Hasher for Hasher {
fn finish(&self) -> u64 {
0
}
fn write(&mut self, _: &[u8]) {
panic!("Hashing of core::any::TypeId changed");
}
fn write_u64(&mut self, _: u64) {}
}
Hash::hash(&TypeId::of::<()>(), &mut Hasher);
}
crate::cfg::alloc! {
#[test]
fn stable_hash_within_same_program_execution() {
use alloc::vec::Vec;
let mut map_1 = <HashMap<_, _>>::default();
let mut map_2 = <HashMap<_, _>>::default();
for i in 1..10 {
map_1.insert(i, i);
map_2.insert(i, i);
}
assert_eq!(
map_1.iter().collect::<Vec<_>>(),
map_2.iter().collect::<Vec<_>>()
);
}
}
}