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
use std::cell::UnsafeCell;
use std::collections::HashSet;
use std::hash::Hash;

pub struct Set<T>
where
    T: Hash,
{
    data: UnsafeCell<HashSet<Box<T>>>,
}

impl<T> Set<T>
where
    T: Hash + Eq,
{
    /// Create an empty `Set`. Uses a `HashSet` internally.
    pub fn new() -> Self {
        Self {
            data: UnsafeCell::new(HashSet::new()),
        }
    }

    /// Returns the number of elements in the set.
    pub fn len(&self) -> usize {
        unsafe {
            (*self.data.get()).len()
        }
    }

    /// Returns a reference to the value in the set, if any, that is equal to the given value.
    pub fn get(&self, value: &T) -> Option<&T> {
        unsafe {
            (*self.data.get()).get(value).map(|boxed| boxed.as_ref())
        }
    }

    /// Adds a value to the set.
    /// If the set did not have this value present, `true` is returned.
    /// If the set did have this value present, `false` is returned.
    pub fn insert(&self, value: T) -> bool {
        unsafe {
            (*self.data.get()).insert(Box::new(value))
        }
    }

    /// Inserts the given value into the set if it is not present, then returns a reference to the value in the set.
    /// Currently, this implementation uses requires `T: Clone`, because the feature `hash_set_entry` isn't stabilized yet.
    pub fn get_or_insert(&self, value: T) -> &T
    where
        T: Clone
    {
        if let Some(output) = self.get(&value) {
            output
        } else {
            assert!(self.insert(value.clone()));
            self.get(&value).unwrap()
        }
    }

    /// Returns `true` if the set contains a value.
    pub fn contains(&self, value: &T) -> bool {
        unsafe {
            (*self.data.get()).contains(value)
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn overflow() {
        let set = Set::new();
        set.insert(1);
        set.insert(2);
        set.insert(3);
        set.insert(4);
        set.insert(5);
        assert_eq!(set.len(), 5);
    }

    #[test]
    fn insert_while_borrowed() {
        let set = Set::new();
        set.insert(1);
        let first = set.get(&1);
        set.insert(2);
        assert_eq!(*first.unwrap(), 1);
    }

    #[test]
    fn contains_inserted() {
        let set = Set::new();
        set.insert(1);
        set.insert(2);
        set.insert(3);
        assert_eq!(set.contains(&3), true);
        assert_eq!(set.contains(&4), false);
    }

    #[test]
    fn get_or_insert() {
        let set = Set::new();
        set.insert(1);
        set.insert(2);
        set.insert(3);
        assert_eq!(set.get_or_insert(3), set.get(&3).unwrap());
        assert_eq!(*set.get_or_insert(4), 4);
    }

    #[test]
    fn big_test() {
        let set = Set::new();
        for i in 0..100000 {
            set.insert(i);
        }
        assert_eq!(set.len(), 100000);

        let mut refs = Vec::new();
        for i in 0..100000 {
            refs.push(set.get(&i).unwrap());
        }

        for i in 100000..200000 {
            set.insert(i);
        }
        assert_eq!(set.len(), 200000);

        for i in 0..100000 {
            assert_eq!(*refs[i], i);
        }
    }
}