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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//! This crate provides `TrashMap` and `TrashSet` types, which allow you to directly use the key
//! hash to operate with your entries. This is typically useful for when it's cheap to hold on to
//! the hash value (e.g. within a single stack frame) and you don't want to incur the cost of
//! rehashing on each access (but you can't use `Entry` as the map may change in the process)
//!
//! The `Trash` type is used to represent computed hashes, lookups via `Trash` are cheap.
//! 
//! An example of using this would be to check for cycles when doing some kind of graph traversal:
//!
//! ```rust
//! use trashmap::TrashSet;
//! # fn lookup_children(entry: &str) -> &'static [&'static str] { &[] }
//! struct State {
//!    seen: TrashSet<str>,
//! }
//!
//! impl State {
//!     fn step_into(&mut self, entry: &str) {
//!         let (id, empty) = self.seen.insert_check(entry);
//!         if !empty {
//!             panic!("found recursive loop!");
//!         }
//!         let children = lookup_children(entry);
//!         for child in children {
//!            self.step_into(child);
//!         }
//!         self.seen.remove(id);
//!     }
//! }
//! ```
//!
use std::borrow::Borrow;
use std::collections::{hash_map::RandomState, HashMap, HashSet};
use std::hash::{BuildHasher, BuildHasherDefault, Hash, Hasher};
use std::marker::PhantomData;

/// A hasher to be used with things that are already hashes
#[derive(Default)]
struct KnownHasher {
    hash: Option<u64>,
}

impl Hasher for KnownHasher {
    #[inline]
    fn write(&mut self, _: &[u8]) {
        panic!("KnownHasher must be called with known u64 hash values")
    }

    #[inline]
    fn write_u64(&mut self, i: u64) {
        debug_assert!(self.hash.is_none());
        self.hash = Some(i);
    }

    #[inline]
    fn finish(&self) -> u64 {
        self.hash.expect("Nothing was hashed") as u64
    }
}

/// `Trash` is a hash, and can be used directly with `TrashMap`
/// and `TrashSet` to interact with entries
///
/// Think of it as an identifier for a map or set entry
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
pub struct Trash(u64);

impl Trash {
    pub fn get_hash(&self) -> u64 {
        self.0
    }
}

/// A hash map that can operate on known hash values (`Trash`) instead of actual keys
///
/// Sometimes you need to access the same element in the hashmap multiple times and
/// don't wish to re-hash each time. In such a case, you can use `TrashMap`, which
/// will provide a `Trash` id that can be used to cheaply access map values as long
/// as you keep it around.
///
/// An assumption made here is that there are no hash collisions in the `u64` hash space
/// for your hasher. If there are, this may result in unpredictable behavior.
///
///
/// ```
/// use trashmap::TrashMap;
/// # fn do_stuff(x: &mut TrashMap<str, &'static str>) {}
///
/// let mut map = TrashMap::new();
/// let id = map.insert("foo", "bar");
/// do_stuff(&mut map);
/// assert!(map.get(id) == Some(&"bar"));
/// map.remove(id);
/// ```
pub struct TrashMap<K: ?Sized, V, S = RandomState> {
    hasher: S,
    map: HashMap<Trash, V, BuildHasherDefault<KnownHasher>>,
    key: PhantomData<*const K>,
}

impl<K: ?Sized, V, S> TrashMap<K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    /// Construct a basic TrashMap
    #[inline]
    pub fn new() -> Self
    where
        S: Default,
    {
        Self {
            hasher: Default::default(),
            map: Default::default(),
            key: PhantomData,
        }
    }

    /// Construct a TrashMap with a custom hasher and/or capacity
    #[inline]
    pub fn with_capacity_and_hasher(cap: usize, hasher: S) -> Self {
        Self {
            hasher,
            map: HashMap::with_capacity_and_hasher(cap, Default::default()),
            key: PhantomData,
        }
    }

    /// Inserts a key-value pair, returning the `Trash` id for the entry
    #[inline]
    pub fn insert<Q: ?Sized>(&mut self, k: &Q, v: V) -> Trash
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(k);
        self.map.insert(trash, v);
        trash
    }

    /// Inserts a key-value pair, using the `Trash` id for the key
    ///
    /// Returns the old value if present
    #[inline]
    pub fn insert_id(&mut self, k: Trash, v: V) -> Option<V>
    {
        self.map.insert(k, v)
    }

    /// Inserts a key-value pair, returning the `Trash` id for the entry as well
    /// as the old entry, if present
    #[inline]
    pub fn insert_replace<Q: ?Sized>(&mut self, k: &Q, v: V) -> (Trash, Option<V>)
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(k);
        (trash, self.map.insert(trash, v))
    }

    /// Gets the entry corresponding to a given `Trash` id, if present
    #[inline]
    pub fn get(&self, key: Trash) -> Option<&V> {
        self.map.get(&key)
    }

    /// Gets the entry corresponding to a given key, if present.
    #[inline]
    pub fn get_key<Q: ?Sized>(&self, key: &Q) -> Option<&V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(key);
        self.map.get(&trash)
    }

    /// Removes and returns the entry corresponding to a given `Trash` id,
    /// if present
    #[inline]
    pub fn remove(&mut self, key: Trash) -> Option<V> {
        self.map.remove(&key)
    }

    /// Removes and returns an entry corresponding to a given key
    #[inline]
    pub fn remove_key<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(key);
        self.map.remove(&trash)
    }

    /// Get the `Trash` id for a given key
    #[inline]
    pub fn trash<Q: ?Sized>(&self, k: &Q) -> Trash
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let mut state = self.hasher.build_hasher();
        k.hash(&mut state);
        Trash(state.finish())
    }
}

