1 // Copyright 2014-2015 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.
12 use self::SearchResult::*;
13 use self::VacantEntryState::*;
17 use cmp::{max, Eq, PartialEq};
19 use fmt::{self, Debug};
20 use hash::{Hash, SipHasher};
21 use iter::{self, Iterator, ExactSizeIterator, IntoIterator, FromIterator, Extend, Map};
23 use mem::{self, replace};
24 use ops::{Deref, FnMut, FnOnce, Index};
25 use option::Option::{self, Some, None};
26 use rand::{self, Rng};
39 use super::table::BucketState::{
43 use super::state::HashState;
45 const INITIAL_LOG2_CAP: usize = 5;
46 const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5
48 /// The default behavior of HashMap implements a load factor of 90.9%.
49 /// This behavior is characterized by the following condition:
51 /// - if size > 0.909 * capacity: grow the map
53 struct DefaultResizePolicy;
55 impl DefaultResizePolicy {
56 fn new() -> DefaultResizePolicy {
61 fn min_capacity(&self, usable_size: usize) -> usize {
62 // Here, we are rephrasing the logic by specifying the lower limit
65 // - if `cap < size * 1.1`: grow the map
69 /// An inverse of `min_capacity`, approximately.
71 fn usable_capacity(&self, cap: usize) -> usize {
72 // As the number of entries approaches usable capacity,
73 // min_capacity(size) must be smaller than the internal capacity,
74 // so that the map is not resized:
75 // `min_capacity(usable_capacity(x)) <= x`.
76 // The left-hand side can only be smaller due to flooring by integer
79 // This doesn't have to be checked for overflow since allocation size
80 // in bytes will overflow earlier than multiplication by 10.
86 fn test_resize_policy() {
87 let rp = DefaultResizePolicy;
89 assert!(rp.min_capacity(rp.usable_capacity(n)) <= n);
90 assert!(rp.usable_capacity(rp.min_capacity(n)) <= n);
94 // The main performance trick in this hashmap is called Robin Hood Hashing.
95 // It gains its excellent performance from one essential operation:
97 // If an insertion collides with an existing element, and that element's
98 // "probe distance" (how far away the element is from its ideal location)
99 // is higher than how far we've already probed, swap the elements.
101 // This massively lowers variance in probe distance, and allows us to get very
102 // high load factors with good performance. The 90% load factor I use is rather
105 // > Why a load factor of approximately 90%?
107 // In general, all the distances to initial buckets will converge on the mean.
108 // At a load factor of α, the odds of finding the target bucket after k
109 // probes is approximately 1-α^k. If we set this equal to 50% (since we converge
110 // on the mean) and set k=8 (64-byte cache line / 8-byte hash), α=0.92. I round
111 // this down to make the math easier on the CPU and avoid its FPU.
112 // Since on average we start the probing in the middle of a cache line, this
113 // strategy pulls in two cache lines of hashes on every lookup. I think that's
114 // pretty good, but if you want to trade off some space, it could go down to one
115 // cache line on average with an α of 0.84.
117 // > Wait, what? Where did you get 1-α^k from?
119 // On the first probe, your odds of a collision with an existing element is α.
120 // The odds of doing this twice in a row is approximately α^2. For three times,
121 // α^3, etc. Therefore, the odds of colliding k times is α^k. The odds of NOT
122 // colliding after k tries is 1-α^k.
124 // The paper from 1986 cited below mentions an implementation which keeps track
125 // of the distance-to-initial-bucket histogram. This approach is not suitable
126 // for modern architectures because it requires maintaining an internal data
127 // structure. This allows very good first guesses, but we are most concerned
128 // with guessing entire cache lines, not individual indexes. Furthermore, array
129 // accesses are no longer linear and in one direction, as we have now. There
130 // is also memory and cache pressure that this would entail that would be very
131 // difficult to properly see in a microbenchmark.
133 // ## Future Improvements (FIXME!)
135 // Allow the load factor to be changed dynamically and/or at initialization.
137 // Also, would it be possible for us to reuse storage when growing the
138 // underlying table? This is exactly the use case for 'realloc', and may
139 // be worth exploring.
141 // ## Future Optimizations (FIXME!)
143 // Another possible design choice that I made without any real reason is
144 // parameterizing the raw table over keys and values. Technically, all we need
145 // is the size and alignment of keys and values, and the code should be just as
146 // efficient (well, we might need one for power-of-two size and one for not...).
147 // This has the potential to reduce code bloat in rust executables, without
148 // really losing anything except 4 words (key size, key alignment, val size,
149 // val alignment) which can be passed in to every call of a `RawTable` function.
150 // This would definitely be an avenue worth exploring if people start complaining
151 // about the size of rust executables.
153 // Annotate exceedingly likely branches in `table::make_hash`
154 // and `search_hashed` to reduce instruction cache pressure
155 // and mispredictions once it becomes possible (blocked on issue #11092).
157 // Shrinking the table could simply reallocate in place after moving buckets
158 // to the first half.
160 // The growth algorithm (fragment of the Proof of Correctness)
161 // --------------------
163 // The growth algorithm is basically a fast path of the naive reinsertion-
164 // during-resize algorithm. Other paths should never be taken.
166 // Consider growing a robin hood hashtable of capacity n. Normally, we do this
167 // by allocating a new table of capacity `2n`, and then individually reinsert
168 // each element in the old table into the new one. This guarantees that the
169 // new table is a valid robin hood hashtable with all the desired statistical
170 // properties. Remark that the order we reinsert the elements in should not
171 // matter. For simplicity and efficiency, we will consider only linear
172 // reinsertions, which consist of reinserting all elements in the old table
173 // into the new one by increasing order of index. However we will not be
174 // starting our reinsertions from index 0 in general. If we start from index
175 // i, for the purpose of reinsertion we will consider all elements with real
176 // index j < i to have virtual index n + j.
178 // Our hash generation scheme consists of generating a 64-bit hash and
179 // truncating the most significant bits. When moving to the new table, we
180 // simply introduce a new bit to the front of the hash. Therefore, if an
181 // elements has ideal index i in the old table, it can have one of two ideal
182 // locations in the new table. If the new bit is 0, then the new ideal index
183 // is i. If the new bit is 1, then the new ideal index is n + i. Intuitively,
184 // we are producing two independent tables of size n, and for each element we
185 // independently choose which table to insert it into with equal probability.
186 // However the rather than wrapping around themselves on overflowing their
187 // indexes, the first table overflows into the first, and the first into the
188 // second. Visually, our new table will look something like:
190 // [yy_xxx_xxxx_xxx|xx_yyy_yyyy_yyy]
192 // Where x's are elements inserted into the first table, y's are elements
193 // inserted into the second, and _'s are empty sections. We now define a few
194 // key concepts that we will use later. Note that this is a very abstract
195 // perspective of the table. A real resized table would be at least half
198 // Theorem: A linear robin hood reinsertion from the first ideal element
199 // produces identical results to a linear naive reinsertion from the same
202 // FIXME(Gankro, pczarn): review the proof and put it all in a separate README.md
204 /// A hash map implementation which uses linear probing with Robin
205 /// Hood bucket stealing.
207 /// The hashes are all keyed by the thread-local random number generator
208 /// on creation by default. This means that the ordering of the keys is
209 /// randomized, but makes the tables more resistant to
210 /// denial-of-service attacks (Hash DoS). This behavior can be
211 /// overridden with one of the constructors.
213 /// It is required that the keys implement the `Eq` and `Hash` traits, although
214 /// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
215 /// If you implement these yourself, it is important that the following
219 /// k1 == k2 -> hash(k1) == hash(k2)
222 /// In other words, if two keys are equal, their hashes must be equal.
224 /// It is a logic error for a key to be modified in such a way that the key's
225 /// hash, as determined by the `Hash` trait, or its equality, as determined by
226 /// the `Eq` trait, changes while it is in the map. This is normally only
227 /// possible through `Cell`, `RefCell`, global state, I/O, or unsafe code.
229 /// Relevant papers/articles:
231 /// 1. Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
232 /// 2. Emmanuel Goossaert. ["Robin Hood
233 /// hashing"](http://codecapsule.com/2013/11/11/robin-hood-hashing/)
234 /// 3. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
235 /// deletion"](http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
240 /// use std::collections::HashMap;
242 /// // type inference lets us omit an explicit type signature (which
243 /// // would be `HashMap<&str, &str>` in this example).
244 /// let mut book_reviews = HashMap::new();
246 /// // review some books.
247 /// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.");
248 /// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
249 /// book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
250 /// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
252 /// // check for a specific one.
253 /// if !book_reviews.contains_key("Les Misérables") {
254 /// println!("We've got {} reviews, but Les Misérables ain't one.",
255 /// book_reviews.len());
258 /// // oops, this review has a lot of spelling mistakes, let's delete it.
259 /// book_reviews.remove("The Adventures of Sherlock Holmes");
261 /// // look up the values associated with some keys.
262 /// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
263 /// for book in &to_find {
264 /// match book_reviews.get(book) {
265 /// Some(review) => println!("{}: {}", book, review),
266 /// None => println!("{} is unreviewed.", book)
270 /// // iterate over everything.
271 /// for (book, review) in &book_reviews {
272 /// println!("{}: \"{}\"", book, review);
276 /// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
277 /// We must also derive `PartialEq`.
280 /// use std::collections::HashMap;
282 /// #[derive(Hash, Eq, PartialEq, Debug)]
289 /// /// Create a new Viking.
290 /// fn new(name: &str, country: &str) -> Viking {
291 /// Viking { name: name.to_string(), country: country.to_string() }
295 /// // Use a HashMap to store the vikings' health points.
296 /// let mut vikings = HashMap::new();
298 /// vikings.insert(Viking::new("Einar", "Norway"), 25);
299 /// vikings.insert(Viking::new("Olaf", "Denmark"), 24);
300 /// vikings.insert(Viking::new("Harald", "Iceland"), 12);
302 /// // Use derived implementation to print the status of the vikings.
303 /// for (viking, health) in &vikings {
304 /// println!("{:?} has {} hp", viking, health);
308 #[stable(feature = "rust1", since = "1.0.0")]
309 pub struct HashMap<K, V, S = RandomState> {
310 // All hashes are keyed on these values, to prevent hash collision attacks.
313 table: RawTable<K, V>,
315 resize_policy: DefaultResizePolicy,
318 /// Search for a pre-hashed key.
319 fn search_hashed<K, V, M, F>(table: M,
322 -> SearchResult<K, V, M> where
323 M: Deref<Target=RawTable<K, V>>,
324 F: FnMut(&K) -> bool,
326 // This is the only function where capacity can be zero. To avoid
327 // undefined behavior when Bucket::new gets the raw bucket in this
328 // case, immediately return the appropriate search result.
329 if table.capacity() == 0 {
330 return TableRef(table);
333 let size = table.size();
334 let mut probe = Bucket::new(table, hash);
335 let ib = probe.index();
337 while probe.index() != ib + size {
338 let full = match probe.peek() {
339 Empty(b) => return TableRef(b.into_table()), // hit an empty bucket
343 if full.distance() + ib < full.index() {
344 // We can finish the search early if we hit any bucket
345 // with a lower distance to initial bucket than we've probed.
346 return TableRef(full.into_table());
349 // If the hash doesn't match, it can't be this one..
350 if hash == full.hash() {
351 // If the key doesn't match, it can't be this one..
352 if is_match(full.read().0) {
353 return FoundExisting(full);
360 TableRef(probe.into_table())
363 fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
364 let (empty, retkey, retval) = starting_bucket.take();
365 let mut gap = match empty.gap_peek() {
367 None => return (retkey, retval)
370 while gap.full().distance() != 0 {
371 gap = match gap.shift() {
377 // Now we've done all our shifting. Return the value we grabbed earlier.
381 /// Perform robin hood bucket stealing at the given `bucket`. You must
382 /// also pass the position of that bucket's initial bucket so we don't have
383 /// to recalculate it.
385 /// `hash`, `k`, and `v` are the elements to "robin hood" into the hashtable.
386 fn robin_hood<'a, K: 'a, V: 'a>(mut bucket: FullBucketMut<'a, K, V>,
392 let starting_index = bucket.index();
394 let table = bucket.table(); // FIXME "lifetime too short".
397 // There can be at most `size - dib` buckets to displace, because
398 // in the worst case, there are `size` elements and we already are
399 // `distance` buckets away from the initial one.
400 let idx_end = starting_index + size - bucket.distance();
403 let (old_hash, old_key, old_val) = bucket.replace(hash, k, v);
405 let probe = bucket.next();
406 assert!(probe.index() != idx_end);
408 let full_bucket = match probe.peek() {
411 let b = bucket.put(old_hash, old_key, old_val);
412 // Now that it's stolen, just read the value's pointer
413 // right out of the table!
414 return Bucket::at_index(b.into_table(), starting_index)
420 Full(bucket) => bucket
423 let probe_ib = full_bucket.index() - full_bucket.distance();
425 bucket = full_bucket;
427 // Robin hood! Steal the spot.
439 /// A result that works like Option<FullBucket<..>> but preserves
440 /// the reference that grants us access to the table in any case.
441 enum SearchResult<K, V, M> {
442 // This is an entry that holds the given key:
443 FoundExisting(FullBucket<K, V, M>),
445 // There was no such entry. The reference is given back:
449 impl<K, V, M> SearchResult<K, V, M> {
450 fn into_option(self) -> Option<FullBucket<K, V, M>> {
452 FoundExisting(bucket) => Some(bucket),
458 impl<K, V, S> HashMap<K, V, S>
459 where K: Eq + Hash, S: HashState
461 fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash where X: Hash {
462 table::make_hash(&self.hash_state, x)
465 /// Search for a key, yielding the index if it's found in the hashtable.
466 /// If you already have the hash for the key lying around, use
468 fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> Option<FullBucketImm<'a, K, V>>
469 where K: Borrow<Q>, Q: Eq + Hash
471 let hash = self.make_hash(q);
472 search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
476 fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> Option<FullBucketMut<'a, K, V>>
477 where K: Borrow<Q>, Q: Eq + Hash
479 let hash = self.make_hash(q);
480 search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
484 // The caller should ensure that invariants by Robin Hood Hashing hold.
485 fn insert_hashed_ordered(&mut self, hash: SafeHash, k: K, v: V) {
486 let cap = self.table.capacity();
487 let mut buckets = Bucket::new(&mut self.table, hash);
488 let ib = buckets.index();
490 while buckets.index() != ib + cap {
491 // We don't need to compare hashes for value swap.
492 // Not even DIBs for Robin Hood.
493 buckets = match buckets.peek() {
495 empty.put(hash, k, v);
498 Full(b) => b.into_bucket()
502 panic!("Internal HashMap error: Out of space.");
506 impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
507 /// Creates an empty HashMap.
512 /// use std::collections::HashMap;
513 /// let mut map: HashMap<&str, isize> = HashMap::new();
516 #[stable(feature = "rust1", since = "1.0.0")]
517 pub fn new() -> HashMap<K, V, RandomState> {
521 /// Creates an empty hash map with the given initial capacity.
526 /// use std::collections::HashMap;
527 /// let mut map: HashMap<&str, isize> = HashMap::with_capacity(10);
530 #[stable(feature = "rust1", since = "1.0.0")]
531 pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
532 HashMap::with_capacity_and_hash_state(capacity, Default::default())
536 impl<K, V, S> HashMap<K, V, S>
537 where K: Eq + Hash, S: HashState
539 /// Creates an empty hashmap which will use the given hasher to hash keys.
541 /// The created map has the default initial capacity.
546 /// #![feature(hashmap_hasher)]
548 /// use std::collections::HashMap;
549 /// use std::collections::hash_map::RandomState;
551 /// let s = RandomState::new();
552 /// let mut map = HashMap::with_hash_state(s);
553 /// map.insert(1, 2);
556 #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
558 pub fn with_hash_state(hash_state: S) -> HashMap<K, V, S> {
560 hash_state: hash_state,
561 resize_policy: DefaultResizePolicy::new(),
562 table: RawTable::new(0),
566 /// Creates an empty HashMap with space for at least `capacity`
567 /// elements, using `hasher` to hash the keys.
569 /// Warning: `hasher` is normally randomly generated, and
570 /// is designed to allow HashMaps to be resistant to attacks that
571 /// cause many collisions and very poor performance. Setting it
572 /// manually using this function can expose a DoS attack vector.
577 /// #![feature(hashmap_hasher)]
579 /// use std::collections::HashMap;
580 /// use std::collections::hash_map::RandomState;
582 /// let s = RandomState::new();
583 /// let mut map = HashMap::with_capacity_and_hash_state(10, s);
584 /// map.insert(1, 2);
587 #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
589 pub fn with_capacity_and_hash_state(capacity: usize, hash_state: S)
590 -> HashMap<K, V, S> {
591 let resize_policy = DefaultResizePolicy::new();
592 let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity));
593 let internal_cap = min_cap.checked_next_power_of_two().expect("capacity overflow");
594 assert!(internal_cap >= capacity, "capacity overflow");
596 hash_state: hash_state,
597 resize_policy: resize_policy,
598 table: RawTable::new(internal_cap),
602 /// Returns the number of elements the map can hold without reallocating.
604 /// This number is a lower bound; the `HashMap<K, V>` might be able to hold
605 /// more, but is guaranteed to be able to hold at least this many.
610 /// use std::collections::HashMap;
611 /// let map: HashMap<isize, isize> = HashMap::with_capacity(100);
612 /// assert!(map.capacity() >= 100);
615 #[stable(feature = "rust1", since = "1.0.0")]
616 pub fn capacity(&self) -> usize {
617 self.resize_policy.usable_capacity(self.table.capacity())
620 /// Reserves capacity for at least `additional` more elements to be inserted
621 /// in the `HashMap`. The collection may reserve more space to avoid
622 /// frequent reallocations.
626 /// Panics if the new allocation size overflows `usize`.
631 /// use std::collections::HashMap;
632 /// let mut map: HashMap<&str, isize> = HashMap::new();
635 #[stable(feature = "rust1", since = "1.0.0")]
636 pub fn reserve(&mut self, additional: usize) {
637 let new_size = self.len().checked_add(additional).expect("capacity overflow");
638 let min_cap = self.resize_policy.min_capacity(new_size);
640 // An invalid value shouldn't make us run out of space. This includes
641 // an overflow check.
642 assert!(new_size <= min_cap);
644 if self.table.capacity() < min_cap {
645 let new_capacity = max(min_cap.next_power_of_two(), INITIAL_CAPACITY);
646 self.resize(new_capacity);
650 /// Resizes the internal vectors to a new capacity. It's your responsibility to:
651 /// 1) Make sure the new capacity is enough for all the elements, accounting
652 /// for the load factor.
653 /// 2) Ensure new_capacity is a power of two or zero.
654 fn resize(&mut self, new_capacity: usize) {
655 assert!(self.table.size() <= new_capacity);
656 assert!(new_capacity.is_power_of_two() || new_capacity == 0);
658 let mut old_table = replace(&mut self.table, RawTable::new(new_capacity));
659 let old_size = old_table.size();
661 if old_table.capacity() == 0 || old_table.size() == 0 {
666 // Specialization of the other branch.
667 let mut bucket = Bucket::first(&mut old_table);
669 // "So a few of the first shall be last: for many be called,
672 // We'll most likely encounter a few buckets at the beginning that
673 // have their initial buckets near the end of the table. They were
674 // placed at the beginning as the probe wrapped around the table
675 // during insertion. We must skip forward to a bucket that won't
676 // get reinserted too early and won't unfairly steal others spot.
677 // This eliminates the need for robin hood.
679 bucket = match bucket.peek() {
681 if full.distance() == 0 {
682 // This bucket occupies its ideal spot.
683 // It indicates the start of another "cluster".
684 bucket = full.into_bucket();
687 // Leaving this bucket in the last cluster for later.
691 // Encountered a hole between clusters.
698 // This is how the buckets might be laid out in memory:
699 // ($ marks an initialized bucket)
701 // |$$$_$$$$$$_$$$$$|
703 // But we've skipped the entire initial cluster of buckets
704 // and will continue iteration in this order:
707 // ^ wrap around once end is reached
710 // ^ exit once table.size == 0
712 bucket = match bucket.peek() {
714 let h = bucket.hash();
715 let (b, k, v) = bucket.take();
716 self.insert_hashed_ordered(h, k, v);
718 let t = b.table(); // FIXME "lifetime too short".
719 if t.size() == 0 { break }
723 Empty(b) => b.into_bucket()
728 assert_eq!(self.table.size(), old_size);
731 /// Shrinks the capacity of the map as much as possible. It will drop
732 /// down as much as possible while maintaining the internal rules
733 /// and possibly leaving some space in accordance with the resize policy.
738 /// use std::collections::HashMap;
740 /// let mut map: HashMap<isize, isize> = HashMap::with_capacity(100);
741 /// map.insert(1, 2);
742 /// map.insert(3, 4);
743 /// assert!(map.capacity() >= 100);
744 /// map.shrink_to_fit();
745 /// assert!(map.capacity() >= 2);
747 #[stable(feature = "rust1", since = "1.0.0")]
748 pub fn shrink_to_fit(&mut self) {
749 let min_capacity = self.resize_policy.min_capacity(self.len());
750 let min_capacity = max(min_capacity.next_power_of_two(), INITIAL_CAPACITY);
752 // An invalid value shouldn't make us run out of space.
753 debug_assert!(self.len() <= min_capacity);
755 if self.table.capacity() != min_capacity {
756 let old_table = replace(&mut self.table, RawTable::new(min_capacity));
757 let old_size = old_table.size();
759 // Shrink the table. Naive algorithm for resizing:
760 for (h, k, v) in old_table.into_iter() {
761 self.insert_hashed_nocheck(h, k, v);
764 debug_assert_eq!(self.table.size(), old_size);
768 /// Insert a pre-hashed key-value pair, without first checking
769 /// that there's enough room in the buckets. Returns a reference to the
770 /// newly insert value.
772 /// If the key already exists, the hashtable will be returned untouched
773 /// and a reference to the existing element will be returned.
774 fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> &mut V {
775 self.insert_or_replace_with(hash, k, v, |_, _, _, _| ())
778 fn insert_or_replace_with<'a, F>(&'a mut self,
782 mut found_existing: F)
784 F: FnMut(&mut K, &mut V, K, V),
786 // Worst case, we'll find one empty bucket among `size + 1` buckets.
787 let size = self.table.size();
788 let mut probe = Bucket::new(&mut self.table, hash);
789 let ib = probe.index();
792 let mut bucket = match probe.peek() {
795 return bucket.put(hash, k, v).into_mut_refs().1;
797 Full(bucket) => bucket
801 if bucket.hash() == hash {
803 if k == *bucket.read_mut().0 {
804 let (bucket_k, bucket_v) = bucket.into_mut_refs();
805 debug_assert!(k == *bucket_k);
806 // Key already exists. Get its reference.
807 found_existing(bucket_k, bucket_v, k, v);
812 let robin_ib = bucket.index() as isize - bucket.distance() as isize;
814 if (ib as isize) < robin_ib {
815 // Found a luckier bucket than me. Better steal his spot.
816 return robin_hood(bucket, robin_ib as usize, hash, k, v);
819 probe = bucket.next();
820 assert!(probe.index() != ib + size + 1);
824 /// An iterator visiting all keys in arbitrary order.
825 /// Iterator element type is `&'a K`.
830 /// use std::collections::HashMap;
832 /// let mut map = HashMap::new();
833 /// map.insert("a", 1);
834 /// map.insert("b", 2);
835 /// map.insert("c", 3);
837 /// for key in map.keys() {
838 /// println!("{}", key);
841 #[stable(feature = "rust1", since = "1.0.0")]
842 pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
843 fn first<A, B>((a, _): (A, B)) -> A { a }
844 let first: fn((&'a K,&'a V)) -> &'a K = first; // coerce to fn ptr
846 Keys { inner: self.iter().map(first) }
849 /// An iterator visiting all values in arbitrary order.
850 /// Iterator element type is `&'a V`.
855 /// use std::collections::HashMap;
857 /// let mut map = HashMap::new();
858 /// map.insert("a", 1);
859 /// map.insert("b", 2);
860 /// map.insert("c", 3);
862 /// for val in map.values() {
863 /// println!("{}", val);
866 #[stable(feature = "rust1", since = "1.0.0")]
867 pub fn values<'a>(&'a self) -> Values<'a, K, V> {
868 fn second<A, B>((_, b): (A, B)) -> B { b }
869 let second: fn((&'a K,&'a V)) -> &'a V = second; // coerce to fn ptr
871 Values { inner: self.iter().map(second) }
874 /// An iterator visiting all key-value pairs in arbitrary order.
875 /// Iterator element type is `(&'a K, &'a V)`.
880 /// use std::collections::HashMap;
882 /// let mut map = HashMap::new();
883 /// map.insert("a", 1);
884 /// map.insert("b", 2);
885 /// map.insert("c", 3);
887 /// for (key, val) in map.iter() {
888 /// println!("key: {} val: {}", key, val);
891 #[stable(feature = "rust1", since = "1.0.0")]
892 pub fn iter(&self) -> Iter<K, V> {
893 Iter { inner: self.table.iter() }
896 /// An iterator visiting all key-value pairs in arbitrary order,
897 /// with mutable references to the values.
898 /// Iterator element type is `(&'a K, &'a mut V)`.
903 /// use std::collections::HashMap;
905 /// let mut map = HashMap::new();
906 /// map.insert("a", 1);
907 /// map.insert("b", 2);
908 /// map.insert("c", 3);
910 /// // Update all values
911 /// for (_, val) in map.iter_mut() {
915 /// for (key, val) in &map {
916 /// println!("key: {} val: {}", key, val);
919 #[stable(feature = "rust1", since = "1.0.0")]
920 pub fn iter_mut(&mut self) -> IterMut<K, V> {
921 IterMut { inner: self.table.iter_mut() }
924 /// Gets the given key's corresponding entry in the map for in-place manipulation.
929 /// use std::collections::HashMap;
931 /// let mut letters = HashMap::new();
933 /// for ch in "a short treatise on fungi".chars() {
934 /// let counter = letters.entry(ch).or_insert(0);
938 /// assert_eq!(letters[&'s'], 2);
939 /// assert_eq!(letters[&'t'], 3);
940 /// assert_eq!(letters[&'u'], 1);
941 /// assert_eq!(letters.get(&'y'), None);
943 #[stable(feature = "rust1", since = "1.0.0")]
944 pub fn entry(&mut self, key: K) -> Entry<K, V> {
948 let hash = self.make_hash(&key);
949 search_entry_hashed(&mut self.table, hash, key)
952 /// Returns the number of elements in the map.
957 /// use std::collections::HashMap;
959 /// let mut a = HashMap::new();
960 /// assert_eq!(a.len(), 0);
961 /// a.insert(1, "a");
962 /// assert_eq!(a.len(), 1);
964 #[stable(feature = "rust1", since = "1.0.0")]
965 pub fn len(&self) -> usize { self.table.size() }
967 /// Returns true if the map contains no elements.
972 /// use std::collections::HashMap;
974 /// let mut a = HashMap::new();
975 /// assert!(a.is_empty());
976 /// a.insert(1, "a");
977 /// assert!(!a.is_empty());
980 #[stable(feature = "rust1", since = "1.0.0")]
981 pub fn is_empty(&self) -> bool { self.len() == 0 }
983 /// Clears the map, returning all key-value pairs as an iterator. Keeps the
984 /// allocated memory for reuse.
989 /// use std::collections::HashMap;
991 /// let mut a = HashMap::new();
992 /// a.insert(1, "a");
993 /// a.insert(2, "b");
995 /// for (k, v) in a.drain().take(1) {
996 /// assert!(k == 1 || k == 2);
997 /// assert!(v == "a" || v == "b");
1000 /// assert!(a.is_empty());
1003 #[stable(feature = "drain", since = "1.6.0")]
1004 pub fn drain(&mut self) -> Drain<K, V> {
1005 fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1006 let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two; // coerce to fn pointer
1009 inner: self.table.drain().map(last_two),
1013 /// Clears the map, removing all key-value pairs. Keeps the allocated memory
1019 /// use std::collections::HashMap;
1021 /// let mut a = HashMap::new();
1022 /// a.insert(1, "a");
1024 /// assert!(a.is_empty());
1026 #[stable(feature = "rust1", since = "1.0.0")]
1028 pub fn clear(&mut self) {
1032 /// Returns a reference to the value corresponding to the key.
1034 /// The key may be any borrowed form of the map's key type, but
1035 /// `Hash` and `Eq` on the borrowed form *must* match those for
1041 /// use std::collections::HashMap;
1043 /// let mut map = HashMap::new();
1044 /// map.insert(1, "a");
1045 /// assert_eq!(map.get(&1), Some(&"a"));
1046 /// assert_eq!(map.get(&2), None);
1048 #[stable(feature = "rust1", since = "1.0.0")]
1049 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
1050 where K: Borrow<Q>, Q: Hash + Eq
1052 self.search(k).map(|bucket| bucket.into_refs().1)
1055 /// Returns true if the map contains a value for the specified key.
1057 /// The key may be any borrowed form of the map's key type, but
1058 /// `Hash` and `Eq` on the borrowed form *must* match those for
1064 /// use std::collections::HashMap;
1066 /// let mut map = HashMap::new();
1067 /// map.insert(1, "a");
1068 /// assert_eq!(map.contains_key(&1), true);
1069 /// assert_eq!(map.contains_key(&2), false);
1071 #[stable(feature = "rust1", since = "1.0.0")]
1072 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
1073 where K: Borrow<Q>, Q: Hash + Eq
1075 self.search(k).is_some()
1078 /// Returns a mutable reference to the value corresponding to the key.
1080 /// The key may be any borrowed form of the map's key type, but
1081 /// `Hash` and `Eq` on the borrowed form *must* match those for
1087 /// use std::collections::HashMap;
1089 /// let mut map = HashMap::new();
1090 /// map.insert(1, "a");
1091 /// if let Some(x) = map.get_mut(&1) {
1094 /// assert_eq!(map[&1], "b");
1096 #[stable(feature = "rust1", since = "1.0.0")]
1097 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
1098 where K: Borrow<Q>, Q: Hash + Eq
1100 self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
1103 /// Inserts a key-value pair into the map.
1105 /// If the map did not have this key present, `None` is returned.
1107 /// If the map did have this key present, the key is not updated, the
1108 /// value is updated and the old value is returned.
1109 /// See the [module-level documentation] for more.
1111 /// [module-level documentation]: index.html#insert-and-complex-keys
1116 /// use std::collections::HashMap;
1118 /// let mut map = HashMap::new();
1119 /// assert_eq!(map.insert(37, "a"), None);
1120 /// assert_eq!(map.is_empty(), false);
1122 /// map.insert(37, "b");
1123 /// assert_eq!(map.insert(37, "c"), Some("b"));
1124 /// assert_eq!(map[&37], "c");
1126 #[stable(feature = "rust1", since = "1.0.0")]
1127 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
1128 let hash = self.make_hash(&k);
1131 let mut retval = None;
1132 self.insert_or_replace_with(hash, k, v, |_, val_ref, _, val| {
1133 retval = Some(replace(val_ref, val));
1138 /// Removes a key from the map, returning the value at the key if the key
1139 /// was previously in the map.
1141 /// The key may be any borrowed form of the map's key type, but
1142 /// `Hash` and `Eq` on the borrowed form *must* match those for
1148 /// use std::collections::HashMap;
1150 /// let mut map = HashMap::new();
1151 /// map.insert(1, "a");
1152 /// assert_eq!(map.remove(&1), Some("a"));
1153 /// assert_eq!(map.remove(&1), None);
1155 #[stable(feature = "rust1", since = "1.0.0")]
1156 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
1157 where K: Borrow<Q>, Q: Hash + Eq
1159 if self.table.size() == 0 {
1163 self.search_mut(k).map(|bucket| pop_internal(bucket).1)
1167 fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: K)
1170 // Worst case, we'll find one empty bucket among `size + 1` buckets.
1171 let size = table.size();
1172 let mut probe = Bucket::new(table, hash);
1173 let ib = probe.index();
1176 let bucket = match probe.peek() {
1179 return Vacant(VacantEntry {
1182 elem: NoElem(bucket),
1185 Full(bucket) => bucket
1189 if bucket.hash() == hash {
1191 if k == *bucket.read().0 {
1192 return Occupied(OccupiedEntry{
1198 let robin_ib = bucket.index() as isize - bucket.distance() as isize;
1200 if (ib as isize) < robin_ib {
1201 // Found a luckier bucket than me. Better steal his spot.
1202 return Vacant(VacantEntry {
1205 elem: NeqElem(bucket, robin_ib as usize),
1209 probe = bucket.next();
1210 assert!(probe.index() != ib + size + 1);
1214 #[stable(feature = "rust1", since = "1.0.0")]
1215 impl<K, V, S> PartialEq for HashMap<K, V, S>
1216 where K: Eq + Hash, V: PartialEq, S: HashState
1218 fn eq(&self, other: &HashMap<K, V, S>) -> bool {
1219 if self.len() != other.len() { return false; }
1221 self.iter().all(|(key, value)|
1222 other.get(key).map_or(false, |v| *value == *v)
1227 #[stable(feature = "rust1", since = "1.0.0")]
1228 impl<K, V, S> Eq for HashMap<K, V, S>
1229 where K: Eq + Hash, V: Eq, S: HashState
1232 #[stable(feature = "rust1", since = "1.0.0")]
1233 impl<K, V, S> Debug for HashMap<K, V, S>
1234 where K: Eq + Hash + Debug, V: Debug, S: HashState
1236 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1237 f.debug_map().entries(self.iter()).finish()
1241 #[stable(feature = "rust1", since = "1.0.0")]
1242 impl<K, V, S> Default for HashMap<K, V, S>
1244 S: HashState + Default,
1246 fn default() -> HashMap<K, V, S> {
1247 HashMap::with_hash_state(Default::default())
1251 #[stable(feature = "rust1", since = "1.0.0")]
1252 impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
1253 where K: Eq + Hash + Borrow<Q>,
1260 fn index(&self, index: &Q) -> &V {
1261 self.get(index).expect("no entry found for key")
1265 /// HashMap iterator.
1266 #[stable(feature = "rust1", since = "1.0.0")]
1267 pub struct Iter<'a, K: 'a, V: 'a> {
1268 inner: table::Iter<'a, K, V>
1271 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1272 #[stable(feature = "rust1", since = "1.0.0")]
1273 impl<'a, K, V> Clone for Iter<'a, K, V> {
1274 fn clone(&self) -> Iter<'a, K, V> {
1276 inner: self.inner.clone()
1281 /// HashMap mutable values iterator.
1282 #[stable(feature = "rust1", since = "1.0.0")]
1283 pub struct IterMut<'a, K: 'a, V: 'a> {
1284 inner: table::IterMut<'a, K, V>
1287 /// HashMap move iterator.
1288 #[stable(feature = "rust1", since = "1.0.0")]
1289 pub struct IntoIter<K, V> {
1290 inner: iter::Map<table::IntoIter<K, V>, fn((SafeHash, K, V)) -> (K, V)>
1293 /// HashMap keys iterator.
1294 #[stable(feature = "rust1", since = "1.0.0")]
1295 pub struct Keys<'a, K: 'a, V: 'a> {
1296 inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
1299 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1300 #[stable(feature = "rust1", since = "1.0.0")]
1301 impl<'a, K, V> Clone for Keys<'a, K, V> {
1302 fn clone(&self) -> Keys<'a, K, V> {
1304 inner: self.inner.clone()
1309 /// HashMap values iterator.
1310 #[stable(feature = "rust1", since = "1.0.0")]
1311 pub struct Values<'a, K: 'a, V: 'a> {
1312 inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
1315 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1316 #[stable(feature = "rust1", since = "1.0.0")]
1317 impl<'a, K, V> Clone for Values<'a, K, V> {
1318 fn clone(&self) -> Values<'a, K, V> {
1320 inner: self.inner.clone()
1325 /// HashMap drain iterator.
1326 #[stable(feature = "drain", since = "1.6.0")]
1327 pub struct Drain<'a, K: 'a, V: 'a> {
1328 inner: iter::Map<table::Drain<'a, K, V>, fn((SafeHash, K, V)) -> (K, V)>
1331 /// A view into a single occupied location in a HashMap.
1332 #[stable(feature = "rust1", since = "1.0.0")]
1333 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
1334 elem: FullBucket<K, V, &'a mut RawTable<K, V>>,
1337 /// A view into a single empty location in a HashMap.
1338 #[stable(feature = "rust1", since = "1.0.0")]
1339 pub struct VacantEntry<'a, K: 'a, V: 'a> {
1342 elem: VacantEntryState<K, V, &'a mut RawTable<K, V>>,
1345 /// A view into a single location in a map, which may be vacant or occupied.
1346 #[stable(feature = "rust1", since = "1.0.0")]
1347 pub enum Entry<'a, K: 'a, V: 'a> {
1348 /// An occupied Entry.
1349 #[stable(feature = "rust1", since = "1.0.0")]
1350 Occupied(OccupiedEntry<'a, K, V>),
1353 #[stable(feature = "rust1", since = "1.0.0")]
1354 Vacant(VacantEntry<'a, K, V>),
1357 /// Possible states of a VacantEntry.
1358 enum VacantEntryState<K, V, M> {
1359 /// The index is occupied, but the key to insert has precedence,
1360 /// and will kick the current one out on insertion.
1361 NeqElem(FullBucket<K, V, M>, usize),
1362 /// The index is genuinely vacant.
1363 NoElem(EmptyBucket<K, V, M>),
1366 #[stable(feature = "rust1", since = "1.0.0")]
1367 impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
1368 where K: Eq + Hash, S: HashState
1370 type Item = (&'a K, &'a V);
1371 type IntoIter = Iter<'a, K, V>;
1373 fn into_iter(self) -> Iter<'a, K, V> {
1378 #[stable(feature = "rust1", since = "1.0.0")]
1379 impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
1380 where K: Eq + Hash, S: HashState
1382 type Item = (&'a K, &'a mut V);
1383 type IntoIter = IterMut<'a, K, V>;
1385 fn into_iter(mut self) -> IterMut<'a, K, V> {
1390 #[stable(feature = "rust1", since = "1.0.0")]
1391 impl<K, V, S> IntoIterator for HashMap<K, V, S>
1392 where K: Eq + Hash, S: HashState
1395 type IntoIter = IntoIter<K, V>;
1397 /// Creates a consuming iterator, that is, one that moves each key-value
1398 /// pair out of the map in arbitrary order. The map cannot be used after
1404 /// use std::collections::HashMap;
1406 /// let mut map = HashMap::new();
1407 /// map.insert("a", 1);
1408 /// map.insert("b", 2);
1409 /// map.insert("c", 3);
1411 /// // Not possible with .iter()
1412 /// let vec: Vec<(&str, isize)> = map.into_iter().collect();
1414 fn into_iter(self) -> IntoIter<K, V> {
1415 fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1416 let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
1419 inner: self.table.into_iter().map(last_two)
1424 #[stable(feature = "rust1", since = "1.0.0")]
1425 impl<'a, K, V> Iterator for Iter<'a, K, V> {
1426 type Item = (&'a K, &'a V);
1428 #[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
1429 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1431 #[stable(feature = "rust1", since = "1.0.0")]
1432 impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1433 #[inline] fn len(&self) -> usize { self.inner.len() }
1436 #[stable(feature = "rust1", since = "1.0.0")]
1437 impl<'a, K, V> Iterator for IterMut<'a, K, V> {
1438 type Item = (&'a K, &'a mut V);
1440 #[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
1441 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1443 #[stable(feature = "rust1", since = "1.0.0")]
1444 impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1445 #[inline] fn len(&self) -> usize { self.inner.len() }
1448 #[stable(feature = "rust1", since = "1.0.0")]
1449 impl<K, V> Iterator for IntoIter<K, V> {
1452 #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1453 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1455 #[stable(feature = "rust1", since = "1.0.0")]
1456 impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1457 #[inline] fn len(&self) -> usize { self.inner.len() }
1460 #[stable(feature = "rust1", since = "1.0.0")]
1461 impl<'a, K, V> Iterator for Keys<'a, K, V> {
1464 #[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1465 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1467 #[stable(feature = "rust1", since = "1.0.0")]
1468 impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1469 #[inline] fn len(&self) -> usize { self.inner.len() }
1472 #[stable(feature = "rust1", since = "1.0.0")]
1473 impl<'a, K, V> Iterator for Values<'a, K, V> {
1476 #[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1477 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1479 #[stable(feature = "rust1", since = "1.0.0")]
1480 impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1481 #[inline] fn len(&self) -> usize { self.inner.len() }
1484 #[stable(feature = "rust1", since = "1.0.0")]
1485 impl<'a, K, V> Iterator for Drain<'a, K, V> {
1488 #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1489 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1491 #[stable(feature = "rust1", since = "1.0.0")]
1492 impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
1493 #[inline] fn len(&self) -> usize { self.inner.len() }
1496 impl<'a, K, V> Entry<'a, K, V> {
1497 #[stable(feature = "rust1", since = "1.0.0")]
1498 /// Ensures a value is in the entry by inserting the default if empty, and returns
1499 /// a mutable reference to the value in the entry.
1500 pub fn or_insert(self, default: V) -> &'a mut V {
1502 Occupied(entry) => entry.into_mut(),
1503 Vacant(entry) => entry.insert(default),
1507 #[stable(feature = "rust1", since = "1.0.0")]
1508 /// Ensures a value is in the entry by inserting the result of the default function if empty,
1509 /// and returns a mutable reference to the value in the entry.
1510 pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1512 Occupied(entry) => entry.into_mut(),
1513 Vacant(entry) => entry.insert(default()),
1518 impl<'a, K, V> OccupiedEntry<'a, K, V> {
1519 /// Gets a reference to the value in the entry.
1520 #[stable(feature = "rust1", since = "1.0.0")]
1521 pub fn get(&self) -> &V {
1525 /// Gets a mutable reference to the value in the entry.
1526 #[stable(feature = "rust1", since = "1.0.0")]
1527 pub fn get_mut(&mut self) -> &mut V {
1528 self.elem.read_mut().1
1531 /// Converts the OccupiedEntry into a mutable reference to the value in the entry
1532 /// with a lifetime bound to the map itself
1533 #[stable(feature = "rust1", since = "1.0.0")]
1534 pub fn into_mut(self) -> &'a mut V {
1535 self.elem.into_mut_refs().1
1538 /// Sets the value of the entry, and returns the entry's old value
1539 #[stable(feature = "rust1", since = "1.0.0")]
1540 pub fn insert(&mut self, mut value: V) -> V {
1541 let old_value = self.get_mut();
1542 mem::swap(&mut value, old_value);
1546 /// Takes the value out of the entry, and returns it
1547 #[stable(feature = "rust1", since = "1.0.0")]
1548 pub fn remove(self) -> V {
1549 pop_internal(self.elem).1
1553 impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
1554 /// Sets the value of the entry with the VacantEntry's key,
1555 /// and returns a mutable reference to it
1556 #[stable(feature = "rust1", since = "1.0.0")]
1557 pub fn insert(self, value: V) -> &'a mut V {
1559 NeqElem(bucket, ib) => {
1560 robin_hood(bucket, ib, self.hash, self.key, value)
1563 bucket.put(self.hash, self.key, value).into_mut_refs().1
1569 #[stable(feature = "rust1", since = "1.0.0")]
1570 impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
1571 where K: Eq + Hash, S: HashState + Default
1573 fn from_iter<T: IntoIterator<Item=(K, V)>>(iterable: T) -> HashMap<K, V, S> {
1574 let iter = iterable.into_iter();
1575 let lower = iter.size_hint().0;
1576 let mut map = HashMap::with_capacity_and_hash_state(lower,
1577 Default::default());
1583 #[stable(feature = "rust1", since = "1.0.0")]
1584 impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
1585 where K: Eq + Hash, S: HashState
1587 fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) {
1588 for (k, v) in iter {
1594 #[stable(feature = "hash_extend_copy", since = "1.4.0")]
1595 impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
1596 where K: Eq + Hash + Copy, V: Copy, S: HashState
1598 fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T) {
1599 self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
1603 /// `RandomState` is the default state for `HashMap` types.
1605 /// A particular instance `RandomState` will create the same instances of
1606 /// `Hasher`, but the hashers created by two different `RandomState`
1607 /// instances are unlikely to produce the same result for the same values.
1609 #[unstable(feature = "hashmap_hasher",
1610 reason = "hashing an hash maps may be altered",
1612 pub struct RandomState {
1617 #[unstable(feature = "hashmap_hasher",
1618 reason = "hashing an hash maps may be altered",
1621 /// Constructs a new `RandomState` that is initialized with random keys.
1623 #[allow(deprecated)] // rand
1624 pub fn new() -> RandomState {
1625 let mut r = rand::thread_rng();
1626 RandomState { k0: r.gen(), k1: r.gen() }
1630 #[unstable(feature = "hashmap_hasher",
1631 reason = "hashing an hash maps may be altered",
1633 impl HashState for RandomState {
1634 type Hasher = SipHasher;
1636 fn hasher(&self) -> SipHasher {
1637 SipHasher::new_with_keys(self.k0, self.k1)
1641 #[stable(feature = "rust1", since = "1.0.0")]
1642 impl Default for RandomState {
1644 fn default() -> RandomState {
1649 impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
1650 where K: Eq + Hash + Borrow<Q>, S: HashState, Q: Eq + Hash
1654 fn get(&self, key: &Q) -> Option<&K> {
1655 self.search(key).map(|bucket| bucket.into_refs().0)
1658 fn take(&mut self, key: &Q) -> Option<K> {
1659 if self.table.size() == 0 {
1663 self.search_mut(key).map(|bucket| pop_internal(bucket).0)
1666 fn replace(&mut self, key: K) -> Option<K> {
1667 let hash = self.make_hash(&key);
1670 let mut retkey = None;
1671 self.insert_or_replace_with(hash, key, (), |key_ref, _, key, _| {
1672 retkey = Some(replace(key_ref, key));
1683 use super::Entry::{Occupied, Vacant};
1684 use iter::range_inclusive;
1686 use rand::{thread_rng, Rng};
1689 fn test_create_capacity_zero() {
1690 let mut m = HashMap::with_capacity(0);
1692 assert!(m.insert(1, 1).is_none());
1694 assert!(m.contains_key(&1));
1695 assert!(!m.contains_key(&0));
1700 let mut m = HashMap::new();
1701 assert_eq!(m.len(), 0);
1702 assert!(m.insert(1, 2).is_none());
1703 assert_eq!(m.len(), 1);
1704 assert!(m.insert(2, 4).is_none());
1705 assert_eq!(m.len(), 2);
1706 assert_eq!(*m.get(&1).unwrap(), 2);
1707 assert_eq!(*m.get(&2).unwrap(), 4);
1710 thread_local! { static DROP_VECTOR: RefCell<Vec<isize>> = RefCell::new(Vec::new()) }
1712 #[derive(Hash, PartialEq, Eq)]
1718 fn new(k: usize) -> Dropable {
1719 DROP_VECTOR.with(|slot| {
1720 slot.borrow_mut()[k] += 1;
1727 impl Drop for Dropable {
1728 fn drop(&mut self) {
1729 DROP_VECTOR.with(|slot| {
1730 slot.borrow_mut()[self.k] -= 1;
1735 impl Clone for Dropable {
1736 fn clone(&self) -> Dropable {
1737 Dropable::new(self.k)
1743 DROP_VECTOR.with(|slot| {
1744 *slot.borrow_mut() = vec![0; 200];
1748 let mut m = HashMap::new();
1750 DROP_VECTOR.with(|v| {
1752 assert_eq!(v.borrow()[i], 0);
1757 let d1 = Dropable::new(i);
1758 let d2 = Dropable::new(i+100);
1762 DROP_VECTOR.with(|v| {
1764 assert_eq!(v.borrow()[i], 1);
1769 let k = Dropable::new(i);
1770 let v = m.remove(&k);
1772 assert!(v.is_some());
1774 DROP_VECTOR.with(|v| {
1775 assert_eq!(v.borrow()[i], 1);
1776 assert_eq!(v.borrow()[i+100], 1);
1780 DROP_VECTOR.with(|v| {
1782 assert_eq!(v.borrow()[i], 0);
1783 assert_eq!(v.borrow()[i+100], 0);
1787 assert_eq!(v.borrow()[i], 1);
1788 assert_eq!(v.borrow()[i+100], 1);
1793 DROP_VECTOR.with(|v| {
1795 assert_eq!(v.borrow()[i], 0);
1801 fn test_move_iter_drops() {
1802 DROP_VECTOR.with(|v| {
1803 *v.borrow_mut() = vec![0; 200];
1807 let mut hm = HashMap::new();
1809 DROP_VECTOR.with(|v| {
1811 assert_eq!(v.borrow()[i], 0);
1816 let d1 = Dropable::new(i);
1817 let d2 = Dropable::new(i+100);
1821 DROP_VECTOR.with(|v| {
1823 assert_eq!(v.borrow()[i], 1);
1830 // By the way, ensure that cloning doesn't screw up the dropping.
1834 let mut half = hm.into_iter().take(50);
1836 DROP_VECTOR.with(|v| {
1838 assert_eq!(v.borrow()[i], 1);
1842 for _ in half.by_ref() {}
1844 DROP_VECTOR.with(|v| {
1845 let nk = (0..100).filter(|&i| {
1849 let nv = (0..100).filter(|&i| {
1850 v.borrow()[i+100] == 1
1858 DROP_VECTOR.with(|v| {
1860 assert_eq!(v.borrow()[i], 0);
1866 fn test_empty_pop() {
1867 let mut m: HashMap<isize, bool> = HashMap::new();
1868 assert_eq!(m.remove(&0), None);
1872 fn test_lots_of_insertions() {
1873 let mut m = HashMap::new();
1875 // Try this a few times to make sure we never screw up the hashmap's
1878 assert!(m.is_empty());
1880 for i in range_inclusive(1, 1000) {
1881 assert!(m.insert(i, i).is_none());
1883 for j in range_inclusive(1, i) {
1885 assert_eq!(r, Some(&j));
1888 for j in range_inclusive(i+1, 1000) {
1890 assert_eq!(r, None);
1894 for i in range_inclusive(1001, 2000) {
1895 assert!(!m.contains_key(&i));
1899 for i in range_inclusive(1, 1000) {
1900 assert!(m.remove(&i).is_some());
1902 for j in range_inclusive(1, i) {
1903 assert!(!m.contains_key(&j));
1906 for j in range_inclusive(i+1, 1000) {
1907 assert!(m.contains_key(&j));
1911 for i in range_inclusive(1, 1000) {
1912 assert!(!m.contains_key(&i));
1915 for i in range_inclusive(1, 1000) {
1916 assert!(m.insert(i, i).is_none());
1920 for i in (1..1001).rev() {
1921 assert!(m.remove(&i).is_some());
1923 for j in range_inclusive(i, 1000) {
1924 assert!(!m.contains_key(&j));
1927 for j in range_inclusive(1, i-1) {
1928 assert!(m.contains_key(&j));
1935 fn test_find_mut() {
1936 let mut m = HashMap::new();
1937 assert!(m.insert(1, 12).is_none());
1938 assert!(m.insert(2, 8).is_none());
1939 assert!(m.insert(5, 14).is_none());
1941 match m.get_mut(&5) {
1942 None => panic!(), Some(x) => *x = new
1944 assert_eq!(m.get(&5), Some(&new));
1948 fn test_insert_overwrite() {
1949 let mut m = HashMap::new();
1950 assert!(m.insert(1, 2).is_none());
1951 assert_eq!(*m.get(&1).unwrap(), 2);
1952 assert!(!m.insert(1, 3).is_none());
1953 assert_eq!(*m.get(&1).unwrap(), 3);
1957 fn test_insert_conflicts() {
1958 let mut m = HashMap::with_capacity(4);
1959 assert!(m.insert(1, 2).is_none());
1960 assert!(m.insert(5, 3).is_none());
1961 assert!(m.insert(9, 4).is_none());
1962 assert_eq!(*m.get(&9).unwrap(), 4);
1963 assert_eq!(*m.get(&5).unwrap(), 3);
1964 assert_eq!(*m.get(&1).unwrap(), 2);
1968 fn test_conflict_remove() {
1969 let mut m = HashMap::with_capacity(4);
1970 assert!(m.insert(1, 2).is_none());
1971 assert_eq!(*m.get(&1).unwrap(), 2);
1972 assert!(m.insert(5, 3).is_none());
1973 assert_eq!(*m.get(&1).unwrap(), 2);
1974 assert_eq!(*m.get(&5).unwrap(), 3);
1975 assert!(m.insert(9, 4).is_none());
1976 assert_eq!(*m.get(&1).unwrap(), 2);
1977 assert_eq!(*m.get(&5).unwrap(), 3);
1978 assert_eq!(*m.get(&9).unwrap(), 4);
1979 assert!(m.remove(&1).is_some());
1980 assert_eq!(*m.get(&9).unwrap(), 4);
1981 assert_eq!(*m.get(&5).unwrap(), 3);
1985 fn test_is_empty() {
1986 let mut m = HashMap::with_capacity(4);
1987 assert!(m.insert(1, 2).is_none());
1988 assert!(!m.is_empty());
1989 assert!(m.remove(&1).is_some());
1990 assert!(m.is_empty());
1995 let mut m = HashMap::new();
1997 assert_eq!(m.remove(&1), Some(2));
1998 assert_eq!(m.remove(&1), None);
2003 let mut m = HashMap::with_capacity(4);
2005 assert!(m.insert(i, i*2).is_none());
2007 assert_eq!(m.len(), 32);
2009 let mut observed: u32 = 0;
2012 assert_eq!(*v, *k * 2);
2013 observed |= 1 << *k;
2015 assert_eq!(observed, 0xFFFF_FFFF);
2020 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2021 let map: HashMap<_, _> = vec.into_iter().collect();
2022 let keys: Vec<_> = map.keys().cloned().collect();
2023 assert_eq!(keys.len(), 3);
2024 assert!(keys.contains(&1));
2025 assert!(keys.contains(&2));
2026 assert!(keys.contains(&3));
2031 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2032 let map: HashMap<_, _> = vec.into_iter().collect();
2033 let values: Vec<_> = map.values().cloned().collect();
2034 assert_eq!(values.len(), 3);
2035 assert!(values.contains(&'a'));
2036 assert!(values.contains(&'b'));
2037 assert!(values.contains(&'c'));
2042 let mut m = HashMap::new();
2043 assert!(m.get(&1).is_none());
2047 Some(v) => assert_eq!(*v, 2)
2053 let mut m1 = HashMap::new();
2058 let mut m2 = HashMap::new();
2071 let mut map = HashMap::new();
2072 let empty: HashMap<i32, i32> = HashMap::new();
2077 let map_str = format!("{:?}", map);
2079 assert!(map_str == "{1: 2, 3: 4}" ||
2080 map_str == "{3: 4, 1: 2}");
2081 assert_eq!(format!("{:?}", empty), "{}");
2086 let mut m = HashMap::new();
2088 assert_eq!(m.len(), 0);
2089 assert!(m.is_empty());
2092 let old_cap = m.table.capacity();
2093 while old_cap == m.table.capacity() {
2098 assert_eq!(m.len(), i);
2099 assert!(!m.is_empty());
2103 fn test_behavior_resize_policy() {
2104 let mut m = HashMap::new();
2106 assert_eq!(m.len(), 0);
2107 assert_eq!(m.table.capacity(), 0);
2108 assert!(m.is_empty());
2112 assert!(m.is_empty());
2113 let initial_cap = m.table.capacity();
2114 m.reserve(initial_cap);
2115 let cap = m.table.capacity();
2117 assert_eq!(cap, initial_cap * 2);
2120 for _ in 0..cap * 3 / 4 {
2124 // three quarters full
2126 assert_eq!(m.len(), i);
2127 assert_eq!(m.table.capacity(), cap);
2129 for _ in 0..cap / 4 {
2135 let new_cap = m.table.capacity();
2136 assert_eq!(new_cap, cap * 2);
2138 for _ in 0..cap / 2 - 1 {
2141 assert_eq!(m.table.capacity(), new_cap);
2143 // A little more than one quarter full.
2145 assert_eq!(m.table.capacity(), cap);
2146 // again, a little more than half full
2147 for _ in 0..cap / 2 - 1 {
2153 assert_eq!(m.len(), i);
2154 assert!(!m.is_empty());
2155 assert_eq!(m.table.capacity(), initial_cap);
2159 fn test_reserve_shrink_to_fit() {
2160 let mut m = HashMap::new();
2163 assert!(m.capacity() >= m.len());
2169 let usable_cap = m.capacity();
2170 for i in 128..(128 + 256) {
2172 assert_eq!(m.capacity(), usable_cap);
2175 for i in 100..(128 + 256) {
2176 assert_eq!(m.remove(&i), Some(i));
2180 assert_eq!(m.len(), 100);
2181 assert!(!m.is_empty());
2182 assert!(m.capacity() >= m.len());
2185 assert_eq!(m.remove(&i), Some(i));
2190 assert_eq!(m.len(), 1);
2191 assert!(m.capacity() >= m.len());
2192 assert_eq!(m.remove(&0), Some(0));
2196 fn test_from_iter() {
2197 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2199 let map: HashMap<_, _> = xs.iter().cloned().collect();
2201 for &(k, v) in &xs {
2202 assert_eq!(map.get(&k), Some(&v));
2207 fn test_size_hint() {
2208 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2210 let map: HashMap<_, _> = xs.iter().cloned().collect();
2212 let mut iter = map.iter();
2214 for _ in iter.by_ref().take(3) {}
2216 assert_eq!(iter.size_hint(), (3, Some(3)));
2220 fn test_iter_len() {
2221 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2223 let map: HashMap<_, _> = xs.iter().cloned().collect();
2225 let mut iter = map.iter();
2227 for _ in iter.by_ref().take(3) {}
2229 assert_eq!(iter.len(), 3);
2233 fn test_mut_size_hint() {
2234 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2236 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2238 let mut iter = map.iter_mut();
2240 for _ in iter.by_ref().take(3) {}
2242 assert_eq!(iter.size_hint(), (3, Some(3)));
2246 fn test_iter_mut_len() {
2247 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2249 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2251 let mut iter = map.iter_mut();
2253 for _ in iter.by_ref().take(3) {}
2255 assert_eq!(iter.len(), 3);
2260 let mut map = HashMap::new();
2266 assert_eq!(map[&2], 1);
2271 fn test_index_nonexistent() {
2272 let mut map = HashMap::new();
2283 let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
2285 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2287 // Existing key (insert)
2288 match map.entry(1) {
2289 Vacant(_) => unreachable!(),
2290 Occupied(mut view) => {
2291 assert_eq!(view.get(), &10);
2292 assert_eq!(view.insert(100), 10);
2295 assert_eq!(map.get(&1).unwrap(), &100);
2296 assert_eq!(map.len(), 6);
2299 // Existing key (update)
2300 match map.entry(2) {
2301 Vacant(_) => unreachable!(),
2302 Occupied(mut view) => {
2303 let v = view.get_mut();
2304 let new_v = (*v) * 10;
2308 assert_eq!(map.get(&2).unwrap(), &200);
2309 assert_eq!(map.len(), 6);
2311 // Existing key (take)
2312 match map.entry(3) {
2313 Vacant(_) => unreachable!(),
2315 assert_eq!(view.remove(), 30);
2318 assert_eq!(map.get(&3), None);
2319 assert_eq!(map.len(), 5);
2322 // Inexistent key (insert)
2323 match map.entry(10) {
2324 Occupied(_) => unreachable!(),
2326 assert_eq!(*view.insert(1000), 1000);
2329 assert_eq!(map.get(&10).unwrap(), &1000);
2330 assert_eq!(map.len(), 6);
2334 fn test_entry_take_doesnt_corrupt() {
2335 #![allow(deprecated)] //rand
2337 fn check(m: &HashMap<isize, ()>) {
2339 assert!(m.contains_key(k),
2340 "{} is in keys() but not in the map?", k);
2344 let mut m = HashMap::new();
2345 let mut rng = thread_rng();
2347 // Populate the map with some items.
2349 let x = rng.gen_range(-10, 10);
2354 let x = rng.gen_range(-10, 10);
2358 println!("{}: remove {}", i, x);
2368 fn test_extend_ref() {
2369 let mut a = HashMap::new();
2371 let mut b = HashMap::new();
2373 b.insert(3, "three");
2377 assert_eq!(a.len(), 3);
2378 assert_eq!(a[&1], "one");
2379 assert_eq!(a[&2], "two");
2380 assert_eq!(a[&3], "three");