logo
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
use crate::gc::{custom_trace, Finalize, Trace};
use indexmap::{
    set::{IntoIter, Iter},
    IndexSet,
};
use std::{
    collections::hash_map::RandomState,
    fmt::Debug,
    hash::{BuildHasher, Hash},
};

/// A newtype wrapping indexmap::IndexSet
#[derive(Clone)]
pub struct OrderedSet<V, S = RandomState>(IndexSet<V, S>)
where
    V: Hash + Eq;

impl<V: Eq + Hash + Trace, S: BuildHasher> Finalize for OrderedSet<V, S> {}
unsafe impl<V: Eq + Hash + Trace, S: BuildHasher> Trace for OrderedSet<V, S> {
    custom_trace!(this, {
        for v in this.0.iter() {
            mark(v);
        }
    });
}

impl<V: Hash + Eq + Debug> Debug for OrderedSet<V> {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        self.0.fmt(formatter)
    }
}

impl<V: Hash + Eq> Default for OrderedSet<V> {
    fn default() -> Self {
        Self::new()
    }
}

impl<V> OrderedSet<V>
where
    V: Hash + Eq,
{
    pub fn new() -> Self {
        OrderedSet(IndexSet::new())
    }

    pub fn with_capacity(capacity: usize) -> Self {
        OrderedSet(IndexSet::with_capacity(capacity))
    }

    /// Return the number of key-value pairs in the map.
    ///
    /// Computes in **O(1)** time.
    pub fn size(&self) -> usize {
        self.0.len()
    }

    /// Returns true if the map contains no elements.
    ///
    /// Computes in **O(1)** time.
    pub fn is_empty(&self) -> bool {
        self.0.len() == 0
    }

    /// Insert a value pair in the set.
    ///
    /// If an equivalent value already exists in the set: ???
    ///
    /// If no equivalent value existed in the set: the new value is
    /// inserted, last in order, and false
    ///
    /// Computes in **O(1)** time (amortized average).
    pub fn add(&mut self, value: V) -> bool {
        self.0.insert(value)
    }

    /// Delete the `value` from the set and return true if successful
    ///
    /// Return `false` if `value` is not in map.
    ///
    /// Computes in **O(n)** time (average).
    pub fn delete(&mut self, value: &V) -> bool {
        self.0.shift_remove(value)
    }

    /// Checks if a given value is present in the set
    ///
    /// Return `true` if `value` is present in set, false otherwise.
    ///
    /// Computes in **O(n)** time (average).
    pub fn contains(&self, value: &V) -> bool {
        self.0.contains(value)
    }

    /// Get a key-value pair by index
    /// Valid indices are 0 <= index < self.len()
    /// Computes in O(1) time.
    pub fn get_index(&self, index: usize) -> Option<&V> {
        self.0.get_index(index)
    }

    /// Return an iterator over the values of the set, in their order
    pub fn iter(&self) -> Iter<'_, V> {
        self.0.iter()
    }
}

impl<'a, V, S> IntoIterator for &'a OrderedSet<V, S>
where
    V: Hash + Eq,
    S: BuildHasher,
{
    type Item = &'a V;
    type IntoIter = Iter<'a, V>;
    fn into_iter(self) -> Self::IntoIter {
        self.0.iter()
    }
}

impl<V, S> IntoIterator for OrderedSet<V, S>
where
    V: Hash + Eq,
    S: BuildHasher,
{
    type Item = V;
    type IntoIter = IntoIter<V>;
    fn into_iter(self) -> IntoIter<V> {
        self.0.into_iter()
    }
}