1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#![doc = include_str!("../README.md")]

#[cfg(test)]
mod tests;
mod borrowed;

pub use borrowed::Borrowed;

use core::{
    borrow::{Borrow, BorrowMut},
    cell::Cell,
    cmp::Ordering,
    fmt::{self, Debug},
    hash::{Hash, Hasher},
    marker::PhantomData,
    ops::{Deref, DerefMut},
    sync::atomic::{AtomicU64, Ordering as MOrd},
};
use std::{
    rc::Rc,
    sync::Arc,
    collections::hash_map::DefaultHasher,
};

/// storage trait for storing hash status
pub trait HashStorer {
    /// Clear stored hash code to none
    fn clear(&mut self);

    /// Get stored hash code
    fn get(&self) -> Option<u64>;

    /// if stored hash code is uninit, call init func
    ///
    /// return inited hash code
    fn get_or_init<F>(&self, f: F) -> u64
    where F: FnOnce() -> u64;

    fn hash_one<T, H>(value: &T) -> u64
    where T: ?Sized + Hash,
          H: Hasher + Default,
          Self: Default,
    {
        Self::default()
            .get_or_init(|| {
                let mut hasher = H::default();
                value.hash(&mut hasher);
                hasher.finish()
            })
    }
}

impl HashStorer for Cell<u64> {
    fn clear(&mut self) {
        self.set(0)
    }

    fn get(&self) -> Option<u64> {
        let n = self.get();
        if n == 0 { return None; }
        Some(n)
    }

    fn get_or_init<F>(&self, f: F) -> u64
    where F: FnOnce() -> u64,
    {
        HashStorer::get(self)
            .unwrap_or_else(|| {
                let mut n = f();
                if n == 0 { n = u64::MAX >> 2 }
                self.set(n);
                n
            })
    }
}
impl HashStorer for AtomicU64 {
    fn clear(&mut self) {
        self.store(0, MOrd::Relaxed)
    }

    fn get(&self) -> Option<u64> {
        let n = self.load(MOrd::Relaxed);
        if n == 0 { return None; }
        Some(n)
    }

    fn get_or_init<F>(&self, f: F) -> u64
    where F: FnOnce() -> u64,
    {
        HashStorer::get(self)
            .unwrap_or_else(|| {
                let mut n = f();
                if n == 0 { n = u64::MAX >> 2 }
                self.store(n, MOrd::Relaxed);
                n
            })
    }
}
impl<T: HashStorer + Default> HashStorer for Rc<T> {
    fn get(&self) -> Option<u64> {
        <T as HashStorer>::get(&**self)
    }

    fn clear(&mut self) {
        Rc::get_mut(self)
            .map(T::clear)
            .unwrap_or_else(|| {
                *self = Default::default()
            })
    }

    fn get_or_init<F>(&self, f: F) -> u64
    where F: FnOnce() -> u64,
    {
        <T as HashStorer>::get_or_init(&**self, f)
    }

    fn hash_one<T1, H>(value: &T1) -> u64
    where T1: ?Sized + Hash,
          H: Hasher + Default,
          Self: Default,
    {
        T::hash_one::<T1, H>(value)
    }
}
impl<T: HashStorer + Default> HashStorer for Arc<T> {
    fn get(&self) -> Option<u64> {
        <T as HashStorer>::get(&**self)
    }

    fn clear(&mut self) {
        Arc::get_mut(self)
            .map(T::clear)
            .unwrap_or_else(|| {
                *self = Default::default()
            })
    }

    fn get_or_init<F>(&self, f: F) -> u64
    where F: FnOnce() -> u64,
    {
        <T as HashStorer>::get_or_init(&**self, f)
    }

    fn hash_one<T1, H>(value: &T1) -> u64
    where T1: ?Sized + Hash,
          H: Hasher + Default,
          Self: Default,
    {
        T::hash_one::<T1, H>(value)
    }
}

