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.
11 // ignore-lexer-test FIXME #15883
14 use self::SearchResult::*;
15 use self::VacantEntryState::*;
19 use cmp::{max, Eq, PartialEq};
21 use fmt::{self, Debug};
22 use hash::{Hash, SipHasher};
23 use iter::{self, Iterator, ExactSizeIterator, IntoIterator, IteratorExt, FromIterator, Extend, Map};
25 use mem::{self, replace};
27 use num::{Int, UnsignedInt};
28 use ops::{Deref, FnMut, Index, IndexMut};
29 use option::Option::{self, Some, None};
30 use rand::{self, Rng};
31 use result::Result::{self, Ok, Err};
43 use super::table::BucketState::{
47 use super::state::HashState;
49 const INITIAL_LOG2_CAP: usize = 5;
50 #[unstable(feature = "std_misc")]
51 pub const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5
53 /// The default behavior of HashMap implements a load factor of 90.9%.
54 /// This behavior is characterized by the following condition:
56 /// - if size > 0.909 * capacity: grow the map
58 struct DefaultResizePolicy;
60 impl DefaultResizePolicy {
61 fn new() -> DefaultResizePolicy {
66 fn min_capacity(&self, usable_size: usize) -> usize {
67 // Here, we are rephrasing the logic by specifying the lower limit
70 // - if `cap < size * 1.1`: grow the map
74 /// An inverse of `min_capacity`, approximately.
76 fn usable_capacity(&self, cap: usize) -> usize {
77 // As the number of entries approaches usable capacity,
78 // min_capacity(size) must be smaller than the internal capacity,
79 // so that the map is not resized:
80 // `min_capacity(usable_capacity(x)) <= x`.
81 // The left-hand side can only be smaller due to flooring by integer
84 // This doesn't have to be checked for overflow since allocation size
85 // in bytes will overflow earlier than multiplication by 10.
91 fn test_resize_policy() {
92 let rp = DefaultResizePolicy;
94 assert!(rp.min_capacity(rp.usable_capacity(n)) <= n);
95 assert!(rp.usable_capacity(rp.min_capacity(n)) <= n);
99 // The main performance trick in this hashmap is called Robin Hood Hashing.
100 // It gains its excellent performance from one essential operation:
102 // If an insertion collides with an existing element, and that element's
103 // "probe distance" (how far away the element is from its ideal location)
104 // is higher than how far we've already probed, swap the elements.
106 // This massively lowers variance in probe distance, and allows us to get very
107 // high load factors with good performance. The 90% load factor I use is rather
110 // > Why a load factor of approximately 90%?
112 // In general, all the distances to initial buckets will converge on the mean.
113 // At a load factor of α, the odds of finding the target bucket after k
114 // probes is approximately 1-α^k. If we set this equal to 50% (since we converge
115 // on the mean) and set k=8 (64-byte cache line / 8-byte hash), α=0.92. I round
116 // this down to make the math easier on the CPU and avoid its FPU.
117 // Since on average we start the probing in the middle of a cache line, this
118 // strategy pulls in two cache lines of hashes on every lookup. I think that's
119 // pretty good, but if you want to trade off some space, it could go down to one
120 // cache line on average with an α of 0.84.
122 // > Wait, what? Where did you get 1-α^k from?
124 // On the first probe, your odds of a collision with an existing element is α.
125 // The odds of doing this twice in a row is approximately α^2. For three times,
126 // α^3, etc. Therefore, the odds of colliding k times is α^k. The odds of NOT
127 // colliding after k tries is 1-α^k.
129 // The paper from 1986 cited below mentions an implementation which keeps track
130 // of the distance-to-initial-bucket histogram. This approach is not suitable
131 // for modern architectures because it requires maintaining an internal data
132 // structure. This allows very good first guesses, but we are most concerned
133 // with guessing entire cache lines, not individual indexes. Furthermore, array
134 // accesses are no longer linear and in one direction, as we have now. There
135 // is also memory and cache pressure that this would entail that would be very
136 // difficult to properly see in a microbenchmark.
138 // ## Future Improvements (FIXME!)
140 // Allow the load factor to be changed dynamically and/or at initialization.
142 // Also, would it be possible for us to reuse storage when growing the
143 // underlying table? This is exactly the use case for 'realloc', and may
144 // be worth exploring.
146 // ## Future Optimizations (FIXME!)
148 // Another possible design choice that I made without any real reason is
149 // parameterizing the raw table over keys and values. Technically, all we need
150 // is the size and alignment of keys and values, and the code should be just as
151 // efficient (well, we might need one for power-of-two size and one for not...).
152 // This has the potential to reduce code bloat in rust executables, without
153 // really losing anything except 4 words (key size, key alignment, val size,
154 // val alignment) which can be passed in to every call of a `RawTable` function.
155 // This would definitely be an avenue worth exploring if people start complaining
156 // about the size of rust executables.
158 // Annotate exceedingly likely branches in `table::make_hash`
159 // and `search_hashed` to reduce instruction cache pressure
160 // and mispredictions once it becomes possible (blocked on issue #11092).
162 // Shrinking the table could simply reallocate in place after moving buckets
163 // to the first half.
165 // The growth algorithm (fragment of the Proof of Correctness)
166 // --------------------
168 // The growth algorithm is basically a fast path of the naive reinsertion-
169 // during-resize algorithm. Other paths should never be taken.
171 // Consider growing a robin hood hashtable of capacity n. Normally, we do this
172 // by allocating a new table of capacity `2n`, and then individually reinsert
173 // each element in the old table into the new one. This guarantees that the
174 // new table is a valid robin hood hashtable with all the desired statistical
175 // properties. Remark that the order we reinsert the elements in should not
176 // matter. For simplicity and efficiency, we will consider only linear
177 // reinsertions, which consist of reinserting all elements in the old table
178 // into the new one by increasing order of index. However we will not be
179 // starting our reinsertions from index 0 in general. If we start from index
180 // i, for the purpose of reinsertion we will consider all elements with real
181 // index j < i to have virtual index n + j.
183 // Our hash generation scheme consists of generating a 64-bit hash and
184 // truncating the most significant bits. When moving to the new table, we
185 // simply introduce a new bit to the front of the hash. Therefore, if an
186 // elements has ideal index i in the old table, it can have one of two ideal
187 // locations in the new table. If the new bit is 0, then the new ideal index
188 // is i. If the new bit is 1, then the new ideal index is n + i. Intuitively,
189 // we are producing two independent tables of size n, and for each element we
190 // independently choose which table to insert it into with equal probability.
191 // However the rather than wrapping around themselves on overflowing their
192 // indexes, the first table overflows into the first, and the first into the
193 // second. Visually, our new table will look something like:
195 // [yy_xxx_xxxx_xxx|xx_yyy_yyyy_yyy]
197 // Where x's are elements inserted into the first table, y's are elements
198 // inserted into the second, and _'s are empty sections. We now define a few
199 // key concepts that we will use later. Note that this is a very abstract
200 // perspective of the table. A real resized table would be at least half
203 // Theorem: A linear robin hood reinsertion from the first ideal element
204 // produces identical results to a linear naive reinsertion from the same
207 // FIXME(Gankro, pczarn): review the proof and put it all in a separate README.md
209 /// A hash map implementation which uses linear probing with Robin
210 /// Hood bucket stealing.
212 /// The hashes are all keyed by the task-local random number generator
213 /// on creation by default. This means that the ordering of the keys is
214 /// randomized, but makes the tables more resistant to
215 /// denial-of-service attacks (Hash DoS). This behaviour can be
216 /// overridden with one of the constructors.
218 /// It is required that the keys implement the `Eq` and `Hash` traits, although
219 /// this can frequently be achieved by using `#[derive(Eq, Hash)]`.
221 /// It is a logic error for a key to be modified in such a way that the key's
222 /// hash, as determined by the `Hash` trait, or its equality, as determined by
223 /// the `Eq` trait, changes while it is in the map. This is normally only
224 /// possible through `Cell`, `RefCell`, global state, I/O, or unsafe code.
226 /// Relevant papers/articles:
228 /// 1. Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
229 /// 2. Emmanuel Goossaert. ["Robin Hood
230 /// hashing"](http://codecapsule.com/2013/11/11/robin-hood-hashing/)
231 /// 3. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
232 /// deletion"](http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
237 /// use std::collections::HashMap;
239 /// // type inference lets us omit an explicit type signature (which
240 /// // would be `HashMap<&str, &str>` in this example).
241 /// let mut book_reviews = HashMap::new();
243 /// // review some books.
244 /// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.");
245 /// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
246 /// book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
247 /// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
249 /// // check for a specific one.
250 /// if !book_reviews.contains_key(&("Les Misérables")) {
251 /// println!("We've got {} reviews, but Les Misérables ain't one.",
252 /// book_reviews.len());
255 /// // oops, this review has a lot of spelling mistakes, let's delete it.
256 /// book_reviews.remove(&("The Adventures of Sherlock Holmes"));
258 /// // look up the values associated with some keys.
259 /// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
260 /// for book in to_find.iter() {
261 /// match book_reviews.get(book) {
262 /// Some(review) => println!("{}: {}", *book, *review),
263 /// None => println!("{} is unreviewed.", *book)
267 /// // iterate over everything.
268 /// for (book, review) in book_reviews.iter() {
269 /// println!("{}: \"{}\"", *book, *review);
273 /// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
274 /// We must also derive `PartialEq`.
277 /// use std::collections::HashMap;
279 /// #[derive(Hash, Eq, PartialEq, Debug)]
286 /// /// Create a new Viking.
287 /// fn new(name: &str, country: &str) -> Viking {
288 /// Viking { name: name.to_string(), country: country.to_string() }
292 /// // Use a HashMap to store the vikings' health points.
293 /// let mut vikings = HashMap::new();
295 /// vikings.insert(Viking::new("Einar", "Norway"), 25);
296 /// vikings.insert(Viking::new("Olaf", "Denmark"), 24);
297 /// vikings.insert(Viking::new("Harald", "Iceland"), 12);
299 /// // Use derived implementation to print the status of the vikings.
300 /// for (viking, health) in vikings.iter() {
301 /// println!("{:?} has {} hp", viking, health);
305 #[stable(feature = "rust1", since = "1.0.0")]
306 pub struct HashMap<K, V, S = RandomState> {
307 // All hashes are keyed on these values, to prevent hash collision attacks.
310 table: RawTable<K, V>,
312 resize_policy: DefaultResizePolicy,
315 /// Search for a pre-hashed key.
316 fn search_hashed<K, V, M, F>(table: M,
319 -> SearchResult<K, V, M> where
320 M: Deref<Target=RawTable<K, V>>,
321 F: FnMut(&K) -> bool,
323 // This is the only function where capacity can be zero. To avoid
324 // undefined behaviour when Bucket::new gets the raw bucket in this
325 // case, immediately return the appropriate search result.
326 if table.capacity() == 0 {
327 return TableRef(table);
330 let size = table.size();
331 let mut probe = Bucket::new(table, hash);
332 let ib = probe.index();
334 while probe.index() != ib + size {
335 let full = match probe.peek() {
336 Empty(b) => return TableRef(b.into_table()), // hit an empty bucket
340 if full.distance() + ib < full.index() {
341 // We can finish the search early if we hit any bucket
342 // with a lower distance to initial bucket than we've probed.
343 return TableRef(full.into_table());
346 // If the hash doesn't match, it can't be this one..
347 if hash == full.hash() {
348 // If the key doesn't match, it can't be this one..
349 if is_match(full.read().0) {
350 return FoundExisting(full);
357 TableRef(probe.into_table())
360 fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
361 let (empty, retkey, retval) = starting_bucket.take();
362 let mut gap = match empty.gap_peek() {
364 None => return (retkey, retval)
367 while gap.full().distance() != 0 {
368 gap = match gap.shift() {
374 // Now we've done all our shifting. Return the value we grabbed earlier.
378 /// Perform robin hood bucket stealing at the given `bucket`. You must
379 /// also pass the position of that bucket's initial bucket so we don't have
380 /// to recalculate it.
382 /// `hash`, `k`, and `v` are the elements to "robin hood" into the hashtable.
383 fn robin_hood<'a, K: 'a, V: 'a>(mut bucket: FullBucketMut<'a, K, V>,
389 let starting_index = bucket.index();
391 let table = bucket.table(); // FIXME "lifetime too short".
394 // There can be at most `size - dib` buckets to displace, because
395 // in the worst case, there are `size` elements and we already are
396 // `distance` buckets away from the initial one.
397 let idx_end = starting_index + size - bucket.distance();
400 let (old_hash, old_key, old_val) = bucket.replace(hash, k, v);
402 let probe = bucket.next();
403 assert!(probe.index() != idx_end);
405 let full_bucket = match probe.peek() {
408 let b = bucket.put(old_hash, old_key, old_val);
409 // Now that it's stolen, just read the value's pointer
410 // right out of the table!
411 return Bucket::at_index(b.into_table(), starting_index)
417 Full(bucket) => bucket
420 let probe_ib = full_bucket.index() - full_bucket.distance();
422 bucket = full_bucket;
424 // Robin hood! Steal the spot.
436 /// A result that works like Option<FullBucket<..>> but preserves
437 /// the reference that grants us access to the table in any case.
438 enum SearchResult<K, V, M> {
439 // This is an entry that holds the given key:
440 FoundExisting(FullBucket<K, V, M>),
442 // There was no such entry. The reference is given back:
446 impl<K, V, M> SearchResult<K, V, M> {
447 fn into_option(self) -> Option<FullBucket<K, V, M>> {
449 FoundExisting(bucket) => Some(bucket),
455 impl<K, V, S> HashMap<K, V, S>
456 where K: Eq + Hash, S: HashState
458 fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash where X: Hash {
459 table::make_hash(&self.hash_state, x)
462 /// Search for a key, yielding the index if it's found in the hashtable.
463 /// If you already have the hash for the key lying around, use
465 fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> Option<FullBucketImm<'a, K, V>>
466 where K: Borrow<Q>, Q: Eq + Hash
468 let hash = self.make_hash(q);
469 search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
473 fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> Option<FullBucketMut<'a, K, V>>
474 where K: Borrow<Q>, Q: Eq + Hash
476 let hash = self.make_hash(q);
477 search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
481 // The caller should ensure that invariants by Robin Hood Hashing hold.
482 fn insert_hashed_ordered(&mut self, hash: SafeHash, k: K, v: V) {
483 let cap = self.table.capacity();
484 let mut buckets = Bucket::new(&mut self.table, hash);
485 let ib = buckets.index();
487 while buckets.index() != ib + cap {
488 // We don't need to compare hashes for value swap.
489 // Not even DIBs for Robin Hood.
490 buckets = match buckets.peek() {
492 empty.put(hash, k, v);
495 Full(b) => b.into_bucket()
499 panic!("Internal HashMap error: Out of space.");
503 impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
504 /// Create an empty HashMap.
509 /// use std::collections::HashMap;
510 /// let mut map: HashMap<&str, int> = HashMap::new();
513 #[stable(feature = "rust1", since = "1.0.0")]
514 pub fn new() -> HashMap<K, V, RandomState> {
518 /// Creates an empty hash map with the given initial capacity.
523 /// use std::collections::HashMap;
524 /// let mut map: HashMap<&str, int> = HashMap::with_capacity(10);
527 #[stable(feature = "rust1", since = "1.0.0")]
528 pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
529 HashMap::with_capacity_and_hash_state(capacity, Default::default())
533 impl<K, V, S> HashMap<K, V, S>
534 where K: Eq + Hash, S: HashState
536 /// Creates an empty hashmap which will use the given hasher to hash keys.
538 /// The creates map has the default initial capacity.
543 /// use std::collections::HashMap;
544 /// use std::collections::hash_map::RandomState;
546 /// let s = RandomState::new();
547 /// let mut map = HashMap::with_hash_state(s);
548 /// map.insert(1, 2);
551 #[unstable(feature = "std_misc", reason = "hasher stuff is unclear")]
552 pub fn with_hash_state(hash_state: S) -> HashMap<K, V, S> {
554 hash_state: hash_state,
555 resize_policy: DefaultResizePolicy::new(),
556 table: RawTable::new(0),
560 /// Create an empty HashMap with space for at least `capacity`
561 /// elements, using `hasher` to hash the keys.
563 /// Warning: `hasher` is normally randomly generated, and
564 /// is designed to allow HashMaps to be resistant to attacks that
565 /// cause many collisions and very poor performance. Setting it
566 /// manually using this function can expose a DoS attack vector.
571 /// use std::collections::HashMap;
572 /// use std::collections::hash_map::RandomState;
574 /// let s = RandomState::new();
575 /// let mut map = HashMap::with_capacity_and_hash_state(10, s);
576 /// map.insert(1, 2);
579 #[unstable(feature = "std_misc", reason = "hasher stuff is unclear")]
580 pub fn with_capacity_and_hash_state(capacity: usize, hash_state: S)
581 -> HashMap<K, V, S> {
582 let resize_policy = DefaultResizePolicy::new();
583 let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity));
584 let internal_cap = min_cap.checked_next_power_of_two().expect("capacity overflow");
585 assert!(internal_cap >= capacity, "capacity overflow");
587 hash_state: hash_state,
588 resize_policy: resize_policy,
589 table: RawTable::new(internal_cap),
593 /// Returns the number of elements the map can hold without reallocating.
598 /// use std::collections::HashMap;
599 /// let map: HashMap<int, int> = HashMap::with_capacity(100);
600 /// assert!(map.capacity() >= 100);
603 #[stable(feature = "rust1", since = "1.0.0")]
604 pub fn capacity(&self) -> usize {
605 self.resize_policy.usable_capacity(self.table.capacity())
608 /// Reserves capacity for at least `additional` more elements to be inserted
609 /// in the `HashMap`. The collection may reserve more space to avoid
610 /// frequent reallocations.
614 /// Panics if the new allocation size overflows `usize`.
619 /// use std::collections::HashMap;
620 /// let mut map: HashMap<&str, int> = HashMap::new();
623 #[stable(feature = "rust1", since = "1.0.0")]
624 pub fn reserve(&mut self, additional: usize) {
625 let new_size = self.len().checked_add(additional).expect("capacity overflow");
626 let min_cap = self.resize_policy.min_capacity(new_size);
628 // An invalid value shouldn't make us run out of space. This includes
629 // an overflow check.
630 assert!(new_size <= min_cap);
632 if self.table.capacity() < min_cap {
633 let new_capacity = max(min_cap.next_power_of_two(), INITIAL_CAPACITY);
634 self.resize(new_capacity);
638 /// Resizes the internal vectors to a new capacity. It's your responsibility to:
639 /// 1) Make sure the new capacity is enough for all the elements, accounting
640 /// for the load factor.
641 /// 2) Ensure new_capacity is a power of two or zero.
642 fn resize(&mut self, new_capacity: usize) {
643 assert!(self.table.size() <= new_capacity);
644 assert!(new_capacity.is_power_of_two() || new_capacity == 0);
646 let mut old_table = replace(&mut self.table, RawTable::new(new_capacity));
647 let old_size = old_table.size();
649 if old_table.capacity() == 0 || old_table.size() == 0 {
654 // Specialization of the other branch.
655 let mut bucket = Bucket::first(&mut old_table);
657 // "So a few of the first shall be last: for many be called,
660 // We'll most likely encounter a few buckets at the beginning that
661 // have their initial buckets near the end of the table. They were
662 // placed at the beginning as the probe wrapped around the table
663 // during insertion. We must skip forward to a bucket that won't
664 // get reinserted too early and won't unfairly steal others spot.
665 // This eliminates the need for robin hood.
667 bucket = match bucket.peek() {
669 if full.distance() == 0 {
670 // This bucket occupies its ideal spot.
671 // It indicates the start of another "cluster".
672 bucket = full.into_bucket();
675 // Leaving this bucket in the last cluster for later.
679 // Encountered a hole between clusters.
686 // This is how the buckets might be laid out in memory:
687 // ($ marks an initialized bucket)
689 // |$$$_$$$$$$_$$$$$|
691 // But we've skipped the entire initial cluster of buckets
692 // and will continue iteration in this order:
695 // ^ wrap around once end is reached
698 // ^ exit once table.size == 0
700 bucket = match bucket.peek() {
702 let h = bucket.hash();
703 let (b, k, v) = bucket.take();
704 self.insert_hashed_ordered(h, k, v);
706 let t = b.table(); // FIXME "lifetime too short".
707 if t.size() == 0 { break }
711 Empty(b) => b.into_bucket()
716 assert_eq!(self.table.size(), old_size);
719 /// Shrinks the capacity of the map as much as possible. It will drop
720 /// down as much as possible while maintaining the internal rules
721 /// and possibly leaving some space in accordance with the resize policy.
726 /// use std::collections::HashMap;
728 /// let mut map: HashMap<int, int> = HashMap::with_capacity(100);
729 /// map.insert(1, 2);
730 /// map.insert(3, 4);
731 /// assert!(map.capacity() >= 100);
732 /// map.shrink_to_fit();
733 /// assert!(map.capacity() >= 2);
735 #[stable(feature = "rust1", since = "1.0.0")]
736 pub fn shrink_to_fit(&mut self) {
737 let min_capacity = self.resize_policy.min_capacity(self.len());
738 let min_capacity = max(min_capacity.next_power_of_two(), INITIAL_CAPACITY);
740 // An invalid value shouldn't make us run out of space.
741 debug_assert!(self.len() <= min_capacity);
743 if self.table.capacity() != min_capacity {
744 let old_table = replace(&mut self.table, RawTable::new(min_capacity));
745 let old_size = old_table.size();
747 // Shrink the table. Naive algorithm for resizing:
748 for (h, k, v) in old_table.into_iter() {
749 self.insert_hashed_nocheck(h, k, v);
752 debug_assert_eq!(self.table.size(), old_size);
756 /// Insert a pre-hashed key-value pair, without first checking
757 /// that there's enough room in the buckets. Returns a reference to the
758 /// newly insert value.
760 /// If the key already exists, the hashtable will be returned untouched
761 /// and a reference to the existing element will be returned.
762 fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> &mut V {
763 self.insert_or_replace_with(hash, k, v, |_, _, _| ())
766 fn insert_or_replace_with<'a, F>(&'a mut self,
770 mut found_existing: F)
772 F: FnMut(&mut K, &mut V, V),
774 // Worst case, we'll find one empty bucket among `size + 1` buckets.
775 let size = self.table.size();
776 let mut probe = Bucket::new(&mut self.table, hash);
777 let ib = probe.index();
780 let mut bucket = match probe.peek() {
783 return bucket.put(hash, k, v).into_mut_refs().1;
785 Full(bucket) => bucket
789 if bucket.hash() == hash {
791 if k == *bucket.read_mut().0 {
792 let (bucket_k, bucket_v) = bucket.into_mut_refs();
793 debug_assert!(k == *bucket_k);
794 // Key already exists. Get its reference.
795 found_existing(bucket_k, bucket_v, v);
800 let robin_ib = bucket.index() as int - bucket.distance() as int;
802 if (ib as int) < robin_ib {
803 // Found a luckier bucket than me. Better steal his spot.
804 return robin_hood(bucket, robin_ib as usize, hash, k, v);
807 probe = bucket.next();
808 assert!(probe.index() != ib + size + 1);
812 /// An iterator visiting all keys in arbitrary order.
813 /// Iterator element type is `&'a K`.
818 /// use std::collections::HashMap;
820 /// let mut map = HashMap::new();
821 /// map.insert("a", 1);
822 /// map.insert("b", 2);
823 /// map.insert("c", 3);
825 /// for key in map.keys() {
826 /// println!("{}", key);
829 #[stable(feature = "rust1", since = "1.0.0")]
830 pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
831 fn first<A, B>((a, _): (A, B)) -> A { a }
832 let first: fn((&'a K,&'a V)) -> &'a K = first; // coerce to fn ptr
834 Keys { inner: self.iter().map(first) }
837 /// An iterator visiting all values in arbitrary order.
838 /// Iterator element type is `&'a V`.
843 /// use std::collections::HashMap;
845 /// let mut map = HashMap::new();
846 /// map.insert("a", 1);
847 /// map.insert("b", 2);
848 /// map.insert("c", 3);
850 /// for val in map.values() {
851 /// println!("{}", val);
854 #[stable(feature = "rust1", since = "1.0.0")]
855 pub fn values<'a>(&'a self) -> Values<'a, K, V> {
856 fn second<A, B>((_, b): (A, B)) -> B { b }
857 let second: fn((&'a K,&'a V)) -> &'a V = second; // coerce to fn ptr
859 Values { inner: self.iter().map(second) }
862 /// An iterator visiting all key-value pairs in arbitrary order.
863 /// Iterator element type is `(&'a K, &'a V)`.
868 /// use std::collections::HashMap;
870 /// let mut map = HashMap::new();
871 /// map.insert("a", 1);
872 /// map.insert("b", 2);
873 /// map.insert("c", 3);
875 /// for (key, val) in map.iter() {
876 /// println!("key: {} val: {}", key, val);
879 #[stable(feature = "rust1", since = "1.0.0")]
880 pub fn iter(&self) -> Iter<K, V> {
881 Iter { inner: self.table.iter() }
884 /// An iterator visiting all key-value pairs in arbitrary order,
885 /// with mutable references to the values.
886 /// Iterator element type is `(&'a K, &'a mut V)`.
891 /// use std::collections::HashMap;
893 /// let mut map = HashMap::new();
894 /// map.insert("a", 1);
895 /// map.insert("b", 2);
896 /// map.insert("c", 3);
898 /// // Update all values
899 /// for (_, val) in map.iter_mut() {
903 /// for (key, val) in map.iter() {
904 /// println!("key: {} val: {}", key, val);
907 #[stable(feature = "rust1", since = "1.0.0")]
908 pub fn iter_mut(&mut self) -> IterMut<K, V> {
909 IterMut { inner: self.table.iter_mut() }
912 /// Creates a consuming iterator, that is, one that moves each key-value
913 /// pair out of the map in arbitrary order. The map cannot be used after
919 /// use std::collections::HashMap;
921 /// let mut map = HashMap::new();
922 /// map.insert("a", 1);
923 /// map.insert("b", 2);
924 /// map.insert("c", 3);
926 /// // Not possible with .iter()
927 /// let vec: Vec<(&str, int)> = map.into_iter().collect();
929 #[stable(feature = "rust1", since = "1.0.0")]
930 pub fn into_iter(self) -> IntoIter<K, V> {
931 fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
932 let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
935 inner: self.table.into_iter().map(last_two)
939 /// Gets the given key's corresponding entry in the map for in-place manipulation.
940 #[stable(feature = "rust1", since = "1.0.0")]
941 pub fn entry(&mut self, key: K) -> Entry<K, V> {
945 let hash = self.make_hash(&key);
946 search_entry_hashed(&mut self.table, hash, key)
949 /// Returns the number of elements in the map.
954 /// use std::collections::HashMap;
956 /// let mut a = HashMap::new();
957 /// assert_eq!(a.len(), 0);
958 /// a.insert(1, "a");
959 /// assert_eq!(a.len(), 1);
961 #[stable(feature = "rust1", since = "1.0.0")]
962 pub fn len(&self) -> usize { self.table.size() }
964 /// Returns true if the map contains no elements.
969 /// use std::collections::HashMap;
971 /// let mut a = HashMap::new();
972 /// assert!(a.is_empty());
973 /// a.insert(1, "a");
974 /// assert!(!a.is_empty());
977 #[stable(feature = "rust1", since = "1.0.0")]
978 pub fn is_empty(&self) -> bool { self.len() == 0 }
980 /// Clears the map, returning all key-value pairs as an iterator. Keeps the
981 /// allocated memory for reuse.
986 /// use std::collections::HashMap;
988 /// let mut a = HashMap::new();
989 /// a.insert(1, "a");
990 /// a.insert(2, "b");
992 /// for (k, v) in a.drain().take(1) {
993 /// assert!(k == 1 || k == 2);
994 /// assert!(v == "a" || v == "b");
997 /// assert!(a.is_empty());
1000 #[unstable(feature = "std_misc",
1001 reason = "matches collection reform specification, waiting for dust to settle")]
1002 pub fn drain(&mut self) -> Drain<K, V> {
1003 fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1004 let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two; // coerce to fn pointer
1007 inner: self.table.drain().map(last_two),
1011 /// Clears the map, removing all key-value pairs. Keeps the allocated memory
1017 /// use std::collections::HashMap;
1019 /// let mut a = HashMap::new();
1020 /// a.insert(1, "a");
1022 /// assert!(a.is_empty());
1024 #[stable(feature = "rust1", since = "1.0.0")]
1026 pub fn clear(&mut self) {
1030 /// Returns a reference to the value corresponding to the key.
1032 /// The key may be any borrowed form of the map's key type, but
1033 /// `Hash` and `Eq` on the borrowed form *must* match those for
1039 /// use std::collections::HashMap;
1041 /// let mut map = HashMap::new();
1042 /// map.insert(1, "a");
1043 /// assert_eq!(map.get(&1), Some(&"a"));
1044 /// assert_eq!(map.get(&2), None);
1046 #[stable(feature = "rust1", since = "1.0.0")]
1047 pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
1048 where K: Borrow<Q>, Q: Hash + Eq
1050 self.search(k).map(|bucket| bucket.into_refs().1)
1053 /// Returns true if the map contains a value for the specified key.
1055 /// The key may be any borrowed form of the map's key type, but
1056 /// `Hash` and `Eq` on the borrowed form *must* match those for
1062 /// use std::collections::HashMap;
1064 /// let mut map = HashMap::new();
1065 /// map.insert(1, "a");
1066 /// assert_eq!(map.contains_key(&1), true);
1067 /// assert_eq!(map.contains_key(&2), false);
1069 #[stable(feature = "rust1", since = "1.0.0")]
1070 pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
1071 where K: Borrow<Q>, Q: Hash + Eq
1073 self.search(k).is_some()
1076 /// Returns a mutable reference to the value corresponding to the key.
1078 /// The key may be any borrowed form of the map's key type, but
1079 /// `Hash` and `Eq` on the borrowed form *must* match those for
1085 /// use std::collections::HashMap;
1087 /// let mut map = HashMap::new();
1088 /// map.insert(1, "a");
1089 /// match map.get_mut(&1) {
1090 /// Some(x) => *x = "b",
1093 /// assert_eq!(map[1], "b");
1095 #[stable(feature = "rust1", since = "1.0.0")]
1096 pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
1097 where K: Borrow<Q>, Q: Hash + Eq
1099 self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
1102 /// Inserts a key-value pair from the map. If the key already had a value
1103 /// present in the map, that value is returned. Otherwise, `None` is returned.
1108 /// use std::collections::HashMap;
1110 /// let mut map = HashMap::new();
1111 /// assert_eq!(map.insert(37, "a"), None);
1112 /// assert_eq!(map.is_empty(), false);
1114 /// map.insert(37, "b");
1115 /// assert_eq!(map.insert(37, "c"), Some("b"));
1116 /// assert_eq!(map[37], "c");
1118 #[stable(feature = "rust1", since = "1.0.0")]
1119 pub fn insert(&mut self, k: K, v: V) -> Option<V> {
1120 let hash = self.make_hash(&k);
1123 let mut retval = None;
1124 self.insert_or_replace_with(hash, k, v, |_, val_ref, val| {
1125 retval = Some(replace(val_ref, val));
1130 /// Removes a key from the map, returning the value at the key if the key
1131 /// was previously in the map.
1133 /// The key may be any borrowed form of the map's key type, but
1134 /// `Hash` and `Eq` on the borrowed form *must* match those for
1140 /// use std::collections::HashMap;
1142 /// let mut map = HashMap::new();
1143 /// map.insert(1, "a");
1144 /// assert_eq!(map.remove(&1), Some("a"));
1145 /// assert_eq!(map.remove(&1), None);
1147 #[stable(feature = "rust1", since = "1.0.0")]
1148 pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
1149 where K: Borrow<Q>, Q: Hash + Eq
1151 if self.table.size() == 0 {
1155 self.search_mut(k).map(|bucket| pop_internal(bucket).1)
1159 fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: K)
1162 // Worst case, we'll find one empty bucket among `size + 1` buckets.
1163 let size = table.size();
1164 let mut probe = Bucket::new(table, hash);
1165 let ib = probe.index();
1168 let bucket = match probe.peek() {
1171 return Vacant(VacantEntry {
1174 elem: NoElem(bucket),
1177 Full(bucket) => bucket
1181 if bucket.hash() == hash {
1183 if k == *bucket.read().0 {
1184 return Occupied(OccupiedEntry{
1190 let robin_ib = bucket.index() as int - bucket.distance() as int;
1192 if (ib as int) < robin_ib {
1193 // Found a luckier bucket than me. Better steal his spot.
1194 return Vacant(VacantEntry {
1197 elem: NeqElem(bucket, robin_ib as usize),
1201 probe = bucket.next();
1202 assert!(probe.index() != ib + size + 1);
1206 impl<K, V, S> PartialEq for HashMap<K, V, S>
1207 where K: Eq + Hash, V: PartialEq, S: HashState
1209 fn eq(&self, other: &HashMap<K, V, S>) -> bool {
1210 if self.len() != other.len() { return false; }
1212 self.iter().all(|(key, value)|
1213 other.get(key).map_or(false, |v| *value == *v)
1218 #[stable(feature = "rust1", since = "1.0.0")]
1219 impl<K, V, S> Eq for HashMap<K, V, S>
1220 where K: Eq + Hash, V: Eq, S: HashState
1223 #[stable(feature = "rust1", since = "1.0.0")]
1224 impl<K, V, S> Debug for HashMap<K, V, S>
1225 where K: Eq + Hash + Debug, V: Debug, S: HashState
1227 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1228 try!(write!(f, "{{"));
1230 for (i, (k, v)) in self.iter().enumerate() {
1231 if i != 0 { try!(write!(f, ", ")); }
1232 try!(write!(f, "{:?}: {:?}", *k, *v));
1239 #[stable(feature = "rust1", since = "1.0.0")]
1240 impl<K, V, S> Default for HashMap<K, V, S>
1242 S: HashState + Default,
1244 fn default() -> HashMap<K, V, S> {
1245 HashMap::with_hash_state(Default::default())
1249 #[stable(feature = "rust1", since = "1.0.0")]
1250 impl<K, Q: ?Sized, V, S> Index<Q> for HashMap<K, V, S>
1251 where K: Eq + Hash + Borrow<Q>,
1258 fn index<'a>(&'a self, index: &Q) -> &'a V {
1259 self.get(index).expect("no entry found for key")
1263 #[stable(feature = "rust1", since = "1.0.0")]
1264 impl<K, V, S, Q: ?Sized> IndexMut<Q> for HashMap<K, V, S>
1265 where K: Eq + Hash + Borrow<Q>,
1270 fn index_mut<'a>(&'a mut self, index: &Q) -> &'a mut V {
1271 self.get_mut(index).expect("no entry found for key")
1275 /// HashMap iterator.
1276 #[stable(feature = "rust1", since = "1.0.0")]
1277 pub struct Iter<'a, K: 'a, V: 'a> {
1278 inner: table::Iter<'a, K, V>
1281 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1282 impl<'a, K, V> Clone for Iter<'a, K, V> {
1283 fn clone(&self) -> Iter<'a, K, V> {
1285 inner: self.inner.clone()
1290 /// HashMap mutable values iterator.
1291 #[stable(feature = "rust1", since = "1.0.0")]
1292 pub struct IterMut<'a, K: 'a, V: 'a> {
1293 inner: table::IterMut<'a, K, V>
1296 /// HashMap move iterator.
1297 #[stable(feature = "rust1", since = "1.0.0")]
1298 pub struct IntoIter<K, V> {
1299 inner: iter::Map<table::IntoIter<K, V>, fn((SafeHash, K, V)) -> (K, V)>
1302 /// HashMap keys iterator.
1303 #[stable(feature = "rust1", since = "1.0.0")]
1304 pub struct Keys<'a, K: 'a, V: 'a> {
1305 inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
1308 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1309 impl<'a, K, V> Clone for Keys<'a, K, V> {
1310 fn clone(&self) -> Keys<'a, K, V> {
1312 inner: self.inner.clone()
1317 /// HashMap values iterator.
1318 #[stable(feature = "rust1", since = "1.0.0")]
1319 pub struct Values<'a, K: 'a, V: 'a> {
1320 inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
1323 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1324 impl<'a, K, V> Clone for Values<'a, K, V> {
1325 fn clone(&self) -> Values<'a, K, V> {
1327 inner: self.inner.clone()
1332 /// HashMap drain iterator.
1333 #[unstable(feature = "std_misc",
1334 reason = "matches collection reform specification, waiting for dust to settle")]
1335 pub struct Drain<'a, K: 'a, V: 'a> {
1336 inner: iter::Map<table::Drain<'a, K, V>, fn((SafeHash, K, V)) -> (K, V)>
1339 /// A view into a single occupied location in a HashMap.
1340 #[unstable(feature = "std_misc",
1341 reason = "precise API still being fleshed out")]
1342 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
1343 elem: FullBucket<K, V, &'a mut RawTable<K, V>>,
1346 /// A view into a single empty location in a HashMap.
1347 #[unstable(feature = "std_misc",
1348 reason = "precise API still being fleshed out")]
1349 pub struct VacantEntry<'a, K: 'a, V: 'a> {
1352 elem: VacantEntryState<K, V, &'a mut RawTable<K, V>>,
1355 /// A view into a single location in a map, which may be vacant or occupied.
1356 #[unstable(feature = "std_misc",
1357 reason = "precise API still being fleshed out")]
1358 pub enum Entry<'a, K: 'a, V: 'a> {
1359 /// An occupied Entry.
1360 Occupied(OccupiedEntry<'a, K, V>),
1362 Vacant(VacantEntry<'a, K, V>),
1365 /// Possible states of a VacantEntry.
1366 enum VacantEntryState<K, V, M> {
1367 /// The index is occupied, but the key to insert has precedence,
1368 /// and will kick the current one out on insertion.
1369 NeqElem(FullBucket<K, V, M>, usize),
1370 /// The index is genuinely vacant.
1371 NoElem(EmptyBucket<K, V, M>),
1374 #[stable(feature = "rust1", since = "1.0.0")]
1375 impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
1376 where K: Eq + Hash, S: HashState
1378 type Item = (&'a K, &'a V);
1379 type IntoIter = Iter<'a, K, V>;
1381 fn into_iter(self) -> Iter<'a, K, V> {
1386 #[stable(feature = "rust1", since = "1.0.0")]
1387 impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
1388 where K: Eq + Hash, S: HashState
1390 type Item = (&'a K, &'a mut V);
1391 type IntoIter = IterMut<'a, K, V>;
1393 fn into_iter(mut self) -> IterMut<'a, K, V> {
1398 #[stable(feature = "rust1", since = "1.0.0")]
1399 impl<K, V, S> IntoIterator for HashMap<K, V, S>
1400 where K: Eq + Hash, S: HashState
1403 type IntoIter = IntoIter<K, V>;
1405 fn into_iter(self) -> IntoIter<K, V> {
1410 #[stable(feature = "rust1", since = "1.0.0")]
1411 impl<'a, K, V> Iterator for Iter<'a, K, V> {
1412 type Item = (&'a K, &'a V);
1414 #[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
1415 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1417 #[stable(feature = "rust1", since = "1.0.0")]
1418 impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1419 #[inline] fn len(&self) -> usize { self.inner.len() }
1422 #[stable(feature = "rust1", since = "1.0.0")]
1423 impl<'a, K, V> Iterator for IterMut<'a, K, V> {
1424 type Item = (&'a K, &'a mut V);
1426 #[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
1427 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1429 #[stable(feature = "rust1", since = "1.0.0")]
1430 impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1431 #[inline] fn len(&self) -> usize { self.inner.len() }
1434 #[stable(feature = "rust1", since = "1.0.0")]
1435 impl<K, V> Iterator for IntoIter<K, V> {
1438 #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1439 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1441 #[stable(feature = "rust1", since = "1.0.0")]
1442 impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1443 #[inline] fn len(&self) -> usize { self.inner.len() }
1446 #[stable(feature = "rust1", since = "1.0.0")]
1447 impl<'a, K, V> Iterator for Keys<'a, K, V> {
1450 #[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1451 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1453 #[stable(feature = "rust1", since = "1.0.0")]
1454 impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1455 #[inline] fn len(&self) -> usize { self.inner.len() }
1458 #[stable(feature = "rust1", since = "1.0.0")]
1459 impl<'a, K, V> Iterator for Values<'a, K, V> {
1462 #[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1463 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1465 #[stable(feature = "rust1", since = "1.0.0")]
1466 impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1467 #[inline] fn len(&self) -> usize { self.inner.len() }
1470 #[stable(feature = "rust1", since = "1.0.0")]
1471 impl<'a, K, V> Iterator for Drain<'a, K, V> {
1474 #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1475 #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1477 #[stable(feature = "rust1", since = "1.0.0")]
1478 impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
1479 #[inline] fn len(&self) -> usize { self.inner.len() }
1482 #[unstable(feature = "std_misc",
1483 reason = "matches collection reform v2 specification, waiting for dust to settle")]
1484 impl<'a, K, V> Entry<'a, K, V> {
1485 /// Returns a mutable reference to the entry if occupied, or the VacantEntry if vacant.
1486 pub fn get(self) -> Result<&'a mut V, VacantEntry<'a, K, V>> {
1488 Occupied(entry) => Ok(entry.into_mut()),
1489 Vacant(entry) => Err(entry),
1494 impl<'a, K, V> OccupiedEntry<'a, K, V> {
1495 /// Gets a reference to the value in the entry.
1496 #[stable(feature = "rust1", since = "1.0.0")]
1497 pub fn get(&self) -> &V {
1501 /// Gets a mutable reference to the value in the entry.
1502 #[stable(feature = "rust1", since = "1.0.0")]
1503 pub fn get_mut(&mut self) -> &mut V {
1504 self.elem.read_mut().1
1507 /// Converts the OccupiedEntry into a mutable reference to the value in the entry
1508 /// with a lifetime bound to the map itself
1509 #[stable(feature = "rust1", since = "1.0.0")]
1510 pub fn into_mut(self) -> &'a mut V {
1511 self.elem.into_mut_refs().1
1514 /// Sets the value of the entry, and returns the entry's old value
1515 #[stable(feature = "rust1", since = "1.0.0")]
1516 pub fn insert(&mut self, mut value: V) -> V {
1517 let old_value = self.get_mut();
1518 mem::swap(&mut value, old_value);
1522 /// Takes the value out of the entry, and returns it
1523 #[stable(feature = "rust1", since = "1.0.0")]
1524 pub fn remove(self) -> V {
1525 pop_internal(self.elem).1
1529 impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
1530 /// Sets the value of the entry with the VacantEntry's key,
1531 /// and returns a mutable reference to it
1532 #[stable(feature = "rust1", since = "1.0.0")]
1533 pub fn insert(self, value: V) -> &'a mut V {
1535 NeqElem(bucket, ib) => {
1536 robin_hood(bucket, ib, self.hash, self.key, value)
1539 bucket.put(self.hash, self.key, value).into_mut_refs().1
1545 #[stable(feature = "rust1", since = "1.0.0")]
1546 impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
1547 where K: Eq + Hash, S: HashState + Default
1549 fn from_iter<T: IntoIterator<Item=(K, V)>>(iterable: T) -> HashMap<K, V, S> {
1550 let iter = iterable.into_iter();
1551 let lower = iter.size_hint().0;
1552 let mut map = HashMap::with_capacity_and_hash_state(lower,
1553 Default::default());
1559 #[stable(feature = "rust1", since = "1.0.0")]
1560 impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
1561 where K: Eq + Hash, S: HashState
1563 fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) {
1564 for (k, v) in iter {
1571 /// `RandomState` is the default state for `HashMap` types.
1573 /// A particular instance `RandomState` will create the same instances of
1574 /// `Hasher`, but the hashers created by two different `RandomState`
1575 /// instances are unlikely to produce the same result for the same values.
1577 #[unstable(feature = "std_misc",
1578 reason = "hashing an hash maps may be altered")]
1579 pub struct RandomState {
1584 #[unstable(feature = "std_misc",
1585 reason = "hashing an hash maps may be altered")]
1587 /// Construct a new `RandomState` that is initialized with random keys.
1589 #[allow(deprecated)]
1590 pub fn new() -> RandomState {
1591 let mut r = rand::thread_rng();
1592 RandomState { k0: r.gen(), k1: r.gen() }
1596 #[unstable(feature = "std_misc",
1597 reason = "hashing an hash maps may be altered")]
1598 impl HashState for RandomState {
1599 type Hasher = SipHasher;
1600 fn hasher(&self) -> SipHasher {
1601 SipHasher::new_with_keys(self.k0, self.k1)
1605 #[unstable(feature = "std_misc",
1606 reason = "hashing an hash maps may be altered")]
1607 impl Default for RandomState {
1609 fn default() -> RandomState {
1619 use super::Entry::{Occupied, Vacant};
1620 use iter::{range_inclusive, range_step_inclusive, repeat};
1622 use rand::{weak_rng, Rng};
1625 fn test_create_capacity_zero() {
1626 let mut m = HashMap::with_capacity(0);
1628 assert!(m.insert(1, 1).is_none());
1630 assert!(m.contains_key(&1));
1631 assert!(!m.contains_key(&0));
1636 let mut m = HashMap::new();
1637 assert_eq!(m.len(), 0);
1638 assert!(m.insert(1, 2).is_none());
1639 assert_eq!(m.len(), 1);
1640 assert!(m.insert(2, 4).is_none());
1641 assert_eq!(m.len(), 2);
1642 assert_eq!(*m.get(&1).unwrap(), 2);
1643 assert_eq!(*m.get(&2).unwrap(), 4);
1646 thread_local! { static DROP_VECTOR: RefCell<Vec<int>> = RefCell::new(Vec::new()) }
1648 #[derive(Hash, PartialEq, Eq)]
1654 fn new(k: usize) -> Dropable {
1655 DROP_VECTOR.with(|slot| {
1656 slot.borrow_mut()[k] += 1;
1663 impl Drop for Dropable {
1664 fn drop(&mut self) {
1665 DROP_VECTOR.with(|slot| {
1666 slot.borrow_mut()[self.k] -= 1;
1671 impl Clone for Dropable {
1672 fn clone(&self) -> Dropable {
1673 Dropable::new(self.k)
1679 DROP_VECTOR.with(|slot| {
1680 *slot.borrow_mut() = repeat(0).take(200).collect();
1684 let mut m = HashMap::new();
1686 DROP_VECTOR.with(|v| {
1688 assert_eq!(v.borrow()[i], 0);
1693 let d1 = Dropable::new(i);
1694 let d2 = Dropable::new(i+100);
1698 DROP_VECTOR.with(|v| {
1700 assert_eq!(v.borrow()[i], 1);
1705 let k = Dropable::new(i);
1706 let v = m.remove(&k);
1708 assert!(v.is_some());
1710 DROP_VECTOR.with(|v| {
1711 assert_eq!(v.borrow()[i], 1);
1712 assert_eq!(v.borrow()[i+100], 1);
1716 DROP_VECTOR.with(|v| {
1718 assert_eq!(v.borrow()[i], 0);
1719 assert_eq!(v.borrow()[i+100], 0);
1723 assert_eq!(v.borrow()[i], 1);
1724 assert_eq!(v.borrow()[i+100], 1);
1729 DROP_VECTOR.with(|v| {
1731 assert_eq!(v.borrow()[i], 0);
1737 fn test_move_iter_drops() {
1738 DROP_VECTOR.with(|v| {
1739 *v.borrow_mut() = repeat(0).take(200).collect();
1743 let mut hm = HashMap::new();
1745 DROP_VECTOR.with(|v| {
1747 assert_eq!(v.borrow()[i], 0);
1752 let d1 = Dropable::new(i);
1753 let d2 = Dropable::new(i+100);
1757 DROP_VECTOR.with(|v| {
1759 assert_eq!(v.borrow()[i], 1);
1766 // By the way, ensure that cloning doesn't screw up the dropping.
1770 let mut half = hm.into_iter().take(50);
1772 DROP_VECTOR.with(|v| {
1774 assert_eq!(v.borrow()[i], 1);
1778 for _ in half.by_ref() {}
1780 DROP_VECTOR.with(|v| {
1781 let nk = (0..100).filter(|&i| {
1785 let nv = (0..100).filter(|&i| {
1786 v.borrow()[i+100] == 1
1794 DROP_VECTOR.with(|v| {
1796 assert_eq!(v.borrow()[i], 0);
1802 fn test_empty_pop() {
1803 let mut m: HashMap<int, bool> = HashMap::new();
1804 assert_eq!(m.remove(&0), None);
1808 fn test_lots_of_insertions() {
1809 let mut m = HashMap::new();
1811 // Try this a few times to make sure we never screw up the hashmap's
1814 assert!(m.is_empty());
1816 for i in range_inclusive(1, 1000) {
1817 assert!(m.insert(i, i).is_none());
1819 for j in range_inclusive(1, i) {
1821 assert_eq!(r, Some(&j));
1824 for j in range_inclusive(i+1, 1000) {
1826 assert_eq!(r, None);
1830 for i in range_inclusive(1001, 2000) {
1831 assert!(!m.contains_key(&i));
1835 for i in range_inclusive(1, 1000) {
1836 assert!(m.remove(&i).is_some());
1838 for j in range_inclusive(1, i) {
1839 assert!(!m.contains_key(&j));
1842 for j in range_inclusive(i+1, 1000) {
1843 assert!(m.contains_key(&j));
1847 for i in range_inclusive(1, 1000) {
1848 assert!(!m.contains_key(&i));
1851 for i in range_inclusive(1, 1000) {
1852 assert!(m.insert(i, i).is_none());
1856 for i in range_step_inclusive(1000, 1, -1) {
1857 assert!(m.remove(&i).is_some());
1859 for j in range_inclusive(i, 1000) {
1860 assert!(!m.contains_key(&j));
1863 for j in range_inclusive(1, i-1) {
1864 assert!(m.contains_key(&j));
1871 fn test_find_mut() {
1872 let mut m = HashMap::new();
1873 assert!(m.insert(1, 12).is_none());
1874 assert!(m.insert(2, 8).is_none());
1875 assert!(m.insert(5, 14).is_none());
1877 match m.get_mut(&5) {
1878 None => panic!(), Some(x) => *x = new
1880 assert_eq!(m.get(&5), Some(&new));
1884 fn test_insert_overwrite() {
1885 let mut m = HashMap::new();
1886 assert!(m.insert(1, 2).is_none());
1887 assert_eq!(*m.get(&1).unwrap(), 2);
1888 assert!(!m.insert(1, 3).is_none());
1889 assert_eq!(*m.get(&1).unwrap(), 3);
1893 fn test_insert_conflicts() {
1894 let mut m = HashMap::with_capacity(4);
1895 assert!(m.insert(1, 2).is_none());
1896 assert!(m.insert(5, 3).is_none());
1897 assert!(m.insert(9, 4).is_none());
1898 assert_eq!(*m.get(&9).unwrap(), 4);
1899 assert_eq!(*m.get(&5).unwrap(), 3);
1900 assert_eq!(*m.get(&1).unwrap(), 2);
1904 fn test_conflict_remove() {
1905 let mut m = HashMap::with_capacity(4);
1906 assert!(m.insert(1, 2).is_none());
1907 assert_eq!(*m.get(&1).unwrap(), 2);
1908 assert!(m.insert(5, 3).is_none());
1909 assert_eq!(*m.get(&1).unwrap(), 2);
1910 assert_eq!(*m.get(&5).unwrap(), 3);
1911 assert!(m.insert(9, 4).is_none());
1912 assert_eq!(*m.get(&1).unwrap(), 2);
1913 assert_eq!(*m.get(&5).unwrap(), 3);
1914 assert_eq!(*m.get(&9).unwrap(), 4);
1915 assert!(m.remove(&1).is_some());
1916 assert_eq!(*m.get(&9).unwrap(), 4);
1917 assert_eq!(*m.get(&5).unwrap(), 3);
1921 fn test_is_empty() {
1922 let mut m = HashMap::with_capacity(4);
1923 assert!(m.insert(1, 2).is_none());
1924 assert!(!m.is_empty());
1925 assert!(m.remove(&1).is_some());
1926 assert!(m.is_empty());
1931 let mut m = HashMap::new();
1933 assert_eq!(m.remove(&1), Some(2));
1934 assert_eq!(m.remove(&1), None);
1939 let mut m = HashMap::with_capacity(4);
1941 assert!(m.insert(i, i*2).is_none());
1943 assert_eq!(m.len(), 32);
1945 let mut observed: u32 = 0;
1948 assert_eq!(*v, *k * 2);
1949 observed |= 1 << *k;
1951 assert_eq!(observed, 0xFFFF_FFFF);
1956 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
1957 let map: HashMap<_, _> = vec.into_iter().collect();
1958 let keys: Vec<_> = map.keys().cloned().collect();
1959 assert_eq!(keys.len(), 3);
1960 assert!(keys.contains(&1));
1961 assert!(keys.contains(&2));
1962 assert!(keys.contains(&3));
1967 let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
1968 let map: HashMap<_, _> = vec.into_iter().collect();
1969 let values: Vec<_> = map.values().cloned().collect();
1970 assert_eq!(values.len(), 3);
1971 assert!(values.contains(&'a'));
1972 assert!(values.contains(&'b'));
1973 assert!(values.contains(&'c'));
1978 let mut m = HashMap::new();
1979 assert!(m.get(&1).is_none());
1983 Some(v) => assert_eq!(*v, 2)
1989 let mut m1 = HashMap::new();
1994 let mut m2 = HashMap::new();
2007 let mut map = HashMap::new();
2008 let empty: HashMap<i32, i32> = HashMap::new();
2013 let map_str = format!("{:?}", map);
2015 assert!(map_str == "{1: 2, 3: 4}" ||
2016 map_str == "{3: 4, 1: 2}");
2017 assert_eq!(format!("{:?}", empty), "{}");
2022 let mut m = HashMap::new();
2024 assert_eq!(m.len(), 0);
2025 assert!(m.is_empty());
2028 let old_cap = m.table.capacity();
2029 while old_cap == m.table.capacity() {
2034 assert_eq!(m.len(), i);
2035 assert!(!m.is_empty());
2039 fn test_behavior_resize_policy() {
2040 let mut m = HashMap::new();
2042 assert_eq!(m.len(), 0);
2043 assert_eq!(m.table.capacity(), 0);
2044 assert!(m.is_empty());
2048 assert!(m.is_empty());
2049 let initial_cap = m.table.capacity();
2050 m.reserve(initial_cap);
2051 let cap = m.table.capacity();
2053 assert_eq!(cap, initial_cap * 2);
2056 for _ in 0..cap * 3 / 4 {
2060 // three quarters full
2062 assert_eq!(m.len(), i);
2063 assert_eq!(m.table.capacity(), cap);
2065 for _ in 0..cap / 4 {
2071 let new_cap = m.table.capacity();
2072 assert_eq!(new_cap, cap * 2);
2074 for _ in 0..cap / 2 - 1 {
2077 assert_eq!(m.table.capacity(), new_cap);
2079 // A little more than one quarter full.
2081 assert_eq!(m.table.capacity(), cap);
2082 // again, a little more than half full
2083 for _ in 0..cap / 2 - 1 {
2089 assert_eq!(m.len(), i);
2090 assert!(!m.is_empty());
2091 assert_eq!(m.table.capacity(), initial_cap);
2095 fn test_reserve_shrink_to_fit() {
2096 let mut m = HashMap::new();
2099 assert!(m.capacity() >= m.len());
2105 let usable_cap = m.capacity();
2106 for i in 128..(128 + 256) {
2108 assert_eq!(m.capacity(), usable_cap);
2111 for i in 100..(128 + 256) {
2112 assert_eq!(m.remove(&i), Some(i));
2116 assert_eq!(m.len(), 100);
2117 assert!(!m.is_empty());
2118 assert!(m.capacity() >= m.len());
2121 assert_eq!(m.remove(&i), Some(i));
2126 assert_eq!(m.len(), 1);
2127 assert!(m.capacity() >= m.len());
2128 assert_eq!(m.remove(&0), Some(0));
2132 fn test_from_iter() {
2133 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2135 let map: HashMap<_, _> = xs.iter().cloned().collect();
2137 for &(k, v) in &xs {
2138 assert_eq!(map.get(&k), Some(&v));
2143 fn test_size_hint() {
2144 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2146 let map: HashMap<_, _> = xs.iter().cloned().collect();
2148 let mut iter = map.iter();
2150 for _ in iter.by_ref().take(3) {}
2152 assert_eq!(iter.size_hint(), (3, Some(3)));
2156 fn test_iter_len() {
2157 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2159 let map: HashMap<_, _> = xs.iter().cloned().collect();
2161 let mut iter = map.iter();
2163 for _ in iter.by_ref().take(3) {}
2165 assert_eq!(iter.len(), 3);
2169 fn test_mut_size_hint() {
2170 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2172 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2174 let mut iter = map.iter_mut();
2176 for _ in iter.by_ref().take(3) {}
2178 assert_eq!(iter.size_hint(), (3, Some(3)));
2182 fn test_iter_mut_len() {
2183 let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2185 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2187 let mut iter = map.iter_mut();
2189 for _ in iter.by_ref().take(3) {}
2191 assert_eq!(iter.len(), 3);
2196 let mut map = HashMap::new();
2202 assert_eq!(map[2], 1);
2207 fn test_index_nonexistent() {
2208 let mut map = HashMap::new();
2219 let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
2221 let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2223 // Existing key (insert)
2224 match map.entry(1) {
2225 Vacant(_) => unreachable!(),
2226 Occupied(mut view) => {
2227 assert_eq!(view.get(), &10);
2228 assert_eq!(view.insert(100), 10);
2231 assert_eq!(map.get(&1).unwrap(), &100);
2232 assert_eq!(map.len(), 6);
2235 // Existing key (update)
2236 match map.entry(2) {
2237 Vacant(_) => unreachable!(),
2238 Occupied(mut view) => {
2239 let v = view.get_mut();
2240 let new_v = (*v) * 10;
2244 assert_eq!(map.get(&2).unwrap(), &200);
2245 assert_eq!(map.len(), 6);
2247 // Existing key (take)
2248 match map.entry(3) {
2249 Vacant(_) => unreachable!(),
2251 assert_eq!(view.remove(), 30);
2254 assert_eq!(map.get(&3), None);
2255 assert_eq!(map.len(), 5);
2258 // Inexistent key (insert)
2259 match map.entry(10) {
2260 Occupied(_) => unreachable!(),
2262 assert_eq!(*view.insert(1000), 1000);
2265 assert_eq!(map.get(&10).unwrap(), &1000);
2266 assert_eq!(map.len(), 6);
2270 fn test_entry_take_doesnt_corrupt() {
2271 #![allow(deprecated)] //rand
2273 fn check(m: &HashMap<isize, ()>) {
2275 assert!(m.contains_key(k),
2276 "{} is in keys() but not in the map?", k);
2280 let mut m = HashMap::new();
2281 let mut rng = weak_rng();
2283 // Populate the map with some items.
2285 let x = rng.gen_range(-10, 10);
2290 let x = rng.gen_range(-10, 10);
2294 println!("{}: remove {}", i, x);