/rust/registry/src/index.crates.io-1949cf8c6b5b557f/flurry-0.5.2/src/set_ref.rs
Line | Count | Source |
1 | | use crate::iter::*; |
2 | | use crate::reclaim::{Guard, GuardRef}; |
3 | | use crate::HashSet; |
4 | | use std::borrow::Borrow; |
5 | | use std::fmt::{self, Debug, Formatter}; |
6 | | use std::hash::{BuildHasher, Hash}; |
7 | | |
8 | | /// A reference to a [`HashSet`], constructed with [`HashSet::pin`] or [`HashSet::with_guard`]. |
9 | | /// |
10 | | /// The current thread will be pinned for the duration of this reference. |
11 | | /// Keep in mind that this prevents the collection of garbage generated by the set. |
12 | | pub struct HashSetRef<'set, T, S = crate::DefaultHashBuilder> { |
13 | | pub(crate) set: &'set HashSet<T, S>, |
14 | | guard: GuardRef<'set>, |
15 | | } |
16 | | |
17 | | impl<T, S> HashSet<T, S> { |
18 | | /// Get a reference to this set with the current thread pinned. |
19 | | /// |
20 | | /// Keep in mind that for as long as you hold onto this, you are preventing the collection of |
21 | | /// garbage generated by the set. |
22 | 0 | pub fn pin(&self) -> HashSetRef<'_, T, S> { |
23 | 0 | HashSetRef { |
24 | 0 | guard: GuardRef::Owned(self.guard()), |
25 | 0 | set: self, |
26 | 0 | } |
27 | 0 | } |
28 | | |
29 | | /// Get a reference to this set with the given guard. |
30 | 0 | pub fn with_guard<'g>(&'g self, guard: &'g Guard<'_>) -> HashSetRef<'g, T, S> { |
31 | 0 | HashSetRef { |
32 | 0 | guard: GuardRef::Ref(guard), |
33 | 0 | set: self, |
34 | 0 | } |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl<T, S> HashSetRef<'_, T, S> { |
39 | | /// Returns the number of elements in the set. |
40 | | /// |
41 | | /// See also [`HashSet::len`]. |
42 | 0 | pub fn len(&self) -> usize { |
43 | 0 | self.set.len() |
44 | 0 | } |
45 | | |
46 | | /// Returns `true` if the set is empty. Otherwise returns `false`. |
47 | | /// |
48 | | /// See also [`HashSet::is_empty`]. |
49 | 0 | pub fn is_empty(&self) -> bool { |
50 | 0 | self.set.is_empty() |
51 | 0 | } |
52 | | |
53 | | /// An iterator visiting all elements in arbitrary order. |
54 | | /// |
55 | | /// The iterator element type is `&'g T`. |
56 | | /// |
57 | | /// See also [`HashSet::iter`]. |
58 | 0 | pub fn iter(&self) -> Keys<'_, T, ()> { |
59 | 0 | self.set.iter(&self.guard) |
60 | 0 | } |
61 | | } |
62 | | |
63 | | impl<T, S> HashSetRef<'_, T, S> |
64 | | where |
65 | | T: Hash + Ord, |
66 | | S: BuildHasher, |
67 | | { |
68 | | /// Returns `true` if the given value is an element of this set. |
69 | | /// |
70 | | /// See also [`HashSet::contains`]. |
71 | | #[inline] |
72 | 0 | pub fn contains<Q>(&self, value: &Q) -> bool |
73 | 0 | where |
74 | 0 | T: Borrow<Q>, |
75 | 0 | Q: ?Sized + Hash + Ord, |
76 | | { |
77 | 0 | self.set.contains(value, &self.guard) |
78 | 0 | } |
79 | | |
80 | | /// Returns a reference to the element in the set, if any, that is equal to the given value. |
81 | | /// |
82 | | /// See also [`HashSet::get`]. |
83 | 0 | pub fn get<'g, Q>(&'g self, value: &Q) -> Option<&'g T> |
84 | 0 | where |
85 | 0 | T: Borrow<Q>, |
86 | 0 | Q: ?Sized + Hash + Ord, |
87 | | { |
88 | 0 | self.set.get(value, &self.guard) |
89 | 0 | } |
90 | | |
91 | | /// Returns `true` if `self` has no elements in common with `other`. |
92 | | /// |
93 | | /// See also [`HashSet::is_disjoint`]. |
94 | 0 | pub fn is_disjoint(&self, other: &HashSetRef<'_, T, S>) -> bool { |
95 | 0 | self.set.is_disjoint(other.set, &self.guard, &other.guard) |
96 | 0 | } |
97 | | |
98 | | /// Returns `true` if the set is a subset of another, i.e., `other` contains at least all the values in `self`. |
99 | | /// |
100 | | /// See also [`HashSet::is_subset`]. |
101 | 0 | pub fn is_subset(&self, other: &HashSetRef<'_, T, S>) -> bool { |
102 | 0 | self.set.is_subset(other.set, &self.guard, &other.guard) |
103 | 0 | } |
104 | | |
105 | | /// Returns `true` if the set is a superset of another, i.e., `self` contains at least all the values in `other`. |
106 | | /// |
107 | | /// See also [`HashSet::is_superset`]. |
108 | 0 | pub fn is_superset(&self, other: &HashSetRef<'_, T, S>) -> bool { |
109 | 0 | self.set.is_superset(other.set, &self.guard, &other.guard) |
110 | 0 | } |
111 | | } |
112 | | |
113 | | impl<T, S> HashSetRef<'_, T, S> |
114 | | where |
115 | | T: Sync + Send + Clone + Hash + Ord, |
116 | | S: BuildHasher, |
117 | | { |
118 | | /// Adds a value to the set. |
119 | | /// |
120 | | /// See also [`HashSet::insert`]. |
121 | 0 | pub fn insert(&self, value: T) -> bool { |
122 | 0 | self.set.insert(value, &self.guard) |
123 | 0 | } |
124 | | |
125 | | /// Removes a value from the set. |
126 | | /// |
127 | | /// See also [`HashSet::remove`]. |
128 | 0 | pub fn remove<Q>(&self, value: &Q) -> bool |
129 | 0 | where |
130 | 0 | T: Borrow<Q>, |
131 | 0 | Q: ?Sized + Hash + Ord, |
132 | | { |
133 | 0 | self.set.remove(value, &self.guard) |
134 | 0 | } |
135 | | |
136 | | /// Removes and returns the value in the set, if any, that is equal to the given one. |
137 | | /// |
138 | | /// See also [`HashSet::take`]. |
139 | 0 | pub fn take<'g, Q>(&'g self, value: &Q) -> Option<&'g T> |
140 | 0 | where |
141 | 0 | T: Borrow<Q>, |
142 | 0 | Q: ?Sized + Hash + Ord, |
143 | | { |
144 | 0 | self.set.take(value, &self.guard) |
145 | 0 | } |
146 | | |
147 | | /// Retains only the elements specified by the predicate. |
148 | | /// |
149 | | /// See also [`HashSet::retain`]. |
150 | 0 | pub fn retain<F>(&self, f: F) |
151 | 0 | where |
152 | 0 | F: FnMut(&T) -> bool, |
153 | | { |
154 | 0 | self.set.retain(f, &self.guard); |
155 | 0 | } |
156 | | } |
157 | | |
158 | | impl<T, S> HashSetRef<'_, T, S> |
159 | | where |
160 | | T: Clone + Ord, |
161 | | { |
162 | | /// Clears the set, removing all elements. |
163 | | /// |
164 | | /// See also [`HashSet::clear`]. |
165 | 0 | pub fn clear(&self) { |
166 | 0 | self.set.clear(&self.guard); |
167 | 0 | } |
168 | | |
169 | | /// Tries to reserve capacity for at least `additional` more elements to |
170 | | /// be inserted into the underlying `HashSet`. |
171 | | /// |
172 | | /// See also [`HashSet::reserve`]. |
173 | 0 | pub fn reserve(&self, additional: usize) { |
174 | 0 | self.set.reserve(additional, &self.guard) |
175 | 0 | } |
176 | | } |
177 | | |
178 | | impl<'g, T, S> IntoIterator for &'g HashSetRef<'_, T, S> { |
179 | | type IntoIter = Keys<'g, T, ()>; |
180 | | type Item = &'g T; |
181 | | |
182 | 0 | fn into_iter(self) -> Self::IntoIter { |
183 | 0 | self.set.iter(&self.guard) |
184 | 0 | } |
185 | | } |
186 | | |
187 | | impl<T, S> Debug for HashSetRef<'_, T, S> |
188 | | where |
189 | | T: Debug, |
190 | | { |
191 | 0 | fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { |
192 | 0 | f.debug_set().entries(self).finish() |
193 | 0 | } |
194 | | } |
195 | | |
196 | | impl<T, S> Clone for HashSetRef<'_, T, S> { |
197 | 0 | fn clone(&self) -> Self { |
198 | 0 | self.set.pin() |
199 | 0 | } |
200 | | } |
201 | | |
202 | | impl<T, S> PartialEq for HashSetRef<'_, T, S> |
203 | | where |
204 | | T: Hash + Ord, |
205 | | S: BuildHasher, |
206 | | { |
207 | 0 | fn eq(&self, other: &Self) -> bool { |
208 | 0 | self.set == other.set |
209 | 0 | } |
210 | | } |
211 | | |
212 | | impl<T, S> PartialEq<HashSet<T, S>> for HashSetRef<'_, T, S> |
213 | | where |
214 | | T: Hash + Ord, |
215 | | S: BuildHasher, |
216 | | { |
217 | 0 | fn eq(&self, other: &HashSet<T, S>) -> bool { |
218 | 0 | self.set.guarded_eq(other, &self.guard, &other.guard()) |
219 | 0 | } |
220 | | } |
221 | | |
222 | | impl<T, S> PartialEq<HashSetRef<'_, T, S>> for HashSet<T, S> |
223 | | where |
224 | | T: Hash + Ord, |
225 | | S: BuildHasher, |
226 | | { |
227 | 0 | fn eq(&self, other: &HashSetRef<'_, T, S>) -> bool { |
228 | 0 | self.guarded_eq(other.set, &self.guard(), &other.guard) |
229 | 0 | } |
230 | | } |
231 | | |
232 | | impl<T, S> Eq for HashSetRef<'_, T, S> |
233 | | where |
234 | | T: Hash + Ord, |
235 | | S: BuildHasher, |
236 | | { |
237 | | } |