pecos_core/sets/vec_set/
operators.rs

1// Copyright 2024 The PECOS Developers
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4// in compliance with the License.You may obtain a copy of the License at
5//
6//     https://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software distributed under the License
9// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10// or implied. See the License for the specific language governing permissions and limitations under
11// the License.
12
13use crate::build_set_bit_ops;
14use crate::Set;
15use crate::VecSet;
16use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Sub, SubAssign};
17
18use crate::Element;
19
20impl<'a, E: Element> BitAnd<&'a VecSet<E>> for &'a VecSet<E> {
21    type Output = VecSet<E>;
22
23    #[inline]
24    fn bitand(self, rhs: &'a VecSet<E>) -> Self::Output {
25        VecSet {
26            elements: self.intersection(rhs).copied().collect(),
27        }
28    }
29}
30
31impl<'a, E: Element> BitOr<&'a VecSet<E>> for &VecSet<E> {
32    type Output = VecSet<E>;
33
34    #[inline]
35    fn bitor(self, rhs: &'a VecSet<E>) -> VecSet<E> {
36        VecSet {
37            elements: self.union(rhs).copied().collect(),
38        }
39    }
40}
41
42impl<'a, E: Element> BitXor<&'a VecSet<E>> for &VecSet<E> {
43    type Output = VecSet<E>;
44
45    #[inline]
46    fn bitxor(self, rhs: &'a VecSet<E>) -> VecSet<E> {
47        VecSet {
48            elements: self.symmetric_difference(rhs).copied().collect(),
49        }
50    }
51}
52
53impl<'a, E: Element> Sub<&'a VecSet<E>> for &VecSet<E> {
54    type Output = VecSet<E>;
55
56    #[inline]
57    fn sub(self, rhs: &'a VecSet<E>) -> VecSet<E> {
58        VecSet {
59            elements: self.difference(rhs).copied().collect(),
60        }
61    }
62}
63
64// TODO: Shouldn't this be fore &VecSet<E>?
65impl<E: Element> BitXorAssign<VecSet<E>> for VecSet<E> {
66    #[inline]
67    fn bitxor_assign(&mut self, rhs: VecSet<E>) {
68        for item in rhs.elements {
69            if self.contains(&item) {
70                self.remove(&item);
71            } else {
72                self.insert(item);
73            }
74        }
75    }
76}
77
78//impl<'a, T> BitXorAssign<&'a Self> for VecSet<T>
79//where
80//T: UnsignedInt + 'a,
81//{
82//fn bitxor_assign(&mut self, rhs: &Self) {
83//self.bitxor_assign_set(rhs);
84//}
85//}
86
87//impl<'a, T> BitXorAssign<&'a T> for VecSet<T>
88//where
89//T: UnsignedInt + 'a,
90//{
91//fn bitxor_assign(&mut self, rhs: &T) {
92//self.bitxor_assign_item(rhs);
93//}
94//}
95
96build_set_bit_ops!(VecSet<E>);