impl<K: ?Sized, V, S> Default for TrashMap<K, V, S>
where
    K: Eq + Hash,
    S: BuildHasher + Default,
{
    fn default() -> Self {
        Self::new()
    }
}


/// A hash set that can operate on known hash values (`Trash`) instead of actual keys
///
/// Sometimes you need to access the same element in the set multiple times and
/// don't wish to re-hash each time. In such a case, you can use `TrashMap`, which
/// will provide a `Trash` id that can be used to cheaply access map values as long
/// as you keep it around.
///
/// An assumption made here is that there are no hash collisions in the `u64` hash space
/// for your hasher. If there are, this may result in unpredictable behavior.
///
///
/// ```
/// use trashmap::TrashSet;
/// # fn do_stuff(x: &mut TrashSet<str>) {}
///
/// let mut map = TrashSet::new();
/// let id = map.insert("foo");
/// do_stuff(&mut map);
/// assert!(map.contains(id));
/// map.remove(id);
/// ```
///
///
/// For example, this is useful if you're doing some kind of recursion-prevention
/// scheme:
///
/// ```rust
/// use trashmap::TrashSet;
/// # fn lookup_children(entry: &str) -> &'static [&'static str] { &[] }
/// struct State {
///    seen: TrashSet<str>,
/// }
///
/// impl State {
///     fn step_into(&mut self, entry: &str) {
///         let (id, empty) = self.seen.insert_check(entry);
///         if !empty {
///             panic!("found recursive loop!");
///         }
///         let children = lookup_children(entry);
///         for child in children {
///            self.step_into(child);
///         }
///         self.seen.remove(id);
///     }
/// }
/// ```
pub struct TrashSet<K: ?Sized, S = RandomState> {
    hasher: S,
    set: HashSet<Trash, BuildHasherDefault<KnownHasher>>,
    key: PhantomData<*const K>,
}

impl<K: ?Sized, S> TrashSet<K, S>
where
    K: Eq + Hash,
    S: BuildHasher,
{
    /// Construct a basic TrashSet
    #[inline]
    pub fn new() -> Self
    where
        S: Default,
    {
        Self {
            hasher: Default::default(),
            set: Default::default(),
            key: PhantomData,
        }
    }

    /// Construct a TrashSet with a custom hasher and/or capacity
    #[inline]
    pub fn with_capacity_and_hasher(cap: usize, hasher: S) -> Self {
        Self {
            hasher,
            set: HashSet::with_capacity_and_hasher(cap, Default::default()),
            key: PhantomData,
        }
    }

    /// Insert a key, getting a `Trash` id to be used to access the entry later
    #[inline]
    pub fn insert<Q: ?Sized>(&mut self, key: &Q) -> Trash
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(key);
        self.set.insert(trash);
        trash
    }

    /// Insert a key, getting a `Trash` id to be used to access the entry later,
    /// as well as a boolean indicating if the entry was empty (true if empty, false otherwise)
    #[inline]
    pub fn insert_check<Q: ?Sized>(&mut self, key: &Q) -> (Trash, bool)
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(key);
        (trash, self.set.insert(trash))
    }

    /// Insert an element based on its `Trash` id
    ///
    /// Returns whether or not the entry was previously unset (true if unset, false otherwise)
    #[inline]
    pub fn insert_id(&mut self, key: Trash) -> bool
    {
        self.set.insert(key)
    }

    /// Check if the `Trash` id has been inserted before
    #[inline]
    pub fn contains(&self, key: Trash) -> bool {
        self.set.contains(&key)
    }

    /// Check if the key has been inserted before
    ///
    /// Also returns the `Trash` id for the key
    #[inline]
    pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let trash = self.trash(key);
        self.set.contains(&trash)
    }

    /// Remove an entry based on its `Trash` id
    #[inline]
    pub fn remove(&mut self, key: Trash) -> bool {
        self.set.remove(&key)
    }

    /// Remove an entry given its key
    #[inline]
    pub fn remove_key<Q: ?Sized>(&mut self, key: &Q) -> bool
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.set.remove(&self.trash(key))
    }

    /// Get the `Trash` id for a given key
    #[inline]
    pub fn trash<Q: ?Sized>(&self, k: &Q) -> Trash
    where
        K: Borrow<Q>,
        Q: Hash + Eq,
    {
        let mut state = self.hasher.build_hasher();
        k.hash(&mut state);
        Trash(state.finish())
    }
}

impl<K: ?Sized, S> Default for TrashSet<K, S>
where
    K: Eq + Hash,
    S: BuildHasher + Default,
{
    fn default() -> Self {
        Self::new()
    }
}