Skip to main content

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        self.base.extend(iterable);
29    }
30}
31
32impl<'a, T> Extend<&'a T> for VecSet<T>
33where
34    T: 'a + Copy + Eq,
35{
36    fn extend<I>(&mut self, iterable: I)
37    where
38        I: IntoIterator<Item = &'a T>,
39    {
40        self.base.extend(iterable.into_iter().copied());
41    }
42}
43
44impl<T> FromIterator<T> for VecSet<T>
45where
46    T: Eq,
47{
48    fn from_iter<I>(iter: I) -> Self
49    where
50        I: IntoIterator<Item = T>,
51    {
52        VecSet {
53            base: super::KeyedVecSet::from_iter(iter),
54        }
55    }
56}
57
58impl<T> From<Vec<T>> for VecSet<T>
59where
60    T: Eq,
61{
62    /// Constructs set from a vector.
63    ///
64    /// **Note**: This conversion has a quadratic complexity because the
65    /// conversion preserves order of elements while at the same time having to
66    /// make sure no duplicate elements exist. To avoid it, sort and deduplicate
67    /// the vector and use [`VecSet::from_vec_unchecked`] instead.
68    fn from(vec: Vec<T>) -> Self {
69        VecSet { base: vec.into() }
70    }
71}
72
73impl<T> From<&[T]> for VecSet<T>
74where
75    T: Clone + Eq,
76{
77    fn from(slice: &[T]) -> Self {
78        VecSet { base: slice.into() }
79    }
80}
81
82impl<T> From<&mut [T]> for VecSet<T>
83where
84    T: Clone + Eq,
85{
86    fn from(slice: &mut [T]) -> Self {
87        VecSet { base: slice.into() }
88    }
89}
90
91impl<T, const N: usize> From<[T; N]> for VecSet<T>
92where
93    T: Eq,
94{
95    fn from(arr: [T; N]) -> Self {
96        VecSet { base: arr.into() }
97    }
98}
99
100impl<T> PartialEq for VecSet<T>
101where
102    T: Eq,
103{
104    fn eq(&self, other: &VecSet<T>) -> bool {
105        self.base == other.base
106    }
107}
108
109impl<T> Eq for VecSet<T> where T: Eq {}
110
111impl<T> BitAnd<&VecSet<T>> for &VecSet<T>
112where
113    T: Eq + Clone,
114{
115    type Output = VecSet<T>;
116
117    /// Returns the set intersection, cloned into a new set.
118    ///
119    /// Values are collected in the same order that they appear in `self`.
120    fn bitand(self, other: &VecSet<T>) -> Self::Output {
121        self.intersection(other).cloned().collect()
122    }
123}
124
125impl<T> BitOr<&VecSet<T>> for &VecSet<T>
126where
127    T: Eq + Clone,
128{
129    type Output = VecSet<T>;
130
131    /// Returns the set union, cloned into a new set.
132    ///
133    /// Values from `self` are collected in their original order, followed by values that are
134    /// unique to `other` in their original order.
135    fn bitor(self, other: &VecSet<T>) -> Self::Output {
136        self.union(other).cloned().collect()
137    }
138}
139
140impl<T> BitXor<&VecSet<T>> for &VecSet<T>
141where
142    T: Eq + Clone,
143{
144    type Output = VecSet<T>;
145
146    /// Returns the set symmetric-difference, cloned into a new set.
147    ///
148    /// Values from `self` are collected in their original order, followed by values from `other`
149    /// in their original order.
150    fn bitxor(self, other: &VecSet<T>) -> Self::Output {
151        self.symmetric_difference(other).cloned().collect()
152    }
153}
154
155impl<T> Sub<&VecSet<T>> for &VecSet<T>
156where
157    T: Eq + Clone,
158{
159    type Output = VecSet<T>;
160
161    /// Returns the set difference, cloned into a new set.
162    ///
163    /// Values are collected in the same order that they appear in `self`.
164    fn sub(self, other: &VecSet<T>) -> Self::Output {
165        self.difference(other).cloned().collect()
166    }
167}