]>
Commit | Line | Data |
---|---|---|
5099ac24 FG |
1 | use crate::TryReserveError; |
2 | use alloc::borrow::ToOwned; | |
3 | use core::borrow::Borrow; | |
4 | use core::fmt; | |
5 | use core::hash::{BuildHasher, Hash}; | |
6 | use core::iter::{Chain, FromIterator, FusedIterator}; | |
7 | use core::mem; | |
8 | use core::ops::{BitAnd, BitOr, BitXor, Sub}; | |
9 | ||
10 | use super::map::{self, ConsumeAllOnDrop, DefaultHashBuilder, DrainFilterInner, HashMap, Keys}; | |
11 | use crate::raw::{Allocator, Global}; | |
12 | ||
13 | // Future Optimization (FIXME!) | |
14 | // ============================= | |
15 | // | |
16 | // Iteration over zero sized values is a noop. There is no need | |
17 | // for `bucket.val` in the case of HashSet. I suppose we would need HKT | |
18 | // to get rid of it properly. | |
19 | ||
20 | /// A hash set implemented as a `HashMap` where the value is `()`. | |
21 | /// | |
22 | /// As with the [`HashMap`] type, a `HashSet` requires that the elements | |
23 | /// implement the [`Eq`] and [`Hash`] traits. This can frequently be achieved by | |
24 | /// using `#[derive(PartialEq, Eq, Hash)]`. If you implement these yourself, | |
25 | /// it is important that the following property holds: | |
26 | /// | |
27 | /// ```text | |
28 | /// k1 == k2 -> hash(k1) == hash(k2) | |
29 | /// ``` | |
30 | /// | |
31 | /// In other words, if two keys are equal, their hashes must be equal. | |
32 | /// | |
33 | /// | |
34 | /// It is a logic error for an item to be modified in such a way that the | |
35 | /// item's hash, as determined by the [`Hash`] trait, or its equality, as | |
36 | /// determined by the [`Eq`] trait, changes while it is in the set. This is | |
37 | /// normally only possible through [`Cell`], [`RefCell`], global state, I/O, or | |
38 | /// unsafe code. | |
39 | /// | |
40 | /// It is also a logic error for the [`Hash`] implementation of a key to panic. | |
41 | /// This is generally only possible if the trait is implemented manually. If a | |
42 | /// panic does occur then the contents of the `HashSet` may become corrupted and | |
43 | /// some items may be dropped from the table. | |
44 | /// | |
45 | /// # Examples | |
46 | /// | |
47 | /// ``` | |
48 | /// use hashbrown::HashSet; | |
49 | /// // Type inference lets us omit an explicit type signature (which | |
50 | /// // would be `HashSet<String>` in this example). | |
51 | /// let mut books = HashSet::new(); | |
52 | /// | |
53 | /// // Add some books. | |
54 | /// books.insert("A Dance With Dragons".to_string()); | |
55 | /// books.insert("To Kill a Mockingbird".to_string()); | |
56 | /// books.insert("The Odyssey".to_string()); | |
57 | /// books.insert("The Great Gatsby".to_string()); | |
58 | /// | |
59 | /// // Check for a specific one. | |
60 | /// if !books.contains("The Winds of Winter") { | |
61 | /// println!("We have {} books, but The Winds of Winter ain't one.", | |
62 | /// books.len()); | |
63 | /// } | |
64 | /// | |
65 | /// // Remove a book. | |
66 | /// books.remove("The Odyssey"); | |
67 | /// | |
68 | /// // Iterate over everything. | |
69 | /// for book in &books { | |
70 | /// println!("{}", book); | |
71 | /// } | |
72 | /// ``` | |
73 | /// | |
74 | /// The easiest way to use `HashSet` with a custom type is to derive | |
75 | /// [`Eq`] and [`Hash`]. We must also derive [`PartialEq`]. This will in the | |
76 | /// future be implied by [`Eq`]. | |
77 | /// | |
78 | /// ``` | |
79 | /// use hashbrown::HashSet; | |
80 | /// #[derive(Hash, Eq, PartialEq, Debug)] | |
81 | /// struct Viking { | |
82 | /// name: String, | |
83 | /// power: usize, | |
84 | /// } | |
85 | /// | |
86 | /// let mut vikings = HashSet::new(); | |
87 | /// | |
88 | /// vikings.insert(Viking { name: "Einar".to_string(), power: 9 }); | |
89 | /// vikings.insert(Viking { name: "Einar".to_string(), power: 9 }); | |
90 | /// vikings.insert(Viking { name: "Olaf".to_string(), power: 4 }); | |
91 | /// vikings.insert(Viking { name: "Harald".to_string(), power: 8 }); | |
92 | /// | |
93 | /// // Use derived implementation to print the vikings. | |
94 | /// for x in &vikings { | |
95 | /// println!("{:?}", x); | |
96 | /// } | |
97 | /// ``` | |
98 | /// | |
99 | /// A `HashSet` with fixed list of elements can be initialized from an array: | |
100 | /// | |
101 | /// ``` | |
102 | /// use hashbrown::HashSet; | |
103 | /// | |
104 | /// let viking_names: HashSet<&'static str> = | |
105 | /// [ "Einar", "Olaf", "Harald" ].iter().cloned().collect(); | |
106 | /// // use the values stored in the set | |
107 | /// ``` | |
108 | /// | |
109 | /// [`Cell`]: https://doc.rust-lang.org/std/cell/struct.Cell.html | |
110 | /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html | |
111 | /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html | |
112 | /// [`HashMap`]: struct.HashMap.html | |
113 | /// [`PartialEq`]: https://doc.rust-lang.org/std/cmp/trait.PartialEq.html | |
114 | /// [`RefCell`]: https://doc.rust-lang.org/std/cell/struct.RefCell.html | |
115 | pub struct HashSet<T, S = DefaultHashBuilder, A: Allocator + Clone = Global> { | |
116 | pub(crate) map: HashMap<T, (), S, A>, | |
117 | } | |
118 | ||
119 | impl<T: Clone, S: Clone, A: Allocator + Clone> Clone for HashSet<T, S, A> { | |
120 | fn clone(&self) -> Self { | |
121 | HashSet { | |
122 | map: self.map.clone(), | |
123 | } | |
124 | } | |
125 | ||
126 | fn clone_from(&mut self, source: &Self) { | |
127 | self.map.clone_from(&source.map); | |
128 | } | |
129 | } | |
130 | ||
131 | #[cfg(feature = "ahash")] | |
132 | impl<T> HashSet<T, DefaultHashBuilder> { | |
133 | /// Creates an empty `HashSet`. | |
134 | /// | |
135 | /// The hash set is initially created with a capacity of 0, so it will not allocate until it | |
136 | /// is first inserted into. | |
137 | /// | |
138 | /// # Examples | |
139 | /// | |
140 | /// ``` | |
141 | /// use hashbrown::HashSet; | |
142 | /// let set: HashSet<i32> = HashSet::new(); | |
143 | /// ``` | |
144 | #[cfg_attr(feature = "inline-more", inline)] | |
145 | pub fn new() -> Self { | |
146 | Self { | |
147 | map: HashMap::new(), | |
148 | } | |
149 | } | |
150 | ||
151 | /// Creates an empty `HashSet` with the specified capacity. | |
152 | /// | |
153 | /// The hash set will be able to hold at least `capacity` elements without | |
154 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
155 | /// | |
156 | /// # Examples | |
157 | /// | |
158 | /// ``` | |
159 | /// use hashbrown::HashSet; | |
160 | /// let set: HashSet<i32> = HashSet::with_capacity(10); | |
161 | /// assert!(set.capacity() >= 10); | |
162 | /// ``` | |
163 | #[cfg_attr(feature = "inline-more", inline)] | |
164 | pub fn with_capacity(capacity: usize) -> Self { | |
165 | Self { | |
166 | map: HashMap::with_capacity(capacity), | |
167 | } | |
168 | } | |
169 | } | |
170 | ||
171 | #[cfg(feature = "ahash")] | |
172 | impl<T: Hash + Eq, A: Allocator + Clone> HashSet<T, DefaultHashBuilder, A> { | |
173 | /// Creates an empty `HashSet`. | |
174 | /// | |
175 | /// The hash set is initially created with a capacity of 0, so it will not allocate until it | |
176 | /// is first inserted into. | |
177 | /// | |
178 | /// # Examples | |
179 | /// | |
180 | /// ``` | |
181 | /// use hashbrown::HashSet; | |
182 | /// let set: HashSet<i32> = HashSet::new(); | |
183 | /// ``` | |
184 | #[cfg_attr(feature = "inline-more", inline)] | |
185 | pub fn new_in(alloc: A) -> Self { | |
186 | Self { | |
187 | map: HashMap::new_in(alloc), | |
188 | } | |
189 | } | |
190 | ||
191 | /// Creates an empty `HashSet` with the specified capacity. | |
192 | /// | |
193 | /// The hash set will be able to hold at least `capacity` elements without | |
194 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
195 | /// | |
196 | /// # Examples | |
197 | /// | |
198 | /// ``` | |
199 | /// use hashbrown::HashSet; | |
200 | /// let set: HashSet<i32> = HashSet::with_capacity(10); | |
201 | /// assert!(set.capacity() >= 10); | |
202 | /// ``` | |
203 | #[cfg_attr(feature = "inline-more", inline)] | |
204 | pub fn with_capacity_in(capacity: usize, alloc: A) -> Self { | |
205 | Self { | |
206 | map: HashMap::with_capacity_in(capacity, alloc), | |
207 | } | |
208 | } | |
209 | } | |
210 | ||
211 | impl<T, S, A: Allocator + Clone> HashSet<T, S, A> { | |
212 | /// Returns the number of elements the set can hold without reallocating. | |
213 | /// | |
214 | /// # Examples | |
215 | /// | |
216 | /// ``` | |
217 | /// use hashbrown::HashSet; | |
218 | /// let set: HashSet<i32> = HashSet::with_capacity(100); | |
219 | /// assert!(set.capacity() >= 100); | |
220 | /// ``` | |
221 | #[cfg_attr(feature = "inline-more", inline)] | |
222 | pub fn capacity(&self) -> usize { | |
223 | self.map.capacity() | |
224 | } | |
225 | ||
226 | /// An iterator visiting all elements in arbitrary order. | |
227 | /// The iterator element type is `&'a T`. | |
228 | /// | |
229 | /// # Examples | |
230 | /// | |
231 | /// ``` | |
232 | /// use hashbrown::HashSet; | |
233 | /// let mut set = HashSet::new(); | |
234 | /// set.insert("a"); | |
235 | /// set.insert("b"); | |
236 | /// | |
237 | /// // Will print in an arbitrary order. | |
238 | /// for x in set.iter() { | |
239 | /// println!("{}", x); | |
240 | /// } | |
241 | /// ``` | |
242 | #[cfg_attr(feature = "inline-more", inline)] | |
243 | pub fn iter(&self) -> Iter<'_, T> { | |
244 | Iter { | |
245 | iter: self.map.keys(), | |
246 | } | |
247 | } | |
248 | ||
249 | /// Returns the number of elements in the set. | |
250 | /// | |
251 | /// # Examples | |
252 | /// | |
253 | /// ``` | |
254 | /// use hashbrown::HashSet; | |
255 | /// | |
256 | /// let mut v = HashSet::new(); | |
257 | /// assert_eq!(v.len(), 0); | |
258 | /// v.insert(1); | |
259 | /// assert_eq!(v.len(), 1); | |
260 | /// ``` | |
261 | #[cfg_attr(feature = "inline-more", inline)] | |
262 | pub fn len(&self) -> usize { | |
263 | self.map.len() | |
264 | } | |
265 | ||
266 | /// Returns `true` if the set contains no elements. | |
267 | /// | |
268 | /// # Examples | |
269 | /// | |
270 | /// ``` | |
271 | /// use hashbrown::HashSet; | |
272 | /// | |
273 | /// let mut v = HashSet::new(); | |
274 | /// assert!(v.is_empty()); | |
275 | /// v.insert(1); | |
276 | /// assert!(!v.is_empty()); | |
277 | /// ``` | |
278 | #[cfg_attr(feature = "inline-more", inline)] | |
279 | pub fn is_empty(&self) -> bool { | |
280 | self.map.is_empty() | |
281 | } | |
282 | ||
283 | /// Clears the set, returning all elements in an iterator. | |
284 | /// | |
285 | /// # Examples | |
286 | /// | |
287 | /// ``` | |
288 | /// use hashbrown::HashSet; | |
289 | /// | |
290 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
291 | /// assert!(!set.is_empty()); | |
292 | /// | |
293 | /// // print 1, 2, 3 in an arbitrary order | |
294 | /// for i in set.drain() { | |
295 | /// println!("{}", i); | |
296 | /// } | |
297 | /// | |
298 | /// assert!(set.is_empty()); | |
299 | /// ``` | |
300 | #[cfg_attr(feature = "inline-more", inline)] | |
301 | pub fn drain(&mut self) -> Drain<'_, T, A> { | |
302 | Drain { | |
303 | iter: self.map.drain(), | |
304 | } | |
305 | } | |
306 | ||
307 | /// Retains only the elements specified by the predicate. | |
308 | /// | |
309 | /// In other words, remove all elements `e` such that `f(&e)` returns `false`. | |
310 | /// | |
311 | /// # Examples | |
312 | /// | |
313 | /// ``` | |
314 | /// use hashbrown::HashSet; | |
315 | /// | |
316 | /// let xs = [1,2,3,4,5,6]; | |
317 | /// let mut set: HashSet<i32> = xs.iter().cloned().collect(); | |
318 | /// set.retain(|&k| k % 2 == 0); | |
319 | /// assert_eq!(set.len(), 3); | |
320 | /// ``` | |
321 | pub fn retain<F>(&mut self, mut f: F) | |
322 | where | |
323 | F: FnMut(&T) -> bool, | |
324 | { | |
325 | self.map.retain(|k, _| f(k)); | |
326 | } | |
327 | ||
328 | /// Drains elements which are true under the given predicate, | |
329 | /// and returns an iterator over the removed items. | |
330 | /// | |
331 | /// In other words, move all elements `e` such that `f(&e)` returns `true` out | |
332 | /// into another iterator. | |
333 | /// | |
334 | /// When the returned DrainedFilter is dropped, any remaining elements that satisfy | |
335 | /// the predicate are dropped from the set. | |
336 | /// | |
337 | /// # Examples | |
338 | /// | |
339 | /// ``` | |
340 | /// use hashbrown::HashSet; | |
341 | /// | |
342 | /// let mut set: HashSet<i32> = (0..8).collect(); | |
343 | /// let drained: HashSet<i32> = set.drain_filter(|v| v % 2 == 0).collect(); | |
344 | /// | |
345 | /// let mut evens = drained.into_iter().collect::<Vec<_>>(); | |
346 | /// let mut odds = set.into_iter().collect::<Vec<_>>(); | |
347 | /// evens.sort(); | |
348 | /// odds.sort(); | |
349 | /// | |
350 | /// assert_eq!(evens, vec![0, 2, 4, 6]); | |
351 | /// assert_eq!(odds, vec![1, 3, 5, 7]); | |
352 | /// ``` | |
353 | #[cfg_attr(feature = "inline-more", inline)] | |
354 | pub fn drain_filter<F>(&mut self, f: F) -> DrainFilter<'_, T, F, A> | |
355 | where | |
356 | F: FnMut(&T) -> bool, | |
357 | { | |
358 | DrainFilter { | |
359 | f, | |
360 | inner: DrainFilterInner { | |
361 | iter: unsafe { self.map.table.iter() }, | |
362 | table: &mut self.map.table, | |
363 | }, | |
364 | } | |
365 | } | |
366 | ||
367 | /// Clears the set, removing all values. | |
368 | /// | |
369 | /// # Examples | |
370 | /// | |
371 | /// ``` | |
372 | /// use hashbrown::HashSet; | |
373 | /// | |
374 | /// let mut v = HashSet::new(); | |
375 | /// v.insert(1); | |
376 | /// v.clear(); | |
377 | /// assert!(v.is_empty()); | |
378 | /// ``` | |
379 | #[cfg_attr(feature = "inline-more", inline)] | |
380 | pub fn clear(&mut self) { | |
381 | self.map.clear() | |
382 | } | |
383 | } | |
384 | ||
385 | impl<T, S> HashSet<T, S, Global> { | |
386 | /// Creates a new empty hash set which will use the given hasher to hash | |
387 | /// keys. | |
388 | /// | |
389 | /// The hash set is also created with the default initial capacity. | |
390 | /// | |
391 | /// Warning: `hasher` is normally randomly generated, and | |
392 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
393 | /// cause many collisions and very poor performance. Setting it | |
394 | /// manually using this function can expose a DoS attack vector. | |
395 | /// | |
396 | /// The `hash_builder` passed should implement the [`BuildHasher`] trait for | |
397 | /// the HashMap to be useful, see its documentation for details. | |
398 | /// | |
399 | /// | |
400 | /// # Examples | |
401 | /// | |
402 | /// ``` | |
403 | /// use hashbrown::HashSet; | |
404 | /// use hashbrown::hash_map::DefaultHashBuilder; | |
405 | /// | |
406 | /// let s = DefaultHashBuilder::default(); | |
407 | /// let mut set = HashSet::with_hasher(s); | |
408 | /// set.insert(2); | |
409 | /// ``` | |
410 | /// | |
411 | /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html | |
412 | #[cfg_attr(feature = "inline-more", inline)] | |
413 | pub const fn with_hasher(hasher: S) -> Self { | |
414 | Self { | |
415 | map: HashMap::with_hasher(hasher), | |
416 | } | |
417 | } | |
418 | ||
419 | /// Creates an empty `HashSet` with the specified capacity, using | |
420 | /// `hasher` to hash the keys. | |
421 | /// | |
422 | /// The hash set will be able to hold at least `capacity` elements without | |
423 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
424 | /// | |
425 | /// Warning: `hasher` is normally randomly generated, and | |
426 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
427 | /// cause many collisions and very poor performance. Setting it | |
428 | /// manually using this function can expose a DoS attack vector. | |
429 | /// | |
430 | /// The `hash_builder` passed should implement the [`BuildHasher`] trait for | |
431 | /// the HashMap to be useful, see its documentation for details. | |
432 | /// | |
433 | /// # Examples | |
434 | /// | |
435 | /// ``` | |
436 | /// use hashbrown::HashSet; | |
437 | /// use hashbrown::hash_map::DefaultHashBuilder; | |
438 | /// | |
439 | /// let s = DefaultHashBuilder::default(); | |
440 | /// let mut set = HashSet::with_capacity_and_hasher(10, s); | |
441 | /// set.insert(1); | |
442 | /// ``` | |
443 | /// | |
444 | /// [`BuildHasher`]: ../../std/hash/trait.BuildHasher.html | |
445 | #[cfg_attr(feature = "inline-more", inline)] | |
446 | pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self { | |
447 | Self { | |
448 | map: HashMap::with_capacity_and_hasher(capacity, hasher), | |
449 | } | |
450 | } | |
451 | } | |
452 | ||
453 | impl<T, S, A> HashSet<T, S, A> | |
454 | where | |
455 | A: Allocator + Clone, | |
456 | { | |
457 | /// Creates a new empty hash set which will use the given hasher to hash | |
458 | /// keys. | |
459 | /// | |
460 | /// The hash set is also created with the default initial capacity. | |
461 | /// | |
462 | /// Warning: `hasher` is normally randomly generated, and | |
463 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
464 | /// cause many collisions and very poor performance. Setting it | |
465 | /// manually using this function can expose a DoS attack vector. | |
466 | /// | |
467 | /// # Examples | |
468 | /// | |
469 | /// ``` | |
470 | /// use hashbrown::HashSet; | |
471 | /// use hashbrown::hash_map::DefaultHashBuilder; | |
472 | /// | |
473 | /// let s = DefaultHashBuilder::default(); | |
474 | /// let mut set = HashSet::with_hasher(s); | |
475 | /// set.insert(2); | |
476 | /// ``` | |
477 | #[cfg_attr(feature = "inline-more", inline)] | |
478 | pub fn with_hasher_in(hasher: S, alloc: A) -> Self { | |
479 | Self { | |
480 | map: HashMap::with_hasher_in(hasher, alloc), | |
481 | } | |
482 | } | |
483 | ||
484 | /// Creates an empty `HashSet` with the specified capacity, using | |
485 | /// `hasher` to hash the keys. | |
486 | /// | |
487 | /// The hash set will be able to hold at least `capacity` elements without | |
488 | /// reallocating. If `capacity` is 0, the hash set will not allocate. | |
489 | /// | |
490 | /// Warning: `hasher` is normally randomly generated, and | |
491 | /// is designed to allow `HashSet`s to be resistant to attacks that | |
492 | /// cause many collisions and very poor performance. Setting it | |
493 | /// manually using this function can expose a DoS attack vector. | |
494 | /// | |
495 | /// # Examples | |
496 | /// | |
497 | /// ``` | |
498 | /// use hashbrown::HashSet; | |
499 | /// use hashbrown::hash_map::DefaultHashBuilder; | |
500 | /// | |
501 | /// let s = DefaultHashBuilder::default(); | |
502 | /// let mut set = HashSet::with_capacity_and_hasher(10, s); | |
503 | /// set.insert(1); | |
504 | /// ``` | |
505 | #[cfg_attr(feature = "inline-more", inline)] | |
506 | pub fn with_capacity_and_hasher_in(capacity: usize, hasher: S, alloc: A) -> Self { | |
507 | Self { | |
508 | map: HashMap::with_capacity_and_hasher_in(capacity, hasher, alloc), | |
509 | } | |
510 | } | |
511 | ||
512 | /// Returns a reference to the set's [`BuildHasher`]. | |
513 | /// | |
514 | /// [`BuildHasher`]: https://doc.rust-lang.org/std/hash/trait.BuildHasher.html | |
515 | /// | |
516 | /// # Examples | |
517 | /// | |
518 | /// ``` | |
519 | /// use hashbrown::HashSet; | |
520 | /// use hashbrown::hash_map::DefaultHashBuilder; | |
521 | /// | |
522 | /// let hasher = DefaultHashBuilder::default(); | |
523 | /// let set: HashSet<i32> = HashSet::with_hasher(hasher); | |
524 | /// let hasher: &DefaultHashBuilder = set.hasher(); | |
525 | /// ``` | |
526 | #[cfg_attr(feature = "inline-more", inline)] | |
527 | pub fn hasher(&self) -> &S { | |
528 | self.map.hasher() | |
529 | } | |
530 | } | |
531 | ||
532 | impl<T, S, A> HashSet<T, S, A> | |
533 | where | |
534 | T: Eq + Hash, | |
535 | S: BuildHasher, | |
536 | A: Allocator + Clone, | |
537 | { | |
538 | /// Reserves capacity for at least `additional` more elements to be inserted | |
539 | /// in the `HashSet`. The collection may reserve more space to avoid | |
540 | /// frequent reallocations. | |
541 | /// | |
542 | /// # Panics | |
543 | /// | |
544 | /// Panics if the new allocation size overflows `usize`. | |
545 | /// | |
546 | /// # Examples | |
547 | /// | |
548 | /// ``` | |
549 | /// use hashbrown::HashSet; | |
550 | /// let mut set: HashSet<i32> = HashSet::new(); | |
551 | /// set.reserve(10); | |
552 | /// assert!(set.capacity() >= 10); | |
553 | /// ``` | |
554 | #[cfg_attr(feature = "inline-more", inline)] | |
555 | pub fn reserve(&mut self, additional: usize) { | |
556 | self.map.reserve(additional) | |
557 | } | |
558 | ||
559 | /// Tries to reserve capacity for at least `additional` more elements to be inserted | |
560 | /// in the given `HashSet<K,V>`. The collection may reserve more space to avoid | |
561 | /// frequent reallocations. | |
562 | /// | |
563 | /// # Errors | |
564 | /// | |
565 | /// If the capacity overflows, or the allocator reports a failure, then an error | |
566 | /// is returned. | |
567 | /// | |
568 | /// # Examples | |
569 | /// | |
570 | /// ``` | |
571 | /// use hashbrown::HashSet; | |
572 | /// let mut set: HashSet<i32> = HashSet::new(); | |
573 | /// set.try_reserve(10).expect("why is the test harness OOMing on 10 bytes?"); | |
574 | /// ``` | |
575 | #[cfg_attr(feature = "inline-more", inline)] | |
576 | pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> { | |
577 | self.map.try_reserve(additional) | |
578 | } | |
579 | ||
580 | /// Shrinks the capacity of the set as much as possible. It will drop | |
581 | /// down as much as possible while maintaining the internal rules | |
582 | /// and possibly leaving some space in accordance with the resize policy. | |
583 | /// | |
584 | /// # Examples | |
585 | /// | |
586 | /// ``` | |
587 | /// use hashbrown::HashSet; | |
588 | /// | |
589 | /// let mut set = HashSet::with_capacity(100); | |
590 | /// set.insert(1); | |
591 | /// set.insert(2); | |
592 | /// assert!(set.capacity() >= 100); | |
593 | /// set.shrink_to_fit(); | |
594 | /// assert!(set.capacity() >= 2); | |
595 | /// ``` | |
596 | #[cfg_attr(feature = "inline-more", inline)] | |
597 | pub fn shrink_to_fit(&mut self) { | |
598 | self.map.shrink_to_fit() | |
599 | } | |
600 | ||
601 | /// Shrinks the capacity of the set with a lower limit. It will drop | |
602 | /// down no lower than the supplied limit while maintaining the internal rules | |
603 | /// and possibly leaving some space in accordance with the resize policy. | |
604 | /// | |
605 | /// Panics if the current capacity is smaller than the supplied | |
606 | /// minimum capacity. | |
607 | /// | |
608 | /// # Examples | |
609 | /// | |
610 | /// ``` | |
611 | /// use hashbrown::HashSet; | |
612 | /// | |
613 | /// let mut set = HashSet::with_capacity(100); | |
614 | /// set.insert(1); | |
615 | /// set.insert(2); | |
616 | /// assert!(set.capacity() >= 100); | |
617 | /// set.shrink_to(10); | |
618 | /// assert!(set.capacity() >= 10); | |
619 | /// set.shrink_to(0); | |
620 | /// assert!(set.capacity() >= 2); | |
621 | /// ``` | |
622 | #[cfg_attr(feature = "inline-more", inline)] | |
623 | pub fn shrink_to(&mut self, min_capacity: usize) { | |
624 | self.map.shrink_to(min_capacity) | |
625 | } | |
626 | ||
627 | /// Visits the values representing the difference, | |
628 | /// i.e., the values that are in `self` but not in `other`. | |
629 | /// | |
630 | /// # Examples | |
631 | /// | |
632 | /// ``` | |
633 | /// use hashbrown::HashSet; | |
634 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
635 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
636 | /// | |
637 | /// // Can be seen as `a - b`. | |
638 | /// for x in a.difference(&b) { | |
639 | /// println!("{}", x); // Print 1 | |
640 | /// } | |
641 | /// | |
642 | /// let diff: HashSet<_> = a.difference(&b).collect(); | |
643 | /// assert_eq!(diff, [1].iter().collect()); | |
644 | /// | |
645 | /// // Note that difference is not symmetric, | |
646 | /// // and `b - a` means something else: | |
647 | /// let diff: HashSet<_> = b.difference(&a).collect(); | |
648 | /// assert_eq!(diff, [4].iter().collect()); | |
649 | /// ``` | |
650 | #[cfg_attr(feature = "inline-more", inline)] | |
651 | pub fn difference<'a>(&'a self, other: &'a Self) -> Difference<'a, T, S, A> { | |
652 | Difference { | |
653 | iter: self.iter(), | |
654 | other, | |
655 | } | |
656 | } | |
657 | ||
658 | /// Visits the values representing the symmetric difference, | |
659 | /// i.e., the values that are in `self` or in `other` but not in both. | |
660 | /// | |
661 | /// # Examples | |
662 | /// | |
663 | /// ``` | |
664 | /// use hashbrown::HashSet; | |
665 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
666 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
667 | /// | |
668 | /// // Print 1, 4 in arbitrary order. | |
669 | /// for x in a.symmetric_difference(&b) { | |
670 | /// println!("{}", x); | |
671 | /// } | |
672 | /// | |
673 | /// let diff1: HashSet<_> = a.symmetric_difference(&b).collect(); | |
674 | /// let diff2: HashSet<_> = b.symmetric_difference(&a).collect(); | |
675 | /// | |
676 | /// assert_eq!(diff1, diff2); | |
677 | /// assert_eq!(diff1, [1, 4].iter().collect()); | |
678 | /// ``` | |
679 | #[cfg_attr(feature = "inline-more", inline)] | |
680 | pub fn symmetric_difference<'a>(&'a self, other: &'a Self) -> SymmetricDifference<'a, T, S, A> { | |
681 | SymmetricDifference { | |
682 | iter: self.difference(other).chain(other.difference(self)), | |
683 | } | |
684 | } | |
685 | ||
686 | /// Visits the values representing the intersection, | |
687 | /// i.e., the values that are both in `self` and `other`. | |
688 | /// | |
689 | /// # Examples | |
690 | /// | |
691 | /// ``` | |
692 | /// use hashbrown::HashSet; | |
693 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
694 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
695 | /// | |
696 | /// // Print 2, 3 in arbitrary order. | |
697 | /// for x in a.intersection(&b) { | |
698 | /// println!("{}", x); | |
699 | /// } | |
700 | /// | |
701 | /// let intersection: HashSet<_> = a.intersection(&b).collect(); | |
702 | /// assert_eq!(intersection, [2, 3].iter().collect()); | |
703 | /// ``` | |
704 | #[cfg_attr(feature = "inline-more", inline)] | |
705 | pub fn intersection<'a>(&'a self, other: &'a Self) -> Intersection<'a, T, S, A> { | |
706 | let (smaller, larger) = if self.len() <= other.len() { | |
707 | (self, other) | |
708 | } else { | |
709 | (other, self) | |
710 | }; | |
711 | Intersection { | |
712 | iter: smaller.iter(), | |
713 | other: larger, | |
714 | } | |
715 | } | |
716 | ||
717 | /// Visits the values representing the union, | |
718 | /// i.e., all the values in `self` or `other`, without duplicates. | |
719 | /// | |
720 | /// # Examples | |
721 | /// | |
722 | /// ``` | |
723 | /// use hashbrown::HashSet; | |
724 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
725 | /// let b: HashSet<_> = [4, 2, 3, 4].iter().cloned().collect(); | |
726 | /// | |
727 | /// // Print 1, 2, 3, 4 in arbitrary order. | |
728 | /// for x in a.union(&b) { | |
729 | /// println!("{}", x); | |
730 | /// } | |
731 | /// | |
732 | /// let union: HashSet<_> = a.union(&b).collect(); | |
733 | /// assert_eq!(union, [1, 2, 3, 4].iter().collect()); | |
734 | /// ``` | |
735 | #[cfg_attr(feature = "inline-more", inline)] | |
736 | pub fn union<'a>(&'a self, other: &'a Self) -> Union<'a, T, S, A> { | |
737 | // We'll iterate one set in full, and only the remaining difference from the other. | |
738 | // Use the smaller set for the difference in order to reduce hash lookups. | |
739 | let (smaller, larger) = if self.len() <= other.len() { | |
740 | (self, other) | |
741 | } else { | |
742 | (other, self) | |
743 | }; | |
744 | Union { | |
745 | iter: larger.iter().chain(smaller.difference(larger)), | |
746 | } | |
747 | } | |
748 | ||
749 | /// Returns `true` if the set contains a value. | |
750 | /// | |
751 | /// The value may be any borrowed form of the set's value type, but | |
752 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for | |
753 | /// the value type. | |
754 | /// | |
755 | /// # Examples | |
756 | /// | |
757 | /// ``` | |
758 | /// use hashbrown::HashSet; | |
759 | /// | |
760 | /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
761 | /// assert_eq!(set.contains(&1), true); | |
762 | /// assert_eq!(set.contains(&4), false); | |
763 | /// ``` | |
764 | /// | |
765 | /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html | |
766 | /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html | |
767 | #[cfg_attr(feature = "inline-more", inline)] | |
768 | pub fn contains<Q: ?Sized>(&self, value: &Q) -> bool | |
769 | where | |
770 | T: Borrow<Q>, | |
771 | Q: Hash + Eq, | |
772 | { | |
773 | self.map.contains_key(value) | |
774 | } | |
775 | ||
776 | /// Returns a reference to the value in the set, if any, that is equal to the given value. | |
777 | /// | |
778 | /// The value may be any borrowed form of the set's value type, but | |
779 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for | |
780 | /// the value type. | |
781 | /// | |
782 | /// # Examples | |
783 | /// | |
784 | /// ``` | |
785 | /// use hashbrown::HashSet; | |
786 | /// | |
787 | /// let set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
788 | /// assert_eq!(set.get(&2), Some(&2)); | |
789 | /// assert_eq!(set.get(&4), None); | |
790 | /// ``` | |
791 | /// | |
792 | /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html | |
793 | /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html | |
794 | #[cfg_attr(feature = "inline-more", inline)] | |
795 | pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T> | |
796 | where | |
797 | T: Borrow<Q>, | |
798 | Q: Hash + Eq, | |
799 | { | |
800 | // Avoid `Option::map` because it bloats LLVM IR. | |
801 | match self.map.get_key_value(value) { | |
802 | Some((k, _)) => Some(k), | |
803 | None => None, | |
804 | } | |
805 | } | |
806 | ||
807 | /// Inserts the given `value` into the set if it is not present, then | |
808 | /// returns a reference to the value in the set. | |
809 | /// | |
810 | /// # Examples | |
811 | /// | |
812 | /// ``` | |
813 | /// use hashbrown::HashSet; | |
814 | /// | |
815 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
816 | /// assert_eq!(set.len(), 3); | |
817 | /// assert_eq!(set.get_or_insert(2), &2); | |
818 | /// assert_eq!(set.get_or_insert(100), &100); | |
819 | /// assert_eq!(set.len(), 4); // 100 was inserted | |
820 | /// ``` | |
821 | #[cfg_attr(feature = "inline-more", inline)] | |
822 | pub fn get_or_insert(&mut self, value: T) -> &T { | |
823 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
824 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
825 | self.map | |
826 | .raw_entry_mut() | |
827 | .from_key(&value) | |
828 | .or_insert(value, ()) | |
829 | .0 | |
830 | } | |
831 | ||
832 | /// Inserts an owned copy of the given `value` into the set if it is not | |
833 | /// present, then returns a reference to the value in the set. | |
834 | /// | |
835 | /// # Examples | |
836 | /// | |
837 | /// ``` | |
838 | /// use hashbrown::HashSet; | |
839 | /// | |
840 | /// let mut set: HashSet<String> = ["cat", "dog", "horse"] | |
841 | /// .iter().map(|&pet| pet.to_owned()).collect(); | |
842 | /// | |
843 | /// assert_eq!(set.len(), 3); | |
844 | /// for &pet in &["cat", "dog", "fish"] { | |
845 | /// let value = set.get_or_insert_owned(pet); | |
846 | /// assert_eq!(value, pet); | |
847 | /// } | |
848 | /// assert_eq!(set.len(), 4); // a new "fish" was inserted | |
849 | /// ``` | |
850 | #[inline] | |
851 | pub fn get_or_insert_owned<Q: ?Sized>(&mut self, value: &Q) -> &T | |
852 | where | |
853 | T: Borrow<Q>, | |
854 | Q: Hash + Eq + ToOwned<Owned = T>, | |
855 | { | |
856 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
857 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
858 | self.map | |
859 | .raw_entry_mut() | |
860 | .from_key(value) | |
861 | .or_insert_with(|| (value.to_owned(), ())) | |
862 | .0 | |
863 | } | |
864 | ||
865 | /// Inserts a value computed from `f` into the set if the given `value` is | |
866 | /// not present, then returns a reference to the value in the set. | |
867 | /// | |
868 | /// # Examples | |
869 | /// | |
870 | /// ``` | |
871 | /// use hashbrown::HashSet; | |
872 | /// | |
873 | /// let mut set: HashSet<String> = ["cat", "dog", "horse"] | |
874 | /// .iter().map(|&pet| pet.to_owned()).collect(); | |
875 | /// | |
876 | /// assert_eq!(set.len(), 3); | |
877 | /// for &pet in &["cat", "dog", "fish"] { | |
878 | /// let value = set.get_or_insert_with(pet, str::to_owned); | |
879 | /// assert_eq!(value, pet); | |
880 | /// } | |
881 | /// assert_eq!(set.len(), 4); // a new "fish" was inserted | |
882 | /// ``` | |
883 | #[cfg_attr(feature = "inline-more", inline)] | |
884 | pub fn get_or_insert_with<Q: ?Sized, F>(&mut self, value: &Q, f: F) -> &T | |
885 | where | |
886 | T: Borrow<Q>, | |
887 | Q: Hash + Eq, | |
888 | F: FnOnce(&Q) -> T, | |
889 | { | |
890 | // Although the raw entry gives us `&mut T`, we only return `&T` to be consistent with | |
891 | // `get`. Key mutation is "raw" because you're not supposed to affect `Eq` or `Hash`. | |
892 | self.map | |
893 | .raw_entry_mut() | |
894 | .from_key(value) | |
895 | .or_insert_with(|| (f(value), ())) | |
896 | .0 | |
897 | } | |
898 | ||
899 | /// Returns `true` if `self` has no elements in common with `other`. | |
900 | /// This is equivalent to checking for an empty intersection. | |
901 | /// | |
902 | /// # Examples | |
903 | /// | |
904 | /// ``` | |
905 | /// use hashbrown::HashSet; | |
906 | /// | |
907 | /// let a: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
908 | /// let mut b = HashSet::new(); | |
909 | /// | |
910 | /// assert_eq!(a.is_disjoint(&b), true); | |
911 | /// b.insert(4); | |
912 | /// assert_eq!(a.is_disjoint(&b), true); | |
913 | /// b.insert(1); | |
914 | /// assert_eq!(a.is_disjoint(&b), false); | |
915 | /// ``` | |
916 | pub fn is_disjoint(&self, other: &Self) -> bool { | |
917 | self.iter().all(|v| !other.contains(v)) | |
918 | } | |
919 | ||
920 | /// Returns `true` if the set is a subset of another, | |
921 | /// i.e., `other` contains at least all the values in `self`. | |
922 | /// | |
923 | /// # Examples | |
924 | /// | |
925 | /// ``` | |
926 | /// use hashbrown::HashSet; | |
927 | /// | |
928 | /// let sup: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
929 | /// let mut set = HashSet::new(); | |
930 | /// | |
931 | /// assert_eq!(set.is_subset(&sup), true); | |
932 | /// set.insert(2); | |
933 | /// assert_eq!(set.is_subset(&sup), true); | |
934 | /// set.insert(4); | |
935 | /// assert_eq!(set.is_subset(&sup), false); | |
936 | /// ``` | |
937 | pub fn is_subset(&self, other: &Self) -> bool { | |
938 | self.len() <= other.len() && self.iter().all(|v| other.contains(v)) | |
939 | } | |
940 | ||
941 | /// Returns `true` if the set is a superset of another, | |
942 | /// i.e., `self` contains at least all the values in `other`. | |
943 | /// | |
944 | /// # Examples | |
945 | /// | |
946 | /// ``` | |
947 | /// use hashbrown::HashSet; | |
948 | /// | |
949 | /// let sub: HashSet<_> = [1, 2].iter().cloned().collect(); | |
950 | /// let mut set = HashSet::new(); | |
951 | /// | |
952 | /// assert_eq!(set.is_superset(&sub), false); | |
953 | /// | |
954 | /// set.insert(0); | |
955 | /// set.insert(1); | |
956 | /// assert_eq!(set.is_superset(&sub), false); | |
957 | /// | |
958 | /// set.insert(2); | |
959 | /// assert_eq!(set.is_superset(&sub), true); | |
960 | /// ``` | |
961 | #[cfg_attr(feature = "inline-more", inline)] | |
962 | pub fn is_superset(&self, other: &Self) -> bool { | |
963 | other.is_subset(self) | |
964 | } | |
965 | ||
966 | /// Adds a value to the set. | |
967 | /// | |
968 | /// If the set did not have this value present, `true` is returned. | |
969 | /// | |
970 | /// If the set did have this value present, `false` is returned. | |
971 | /// | |
972 | /// # Examples | |
973 | /// | |
974 | /// ``` | |
975 | /// use hashbrown::HashSet; | |
976 | /// | |
977 | /// let mut set = HashSet::new(); | |
978 | /// | |
979 | /// assert_eq!(set.insert(2), true); | |
980 | /// assert_eq!(set.insert(2), false); | |
981 | /// assert_eq!(set.len(), 1); | |
982 | /// ``` | |
983 | #[cfg_attr(feature = "inline-more", inline)] | |
984 | pub fn insert(&mut self, value: T) -> bool { | |
985 | self.map.insert(value, ()).is_none() | |
986 | } | |
987 | ||
988 | /// Adds a value to the set, replacing the existing value, if any, that is equal to the given | |
989 | /// one. Returns the replaced value. | |
990 | /// | |
991 | /// # Examples | |
992 | /// | |
993 | /// ``` | |
994 | /// use hashbrown::HashSet; | |
995 | /// | |
996 | /// let mut set = HashSet::new(); | |
997 | /// set.insert(Vec::<i32>::new()); | |
998 | /// | |
999 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 0); | |
1000 | /// set.replace(Vec::with_capacity(10)); | |
1001 | /// assert_eq!(set.get(&[][..]).unwrap().capacity(), 10); | |
1002 | /// ``` | |
1003 | #[cfg_attr(feature = "inline-more", inline)] | |
1004 | pub fn replace(&mut self, value: T) -> Option<T> { | |
1005 | match self.map.entry(value) { | |
1006 | map::Entry::Occupied(occupied) => Some(occupied.replace_key()), | |
1007 | map::Entry::Vacant(vacant) => { | |
1008 | vacant.insert(()); | |
1009 | None | |
1010 | } | |
1011 | } | |
1012 | } | |
1013 | ||
1014 | /// Removes a value from the set. Returns whether the value was | |
1015 | /// present in the set. | |
1016 | /// | |
1017 | /// The value may be any borrowed form of the set's value type, but | |
1018 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for | |
1019 | /// the value type. | |
1020 | /// | |
1021 | /// # Examples | |
1022 | /// | |
1023 | /// ``` | |
1024 | /// use hashbrown::HashSet; | |
1025 | /// | |
1026 | /// let mut set = HashSet::new(); | |
1027 | /// | |
1028 | /// set.insert(2); | |
1029 | /// assert_eq!(set.remove(&2), true); | |
1030 | /// assert_eq!(set.remove(&2), false); | |
1031 | /// ``` | |
1032 | /// | |
1033 | /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html | |
1034 | /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html | |
1035 | #[cfg_attr(feature = "inline-more", inline)] | |
1036 | pub fn remove<Q: ?Sized>(&mut self, value: &Q) -> bool | |
1037 | where | |
1038 | T: Borrow<Q>, | |
1039 | Q: Hash + Eq, | |
1040 | { | |
1041 | self.map.remove(value).is_some() | |
1042 | } | |
1043 | ||
1044 | /// Removes and returns the value in the set, if any, that is equal to the given one. | |
1045 | /// | |
1046 | /// The value may be any borrowed form of the set's value type, but | |
1047 | /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for | |
1048 | /// the value type. | |
1049 | /// | |
1050 | /// # Examples | |
1051 | /// | |
1052 | /// ``` | |
1053 | /// use hashbrown::HashSet; | |
1054 | /// | |
1055 | /// let mut set: HashSet<_> = [1, 2, 3].iter().cloned().collect(); | |
1056 | /// assert_eq!(set.take(&2), Some(2)); | |
1057 | /// assert_eq!(set.take(&2), None); | |
1058 | /// ``` | |
1059 | /// | |
1060 | /// [`Eq`]: https://doc.rust-lang.org/std/cmp/trait.Eq.html | |
1061 | /// [`Hash`]: https://doc.rust-lang.org/std/hash/trait.Hash.html | |
1062 | #[cfg_attr(feature = "inline-more", inline)] | |
1063 | pub fn take<Q: ?Sized>(&mut self, value: &Q) -> Option<T> | |
1064 | where | |
1065 | T: Borrow<Q>, | |
1066 | Q: Hash + Eq, | |
1067 | { | |
1068 | // Avoid `Option::map` because it bloats LLVM IR. | |
1069 | match self.map.remove_entry(value) { | |
1070 | Some((k, _)) => Some(k), | |
1071 | None => None, | |
1072 | } | |
1073 | } | |
1074 | } | |
1075 | ||
1076 | impl<T, S, A> PartialEq for HashSet<T, S, A> | |
1077 | where | |
1078 | T: Eq + Hash, | |
1079 | S: BuildHasher, | |
1080 | A: Allocator + Clone, | |
1081 | { | |
1082 | fn eq(&self, other: &Self) -> bool { | |
1083 | if self.len() != other.len() { | |
1084 | return false; | |
1085 | } | |
1086 | ||
1087 | self.iter().all(|key| other.contains(key)) | |
1088 | } | |
1089 | } | |
1090 | ||
1091 | impl<T, S, A> Eq for HashSet<T, S, A> | |
1092 | where | |
1093 | T: Eq + Hash, | |
1094 | S: BuildHasher, | |
1095 | A: Allocator + Clone, | |
1096 | { | |
1097 | } | |
1098 | ||
1099 | impl<T, S, A> fmt::Debug for HashSet<T, S, A> | |
1100 | where | |
1101 | T: Eq + Hash + fmt::Debug, | |
1102 | S: BuildHasher, | |
1103 | A: Allocator + Clone, | |
1104 | { | |
1105 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1106 | f.debug_set().entries(self.iter()).finish() | |
1107 | } | |
1108 | } | |
1109 | ||
1110 | impl<T, S, A> From<HashMap<T, (), S, A>> for HashSet<T, S, A> | |
1111 | where | |
1112 | A: Allocator + Clone, | |
1113 | { | |
1114 | fn from(map: HashMap<T, (), S, A>) -> Self { | |
1115 | Self { map } | |
1116 | } | |
1117 | } | |
1118 | ||
1119 | impl<T, S, A> FromIterator<T> for HashSet<T, S, A> | |
1120 | where | |
1121 | T: Eq + Hash, | |
1122 | S: BuildHasher + Default, | |
1123 | A: Default + Allocator + Clone, | |
1124 | { | |
1125 | #[cfg_attr(feature = "inline-more", inline)] | |
1126 | fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { | |
1127 | let mut set = Self::with_hasher_in(Default::default(), Default::default()); | |
1128 | set.extend(iter); | |
1129 | set | |
1130 | } | |
1131 | } | |
1132 | ||
1133 | impl<T, S, A> Extend<T> for HashSet<T, S, A> | |
1134 | where | |
1135 | T: Eq + Hash, | |
1136 | S: BuildHasher, | |
1137 | A: Allocator + Clone, | |
1138 | { | |
1139 | #[cfg_attr(feature = "inline-more", inline)] | |
1140 | fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) { | |
1141 | self.map.extend(iter.into_iter().map(|k| (k, ()))); | |
1142 | } | |
1143 | ||
1144 | #[inline] | |
1145 | #[cfg(feature = "nightly")] | |
1146 | fn extend_one(&mut self, k: T) { | |
1147 | self.map.insert(k, ()); | |
1148 | } | |
1149 | ||
1150 | #[inline] | |
1151 | #[cfg(feature = "nightly")] | |
1152 | fn extend_reserve(&mut self, additional: usize) { | |
1153 | Extend::<(T, ())>::extend_reserve(&mut self.map, additional); | |
1154 | } | |
1155 | } | |
1156 | ||
1157 | impl<'a, T, S, A> Extend<&'a T> for HashSet<T, S, A> | |
1158 | where | |
1159 | T: 'a + Eq + Hash + Copy, | |
1160 | S: BuildHasher, | |
1161 | A: Allocator + Clone, | |
1162 | { | |
1163 | #[cfg_attr(feature = "inline-more", inline)] | |
1164 | fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I) { | |
1165 | self.extend(iter.into_iter().cloned()); | |
1166 | } | |
1167 | ||
1168 | #[inline] | |
1169 | #[cfg(feature = "nightly")] | |
1170 | fn extend_one(&mut self, k: &'a T) { | |
1171 | self.map.insert(*k, ()); | |
1172 | } | |
1173 | ||
1174 | #[inline] | |
1175 | #[cfg(feature = "nightly")] | |
1176 | fn extend_reserve(&mut self, additional: usize) { | |
1177 | Extend::<(T, ())>::extend_reserve(&mut self.map, additional); | |
1178 | } | |
1179 | } | |
1180 | ||
1181 | impl<T, S, A> Default for HashSet<T, S, A> | |
1182 | where | |
1183 | S: Default, | |
1184 | A: Default + Allocator + Clone, | |
1185 | { | |
1186 | /// Creates an empty `HashSet<T, S>` with the `Default` value for the hasher. | |
1187 | #[cfg_attr(feature = "inline-more", inline)] | |
1188 | fn default() -> Self { | |
1189 | Self { | |
1190 | map: HashMap::default(), | |
1191 | } | |
1192 | } | |
1193 | } | |
1194 | ||
1195 | impl<T, S, A> BitOr<&HashSet<T, S, A>> for &HashSet<T, S, A> | |
1196 | where | |
1197 | T: Eq + Hash + Clone, | |
1198 | S: BuildHasher + Default, | |
1199 | A: Allocator + Clone, | |
1200 | { | |
1201 | type Output = HashSet<T, S>; | |
1202 | ||
1203 | /// Returns the union of `self` and `rhs` as a new `HashSet<T, S>`. | |
1204 | /// | |
1205 | /// # Examples | |
1206 | /// | |
1207 | /// ``` | |
1208 | /// use hashbrown::HashSet; | |
1209 | /// | |
1210 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | |
1211 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1212 | /// | |
1213 | /// let set = &a | &b; | |
1214 | /// | |
1215 | /// let mut i = 0; | |
1216 | /// let expected = [1, 2, 3, 4, 5]; | |
1217 | /// for x in &set { | |
1218 | /// assert!(expected.contains(x)); | |
1219 | /// i += 1; | |
1220 | /// } | |
1221 | /// assert_eq!(i, expected.len()); | |
1222 | /// ``` | |
1223 | fn bitor(self, rhs: &HashSet<T, S, A>) -> HashSet<T, S> { | |
1224 | self.union(rhs).cloned().collect() | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | impl<T, S, A> BitAnd<&HashSet<T, S, A>> for &HashSet<T, S, A> | |
1229 | where | |
1230 | T: Eq + Hash + Clone, | |
1231 | S: BuildHasher + Default, | |
1232 | A: Allocator + Clone, | |
1233 | { | |
1234 | type Output = HashSet<T, S>; | |
1235 | ||
1236 | /// Returns the intersection of `self` and `rhs` as a new `HashSet<T, S>`. | |
1237 | /// | |
1238 | /// # Examples | |
1239 | /// | |
1240 | /// ``` | |
1241 | /// use hashbrown::HashSet; | |
1242 | /// | |
1243 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | |
1244 | /// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect(); | |
1245 | /// | |
1246 | /// let set = &a & &b; | |
1247 | /// | |
1248 | /// let mut i = 0; | |
1249 | /// let expected = [2, 3]; | |
1250 | /// for x in &set { | |
1251 | /// assert!(expected.contains(x)); | |
1252 | /// i += 1; | |
1253 | /// } | |
1254 | /// assert_eq!(i, expected.len()); | |
1255 | /// ``` | |
1256 | fn bitand(self, rhs: &HashSet<T, S, A>) -> HashSet<T, S> { | |
1257 | self.intersection(rhs).cloned().collect() | |
1258 | } | |
1259 | } | |
1260 | ||
1261 | impl<T, S> BitXor<&HashSet<T, S>> for &HashSet<T, S> | |
1262 | where | |
1263 | T: Eq + Hash + Clone, | |
1264 | S: BuildHasher + Default, | |
1265 | { | |
1266 | type Output = HashSet<T, S>; | |
1267 | ||
1268 | /// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, S>`. | |
1269 | /// | |
1270 | /// # Examples | |
1271 | /// | |
1272 | /// ``` | |
1273 | /// use hashbrown::HashSet; | |
1274 | /// | |
1275 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | |
1276 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1277 | /// | |
1278 | /// let set = &a ^ &b; | |
1279 | /// | |
1280 | /// let mut i = 0; | |
1281 | /// let expected = [1, 2, 4, 5]; | |
1282 | /// for x in &set { | |
1283 | /// assert!(expected.contains(x)); | |
1284 | /// i += 1; | |
1285 | /// } | |
1286 | /// assert_eq!(i, expected.len()); | |
1287 | /// ``` | |
1288 | fn bitxor(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1289 | self.symmetric_difference(rhs).cloned().collect() | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | impl<T, S> Sub<&HashSet<T, S>> for &HashSet<T, S> | |
1294 | where | |
1295 | T: Eq + Hash + Clone, | |
1296 | S: BuildHasher + Default, | |
1297 | { | |
1298 | type Output = HashSet<T, S>; | |
1299 | ||
1300 | /// Returns the difference of `self` and `rhs` as a new `HashSet<T, S>`. | |
1301 | /// | |
1302 | /// # Examples | |
1303 | /// | |
1304 | /// ``` | |
1305 | /// use hashbrown::HashSet; | |
1306 | /// | |
1307 | /// let a: HashSet<_> = vec![1, 2, 3].into_iter().collect(); | |
1308 | /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect(); | |
1309 | /// | |
1310 | /// let set = &a - &b; | |
1311 | /// | |
1312 | /// let mut i = 0; | |
1313 | /// let expected = [1, 2]; | |
1314 | /// for x in &set { | |
1315 | /// assert!(expected.contains(x)); | |
1316 | /// i += 1; | |
1317 | /// } | |
1318 | /// assert_eq!(i, expected.len()); | |
1319 | /// ``` | |
1320 | fn sub(self, rhs: &HashSet<T, S>) -> HashSet<T, S> { | |
1321 | self.difference(rhs).cloned().collect() | |
1322 | } | |
1323 | } | |
1324 | ||
1325 | /// An iterator over the items of a `HashSet`. | |
1326 | /// | |
1327 | /// This `struct` is created by the [`iter`] method on [`HashSet`]. | |
1328 | /// See its documentation for more. | |
1329 | /// | |
1330 | /// [`HashSet`]: struct.HashSet.html | |
1331 | /// [`iter`]: struct.HashSet.html#method.iter | |
1332 | pub struct Iter<'a, K> { | |
1333 | iter: Keys<'a, K, ()>, | |
1334 | } | |
1335 | ||
1336 | /// An owning iterator over the items of a `HashSet`. | |
1337 | /// | |
1338 | /// This `struct` is created by the [`into_iter`] method on [`HashSet`] | |
1339 | /// (provided by the `IntoIterator` trait). See its documentation for more. | |
1340 | /// | |
1341 | /// [`HashSet`]: struct.HashSet.html | |
1342 | /// [`into_iter`]: struct.HashSet.html#method.into_iter | |
1343 | pub struct IntoIter<K, A: Allocator + Clone = Global> { | |
1344 | iter: map::IntoIter<K, (), A>, | |
1345 | } | |
1346 | ||
1347 | /// A draining iterator over the items of a `HashSet`. | |
1348 | /// | |
1349 | /// This `struct` is created by the [`drain`] method on [`HashSet`]. | |
1350 | /// See its documentation for more. | |
1351 | /// | |
1352 | /// [`HashSet`]: struct.HashSet.html | |
1353 | /// [`drain`]: struct.HashSet.html#method.drain | |
1354 | pub struct Drain<'a, K, A: Allocator + Clone = Global> { | |
1355 | iter: map::Drain<'a, K, (), A>, | |
1356 | } | |
1357 | ||
1358 | /// A draining iterator over entries of a `HashSet` which don't satisfy the predicate `f`. | |
1359 | /// | |
1360 | /// This `struct` is created by the [`drain_filter`] method on [`HashSet`]. See its | |
1361 | /// documentation for more. | |
1362 | /// | |
1363 | /// [`drain_filter`]: struct.HashSet.html#method.drain_filter | |
1364 | /// [`HashSet`]: struct.HashSet.html | |
1365 | pub struct DrainFilter<'a, K, F, A: Allocator + Clone = Global> | |
1366 | where | |
1367 | F: FnMut(&K) -> bool, | |
1368 | { | |
1369 | f: F, | |
1370 | inner: DrainFilterInner<'a, K, (), A>, | |
1371 | } | |
1372 | ||
1373 | /// A lazy iterator producing elements in the intersection of `HashSet`s. | |
1374 | /// | |
1375 | /// This `struct` is created by the [`intersection`] method on [`HashSet`]. | |
1376 | /// See its documentation for more. | |
1377 | /// | |
1378 | /// [`HashSet`]: struct.HashSet.html | |
1379 | /// [`intersection`]: struct.HashSet.html#method.intersection | |
1380 | pub struct Intersection<'a, T, S, A: Allocator + Clone = Global> { | |
1381 | // iterator of the first set | |
1382 | iter: Iter<'a, T>, | |
1383 | // the second set | |
1384 | other: &'a HashSet<T, S, A>, | |
1385 | } | |
1386 | ||
1387 | /// A lazy iterator producing elements in the difference of `HashSet`s. | |
1388 | /// | |
1389 | /// This `struct` is created by the [`difference`] method on [`HashSet`]. | |
1390 | /// See its documentation for more. | |
1391 | /// | |
1392 | /// [`HashSet`]: struct.HashSet.html | |
1393 | /// [`difference`]: struct.HashSet.html#method.difference | |
1394 | pub struct Difference<'a, T, S, A: Allocator + Clone = Global> { | |
1395 | // iterator of the first set | |
1396 | iter: Iter<'a, T>, | |
1397 | // the second set | |
1398 | other: &'a HashSet<T, S, A>, | |
1399 | } | |
1400 | ||
1401 | /// A lazy iterator producing elements in the symmetric difference of `HashSet`s. | |
1402 | /// | |
1403 | /// This `struct` is created by the [`symmetric_difference`] method on | |
1404 | /// [`HashSet`]. See its documentation for more. | |
1405 | /// | |
1406 | /// [`HashSet`]: struct.HashSet.html | |
1407 | /// [`symmetric_difference`]: struct.HashSet.html#method.symmetric_difference | |
1408 | pub struct SymmetricDifference<'a, T, S, A: Allocator + Clone = Global> { | |
1409 | iter: Chain<Difference<'a, T, S, A>, Difference<'a, T, S, A>>, | |
1410 | } | |
1411 | ||
1412 | /// A lazy iterator producing elements in the union of `HashSet`s. | |
1413 | /// | |
1414 | /// This `struct` is created by the [`union`] method on [`HashSet`]. | |
1415 | /// See its documentation for more. | |
1416 | /// | |
1417 | /// [`HashSet`]: struct.HashSet.html | |
1418 | /// [`union`]: struct.HashSet.html#method.union | |
1419 | pub struct Union<'a, T, S, A: Allocator + Clone = Global> { | |
1420 | iter: Chain<Iter<'a, T>, Difference<'a, T, S, A>>, | |
1421 | } | |
1422 | ||
1423 | impl<'a, T, S, A: Allocator + Clone> IntoIterator for &'a HashSet<T, S, A> { | |
1424 | type Item = &'a T; | |
1425 | type IntoIter = Iter<'a, T>; | |
1426 | ||
1427 | #[cfg_attr(feature = "inline-more", inline)] | |
1428 | fn into_iter(self) -> Iter<'a, T> { | |
1429 | self.iter() | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | impl<T, S, A: Allocator + Clone> IntoIterator for HashSet<T, S, A> { | |
1434 | type Item = T; | |
1435 | type IntoIter = IntoIter<T, A>; | |
1436 | ||
1437 | /// Creates a consuming iterator, that is, one that moves each value out | |
1438 | /// of the set in arbitrary order. The set cannot be used after calling | |
1439 | /// this. | |
1440 | /// | |
1441 | /// # Examples | |
1442 | /// | |
1443 | /// ``` | |
1444 | /// use hashbrown::HashSet; | |
1445 | /// let mut set = HashSet::new(); | |
1446 | /// set.insert("a".to_string()); | |
1447 | /// set.insert("b".to_string()); | |
1448 | /// | |
1449 | /// // Not possible to collect to a Vec<String> with a regular `.iter()`. | |
1450 | /// let v: Vec<String> = set.into_iter().collect(); | |
1451 | /// | |
1452 | /// // Will print in an arbitrary order. | |
1453 | /// for x in &v { | |
1454 | /// println!("{}", x); | |
1455 | /// } | |
1456 | /// ``` | |
1457 | #[cfg_attr(feature = "inline-more", inline)] | |
1458 | fn into_iter(self) -> IntoIter<T, A> { | |
1459 | IntoIter { | |
1460 | iter: self.map.into_iter(), | |
1461 | } | |
1462 | } | |
1463 | } | |
1464 | ||
1465 | impl<K> Clone for Iter<'_, K> { | |
1466 | #[cfg_attr(feature = "inline-more", inline)] | |
1467 | fn clone(&self) -> Self { | |
1468 | Iter { | |
1469 | iter: self.iter.clone(), | |
1470 | } | |
1471 | } | |
1472 | } | |
1473 | impl<'a, K> Iterator for Iter<'a, K> { | |
1474 | type Item = &'a K; | |
1475 | ||
1476 | #[cfg_attr(feature = "inline-more", inline)] | |
1477 | fn next(&mut self) -> Option<&'a K> { | |
1478 | self.iter.next() | |
1479 | } | |
1480 | #[cfg_attr(feature = "inline-more", inline)] | |
1481 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1482 | self.iter.size_hint() | |
1483 | } | |
1484 | } | |
1485 | impl<'a, K> ExactSizeIterator for Iter<'a, K> { | |
1486 | #[cfg_attr(feature = "inline-more", inline)] | |
1487 | fn len(&self) -> usize { | |
1488 | self.iter.len() | |
1489 | } | |
1490 | } | |
1491 | impl<K> FusedIterator for Iter<'_, K> {} | |
1492 | ||
1493 | impl<K: fmt::Debug> fmt::Debug for Iter<'_, K> { | |
1494 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1495 | f.debug_list().entries(self.clone()).finish() | |
1496 | } | |
1497 | } | |
1498 | ||
1499 | impl<K, A: Allocator + Clone> Iterator for IntoIter<K, A> { | |
1500 | type Item = K; | |
1501 | ||
1502 | #[cfg_attr(feature = "inline-more", inline)] | |
1503 | fn next(&mut self) -> Option<K> { | |
1504 | // Avoid `Option::map` because it bloats LLVM IR. | |
1505 | match self.iter.next() { | |
1506 | Some((k, _)) => Some(k), | |
1507 | None => None, | |
1508 | } | |
1509 | } | |
1510 | #[cfg_attr(feature = "inline-more", inline)] | |
1511 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1512 | self.iter.size_hint() | |
1513 | } | |
1514 | } | |
1515 | impl<K, A: Allocator + Clone> ExactSizeIterator for IntoIter<K, A> { | |
1516 | #[cfg_attr(feature = "inline-more", inline)] | |
1517 | fn len(&self) -> usize { | |
1518 | self.iter.len() | |
1519 | } | |
1520 | } | |
1521 | impl<K, A: Allocator + Clone> FusedIterator for IntoIter<K, A> {} | |
1522 | ||
1523 | impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for IntoIter<K, A> { | |
1524 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1525 | let entries_iter = self.iter.iter().map(|(k, _)| k); | |
1526 | f.debug_list().entries(entries_iter).finish() | |
1527 | } | |
1528 | } | |
1529 | ||
1530 | impl<K, A: Allocator + Clone> Iterator for Drain<'_, K, A> { | |
1531 | type Item = K; | |
1532 | ||
1533 | #[cfg_attr(feature = "inline-more", inline)] | |
1534 | fn next(&mut self) -> Option<K> { | |
1535 | // Avoid `Option::map` because it bloats LLVM IR. | |
1536 | match self.iter.next() { | |
1537 | Some((k, _)) => Some(k), | |
1538 | None => None, | |
1539 | } | |
1540 | } | |
1541 | #[cfg_attr(feature = "inline-more", inline)] | |
1542 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1543 | self.iter.size_hint() | |
1544 | } | |
1545 | } | |
1546 | impl<K, A: Allocator + Clone> ExactSizeIterator for Drain<'_, K, A> { | |
1547 | #[cfg_attr(feature = "inline-more", inline)] | |
1548 | fn len(&self) -> usize { | |
1549 | self.iter.len() | |
1550 | } | |
1551 | } | |
1552 | impl<K, A: Allocator + Clone> FusedIterator for Drain<'_, K, A> {} | |
1553 | ||
1554 | impl<K: fmt::Debug, A: Allocator + Clone> fmt::Debug for Drain<'_, K, A> { | |
1555 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1556 | let entries_iter = self.iter.iter().map(|(k, _)| k); | |
1557 | f.debug_list().entries(entries_iter).finish() | |
1558 | } | |
1559 | } | |
1560 | ||
1561 | impl<'a, K, F, A: Allocator + Clone> Drop for DrainFilter<'a, K, F, A> | |
1562 | where | |
1563 | F: FnMut(&K) -> bool, | |
1564 | { | |
1565 | #[cfg_attr(feature = "inline-more", inline)] | |
1566 | fn drop(&mut self) { | |
1567 | while let Some(item) = self.next() { | |
1568 | let guard = ConsumeAllOnDrop(self); | |
1569 | drop(item); | |
1570 | mem::forget(guard); | |
1571 | } | |
1572 | } | |
1573 | } | |
1574 | ||
1575 | impl<K, F, A: Allocator + Clone> Iterator for DrainFilter<'_, K, F, A> | |
1576 | where | |
1577 | F: FnMut(&K) -> bool, | |
1578 | { | |
1579 | type Item = K; | |
1580 | ||
1581 | #[cfg_attr(feature = "inline-more", inline)] | |
1582 | fn next(&mut self) -> Option<Self::Item> { | |
1583 | let f = &mut self.f; | |
1584 | let (k, _) = self.inner.next(&mut |k, _| f(k))?; | |
1585 | Some(k) | |
1586 | } | |
1587 | ||
1588 | #[inline] | |
1589 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1590 | (0, self.inner.iter.size_hint().1) | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | impl<K, F, A: Allocator + Clone> FusedIterator for DrainFilter<'_, K, F, A> where | |
1595 | F: FnMut(&K) -> bool | |
1596 | { | |
1597 | } | |
1598 | ||
1599 | impl<T, S, A: Allocator + Clone> Clone for Intersection<'_, T, S, A> { | |
1600 | #[cfg_attr(feature = "inline-more", inline)] | |
1601 | fn clone(&self) -> Self { | |
1602 | Intersection { | |
1603 | iter: self.iter.clone(), | |
1604 | ..*self | |
1605 | } | |
1606 | } | |
1607 | } | |
1608 | ||
1609 | impl<'a, T, S, A> Iterator for Intersection<'a, T, S, A> | |
1610 | where | |
1611 | T: Eq + Hash, | |
1612 | S: BuildHasher, | |
1613 | A: Allocator + Clone, | |
1614 | { | |
1615 | type Item = &'a T; | |
1616 | ||
1617 | #[cfg_attr(feature = "inline-more", inline)] | |
1618 | fn next(&mut self) -> Option<&'a T> { | |
1619 | loop { | |
1620 | let elt = self.iter.next()?; | |
1621 | if self.other.contains(elt) { | |
1622 | return Some(elt); | |
1623 | } | |
1624 | } | |
1625 | } | |
1626 | ||
1627 | #[cfg_attr(feature = "inline-more", inline)] | |
1628 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1629 | let (_, upper) = self.iter.size_hint(); | |
1630 | (0, upper) | |
1631 | } | |
1632 | } | |
1633 | ||
1634 | impl<T, S, A> fmt::Debug for Intersection<'_, T, S, A> | |
1635 | where | |
1636 | T: fmt::Debug + Eq + Hash, | |
1637 | S: BuildHasher, | |
1638 | A: Allocator + Clone, | |
1639 | { | |
1640 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1641 | f.debug_list().entries(self.clone()).finish() | |
1642 | } | |
1643 | } | |
1644 | ||
1645 | impl<T, S, A> FusedIterator for Intersection<'_, T, S, A> | |
1646 | where | |
1647 | T: Eq + Hash, | |
1648 | S: BuildHasher, | |
1649 | A: Allocator + Clone, | |
1650 | { | |
1651 | } | |
1652 | ||
1653 | impl<T, S, A: Allocator + Clone> Clone for Difference<'_, T, S, A> { | |
1654 | #[cfg_attr(feature = "inline-more", inline)] | |
1655 | fn clone(&self) -> Self { | |
1656 | Difference { | |
1657 | iter: self.iter.clone(), | |
1658 | ..*self | |
1659 | } | |
1660 | } | |
1661 | } | |
1662 | ||
1663 | impl<'a, T, S, A> Iterator for Difference<'a, T, S, A> | |
1664 | where | |
1665 | T: Eq + Hash, | |
1666 | S: BuildHasher, | |
1667 | A: Allocator + Clone, | |
1668 | { | |
1669 | type Item = &'a T; | |
1670 | ||
1671 | #[cfg_attr(feature = "inline-more", inline)] | |
1672 | fn next(&mut self) -> Option<&'a T> { | |
1673 | loop { | |
1674 | let elt = self.iter.next()?; | |
1675 | if !self.other.contains(elt) { | |
1676 | return Some(elt); | |
1677 | } | |
1678 | } | |
1679 | } | |
1680 | ||
1681 | #[cfg_attr(feature = "inline-more", inline)] | |
1682 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1683 | let (_, upper) = self.iter.size_hint(); | |
1684 | (0, upper) | |
1685 | } | |
1686 | } | |
1687 | ||
1688 | impl<T, S, A> FusedIterator for Difference<'_, T, S, A> | |
1689 | where | |
1690 | T: Eq + Hash, | |
1691 | S: BuildHasher, | |
1692 | A: Allocator + Clone, | |
1693 | { | |
1694 | } | |
1695 | ||
1696 | impl<T, S, A> fmt::Debug for Difference<'_, T, S, A> | |
1697 | where | |
1698 | T: fmt::Debug + Eq + Hash, | |
1699 | S: BuildHasher, | |
1700 | A: Allocator + Clone, | |
1701 | { | |
1702 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1703 | f.debug_list().entries(self.clone()).finish() | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | impl<T, S, A: Allocator + Clone> Clone for SymmetricDifference<'_, T, S, A> { | |
1708 | #[cfg_attr(feature = "inline-more", inline)] | |
1709 | fn clone(&self) -> Self { | |
1710 | SymmetricDifference { | |
1711 | iter: self.iter.clone(), | |
1712 | } | |
1713 | } | |
1714 | } | |
1715 | ||
1716 | impl<'a, T, S, A> Iterator for SymmetricDifference<'a, T, S, A> | |
1717 | where | |
1718 | T: Eq + Hash, | |
1719 | S: BuildHasher, | |
1720 | A: Allocator + Clone, | |
1721 | { | |
1722 | type Item = &'a T; | |
1723 | ||
1724 | #[cfg_attr(feature = "inline-more", inline)] | |
1725 | fn next(&mut self) -> Option<&'a T> { | |
1726 | self.iter.next() | |
1727 | } | |
1728 | #[cfg_attr(feature = "inline-more", inline)] | |
1729 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1730 | self.iter.size_hint() | |
1731 | } | |
1732 | } | |
1733 | ||
1734 | impl<T, S, A> FusedIterator for SymmetricDifference<'_, T, S, A> | |
1735 | where | |
1736 | T: Eq + Hash, | |
1737 | S: BuildHasher, | |
1738 | A: Allocator + Clone, | |
1739 | { | |
1740 | } | |
1741 | ||
1742 | impl<T, S, A> fmt::Debug for SymmetricDifference<'_, T, S, A> | |
1743 | where | |
1744 | T: fmt::Debug + Eq + Hash, | |
1745 | S: BuildHasher, | |
1746 | A: Allocator + Clone, | |
1747 | { | |
1748 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1749 | f.debug_list().entries(self.clone()).finish() | |
1750 | } | |
1751 | } | |
1752 | ||
1753 | impl<T, S, A: Allocator + Clone> Clone for Union<'_, T, S, A> { | |
1754 | #[cfg_attr(feature = "inline-more", inline)] | |
1755 | fn clone(&self) -> Self { | |
1756 | Union { | |
1757 | iter: self.iter.clone(), | |
1758 | } | |
1759 | } | |
1760 | } | |
1761 | ||
1762 | impl<T, S, A> FusedIterator for Union<'_, T, S, A> | |
1763 | where | |
1764 | T: Eq + Hash, | |
1765 | S: BuildHasher, | |
1766 | A: Allocator + Clone, | |
1767 | { | |
1768 | } | |
1769 | ||
1770 | impl<T, S, A> fmt::Debug for Union<'_, T, S, A> | |
1771 | where | |
1772 | T: fmt::Debug + Eq + Hash, | |
1773 | S: BuildHasher, | |
1774 | A: Allocator + Clone, | |
1775 | { | |
1776 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
1777 | f.debug_list().entries(self.clone()).finish() | |
1778 | } | |
1779 | } | |
1780 | ||
1781 | impl<'a, T, S, A> Iterator for Union<'a, T, S, A> | |
1782 | where | |
1783 | T: Eq + Hash, | |
1784 | S: BuildHasher, | |
1785 | A: Allocator + Clone, | |
1786 | { | |
1787 | type Item = &'a T; | |
1788 | ||
1789 | #[cfg_attr(feature = "inline-more", inline)] | |
1790 | fn next(&mut self) -> Option<&'a T> { | |
1791 | self.iter.next() | |
1792 | } | |
1793 | #[cfg_attr(feature = "inline-more", inline)] | |
1794 | fn size_hint(&self) -> (usize, Option<usize>) { | |
1795 | self.iter.size_hint() | |
1796 | } | |
1797 | } | |
1798 | ||
1799 | #[allow(dead_code)] | |
1800 | fn assert_covariance() { | |
1801 | fn set<'new>(v: HashSet<&'static str>) -> HashSet<&'new str> { | |
1802 | v | |
1803 | } | |
1804 | fn iter<'a, 'new>(v: Iter<'a, &'static str>) -> Iter<'a, &'new str> { | |
1805 | v | |
1806 | } | |
1807 | fn into_iter<'new, A: Allocator + Clone>( | |
1808 | v: IntoIter<&'static str, A>, | |
1809 | ) -> IntoIter<&'new str, A> { | |
1810 | v | |
1811 | } | |
1812 | fn difference<'a, 'new, A: Allocator + Clone>( | |
1813 | v: Difference<'a, &'static str, DefaultHashBuilder, A>, | |
1814 | ) -> Difference<'a, &'new str, DefaultHashBuilder, A> { | |
1815 | v | |
1816 | } | |
1817 | fn symmetric_difference<'a, 'new, A: Allocator + Clone>( | |
1818 | v: SymmetricDifference<'a, &'static str, DefaultHashBuilder, A>, | |
1819 | ) -> SymmetricDifference<'a, &'new str, DefaultHashBuilder, A> { | |
1820 | v | |
1821 | } | |
1822 | fn intersection<'a, 'new, A: Allocator + Clone>( | |
1823 | v: Intersection<'a, &'static str, DefaultHashBuilder, A>, | |
1824 | ) -> Intersection<'a, &'new str, DefaultHashBuilder, A> { | |
1825 | v | |
1826 | } | |
1827 | fn union<'a, 'new, A: Allocator + Clone>( | |
1828 | v: Union<'a, &'static str, DefaultHashBuilder, A>, | |
1829 | ) -> Union<'a, &'new str, DefaultHashBuilder, A> { | |
1830 | v | |
1831 | } | |
1832 | fn drain<'new, A: Allocator + Clone>( | |
1833 | d: Drain<'static, &'static str, A>, | |
1834 | ) -> Drain<'new, &'new str, A> { | |
1835 | d | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | #[cfg(test)] | |
1840 | mod test_set { | |
1841 | use super::super::map::DefaultHashBuilder; | |
1842 | use super::HashSet; | |
1843 | use std::vec::Vec; | |
1844 | ||
1845 | #[test] | |
1846 | fn test_zero_capacities() { | |
1847 | type HS = HashSet<i32>; | |
1848 | ||
1849 | let s = HS::new(); | |
1850 | assert_eq!(s.capacity(), 0); | |
1851 | ||
1852 | let s = HS::default(); | |
1853 | assert_eq!(s.capacity(), 0); | |
1854 | ||
1855 | let s = HS::with_hasher(DefaultHashBuilder::default()); | |
1856 | assert_eq!(s.capacity(), 0); | |
1857 | ||
1858 | let s = HS::with_capacity(0); | |
1859 | assert_eq!(s.capacity(), 0); | |
1860 | ||
1861 | let s = HS::with_capacity_and_hasher(0, DefaultHashBuilder::default()); | |
1862 | assert_eq!(s.capacity(), 0); | |
1863 | ||
1864 | let mut s = HS::new(); | |
1865 | s.insert(1); | |
1866 | s.insert(2); | |
1867 | s.remove(&1); | |
1868 | s.remove(&2); | |
1869 | s.shrink_to_fit(); | |
1870 | assert_eq!(s.capacity(), 0); | |
1871 | ||
1872 | let mut s = HS::new(); | |
1873 | s.reserve(0); | |
1874 | assert_eq!(s.capacity(), 0); | |
1875 | } | |
1876 | ||
1877 | #[test] | |
1878 | fn test_disjoint() { | |
1879 | let mut xs = HashSet::new(); | |
1880 | let mut ys = HashSet::new(); | |
1881 | assert!(xs.is_disjoint(&ys)); | |
1882 | assert!(ys.is_disjoint(&xs)); | |
1883 | assert!(xs.insert(5)); | |
1884 | assert!(ys.insert(11)); | |
1885 | assert!(xs.is_disjoint(&ys)); | |
1886 | assert!(ys.is_disjoint(&xs)); | |
1887 | assert!(xs.insert(7)); | |
1888 | assert!(xs.insert(19)); | |
1889 | assert!(xs.insert(4)); | |
1890 | assert!(ys.insert(2)); | |
1891 | assert!(ys.insert(-11)); | |
1892 | assert!(xs.is_disjoint(&ys)); | |
1893 | assert!(ys.is_disjoint(&xs)); | |
1894 | assert!(ys.insert(7)); | |
1895 | assert!(!xs.is_disjoint(&ys)); | |
1896 | assert!(!ys.is_disjoint(&xs)); | |
1897 | } | |
1898 | ||
1899 | #[test] | |
1900 | fn test_subset_and_superset() { | |
1901 | let mut a = HashSet::new(); | |
1902 | assert!(a.insert(0)); | |
1903 | assert!(a.insert(5)); | |
1904 | assert!(a.insert(11)); | |
1905 | assert!(a.insert(7)); | |
1906 | ||
1907 | let mut b = HashSet::new(); | |
1908 | assert!(b.insert(0)); | |
1909 | assert!(b.insert(7)); | |
1910 | assert!(b.insert(19)); | |
1911 | assert!(b.insert(250)); | |
1912 | assert!(b.insert(11)); | |
1913 | assert!(b.insert(200)); | |
1914 | ||
1915 | assert!(!a.is_subset(&b)); | |
1916 | assert!(!a.is_superset(&b)); | |
1917 | assert!(!b.is_subset(&a)); | |
1918 | assert!(!b.is_superset(&a)); | |
1919 | ||
1920 | assert!(b.insert(5)); | |
1921 | ||
1922 | assert!(a.is_subset(&b)); | |
1923 | assert!(!a.is_superset(&b)); | |
1924 | assert!(!b.is_subset(&a)); | |
1925 | assert!(b.is_superset(&a)); | |
1926 | } | |
1927 | ||
1928 | #[test] | |
1929 | fn test_iterate() { | |
1930 | let mut a = HashSet::new(); | |
1931 | for i in 0..32 { | |
1932 | assert!(a.insert(i)); | |
1933 | } | |
1934 | let mut observed: u32 = 0; | |
1935 | for k in &a { | |
1936 | observed |= 1 << *k; | |
1937 | } | |
1938 | assert_eq!(observed, 0xFFFF_FFFF); | |
1939 | } | |
1940 | ||
1941 | #[test] | |
1942 | fn test_intersection() { | |
1943 | let mut a = HashSet::new(); | |
1944 | let mut b = HashSet::new(); | |
1945 | ||
1946 | assert!(a.insert(11)); | |
1947 | assert!(a.insert(1)); | |
1948 | assert!(a.insert(3)); | |
1949 | assert!(a.insert(77)); | |
1950 | assert!(a.insert(103)); | |
1951 | assert!(a.insert(5)); | |
1952 | assert!(a.insert(-5)); | |
1953 | ||
1954 | assert!(b.insert(2)); | |
1955 | assert!(b.insert(11)); | |
1956 | assert!(b.insert(77)); | |
1957 | assert!(b.insert(-9)); | |
1958 | assert!(b.insert(-42)); | |
1959 | assert!(b.insert(5)); | |
1960 | assert!(b.insert(3)); | |
1961 | ||
1962 | let mut i = 0; | |
1963 | let expected = [3, 5, 11, 77]; | |
1964 | for x in a.intersection(&b) { | |
1965 | assert!(expected.contains(x)); | |
1966 | i += 1 | |
1967 | } | |
1968 | assert_eq!(i, expected.len()); | |
1969 | } | |
1970 | ||
1971 | #[test] | |
1972 | fn test_difference() { | |
1973 | let mut a = HashSet::new(); | |
1974 | let mut b = HashSet::new(); | |
1975 | ||
1976 | assert!(a.insert(1)); | |
1977 | assert!(a.insert(3)); | |
1978 | assert!(a.insert(5)); | |
1979 | assert!(a.insert(9)); | |
1980 | assert!(a.insert(11)); | |
1981 | ||
1982 | assert!(b.insert(3)); | |
1983 | assert!(b.insert(9)); | |
1984 | ||
1985 | let mut i = 0; | |
1986 | let expected = [1, 5, 11]; | |
1987 | for x in a.difference(&b) { | |
1988 | assert!(expected.contains(x)); | |
1989 | i += 1 | |
1990 | } | |
1991 | assert_eq!(i, expected.len()); | |
1992 | } | |
1993 | ||
1994 | #[test] | |
1995 | fn test_symmetric_difference() { | |
1996 | let mut a = HashSet::new(); | |
1997 | let mut b = HashSet::new(); | |
1998 | ||
1999 | assert!(a.insert(1)); | |
2000 | assert!(a.insert(3)); | |
2001 | assert!(a.insert(5)); | |
2002 | assert!(a.insert(9)); | |
2003 | assert!(a.insert(11)); | |
2004 | ||
2005 | assert!(b.insert(-2)); | |
2006 | assert!(b.insert(3)); | |
2007 | assert!(b.insert(9)); | |
2008 | assert!(b.insert(14)); | |
2009 | assert!(b.insert(22)); | |
2010 | ||
2011 | let mut i = 0; | |
2012 | let expected = [-2, 1, 5, 11, 14, 22]; | |
2013 | for x in a.symmetric_difference(&b) { | |
2014 | assert!(expected.contains(x)); | |
2015 | i += 1 | |
2016 | } | |
2017 | assert_eq!(i, expected.len()); | |
2018 | } | |
2019 | ||
2020 | #[test] | |
2021 | fn test_union() { | |
2022 | let mut a = HashSet::new(); | |
2023 | let mut b = HashSet::new(); | |
2024 | ||
2025 | assert!(a.insert(1)); | |
2026 | assert!(a.insert(3)); | |
2027 | assert!(a.insert(5)); | |
2028 | assert!(a.insert(9)); | |
2029 | assert!(a.insert(11)); | |
2030 | assert!(a.insert(16)); | |
2031 | assert!(a.insert(19)); | |
2032 | assert!(a.insert(24)); | |
2033 | ||
2034 | assert!(b.insert(-2)); | |
2035 | assert!(b.insert(1)); | |
2036 | assert!(b.insert(5)); | |
2037 | assert!(b.insert(9)); | |
2038 | assert!(b.insert(13)); | |
2039 | assert!(b.insert(19)); | |
2040 | ||
2041 | let mut i = 0; | |
2042 | let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]; | |
2043 | for x in a.union(&b) { | |
2044 | assert!(expected.contains(x)); | |
2045 | i += 1 | |
2046 | } | |
2047 | assert_eq!(i, expected.len()); | |
2048 | } | |
2049 | ||
2050 | #[test] | |
2051 | fn test_from_map() { | |
2052 | let mut a = crate::HashMap::new(); | |
2053 | a.insert(1, ()); | |
2054 | a.insert(2, ()); | |
2055 | a.insert(3, ()); | |
2056 | a.insert(4, ()); | |
2057 | ||
2058 | let a: HashSet<_> = a.into(); | |
2059 | ||
2060 | assert_eq!(a.len(), 4); | |
2061 | assert!(a.contains(&1)); | |
2062 | assert!(a.contains(&2)); | |
2063 | assert!(a.contains(&3)); | |
2064 | assert!(a.contains(&4)); | |
2065 | } | |
2066 | ||
2067 | #[test] | |
2068 | fn test_from_iter() { | |
2069 | let xs = [1, 2, 2, 3, 4, 5, 6, 7, 8, 9]; | |
2070 | ||
2071 | let set: HashSet<_> = xs.iter().cloned().collect(); | |
2072 | ||
2073 | for x in &xs { | |
2074 | assert!(set.contains(x)); | |
2075 | } | |
2076 | ||
2077 | assert_eq!(set.iter().len(), xs.len() - 1); | |
2078 | } | |
2079 | ||
2080 | #[test] | |
2081 | fn test_move_iter() { | |
2082 | let hs = { | |
2083 | let mut hs = HashSet::new(); | |
2084 | ||
2085 | hs.insert('a'); | |
2086 | hs.insert('b'); | |
2087 | ||
2088 | hs | |
2089 | }; | |
2090 | ||
2091 | let v = hs.into_iter().collect::<Vec<char>>(); | |
2092 | assert!(v == ['a', 'b'] || v == ['b', 'a']); | |
2093 | } | |
2094 | ||
2095 | #[test] | |
2096 | fn test_eq() { | |
2097 | // These constants once happened to expose a bug in insert(). | |
2098 | // I'm keeping them around to prevent a regression. | |
2099 | let mut s1 = HashSet::new(); | |
2100 | ||
2101 | s1.insert(1); | |
2102 | s1.insert(2); | |
2103 | s1.insert(3); | |
2104 | ||
2105 | let mut s2 = HashSet::new(); | |
2106 | ||
2107 | s2.insert(1); | |
2108 | s2.insert(2); | |
2109 | ||
2110 | assert!(s1 != s2); | |
2111 | ||
2112 | s2.insert(3); | |
2113 | ||
2114 | assert_eq!(s1, s2); | |
2115 | } | |
2116 | ||
2117 | #[test] | |
2118 | fn test_show() { | |
2119 | let mut set = HashSet::new(); | |
2120 | let empty = HashSet::<i32>::new(); | |
2121 | ||
2122 | set.insert(1); | |
2123 | set.insert(2); | |
2124 | ||
2125 | let set_str = format!("{:?}", set); | |
2126 | ||
2127 | assert!(set_str == "{1, 2}" || set_str == "{2, 1}"); | |
2128 | assert_eq!(format!("{:?}", empty), "{}"); | |
2129 | } | |
2130 | ||
2131 | #[test] | |
2132 | fn test_trivial_drain() { | |
2133 | let mut s = HashSet::<i32>::new(); | |
2134 | for _ in s.drain() {} | |
2135 | assert!(s.is_empty()); | |
2136 | drop(s); | |
2137 | ||
2138 | let mut s = HashSet::<i32>::new(); | |
2139 | drop(s.drain()); | |
2140 | assert!(s.is_empty()); | |
2141 | } | |
2142 | ||
2143 | #[test] | |
2144 | fn test_drain() { | |
2145 | let mut s: HashSet<_> = (1..100).collect(); | |
2146 | ||
2147 | // try this a bunch of times to make sure we don't screw up internal state. | |
2148 | for _ in 0..20 { | |
2149 | assert_eq!(s.len(), 99); | |
2150 | ||
2151 | { | |
2152 | let mut last_i = 0; | |
2153 | let mut d = s.drain(); | |
2154 | for (i, x) in d.by_ref().take(50).enumerate() { | |
2155 | last_i = i; | |
2156 | assert!(x != 0); | |
2157 | } | |
2158 | assert_eq!(last_i, 49); | |
2159 | } | |
2160 | ||
2161 | for _ in &s { | |
2162 | panic!("s should be empty!"); | |
2163 | } | |
2164 | ||
2165 | // reset to try again. | |
2166 | s.extend(1..100); | |
2167 | } | |
2168 | } | |
2169 | ||
2170 | #[test] | |
2171 | fn test_replace() { | |
2172 | use core::hash; | |
2173 | ||
2174 | #[derive(Debug)] | |
2175 | struct Foo(&'static str, i32); | |
2176 | ||
2177 | impl PartialEq for Foo { | |
2178 | fn eq(&self, other: &Self) -> bool { | |
2179 | self.0 == other.0 | |
2180 | } | |
2181 | } | |
2182 | ||
2183 | impl Eq for Foo {} | |
2184 | ||
2185 | impl hash::Hash for Foo { | |
2186 | fn hash<H: hash::Hasher>(&self, h: &mut H) { | |
2187 | self.0.hash(h); | |
2188 | } | |
2189 | } | |
2190 | ||
2191 | let mut s = HashSet::new(); | |
2192 | assert_eq!(s.replace(Foo("a", 1)), None); | |
2193 | assert_eq!(s.len(), 1); | |
2194 | assert_eq!(s.replace(Foo("a", 2)), Some(Foo("a", 1))); | |
2195 | assert_eq!(s.len(), 1); | |
2196 | ||
2197 | let mut it = s.iter(); | |
2198 | assert_eq!(it.next(), Some(&Foo("a", 2))); | |
2199 | assert_eq!(it.next(), None); | |
2200 | } | |
2201 | ||
2202 | #[test] | |
2203 | fn test_extend_ref() { | |
2204 | let mut a = HashSet::new(); | |
2205 | a.insert(1); | |
2206 | ||
2207 | a.extend(&[2, 3, 4]); | |
2208 | ||
2209 | assert_eq!(a.len(), 4); | |
2210 | assert!(a.contains(&1)); | |
2211 | assert!(a.contains(&2)); | |
2212 | assert!(a.contains(&3)); | |
2213 | assert!(a.contains(&4)); | |
2214 | ||
2215 | let mut b = HashSet::new(); | |
2216 | b.insert(5); | |
2217 | b.insert(6); | |
2218 | ||
2219 | a.extend(&b); | |
2220 | ||
2221 | assert_eq!(a.len(), 6); | |
2222 | assert!(a.contains(&1)); | |
2223 | assert!(a.contains(&2)); | |
2224 | assert!(a.contains(&3)); | |
2225 | assert!(a.contains(&4)); | |
2226 | assert!(a.contains(&5)); | |
2227 | assert!(a.contains(&6)); | |
2228 | } | |
2229 | ||
2230 | #[test] | |
2231 | fn test_retain() { | |
2232 | let xs = [1, 2, 3, 4, 5, 6]; | |
2233 | let mut set: HashSet<i32> = xs.iter().cloned().collect(); | |
2234 | set.retain(|&k| k % 2 == 0); | |
2235 | assert_eq!(set.len(), 3); | |
2236 | assert!(set.contains(&2)); | |
2237 | assert!(set.contains(&4)); | |
2238 | assert!(set.contains(&6)); | |
2239 | } | |
2240 | ||
2241 | #[test] | |
2242 | fn test_drain_filter() { | |
2243 | { | |
2244 | let mut set: HashSet<i32> = (0..8).collect(); | |
2245 | let drained = set.drain_filter(|&k| k % 2 == 0); | |
2246 | let mut out = drained.collect::<Vec<_>>(); | |
2247 | out.sort_unstable(); | |
2248 | assert_eq!(vec![0, 2, 4, 6], out); | |
2249 | assert_eq!(set.len(), 4); | |
2250 | } | |
2251 | { | |
2252 | let mut set: HashSet<i32> = (0..8).collect(); | |
2253 | drop(set.drain_filter(|&k| k % 2 == 0)); | |
2254 | assert_eq!(set.len(), 4, "Removes non-matching items on drop"); | |
2255 | } | |
2256 | } | |
2257 | ||
2258 | #[test] | |
2259 | fn test_const_with_hasher() { | |
2260 | use core::hash::BuildHasher; | |
2261 | use std::collections::hash_map::DefaultHasher; | |
2262 | ||
2263 | #[derive(Clone)] | |
2264 | struct MyHasher; | |
2265 | impl BuildHasher for MyHasher { | |
2266 | type Hasher = DefaultHasher; | |
2267 | ||
2268 | fn build_hasher(&self) -> DefaultHasher { | |
2269 | DefaultHasher::new() | |
2270 | } | |
2271 | } | |
2272 | ||
2273 | const EMPTY_SET: HashSet<u32, MyHasher> = HashSet::with_hasher(MyHasher); | |
2274 | ||
2275 | let mut set = EMPTY_SET.clone(); | |
2276 | set.insert(19); | |
2277 | assert!(set.contains(&19)); | |
2278 | } | |
2279 | ||
2280 | #[test] | |
2281 | fn rehash_in_place() { | |
2282 | let mut set = HashSet::new(); | |
2283 | ||
2284 | for i in 0..224 { | |
2285 | set.insert(i); | |
2286 | } | |
2287 | ||
2288 | assert_eq!( | |
2289 | set.capacity(), | |
2290 | 224, | |
2291 | "The set must be at or close to capacity to trigger a re hashing" | |
2292 | ); | |
2293 | ||
2294 | for i in 100..1400 { | |
2295 | set.remove(&(i - 100)); | |
2296 | set.insert(i); | |
2297 | } | |
2298 | } | |
2299 | } |