pecos_core/sets/vec_set/
set_impl.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::sets::vec_set::iterators::{Difference, Intersection, SymmetricDifference, Union};
14use crate::VecSet;
15use core::slice::Iter;
16
17use crate::{Element, Set};
18
19impl<'a, E: Element + 'a> Set<'a> for VecSet<E> {
20    type Element = E;
21    type Iter = Iter<'a, E>;
22    type Difference = Difference<'a, E>;
23    type Intersection = Intersection<'a, E>;
24    type SymmetricDifference = SymmetricDifference<'a, E>;
25    type Union = Union<'a, E>;
26
27    #[inline]
28    #[must_use]
29    fn new() -> Self {
30        Self::new()
31    }
32
33    #[inline]
34    fn capacity(&self) -> usize {
35        self.elements.capacity()
36    }
37
38    #[inline]
39    fn clear(&mut self) {
40        self.elements.clear();
41    }
42
43    #[inline]
44    fn contains(&self, value: &E) -> bool {
45        self.elements.contains(value)
46    }
47
48    #[inline]
49    fn difference(&'a self, other: &'a Self) -> Self::Difference {
50        Difference {
51            iter: self.elements.iter(),
52            other,
53        }
54    }
55
56    #[inline]
57    fn difference_ref(&'a self, other: &'a Self) -> Self::Difference {
58        self.difference(other)
59    }
60
61    #[inline]
62    fn insert(&mut self, value: E) {
63        if !self.elements.contains(&value) {
64            self.elements.push(value);
65        }
66    }
67
68    #[inline]
69    fn intersection(&'a self, other: &'a Self) -> Self::Intersection {
70        if self.len() <= other.len() {
71            Intersection {
72                iter: self.iter(),
73                other,
74            }
75        } else {
76            Intersection {
77                iter: other.iter(),
78                other: self,
79            }
80        }
81    }
82
83    #[inline]
84    fn is_empty(&self) -> bool {
85        self.elements.is_empty()
86    }
87
88    #[inline]
89    fn iter(&'a self) -> Self::Iter {
90        self.elements.iter()
91    }
92
93    #[inline]
94    fn len(&self) -> usize {
95        self.elements.len()
96    }
97
98    #[inline]
99    fn remove(&mut self, value: &E) {
100        self.elements.retain(|&x| x != *value);
101    }
102
103    #[inline]
104    fn retain<F>(&mut self, f: F)
105    where
106        F: FnMut(&E) -> bool,
107    {
108        self.elements.retain(f);
109    }
110
111    #[inline]
112    fn symmetric_difference(&'a self, other: &'a Self) -> Self::SymmetricDifference {
113        SymmetricDifference {
114            iter: self.difference(other).chain(other.difference(self)),
115        }
116    }
117
118    #[inline]
119    fn symmetric_difference_update(&mut self, rhs: &Self) {
120        let mut temp = Self::new();
121        self.retain(|x| {
122            if rhs.contains(x) {
123                temp.insert(*x);
124                false
125            } else {
126                true
127            }
128        });
129        for item in &rhs.elements {
130            if !temp.contains(item) {
131                self.insert(*item);
132            }
133        }
134    }
135
136    #[inline]
137    fn union(&'a self, other: &'a Self) -> Self::Union {
138        if self.len() >= other.len() {
139            Union {
140                iter: self.iter().chain(other.difference(self)),
141            }
142        } else {
143            Union {
144                iter: other.iter().chain(self.difference(other)),
145            }
146        }
147    }
148
149    #[inline]
150    fn union_update(&mut self, rhs: &Self) {
151        for &item in &rhs.elements {
152            self.insert(item);
153        }
154    }
155
156    #[inline]
157    #[must_use]
158    fn with_capacity(capacity: usize) -> Self {
159        Self {
160            elements: Vec::with_capacity(capacity),
161        }
162    }
163}