vecmap/set/
impls.rs

1use core::ops::{BitAnd, BitOr, BitXor, Index, Sub};
2
3use super::VecSet;
4use alloc::vec::Vec;
5
6impl<T> Default for VecSet<T> {
7    fn default() -> Self {
8        VecSet::new()
9    }
10}
11
12impl<T> Index<usize> for VecSet<T> {
13    type Output = T;
14
15    fn index(&self, index: usize) -> &T {
16        self.get_index(index).expect("VecSet: index out of bounds")
17    }
18}
19
20impl<T> Extend<T> for VecSet<T>
21where
22    T: Eq,
23{
24    fn extend<I>(&mut self, iterable: I)
25    where
26        I: IntoIterator<Item = T>,
27    {
28        let iter = iterable.into_iter();
29        let reserve = if self.is_empty() {
30            iter.size_hint().0
31        } else {
32            (iter.size_hint().0 + 1) / 2
33        };
34        self.reserve(reserve);
35        iter.for_each(move |elem| {
36            self.insert(elem);
37        });
38    }
39}
40
41impl<'a, T> Extend<&'a T> for VecSet<T>
42where
43    T: 'a + Copy + Eq,
44{
45    fn extend<I>(&mut self, iterable: I)
46    where
47        I: IntoIterator<Item = &'a T>,
48    {
49        self.extend(iterable.into_iter().copied());
50    }
51}
52
53impl<T> FromIterator<T> for VecSet<T>
54where
55    T: Eq,
56{
57    fn from_iter<I>(iter: I) -> Self
58    where
59        I: IntoIterator<Item = T>,
60    {
61        let iter = iter.into_iter();
62        let lower = iter.size_hint().0;
63        let mut map = VecSet::with_capacity(lower);
64        map.extend(iter);
65        map
66    }
67}
68
69impl<T> From<Vec<T>> for VecSet<T>
70where
71    T: Eq,
72{
73    /// Constructs set from a vector.
74    ///
75    /// **Note**: This conversion has a quadratic complexity because the
76    /// conversion preserves order of elements while at the same time having to
77    /// make sure no duplicate elements exist. To avoid it, sort and deduplicate
78    /// the vector and use [`VecSet::from_vec_unchecked`] instead.
79    fn from(mut vec: Vec<T>) -> Self {
80        crate::dedup(&mut vec, |rhs, lhs| rhs == lhs);
81        // SAFETY: We've just deduplicated the elements.
82        unsafe { Self::from_vec_unchecked(vec) }
83    }
84}
85
86impl<T> From<&[T]> for VecSet<T>
87where
88    T: Clone + Eq,
89{
90    fn from(slice: &[T]) -> Self {
91        VecSet::from_iter(slice.to_vec())
92    }
93}
94
95impl<T> From<&mut [T]> for VecSet<T>
96where
97    T: Clone + Eq,
98{
99    fn from(slice: &mut [T]) -> Self {
100        VecSet::from_iter(slice.to_vec())
101    }
102}
103
104impl<T, const N: usize> From<[T; N]> for VecSet<T>
105where
106    T: Eq,
107{
108    fn from(arr: [T; N]) -> Self {
109        VecSet::from_iter(arr)
110    }
111}
112
113impl<T> PartialEq for VecSet<T>
114where
115    T: Eq,
116{
117    fn eq(&self, other: &VecSet<T>) -> bool {
118        if self.len() != other.len() {
119            return false;
120        }
121
122        self.iter().all(|key| other.contains(key))
123    }
124}
125
126impl<T> Eq for VecSet<T> where T: Eq {}
127
128impl<T> BitAnd<&VecSet<T>> for &VecSet<T>
129where
130    T: Eq + Clone,
131{
132    type Output = VecSet<T>;
133
134    /// Returns the set intersection, cloned into a new set.
135    ///
136    /// Values are collected in the same order that they appear in `self`.
137    fn bitand(self, other: &VecSet<T>) -> Self::Output {
138        self.intersection(other).cloned().collect()
139    }
140}
141
142impl<T> BitOr<&VecSet<T>> for &VecSet<T>
143where
144    T: Eq + Clone,
145{
146    type Output = VecSet<T>;
147
148    /// Returns the set union, cloned into a new set.
149    ///
150    /// Values from `self` are collected in their original order, followed by values that are
151    /// unique to `other` in their original order.
152    fn bitor(self, other: &VecSet<T>) -> Self::Output {
153        self.union(other).cloned().collect()
154    }
155}
156
157impl<T> BitXor<&VecSet<T>> for &VecSet<T>
158where
159    T: Eq + Clone,
160{
161    type Output = VecSet<T>;
162
163    /// Returns the set symmetric-difference, cloned into a new set.
164    ///
165    /// Values from `self` are collected in their original order, followed by values from `other`
166    /// in their original order.
167    fn bitxor(self, other: &VecSet<T>) -> Self::Output {
168        self.symmetric_difference(other).cloned().collect()
169    }
170}
171
172impl<T> Sub<&VecSet<T>> for &VecSet<T>
173where
174    T: Eq + Clone,
175{
176    type Output = VecSet<T>;
177
178    /// Returns the set difference, cloned into a new set.
179    ///
180    /// Values are collected in the same order that they appear in `self`.
181    fn sub(self, other: &VecSet<T>) -> Self::Output {
182        self.difference(other).cloned().collect()
183    }
184}