|
| 1 | +//! Hash Value Memoization |
| 2 | +
|
| 3 | +use std::{ |
| 4 | + borrow::Borrow, |
| 5 | + hash::{BuildHasher, BuildHasherDefault, Hash, Hasher}, |
| 6 | + num::NonZeroU64, |
| 7 | + sync::atomic::{AtomicU64, Ordering}, |
| 8 | +}; |
| 9 | + |
| 10 | +use ahash::RandomState; |
| 11 | + |
| 12 | +/// A wrapper that memoizes the hash value of its contained data. |
| 13 | +#[derive(Debug)] |
| 14 | +pub struct HashMemo<T, H: BuildHasher = RandomState> |
| 15 | +where |
| 16 | + T: Eq + PartialEq + Hash, |
| 17 | +{ |
| 18 | + value: T, |
| 19 | + hash: AtomicU64, |
| 20 | + hasher: H, |
| 21 | +} |
| 22 | + |
| 23 | +impl<T, H> PartialOrd for HashMemo<T, H> |
| 24 | +where |
| 25 | + T: Eq + Hash + PartialOrd, |
| 26 | + H: BuildHasher, |
| 27 | +{ |
| 28 | + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { |
| 29 | + self.value.partial_cmp(&other.value) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl<T, H> Ord for HashMemo<T, H> |
| 34 | +where |
| 35 | + T: Eq + Hash + Ord, |
| 36 | + H: BuildHasher, |
| 37 | +{ |
| 38 | + fn cmp(&self, other: &Self) -> std::cmp::Ordering { |
| 39 | + self.value.cmp(&other.value) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +impl<T, H> HashMemo<T, H> |
| 44 | +where |
| 45 | + T: Eq + Hash, |
| 46 | + H: BuildHasher, |
| 47 | +{ |
| 48 | + /// Creates a new `HashMemo` with a custom hasher. |
| 49 | + /// |
| 50 | + /// This allows you to specify a custom `BuildHasher` implementation for |
| 51 | + /// controlling how hash values are computed. |
| 52 | + pub const fn with_hasher(value: T, hasher: H) -> Self { |
| 53 | + Self { |
| 54 | + value, |
| 55 | + hash: AtomicU64::new(u64::MIN), |
| 56 | + hasher, |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +impl<T, H> PartialEq for HashMemo<T, H> |
| 62 | +where |
| 63 | + T: Eq + Hash, |
| 64 | + H: BuildHasher, |
| 65 | +{ |
| 66 | + fn eq(&self, other: &Self) -> bool { |
| 67 | + self.value == other.value |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +impl<T, H> Eq for HashMemo<T, H> |
| 72 | +where |
| 73 | + T: Eq + Hash, |
| 74 | + H: BuildHasher, |
| 75 | +{ |
| 76 | +} |
| 77 | + |
| 78 | +impl<T, H> Hash for HashMemo<T, H> |
| 79 | +where |
| 80 | + T: Eq + Hash, |
| 81 | + H: BuildHasher, |
| 82 | +{ |
| 83 | + fn hash<H2: Hasher>(&self, state: &mut H2) { |
| 84 | + let hash = self.hash.load(Ordering::Relaxed); |
| 85 | + if hash != 0 { |
| 86 | + state.write_u64(hash); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + let computed_hash = NonZeroU64::new(self.hasher.hash_one(&self.value)) |
| 91 | + .map(NonZeroU64::get) |
| 92 | + .unwrap_or(u64::MIN | 1); |
| 93 | + |
| 94 | + let _ = self.hash.compare_exchange( |
| 95 | + u64::MIN, |
| 96 | + computed_hash, |
| 97 | + Ordering::Relaxed, |
| 98 | + Ordering::Relaxed, |
| 99 | + ); |
| 100 | + state.write_u64(computed_hash); |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +impl<T, H> AsRef<T> for HashMemo<T, H> |
| 105 | +where |
| 106 | + T: Eq + Hash, |
| 107 | + H: BuildHasher, |
| 108 | +{ |
| 109 | + fn as_ref(&self) -> &T { |
| 110 | + &self.value |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +impl<T, H> Borrow<T> for HashMemo<T, H> |
| 115 | +where |
| 116 | + T: Eq + Hash, |
| 117 | + H: BuildHasher, |
| 118 | +{ |
| 119 | + fn borrow(&self) -> &T { |
| 120 | + &self.value |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +impl<T, H> From<T> for HashMemo<T, BuildHasherDefault<H>> |
| 125 | +where |
| 126 | + T: Eq + Hash, |
| 127 | + H: Hasher + Default, |
| 128 | +{ |
| 129 | + fn from(value: T) -> Self { |
| 130 | + Self::with_hasher(value, BuildHasherDefault::<H>::default()) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +impl<T, H> Clone for HashMemo<T, H> |
| 135 | +where |
| 136 | + T: Eq + Hash + Clone, |
| 137 | + H: BuildHasher + Clone, |
| 138 | +{ |
| 139 | + fn clone(&self) -> Self { |
| 140 | + Self { |
| 141 | + value: self.value.clone(), |
| 142 | + hash: AtomicU64::new(self.hash.load(Ordering::Relaxed)), |
| 143 | + hasher: self.hasher.clone(), |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments