]>
Commit | Line | Data |
---|---|---|
1b1a35ee XL |
1 | #[cfg(test)] |
2 | mod tests; | |
3 | ||
4 | use hashbrown::hash_set as base; | |
5 | ||
532ac7d7 | 6 | use crate::borrow::Borrow; |
e1599b0c | 7 | use crate::collections::TryReserveError; |
532ac7d7 | 8 | use crate::fmt; |
dfeec247 | 9 | use crate::hash::{BuildHasher, Hash}; |
532ac7d7 | 10 | use crate::iter::{Chain, FromIterator, FusedIterator}; |
dfeec247 | 11 | use crate::ops::{BitAnd, BitOr, BitXor, Sub}; |
1a4d82fc | 12 | |
1b1a35ee | 13 | use super::map::{map_try_reserve_error, RandomState}; |
1a4d82fc JJ |
14 | |
15 | // Future Optimization (FIXME!) | |
532ac7d7 | 16 | // ============================ |
1a4d82fc JJ |
17 | // |
18 | // Iteration over zero sized values is a noop. There is no need | |
19 | // for `bucket.val` in the case of HashSet. I suppose we would need HKT | |
20 | // to get rid of it properly. | |
21 | ||
6a06907d | 22 | /// A [hash set] implemented as a `HashMap` where the value is `()`. |
d9579d0f | 23 | /// |
cc61c64b XL |
24 | /// As with the [`HashMap`] type, a `HashSet` requires that the elements |
25 | /// implement the [`Eq`] and [`Hash`] traits. This can frequently be achieved by | |
d9579d0f AL |
26 | /// using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself, |
27 | /// it is important that the following property holds: | |
c34b1796 AL |
28 | /// |
29 | /// ```text | |
30 | /// k1 == k2 -> hash(k1) == hash(k2) | |
31 | /// ``` | |
32 | /// | |
33 | /// In other words, if two keys are equal, their hashes must be equal. | |
1a4d82fc | 34 | /// |
c34b1796 AL |
35 | /// |
36 | /// It is a logic error for an item to be modified in such a way that the | |
cc61c64b XL |
37 | /// item's hash, as determined by the [`Hash`] trait, or its equality, as |
38 | /// determined by the [`Eq`] trait, changes while it is in the set. This is | |
39 | /// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or | |
5869c6ff XL |
40 | /// unsafe code. The behavior resulting from such a logic error is not |
41 | /// specified, but will not result in undefined behavior. This could include | |
42 | /// panics, incorrect results, aborts, memory leaks, and non-termination. | |
c34b1796 AL |
43 | /// |
44 | /// # Examples | |
1a4d82fc JJ |
45 | /// |
46 | /// ``` | |
47 | /// use std::collections::HashSet; | |
48 | /// // Type inference lets us omit an explicit type signature (which | |
94b46f34 | 49 | /// // would be `HashSet<String>` in this example). |
1a4d82fc JJ |
50 | /// let mut books = HashSet::new(); |
51 | /// | |
52 | /// // Add some books. | |
94b46f34 XL |
53 | /// books.insert("A Dance With Dragons".to_string()); |
54 | /// books.insert("To Kill a Mockingbird".to_string()); | |
55 | /// books.insert("The Odyssey".to_string()); | |
56 | /// books.insert("The Great Gatsby".to_string()); | |
1a4d82fc JJ |
57 | /// |
58 | /// // Check for a specific one. | |
d9579d0f | 59 | /// if !books.contains("The Winds of Winter") { |
1a4d82fc JJ |
60 | /// println!("We have {} books, but The Winds of Winter ain't one.", |
61 | /// books.len()); | |
62 | /// } | |
63 | /// | |
64 | /// // Remove a book. | |
d9579d0f | 65 | /// books.remove("The Odyssey"); |
1a4d82fc JJ |
66 | /// |
67 | /// // Iterate over everything. | |
d9579d0f AL |
68 | /// for book in &books { |
69 | /// println!("{}", book); | |
1a4d82fc JJ |
70 | /// } |
71 | /// ``` | |
72 | /// | |
73 | /// The easiest way to use `HashSet` with a custom type is to derive | |
cc61c64b XL |
74 | /// [`Eq`] and [`Hash`]. We must also derive [`PartialEq`], this will in the |
75 | /// future be implied by [`Eq`]. | |
1a4d82fc JJ |
76 | /// |
77 | /// ``` | |
78 | /// use std::collections::HashSet; | |
85aaf69f | 79 | /// #[derive(Hash, Eq, PartialEq, Debug)] |
94b46f34 XL |
80 | /// struct Viking { |
81 | /// name: String, | |
85aaf69f | 82 | /// power: usize, |
1a4d82fc JJ |
83 | /// } |
84 | /// | |
85 | /// let mut vikings = HashSet::new(); | |
86 | /// | |
94b46f34 XL |
87 | /// vikings.insert(Viking { name: "Einar".to_string(), power: 9 }); |
88 | /// vikings.insert(Viking { name: "Einar".to_string(), power: 9 }); | |
89 | /// vikings.insert(Viking { name: "Olaf".to_string(), power: 4 }); | |
90 | /// vikings.insert(Viking { name: "Harald".to_string(), power: 8 }); | |
1a4d82fc JJ |
91 | /// |
92 | /// // Use derived implementation to print the vikings. | |
d9579d0f | 93 | /// for x in &vikings { |
1a4d82fc JJ |
94 | /// println!("{:?}", x); |
95 | /// } | |
96 | /// ``` | |
c30ab7b3 | 97 | /// |
94222f64 | 98 | /// A `HashSet` with a known list of items can be initialized from an array: |
c30ab7b3 SL |
99 | /// |
100 | /// ``` | |
101 | /// use std::collections::HashSet; | |
102 | /// | |
94222f64 | 103 | /// let viking_names = HashSet::from(["Einar", "Olaf", "Harald"]); |
c30ab7b3 | 104 | /// ``` |
cc61c64b | 105 | /// |
6a06907d | 106 | /// [hash set]: crate::collections#use-the-set-variant-of-any-of-these-maps-when |
1b1a35ee | 107 | /// [`HashMap`]: crate::collections::HashMap |
3dfed10e XL |
108 | /// [`RefCell`]: crate::cell::RefCell |
109 | /// [`Cell`]: crate::cell::Cell | |
f9f354fc | 110 | #[cfg_attr(not(test), rustc_diagnostic_item = "hashset_type")] |
85aaf69f | 111 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 112 | pub struct HashSet<T, S = RandomState> { |
1b1a35ee | 113 | base: base::HashSet<T, S>, |
1a4d82fc JJ |
114 | } |
115 | ||
74b04a01 | 116 | impl<T> HashSet<T, RandomState> { |
3b2f2976 | 117 | /// Creates an empty `HashSet`. |
1a4d82fc | 118 | /// |
ea8adc8c XL |
119 | /// The hash set is initially created with a capacity of 0, so it will not allocate until it |
120 | /// is first inserted into. | |
121 | /// | |
c34b1796 | 122 | /// # Examples |
1a4d82fc JJ |
123 | /// |
124 | /// ``` | |
125 | /// use std::collections::HashSet; | |
3b2f2976 | 126 | /// let set: HashSet<i32> = HashSet::new(); |
1a4d82fc JJ |
127 | /// ``` |
128 | #[inline] | |
85aaf69f | 129 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 130 | pub fn new() -> HashSet<T, RandomState> { |
1b1a35ee | 131 | Default::default() |
1a4d82fc JJ |
132 | } |
133 | ||
c30ab7b3 SL |
134 | /// Creates an empty `HashSet` with the specified capacity. |
135 | /// | |
136 | /// The hash set will be able to hold at least `capacity` elements without | |
137 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
1a4d82fc | 138 | /// |
c34b1796 | 139 | /// # Examples |
1a4d82fc JJ |
140 | /// |
141 | /// ``` | |
142 | /// use std::collections::HashSet; | |
3b2f2976 XL |
143 | /// let set: HashSet<i32> = HashSet::with_capacity(10); |
144 | /// assert!(set.capacity() >= 10); | |
1a4d82fc JJ |
145 | /// ``` |
146 | #[inline] | |
85aaf69f SL |
147 | #[stable(feature = "rust1", since = "1.0.0")] |
148 | pub fn with_capacity(capacity: usize) -> HashSet<T, RandomState> { | |
1b1a35ee | 149 | HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, Default::default()) } |
1a4d82fc JJ |
150 | } |
151 | } | |
152 | ||
9fa01778 XL |
153 | impl<T, S> HashSet<T, S> { |
154 | /// Returns the number of elements the set can hold without reallocating. | |
155 | /// | |
156 | /// # Examples | |
157 | /// | |
158 | /// ``` | |
159 | /// use std::collections::HashSet; | |
160 | /// let set: HashSet<i32> = HashSet::with_capacity(100); | |
161 | /// assert!(set.capacity() >= 100); | |
162 | /// ``` | |
163 | #[inline] | |
164 | #[stable(feature = "rust1", since = "1.0.0")] | |
165 | pub fn capacity(&self) -> usize { | |
1b1a35ee | 166 | self.base.capacity() |
9fa01778 XL |
167 | } |
168 | ||
169 | /// An iterator visiting all elements in arbitrary order. | |
170 | /// The iterator element type is `&'a T`. | |
171 | /// | |
172 | /// # Examples | |
173 | /// | |
174 | /// ``` | |
175 | /// use std::collections::HashSet; | |
176 | /// let mut set = HashSet::new(); | |
177 | /// set.insert("a"); | |
178 | /// set.insert("b"); | |
179 | /// | |
180 | /// // Will print in an arbitrary order. | |
181 | /// for x in set.iter() { | |
182 | /// println!("{}", x); | |
183 | /// } | |
184 | /// ``` | |
48663c56 | 185 | #[inline] |
9fa01778 | 186 | #[stable(feature = "rust1", since = "1.0.0")] |
532ac7d7 | 187 | pub fn iter(&self) -> Iter<'_, T> { |
1b1a35ee | 188 | Iter { base: self.base.iter() } |
9fa01778 XL |
189 | } |
190 | ||
191 | /// Returns the number of elements in the set. | |
192 | /// | |
193 | /// # Examples | |
194 | /// | |
195 | /// ``` | |
196 | /// use std::collections::HashSet; | |
197 | /// | |
198 | /// let mut v = HashSet::new(); | |
199 | /// assert_eq!(v.len(), 0); | |
200 | /// v.insert(1); | |
201 | /// assert_eq!(v.len(), 1); | |
202 | /// ``` | |
48663c56 | 203 | #[inline] |
9fa01778 XL |
204 | #[stable(feature = "rust1", since = "1.0.0")] |
205 | pub fn len(&self) -> usize { | |
1b1a35ee | 206 | self.base.len() |
9fa01778 XL |
207 | } |
208 | ||
209 | /// Returns `true` if the set contains no elements. | |
210 | /// | |
211 | /// # Examples | |
212 | /// | |
213 | /// ``` | |
214 | /// use std::collections::HashSet; | |
215 | /// | |
216 | /// let mut v = HashSet::new(); | |
217 | /// assert!(v.is_empty()); | |
218 | /// v.insert(1); | |
219 | /// assert!(!v.is_empty()); | |
220 | /// ``` | |
48663c56 | 221 | #[inline] |
9fa01778 XL |
222 | #[stable(feature = "rust1", since = "1.0.0")] |
223 | pub fn is_empty(&self) -> bool { | |
1b1a35ee | 224 | self.base.is_empty() |
9fa01778 XL |
225 | } |
226 | ||
227 | /// Clears the set, returning all elements in an iterator. | |
228 | /// | |
229 | /// # Examples | |
230 | /// | |
231 | /// ``` | |
232 | /// use std::collections::HashSet; | |
233 | /// | |
234 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
235 | /// assert!(!set.is_empty()); | |
236 | /// | |
237 | /// // print 1, 2, 3 in an arbitrary order | |
238 | /// for i in set.drain() { | |
239 | /// println!("{}", i); | |
240 | /// } | |
241 | /// | |
242 | /// assert!(set.is_empty()); | |
243 | /// ``` | |
244 | #[inline] | |
245 | #[stable(feature = "drain", since = "1.6.0")] | |
532ac7d7 | 246 | pub fn drain(&mut self) -> Drain<'_, T> { |
1b1a35ee XL |
247 | Drain { base: self.base.drain() } |
248 | } | |
249 | ||
250 | /// Creates an iterator which uses a closure to determine if a value should be removed. | |
251 | /// | |
252 | /// If the closure returns true, then the value is removed and yielded. | |
253 | /// If the closure returns false, the value will remain in the list and will not be yielded | |
254 | /// by the iterator. | |
255 | /// | |
256 | /// If the iterator is only partially consumed or not consumed at all, each of the remaining | |
257 | /// values will still be subjected to the closure and removed and dropped if it returns true. | |
258 | /// | |
259 | /// It is unspecified how many more values will be subjected to the closure | |
260 | /// if a panic occurs in the closure, or if a panic occurs while dropping a value, or if the | |
261 | /// `DrainFilter` itself is leaked. | |
262 | /// | |
263 | /// # Examples | |
264 | /// | |
265 | /// Splitting a set into even and odd values, reusing the original set: | |
266 | /// | |
267 | /// ``` | |
268 | /// #![feature(hash_drain_filter)] | |
269 | /// use std::collections::HashSet; | |
270 | /// | |
271 | /// let mut set: HashSet<i32> = (0..8).collect(); | |
272 | /// let drained: HashSet<i32> = set.drain_filter(|v| v % 2 == 0).collect(); | |
273 | /// | |
274 | /// let mut evens = drained.into_iter().collect::<Vec<_>>(); | |
275 | /// let mut odds = set.into_iter().collect::<Vec<_>>(); | |
276 | /// evens.sort(); | |
277 | /// odds.sort(); | |
278 | /// | |
279 | /// assert_eq!(evens, vec![0, 2, 4, 6]); | |
280 | /// assert_eq!(odds, vec![1, 3, 5, 7]); | |
281 | /// ``` | |
282 | #[inline] | |
283 | #[unstable(feature = "hash_drain_filter", issue = "59618")] | |
284 | pub fn drain_filter<F>(&mut self, pred: F) -> DrainFilter<'_, T, F> | |
285 | where | |
286 | F: FnMut(&T) -> bool, | |
287 | { | |
288 | DrainFilter { base: self.base.drain_filter(pred) } | |
9fa01778 XL |
289 | } |
290 | ||
291 | /// Clears the set, removing all values. | |
292 | /// | |
293 | /// # Examples | |
294 | /// | |
295 | /// ``` | |
296 | /// use std::collections::HashSet; | |
297 | /// | |
298 | /// let mut v = HashSet::new(); | |
299 | /// v.insert(1); | |
300 | /// v.clear(); | |
301 | /// assert!(v.is_empty()); | |
302 | /// ``` | |
48663c56 | 303 | #[inline] |
9fa01778 XL |
304 | #[stable(feature = "rust1", since = "1.0.0")] |
305 | pub fn clear(&mut self) { | |
1b1a35ee | 306 | self.base.clear() |
9fa01778 | 307 | } |
9fa01778 | 308 | |
1a4d82fc JJ |
309 | /// Creates a new empty hash set which will use the given hasher to hash |
310 | /// keys. | |
311 | /// | |
312 | /// The hash set is also created with the default initial capacity. | |
313 | /// | |
9cc50fc6 SL |
314 | /// Warning: `hasher` is normally randomly generated, and |
315 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
316 | /// cause many collisions and very poor performance. Setting it | |
317 | /// manually using this function can expose a DoS attack vector. | |
318 | /// | |
f9f354fc XL |
319 | /// The `hash_builder` passed should implement the [`BuildHasher`] trait for |
320 | /// the HashMap to be useful, see its documentation for details. | |
321 | /// | |
c34b1796 | 322 | /// # Examples |
1a4d82fc JJ |
323 | /// |
324 | /// ``` | |
325 | /// use std::collections::HashSet; | |
326 | /// use std::collections::hash_map::RandomState; | |
327 | /// | |
328 | /// let s = RandomState::new(); | |
9cc50fc6 | 329 | /// let mut set = HashSet::with_hasher(s); |
85aaf69f | 330 | /// set.insert(2); |
1a4d82fc JJ |
331 | /// ``` |
332 | #[inline] | |
9cc50fc6 SL |
333 | #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] |
334 | pub fn with_hasher(hasher: S) -> HashSet<T, S> { | |
1b1a35ee | 335 | HashSet { base: base::HashSet::with_hasher(hasher) } |
1a4d82fc JJ |
336 | } |
337 | ||
0731742a | 338 | /// Creates an empty `HashSet` with the specified capacity, using |
c30ab7b3 SL |
339 | /// `hasher` to hash the keys. |
340 | /// | |
341 | /// The hash set will be able to hold at least `capacity` elements without | |
342 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
1a4d82fc JJ |
343 | /// |
344 | /// Warning: `hasher` is normally randomly generated, and | |
345 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
346 | /// cause many collisions and very poor performance. Setting it | |
347 | /// manually using this function can expose a DoS attack vector. | |
348 | /// | |
f9f354fc XL |
349 | /// The `hash_builder` passed should implement the [`BuildHasher`] trait for |
350 | /// the HashMap to be useful, see its documentation for details. | |
351 | /// | |
c34b1796 | 352 | /// # Examples |
1a4d82fc JJ |
353 | /// |
354 | /// ``` | |
355 | /// use std::collections::HashSet; | |
356 | /// use std::collections::hash_map::RandomState; | |
357 | /// | |
358 | /// let s = RandomState::new(); | |
9cc50fc6 | 359 | /// let mut set = HashSet::with_capacity_and_hasher(10, s); |
85aaf69f | 360 | /// set.insert(1); |
1a4d82fc JJ |
361 | /// ``` |
362 | #[inline] | |
9cc50fc6 | 363 | #[stable(feature = "hashmap_build_hasher", since = "1.7.0")] |
c30ab7b3 | 364 | pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> HashSet<T, S> { |
1b1a35ee | 365 | HashSet { base: base::HashSet::with_capacity_and_hasher(capacity, hasher) } |
9cc50fc6 SL |
366 | } |
367 | ||
cc61c64b XL |
368 | /// Returns a reference to the set's [`BuildHasher`]. |
369 | /// | |
3b2f2976 XL |
370 | /// # Examples |
371 | /// | |
372 | /// ``` | |
373 | /// use std::collections::HashSet; | |
374 | /// use std::collections::hash_map::RandomState; | |
375 | /// | |
376 | /// let hasher = RandomState::new(); | |
377 | /// let set: HashSet<i32> = HashSet::with_hasher(hasher); | |
378 | /// let hasher: &RandomState = set.hasher(); | |
379 | /// ``` | |
48663c56 | 380 | #[inline] |
54a0048b | 381 | #[stable(feature = "hashmap_public_hasher", since = "1.9.0")] |
7453a54e | 382 | pub fn hasher(&self) -> &S { |
1b1a35ee | 383 | self.base.hasher() |
7453a54e | 384 | } |
74b04a01 | 385 | } |
7453a54e | 386 | |
74b04a01 XL |
387 | impl<T, S> HashSet<T, S> |
388 | where | |
389 | T: Eq + Hash, | |
390 | S: BuildHasher, | |
391 | { | |
1a4d82fc JJ |
392 | /// Reserves capacity for at least `additional` more elements to be inserted |
393 | /// in the `HashSet`. The collection may reserve more space to avoid | |
394 | /// frequent reallocations. | |
395 | /// | |
396 | /// # Panics | |
397 | /// | |
85aaf69f | 398 | /// Panics if the new allocation size overflows `usize`. |
1a4d82fc | 399 | /// |
c34b1796 | 400 | /// # Examples |
1a4d82fc JJ |
401 | /// |
402 | /// ``` | |
403 | /// use std::collections::HashSet; | |
85aaf69f | 404 | /// let mut set: HashSet<i32> = HashSet::new(); |
1a4d82fc | 405 | /// set.reserve(10); |
3b2f2976 | 406 | /// assert!(set.capacity() >= 10); |
1a4d82fc | 407 | /// ``` |
48663c56 | 408 | #[inline] |
85aaf69f SL |
409 | #[stable(feature = "rust1", since = "1.0.0")] |
410 | pub fn reserve(&mut self, additional: usize) { | |
1b1a35ee | 411 | self.base.reserve(additional) |
1a4d82fc JJ |
412 | } |
413 | ||
48663c56 | 414 | /// Tries to reserve capacity for at least `additional` more elements to be inserted |
29967ef6 | 415 | /// in the given `HashSet<K, V>`. The collection may reserve more space to avoid |
48663c56 XL |
416 | /// frequent reallocations. |
417 | /// | |
418 | /// # Errors | |
419 | /// | |
420 | /// If the capacity overflows, or the allocator reports a failure, then an error | |
421 | /// is returned. | |
422 | /// | |
423 | /// # Examples | |
424 | /// | |
425 | /// ``` | |
426 | /// #![feature(try_reserve)] | |
427 | /// use std::collections::HashSet; | |
428 | /// let mut set: HashSet<i32> = HashSet::new(); | |
429 | /// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?"); | |
430 | /// ``` | |
431 | #[inline] | |
dfeec247 | 432 | #[unstable(feature = "try_reserve", reason = "new API", issue = "48043")] |
e1599b0c | 433 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { |
1b1a35ee | 434 | self.base.try_reserve(additional).map_err(map_try_reserve_error) |
48663c56 XL |
435 | } |
436 | ||
1a4d82fc JJ |
437 | /// Shrinks the capacity of the set as much as possible. It will drop |
438 | /// down as much as possible while maintaining the internal rules | |
439 | /// and possibly leaving some space in accordance with the resize policy. | |
440 | /// | |
c34b1796 | 441 | /// # Examples |
1a4d82fc JJ |
442 | /// |
443 | /// ``` | |
444 | /// use std::collections::HashSet; | |
445 | /// | |
85aaf69f | 446 | /// let mut set = HashSet::with_capacity(100); |
1a4d82fc JJ |
447 | /// set.insert(1); |
448 | /// set.insert(2); | |
449 | /// assert!(set.capacity() >= 100); | |
450 | /// set.shrink_to_fit(); | |
451 | /// assert!(set.capacity() >= 2); | |
452 | /// ``` | |
48663c56 | 453 | #[inline] |
85aaf69f | 454 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 455 | pub fn shrink_to_fit(&mut self) { |
1b1a35ee | 456 | self.base.shrink_to_fit() |
1a4d82fc JJ |
457 | } |
458 | ||
0531ce1d XL |
459 | /// Shrinks the capacity of the set with a lower limit. It will drop |
460 | /// down no lower than the supplied limit while maintaining the internal rules | |
461 | /// and possibly leaving some space in accordance with the resize policy. | |
462 | /// | |
5869c6ff | 463 | /// If the current capacity is less than the lower limit, this is a no-op. |
0531ce1d XL |
464 | /// # Examples |
465 | /// | |
466 | /// ``` | |
0531ce1d XL |
467 | /// use std::collections::HashSet; |
468 | /// | |
469 | /// let mut set = HashSet::with_capacity(100); | |
470 | /// set.insert(1); | |
471 | /// set.insert(2); | |
472 | /// assert!(set.capacity() >= 100); | |
473 | /// set.shrink_to(10); | |
474 | /// assert!(set.capacity() >= 10); | |
475 | /// set.shrink_to(0); | |
476 | /// assert!(set.capacity() >= 2); | |
477 | /// ``` | |
478 | #[inline] | |
94222f64 | 479 | #[stable(feature = "shrink_to", since = "1.56.0")] |
0531ce1d | 480 | pub fn shrink_to(&mut self, min_capacity: usize) { |
1b1a35ee | 481 | self.base.shrink_to(min_capacity) |
0531ce1d XL |
482 | } |
483 | ||
cc61c64b | 484 | /// Visits the values representing the difference, |
0731742a | 485 | /// i.e., the values that are in `self` but not in `other`. |
1a4d82fc | 486 | /// |
c34b1796 | 487 | /// # Examples |
1a4d82fc JJ |
488 | /// |
489 | /// ``` | |
490 | /// use std::collections::HashSet; | |
85aaf69f SL |
491 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
492 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1a4d82fc JJ |
493 | /// |
494 | /// // Can be seen as `a - b`. | |
495 | /// for x in a.difference(&b) { | |
496 | /// println!("{}", x); // Print 1 | |
497 | /// } | |
498 | /// | |
3b2f2976 XL |
499 | /// let diff: HashSet<_> = a.difference(&b).collect(); |
500 | /// assert_eq!(diff, [1].iter().collect()); | |
1a4d82fc JJ |
501 | /// |
502 | /// // Note that difference is not symmetric, | |
503 | /// // and `b - a` means something else: | |
3b2f2976 XL |
504 | /// let diff: HashSet<_> = b.difference(&a).collect(); |
505 | /// assert_eq!(diff, [4].iter().collect()); | |
1a4d82fc | 506 | /// ``` |
48663c56 | 507 | #[inline] |
85aaf69f | 508 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 509 | pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> { |
dfeec247 | 510 | Difference { iter: self.iter(), other } |
1a4d82fc JJ |
511 | } |
512 | ||
cc61c64b | 513 | /// Visits the values representing the symmetric difference, |
0731742a | 514 | /// i.e., the values that are in `self` or in `other` but not in both. |
1a4d82fc | 515 | /// |
c34b1796 | 516 | /// # Examples |
1a4d82fc JJ |
517 | /// |
518 | /// ``` | |
519 | /// use std::collections::HashSet; | |
85aaf69f SL |
520 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
521 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1a4d82fc JJ |
522 | /// |
523 | /// // Print 1, 4 in arbitrary order. | |
524 | /// for x in a.symmetric_difference(&b) { | |
525 | /// println!("{}", x); | |
526 | /// } | |
527 | /// | |
3b2f2976 XL |
528 | /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect(); |
529 | /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect(); | |
1a4d82fc JJ |
530 | /// |
531 | /// assert_eq!(diff1, diff2); | |
3b2f2976 | 532 | /// assert_eq!(diff1, [1, 4].iter().collect()); |
1a4d82fc | 533 | /// ``` |
48663c56 | 534 | #[inline] |
85aaf69f | 535 | #[stable(feature = "rust1", since = "1.0.0")] |
dfeec247 XL |
536 | pub fn symmetric_difference<'a>( |
537 | &'a self, | |
538 | other: &'a HashSet<T, S>, | |
539 | ) -> SymmetricDifference<'a, T, S> { | |
1a4d82fc JJ |
540 | SymmetricDifference { iter: self.difference(other).chain(other.difference(self)) } |
541 | } | |
542 | ||
cc61c64b | 543 | /// Visits the values representing the intersection, |
0731742a | 544 | /// i.e., the values that are both in `self` and `other`. |
1a4d82fc | 545 | /// |
c34b1796 | 546 | /// # Examples |
1a4d82fc JJ |
547 | /// |
548 | /// ``` | |
549 | /// use std::collections::HashSet; | |
85aaf69f SL |
550 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
551 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1a4d82fc JJ |
552 | /// |
553 | /// // Print 2, 3 in arbitrary order. | |
554 | /// for x in a.intersection(&b) { | |
555 | /// println!("{}", x); | |
556 | /// } | |
557 | /// | |
3b2f2976 XL |
558 | /// let intersection: HashSet<_> = a.intersection(&b).collect(); |
559 | /// assert_eq!(intersection, [2, 3].iter().collect()); | |
1a4d82fc | 560 | /// ``` |
48663c56 | 561 | #[inline] |
85aaf69f | 562 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 563 | pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S> { |
0731742a | 564 | if self.len() <= other.len() { |
dfeec247 | 565 | Intersection { iter: self.iter(), other } |
0731742a | 566 | } else { |
dfeec247 | 567 | Intersection { iter: other.iter(), other: self } |
1a4d82fc JJ |
568 | } |
569 | } | |
570 | ||
cc61c64b | 571 | /// Visits the values representing the union, |
0731742a | 572 | /// i.e., all the values in `self` or `other`, without duplicates. |
1a4d82fc | 573 | /// |
c34b1796 | 574 | /// # Examples |
1a4d82fc JJ |
575 | /// |
576 | /// ``` | |
577 | /// use std::collections::HashSet; | |
85aaf69f SL |
578 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
579 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1a4d82fc JJ |
580 | /// |
581 | /// // Print 1, 2, 3, 4 in arbitrary order. | |
582 | /// for x in a.union(&b) { | |
583 | /// println!("{}", x); | |
584 | /// } | |
585 | /// | |
3b2f2976 XL |
586 | /// let union: HashSet<_> = a.union(&b).collect(); |
587 | /// assert_eq!(union, [1, 2, 3, 4].iter().collect()); | |
1a4d82fc | 588 | /// ``` |
48663c56 | 589 | #[inline] |
85aaf69f | 590 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 591 | pub fn union<'a>(&'a self, other: &'a HashSet<T, S>) -> Union<'a, T, S> { |
60c5eb7d | 592 | if self.len() >= other.len() { |
dfeec247 | 593 | Union { iter: self.iter().chain(other.difference(self)) } |
0731742a | 594 | } else { |
dfeec247 | 595 | Union { iter: other.iter().chain(self.difference(other)) } |
0731742a | 596 | } |
1a4d82fc JJ |
597 | } |
598 | ||
1a4d82fc JJ |
599 | /// Returns `true` if the set contains a value. |
600 | /// | |
601 | /// The value may be any borrowed form of the set's value type, but | |
cc61c64b | 602 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for |
1a4d82fc JJ |
603 | /// the value type. |
604 | /// | |
c34b1796 | 605 | /// # Examples |
1a4d82fc JJ |
606 | /// |
607 | /// ``` | |
608 | /// use std::collections::HashSet; | |
609 | /// | |
85aaf69f | 610 | /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
1a4d82fc JJ |
611 | /// assert_eq!(set.contains(&1), true); |
612 | /// assert_eq!(set.contains(&4), false); | |
613 | /// ``` | |
48663c56 | 614 | #[inline] |
85aaf69f | 615 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 616 | pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool |
dfeec247 XL |
617 | where |
618 | T: Borrow<Q>, | |
619 | Q: Hash + Eq, | |
1a4d82fc | 620 | { |
1b1a35ee | 621 | self.base.contains(value) |
1a4d82fc JJ |
622 | } |
623 | ||
e9174d1e SL |
624 | /// Returns a reference to the value in the set, if any, that is equal to the given value. |
625 | /// | |
626 | /// The value may be any borrowed form of the set's value type, but | |
cc61c64b | 627 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for |
e9174d1e | 628 | /// the value type. |
cc61c64b | 629 | /// |
2c00a5a8 XL |
630 | /// # Examples |
631 | /// | |
632 | /// ``` | |
633 | /// use std::collections::HashSet; | |
634 | /// | |
635 | /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
636 | /// assert_eq!(set.get(&2), Some(&2)); | |
637 | /// assert_eq!(set.get(&4), None); | |
638 | /// ``` | |
48663c56 | 639 | #[inline] |
54a0048b | 640 | #[stable(feature = "set_recovery", since = "1.9.0")] |
e9174d1e | 641 | pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> |
dfeec247 XL |
642 | where |
643 | T: Borrow<Q>, | |
644 | Q: Hash + Eq, | |
e9174d1e | 645 | { |
1b1a35ee | 646 | self.base.get(value) |
48663c56 XL |
647 | } |
648 | ||
649 | /// Inserts the given `value` into the set if it is not present, then | |
650 | /// returns a reference to the value in the set. | |
651 | /// | |
652 | /// # Examples | |
653 | /// | |
654 | /// ``` | |
655 | /// #![feature(hash_set_entry)] | |
656 | /// | |
657 | /// use std::collections::HashSet; | |
658 | /// | |
659 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
660 | /// assert_eq!(set.len(), 3); | |
661 | /// assert_eq!(set.get_or_insert(2), &2); | |
662 | /// assert_eq!(set.get_or_insert(100), &100); | |
663 | /// assert_eq!(set.len(), 4); // 100 was inserted | |
664 | /// ``` | |
665 | #[inline] | |
666 | #[unstable(feature = "hash_set_entry", issue = "60896")] | |
667 | pub fn get_or_insert(&mut self, value: T) -> &T { | |
668 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
669 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
1b1a35ee | 670 | self.base.get_or_insert(value) |
48663c56 XL |
671 | } |
672 | ||
dfeec247 XL |
673 | /// Inserts an owned copy of the given `value` into the set if it is not |
674 | /// present, then returns a reference to the value in the set. | |
675 | /// | |
676 | /// # Examples | |
677 | /// | |
678 | /// ``` | |
679 | /// #![feature(hash_set_entry)] | |
680 | /// | |
681 | /// use std::collections::HashSet; | |
682 | /// | |
683 | /// let mut set: HashSet<String> = ["cat", "dog", "horse"] | |
684 | /// .iter().map(|&pet| pet.to_owned()).collect(); | |
685 | /// | |
686 | /// assert_eq!(set.len(), 3); | |
687 | /// for &pet in &["cat", "dog", "fish"] { | |
688 | /// let value = set.get_or_insert_owned(pet); | |
689 | /// assert_eq!(value, pet); | |
690 | /// } | |
691 | /// assert_eq!(set.len(), 4); // a new "fish" was inserted | |
692 | /// ``` | |
693 | #[inline] | |
694 | #[unstable(feature = "hash_set_entry", issue = "60896")] | |
695 | pub fn get_or_insert_owned<Q: ?Sized>(&mut self, value: &Q) -> &T | |
696 | where | |
697 | T: Borrow<Q>, | |
698 | Q: Hash + Eq + ToOwned<Owned = T>, | |
699 | { | |
700 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
701 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
1b1a35ee | 702 | self.base.get_or_insert_owned(value) |
dfeec247 XL |
703 | } |
704 | ||
48663c56 XL |
705 | /// Inserts a value computed from `f` into the set if the given `value` is |
706 | /// not present, then returns a reference to the value in the set. | |
707 | /// | |
708 | /// # Examples | |
709 | /// | |
710 | /// ``` | |
711 | /// #![feature(hash_set_entry)] | |
712 | /// | |
713 | /// use std::collections::HashSet; | |
714 | /// | |
715 | /// let mut set: HashSet<String> = ["cat", "dog", "horse"] | |
716 | /// .iter().map(|&pet| pet.to_owned()).collect(); | |
717 | /// | |
718 | /// assert_eq!(set.len(), 3); | |
719 | /// for &pet in &["cat", "dog", "fish"] { | |
720 | /// let value = set.get_or_insert_with(pet, str::to_owned); | |
721 | /// assert_eq!(value, pet); | |
722 | /// } | |
723 | /// assert_eq!(set.len(), 4); // a new "fish" was inserted | |
724 | /// ``` | |
725 | #[inline] | |
726 | #[unstable(feature = "hash_set_entry", issue = "60896")] | |
727 | pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T | |
dfeec247 XL |
728 | where |
729 | T: Borrow<Q>, | |
730 | Q: Hash + Eq, | |
731 | F: FnOnce(&Q) -> T, | |
48663c56 XL |
732 | { |
733 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
734 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
1b1a35ee | 735 | self.base.get_or_insert_with(value, f) |
e9174d1e SL |
736 | } |
737 | ||
8bb4bdeb | 738 | /// Returns `true` if `self` has no elements in common with `other`. |
1a4d82fc JJ |
739 | /// This is equivalent to checking for an empty intersection. |
740 | /// | |
c34b1796 | 741 | /// # Examples |
1a4d82fc JJ |
742 | /// |
743 | /// ``` | |
744 | /// use std::collections::HashSet; | |
745 | /// | |
85aaf69f SL |
746 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
747 | /// let mut b = HashSet::new(); | |
1a4d82fc JJ |
748 | /// |
749 | /// assert_eq!(a.is_disjoint(&b), true); | |
750 | /// b.insert(4); | |
751 | /// assert_eq!(a.is_disjoint(&b), true); | |
752 | /// b.insert(1); | |
753 | /// assert_eq!(a.is_disjoint(&b), false); | |
754 | /// ``` | |
85aaf69f | 755 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 756 | pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool { |
0731742a XL |
757 | if self.len() <= other.len() { |
758 | self.iter().all(|v| !other.contains(v)) | |
759 | } else { | |
760 | other.iter().all(|v| !self.contains(v)) | |
761 | } | |
1a4d82fc JJ |
762 | } |
763 | ||
8bb4bdeb | 764 | /// Returns `true` if the set is a subset of another, |
0731742a | 765 | /// i.e., `other` contains at least all the values in `self`. |
1a4d82fc | 766 | /// |
c34b1796 | 767 | /// # Examples |
1a4d82fc JJ |
768 | /// |
769 | /// ``` | |
770 | /// use std::collections::HashSet; | |
771 | /// | |
85aaf69f SL |
772 | /// let sup: HashSet<_> = [1, 2, 3].iter().cloned().collect(); |
773 | /// let mut set = HashSet::new(); | |
1a4d82fc JJ |
774 | /// |
775 | /// assert_eq!(set.is_subset(&sup), true); | |
776 | /// set.insert(2); | |
777 | /// assert_eq!(set.is_subset(&sup), true); | |
778 | /// set.insert(4); | |
779 | /// assert_eq!(set.is_subset(&sup), false); | |
780 | /// ``` | |
85aaf69f | 781 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 782 | pub fn is_subset(&self, other: &HashSet<T, S>) -> bool { |
dfeec247 | 783 | if self.len() <= other.len() { self.iter().all(|v| other.contains(v)) } else { false } |
1a4d82fc JJ |
784 | } |
785 | ||
8bb4bdeb | 786 | /// Returns `true` if the set is a superset of another, |
0731742a | 787 | /// i.e., `self` contains at least all the values in `other`. |
1a4d82fc | 788 | /// |
c34b1796 | 789 | /// # Examples |
1a4d82fc JJ |
790 | /// |
791 | /// ``` | |
792 | /// use std::collections::HashSet; | |
793 | /// | |
85aaf69f SL |
794 | /// let sub: HashSet<_> = [1, 2].iter().cloned().collect(); |
795 | /// let mut set = HashSet::new(); | |
1a4d82fc JJ |
796 | /// |
797 | /// assert_eq!(set.is_superset(&sub), false); | |
798 | /// | |
799 | /// set.insert(0); | |
800 | /// set.insert(1); | |
801 | /// assert_eq!(set.is_superset(&sub), false); | |
802 | /// | |
803 | /// set.insert(2); | |
804 | /// assert_eq!(set.is_superset(&sub), true); | |
805 | /// ``` | |
806 | #[inline] | |
85aaf69f | 807 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
808 | pub fn is_superset(&self, other: &HashSet<T, S>) -> bool { |
809 | other.is_subset(self) | |
810 | } | |
811 | ||
b039eaaf SL |
812 | /// Adds a value to the set. |
813 | /// | |
a7813a04 | 814 | /// If the set did not have this value present, `true` is returned. |
b039eaaf | 815 | /// |
a7813a04 | 816 | /// If the set did have this value present, `false` is returned. |
1a4d82fc | 817 | /// |
c34b1796 | 818 | /// # Examples |
1a4d82fc JJ |
819 | /// |
820 | /// ``` | |
821 | /// use std::collections::HashSet; | |
822 | /// | |
823 | /// let mut set = HashSet::new(); | |
824 | /// | |
85aaf69f | 825 | /// assert_eq!(set.insert(2), true); |
1a4d82fc JJ |
826 | /// assert_eq!(set.insert(2), false); |
827 | /// assert_eq!(set.len(), 1); | |
828 | /// ``` | |
48663c56 | 829 | #[inline] |
85aaf69f | 830 | #[stable(feature = "rust1", since = "1.0.0")] |
c30ab7b3 | 831 | pub fn insert(&mut self, value: T) -> bool { |
1b1a35ee | 832 | self.base.insert(value) |
c30ab7b3 | 833 | } |
1a4d82fc | 834 | |
e9174d1e SL |
835 | /// Adds a value to the set, replacing the existing value, if any, that is equal to the given |
836 | /// one. Returns the replaced value. | |
2c00a5a8 XL |
837 | /// |
838 | /// # Examples | |
839 | /// | |
840 | /// ``` | |
841 | /// use std::collections::HashSet; | |
842 | /// | |
843 | /// let mut set = HashSet::new(); | |
844 | /// set.insert(Vec::<i32>::new()); | |
845 | /// | |
846 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0); | |
847 | /// set.replace(Vec::with_capacity(10)); | |
848 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10); | |
849 | /// ``` | |
48663c56 | 850 | #[inline] |
54a0048b | 851 | #[stable(feature = "set_recovery", since = "1.9.0")] |
e9174d1e | 852 | pub fn replace(&mut self, value: T) -> Option<T> { |
1b1a35ee | 853 | self.base.replace(value) |
e9174d1e SL |
854 | } |
855 | ||
9fa01778 | 856 | /// Removes a value from the set. Returns whether the value was |
1a4d82fc JJ |
857 | /// present in the set. |
858 | /// | |
859 | /// The value may be any borrowed form of the set's value type, but | |
cc61c64b | 860 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for |
1a4d82fc JJ |
861 | /// the value type. |
862 | /// | |
c34b1796 | 863 | /// # Examples |
1a4d82fc JJ |
864 | /// |
865 | /// ``` | |
866 | /// use std::collections::HashSet; | |
867 | /// | |
868 | /// let mut set = HashSet::new(); | |
869 | /// | |
85aaf69f | 870 | /// set.insert(2); |
1a4d82fc JJ |
871 | /// assert_eq!(set.remove(&2), true); |
872 | /// assert_eq!(set.remove(&2), false); | |
873 | /// ``` | |
48663c56 | 874 | #[inline] |
85aaf69f | 875 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 876 | pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool |
dfeec247 XL |
877 | where |
878 | T: Borrow<Q>, | |
879 | Q: Hash + Eq, | |
1a4d82fc | 880 | { |
1b1a35ee | 881 | self.base.remove(value) |
1a4d82fc | 882 | } |
e9174d1e SL |
883 | |
884 | /// Removes and returns the value in the set, if any, that is equal to the given one. | |
885 | /// | |
886 | /// The value may be any borrowed form of the set's value type, but | |
cc61c64b | 887 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for |
e9174d1e | 888 | /// the value type. |
cc61c64b | 889 | /// |
2c00a5a8 XL |
890 | /// # Examples |
891 | /// | |
892 | /// ``` | |
893 | /// use std::collections::HashSet; | |
894 | /// | |
895 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
896 | /// assert_eq!(set.take(&2), Some(2)); | |
897 | /// assert_eq!(set.take(&2), None); | |
898 | /// ``` | |
48663c56 | 899 | #[inline] |
54a0048b | 900 | #[stable(feature = "set_recovery", since = "1.9.0")] |
e9174d1e | 901 | pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T> |
dfeec247 XL |
902 | where |
903 | T: Borrow<Q>, | |
904 | Q: Hash + Eq, | |
e9174d1e | 905 | { |
1b1a35ee | 906 | self.base.take(value) |
e9174d1e | 907 | } |
8bb4bdeb XL |
908 | |
909 | /// Retains only the elements specified by the predicate. | |
910 | /// | |
911 | /// In other words, remove all elements `e` such that `f(&e)` returns `false`. | |
136023e0 | 912 | /// The elements are visited in unsorted (and unspecified) order. |
8bb4bdeb XL |
913 | /// |
914 | /// # Examples | |
915 | /// | |
916 | /// ``` | |
8bb4bdeb XL |
917 | /// use std::collections::HashSet; |
918 | /// | |
29967ef6 | 919 | /// let xs = [1, 2, 3, 4, 5, 6]; |
0531ce1d | 920 | /// let mut set: HashSet<i32> = xs.iter().cloned().collect(); |
8bb4bdeb XL |
921 | /// set.retain(|&k| k % 2 == 0); |
922 | /// assert_eq!(set.len(), 3); | |
923 | /// ``` | |
cc61c64b | 924 | #[stable(feature = "retain_hash_collection", since = "1.18.0")] |
1b1a35ee | 925 | pub fn retain<F>(&mut self, f: F) |
dfeec247 XL |
926 | where |
927 | F: FnMut(&T) -> bool, | |
8bb4bdeb | 928 | { |
1b1a35ee | 929 | self.base.retain(f) |
8bb4bdeb | 930 | } |
1a4d82fc JJ |
931 | } |
932 | ||
5869c6ff XL |
933 | #[stable(feature = "rust1", since = "1.0.0")] |
934 | impl<T, S> Clone for HashSet<T, S> | |
935 | where | |
936 | T: Clone, | |
937 | S: Clone, | |
938 | { | |
939 | #[inline] | |
940 | fn clone(&self) -> Self { | |
941 | Self { base: self.base.clone() } | |
942 | } | |
943 | ||
944 | #[inline] | |
945 | fn clone_from(&mut self, other: &Self) { | |
946 | self.base.clone_from(&other.base); | |
947 | } | |
948 | } | |
949 | ||
85aaf69f SL |
950 | #[stable(feature = "rust1", since = "1.0.0")] |
951 | impl<T, S> PartialEq for HashSet<T, S> | |
dfeec247 XL |
952 | where |
953 | T: Eq + Hash, | |
954 | S: BuildHasher, | |
1a4d82fc JJ |
955 | { |
956 | fn eq(&self, other: &HashSet<T, S>) -> bool { | |
c30ab7b3 SL |
957 | if self.len() != other.len() { |
958 | return false; | |
959 | } | |
1a4d82fc JJ |
960 | |
961 | self.iter().all(|key| other.contains(key)) | |
962 | } | |
963 | } | |
964 | ||
85aaf69f SL |
965 | #[stable(feature = "rust1", since = "1.0.0")] |
966 | impl<T, S> Eq for HashSet<T, S> | |
dfeec247 XL |
967 | where |
968 | T: Eq + Hash, | |
969 | S: BuildHasher, | |
c30ab7b3 SL |
970 | { |
971 | } | |
1a4d82fc | 972 | |
85aaf69f SL |
973 | #[stable(feature = "rust1", since = "1.0.0")] |
974 | impl<T, S> fmt::Debug for HashSet<T, S> | |
dfeec247 | 975 | where |
74b04a01 | 976 | T: fmt::Debug, |
1a4d82fc | 977 | { |
532ac7d7 | 978 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
62682a34 | 979 | f.debug_set().entries(self.iter()).finish() |
1a4d82fc JJ |
980 | } |
981 | } | |
982 | ||
85aaf69f SL |
983 | #[stable(feature = "rust1", since = "1.0.0")] |
984 | impl<T, S> FromIterator<T> for HashSet<T, S> | |
dfeec247 XL |
985 | where |
986 | T: Eq + Hash, | |
987 | S: BuildHasher + Default, | |
1a4d82fc | 988 | { |
48663c56 | 989 | #[inline] |
c30ab7b3 | 990 | fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> HashSet<T, S> { |
476ff2be SL |
991 | let mut set = HashSet::with_hasher(Default::default()); |
992 | set.extend(iter); | |
1a4d82fc JJ |
993 | set |
994 | } | |
995 | } | |
996 | ||
94222f64 XL |
997 | #[stable(feature = "std_collections_from_array", since = "1.56.0")] |
998 | // Note: as what is currently the most convenient built-in way to construct | |
999 | // a HashSet, a simple usage of this function must not *require* the user | |
1000 | // to provide a type annotation in order to infer the third type parameter | |
1001 | // (the hasher parameter, conventionally "S"). | |
1002 | // To that end, this impl is defined using RandomState as the concrete | |
1003 | // type of S, rather than being generic over `S: BuildHasher + Default`. | |
1004 | // It is expected that users who want to specify a hasher will manually use | |
1005 | // `with_capacity_and_hasher`. | |
1006 | // If type parameter defaults worked on impls, and if type parameter | |
1007 | // defaults could be mixed with const generics, then perhaps | |
1008 | // this could be generalized. | |
1009 | // See also the equivalent impl on HashMap. | |
1010 | impl<T, const N: usize> From<[T; N]> for HashSet<T, RandomState> | |
1011 | where | |
1012 | T: Eq + Hash, | |
1013 | { | |
1014 | /// # Examples | |
1015 | /// | |
1016 | /// ``` | |
1017 | /// use std::collections::HashSet; | |
1018 | /// | |
1019 | /// let set1 = HashSet::from([1, 2, 3, 4]); | |
1020 | /// let set2: HashSet<_> = [1, 2, 3, 4].into(); | |
1021 | /// assert_eq!(set1, set2); | |
1022 | /// ``` | |
1023 | fn from(arr: [T; N]) -> Self { | |
1024 | crate::array::IntoIter::new(arr).collect() | |
1025 | } | |
1026 | } | |
1027 | ||
85aaf69f SL |
1028 | #[stable(feature = "rust1", since = "1.0.0")] |
1029 | impl<T, S> Extend<T> for HashSet<T, S> | |
dfeec247 XL |
1030 | where |
1031 | T: Eq + Hash, | |
1032 | S: BuildHasher, | |
1a4d82fc | 1033 | { |
48663c56 | 1034 | #[inline] |
c30ab7b3 | 1035 | fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { |
1b1a35ee | 1036 | self.base.extend(iter); |
1a4d82fc | 1037 | } |
f9f354fc XL |
1038 | |
1039 | #[inline] | |
1040 | fn extend_one(&mut self, item: T) { | |
1b1a35ee | 1041 | self.base.insert(item); |
f9f354fc XL |
1042 | } |
1043 | ||
1044 | #[inline] | |
1045 | fn extend_reserve(&mut self, additional: usize) { | |
1b1a35ee | 1046 | self.base.extend_reserve(additional); |
f9f354fc | 1047 | } |
1a4d82fc JJ |
1048 | } |
1049 | ||
e9174d1e SL |
1050 | #[stable(feature = "hash_extend_copy", since = "1.4.0")] |
1051 | impl<'a, T, S> Extend<&'a T> for HashSet<T, S> | |
dfeec247 XL |
1052 | where |
1053 | T: 'a + Eq + Hash + Copy, | |
1054 | S: BuildHasher, | |
e9174d1e | 1055 | { |
48663c56 | 1056 | #[inline] |
c30ab7b3 | 1057 | fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { |
e9174d1e SL |
1058 | self.extend(iter.into_iter().cloned()); |
1059 | } | |
f9f354fc XL |
1060 | |
1061 | #[inline] | |
1062 | fn extend_one(&mut self, &item: &'a T) { | |
1b1a35ee | 1063 | self.base.insert(item); |
f9f354fc XL |
1064 | } |
1065 | ||
1066 | #[inline] | |
1067 | fn extend_reserve(&mut self, additional: usize) { | |
1068 | Extend::<T>::extend_reserve(self, additional) | |
1069 | } | |
e9174d1e SL |
1070 | } |
1071 | ||
85aaf69f SL |
1072 | #[stable(feature = "rust1", since = "1.0.0")] |
1073 | impl<T, S> Default for HashSet<T, S> | |
dfeec247 | 1074 | where |
74b04a01 | 1075 | S: Default, |
1a4d82fc | 1076 | { |
9e0c209e | 1077 | /// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher. |
48663c56 | 1078 | #[inline] |
1a4d82fc | 1079 | fn default() -> HashSet<T, S> { |
1b1a35ee | 1080 | HashSet { base: Default::default() } |
1a4d82fc JJ |
1081 | } |
1082 | } | |
1083 | ||
85aaf69f | 1084 | #[stable(feature = "rust1", since = "1.0.0")] |
532ac7d7 | 1085 | impl<T, S> BitOr<&HashSet<T, S>> for &HashSet<T, S> |
dfeec247 XL |
1086 | where |
1087 | T: Eq + Hash + Clone, | |
1088 | S: BuildHasher + Default, | |
1a4d82fc JJ |
1089 | { |
1090 | type Output = HashSet<T, S>; | |
1091 | ||
1092 | /// Returns the union of `self` and `rhs` as a new `HashSet<T, S>`. | |
1093 | /// | |
1094 | /// # Examples | |
1095 | /// | |
1096 | /// ``` | |
1097 | /// use std::collections::HashSet; | |
1098 | /// | |
85aaf69f SL |
1099 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); |
1100 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1a4d82fc | 1101 | /// |
85aaf69f | 1102 | /// let set = &a | &b; |
1a4d82fc JJ |
1103 | /// |
1104 | /// let mut i = 0; | |
1105 | /// let expected = [1, 2, 3, 4, 5]; | |
62682a34 | 1106 | /// for x in &set { |
1a4d82fc JJ |
1107 | /// assert!(expected.contains(x)); |
1108 | /// i += 1; | |
1109 | /// } | |
1110 | /// assert_eq!(i, expected.len()); | |
1111 | /// ``` | |
1112 | fn bitor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1113 | self.union(rhs).cloned().collect() | |
1114 | } | |
1115 | } | |
1116 | ||
85aaf69f | 1117 | #[stable(feature = "rust1", since = "1.0.0")] |
532ac7d7 | 1118 | impl<T, S> BitAnd<&HashSet<T, S>> for &HashSet<T, S> |
dfeec247 XL |
1119 | where |
1120 | T: Eq + Hash + Clone, | |
1121 | S: BuildHasher + Default, | |
1a4d82fc JJ |
1122 | { |
1123 | type Output = HashSet<T, S>; | |
1124 | ||
1125 | /// Returns the intersection of `self` and `rhs` as a new `HashSet<T, S>`. | |
1126 | /// | |
1127 | /// # Examples | |
1128 | /// | |
1129 | /// ``` | |
1130 | /// use std::collections::HashSet; | |
1131 | /// | |
85aaf69f SL |
1132 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); |
1133 | /// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect(); | |
1a4d82fc | 1134 | /// |
85aaf69f | 1135 | /// let set = &a & &b; |
1a4d82fc JJ |
1136 | /// |
1137 | /// let mut i = 0; | |
1138 | /// let expected = [2, 3]; | |
62682a34 | 1139 | /// for x in &set { |
1a4d82fc JJ |
1140 | /// assert!(expected.contains(x)); |
1141 | /// i += 1; | |
1142 | /// } | |
1143 | /// assert_eq!(i, expected.len()); | |
1144 | /// ``` | |
1145 | fn bitand(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1146 | self.intersection(rhs).cloned().collect() | |
1147 | } | |
1148 | } | |
1149 | ||
85aaf69f | 1150 | #[stable(feature = "rust1", since = "1.0.0")] |
532ac7d7 | 1151 | impl<T, S> BitXor<&HashSet<T, S>> for &HashSet<T, S> |
dfeec247 XL |
1152 | where |
1153 | T: Eq + Hash + Clone, | |
1154 | S: BuildHasher + Default, | |
1a4d82fc JJ |
1155 | { |
1156 | type Output = HashSet<T, S>; | |
1157 | ||
1158 | /// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, S>`. | |
1159 | /// | |
1160 | /// # Examples | |
1161 | /// | |
1162 | /// ``` | |
1163 | /// use std::collections::HashSet; | |
1164 | /// | |
85aaf69f SL |
1165 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); |
1166 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1a4d82fc | 1167 | /// |
85aaf69f | 1168 | /// let set = &a ^ &b; |
1a4d82fc JJ |
1169 | /// |
1170 | /// let mut i = 0; | |
1171 | /// let expected = [1, 2, 4, 5]; | |
62682a34 | 1172 | /// for x in &set { |
1a4d82fc JJ |
1173 | /// assert!(expected.contains(x)); |
1174 | /// i += 1; | |
1175 | /// } | |
1176 | /// assert_eq!(i, expected.len()); | |
1177 | /// ``` | |
1178 | fn bitxor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1179 | self.symmetric_difference(rhs).cloned().collect() | |
1180 | } | |
1181 | } | |
1182 | ||
85aaf69f | 1183 | #[stable(feature = "rust1", since = "1.0.0")] |
532ac7d7 | 1184 | impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S> |
dfeec247 XL |
1185 | where |
1186 | T: Eq + Hash + Clone, | |
1187 | S: BuildHasher + Default, | |
1a4d82fc JJ |
1188 | { |
1189 | type Output = HashSet<T, S>; | |
1190 | ||
1191 | /// Returns the difference of `self` and `rhs` as a new `HashSet<T, S>`. | |
1192 | /// | |
1193 | /// # Examples | |
1194 | /// | |
1195 | /// ``` | |
1196 | /// use std::collections::HashSet; | |
1197 | /// | |
85aaf69f SL |
1198 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); |
1199 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1a4d82fc | 1200 | /// |
85aaf69f | 1201 | /// let set = &a - &b; |
1a4d82fc JJ |
1202 | /// |
1203 | /// let mut i = 0; | |
1204 | /// let expected = [1, 2]; | |
62682a34 | 1205 | /// for x in &set { |
1a4d82fc JJ |
1206 | /// assert!(expected.contains(x)); |
1207 | /// i += 1; | |
1208 | /// } | |
1209 | /// assert_eq!(i, expected.len()); | |
1210 | /// ``` | |
1211 | fn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1212 | self.difference(rhs).cloned().collect() | |
1213 | } | |
1214 | } | |
1215 | ||
cc61c64b XL |
1216 | /// An iterator over the items of a `HashSet`. |
1217 | /// | |
1218 | /// This `struct` is created by the [`iter`] method on [`HashSet`]. | |
1219 | /// See its documentation for more. | |
1220 | /// | |
3dfed10e | 1221 | /// [`iter`]: HashSet::iter |
1b1a35ee XL |
1222 | /// |
1223 | /// # Examples | |
1224 | /// | |
1225 | /// ``` | |
1226 | /// use std::collections::HashSet; | |
1227 | /// | |
1228 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1229 | /// | |
1230 | /// let mut iter = a.iter(); | |
1231 | /// ``` | |
85aaf69f | 1232 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1233 | pub struct Iter<'a, K: 'a> { |
1b1a35ee | 1234 | base: base::Iter<'a, K>, |
1a4d82fc JJ |
1235 | } |
1236 | ||
cc61c64b XL |
1237 | /// An owning iterator over the items of a `HashSet`. |
1238 | /// | |
dfeec247 | 1239 | /// This `struct` is created by the [`into_iter`] method on [`HashSet`] |
cc61c64b XL |
1240 | /// (provided by the `IntoIterator` trait). See its documentation for more. |
1241 | /// | |
3dfed10e | 1242 | /// [`into_iter`]: IntoIterator::into_iter |
1b1a35ee XL |
1243 | /// |
1244 | /// # Examples | |
1245 | /// | |
1246 | /// ``` | |
1247 | /// use std::collections::HashSet; | |
1248 | /// | |
1249 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1250 | /// | |
1251 | /// let mut iter = a.into_iter(); | |
1252 | /// ``` | |
85aaf69f | 1253 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1254 | pub struct IntoIter<K> { |
1b1a35ee | 1255 | base: base::IntoIter<K>, |
1a4d82fc JJ |
1256 | } |
1257 | ||
cc61c64b XL |
1258 | /// A draining iterator over the items of a `HashSet`. |
1259 | /// | |
1260 | /// This `struct` is created by the [`drain`] method on [`HashSet`]. | |
1261 | /// See its documentation for more. | |
1262 | /// | |
3dfed10e | 1263 | /// [`drain`]: HashSet::drain |
1b1a35ee XL |
1264 | /// |
1265 | /// # Examples | |
1266 | /// | |
1267 | /// ``` | |
1268 | /// use std::collections::HashSet; | |
1269 | /// | |
1270 | /// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1271 | /// | |
1272 | /// let mut drain = a.drain(); | |
1273 | /// ``` | |
85aaf69f | 1274 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1275 | pub struct Drain<'a, K: 'a> { |
1b1a35ee XL |
1276 | base: base::Drain<'a, K>, |
1277 | } | |
1278 | ||
1279 | /// A draining, filtering iterator over the items of a `HashSet`. | |
1280 | /// | |
1281 | /// This `struct` is created by the [`drain_filter`] method on [`HashSet`]. | |
1282 | /// | |
1283 | /// [`drain_filter`]: HashSet::drain_filter | |
1284 | /// | |
1285 | /// # Examples | |
1286 | /// | |
1287 | /// ``` | |
1288 | /// #![feature(hash_drain_filter)] | |
1289 | /// | |
1290 | /// use std::collections::HashSet; | |
1291 | /// | |
1292 | /// let mut a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1293 | /// | |
1294 | /// let mut drain_filtered = a.drain_filter(|v| v % 2 == 0); | |
1295 | /// ``` | |
1296 | #[unstable(feature = "hash_drain_filter", issue = "59618")] | |
1297 | pub struct DrainFilter<'a, K, F> | |
1298 | where | |
1299 | F: FnMut(&K) -> bool, | |
1300 | { | |
1301 | base: base::DrainFilter<'a, K, F>, | |
1a4d82fc JJ |
1302 | } |
1303 | ||
cc61c64b XL |
1304 | /// A lazy iterator producing elements in the intersection of `HashSet`s. |
1305 | /// | |
1306 | /// This `struct` is created by the [`intersection`] method on [`HashSet`]. | |
1307 | /// See its documentation for more. | |
1308 | /// | |
3dfed10e | 1309 | /// [`intersection`]: HashSet::intersection |
1b1a35ee XL |
1310 | /// |
1311 | /// # Examples | |
1312 | /// | |
1313 | /// ``` | |
1314 | /// use std::collections::HashSet; | |
1315 | /// | |
1316 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1317 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1318 | /// | |
1319 | /// let mut intersection = a.intersection(&b); | |
1320 | /// ``` | |
85aaf69f | 1321 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1322 | pub struct Intersection<'a, T: 'a, S: 'a> { |
1323 | // iterator of the first set | |
1324 | iter: Iter<'a, T>, | |
1325 | // the second set | |
1326 | other: &'a HashSet<T, S>, | |
1327 | } | |
1328 | ||
cc61c64b XL |
1329 | /// A lazy iterator producing elements in the difference of `HashSet`s. |
1330 | /// | |
1331 | /// This `struct` is created by the [`difference`] method on [`HashSet`]. | |
1332 | /// See its documentation for more. | |
1333 | /// | |
3dfed10e | 1334 | /// [`difference`]: HashSet::difference |
1b1a35ee XL |
1335 | /// |
1336 | /// # Examples | |
1337 | /// | |
1338 | /// ``` | |
1339 | /// use std::collections::HashSet; | |
1340 | /// | |
1341 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1342 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1343 | /// | |
1344 | /// let mut difference = a.difference(&b); | |
1345 | /// ``` | |
85aaf69f | 1346 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1347 | pub struct Difference<'a, T: 'a, S: 'a> { |
1348 | // iterator of the first set | |
1349 | iter: Iter<'a, T>, | |
1350 | // the second set | |
1351 | other: &'a HashSet<T, S>, | |
1352 | } | |
1353 | ||
cc61c64b XL |
1354 | /// A lazy iterator producing elements in the symmetric difference of `HashSet`s. |
1355 | /// | |
1356 | /// This `struct` is created by the [`symmetric_difference`] method on | |
1357 | /// [`HashSet`]. See its documentation for more. | |
1358 | /// | |
3dfed10e | 1359 | /// [`symmetric_difference`]: HashSet::symmetric_difference |
1b1a35ee XL |
1360 | /// |
1361 | /// # Examples | |
1362 | /// | |
1363 | /// ``` | |
1364 | /// use std::collections::HashSet; | |
1365 | /// | |
1366 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1367 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1368 | /// | |
1369 | /// let mut intersection = a.symmetric_difference(&b); | |
1370 | /// ``` | |
85aaf69f | 1371 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1372 | pub struct SymmetricDifference<'a, T: 'a, S: 'a> { |
c30ab7b3 | 1373 | iter: Chain<Difference<'a, T, S>, Difference<'a, T, S>>, |
1a4d82fc JJ |
1374 | } |
1375 | ||
cc61c64b XL |
1376 | /// A lazy iterator producing elements in the union of `HashSet`s. |
1377 | /// | |
1378 | /// This `struct` is created by the [`union`] method on [`HashSet`]. | |
1379 | /// See its documentation for more. | |
1380 | /// | |
3dfed10e | 1381 | /// [`union`]: HashSet::union |
1b1a35ee XL |
1382 | /// |
1383 | /// # Examples | |
1384 | /// | |
1385 | /// ``` | |
1386 | /// use std::collections::HashSet; | |
1387 | /// | |
1388 | /// let a: HashSet<u32> = vec![1, 2, 3].into_iter().collect(); | |
1389 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
1390 | /// | |
1391 | /// let mut union_iter = a.union(&b); | |
1392 | /// ``` | |
85aaf69f | 1393 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc | 1394 | pub struct Union<'a, T: 'a, S: 'a> { |
c30ab7b3 | 1395 | iter: Chain<Iter<'a, T>, Difference<'a, T, S>>, |
1a4d82fc JJ |
1396 | } |
1397 | ||
85aaf69f | 1398 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1399 | impl<'a, T, S> IntoIterator for &'a HashSet<T, S> { |
85aaf69f SL |
1400 | type Item = &'a T; |
1401 | type IntoIter = Iter<'a, T>; | |
1402 | ||
48663c56 | 1403 | #[inline] |
85aaf69f SL |
1404 | fn into_iter(self) -> Iter<'a, T> { |
1405 | self.iter() | |
1406 | } | |
1407 | } | |
1408 | ||
1409 | #[stable(feature = "rust1", since = "1.0.0")] | |
9fa01778 | 1410 | impl<T, S> IntoIterator for HashSet<T, S> { |
85aaf69f SL |
1411 | type Item = T; |
1412 | type IntoIter = IntoIter<T>; | |
1413 | ||
9346a6ac AL |
1414 | /// Creates a consuming iterator, that is, one that moves each value out |
1415 | /// of the set in arbitrary order. The set cannot be used after calling | |
1416 | /// this. | |
1417 | /// | |
1418 | /// # Examples | |
1419 | /// | |
1420 | /// ``` | |
1421 | /// use std::collections::HashSet; | |
1422 | /// let mut set = HashSet::new(); | |
1423 | /// set.insert("a".to_string()); | |
1424 | /// set.insert("b".to_string()); | |
1425 | /// | |
1426 | /// // Not possible to collect to a Vec<String> with a regular `.iter()`. | |
1427 | /// let v: Vec<String> = set.into_iter().collect(); | |
1428 | /// | |
1429 | /// // Will print in an arbitrary order. | |
62682a34 | 1430 | /// for x in &v { |
9346a6ac AL |
1431 | /// println!("{}", x); |
1432 | /// } | |
1433 | /// ``` | |
48663c56 | 1434 | #[inline] |
85aaf69f | 1435 | fn into_iter(self) -> IntoIter<T> { |
1b1a35ee | 1436 | IntoIter { base: self.base.into_iter() } |
85aaf69f SL |
1437 | } |
1438 | } | |
1439 | ||
92a42be0 | 1440 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1441 | impl<K> Clone for Iter<'_, K> { |
48663c56 | 1442 | #[inline] |
9fa01778 | 1443 | fn clone(&self) -> Self { |
1b1a35ee | 1444 | Iter { base: self.base.clone() } |
c30ab7b3 | 1445 | } |
c34b1796 | 1446 | } |
85aaf69f | 1447 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1448 | impl<'a, K> Iterator for Iter<'a, K> { |
1449 | type Item = &'a K; | |
1450 | ||
48663c56 | 1451 | #[inline] |
c30ab7b3 | 1452 | fn next(&mut self) -> Option<&'a K> { |
1b1a35ee | 1453 | self.base.next() |
c30ab7b3 | 1454 | } |
48663c56 | 1455 | #[inline] |
c30ab7b3 | 1456 | fn size_hint(&self) -> (usize, Option<usize>) { |
1b1a35ee | 1457 | self.base.size_hint() |
c30ab7b3 | 1458 | } |
85aaf69f SL |
1459 | } |
1460 | #[stable(feature = "rust1", since = "1.0.0")] | |
9fa01778 | 1461 | impl<K> ExactSizeIterator for Iter<'_, K> { |
48663c56 | 1462 | #[inline] |
c30ab7b3 | 1463 | fn len(&self) -> usize { |
1b1a35ee | 1464 | self.base.len() |
c30ab7b3 | 1465 | } |
1a4d82fc | 1466 | } |
0531ce1d | 1467 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1468 | impl<K> FusedIterator for Iter<'_, K> {} |
1a4d82fc | 1469 | |
8bb4bdeb | 1470 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1471 | impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> { |
532ac7d7 | 1472 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cc61c64b | 1473 | f.debug_list().entries(self.clone()).finish() |
32a655c1 SL |
1474 | } |
1475 | } | |
1476 | ||
85aaf69f | 1477 | #[stable(feature = "rust1", since = "1.0.0")] |
1a4d82fc JJ |
1478 | impl<K> Iterator for IntoIter<K> { |
1479 | type Item = K; | |
1480 | ||
48663c56 | 1481 | #[inline] |
c30ab7b3 | 1482 | fn next(&mut self) -> Option<K> { |
1b1a35ee | 1483 | self.base.next() |
c30ab7b3 | 1484 | } |
48663c56 | 1485 | #[inline] |
c30ab7b3 | 1486 | fn size_hint(&self) -> (usize, Option<usize>) { |
1b1a35ee | 1487 | self.base.size_hint() |
c30ab7b3 | 1488 | } |
85aaf69f SL |
1489 | } |
1490 | #[stable(feature = "rust1", since = "1.0.0")] | |
1491 | impl<K> ExactSizeIterator for IntoIter<K> { | |
48663c56 | 1492 | #[inline] |
c30ab7b3 | 1493 | fn len(&self) -> usize { |
1b1a35ee | 1494 | self.base.len() |
c30ab7b3 | 1495 | } |
1a4d82fc | 1496 | } |
0531ce1d | 1497 | #[stable(feature = "fused", since = "1.26.0")] |
9e0c209e | 1498 | impl<K> FusedIterator for IntoIter<K> {} |
1a4d82fc | 1499 | |
8bb4bdeb | 1500 | #[stable(feature = "std_debug", since = "1.16.0")] |
32a655c1 | 1501 | impl<K: fmt::Debug> fmt::Debug for IntoIter<K> { |
532ac7d7 | 1502 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1b1a35ee | 1503 | fmt::Debug::fmt(&self.base, f) |
32a655c1 SL |
1504 | } |
1505 | } | |
1506 | ||
85aaf69f SL |
1507 | #[stable(feature = "rust1", since = "1.0.0")] |
1508 | impl<'a, K> Iterator for Drain<'a, K> { | |
1a4d82fc JJ |
1509 | type Item = K; |
1510 | ||
48663c56 | 1511 | #[inline] |
c30ab7b3 | 1512 | fn next(&mut self) -> Option<K> { |
1b1a35ee | 1513 | self.base.next() |
c30ab7b3 | 1514 | } |
48663c56 | 1515 | #[inline] |
c30ab7b3 | 1516 | fn size_hint(&self) -> (usize, Option<usize>) { |
1b1a35ee | 1517 | self.base.size_hint() |
c30ab7b3 | 1518 | } |
85aaf69f SL |
1519 | } |
1520 | #[stable(feature = "rust1", since = "1.0.0")] | |
9fa01778 | 1521 | impl<K> ExactSizeIterator for Drain<'_, K> { |
48663c56 | 1522 | #[inline] |
c30ab7b3 | 1523 | fn len(&self) -> usize { |
1b1a35ee | 1524 | self.base.len() |
c30ab7b3 | 1525 | } |
1a4d82fc | 1526 | } |
0531ce1d | 1527 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1528 | impl<K> FusedIterator for Drain<'_, K> {} |
1a4d82fc | 1529 | |
8bb4bdeb | 1530 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1531 | impl<K: fmt::Debug> fmt::Debug for Drain<'_, K> { |
532ac7d7 | 1532 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1b1a35ee XL |
1533 | fmt::Debug::fmt(&self.base, f) |
1534 | } | |
1535 | } | |
1536 | ||
1537 | #[unstable(feature = "hash_drain_filter", issue = "59618")] | |
1538 | impl<K, F> Iterator for DrainFilter<'_, K, F> | |
1539 | where | |
1540 | F: FnMut(&K) -> bool, | |
1541 | { | |
1542 | type Item = K; | |
1543 | ||
1544 | #[inline] | |
1545 | fn next(&mut self) -> Option<K> { | |
1546 | self.base.next() | |
1547 | } | |
1548 | #[inline] | |
1549 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1550 | self.base.size_hint() | |
1551 | } | |
1552 | } | |
1553 | ||
1554 | #[unstable(feature = "hash_drain_filter", issue = "59618")] | |
1555 | impl<K, F> FusedIterator for DrainFilter<'_, K, F> where F: FnMut(&K) -> bool {} | |
1556 | ||
1557 | #[unstable(feature = "hash_drain_filter", issue = "59618")] | |
1558 | impl<'a, K, F> fmt::Debug for DrainFilter<'a, K, F> | |
1559 | where | |
1560 | F: FnMut(&K) -> bool, | |
1561 | { | |
1562 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
cdc7bbd5 | 1563 | f.debug_struct("DrainFilter").finish_non_exhaustive() |
32a655c1 SL |
1564 | } |
1565 | } | |
1566 | ||
92a42be0 | 1567 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1568 | impl<T, S> Clone for Intersection<'_, T, S> { |
48663c56 | 1569 | #[inline] |
9fa01778 | 1570 | fn clone(&self) -> Self { |
c34b1796 AL |
1571 | Intersection { iter: self.iter.clone(), ..*self } |
1572 | } | |
1573 | } | |
1574 | ||
85aaf69f SL |
1575 | #[stable(feature = "rust1", since = "1.0.0")] |
1576 | impl<'a, T, S> Iterator for Intersection<'a, T, S> | |
dfeec247 XL |
1577 | where |
1578 | T: Eq + Hash, | |
1579 | S: BuildHasher, | |
1a4d82fc JJ |
1580 | { |
1581 | type Item = &'a T; | |
1582 | ||
48663c56 | 1583 | #[inline] |
1a4d82fc JJ |
1584 | fn next(&mut self) -> Option<&'a T> { |
1585 | loop { | |
ff7c6d11 XL |
1586 | let elt = self.iter.next()?; |
1587 | if self.other.contains(elt) { | |
1588 | return Some(elt); | |
1a4d82fc JJ |
1589 | } |
1590 | } | |
1591 | } | |
1592 | ||
48663c56 | 1593 | #[inline] |
85aaf69f | 1594 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1595 | let (_, upper) = self.iter.size_hint(); |
1596 | (0, upper) | |
1597 | } | |
1598 | } | |
1599 | ||
8bb4bdeb | 1600 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1601 | impl<T, S> fmt::Debug for Intersection<'_, T, S> |
dfeec247 XL |
1602 | where |
1603 | T: fmt::Debug + Eq + Hash, | |
1604 | S: BuildHasher, | |
32a655c1 | 1605 | { |
532ac7d7 | 1606 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cc61c64b | 1607 | f.debug_list().entries(self.clone()).finish() |
32a655c1 SL |
1608 | } |
1609 | } | |
1610 | ||
0531ce1d | 1611 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1612 | impl<T, S> FusedIterator for Intersection<'_, T, S> |
dfeec247 XL |
1613 | where |
1614 | T: Eq + Hash, | |
1615 | S: BuildHasher, | |
c30ab7b3 SL |
1616 | { |
1617 | } | |
9e0c209e | 1618 | |
92a42be0 | 1619 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1620 | impl<T, S> Clone for Difference<'_, T, S> { |
48663c56 | 1621 | #[inline] |
9fa01778 | 1622 | fn clone(&self) -> Self { |
c34b1796 AL |
1623 | Difference { iter: self.iter.clone(), ..*self } |
1624 | } | |
1625 | } | |
1626 | ||
85aaf69f SL |
1627 | #[stable(feature = "rust1", since = "1.0.0")] |
1628 | impl<'a, T, S> Iterator for Difference<'a, T, S> | |
dfeec247 XL |
1629 | where |
1630 | T: Eq + Hash, | |
1631 | S: BuildHasher, | |
1a4d82fc JJ |
1632 | { |
1633 | type Item = &'a T; | |
1634 | ||
48663c56 | 1635 | #[inline] |
1a4d82fc JJ |
1636 | fn next(&mut self) -> Option<&'a T> { |
1637 | loop { | |
ff7c6d11 XL |
1638 | let elt = self.iter.next()?; |
1639 | if !self.other.contains(elt) { | |
1640 | return Some(elt); | |
1a4d82fc JJ |
1641 | } |
1642 | } | |
1643 | } | |
1644 | ||
48663c56 | 1645 | #[inline] |
85aaf69f | 1646 | fn size_hint(&self) -> (usize, Option<usize>) { |
1a4d82fc JJ |
1647 | let (_, upper) = self.iter.size_hint(); |
1648 | (0, upper) | |
1649 | } | |
1650 | } | |
1651 | ||
0531ce1d | 1652 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1653 | impl<T, S> FusedIterator for Difference<'_, T, S> |
dfeec247 XL |
1654 | where |
1655 | T: Eq + Hash, | |
1656 | S: BuildHasher, | |
c30ab7b3 SL |
1657 | { |
1658 | } | |
9e0c209e | 1659 | |
8bb4bdeb | 1660 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1661 | impl<T, S> fmt::Debug for Difference<'_, T, S> |
dfeec247 XL |
1662 | where |
1663 | T: fmt::Debug + Eq + Hash, | |
1664 | S: BuildHasher, | |
32a655c1 | 1665 | { |
532ac7d7 | 1666 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cc61c64b | 1667 | f.debug_list().entries(self.clone()).finish() |
32a655c1 SL |
1668 | } |
1669 | } | |
1670 | ||
92a42be0 | 1671 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1672 | impl<T, S> Clone for SymmetricDifference<'_, T, S> { |
48663c56 | 1673 | #[inline] |
9fa01778 | 1674 | fn clone(&self) -> Self { |
c34b1796 AL |
1675 | SymmetricDifference { iter: self.iter.clone() } |
1676 | } | |
1677 | } | |
1678 | ||
85aaf69f SL |
1679 | #[stable(feature = "rust1", since = "1.0.0")] |
1680 | impl<'a, T, S> Iterator for SymmetricDifference<'a, T, S> | |
dfeec247 XL |
1681 | where |
1682 | T: Eq + Hash, | |
1683 | S: BuildHasher, | |
1a4d82fc JJ |
1684 | { |
1685 | type Item = &'a T; | |
1686 | ||
48663c56 | 1687 | #[inline] |
c30ab7b3 SL |
1688 | fn next(&mut self) -> Option<&'a T> { |
1689 | self.iter.next() | |
1690 | } | |
48663c56 | 1691 | #[inline] |
c30ab7b3 SL |
1692 | fn size_hint(&self) -> (usize, Option<usize>) { |
1693 | self.iter.size_hint() | |
1694 | } | |
1a4d82fc JJ |
1695 | } |
1696 | ||
0531ce1d | 1697 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1698 | impl<T, S> FusedIterator for SymmetricDifference<'_, T, S> |
dfeec247 XL |
1699 | where |
1700 | T: Eq + Hash, | |
1701 | S: BuildHasher, | |
c30ab7b3 SL |
1702 | { |
1703 | } | |
9e0c209e | 1704 | |
8bb4bdeb | 1705 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1706 | impl<T, S> fmt::Debug for SymmetricDifference<'_, T, S> |
dfeec247 XL |
1707 | where |
1708 | T: fmt::Debug + Eq + Hash, | |
1709 | S: BuildHasher, | |
32a655c1 | 1710 | { |
532ac7d7 | 1711 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cc61c64b | 1712 | f.debug_list().entries(self.clone()).finish() |
32a655c1 SL |
1713 | } |
1714 | } | |
1715 | ||
92a42be0 | 1716 | #[stable(feature = "rust1", since = "1.0.0")] |
9fa01778 | 1717 | impl<T, S> Clone for Union<'_, T, S> { |
48663c56 | 1718 | #[inline] |
9fa01778 | 1719 | fn clone(&self) -> Self { |
c30ab7b3 SL |
1720 | Union { iter: self.iter.clone() } |
1721 | } | |
c34b1796 AL |
1722 | } |
1723 | ||
0531ce1d | 1724 | #[stable(feature = "fused", since = "1.26.0")] |
9fa01778 | 1725 | impl<T, S> FusedIterator for Union<'_, T, S> |
dfeec247 XL |
1726 | where |
1727 | T: Eq + Hash, | |
1728 | S: BuildHasher, | |
c30ab7b3 SL |
1729 | { |
1730 | } | |
9e0c209e | 1731 | |
8bb4bdeb | 1732 | #[stable(feature = "std_debug", since = "1.16.0")] |
9fa01778 | 1733 | impl<T, S> fmt::Debug for Union<'_, T, S> |
dfeec247 XL |
1734 | where |
1735 | T: fmt::Debug + Eq + Hash, | |
1736 | S: BuildHasher, | |
32a655c1 | 1737 | { |
532ac7d7 | 1738 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
cc61c64b | 1739 | f.debug_list().entries(self.clone()).finish() |
32a655c1 SL |
1740 | } |
1741 | } | |
1742 | ||
85aaf69f SL |
1743 | #[stable(feature = "rust1", since = "1.0.0")] |
1744 | impl<'a, T, S> Iterator for Union<'a, T, S> | |
dfeec247 XL |
1745 | where |
1746 | T: Eq + Hash, | |
1747 | S: BuildHasher, | |
1a4d82fc JJ |
1748 | { |
1749 | type Item = &'a T; | |
1750 | ||
48663c56 | 1751 | #[inline] |
c30ab7b3 SL |
1752 | fn next(&mut self) -> Option<&'a T> { |
1753 | self.iter.next() | |
1754 | } | |
48663c56 | 1755 | #[inline] |
c30ab7b3 SL |
1756 | fn size_hint(&self) -> (usize, Option<usize>) { |
1757 | self.iter.size_hint() | |
1758 | } | |
1a4d82fc JJ |
1759 | } |
1760 | ||
54a0048b SL |
1761 | #[allow(dead_code)] |
1762 | fn assert_covariance() { | |
c30ab7b3 SL |
1763 | fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> { |
1764 | v | |
1765 | } | |
1766 | fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> { | |
1767 | v | |
1768 | } | |
1769 | fn into_iter<'new>(v: IntoIter<&'static str>) -> IntoIter<&'new str> { | |
1770 | v | |
1771 | } | |
dfeec247 XL |
1772 | fn difference<'a, 'new>( |
1773 | v: Difference<'a, &'static str, RandomState>, | |
1774 | ) -> Difference<'a, &'new str, RandomState> { | |
c30ab7b3 SL |
1775 | v |
1776 | } | |
dfeec247 XL |
1777 | fn symmetric_difference<'a, 'new>( |
1778 | v: SymmetricDifference<'a, &'static str, RandomState>, | |
1779 | ) -> SymmetricDifference<'a, &'new str, RandomState> { | |
c30ab7b3 SL |
1780 | v |
1781 | } | |
dfeec247 XL |
1782 | fn intersection<'a, 'new>( |
1783 | v: Intersection<'a, &'static str, RandomState>, | |
1784 | ) -> Intersection<'a, &'new str, RandomState> { | |
c30ab7b3 SL |
1785 | v |
1786 | } | |
dfeec247 XL |
1787 | fn union<'a, 'new>( |
1788 | v: Union<'a, &'static str, RandomState>, | |
1789 | ) -> Union<'a, &'new str, RandomState> { | |
c30ab7b3 SL |
1790 | v |
1791 | } | |
1792 | fn drain<'new>(d: Drain<'static, &'static str>) -> Drain<'new, &'new str> { | |
1793 | d | |
1794 | } | |
54a0048b | 1795 | } |