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
pub use rustc_hash::FxHashSet;
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;

/// A deterministic wrapper around FxHashSet that does not provide iteration support.
///
/// It supports insert, remove, get functions from FxHashSet.
/// It also allows to convert hashset to a sorted vector with the method `into_sorted_vector()`.
#[derive(Clone)]
pub struct StableSet<T> {
    base: FxHashSet<T>,
}

impl<T> Default for StableSet<T>
where
    T: Eq + Hash,
{
    fn default() -> StableSet<T> {
        StableSet::new()
    }
}

impl<T> fmt::Debug for StableSet<T>
where
    T: Eq + Hash + fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}", self.base)
    }
}

impl<T> PartialEq<StableSet<T>> for StableSet<T>
where
    T: Eq + Hash,
{
    fn eq(&self, other: &StableSet<T>) -> bool {
        self.base == other.base
    }
}

impl<T> Eq for StableSet<T> where T: Eq + Hash {}

impl<T: Hash + Eq> StableSet<T> {
    pub fn new() -> StableSet<T> {
        StableSet { base: FxHashSet::default() }
    }

    pub fn into_sorted_vector(self) -> Vec<T>
    where
        T: Ord,
    {
        let mut vector = self.base.into_iter().collect::<Vec<_>>();
        vector.sort_unstable();
        vector
    }

    pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
    where
        T: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.base.get(value)
    }

    pub fn insert(&mut self, value: T) -> bool {
        self.base.insert(value)
    }

    pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool
    where
        T: Borrow<Q>,
        Q: Hash + Eq,
    {
        self.base.remove(value)
    }
}