/// A wrapper for storing hash results to avoid running costly hash functions
/// multiple times without modifying the value
///
/// # Examples
/// ```
/// # use hash_on_write::How;
/// # use std::collections::HashSet;
/// let mut x = How::new_default("foo".to_owned());
///
/// assert!(! How::is_hashed(&x));
/// HashSet::new().insert(&x);
/// assert!(How::is_hashed(&x));
///
/// How::make_mut(&mut x).push('!');
/// assert!(! How::is_hashed(&x));
/// assert_eq!(*x, "foo!");
/// ```
///
/// ---
/// Due to the inability of the stored hashcode to replicate the action of `T::hash,`
/// it is not possible to implement [`Borrow<T>`]
///
/// [`Borrow<T>`]: core::borrow::Borrow
pub struct How<T: ?Sized, H = DefaultHasher, S = Cell<u64>> {
    _hasher: PhantomData<H>,
    hashcode: S,
    value: T,
}
impl<T, H, S> Default for How<T, H, S>
where T: ?Sized + Default,
      S: Default,
{
    fn default() -> Self {
        Self::new(Default::default())
    }
}
impl<T: ?Sized, H, S> AsRef<T> for How<T, H, S> {
    fn as_ref(&self) -> &T {
        &self.value
    }
}
impl<T: ?Sized, H, S: HashStorer> AsMut<T> for How<T, H, S> {
    fn as_mut(&mut self) -> &mut T {
        How::make_mut(self)
    }
}
impl<T: ?Sized, H, S> AsRef<Self> for How<T, H, S> {
    fn as_ref(&self) -> &Self {
        self
    }
}
impl<T: ?Sized, H, S> AsMut<Self> for How<T, H, S> {
    fn as_mut(&mut self) -> &mut Self {
        self
    }
}
impl<T, Q, H, S> Borrow<Borrowed<Q, H, S>> for How<T, H, S>
where T: ?Sized + Borrow<Q>,
      Q: ?Sized,
{
    fn borrow(&self) -> &Borrowed<Q, H, S> {
        Borrowed::make_ref(self.value.borrow())
    }
}
impl<T, Q, H, S> BorrowMut<Borrowed<Q, H, S>> for How<T, H, S>
where T: ?Sized + BorrowMut<Q>,
      Q: ?Sized,
{
    fn borrow_mut(&mut self) -> &mut Borrowed<Q, H, S> {
        Borrowed::make_mut(self.value.borrow_mut())
    }
}
impl<T: ?Sized + Debug, H, S: Debug> Debug for How<T, H, S> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("How")
            .field("hashcode", &self.hashcode)
            .field("value", &&self.value)
            .finish()
    }
}
impl<T: ?Sized + Clone, H, S: Clone> Clone for How<T, H, S> {
    fn clone(&self) -> Self {
        Self {
            _hasher: PhantomData,
            hashcode: self.hashcode.clone(),
            value: self.value.clone(),
        }
    }
}
impl<T: ?Sized + Eq, H, S: HashStorer> Eq for How<T, H, S> { }
impl<T: ?Sized + Ord, H, S: HashStorer> Ord for How<T, H, S> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.value.cmp(&other.value)
    }
}
impl<T: ?Sized + PartialEq, H, S: HashStorer> PartialEq for How<T, H, S> {
    fn eq(&self, other: &Self) -> bool {
        self.hashcode.get()
            .zip(other.hashcode.get())
            .map_or(true, |(a, b)| a == b)
            && self.value == other.value
    }
}
impl<T: ?Sized + PartialOrd, H, S: HashStorer> PartialOrd for How<T, H, S> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        self.value.partial_cmp(&other.value)
    }
}
impl<T: ?Sized, H, S> Deref for How<T, H, S> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}
impl<T, H, S> DerefMut for How<T, H, S>
where T: ?Sized + Hash,
      H: Hasher + Default,
      S: HashStorer,
{
    fn deref_mut(&mut self) -> &mut Self::Target {
        Self::make_mut(self)
    }
}
impl<T, IH, S> Hash for How<T, IH, S>
where T: ?Sized + Hash,
      IH: Hasher + Default,
      S: HashStorer,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        Self::make_hash(self)
            .hash(state)
    }
}
impl<T, H, S> From<T> for How<T, H, S>
where H: Hasher + Default,
      S: HashStorer + Default,
{
    fn from(value: T) -> Self {
        Self::new(value)
    }
}
impl<T> How<T> {
    /// new, but use [`DefaultHasher`]
    ///
    /// [`DefaultHasher`]: std::collections::hash_map::DefaultHasher
    pub fn new_default(value: T) -> Self {
        How {
            _hasher: PhantomData,
            hashcode: Default::default(),
            value,
        }
    }
}
impl<T, H, S: Default> How<T, H, S> {
    /// New a wrapped value
    pub fn new(value: T) -> Self {
        How {
            _hasher: PhantomData,
            hashcode: Default::default(),
            value,
        }
    }

    /// Consume `self` into wrapped value
    pub fn into_inner(this: Self) -> T {
        this.value
    }
}
impl<T: ?Sized, H, S: HashStorer> How<T, H, S> {
    /// Get mutable and clear hash cache
    pub fn make_mut(this: &mut Self) -> &mut T {
        this.hashcode.clear();
        &mut this.value
    }

    /// Get hash cache status
    pub fn hash_code(this: &Self) -> Option<u64> {
        this.hashcode.get()
    }

    /// Get hash cache status is cached,
    /// like `How::hash_code(&value).is_some()`
    pub fn is_hashed(this: &Self) -> bool {
        Self::hash_code(this).is_some()
    }
}
impl<T, H, S> How<T, H, S>
where T: ?Sized + Hash,
      H: Default + Hasher,
      S: HashStorer,
{
    /// Get or init hash cache
    pub fn make_hash(this: &Self) -> u64 {
        this.hashcode.get_or_init(|| {
            let mut inner_hasher = H::default();
            this.value.hash(&mut inner_hasher);
            inner_hasher.finish()
        })
    }
}