1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15883
13 //! Unordered containers, implemented as hash-tables (`HashSet` and `HashMap` types)
16 use cmp::{max, Eq, Equiv, PartialEq};
17 use collections::{Collection, Mutable, Set, MutableSet, Map, MutableMap};
21 use hash::{Hash, Hasher, RandomSipHasher};
22 use iter::{Iterator, FilterMap, Chain, Repeat, Zip, Extendable};
23 use iter::{range, range_inclusive, FromIterator};
27 use option::{Some, None, Option};
28 use result::{Ok, Err};
33 use hash::{Hash, Hasher};
34 use iter::range_step_inclusive;
35 use iter::{Iterator, range};
37 use mem::{min_align_of, size_of};
38 use mem::{overwrite, transmute};
39 use num::{CheckedMul, is_power_of_two};
41 use option::{Some, None, Option};
45 use rt::heap::{allocate, deallocate};
47 static EMPTY_BUCKET: u64 = 0u64;
49 /// The raw hashtable, providing safe-ish access to the unzipped and highly
50 /// optimized arrays of hashes, keys, and values.
52 /// This design uses less memory and is a lot faster than the naive
53 /// `Vec<Option<u64, K, V>>`, because we don't pay for the overhead of an
54 /// option on every element, and we get a generally more cache-aware design.
56 /// Key invariants of this structure:
58 /// - if hashes[i] == EMPTY_BUCKET, then keys[i] and vals[i] have
59 /// 'undefined' contents. Don't read from them. This invariant is
60 /// enforced outside this module with the `EmptyIndex`, `FullIndex`,
61 /// and `SafeHash` types.
63 /// - An `EmptyIndex` is only constructed for a bucket at an index with
64 /// a hash of EMPTY_BUCKET.
66 /// - A `FullIndex` is only constructed for a bucket at an index with a
67 /// non-EMPTY_BUCKET hash.
69 /// - A `SafeHash` is only constructed for non-`EMPTY_BUCKET` hash. We get
70 /// around hashes of zero by changing them to 0x8000_0000_0000_0000,
71 /// which will likely map to the same bucket, while not being confused
74 /// - All three "arrays represented by pointers" are the same length:
75 /// `capacity`. This is set at creation and never changes. The arrays
76 /// are unzipped to save space (we don't have to pay for the padding
77 /// between odd sized elements, such as in a map from u64 to u8), and
78 /// be more cache aware (scanning through 8 hashes brings in 2 cache
79 /// lines, since they're all right beside each other).
81 /// You can kind of think of this module/data structure as a safe wrapper
82 /// around just the "table" part of the hashtable. It enforces some
83 /// invariants at the type level and employs some performance trickery,
84 /// but in general is just a tricked out `Vec<Option<u64, K, V>>`.
88 /// Feb 11, 2014: This hashtable was just implemented, and, hard as I tried,
89 /// isn't yet totally safe. There's a "known exploit" that you can create
90 /// multiple FullIndexes for a bucket, `take` one, and then still `take`
91 /// the other causing undefined behavior. Currently, there's no story
92 /// for how to protect against this statically. Therefore, there are asserts
93 /// on `take`, `get`, `get_mut`, and `put` which check the bucket state.
94 /// With time, and when we're confident this works correctly, they should
95 /// be removed. Also, the bounds check in `peek` is especially painful,
96 /// as that's called in the innermost loops of the hashtable and has the
97 /// potential to be a major performance drain. Remove this too.
99 /// Or, better than remove, only enable these checks for debug builds.
100 /// There's currently no "debug-only" asserts in rust, so if you're reading
101 /// this and going "what? of course there are debug-only asserts!", then
102 /// please make this use them!
103 #[unsafe_no_drop_flag]
104 pub struct RawTable<K, V> {
112 /// Represents an index into a `RawTable` with no key or value in it.
113 pub struct EmptyIndex {
115 nocopy: marker::NoCopy,
118 /// Represents an index into a `RawTable` with a key, value, and hash
120 pub struct FullIndex {
123 nocopy: marker::NoCopy,
127 /// Since we get the hash for free whenever we check the bucket state,
128 /// this function is provided for fast access, letting us avoid
129 /// redundant trips back to the hashtable.
131 pub fn hash(&self) -> SafeHash { self.hash }
133 /// Same comment as with `hash`.
135 pub fn raw_index(&self) -> uint { self.idx as uint }
138 /// Represents the state of a bucket: it can either have a key/value
139 /// pair (be full) or not (be empty). You cannot `take` empty buckets,
140 /// and you cannot `put` into full buckets.
141 pub enum BucketState {
146 /// A hash that is not zero, since we use a hash of zero to represent empty
148 #[deriving(PartialEq)]
149 pub struct SafeHash {
154 /// Peek at the hash value, which is guaranteed to be non-zero.
156 pub fn inspect(&self) -> u64 { self.hash }
159 /// We need to remove hashes of 0. That's reserved for empty buckets.
160 /// This function wraps up `hash_keyed` to be the only way outside this
161 /// module to generate a SafeHash.
162 pub fn make_hash<T: Hash<S>, S, H: Hasher<S>>(hasher: &H, t: &T) -> SafeHash {
163 match hasher.hash(t) {
164 // This constant is exceedingly likely to hash to the same
165 // bucket, but it won't be counted as empty!
166 EMPTY_BUCKET => SafeHash { hash: 0x8000_0000_0000_0000 },
167 h => SafeHash { hash: h },
171 fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint {
172 assert!(is_power_of_two(target_alignment));
173 (unrounded + target_alignment - 1) & !(target_alignment - 1)
178 assert_eq!(round_up_to_next(0, 4), 0);
179 assert_eq!(round_up_to_next(1, 4), 4);
180 assert_eq!(round_up_to_next(2, 4), 4);
181 assert_eq!(round_up_to_next(3, 4), 4);
182 assert_eq!(round_up_to_next(4, 4), 4);
183 assert_eq!(round_up_to_next(5, 4), 8);
186 // Returns a tuple of (minimum required malloc alignment, hash_offset,
187 // key_offset, val_offset, array_size), from the start of a mallocated array.
188 fn calculate_offsets(
189 hash_size: uint, hash_align: uint,
190 keys_size: uint, keys_align: uint,
191 vals_size: uint, vals_align: uint) -> (uint, uint, uint, uint, uint) {
194 let end_of_hashes = hash_offset + hash_size;
196 let keys_offset = round_up_to_next(end_of_hashes, keys_align);
197 let end_of_keys = keys_offset + keys_size;
199 let vals_offset = round_up_to_next(end_of_keys, vals_align);
200 let end_of_vals = vals_offset + vals_size;
202 let min_align = cmp::max(hash_align, cmp::max(keys_align, vals_align));
204 (min_align, hash_offset, keys_offset, vals_offset, end_of_vals)
208 fn test_offset_calculation() {
209 assert_eq!(calculate_offsets(128, 8, 15, 1, 4, 4 ), (8, 0, 128, 144, 148));
210 assert_eq!(calculate_offsets(3, 1, 2, 1, 1, 1 ), (1, 0, 3, 5, 6));
211 assert_eq!(calculate_offsets(6, 2, 12, 4, 24, 8), (8, 0, 8, 24, 48));
214 impl<K, V> RawTable<K, V> {
216 /// Does not initialize the buckets. The caller should ensure they,
217 /// at the very least, set every hash to EMPTY_BUCKET.
218 unsafe fn new_uninitialized(capacity: uint) -> RawTable<K, V> {
219 let hashes_size = capacity.checked_mul(&size_of::<u64>())
220 .expect("capacity overflow");
221 let keys_size = capacity.checked_mul(&size_of::< K >())
222 .expect("capacity overflow");
223 let vals_size = capacity.checked_mul(&size_of::< V >())
224 .expect("capacity overflow");
226 // Allocating hashmaps is a little tricky. We need to allocate three
227 // arrays, but since we know their sizes and alignments up front,
228 // we just allocate a single array, and then have the subarrays
231 // This is great in theory, but in practice getting the alignment
232 // right is a little subtle. Therefore, calculating offsets has been
233 // factored out into a different function.
234 let (malloc_alignment, hash_offset, keys_offset, vals_offset, size) =
236 hashes_size, min_align_of::<u64>(),
237 keys_size, min_align_of::< K >(),
238 vals_size, min_align_of::< V >());
240 let buffer = allocate(size, malloc_alignment);
242 let hashes = buffer.offset(hash_offset as int) as *mut u64;
243 let keys = buffer.offset(keys_offset as int) as *mut K;
244 let vals = buffer.offset(vals_offset as int) as *mut V;
255 /// Creates a new raw table from a given capacity. All buckets are
257 #[allow(experimental)]
258 pub fn new(capacity: uint) -> RawTable<K, V> {
260 let ret = RawTable::new_uninitialized(capacity);
261 set_memory(ret.hashes, 0u8, capacity);
266 /// Reads a bucket at a given index, returning an enum indicating whether
267 /// there's anything there or not. You need to match on this enum to get
268 /// the appropriate types to pass on to most of the other functions in
270 pub fn peek(&self, index: uint) -> BucketState {
271 debug_assert!(index < self.capacity);
273 let idx = index as int;
274 let hash = unsafe { *self.hashes.offset(idx) };
276 let nocopy = marker::NoCopy;
287 hash: SafeHash { hash: full_hash },
293 /// Gets references to the key and value at a given index.
294 pub fn read<'a>(&'a self, index: &FullIndex) -> (&'a K, &'a V) {
298 debug_assert!(*self.hashes.offset(idx) != EMPTY_BUCKET);
299 (&*self.keys.offset(idx), &*self.vals.offset(idx))
303 /// Gets references to the key and value at a given index, with the
304 /// value's reference being mutable.
305 pub fn read_mut<'a>(&'a mut self, index: &FullIndex) -> (&'a K, &'a mut V) {
309 debug_assert!(*self.hashes.offset(idx) != EMPTY_BUCKET);
310 (&*self.keys.offset(idx), &mut *self.vals.offset(idx))
314 /// Read everything, mutably.
315 pub fn read_all_mut<'a>(&'a mut self, index: &FullIndex)
316 -> (&'a mut SafeHash, &'a mut K, &'a mut V) {
320 debug_assert!(*self.hashes.offset(idx) != EMPTY_BUCKET);
321 (transmute(self.hashes.offset(idx)),
322 &mut *self.keys.offset(idx), &mut *self.vals.offset(idx))
326 /// Puts a key and value pair, along with the key's hash, into a given
327 /// index in the hashtable. Note how the `EmptyIndex` is 'moved' into this
328 /// function, because that slot will no longer be empty when we return!
329 /// A FullIndex is returned for later use, pointing to the newly-filled
330 /// slot in the hashtable.
332 /// Use `make_hash` to construct a `SafeHash` to pass to this function.
333 pub fn put(&mut self, index: EmptyIndex, hash: SafeHash, k: K, v: V) -> FullIndex {
337 debug_assert_eq!(*self.hashes.offset(idx), EMPTY_BUCKET);
338 *self.hashes.offset(idx) = hash.inspect();
339 overwrite(&mut *self.keys.offset(idx), k);
340 overwrite(&mut *self.vals.offset(idx), v);
345 FullIndex { idx: idx, hash: hash, nocopy: marker::NoCopy }
348 /// Removes a key and value from the hashtable.
350 /// This works similarly to `put`, building an `EmptyIndex` out of the
352 pub fn take(&mut self, index: FullIndex) -> (EmptyIndex, K, V) {
356 debug_assert!(*self.hashes.offset(idx) != EMPTY_BUCKET);
358 *self.hashes.offset(idx) = EMPTY_BUCKET;
360 // Drop the mutable constraint.
361 let keys = self.keys as *const K;
362 let vals = self.vals as *const V;
364 let k = ptr::read(keys.offset(idx));
365 let v = ptr::read(vals.offset(idx));
369 (EmptyIndex { idx: idx, nocopy: marker::NoCopy }, k, v)
373 /// The hashtable's capacity, similar to a vector's.
374 pub fn capacity(&self) -> uint {
378 /// The number of elements ever `put` in the hashtable, minus the number
379 /// of elements ever `take`n.
380 pub fn size(&self) -> uint {
384 pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
385 Entries { table: self, idx: 0, elems_seen: 0 }
388 pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
389 MutEntries { table: self, idx: 0, elems_seen: 0 }
392 pub fn move_iter(self) -> MoveEntries<K, V> {
393 MoveEntries { table: self, idx: 0 }
397 // `read_all_mut` casts a `*u64` to a `*SafeHash`. Since we statically
398 // ensure that a `FullIndex` points to an index with a non-zero hash,
399 // and a `SafeHash` is just a `u64` with a different name, this is
402 // This test ensures that a `SafeHash` really IS the same size as a
403 // `u64`. If you need to change the size of `SafeHash` (and
404 // consequently made this test fail), `read_all_mut` needs to be
405 // modified to no longer assume this.
407 fn can_alias_safehash_as_u64() {
408 assert_eq!(size_of::<SafeHash>(), size_of::<u64>())
411 /// Iterator over shared references to entries in a table.
412 pub struct Entries<'a, K, V> {
413 table: &'a RawTable<K, V>,
418 /// Iterator over mutable references to entries in a table.
419 pub struct MutEntries<'a, K, V> {
420 table: &'a mut RawTable<K, V>,
425 /// Iterator over the entries in a table, consuming the table.
426 pub struct MoveEntries<K, V> {
427 table: RawTable<K, V>,
431 impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
432 fn next(&mut self) -> Option<(&'a K, &'a V)> {
433 while self.idx < self.table.capacity() {
437 match self.table.peek(i) {
440 self.elems_seen += 1;
441 return Some(self.table.read(&idx));
449 fn size_hint(&self) -> (uint, Option<uint>) {
450 let size = self.table.size() - self.elems_seen;
455 impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
456 fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
457 while self.idx < self.table.capacity() {
461 match self.table.peek(i) {
463 // the transmute here fixes:
464 // error: lifetime of `self` is too short to guarantee its contents
465 // can be safely reborrowed
466 Full(idx) => unsafe {
467 self.elems_seen += 1;
468 return Some(transmute(self.table.read_mut(&idx)));
476 fn size_hint(&self) -> (uint, Option<uint>) {
477 let size = self.table.size() - self.elems_seen;
482 impl<K, V> Iterator<(SafeHash, K, V)> for MoveEntries<K, V> {
483 fn next(&mut self) -> Option<(SafeHash, K, V)> {
484 while self.idx < self.table.capacity() {
488 match self.table.peek(i) {
492 let (_, k, v) = self.table.take(idx);
493 return Some((h, k, v));
501 fn size_hint(&self) -> (uint, Option<uint>) {
502 let size = self.table.size();
507 impl<K: Clone, V: Clone> Clone for RawTable<K, V> {
508 fn clone(&self) -> RawTable<K, V> {
510 let mut new_ht = RawTable::new_uninitialized(self.capacity());
512 for i in range(0, self.capacity()) {
515 *new_ht.hashes.offset(i as int) = EMPTY_BUCKET;
518 let hash = idx.hash().inspect();
519 let (k, v) = self.read(&idx);
520 *new_ht.hashes.offset(i as int) = hash;
521 overwrite(&mut *new_ht.keys.offset(i as int), (*k).clone());
522 overwrite(&mut *new_ht.vals.offset(i as int), (*v).clone());
527 new_ht.size = self.size();
535 impl<K, V> Drop for RawTable<K, V> {
537 // This is in reverse because we're likely to have partially taken
538 // some elements out with `.move_iter()` from the front.
539 for i in range_step_inclusive(self.capacity as int - 1, 0, -1) {
540 // Check if the size is 0, so we don't do a useless scan when
541 // dropping empty tables such as on resize.
542 if self.size == 0 { break }
544 match self.peek(i as uint) {
546 Full(idx) => { self.take(idx); }
550 assert_eq!(self.size, 0);
552 if self.hashes.is_not_null() {
553 let hashes_size = self.capacity * size_of::<u64>();
554 let keys_size = self.capacity * size_of::<K>();
555 let vals_size = self.capacity * size_of::<V>();
556 let (align, _, _, _, size) = calculate_offsets(hashes_size, min_align_of::<u64>(),
557 keys_size, min_align_of::<K>(),
558 vals_size, min_align_of::<V>());
561 deallocate(self.hashes as *mut u8, size, align);
562 // Remember how everything was allocated out of one buffer
563 // during initialization? We only need one call to free here.
566 self.hashes = RawPtr::null();
572 static INITIAL_LOG2_CAP: uint = 5;
573 static INITIAL_CAPACITY: uint = 1 << INITIAL_LOG2_CAP; // 2^5
575 /// The default behavior of HashMap implements a load factor of 90.9%.
576 /// This behavior is characterized by the following conditions:
578 /// - if `size * 1.1 < cap < size * 4` then shouldn't resize
579 /// - if `cap < minimum_capacity * 2` then shouldn't shrink
581 struct DefaultResizePolicy {
582 /// Doubled minimal capacity. The capacity must never drop below
583 /// the minimum capacity. (The check happens before the capacity
584 /// is potentially halved.)
585 minimum_capacity2: uint
588 impl DefaultResizePolicy {
589 fn new(new_capacity: uint) -> DefaultResizePolicy {
590 DefaultResizePolicy {
591 minimum_capacity2: new_capacity << 1
596 fn capacity_range(&self, new_size: uint) -> (uint, uint) {
597 ((new_size * 11) / 10, max(new_size << 3, self.minimum_capacity2))
601 fn reserve(&mut self, new_capacity: uint) {
602 self.minimum_capacity2 = new_capacity << 1;
606 // The main performance trick in this hashmap is called Robin Hood Hashing.
607 // It gains its excellent performance from one key invariant:
609 // If an insertion collides with an existing element, and that elements
610 // "probe distance" (how far away the element is from its ideal location)
611 // is higher than how far we've already probed, swap the elements.
613 // This massively lowers variance in probe distance, and allows us to get very
614 // high load factors with good performance. The 90% load factor I use is rather
617 // > Why a load factor of approximately 90%?
619 // In general, all the distances to initial buckets will converge on the mean.
620 // At a load factor of α, the odds of finding the target bucket after k
621 // probes is approximately 1-α^k. If we set this equal to 50% (since we converge
622 // on the mean) and set k=8 (64-byte cache line / 8-byte hash), α=0.92. I round
623 // this down to make the math easier on the CPU and avoid its FPU.
624 // Since on average we start the probing in the middle of a cache line, this
625 // strategy pulls in two cache lines of hashes on every lookup. I think that's
626 // pretty good, but if you want to trade off some space, it could go down to one
627 // cache line on average with an α of 0.84.
629 // > Wait, what? Where did you get 1-α^k from?
631 // On the first probe, your odds of a collision with an existing element is α.
632 // The odds of doing this twice in a row is approximately α^2. For three times,
633 // α^3, etc. Therefore, the odds of colliding k times is α^k. The odds of NOT
634 // colliding after k tries is 1-α^k.
636 // Future Improvements (FIXME!)
637 // ============================
639 // Allow the load factor to be changed dynamically and/or at initialization.
641 // Also, would it be possible for us to reuse storage when growing the
642 // underlying table? This is exactly the use case for 'realloc', and may
643 // be worth exploring.
645 // Future Optimizations (FIXME!)
646 // =============================
648 // The paper cited below mentions an implementation which keeps track of the
649 // distance-to-initial-bucket histogram. I'm suspicious of this approach because
650 // it requires maintaining an internal map. If this map were replaced with a
651 // hashmap, it would be faster, but now our data structure is self-referential
652 // and blows up. Also, this allows very good first guesses, but array accesses
653 // are no longer linear and in one direction, as we have now. There is also
654 // memory and cache pressure that this map would entail that would be very
655 // difficult to properly see in a microbenchmark.
657 // Another possible design choice that I made without any real reason is
658 // parameterizing the raw table over keys and values. Technically, all we need
659 // is the size and alignment of keys and values, and the code should be just as
660 // efficient (well, we might need one for power-of-two size and one for not...).
661 // This has the potential to reduce code bloat in rust executables, without
662 // really losing anything except 4 words (key size, key alignment, val size,
663 // val alignment) which can be passed in to every call of a `RawTable` function.
664 // This would definitely be an avenue worth exploring if people start complaining
665 // about the size of rust executables.
667 // There's also an "optimization" that has been omitted regarding how the
668 // hashtable allocates. The vector type has set the expectation that a hashtable
669 // which never has an element inserted should not allocate. I'm suspicious of
670 // implementing this for hashtables, because supporting it has no performance
671 // benefit over using an `Option<HashMap<K, V>>`, and is significantly more
674 /// A hash map implementation which uses linear probing with Robin
675 /// Hood bucket stealing.
677 /// The hashes are all keyed by the task-local random number generator
678 /// on creation by default, this means the ordering of the keys is
679 /// randomized, but makes the tables more resistant to
680 /// denial-of-service attacks (Hash DoS). This behaviour can be
681 /// overridden with one of the constructors.
683 /// It is required that the keys implement the `Eq` and `Hash` traits, although
684 /// this can frequently be achieved by using `#[deriving(Eq, Hash)]`.
686 /// Relevant papers/articles:
688 /// 1. Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
689 /// 2. Emmanuel Goossaert. ["Robin Hood
690 /// hashing"](http://codecapsule.com/2013/11/11/robin-hood-hashing/)
691 /// 3. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
692 /// deletion"](http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
697 /// use std::collections::HashMap;
699 /// // type inference lets us omit an explicit type signature (which
700 /// // would be `HashMap<&str, &str>` in this example).
701 /// let mut book_reviews = HashMap::new();
703 /// // review some books.
704 /// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.");
705 /// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
706 /// book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
707 /// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
709 /// // check for a specific one.
710 /// if !book_reviews.contains_key(&("Les Misérables")) {
711 /// println!("We've got {} reviews, but Les Misérables ain't one.",
712 /// book_reviews.len());
715 /// // oops, this review has a lot of spelling mistakes, let's delete it.
716 /// book_reviews.remove(&("The Adventures of Sherlock Holmes"));
718 /// // look up the values associated with some keys.
719 /// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
720 /// for book in to_find.iter() {
721 /// match book_reviews.find(book) {
722 /// Some(review) => println!("{}: {}", *book, *review),
723 /// None => println!("{} is unreviewed.", *book)
727 /// // iterate over everything.
728 /// for (book, review) in book_reviews.iter() {
729 /// println!("{}: \"{}\"", *book, *review);
733 pub struct HashMap<K, V, H = RandomSipHasher> {
734 // All hashes are keyed on these values, to prevent hash collision attacks.
737 table: table::RawTable<K, V>,
739 // We keep this at the end since it might as well have tail padding.
740 resize_policy: DefaultResizePolicy,
743 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
744 // Probe the `idx`th bucket for a given hash, returning the index of the
747 // This exploits the power-of-two size of the hashtable. As long as this
748 // is always true, we can use a bitmask of cap-1 to do modular arithmetic.
750 // Prefer using this with increasing values of `idx` rather than repeatedly
751 // calling `probe_next`. This reduces data-dependencies between loops, which
752 // can help the optimizer, and certainly won't hurt it. `probe_next` is
753 // simply for convenience, and is no more efficient than `probe`.
754 fn probe(&self, hash: &table::SafeHash, idx: uint) -> uint {
755 let hash_mask = self.table.capacity() - 1;
757 // So I heard a rumor that unsigned overflow is safe in rust..
758 ((hash.inspect() as uint) + idx) & hash_mask
761 // Generate the next probe in a sequence. Prefer using 'probe' by itself,
762 // but this can sometimes be useful.
763 fn probe_next(&self, probe: uint) -> uint {
764 let hash_mask = self.table.capacity() - 1;
765 (probe + 1) & hash_mask
768 fn make_hash<X: Hash<S>>(&self, x: &X) -> table::SafeHash {
769 table::make_hash(&self.hasher, x)
772 /// Get the distance of the bucket at the given index that it lies
773 /// from its 'ideal' location.
775 /// In the cited blog posts above, this is called the "distance to
776 /// initial bucket", or DIB.
777 fn bucket_distance(&self, index_of_elem: &table::FullIndex) -> uint {
778 // where the hash of the element that happens to reside at
779 // `index_of_elem` tried to place itself first.
780 let first_probe_index = self.probe(&index_of_elem.hash(), 0);
782 let raw_index = index_of_elem.raw_index();
784 if first_probe_index <= raw_index {
785 // probe just went forward
786 raw_index - first_probe_index
788 // probe wrapped around the hashtable
789 raw_index + (self.table.capacity() - first_probe_index)
793 /// Search for a pre-hashed key.
794 fn search_hashed_generic(&self, hash: &table::SafeHash, is_match: |&K| -> bool)
795 -> Option<table::FullIndex> {
796 for num_probes in range(0u, self.table.size()) {
797 let probe = self.probe(hash, num_probes);
799 let idx = match self.table.peek(probe) {
800 table::Empty(_) => return None, // hit an empty bucket
801 table::Full(idx) => idx
804 // We can finish the search early if we hit any bucket
805 // with a lower distance to initial bucket than we've probed.
806 if self.bucket_distance(&idx) < num_probes { return None }
808 // If the hash doesn't match, it can't be this one..
809 if *hash != idx.hash() { continue }
811 let (k, _) = self.table.read(&idx);
813 // If the key doesn't match, it can't be this one..
814 if !is_match(k) { continue }
822 fn search_hashed(&self, hash: &table::SafeHash, k: &K) -> Option<table::FullIndex> {
823 self.search_hashed_generic(hash, |k_| *k == *k_)
826 fn search_equiv<Q: Hash<S> + Equiv<K>>(&self, q: &Q) -> Option<table::FullIndex> {
827 self.search_hashed_generic(&self.make_hash(q), |k| q.equiv(k))
830 /// Search for a key, yielding the index if it's found in the hashtable.
831 /// If you already have the hash for the key lying around, use
833 fn search(&self, k: &K) -> Option<table::FullIndex> {
834 self.search_hashed(&self.make_hash(k), k)
837 fn pop_internal(&mut self, starting_index: table::FullIndex) -> Option<V> {
838 let starting_probe = starting_index.raw_index();
841 let mut probe = self.probe_next(starting_probe);
842 for _ in range(0u, self.table.size()) {
843 match self.table.peek(probe) {
844 table::Empty(_) => {}, // empty bucket. this is the end of our shifting.
845 table::Full(idx) => {
846 // Bucket that isn't us, which has a non-zero probe distance.
847 // This isn't the ending index, so keep searching.
848 if self.bucket_distance(&idx) != 0 {
849 probe = self.probe_next(probe);
853 // if we do have a bucket_distance of zero, we're at the end
854 // of what we need to shift.
863 let (_, _, retval) = self.table.take(starting_index);
865 let mut probe = starting_probe;
866 let mut next_probe = self.probe_next(probe);
868 // backwards-shift all the elements after our newly-deleted one.
869 while next_probe != ending_probe {
870 match self.table.peek(next_probe) {
872 // nothing to shift in. just empty it out.
873 match self.table.peek(probe) {
874 table::Empty(_) => {},
875 table::Full(idx) => { self.table.take(idx); }
878 table::Full(next_idx) => {
879 // something to shift. move it over!
880 let next_hash = next_idx.hash();
881 let (_, next_key, next_val) = self.table.take(next_idx);
882 match self.table.peek(probe) {
883 table::Empty(idx) => {
884 self.table.put(idx, next_hash, next_key, next_val);
886 table::Full(idx) => {
887 let (emptyidx, _, _) = self.table.take(idx);
888 self.table.put(emptyidx, next_hash, next_key, next_val);
895 next_probe = self.probe_next(next_probe);
898 // Done the backwards shift, but there's still an element left!
900 match self.table.peek(probe) {
901 table::Empty(_) => {},
902 table::Full(idx) => { self.table.take(idx); }
905 // Now we're done all our shifting. Return the value we grabbed
910 /// Like `pop`, but can operate on any type that is equivalent to a key.
912 pub fn pop_equiv<Q:Hash<S> + Equiv<K>>(&mut self, k: &Q) -> Option<V> {
913 if self.table.size() == 0 {
917 let potential_new_size = self.table.size() - 1;
918 self.make_some_room(potential_new_size);
920 let starting_index = match self.search_equiv(k) {
925 self.pop_internal(starting_index)
929 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Collection for HashMap<K, V, H> {
930 /// Return the number of elements in the map
931 fn len(&self) -> uint { self.table.size() }
934 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Mutable for HashMap<K, V, H> {
935 /// Clear the map, removing all key-value pairs. Keeps the allocated memory
937 fn clear(&mut self) {
938 // Prevent reallocations from happening from now on. Makes it possible
939 // for the map to be reused but has a downside: reserves permanently.
940 self.resize_policy.reserve(self.table.size());
942 for i in range(0, self.table.capacity()) {
943 match self.table.peek(i) {
944 table::Empty(_) => {},
945 table::Full(idx) => { self.table.take(idx); }
951 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> Map<K, V> for HashMap<K, V, H> {
952 fn find<'a>(&'a self, k: &K) -> Option<&'a V> {
953 self.search(k).map(|idx| {
954 let (_, v) = self.table.read(&idx);
959 fn contains_key(&self, k: &K) -> bool {
960 self.search(k).is_some()
964 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> MutableMap<K, V> for HashMap<K, V, H> {
965 fn find_mut<'a>(&'a mut self, k: &K) -> Option<&'a mut V> {
966 match self.search(k) {
969 let (_, v) = self.table.read_mut(&idx);
975 fn swap(&mut self, k: K, v: V) -> Option<V> {
976 let hash = self.make_hash(&k);
977 let potential_new_size = self.table.size() + 1;
978 self.make_some_room(potential_new_size);
980 for dib in range_inclusive(0u, self.table.size()) {
981 let probe = self.probe(&hash, dib);
983 let idx = match self.table.peek(probe) {
984 table::Empty(idx) => {
986 self.table.put(idx, hash, k, v);
989 table::Full(idx) => idx
992 if idx.hash() == hash {
993 let (bucket_k, bucket_v) = self.table.read_mut(&idx);
995 // Found an existing value.
996 return Some(replace(bucket_v, v));
1000 let probe_dib = self.bucket_distance(&idx);
1002 if probe_dib < dib {
1003 // Found a luckier bucket. This implies that the key does not
1004 // already exist in the hashtable. Just do a robin hood
1006 self.robin_hood(idx, probe_dib, hash, k, v);
1011 // We really shouldn't be here.
1012 fail!("Internal HashMap error: Out of space.");
1015 fn pop(&mut self, k: &K) -> Option<V> {
1016 if self.table.size() == 0 {
1020 let potential_new_size = self.table.size() - 1;
1021 self.make_some_room(potential_new_size);
1023 let starting_index = match self.search(k) {
1025 None => return None,
1028 self.pop_internal(starting_index)
1033 impl<K: Hash + Eq, V> HashMap<K, V, RandomSipHasher> {
1034 /// Create an empty HashMap.
1036 pub fn new() -> HashMap<K, V, RandomSipHasher> {
1037 HashMap::with_capacity(INITIAL_CAPACITY)
1040 /// Creates an empty hash map with the given initial capacity.
1042 pub fn with_capacity(capacity: uint) -> HashMap<K, V, RandomSipHasher> {
1043 let hasher = RandomSipHasher::new();
1044 HashMap::with_capacity_and_hasher(capacity, hasher)
1048 impl<K: Eq + Hash<S>, V, S, H: Hasher<S>> HashMap<K, V, H> {
1049 /// Creates an empty hashmap which will use the given hasher to hash keys.
1051 /// The creates map has the default initial capacity.
1053 pub fn with_hasher(hasher: H) -> HashMap<K, V, H> {
1054 HashMap::with_capacity_and_hasher(INITIAL_CAPACITY, hasher)
1057 /// Create an empty HashMap with space for at least `capacity`
1058 /// elements, using `hasher` to hash the keys.
1060 /// Warning: `hasher` is normally randomly generated, and
1061 /// is designed to allow HashMaps to be resistant to attacks that
1062 /// cause many collisions and very poor performance. Setting it
1063 /// manually using this function can expose a DoS attack vector.
1065 pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashMap<K, V, H> {
1066 let cap = num::next_power_of_two(max(INITIAL_CAPACITY, capacity));
1069 resize_policy: DefaultResizePolicy::new(cap),
1070 table: table::RawTable::new(cap),
1074 /// The hashtable will never try to shrink below this size. You can use
1075 /// this function to reduce reallocations if your hashtable frequently
1076 /// grows and shrinks by large amounts.
1078 /// This function has no effect on the operational semantics of the
1079 /// hashtable, only on performance.
1080 pub fn reserve(&mut self, new_minimum_capacity: uint) {
1081 let cap = num::next_power_of_two(
1082 max(INITIAL_CAPACITY, new_minimum_capacity));
1084 self.resize_policy.reserve(cap);
1086 if self.table.capacity() < cap {
1091 /// Resizes the internal vectors to a new capacity. It's your responsibility to:
1092 /// 1) Make sure the new capacity is enough for all the elements, accounting
1093 /// for the load factor.
1094 /// 2) Ensure new_capacity is a power of two.
1095 fn resize(&mut self, new_capacity: uint) {
1096 assert!(self.table.size() <= new_capacity);
1097 assert!(num::is_power_of_two(new_capacity));
1099 let old_table = replace(&mut self.table, table::RawTable::new(new_capacity));
1100 let old_size = old_table.size();
1102 for (h, k, v) in old_table.move_iter() {
1103 self.insert_hashed_nocheck(h, k, v);
1106 assert_eq!(self.table.size(), old_size);
1109 /// Performs any necessary resize operations, such that there's space for
1110 /// new_size elements.
1111 fn make_some_room(&mut self, new_size: uint) {
1112 let (grow_at, shrink_at) = self.resize_policy.capacity_range(new_size);
1113 let cap = self.table.capacity();
1115 // An invalid value shouldn't make us run out of space.
1116 debug_assert!(grow_at >= new_size);
1119 let new_capacity = cap << 1;
1120 self.resize(new_capacity);
1121 } else if shrink_at <= cap {
1122 let new_capacity = cap >> 1;
1123 self.resize(new_capacity);
1127 /// Perform robin hood bucket stealing at the given 'index'. You must
1128 /// also pass that probe's "distance to initial bucket" so we don't have
1129 /// to recalculate it, as well as the total number of probes already done
1130 /// so we have some sort of upper bound on the number of probes to do.
1132 /// 'hash', 'k', and 'v' are the elements to robin hood into the hashtable.
1133 fn robin_hood(&mut self, mut index: table::FullIndex, mut dib_param: uint,
1134 mut hash: table::SafeHash, mut k: K, mut v: V) {
1136 let (old_hash, old_key, old_val) = {
1137 let (old_hash_ref, old_key_ref, old_val_ref) =
1138 self.table.read_all_mut(&index);
1140 let old_hash = replace(old_hash_ref, hash);
1141 let old_key = replace(old_key_ref, k);
1142 let old_val = replace(old_val_ref, v);
1144 (old_hash, old_key, old_val)
1147 let mut probe = self.probe_next(index.raw_index());
1149 for dib in range(dib_param + 1, self.table.size()) {
1150 let full_index = match self.table.peek(probe) {
1151 table::Empty(idx) => {
1153 self.table.put(idx, old_hash, old_key, old_val);
1156 table::Full(idx) => idx
1159 let probe_dib = self.bucket_distance(&full_index);
1161 // Robin hood! Steal the spot.
1162 if probe_dib < dib {
1164 dib_param = probe_dib;
1171 probe = self.probe_next(probe);
1174 fail!("HashMap fatal error: 100% load factor?");
1178 /// Insert a pre-hashed key-value pair, without first checking
1179 /// that there's enough room in the buckets. Returns a reference to the
1180 /// newly insert value.
1182 /// If the key already exists, the hashtable will be returned untouched
1183 /// and a reference to the existing element will be returned.
1184 fn insert_hashed_nocheck<'a>(
1185 &'a mut self, hash: table::SafeHash, k: K, v: V) -> &'a mut V {
1187 for dib in range_inclusive(0u, self.table.size()) {
1188 let probe = self.probe(&hash, dib);
1190 let idx = match self.table.peek(probe) {
1191 table::Empty(idx) => {
1193 let fullidx = self.table.put(idx, hash, k, v);
1194 let (_, val) = self.table.read_mut(&fullidx);
1197 table::Full(idx) => idx
1200 if idx.hash() == hash {
1201 let (bucket_k, bucket_v) = self.table.read_mut(&idx);
1202 // FIXME #12147 the conditional return confuses
1203 // borrowck if we return bucket_v directly
1204 let bv: *mut V = bucket_v;
1206 // Key already exists. Get its reference.
1207 return unsafe {&mut *bv};
1211 let probe_dib = self.bucket_distance(&idx);
1213 if probe_dib < dib {
1214 // Found a luckier bucket than me. Better steal his spot.
1215 self.robin_hood(idx, probe_dib, hash, k, v);
1217 // Now that it's stolen, just read the value's pointer
1218 // right out of the table!
1219 match self.table.peek(probe) {
1220 table::Empty(_) => fail!("Just stole a spot, but now that spot's empty."),
1221 table::Full(idx) => {
1222 let (_, v) = self.table.read_mut(&idx);
1229 // We really shouldn't be here.
1230 fail!("Internal HashMap error: Out of space.");
1233 /// Inserts an element which has already been hashed, returning a reference
1234 /// to that element inside the hashtable. This is more efficient that using
1235 /// `insert`, since the key will not be rehashed.
1236 fn insert_hashed<'a>(&'a mut self, hash: table::SafeHash, k: K, v: V) -> &'a mut V {
1237 let potential_new_size = self.table.size() + 1;
1238 self.make_some_room(potential_new_size);
1239 self.insert_hashed_nocheck(hash, k, v)
1242 /// Return the value corresponding to the key in the map, or insert
1243 /// and return the value if it doesn't exist.
1244 pub fn find_or_insert<'a>(&'a mut self, k: K, v: V) -> &'a mut V {
1245 self.find_with_or_insert_with(k, v, |_k, _v, _a| (), |_k, a| a)
1248 /// Return the value corresponding to the key in the map, or create,
1249 /// insert, and return a new value if it doesn't exist.
1250 pub fn find_or_insert_with<'a>(&'a mut self, k: K, f: |&K| -> V)
1252 self.find_with_or_insert_with(k, (), |_k, _v, _a| (), |k, _a| f(k))
1255 /// Insert a key-value pair into the map if the key is not already present.
1256 /// Otherwise, modify the existing value for the key.
1257 /// Returns the new or modified value for the key.
1258 pub fn insert_or_update_with<'a>(
1264 self.find_with_or_insert_with(k, v, |k, v, _a| f(k, v), |_k, a| a)
1267 /// Modify and return the value corresponding to the key in the map, or
1268 /// insert and return a new value if it doesn't exist.
1270 /// This method allows for all insertion behaviours of a hashmap;
1271 /// see methods like `insert`, `find_or_insert` and
1272 /// `insert_or_update_with` for less general and more friendly
1273 /// variations of this.
1278 /// use std::collections::HashMap;
1280 /// // map some strings to vectors of strings
1281 /// let mut map = HashMap::new();
1282 /// map.insert("a key", vec!["value"]);
1283 /// map.insert("z key", vec!["value"]);
1285 /// let new = vec!["a key", "b key", "z key"];
1287 /// for k in new.move_iter() {
1288 /// map.find_with_or_insert_with(
1290 /// // if the key does exist either prepend or append this
1291 /// // new value based on the first letter of the key.
1292 /// |key, already, new| {
1293 /// if key.as_slice().starts_with("z") {
1294 /// already.unshift(new);
1296 /// already.push(new);
1299 /// // if the key doesn't exist in the map yet, add it in
1300 /// // the obvious way.
1301 /// |_k, v| vec![v]);
1304 /// assert_eq!(map.len(), 3);
1305 /// assert_eq!(map.get(&"a key"), &vec!["value", "new value"]);
1306 /// assert_eq!(map.get(&"b key"), &vec!["new value"]);
1307 /// assert_eq!(map.get(&"z key"), &vec!["new value", "value"]);
1309 pub fn find_with_or_insert_with<'a, A>(&'a mut self,
1312 found: |&K, &mut V, A|,
1313 not_found: |&K, A| -> V)
1315 let hash = self.make_hash(&k);
1316 match self.search_hashed(&hash, &k) {
1318 let v = not_found(&k, a);
1319 self.insert_hashed(hash, k, v)
1322 let (_, v_ref) = self.table.read_mut(&idx);
1323 found(&k, v_ref, a);
1329 /// Retrieves a value for the given key, failing if the key is not present.
1330 pub fn get<'a>(&'a self, k: &K) -> &'a V {
1331 match self.find(k) {
1333 None => fail!("no entry found for key")
1337 /// Retrieves a (mutable) value for the given key, failing if the key is not present.
1338 pub fn get_mut<'a>(&'a mut self, k: &K) -> &'a mut V {
1339 match self.find_mut(k) {
1341 None => fail!("no entry found for key")
1345 /// Return true if the map contains a value for the specified key,
1346 /// using equivalence.
1347 pub fn contains_key_equiv<Q: Hash<S> + Equiv<K>>(&self, key: &Q) -> bool {
1348 self.search_equiv(key).is_some()
1351 /// Return the value corresponding to the key in the map, using
1353 pub fn find_equiv<'a, Q: Hash<S> + Equiv<K>>(&'a self, k: &Q) -> Option<&'a V> {
1354 match self.search_equiv(k) {
1357 let (_, v_ref) = self.table.read(&idx);
1363 /// An iterator visiting all keys in arbitrary order.
1364 /// Iterator element type is &'a K.
1365 pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
1366 self.iter().map(|(k, _v)| k)
1369 /// An iterator visiting all values in arbitrary order.
1370 /// Iterator element type is &'a V.
1371 pub fn values<'a>(&'a self) -> Values<'a, K, V> {
1372 self.iter().map(|(_k, v)| v)
1375 /// An iterator visiting all key-value pairs in arbitrary order.
1376 /// Iterator element type is (&'a K, &'a V).
1377 pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
1381 /// An iterator visiting all key-value pairs in arbitrary order,
1382 /// with mutable references to the values.
1383 /// Iterator element type is (&'a K, &'a mut V).
1384 pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
1385 self.table.mut_iter()
1388 /// Creates a consuming iterator, that is, one that moves each key-value
1389 /// pair out of the map in arbitrary order. The map cannot be used after
1391 pub fn move_iter(self) -> MoveEntries<K, V> {
1392 self.table.move_iter().map(|(_, k, v)| (k, v))
1396 impl<K: Eq + Hash<S>, V: Clone, S, H: Hasher<S>> HashMap<K, V, H> {
1397 /// Like `find`, but returns a copy of the value.
1398 pub fn find_copy(&self, k: &K) -> Option<V> {
1399 self.find(k).map(|v| (*v).clone())
1402 /// Like `get`, but returns a copy of the value.
1403 pub fn get_copy(&self, k: &K) -> V {
1404 (*self.get(k)).clone()
1408 impl<K: Eq + Hash<S>, V: PartialEq, S, H: Hasher<S>> PartialEq for HashMap<K, V, H> {
1409 fn eq(&self, other: &HashMap<K, V, H>) -> bool {
1410 if self.len() != other.len() { return false; }
1413 .all(|(key, value)| {
1414 match other.find(key) {
1416 Some(v) => *value == *v
1422 impl<K: Eq + Hash<S>, V: Eq, S, H: Hasher<S>> Eq for HashMap<K, V, H> {}
1424 impl<K: Eq + Hash<S> + Show, V: Show, S, H: Hasher<S>> Show for HashMap<K, V, H> {
1425 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1426 try!(write!(f, "{{"));
1428 for (i, (k, v)) in self.iter().enumerate() {
1429 if i != 0 { try!(write!(f, ", ")); }
1430 try!(write!(f, "{}: {}", *k, *v));
1437 impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Default for HashMap<K, V, H> {
1438 fn default() -> HashMap<K, V, H> {
1439 HashMap::with_hasher(Default::default())
1443 /// HashMap iterator
1444 pub type Entries<'a, K, V> = table::Entries<'a, K, V>;
1446 /// HashMap mutable values iterator
1447 pub type MutEntries<'a, K, V> = table::MutEntries<'a, K, V>;
1449 /// HashMap move iterator
1450 pub type MoveEntries<K, V> =
1451 iter::Map<'static, (table::SafeHash, K, V), (K, V), table::MoveEntries<K, V>>;
1453 /// HashMap keys iterator
1454 pub type Keys<'a, K, V> =
1455 iter::Map<'static, (&'a K, &'a V), &'a K, Entries<'a, K, V>>;
1457 /// HashMap values iterator
1458 pub type Values<'a, K, V> =
1459 iter::Map<'static, (&'a K, &'a V), &'a V, Entries<'a, K, V>>;
1461 impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> FromIterator<(K, V)> for HashMap<K, V, H> {
1462 fn from_iter<T: Iterator<(K, V)>>(iter: T) -> HashMap<K, V, H> {
1463 let (lower, _) = iter.size_hint();
1464 let mut map = HashMap::with_capacity_and_hasher(lower, Default::default());
1470 impl<K: Eq + Hash<S>, V, S, H: Hasher<S> + Default> Extendable<(K, V)> for HashMap<K, V, H> {
1471 fn extend<T: Iterator<(K, V)>>(&mut self, mut iter: T) {
1472 for (k, v) in iter {
1478 /// HashSet iterator
1479 pub type SetItems<'a, K> =
1480 iter::Map<'static, (&'a K, &'a ()), &'a K, Entries<'a, K, ()>>;
1482 /// HashSet move iterator
1483 pub type SetMoveItems<K> =
1484 iter::Map<'static, (K, ()), K, MoveEntries<K, ()>>;
1486 /// An implementation of a hash set using the underlying representation of a
1487 /// HashMap where the value is (). As with the `HashMap` type, a `HashSet`
1488 /// requires that the elements implement the `Eq` and `Hash` traits.
1493 /// use std::collections::HashSet;
1495 /// // Type inference lets us omit an explicit type signature (which
1496 /// // would be `HashSet<&str>` in this example).
1497 /// let mut books = HashSet::new();
1499 /// // Add some books.
1500 /// books.insert("A Dance With Dragons");
1501 /// books.insert("To Kill a Mockingbird");
1502 /// books.insert("The Odyssey");
1503 /// books.insert("The Great Gatsby");
1505 /// // Check for a specific one.
1506 /// if !books.contains(&("The Winds of Winter")) {
1507 /// println!("We have {} books, but The Winds of Winter ain't one.",
1511 /// // Remove a book.
1512 /// books.remove(&"The Odyssey");
1514 /// // Iterate over everything.
1515 /// for book in books.iter() {
1516 /// println!("{}", *book);
1520 /// The easiest way to use `HashSet` with a custom type is to derive
1521 /// `Eq` and `Hash`. We must also derive `PartialEq`, this will in the
1522 /// future be implied by `Eq`.
1525 /// use std::collections::HashSet;
1527 /// #[deriving(Hash, Eq, PartialEq, Show)]
1528 /// struct Viking<'a> {
1533 /// let mut vikings = HashSet::new();
1535 /// vikings.insert(Viking { name: "Einar", power: 9u });
1536 /// vikings.insert(Viking { name: "Einar", power: 9u });
1537 /// vikings.insert(Viking { name: "Olaf", power: 4u });
1538 /// vikings.insert(Viking { name: "Harald", power: 8u });
1540 /// // Use derived implementation to print the vikings.
1541 /// for x in vikings.iter() {
1542 /// println!("{}", x);
1546 pub struct HashSet<T, H = RandomSipHasher> {
1547 map: HashMap<T, (), H>
1550 impl<T: Hash + Eq> HashSet<T, RandomSipHasher> {
1551 /// Create an empty HashSet.
1556 /// use std::collections::HashSet;
1557 /// let mut set: HashSet<int> = HashSet::new();
1560 pub fn new() -> HashSet<T, RandomSipHasher> {
1561 HashSet::with_capacity(INITIAL_CAPACITY)
1564 /// Create an empty HashSet with space for at least `n` elements in
1570 /// use std::collections::HashSet;
1571 /// let mut set: HashSet<int> = HashSet::with_capacity(10);
1574 pub fn with_capacity(capacity: uint) -> HashSet<T, RandomSipHasher> {
1575 HashSet { map: HashMap::with_capacity(capacity) }
1579 impl<T: Eq + Hash<S>, S, H: Hasher<S>> HashSet<T, H> {
1580 /// Creates a new empty hash set which will use the given hasher to hash
1583 /// The hash set is also created with the default initial capacity.
1588 /// use std::collections::HashSet;
1589 /// use std::hash::sip::SipHasher;
1591 /// let h = SipHasher::new();
1592 /// let mut set = HashSet::with_hasher(h);
1596 pub fn with_hasher(hasher: H) -> HashSet<T, H> {
1597 HashSet::with_capacity_and_hasher(INITIAL_CAPACITY, hasher)
1600 /// Create an empty HashSet with space for at least `capacity`
1601 /// elements in the hash table, using `hasher` to hash the keys.
1603 /// Warning: `hasher` is normally randomly generated, and
1604 /// is designed to allow `HashSet`s to be resistant to attacks that
1605 /// cause many collisions and very poor performance. Setting it
1606 /// manually using this function can expose a DoS attack vector.
1611 /// use std::collections::HashSet;
1612 /// use std::hash::sip::SipHasher;
1614 /// let h = SipHasher::new();
1615 /// let mut set = HashSet::with_capacity_and_hasher(10u, h);
1619 pub fn with_capacity_and_hasher(capacity: uint, hasher: H) -> HashSet<T, H> {
1620 HashSet { map: HashMap::with_capacity_and_hasher(capacity, hasher) }
1623 /// Reserve space for at least `n` elements in the hash table.
1628 /// use std::collections::HashSet;
1629 /// let mut set: HashSet<int> = HashSet::new();
1630 /// set.reserve(10);
1632 pub fn reserve(&mut self, n: uint) {
1636 /// Returns true if the hash set contains a value equivalent to the
1637 /// given query value.
1641 /// This is a slightly silly example where we define the number's
1642 /// parity as the equivilance class. It is important that the
1643 /// values hash the same, which is why we implement `Hash`.
1646 /// use std::collections::HashSet;
1647 /// use std::hash::Hash;
1648 /// use std::hash::sip::SipState;
1650 /// #[deriving(Eq, PartialEq)]
1651 /// struct EvenOrOdd {
1655 /// impl Hash for EvenOrOdd {
1656 /// fn hash(&self, state: &mut SipState) {
1657 /// let parity = self.num % 2;
1658 /// parity.hash(state);
1662 /// impl Equiv<EvenOrOdd> for EvenOrOdd {
1663 /// fn equiv(&self, other: &EvenOrOdd) -> bool {
1664 /// self.num % 2 == other.num % 2
1668 /// let mut set = HashSet::new();
1669 /// set.insert(EvenOrOdd { num: 3u });
1671 /// assert!(set.contains_equiv(&EvenOrOdd { num: 3u }));
1672 /// assert!(set.contains_equiv(&EvenOrOdd { num: 5u }));
1673 /// assert!(!set.contains_equiv(&EvenOrOdd { num: 4u }));
1674 /// assert!(!set.contains_equiv(&EvenOrOdd { num: 2u }));
1677 pub fn contains_equiv<Q: Hash<S> + Equiv<T>>(&self, value: &Q) -> bool {
1678 self.map.contains_key_equiv(value)
1681 /// An iterator visiting all elements in arbitrary order.
1682 /// Iterator element type is &'a T.
1687 /// use std::collections::HashSet;
1689 /// let mut set = HashSet::new();
1690 /// set.insert("a");
1691 /// set.insert("b");
1693 /// // Will print in an arbitrary order.
1694 /// for x in set.iter() {
1695 /// println!("{}", x);
1698 pub fn iter<'a>(&'a self) -> SetItems<'a, T> {
1702 /// Creates a consuming iterator, that is, one that moves each value out
1703 /// of the set in arbitrary order. The set cannot be used after calling
1709 /// use std::collections::HashSet;
1711 /// let mut set = HashSet::new();
1712 /// set.insert("a".to_string());
1713 /// set.insert("b".to_string());
1715 /// // Not possible to collect to a Vec<String> with a regular `.iter()`.
1716 /// let v: Vec<String> = set.move_iter().collect();
1718 /// // Will print in an arbitrary order.
1719 /// for x in v.iter() {
1720 /// println!("{}", x);
1723 pub fn move_iter(self) -> SetMoveItems<T> {
1724 self.map.move_iter().map(|(k, _)| k)
1727 /// Visit the values representing the difference.
1732 /// use std::collections::HashSet;
1734 /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1735 /// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1737 /// // Can be seen as `a - b`.
1738 /// for x in a.difference(&b) {
1739 /// println!("{}", x); // Print 1
1742 /// let diff: HashSet<int> = a.difference(&b).map(|&x| x).collect();
1743 /// assert_eq!(diff, [1i].iter().map(|&x| x).collect());
1745 /// // Note that difference is not symmetric,
1746 /// // and `b - a` means something else:
1747 /// let diff: HashSet<int> = b.difference(&a).map(|&x| x).collect();
1748 /// assert_eq!(diff, [4i].iter().map(|&x| x).collect());
1750 pub fn difference<'a>(&'a self, other: &'a HashSet<T, H>) -> SetAlgebraItems<'a, T, H> {
1751 Repeat::new(other).zip(self.iter())
1752 .filter_map(|(other, elt)| {
1753 if !other.contains(elt) { Some(elt) } else { None }
1757 /// Visit the values representing the symmetric difference.
1762 /// use std::collections::HashSet;
1764 /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1765 /// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1767 /// // Print 1, 4 in arbitrary order.
1768 /// for x in a.symmetric_difference(&b) {
1769 /// println!("{}", x);
1772 /// let diff1: HashSet<int> = a.symmetric_difference(&b).map(|&x| x).collect();
1773 /// let diff2: HashSet<int> = b.symmetric_difference(&a).map(|&x| x).collect();
1775 /// assert_eq!(diff1, diff2);
1776 /// assert_eq!(diff1, [1i, 4].iter().map(|&x| x).collect());
1778 pub fn symmetric_difference<'a>(&'a self, other: &'a HashSet<T, H>)
1779 -> Chain<SetAlgebraItems<'a, T, H>, SetAlgebraItems<'a, T, H>> {
1780 self.difference(other).chain(other.difference(self))
1783 /// Visit the values representing the intersection.
1788 /// use std::collections::HashSet;
1790 /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1791 /// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1793 /// // Print 2, 3 in arbitrary order.
1794 /// for x in a.intersection(&b) {
1795 /// println!("{}", x);
1798 /// let diff: HashSet<int> = a.intersection(&b).map(|&x| x).collect();
1799 /// assert_eq!(diff, [2i, 3].iter().map(|&x| x).collect());
1801 pub fn intersection<'a>(&'a self, other: &'a HashSet<T, H>)
1802 -> SetAlgebraItems<'a, T, H> {
1803 Repeat::new(other).zip(self.iter())
1804 .filter_map(|(other, elt)| {
1805 if other.contains(elt) { Some(elt) } else { None }
1809 /// Visit the values representing the union.
1814 /// use std::collections::HashSet;
1816 /// let a: HashSet<int> = [1i, 2, 3].iter().map(|&x| x).collect();
1817 /// let b: HashSet<int> = [4i, 2, 3, 4].iter().map(|&x| x).collect();
1819 /// // Print 1, 2, 3, 4 in arbitrary order.
1820 /// for x in a.union(&b) {
1821 /// println!("{}", x);
1824 /// let diff: HashSet<int> = a.union(&b).map(|&x| x).collect();
1825 /// assert_eq!(diff, [1i, 2, 3, 4].iter().map(|&x| x).collect());
1827 pub fn union<'a>(&'a self, other: &'a HashSet<T, H>)
1828 -> Chain<SetItems<'a, T>, SetAlgebraItems<'a, T, H>> {
1829 self.iter().chain(other.difference(self))
1833 impl<T: Eq + Hash<S>, S, H: Hasher<S>> PartialEq for HashSet<T, H> {
1834 fn eq(&self, other: &HashSet<T, H>) -> bool {
1835 if self.len() != other.len() { return false; }
1837 self.iter().all(|key| other.contains(key))
1841 impl<T: Eq + Hash<S>, S, H: Hasher<S>> Eq for HashSet<T, H> {}
1843 impl<T: Eq + Hash<S>, S, H: Hasher<S>> Collection for HashSet<T, H> {
1844 fn len(&self) -> uint { self.map.len() }
1847 impl<T: Eq + Hash<S>, S, H: Hasher<S>> Mutable for HashSet<T, H> {
1848 fn clear(&mut self) { self.map.clear() }
1851 impl<T: Eq + Hash<S>, S, H: Hasher<S>> Set<T> for HashSet<T, H> {
1852 fn contains(&self, value: &T) -> bool { self.map.contains_key(value) }
1854 fn is_disjoint(&self, other: &HashSet<T, H>) -> bool {
1855 self.iter().all(|v| !other.contains(v))
1858 fn is_subset(&self, other: &HashSet<T, H>) -> bool {
1859 self.iter().all(|v| other.contains(v))
1863 impl<T: Eq + Hash<S>, S, H: Hasher<S>> MutableSet<T> for HashSet<T, H> {
1864 fn insert(&mut self, value: T) -> bool { self.map.insert(value, ()) }
1866 fn remove(&mut self, value: &T) -> bool { self.map.remove(value) }
1870 impl<T: Eq + Hash<S> + fmt::Show, S, H: Hasher<S>> fmt::Show for HashSet<T, H> {
1871 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1872 try!(write!(f, "{{"));
1874 for (i, x) in self.iter().enumerate() {
1875 if i != 0 { try!(write!(f, ", ")); }
1876 try!(write!(f, "{}", *x));
1883 impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> FromIterator<T> for HashSet<T, H> {
1884 fn from_iter<I: Iterator<T>>(iter: I) -> HashSet<T, H> {
1885 let (lower, _) = iter.size_hint();
1886 let mut set = HashSet::with_capacity_and_hasher(lower, Default::default());
1892 impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Extendable<T> for HashSet<T, H> {
1893 fn extend<I: Iterator<T>>(&mut self, mut iter: I) {
1900 impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
1901 fn default() -> HashSet<T, H> {
1902 HashSet::with_hasher(Default::default())
1906 // `Repeat` is used to feed the filter closure an explicit capture
1907 // of a reference to the other set
1908 /// Set operations iterator
1909 pub type SetAlgebraItems<'a, T, H> =
1910 FilterMap<'static, (&'a HashSet<T, H>, &'a T), &'a T,
1911 Zip<Repeat<&'a HashSet<T, H>>, SetItems<'a, T>>>;
1920 use iter::{Iterator,range_inclusive,range_step_inclusive};
1923 struct KindaIntLike(int);
1925 impl Equiv<int> for KindaIntLike {
1926 fn equiv(&self, other: &int) -> bool {
1927 let KindaIntLike(this) = *self;
1931 impl<S: hash::Writer> hash::Hash<S> for KindaIntLike {
1932 fn hash(&self, state: &mut S) {
1933 let KindaIntLike(this) = *self;
1939 fn test_create_capacity_zero() {
1940 let mut m = HashMap::with_capacity(0);
1942 assert!(m.insert(1i, 1i));
1944 assert!(m.contains_key(&1));
1945 assert!(!m.contains_key(&0));
1950 let mut m = HashMap::new();
1951 assert_eq!(m.len(), 0);
1952 assert!(m.insert(1i, 2i));
1953 assert_eq!(m.len(), 1);
1954 assert!(m.insert(2i, 4i));
1955 assert_eq!(m.len(), 2);
1956 assert_eq!(*m.find(&1).unwrap(), 2);
1957 assert_eq!(*m.find(&2).unwrap(), 4);
1960 local_data_key!(drop_vector: RefCell<Vec<int>>)
1962 #[deriving(Hash, PartialEq, Eq)]
1969 fn new(k: uint) -> Dropable {
1970 let v = drop_vector.get().unwrap();
1971 v.borrow_mut().as_mut_slice()[k] += 1;
1977 impl Drop for Dropable {
1978 fn drop(&mut self) {
1979 let v = drop_vector.get().unwrap();
1980 v.borrow_mut().as_mut_slice()[self.k] -= 1;
1986 drop_vector.replace(Some(RefCell::new(Vec::from_elem(200, 0i))));
1989 let mut m = HashMap::new();
1991 let v = drop_vector.get().unwrap();
1992 for i in range(0u, 200) {
1993 assert_eq!(v.borrow().as_slice()[i], 0);
1997 for i in range(0u, 100) {
1998 let d1 = Dropable::new(i);
1999 let d2 = Dropable::new(i+100);
2003 let v = drop_vector.get().unwrap();
2004 for i in range(0u, 200) {
2005 assert_eq!(v.borrow().as_slice()[i], 1);
2009 for i in range(0u, 50) {
2010 let k = Dropable::new(i);
2013 assert!(v.is_some());
2015 let v = drop_vector.get().unwrap();
2016 assert_eq!(v.borrow().as_slice()[i], 1);
2017 assert_eq!(v.borrow().as_slice()[i+100], 1);
2020 let v = drop_vector.get().unwrap();
2021 for i in range(0u, 50) {
2022 assert_eq!(v.borrow().as_slice()[i], 0);
2023 assert_eq!(v.borrow().as_slice()[i+100], 0);
2026 for i in range(50u, 100) {
2027 assert_eq!(v.borrow().as_slice()[i], 1);
2028 assert_eq!(v.borrow().as_slice()[i+100], 1);
2032 let v = drop_vector.get().unwrap();
2033 for i in range(0u, 200) {
2034 assert_eq!(v.borrow().as_slice()[i], 0);
2039 fn test_empty_pop() {
2040 let mut m: HashMap<int, bool> = HashMap::new();
2041 assert_eq!(m.pop(&0), None);
2045 fn test_lots_of_insertions() {
2046 let mut m = HashMap::new();
2048 // Try this a few times to make sure we never screw up the hashmap's
2050 for _ in range(0i, 10) {
2051 assert!(m.is_empty());
2053 for i in range_inclusive(1i, 1000) {
2054 assert!(m.insert(i, i));
2056 for j in range_inclusive(1, i) {
2058 assert_eq!(r, Some(&j));
2061 for j in range_inclusive(i+1, 1000) {
2063 assert_eq!(r, None);
2067 for i in range_inclusive(1001i, 2000) {
2068 assert!(!m.contains_key(&i));
2072 for i in range_inclusive(1i, 1000) {
2073 assert!(m.remove(&i));
2075 for j in range_inclusive(1, i) {
2076 assert!(!m.contains_key(&j));
2079 for j in range_inclusive(i+1, 1000) {
2080 assert!(m.contains_key(&j));
2084 for i in range_inclusive(1i, 1000) {
2085 assert!(!m.contains_key(&i));
2088 for i in range_inclusive(1i, 1000) {
2089 assert!(m.insert(i, i));
2093 for i in range_step_inclusive(1000i, 1, -1) {
2094 assert!(m.remove(&i));
2096 for j in range_inclusive(i, 1000) {
2097 assert!(!m.contains_key(&j));
2100 for j in range_inclusive(1, i-1) {
2101 assert!(m.contains_key(&j));
2108 fn test_find_mut() {
2109 let mut m = HashMap::new();
2110 assert!(m.insert(1i, 12i));
2111 assert!(m.insert(2i, 8i));
2112 assert!(m.insert(5i, 14i));
2114 match m.find_mut(&5) {
2115 None => fail!(), Some(x) => *x = new
2117 assert_eq!(m.find(&5), Some(&new));
2121 fn test_insert_overwrite() {
2122 let mut m = HashMap::new();
2123 assert!(m.insert(1i, 2i));
2124 assert_eq!(*m.find(&1).unwrap(), 2);
2125 assert!(!m.insert(1i, 3i));
2126 assert_eq!(*m.find(&1).unwrap(), 3);
2130 fn test_insert_conflicts() {
2131 let mut m = HashMap::with_capacity(4);
2132 assert!(m.insert(1i, 2i));
2133 assert!(m.insert(5i, 3i));
2134 assert!(m.insert(9i, 4i));
2135 assert_eq!(*m.find(&9).unwrap(), 4);
2136 assert_eq!(*m.find(&5).unwrap(), 3);
2137 assert_eq!(*m.find(&1).unwrap(), 2);
2141 fn test_conflict_remove() {
2142 let mut m = HashMap::with_capacity(4);
2143 assert!(m.insert(1i, 2i));
2144 assert_eq!(*m.find(&1).unwrap(), 2);
2145 assert!(m.insert(5, 3));
2146 assert_eq!(*m.find(&1).unwrap(), 2);
2147 assert_eq!(*m.find(&5).unwrap(), 3);
2148 assert!(m.insert(9, 4));
2149 assert_eq!(*m.find(&1).unwrap(), 2);
2150 assert_eq!(*m.find(&5).unwrap(), 3);
2151 assert_eq!(*m.find(&9).unwrap(), 4);
2152 assert!(m.remove(&1));
2153 assert_eq!(*m.find(&9).unwrap(), 4);
2154 assert_eq!(*m.find(&5).unwrap(), 3);
2158 fn test_is_empty() {
2159 let mut m = HashMap::with_capacity(4);
2160 assert!(m.insert(1i, 2i));
2161 assert!(!m.is_empty());
2162 assert!(m.remove(&1));
2163 assert!(m.is_empty());
2168 let mut m = HashMap::new();
2170 assert_eq!(m.pop(&1), Some(2));
2171 assert_eq!(m.pop(&1), None);
2175 #[allow(experimental)]
2176 fn test_pop_equiv() {
2177 let mut m = HashMap::new();
2179 assert_eq!(m.pop_equiv(&KindaIntLike(1)), Some(2));
2180 assert_eq!(m.pop_equiv(&KindaIntLike(1)), None);
2185 let mut m = HashMap::new();
2186 assert_eq!(m.swap(1i, 2i), None);
2187 assert_eq!(m.swap(1i, 3i), Some(2));
2188 assert_eq!(m.swap(1i, 4i), Some(3));
2192 fn test_move_iter() {
2194 let mut hm = HashMap::new();
2202 let v = hm.move_iter().collect::<Vec<(char, int)>>();
2203 assert!([('a', 1), ('b', 2)] == v.as_slice() || [('b', 2), ('a', 1)] == v.as_slice());
2208 let mut m = HashMap::with_capacity(4);
2209 for i in range(0u, 32) {
2210 assert!(m.insert(i, i*2));
2212 assert_eq!(m.len(), 32);
2214 let mut observed: u32 = 0;
2216 for (k, v) in m.iter() {
2217 assert_eq!(*v, *k * 2);
2218 observed |= 1 << *k;
2220 assert_eq!(observed, 0xFFFF_FFFF);
2225 let vec = vec![(1i, 'a'), (2i, 'b'), (3i, 'c')];
2226 let map = vec.move_iter().collect::<HashMap<int, char>>();
2227 let keys = map.keys().map(|&k| k).collect::<Vec<int>>();
2228 assert_eq!(keys.len(), 3);
2229 assert!(keys.contains(&1));
2230 assert!(keys.contains(&2));
2231 assert!(keys.contains(&3));
2236 let vec = vec![(1i, 'a'), (2i, 'b'), (3i, 'c')];
2237 let map = vec.move_iter().collect::<HashMap<int, char>>();
2238 let values = map.values().map(|&v| v).collect::<Vec<char>>();
2239 assert_eq!(values.len(), 3);
2240 assert!(values.contains(&'a'));
2241 assert!(values.contains(&'b'));
2242 assert!(values.contains(&'c'));
2247 let mut m = HashMap::new();
2248 assert!(m.find(&1i).is_none());
2252 Some(v) => assert_eq!(*v, 2)
2258 let mut m1 = HashMap::new();
2263 let mut m2 = HashMap::new();
2276 let mut map: HashMap<int, int> = HashMap::new();
2277 let empty: HashMap<int, int> = HashMap::new();
2282 let map_str = format!("{}", map);
2284 assert!(map_str == "{1: 2, 3: 4}".to_string() || map_str == "{3: 4, 1: 2}".to_string());
2285 assert_eq!(format!("{}", empty), "{}".to_string());
2290 let mut m = HashMap::new();
2292 assert_eq!(m.len(), 0);
2293 assert!(m.is_empty());
2296 let old_cap = m.table.capacity();
2297 while old_cap == m.table.capacity() {
2302 assert_eq!(m.len(), i);
2303 assert!(!m.is_empty());
2307 fn test_resize_policy() {
2308 let mut m = HashMap::new();
2310 assert_eq!(m.len(), 0);
2311 assert!(m.is_empty());
2313 let initial_cap = m.table.capacity();
2314 m.reserve(initial_cap * 2);
2315 let cap = m.table.capacity();
2317 assert_eq!(cap, initial_cap * 2);
2320 for _ in range(0, cap * 3 / 4) {
2325 assert_eq!(m.len(), i);
2326 assert_eq!(m.table.capacity(), cap);
2328 for _ in range(0, cap / 4) {
2333 let new_cap = m.table.capacity();
2334 assert_eq!(new_cap, cap * 2);
2336 for _ in range(0, cap / 2) {
2339 assert_eq!(m.table.capacity(), new_cap);
2342 for _ in range(0, cap / 2 - 1) {
2347 assert_eq!(m.table.capacity(), cap);
2348 assert_eq!(m.len(), i);
2349 assert!(!m.is_empty());
2353 fn test_find_equiv() {
2354 let mut m = HashMap::new();
2356 let (foo, bar, baz) = (1i,2i,3i);
2357 m.insert("foo".to_string(), foo);
2358 m.insert("bar".to_string(), bar);
2359 m.insert("baz".to_string(), baz);
2362 assert_eq!(m.find_equiv(&("foo")), Some(&foo));
2363 assert_eq!(m.find_equiv(&("bar")), Some(&bar));
2364 assert_eq!(m.find_equiv(&("baz")), Some(&baz));
2366 assert_eq!(m.find_equiv(&("qux")), None);
2370 fn test_from_iter() {
2371 let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2373 let map: HashMap<int, int> = xs.iter().map(|&x| x).collect();
2375 for &(k, v) in xs.iter() {
2376 assert_eq!(map.find(&k), Some(&v));
2381 fn test_size_hint() {
2382 let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2384 let map: HashMap<int, int> = xs.iter().map(|&x| x).collect();
2386 let mut iter = map.iter();
2388 for _ in iter.by_ref().take(3) {}
2390 assert_eq!(iter.size_hint(), (3, Some(3)));
2394 fn test_mut_size_hint() {
2395 let xs = [(1i, 1i), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2397 let mut map: HashMap<int, int> = xs.iter().map(|&x| x).collect();
2399 let mut iter = map.mut_iter();
2401 for _ in iter.by_ref().take(3) {}
2403 assert_eq!(iter.size_hint(), (3, Some(3)));
2412 use slice::ImmutableEqVector;
2413 use collections::Collection;
2416 fn test_disjoint() {
2417 let mut xs = HashSet::new();
2418 let mut ys = HashSet::new();
2419 assert!(xs.is_disjoint(&ys));
2420 assert!(ys.is_disjoint(&xs));
2421 assert!(xs.insert(5i));
2422 assert!(ys.insert(11i));
2423 assert!(xs.is_disjoint(&ys));
2424 assert!(ys.is_disjoint(&xs));
2425 assert!(xs.insert(7));
2426 assert!(xs.insert(19));
2427 assert!(xs.insert(4));
2428 assert!(ys.insert(2));
2429 assert!(ys.insert(-11));
2430 assert!(xs.is_disjoint(&ys));
2431 assert!(ys.is_disjoint(&xs));
2432 assert!(ys.insert(7));
2433 assert!(!xs.is_disjoint(&ys));
2434 assert!(!ys.is_disjoint(&xs));
2438 fn test_subset_and_superset() {
2439 let mut a = HashSet::new();
2440 assert!(a.insert(0i));
2441 assert!(a.insert(5));
2442 assert!(a.insert(11));
2443 assert!(a.insert(7));
2445 let mut b = HashSet::new();
2446 assert!(b.insert(0i));
2447 assert!(b.insert(7));
2448 assert!(b.insert(19));
2449 assert!(b.insert(250));
2450 assert!(b.insert(11));
2451 assert!(b.insert(200));
2453 assert!(!a.is_subset(&b));
2454 assert!(!a.is_superset(&b));
2455 assert!(!b.is_subset(&a));
2456 assert!(!b.is_superset(&a));
2458 assert!(b.insert(5));
2460 assert!(a.is_subset(&b));
2461 assert!(!a.is_superset(&b));
2462 assert!(!b.is_subset(&a));
2463 assert!(b.is_superset(&a));
2468 let mut a = HashSet::new();
2469 for i in range(0u, 32) {
2470 assert!(a.insert(i));
2472 let mut observed: u32 = 0;
2474 observed |= 1 << *k;
2476 assert_eq!(observed, 0xFFFF_FFFF);
2480 fn test_intersection() {
2481 let mut a = HashSet::new();
2482 let mut b = HashSet::new();
2484 assert!(a.insert(11i));
2485 assert!(a.insert(1));
2486 assert!(a.insert(3));
2487 assert!(a.insert(77));
2488 assert!(a.insert(103));
2489 assert!(a.insert(5));
2490 assert!(a.insert(-5));
2492 assert!(b.insert(2i));
2493 assert!(b.insert(11));
2494 assert!(b.insert(77));
2495 assert!(b.insert(-9));
2496 assert!(b.insert(-42));
2497 assert!(b.insert(5));
2498 assert!(b.insert(3));
2501 let expected = [3, 5, 11, 77];
2502 for x in a.intersection(&b) {
2503 assert!(expected.contains(x));
2506 assert_eq!(i, expected.len());
2510 fn test_difference() {
2511 let mut a = HashSet::new();
2512 let mut b = HashSet::new();
2514 assert!(a.insert(1i));
2515 assert!(a.insert(3));
2516 assert!(a.insert(5));
2517 assert!(a.insert(9));
2518 assert!(a.insert(11));
2520 assert!(b.insert(3i));
2521 assert!(b.insert(9));
2524 let expected = [1, 5, 11];
2525 for x in a.difference(&b) {
2526 assert!(expected.contains(x));
2529 assert_eq!(i, expected.len());
2533 fn test_symmetric_difference() {
2534 let mut a = HashSet::new();
2535 let mut b = HashSet::new();
2537 assert!(a.insert(1i));
2538 assert!(a.insert(3));
2539 assert!(a.insert(5));
2540 assert!(a.insert(9));
2541 assert!(a.insert(11));
2543 assert!(b.insert(-2i));
2544 assert!(b.insert(3));
2545 assert!(b.insert(9));
2546 assert!(b.insert(14));
2547 assert!(b.insert(22));
2550 let expected = [-2, 1, 5, 11, 14, 22];
2551 for x in a.symmetric_difference(&b) {
2552 assert!(expected.contains(x));
2555 assert_eq!(i, expected.len());
2560 let mut a = HashSet::new();
2561 let mut b = HashSet::new();
2563 assert!(a.insert(1i));
2564 assert!(a.insert(3));
2565 assert!(a.insert(5));
2566 assert!(a.insert(9));
2567 assert!(a.insert(11));
2568 assert!(a.insert(16));
2569 assert!(a.insert(19));
2570 assert!(a.insert(24));
2572 assert!(b.insert(-2i));
2573 assert!(b.insert(1));
2574 assert!(b.insert(5));
2575 assert!(b.insert(9));
2576 assert!(b.insert(13));
2577 assert!(b.insert(19));
2580 let expected = [-2, 1, 3, 5, 9, 11, 13, 16, 19, 24];
2581 for x in a.union(&b) {
2582 assert!(expected.contains(x));
2585 assert_eq!(i, expected.len());
2589 fn test_from_iter() {
2590 let xs = [1i, 2, 3, 4, 5, 6, 7, 8, 9];
2592 let set: HashSet<int> = xs.iter().map(|&x| x).collect();
2594 for x in xs.iter() {
2595 assert!(set.contains(x));
2600 fn test_move_iter() {
2602 let mut hs = HashSet::new();
2610 let v = hs.move_iter().collect::<Vec<char>>();
2611 assert!(['a', 'b'] == v.as_slice() || ['b', 'a'] == v.as_slice());
2616 // These constants once happened to expose a bug in insert().
2617 // I'm keeping them around to prevent a regression.
2618 let mut s1 = HashSet::new();
2624 let mut s2 = HashSet::new();
2638 let mut set: HashSet<int> = HashSet::new();
2639 let empty: HashSet<int> = HashSet::new();
2644 let set_str = format!("{}", set);
2646 assert!(set_str == "{1, 2}".to_string() || set_str == "{2, 1}".to_string());
2647 assert_eq!(format!("{}", empty), "{}".to_string());
2656 use self::test::Bencher;
2657 use iter::{range_inclusive};
2660 fn new_drop(b : &mut Bencher) {
2664 let m : HashMap<int, int> = HashMap::new();
2665 assert_eq!(m.len(), 0);
2670 fn new_insert_drop(b : &mut Bencher) {
2674 let mut m = HashMap::new();
2676 assert_eq!(m.len(), 1);
2681 fn insert(b: &mut Bencher) {
2684 let mut m = HashMap::new();
2686 for i in range_inclusive(1i, 1000) {
2699 fn find_existing(b: &mut Bencher) {
2702 let mut m = HashMap::new();
2704 for i in range_inclusive(1i, 1000) {
2709 for i in range_inclusive(1i, 1000) {
2716 fn find_nonexisting(b: &mut Bencher) {
2719 let mut m = HashMap::new();
2721 for i in range_inclusive(1i, 1000) {
2726 for i in range_inclusive(1001i, 2000) {
2733 fn hashmap_as_queue(b: &mut Bencher) {
2736 let mut m = HashMap::new();
2738 for i in range_inclusive(1i, 1000) {
2746 m.insert(k + 1000, k + 1000);
2752 fn find_pop_insert(b: &mut Bencher) {
2755 let mut m = HashMap::new();
2757 for i in range_inclusive(1i, 1000) {
2765 m.find(&(k + 2000));
2767 m.insert(k + 1000, k + 1000);