Add epaint::util::hash function for hashing a value
This commit is contained in:
parent
976260c2bd
commit
ba0e3780a1
|
|
@ -13,11 +13,6 @@ impl TypeId {
|
|||
|
||||
impl From<std::any::TypeId> for TypeId {
|
||||
fn from(id: std::any::TypeId) -> Self {
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
use std::hash::{Hash, Hasher};
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
id.hash(&mut hasher);
|
||||
Self(hasher.finish())
|
||||
Self(epaint::util::hash(id))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
use std::hash::{Hash, Hasher};
|
||||
use epaint::util::hash;
|
||||
|
||||
const SIZE: usize = 1024; // must be small for web/WASM build (for unknown reason)
|
||||
|
||||
|
|
@ -24,7 +24,7 @@ impl<K, V> std::fmt::Debug for Cache<K, V> {
|
|||
|
||||
impl<K, V> Cache<K, V>
|
||||
where
|
||||
K: Hash + PartialEq,
|
||||
K: std::hash::Hash + PartialEq,
|
||||
{
|
||||
pub fn get(&self, key: &K) -> Option<&V> {
|
||||
let bucket = (hash(key) % (SIZE as u64)) as usize;
|
||||
|
|
@ -39,10 +39,3 @@ where
|
|||
self.0[bucket] = Some((key, value));
|
||||
}
|
||||
}
|
||||
|
||||
fn hash(value: impl Hash) -> u64 {
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
let mut hasher = DefaultHasher::default();
|
||||
value.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -82,6 +82,7 @@ mod stroke;
|
|||
pub mod tessellator;
|
||||
pub mod text;
|
||||
mod texture_atlas;
|
||||
pub mod util;
|
||||
|
||||
pub use {
|
||||
color::{Color32, Rgba},
|
||||
|
|
|
|||
|
|
@ -1,8 +1,4 @@
|
|||
use std::{
|
||||
collections::BTreeMap,
|
||||
hash::{Hash, Hasher},
|
||||
sync::Arc,
|
||||
};
|
||||
use std::{collections::BTreeMap, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
mutex::Mutex,
|
||||
|
|
@ -261,10 +257,7 @@ impl Fonts {
|
|||
let mut atlas = atlas.lock();
|
||||
let texture = atlas.texture_mut();
|
||||
// Make sure we seed the texture version with something unique based on the default characters:
|
||||
use std::collections::hash_map::DefaultHasher;
|
||||
let mut hasher = DefaultHasher::default();
|
||||
texture.pixels.hash(&mut hasher);
|
||||
texture.version = hasher.finish();
|
||||
texture.version = crate::util::hash(&texture.pixels);
|
||||
}
|
||||
|
||||
Self {
|
||||
|
|
@ -412,11 +405,7 @@ struct GalleyCache {
|
|||
|
||||
impl GalleyCache {
|
||||
fn layout(&mut self, fonts: &Fonts, job: LayoutJob) -> Arc<Galley> {
|
||||
let hash = {
|
||||
let mut hasher = ahash::AHasher::new_with_keys(123, 456); // TODO: even faster hasher?
|
||||
job.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
};
|
||||
let hash = crate::util::hash_with(&job, ahash::AHasher::new_with_keys(123, 456)); // TODO: even faster hasher?
|
||||
|
||||
match self.cache.entry(hash) {
|
||||
std::collections::hash_map::Entry::Occupied(entry) => {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,12 @@
|
|||
/// Hash the given value with a predictable hasher.
|
||||
#[inline]
|
||||
pub fn hash(value: impl std::hash::Hash) -> u64 {
|
||||
hash_with(value, std::collections::hash_map::DefaultHasher::default())
|
||||
}
|
||||
|
||||
/// Hash the given value with the given hasher.
|
||||
#[inline]
|
||||
pub fn hash_with(value: impl std::hash::Hash, mut hasher: impl std::hash::Hasher) -> u64 {
|
||||
value.hash(&mut hasher);
|
||||
hasher.finish()
|
||||
}
|
||||
Loading…
Reference in New Issue