Skip to main content

finit/impls/
array.rs

1use crate::Set;
2use crate::operations::{
3    Difference, DifferenceAssign, DisjunctiveUnion, DisjunctiveUnionAssign, Intersection,
4    IntersectionAssign, Union, UnionAssign,
5};
6use crate::comparisons::{SetEq, SubsetOf};
7
8impl<const N: usize, Value: Set<Empty = Value>> Set for [Value; N] {
9    type Empty = Self;
10
11    fn is_empty(&self) -> bool {
12        self.iter().all(|value| value.is_empty())
13    }
14
15    fn empty() -> Self::Empty {
16        core::array::from_fn(|_| Value::empty())
17    }
18}
19
20impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> UnionAssign<[OtherValue; N]>
21    for [Value; N]
22where
23    Value: UnionAssign<OtherValue>,
24{
25    fn union_assign(&mut self, rhs: [OtherValue; N]) {
26        for (self_i, rhs_i) in self.iter_mut().zip(rhs.into_iter()) {
27            self_i.union_assign(rhs_i);
28        }
29    }
30}
31
32impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> Union<[OtherValue; N]>
33    for [Value; N]
34where
35    Value: UnionAssign<OtherValue>,
36{
37    type Output = Self;
38
39    fn union(mut self, rhs: [OtherValue; N]) -> Self::Output {
40        self.union_assign(rhs);
41
42        self
43    }
44}
45
46impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
47    DifferenceAssign<[OtherValue; N]> for [Value; N]
48where
49    Value: DifferenceAssign<OtherValue>,
50{
51    fn difference_assign(&mut self, rhs: [OtherValue; N]) {
52        for (self_i, rhs_i) in self.iter_mut().zip(rhs.into_iter()) {
53            self_i.difference_assign(rhs_i);
54        }
55    }
56}
57
58impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
59    Difference<[OtherValue; N]> for [Value; N]
60where
61    Value: DifferenceAssign<OtherValue>,
62{
63    type Output = Self;
64
65    fn difference(mut self, rhs: [OtherValue; N]) -> Self::Output {
66        self.difference_assign(rhs);
67
68        self
69    }
70}
71
72impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
73    IntersectionAssign<[OtherValue; N]> for [Value; N]
74where
75    Value: IntersectionAssign<OtherValue>,
76{
77    fn intersection_assign(&mut self, rhs: [OtherValue; N]) {
78        for (self_i, rhs_i) in self.iter_mut().zip(rhs.into_iter()) {
79            self_i.intersection_assign(rhs_i);
80        }
81    }
82}
83
84impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
85    Intersection<[OtherValue; N]> for [Value; N]
86where
87    Value: IntersectionAssign<OtherValue>,
88{
89    type Output = Self;
90    fn intersection(mut self, other: [OtherValue; N]) -> Self::Output {
91        self.intersection_assign(other);
92
93        self
94    }
95}
96
97impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set>
98    DisjunctiveUnionAssign<[OtherValue; N]> for [Value; N]
99where
100    Value: DisjunctiveUnionAssign<OtherValue>,
101{
102    fn disjunctive_union_assign(&mut self, rhs: [OtherValue; N]) {
103        for (self_i, rhs_i) in self.iter_mut().zip(rhs.into_iter()) {
104            self_i.disjunctive_union_assign(rhs_i);
105        }
106    }
107}
108
109impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> DisjunctiveUnion<[OtherValue; N]>
110    for [Value; N]
111where
112    Value: DisjunctiveUnionAssign<OtherValue>,
113{
114    type Output = Self;
115
116    fn disjunctive_union(mut self, rhs: [OtherValue; N]) -> Self::Output {
117        self.disjunctive_union_assign(rhs);
118
119        self
120    }
121}
122
123impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> UnionAssign<&[OtherValue; N]>
124    for [Value; N]
125where
126    for<'a> Value: UnionAssign<&'a OtherValue>,
127{
128    fn union_assign(&mut self, rhs: &[OtherValue; N]) {
129        for (self_i, rhs_i) in self.iter_mut().zip(rhs.iter()) {
130            self_i.union_assign(rhs_i);
131        }
132    }
133}
134
135impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> Union<&[OtherValue; N]>
136    for [Value; N]
137where
138    for<'a> Value: UnionAssign<&'a OtherValue>,
139{
140    type Output = Self;
141
142    fn union(mut self, rhs: &[OtherValue; N]) -> Self::Output {
143        self.union_assign(rhs);
144
145        self
146    }
147}
148
149impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
150    DifferenceAssign<&[OtherValue; N]> for [Value; N]
151where
152    for<'a> Value: DifferenceAssign<&'a OtherValue>,
153{
154    fn difference_assign(&mut self, rhs: &[OtherValue; N]) {
155        for (self_i, rhs_i) in self.iter_mut().zip(rhs.iter()) {
156            self_i.difference_assign(rhs_i);
157        }
158    }
159}
160
161impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
162    Difference<&[OtherValue; N]> for [Value; N]
163where
164    for<'a> Value: DifferenceAssign<&'a OtherValue>,
165{
166    type Output = Self;
167
168    fn difference(mut self, rhs: &[OtherValue; N]) -> Self::Output {
169        self.difference_assign(rhs);
170
171        self
172    }
173}
174
175impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
176    IntersectionAssign<&[OtherValue; N]> for [Value; N]
177where
178    for<'a> Value: IntersectionAssign<&'a OtherValue>,
179{
180    fn intersection_assign(&mut self, rhs: &[OtherValue; N]) {
181        for (self_i, rhs_i) in self.iter_mut().zip(rhs.iter()) {
182            self_i.intersection_assign(rhs_i);
183        }
184    }
185}
186
187impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set<Empty = OtherValue>>
188    Intersection<&[OtherValue; N]> for [Value; N]
189where
190    for<'a> Value: IntersectionAssign<&'a OtherValue>,
191{
192    type Output = Self;
193    fn intersection(mut self, other: &[OtherValue; N]) -> Self::Output {
194        self.intersection_assign(other);
195
196        self
197    }
198}
199
200impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set>
201    DisjunctiveUnionAssign<&[OtherValue; N]> for [Value; N]
202where
203    for<'a> Value: DisjunctiveUnionAssign<&'a OtherValue>,
204{
205    fn disjunctive_union_assign(&mut self, rhs: &[OtherValue; N]) {
206        for (self_i, rhs_i) in self.iter_mut().zip(rhs.iter()) {
207            self_i.disjunctive_union_assign(rhs_i);
208        }
209    }
210}
211
212impl<const N: usize, Value: Set<Empty = Value>, OtherValue: Set> DisjunctiveUnion<&[OtherValue; N]>
213    for [Value; N]
214where
215    for<'a> Value: DisjunctiveUnionAssign<&'a OtherValue>,
216{
217    type Output = Self;
218
219    fn disjunctive_union(mut self, rhs: &[OtherValue; N]) -> Self::Output {
220        self.disjunctive_union_assign(rhs);
221
222        self
223    }
224}
225
226impl<const N: usize, Value: SetEq<OtherValue>, OtherValue> SetEq<[OtherValue; N]>
227    for [Value; N]
228{
229    fn set_eq(&self, rhs: &[OtherValue; N]) -> bool {
230        self.iter().zip(rhs.iter()).all(|(self_v, rhs_v)| self_v.set_eq(rhs_v))
231    }
232}
233
234impl<const N: usize, Value: SubsetOf<OtherValue>, OtherValue> SubsetOf<[OtherValue; N]>
235    for [Value; N]
236{
237    fn subset_of(&self, rhs: &[OtherValue; N]) -> bool {
238        self.iter().zip(rhs.iter()).all(|(self_v, rhs_v)| self_v.subset_of(rhs_v))
239    }
240}
241
242#[cfg(test)]
243mod tests {
244    use core::fmt::Debug;
245
246    use rstest::*;
247
248    use crate::operations::Union;
249
250    #[allow(unused_imports)]
251    use super::*;
252
253    #[rstest]
254    #[case([true, false, false, true], [false, true, false, true], [true, true, false, true])]
255    fn union_list_tests<T, U, V>(#[case] a: T, #[case] b: U, #[case] c: V)
256    where
257        T: for<'a> Union<&'a U, Output = V>,
258        V: PartialEq + Debug,
259    {
260        assert_eq!(a.union(&b), c);
261    }
262
263    #[rstest]
264    #[case([true, false, false, true], [false, true, false, true], [true, false, false, false])]
265    fn difference_list_tests<T, U, V>(#[case] a: T, #[case] b: U, #[case] c: V)
266    where
267        T: for<'a> Difference<&'a U, Output = V>,
268        V: PartialEq + Debug,
269    {
270        assert_eq!(a.difference(&b), c);
271    }
272
273    #[rstest]
274    #[case([true, false, false, true], [false, true, false, true], [false, false, false, true])]
275    fn intersection_list_tests<T, U, V>(#[case] a: T, #[case] b: U, #[case] c: V)
276    where
277        T: for<'a> Intersection<&'a U, Output = V>,
278        V: PartialEq + Debug,
279    {
280        assert_eq!(a.intersection(&b), c);
281    }
282}