]> git.lizzy.rs Git - rust.git/blob - src/libstd/collections/hash/map.rs
Auto merge of #30188 - tshepang:lookup_addr-example, r=alexcrichton
[rust.git] / src / libstd / collections / hash / map.rs
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.
4 //
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.
10
11 use self::Entry::*;
12 use self::SearchResult::*;
13 use self::VacantEntryState::*;
14
15 use borrow::Borrow;
16 use clone::Clone;
17 use cmp::{max, Eq, PartialEq};
18 use default::Default;
19 use fmt::{self, Debug};
20 use hash::{Hash, SipHasher};
21 use iter::{self, Iterator, ExactSizeIterator, IntoIterator, FromIterator, Extend, Map};
22 use marker::Sized;
23 use mem::{self, replace};
24 use ops::{Deref, FnMut, FnOnce, Index};
25 use option::Option::{self, Some, None};
26 use rand::{self, Rng};
27 use result::Result;
28
29 use super::table::{
30     self,
31     Bucket,
32     EmptyBucket,
33     FullBucket,
34     FullBucketImm,
35     FullBucketMut,
36     RawTable,
37     SafeHash
38 };
39 use super::table::BucketState::{
40     Empty,
41     Full,
42 };
43 use super::state::HashState;
44
45 const INITIAL_LOG2_CAP: usize = 5;
46 const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5
47
48 /// The default behavior of HashMap implements a load factor of 90.9%.
49 /// This behavior is characterized by the following condition:
50 ///
51 /// - if size > 0.909 * capacity: grow the map
52 #[derive(Clone)]
53 struct DefaultResizePolicy;
54
55 impl DefaultResizePolicy {
56     fn new() -> DefaultResizePolicy {
57         DefaultResizePolicy
58     }
59
60     #[inline]
61     fn min_capacity(&self, usable_size: usize) -> usize {
62         // Here, we are rephrasing the logic by specifying the lower limit
63         // on capacity:
64         //
65         // - if `cap < size * 1.1`: grow the map
66         usable_size * 11 / 10
67     }
68
69     /// An inverse of `min_capacity`, approximately.
70     #[inline]
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
77         // division.
78         //
79         // This doesn't have to be checked for overflow since allocation size
80         // in bytes will overflow earlier than multiplication by 10.
81         cap * 10 / 11
82     }
83 }
84
85 #[test]
86 fn test_resize_policy() {
87     let rp = DefaultResizePolicy;
88     for n in 0..1000 {
89         assert!(rp.min_capacity(rp.usable_capacity(n)) <= n);
90         assert!(rp.usable_capacity(rp.min_capacity(n)) <= n);
91     }
92 }
93
94 // The main performance trick in this hashmap is called Robin Hood Hashing.
95 // It gains its excellent performance from one essential operation:
96 //
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.
100 //
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
103 // conservative.
104 //
105 // > Why a load factor of approximately 90%?
106 //
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.
116 //
117 // > Wait, what? Where did you get 1-α^k from?
118 //
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.
123 //
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.
132 //
133 // ## Future Improvements (FIXME!)
134 //
135 // Allow the load factor to be changed dynamically and/or at initialization.
136 //
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.
140 //
141 // ## Future Optimizations (FIXME!)
142 //
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.
152 //
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).
156 //
157 // Shrinking the table could simply reallocate in place after moving buckets
158 // to the first half.
159 //
160 // The growth algorithm (fragment of the Proof of Correctness)
161 // --------------------
162 //
163 // The growth algorithm is basically a fast path of the naive reinsertion-
164 // during-resize algorithm. Other paths should never be taken.
165 //
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.
177 //
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:
189 //
190 // [yy_xxx_xxxx_xxx|xx_yyy_yyyy_yyy]
191 //
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
196 // empty.
197 //
198 // Theorem: A linear robin hood reinsertion from the first ideal element
199 // produces identical results to a linear naive reinsertion from the same
200 // element.
201 //
202 // FIXME(Gankro, pczarn): review the proof and put it all in a separate README.md
203
204 /// A hash map implementation which uses linear probing with Robin
205 /// Hood bucket stealing.
206 ///
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.
212 ///
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
216 /// property holds:
217 ///
218 /// ```text
219 /// k1 == k2 -> hash(k1) == hash(k2)
220 /// ```
221 ///
222 /// In other words, if two keys are equal, their hashes must be equal.
223 ///
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.
228 ///
229 /// Relevant papers/articles:
230 ///
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/)
236 ///
237 /// # Examples
238 ///
239 /// ```
240 /// use std::collections::HashMap;
241 ///
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();
245 ///
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.");
251 ///
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());
256 /// }
257 ///
258 /// // oops, this review has a lot of spelling mistakes, let's delete it.
259 /// book_reviews.remove("The Adventures of Sherlock Holmes");
260 ///
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)
267 ///     }
268 /// }
269 ///
270 /// // iterate over everything.
271 /// for (book, review) in &book_reviews {
272 ///     println!("{}: \"{}\"", book, review);
273 /// }
274 /// ```
275 ///
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`.
278 ///
279 /// ```
280 /// use std::collections::HashMap;
281 ///
282 /// #[derive(Hash, Eq, PartialEq, Debug)]
283 /// struct Viking {
284 ///     name: String,
285 ///     country: String,
286 /// }
287 ///
288 /// impl Viking {
289 ///     /// Create a new Viking.
290 ///     fn new(name: &str, country: &str) -> Viking {
291 ///         Viking { name: name.to_string(), country: country.to_string() }
292 ///     }
293 /// }
294 ///
295 /// // Use a HashMap to store the vikings' health points.
296 /// let mut vikings = HashMap::new();
297 ///
298 /// vikings.insert(Viking::new("Einar", "Norway"), 25);
299 /// vikings.insert(Viking::new("Olaf", "Denmark"), 24);
300 /// vikings.insert(Viking::new("Harald", "Iceland"), 12);
301 ///
302 /// // Use derived implementation to print the status of the vikings.
303 /// for (viking, health) in &vikings {
304 ///     println!("{:?} has {} hp", viking, health);
305 /// }
306 /// ```
307 #[derive(Clone)]
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.
311     hash_state: S,
312
313     table: RawTable<K, V>,
314
315     resize_policy: DefaultResizePolicy,
316 }
317
318 /// Search for a pre-hashed key.
319 fn search_hashed<K, V, M, F>(table: M,
320                              hash: SafeHash,
321                              mut is_match: F)
322                              -> SearchResult<K, V, M> where
323     M: Deref<Target=RawTable<K, V>>,
324     F: FnMut(&K) -> bool,
325 {
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);
331     }
332
333     let size = table.size();
334     let mut probe = Bucket::new(table, hash);
335     let ib = probe.index();
336
337     while probe.index() != ib + size {
338         let full = match probe.peek() {
339             Empty(b) => return TableRef(b.into_table()), // hit an empty bucket
340             Full(b) => b
341         };
342
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());
347         }
348
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);
354             }
355         }
356
357         probe = full.next();
358     }
359
360     TableRef(probe.into_table())
361 }
362
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() {
366         Some(b) => b,
367         None => return (retkey, retval)
368     };
369
370     while gap.full().distance() != 0 {
371         gap = match gap.shift() {
372             Some(b) => b,
373             None => break
374         };
375     }
376
377     // Now we've done all our shifting. Return the value we grabbed earlier.
378     (retkey, retval)
379 }
380
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.
384 ///
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>,
387                         mut ib: usize,
388                         mut hash: SafeHash,
389                         mut k: K,
390                         mut v: V)
391                         -> &'a mut V {
392     let starting_index = bucket.index();
393     let size = {
394         let table = bucket.table(); // FIXME "lifetime too short".
395         table.size()
396     };
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();
401
402     loop {
403         let (old_hash, old_key, old_val) = bucket.replace(hash, k, v);
404         loop {
405             let probe = bucket.next();
406             assert!(probe.index() != idx_end);
407
408             let full_bucket = match probe.peek() {
409                 Empty(bucket) => {
410                     // Found a hole!
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)
415                                .peek()
416                                .expect_full()
417                                .into_mut_refs()
418                                .1;
419                 },
420                 Full(bucket) => bucket
421             };
422
423             let probe_ib = full_bucket.index() - full_bucket.distance();
424
425             bucket = full_bucket;
426
427             // Robin hood! Steal the spot.
428             if ib < probe_ib {
429                 ib = probe_ib;
430                 hash = old_hash;
431                 k = old_key;
432                 v = old_val;
433                 break;
434             }
435         }
436     }
437 }
438
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>),
444
445     // There was no such entry. The reference is given back:
446     TableRef(M)
447 }
448
449 impl<K, V, M> SearchResult<K, V, M> {
450     fn into_option(self) -> Option<FullBucket<K, V, M>> {
451         match self {
452             FoundExisting(bucket) => Some(bucket),
453             TableRef(_) => None
454         }
455     }
456 }
457
458 impl<K, V, S> HashMap<K, V, S>
459     where K: Eq + Hash, S: HashState
460 {
461     fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash where X: Hash {
462         table::make_hash(&self.hash_state, x)
463     }
464
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
467     /// search_hashed.
468     fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> Option<FullBucketImm<'a, K, V>>
469         where K: Borrow<Q>, Q: Eq + Hash
470     {
471         let hash = self.make_hash(q);
472         search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
473             .into_option()
474     }
475
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
478     {
479         let hash = self.make_hash(q);
480         search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
481             .into_option()
482     }
483
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();
489
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() {
494                 Empty(empty) => {
495                     empty.put(hash, k, v);
496                     return;
497                 }
498                 Full(b) => b.into_bucket()
499             };
500             buckets.next();
501         }
502         panic!("Internal HashMap error: Out of space.");
503     }
504 }
505
506 impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
507     /// Creates an empty HashMap.
508     ///
509     /// # Examples
510     ///
511     /// ```
512     /// use std::collections::HashMap;
513     /// let mut map: HashMap<&str, isize> = HashMap::new();
514     /// ```
515     #[inline]
516     #[stable(feature = "rust1", since = "1.0.0")]
517     pub fn new() -> HashMap<K, V, RandomState> {
518         Default::default()
519     }
520
521     /// Creates an empty hash map with the given initial capacity.
522     ///
523     /// # Examples
524     ///
525     /// ```
526     /// use std::collections::HashMap;
527     /// let mut map: HashMap<&str, isize> = HashMap::with_capacity(10);
528     /// ```
529     #[inline]
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())
533     }
534 }
535
536 impl<K, V, S> HashMap<K, V, S>
537     where K: Eq + Hash, S: HashState
538 {
539     /// Creates an empty hashmap which will use the given hasher to hash keys.
540     ///
541     /// The created map has the default initial capacity.
542     ///
543     /// # Examples
544     ///
545     /// ```
546     /// #![feature(hashmap_hasher)]
547     ///
548     /// use std::collections::HashMap;
549     /// use std::collections::hash_map::RandomState;
550     ///
551     /// let s = RandomState::new();
552     /// let mut map = HashMap::with_hash_state(s);
553     /// map.insert(1, 2);
554     /// ```
555     #[inline]
556     #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
557                issue = "27713")]
558     pub fn with_hash_state(hash_state: S) -> HashMap<K, V, S> {
559         HashMap {
560             hash_state:    hash_state,
561             resize_policy: DefaultResizePolicy::new(),
562             table:         RawTable::new(0),
563         }
564     }
565
566     /// Creates an empty HashMap with space for at least `capacity`
567     /// elements, using `hasher` to hash the keys.
568     ///
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.
573     ///
574     /// # Examples
575     ///
576     /// ```
577     /// #![feature(hashmap_hasher)]
578     ///
579     /// use std::collections::HashMap;
580     /// use std::collections::hash_map::RandomState;
581     ///
582     /// let s = RandomState::new();
583     /// let mut map = HashMap::with_capacity_and_hash_state(10, s);
584     /// map.insert(1, 2);
585     /// ```
586     #[inline]
587     #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
588                issue = "27713")]
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");
595         HashMap {
596             hash_state:    hash_state,
597             resize_policy: resize_policy,
598             table:         RawTable::new(internal_cap),
599         }
600     }
601
602     /// Returns the number of elements the map can hold without reallocating.
603     ///
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.
606     ///
607     /// # Examples
608     ///
609     /// ```
610     /// use std::collections::HashMap;
611     /// let map: HashMap<isize, isize> = HashMap::with_capacity(100);
612     /// assert!(map.capacity() >= 100);
613     /// ```
614     #[inline]
615     #[stable(feature = "rust1", since = "1.0.0")]
616     pub fn capacity(&self) -> usize {
617         self.resize_policy.usable_capacity(self.table.capacity())
618     }
619
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.
623     ///
624     /// # Panics
625     ///
626     /// Panics if the new allocation size overflows `usize`.
627     ///
628     /// # Examples
629     ///
630     /// ```
631     /// use std::collections::HashMap;
632     /// let mut map: HashMap<&str, isize> = HashMap::new();
633     /// map.reserve(10);
634     /// ```
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);
639
640         // An invalid value shouldn't make us run out of space. This includes
641         // an overflow check.
642         assert!(new_size <= min_cap);
643
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);
647         }
648     }
649
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);
657
658         let mut old_table = replace(&mut self.table, RawTable::new(new_capacity));
659         let old_size = old_table.size();
660
661         if old_table.capacity() == 0 || old_table.size() == 0 {
662             return;
663         }
664
665         // Grow the table.
666         // Specialization of the other branch.
667         let mut bucket = Bucket::first(&mut old_table);
668
669         // "So a few of the first shall be last: for many be called,
670         // but few chosen."
671         //
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.
678         loop {
679             bucket = match bucket.peek() {
680                 Full(full) => {
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();
685                         break;
686                     }
687                     // Leaving this bucket in the last cluster for later.
688                     full.into_bucket()
689                 }
690                 Empty(b) => {
691                     // Encountered a hole between clusters.
692                     b.into_bucket()
693                 }
694             };
695             bucket.next();
696         }
697
698         // This is how the buckets might be laid out in memory:
699         // ($ marks an initialized bucket)
700         //  ________________
701         // |$$$_$$$$$$_$$$$$|
702         //
703         // But we've skipped the entire initial cluster of buckets
704         // and will continue iteration in this order:
705         //  ________________
706         //     |$$$$$$_$$$$$
707         //                  ^ wrap around once end is reached
708         //  ________________
709         //  $$$_____________|
710         //    ^ exit once table.size == 0
711         loop {
712             bucket = match bucket.peek() {
713                 Full(bucket) => {
714                     let h = bucket.hash();
715                     let (b, k, v) = bucket.take();
716                     self.insert_hashed_ordered(h, k, v);
717                     {
718                         let t = b.table(); // FIXME "lifetime too short".
719                         if t.size() == 0 { break }
720                     };
721                     b.into_bucket()
722                 }
723                 Empty(b) => b.into_bucket()
724             };
725             bucket.next();
726         }
727
728         assert_eq!(self.table.size(), old_size);
729     }
730
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.
734     ///
735     /// # Examples
736     ///
737     /// ```
738     /// use std::collections::HashMap;
739     ///
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);
746     /// ```
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);
751
752         // An invalid value shouldn't make us run out of space.
753         debug_assert!(self.len() <= min_capacity);
754
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();
758
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);
762             }
763
764             debug_assert_eq!(self.table.size(), old_size);
765         }
766     }
767
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.
771     ///
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, |_, _, _, _| ())
776     }
777
778     fn insert_or_replace_with<'a, F>(&'a mut self,
779                                      hash: SafeHash,
780                                      k: K,
781                                      v: V,
782                                      mut found_existing: F)
783                                      -> &'a mut V where
784         F: FnMut(&mut K, &mut V, K, V),
785     {
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();
790
791         loop {
792             let mut bucket = match probe.peek() {
793                 Empty(bucket) => {
794                     // Found a hole!
795                     return bucket.put(hash, k, v).into_mut_refs().1;
796                 }
797                 Full(bucket) => bucket
798             };
799
800             // hash matches?
801             if bucket.hash() == hash {
802                 // key matches?
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);
808                     return bucket_v;
809                 }
810             }
811
812             let robin_ib = bucket.index() as isize - bucket.distance() as isize;
813
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);
817             }
818
819             probe = bucket.next();
820             assert!(probe.index() != ib + size + 1);
821         }
822     }
823
824     /// An iterator visiting all keys in arbitrary order.
825     /// Iterator element type is `&'a K`.
826     ///
827     /// # Examples
828     ///
829     /// ```
830     /// use std::collections::HashMap;
831     ///
832     /// let mut map = HashMap::new();
833     /// map.insert("a", 1);
834     /// map.insert("b", 2);
835     /// map.insert("c", 3);
836     ///
837     /// for key in map.keys() {
838     ///     println!("{}", key);
839     /// }
840     /// ```
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
845
846         Keys { inner: self.iter().map(first) }
847     }
848
849     /// An iterator visiting all values in arbitrary order.
850     /// Iterator element type is `&'a V`.
851     ///
852     /// # Examples
853     ///
854     /// ```
855     /// use std::collections::HashMap;
856     ///
857     /// let mut map = HashMap::new();
858     /// map.insert("a", 1);
859     /// map.insert("b", 2);
860     /// map.insert("c", 3);
861     ///
862     /// for val in map.values() {
863     ///     println!("{}", val);
864     /// }
865     /// ```
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
870
871         Values { inner: self.iter().map(second) }
872     }
873
874     /// An iterator visiting all key-value pairs in arbitrary order.
875     /// Iterator element type is `(&'a K, &'a V)`.
876     ///
877     /// # Examples
878     ///
879     /// ```
880     /// use std::collections::HashMap;
881     ///
882     /// let mut map = HashMap::new();
883     /// map.insert("a", 1);
884     /// map.insert("b", 2);
885     /// map.insert("c", 3);
886     ///
887     /// for (key, val) in map.iter() {
888     ///     println!("key: {} val: {}", key, val);
889     /// }
890     /// ```
891     #[stable(feature = "rust1", since = "1.0.0")]
892     pub fn iter(&self) -> Iter<K, V> {
893         Iter { inner: self.table.iter() }
894     }
895
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)`.
899     ///
900     /// # Examples
901     ///
902     /// ```
903     /// use std::collections::HashMap;
904     ///
905     /// let mut map = HashMap::new();
906     /// map.insert("a", 1);
907     /// map.insert("b", 2);
908     /// map.insert("c", 3);
909     ///
910     /// // Update all values
911     /// for (_, val) in map.iter_mut() {
912     ///     *val *= 2;
913     /// }
914     ///
915     /// for (key, val) in &map {
916     ///     println!("key: {} val: {}", key, val);
917     /// }
918     /// ```
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() }
922     }
923
924     /// Gets the given key's corresponding entry in the map for in-place manipulation.
925     ///
926     /// # Examples
927     ///
928     /// ```
929     /// use std::collections::HashMap;
930     ///
931     /// let mut letters = HashMap::new();
932     ///
933     /// for ch in "a short treatise on fungi".chars() {
934     ///     let counter = letters.entry(ch).or_insert(0);
935     ///     *counter += 1;
936     /// }
937     ///
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);
942     /// ```
943     #[stable(feature = "rust1", since = "1.0.0")]
944     pub fn entry(&mut self, key: K) -> Entry<K, V> {
945         // Gotta resize now.
946         self.reserve(1);
947
948         let hash = self.make_hash(&key);
949         search_entry_hashed(&mut self.table, hash, key)
950     }
951
952     /// Returns the number of elements in the map.
953     ///
954     /// # Examples
955     ///
956     /// ```
957     /// use std::collections::HashMap;
958     ///
959     /// let mut a = HashMap::new();
960     /// assert_eq!(a.len(), 0);
961     /// a.insert(1, "a");
962     /// assert_eq!(a.len(), 1);
963     /// ```
964     #[stable(feature = "rust1", since = "1.0.0")]
965     pub fn len(&self) -> usize { self.table.size() }
966
967     /// Returns true if the map contains no elements.
968     ///
969     /// # Examples
970     ///
971     /// ```
972     /// use std::collections::HashMap;
973     ///
974     /// let mut a = HashMap::new();
975     /// assert!(a.is_empty());
976     /// a.insert(1, "a");
977     /// assert!(!a.is_empty());
978     /// ```
979     #[inline]
980     #[stable(feature = "rust1", since = "1.0.0")]
981     pub fn is_empty(&self) -> bool { self.len() == 0 }
982
983     /// Clears the map, returning all key-value pairs as an iterator. Keeps the
984     /// allocated memory for reuse.
985     ///
986     /// # Examples
987     ///
988     /// ```
989     /// use std::collections::HashMap;
990     ///
991     /// let mut a = HashMap::new();
992     /// a.insert(1, "a");
993     /// a.insert(2, "b");
994     ///
995     /// for (k, v) in a.drain().take(1) {
996     ///     assert!(k == 1 || k == 2);
997     ///     assert!(v == "a" || v == "b");
998     /// }
999     ///
1000     /// assert!(a.is_empty());
1001     /// ```
1002     #[inline]
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
1007
1008         Drain {
1009             inner: self.table.drain().map(last_two),
1010         }
1011     }
1012
1013     /// Clears the map, removing all key-value pairs. Keeps the allocated memory
1014     /// for reuse.
1015     ///
1016     /// # Examples
1017     ///
1018     /// ```
1019     /// use std::collections::HashMap;
1020     ///
1021     /// let mut a = HashMap::new();
1022     /// a.insert(1, "a");
1023     /// a.clear();
1024     /// assert!(a.is_empty());
1025     /// ```
1026     #[stable(feature = "rust1", since = "1.0.0")]
1027     #[inline]
1028     pub fn clear(&mut self) {
1029         self.drain();
1030     }
1031
1032     /// Returns a reference to the value corresponding to the key.
1033     ///
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
1036     /// the key type.
1037     ///
1038     /// # Examples
1039     ///
1040     /// ```
1041     /// use std::collections::HashMap;
1042     ///
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);
1047     /// ```
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
1051     {
1052         self.search(k).map(|bucket| bucket.into_refs().1)
1053     }
1054
1055     /// Returns true if the map contains a value for the specified key.
1056     ///
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
1059     /// the key type.
1060     ///
1061     /// # Examples
1062     ///
1063     /// ```
1064     /// use std::collections::HashMap;
1065     ///
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);
1070     /// ```
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
1074     {
1075         self.search(k).is_some()
1076     }
1077
1078     /// Returns a mutable reference to the value corresponding to the key.
1079     ///
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
1082     /// the key type.
1083     ///
1084     /// # Examples
1085     ///
1086     /// ```
1087     /// use std::collections::HashMap;
1088     ///
1089     /// let mut map = HashMap::new();
1090     /// map.insert(1, "a");
1091     /// if let Some(x) = map.get_mut(&1) {
1092     ///     *x = "b";
1093     /// }
1094     /// assert_eq!(map[&1], "b");
1095     /// ```
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
1099     {
1100         self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
1101     }
1102
1103     /// Inserts a key-value pair into the map.
1104     ///
1105     /// If the map did not have this key present, `None` is returned.
1106     ///
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.
1110     ///
1111     /// [module-level documentation]: index.html#insert-and-complex-keys
1112     ///
1113     /// # Examples
1114     ///
1115     /// ```
1116     /// use std::collections::HashMap;
1117     ///
1118     /// let mut map = HashMap::new();
1119     /// assert_eq!(map.insert(37, "a"), None);
1120     /// assert_eq!(map.is_empty(), false);
1121     ///
1122     /// map.insert(37, "b");
1123     /// assert_eq!(map.insert(37, "c"), Some("b"));
1124     /// assert_eq!(map[&37], "c");
1125     /// ```
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);
1129         self.reserve(1);
1130
1131         let mut retval = None;
1132         self.insert_or_replace_with(hash, k, v, |_, val_ref, _, val| {
1133             retval = Some(replace(val_ref, val));
1134         });
1135         retval
1136     }
1137
1138     /// Removes a key from the map, returning the value at the key if the key
1139     /// was previously in the map.
1140     ///
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
1143     /// the key type.
1144     ///
1145     /// # Examples
1146     ///
1147     /// ```
1148     /// use std::collections::HashMap;
1149     ///
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);
1154     /// ```
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
1158     {
1159         if self.table.size() == 0 {
1160             return None
1161         }
1162
1163         self.search_mut(k).map(|bucket| pop_internal(bucket).1)
1164     }
1165 }
1166
1167 fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: K)
1168         -> Entry<'a, K, V>
1169 {
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();
1174
1175     loop {
1176         let bucket = match probe.peek() {
1177             Empty(bucket) => {
1178                 // Found a hole!
1179                 return Vacant(VacantEntry {
1180                     hash: hash,
1181                     key: k,
1182                     elem: NoElem(bucket),
1183                 });
1184             },
1185             Full(bucket) => bucket
1186         };
1187
1188         // hash matches?
1189         if bucket.hash() == hash {
1190             // key matches?
1191             if k == *bucket.read().0 {
1192                 return Occupied(OccupiedEntry{
1193                     elem: bucket,
1194                 });
1195             }
1196         }
1197
1198         let robin_ib = bucket.index() as isize - bucket.distance() as isize;
1199
1200         if (ib as isize) < robin_ib {
1201             // Found a luckier bucket than me. Better steal his spot.
1202             return Vacant(VacantEntry {
1203                 hash: hash,
1204                 key: k,
1205                 elem: NeqElem(bucket, robin_ib as usize),
1206             });
1207         }
1208
1209         probe = bucket.next();
1210         assert!(probe.index() != ib + size + 1);
1211     }
1212 }
1213
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
1217 {
1218     fn eq(&self, other: &HashMap<K, V, S>) -> bool {
1219         if self.len() != other.len() { return false; }
1220
1221         self.iter().all(|(key, value)|
1222             other.get(key).map_or(false, |v| *value == *v)
1223         )
1224     }
1225 }
1226
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
1230 {}
1231
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
1235 {
1236     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1237         f.debug_map().entries(self.iter()).finish()
1238     }
1239 }
1240
1241 #[stable(feature = "rust1", since = "1.0.0")]
1242 impl<K, V, S> Default for HashMap<K, V, S>
1243     where K: Eq + Hash,
1244           S: HashState + Default,
1245 {
1246     fn default() -> HashMap<K, V, S> {
1247         HashMap::with_hash_state(Default::default())
1248     }
1249 }
1250
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>,
1254           Q: Eq + Hash,
1255           S: HashState,
1256 {
1257     type Output = V;
1258
1259     #[inline]
1260     fn index(&self, index: &Q) -> &V {
1261         self.get(index).expect("no entry found for key")
1262     }
1263 }
1264
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>
1269 }
1270
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> {
1275         Iter {
1276             inner: self.inner.clone()
1277         }
1278     }
1279 }
1280
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>
1285 }
1286
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)>
1291 }
1292
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>
1297 }
1298
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> {
1303         Keys {
1304             inner: self.inner.clone()
1305         }
1306     }
1307 }
1308
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>
1313 }
1314
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> {
1319         Values {
1320             inner: self.inner.clone()
1321         }
1322     }
1323 }
1324
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)>
1329 }
1330
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>>,
1335 }
1336
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> {
1340     hash: SafeHash,
1341     key: K,
1342     elem: VacantEntryState<K, V, &'a mut RawTable<K, V>>,
1343 }
1344
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>),
1351
1352     /// A vacant Entry.
1353     #[stable(feature = "rust1", since = "1.0.0")]
1354     Vacant(VacantEntry<'a, K, V>),
1355 }
1356
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>),
1364 }
1365
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
1369 {
1370     type Item = (&'a K, &'a V);
1371     type IntoIter = Iter<'a, K, V>;
1372
1373     fn into_iter(self) -> Iter<'a, K, V> {
1374         self.iter()
1375     }
1376 }
1377
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
1381 {
1382     type Item = (&'a K, &'a mut V);
1383     type IntoIter = IterMut<'a, K, V>;
1384
1385     fn into_iter(mut self) -> IterMut<'a, K, V> {
1386         self.iter_mut()
1387     }
1388 }
1389
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
1393 {
1394     type Item = (K, V);
1395     type IntoIter = IntoIter<K, V>;
1396
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
1399     /// calling this.
1400     ///
1401     /// # Examples
1402     ///
1403     /// ```
1404     /// use std::collections::HashMap;
1405     ///
1406     /// let mut map = HashMap::new();
1407     /// map.insert("a", 1);
1408     /// map.insert("b", 2);
1409     /// map.insert("c", 3);
1410     ///
1411     /// // Not possible with .iter()
1412     /// let vec: Vec<(&str, isize)> = map.into_iter().collect();
1413     /// ```
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;
1417
1418         IntoIter {
1419             inner: self.table.into_iter().map(last_two)
1420         }
1421     }
1422 }
1423
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);
1427
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() }
1430 }
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() }
1434 }
1435
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);
1439
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() }
1442 }
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() }
1446 }
1447
1448 #[stable(feature = "rust1", since = "1.0.0")]
1449 impl<K, V> Iterator for IntoIter<K, V> {
1450     type Item = (K, V);
1451
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() }
1454 }
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() }
1458 }
1459
1460 #[stable(feature = "rust1", since = "1.0.0")]
1461 impl<'a, K, V> Iterator for Keys<'a, K, V> {
1462     type Item = &'a K;
1463
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() }
1466 }
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() }
1470 }
1471
1472 #[stable(feature = "rust1", since = "1.0.0")]
1473 impl<'a, K, V> Iterator for Values<'a, K, V> {
1474     type Item = &'a V;
1475
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() }
1478 }
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() }
1482 }
1483
1484 #[stable(feature = "rust1", since = "1.0.0")]
1485 impl<'a, K, V> Iterator for Drain<'a, K, V> {
1486     type Item = (K, V);
1487
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() }
1490 }
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() }
1494 }
1495
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 {
1501         match self {
1502             Occupied(entry) => entry.into_mut(),
1503             Vacant(entry) => entry.insert(default),
1504         }
1505     }
1506
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 {
1511         match self {
1512             Occupied(entry) => entry.into_mut(),
1513             Vacant(entry) => entry.insert(default()),
1514         }
1515     }
1516 }
1517
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 {
1522         self.elem.read().1
1523     }
1524
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
1529     }
1530
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
1536     }
1537
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);
1543         value
1544     }
1545
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
1550     }
1551 }
1552
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 {
1558         match self.elem {
1559             NeqElem(bucket, ib) => {
1560                 robin_hood(bucket, ib, self.hash, self.key, value)
1561             }
1562             NoElem(bucket) => {
1563                 bucket.put(self.hash, self.key, value).into_mut_refs().1
1564             }
1565         }
1566     }
1567 }
1568
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
1572 {
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());
1578         map.extend(iter);
1579         map
1580     }
1581 }
1582
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
1586 {
1587     fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) {
1588         for (k, v) in iter {
1589             self.insert(k, v);
1590         }
1591     }
1592 }
1593
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
1597 {
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)));
1600     }
1601 }
1602
1603 /// `RandomState` is the default state for `HashMap` types.
1604 ///
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.
1608 #[derive(Clone)]
1609 #[unstable(feature = "hashmap_hasher",
1610            reason = "hashing an hash maps may be altered",
1611            issue = "27713")]
1612 pub struct RandomState {
1613     k0: u64,
1614     k1: u64,
1615 }
1616
1617 #[unstable(feature = "hashmap_hasher",
1618            reason = "hashing an hash maps may be altered",
1619            issue = "27713")]
1620 impl RandomState {
1621     /// Constructs a new `RandomState` that is initialized with random keys.
1622     #[inline]
1623     #[allow(deprecated)] // rand
1624     pub fn new() -> RandomState {
1625         let mut r = rand::thread_rng();
1626         RandomState { k0: r.gen(), k1: r.gen() }
1627     }
1628 }
1629
1630 #[unstable(feature = "hashmap_hasher",
1631            reason = "hashing an hash maps may be altered",
1632            issue = "27713")]
1633 impl HashState for RandomState {
1634     type Hasher = SipHasher;
1635     #[inline]
1636     fn hasher(&self) -> SipHasher {
1637         SipHasher::new_with_keys(self.k0, self.k1)
1638     }
1639 }
1640
1641 #[stable(feature = "rust1", since = "1.0.0")]
1642 impl Default for RandomState {
1643     #[inline]
1644     fn default() -> RandomState {
1645         RandomState::new()
1646     }
1647 }
1648
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
1651 {
1652     type Key = K;
1653
1654     fn get(&self, key: &Q) -> Option<&K> {
1655         self.search(key).map(|bucket| bucket.into_refs().0)
1656     }
1657
1658     fn take(&mut self, key: &Q) -> Option<K> {
1659         if self.table.size() == 0 {
1660             return None
1661         }
1662
1663         self.search_mut(key).map(|bucket| pop_internal(bucket).0)
1664     }
1665
1666     fn replace(&mut self, key: K) -> Option<K> {
1667         let hash = self.make_hash(&key);
1668         self.reserve(1);
1669
1670         let mut retkey = None;
1671         self.insert_or_replace_with(hash, key, (), |key_ref, _, key, _| {
1672             retkey = Some(replace(key_ref, key));
1673         });
1674         retkey
1675     }
1676 }
1677
1678 #[cfg(test)]
1679 mod test_map {
1680     use prelude::v1::*;
1681
1682     use super::HashMap;
1683     use super::Entry::{Occupied, Vacant};
1684     use iter::range_inclusive;
1685     use cell::RefCell;
1686     use rand::{thread_rng, Rng};
1687
1688     #[test]
1689     fn test_create_capacity_zero() {
1690         let mut m = HashMap::with_capacity(0);
1691
1692         assert!(m.insert(1, 1).is_none());
1693
1694         assert!(m.contains_key(&1));
1695         assert!(!m.contains_key(&0));
1696     }
1697
1698     #[test]
1699     fn test_insert() {
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);
1708     }
1709
1710     thread_local! { static DROP_VECTOR: RefCell<Vec<isize>> = RefCell::new(Vec::new()) }
1711
1712     #[derive(Hash, PartialEq, Eq)]
1713     struct Dropable {
1714         k: usize
1715     }
1716
1717     impl Dropable {
1718         fn new(k: usize) -> Dropable {
1719             DROP_VECTOR.with(|slot| {
1720                 slot.borrow_mut()[k] += 1;
1721             });
1722
1723             Dropable { k: k }
1724         }
1725     }
1726
1727     impl Drop for Dropable {
1728         fn drop(&mut self) {
1729             DROP_VECTOR.with(|slot| {
1730                 slot.borrow_mut()[self.k] -= 1;
1731             });
1732         }
1733     }
1734
1735     impl Clone for Dropable {
1736         fn clone(&self) -> Dropable {
1737             Dropable::new(self.k)
1738         }
1739     }
1740
1741     #[test]
1742     fn test_drops() {
1743         DROP_VECTOR.with(|slot| {
1744             *slot.borrow_mut() = vec![0; 200];
1745         });
1746
1747         {
1748             let mut m = HashMap::new();
1749
1750             DROP_VECTOR.with(|v| {
1751                 for i in 0..200 {
1752                     assert_eq!(v.borrow()[i], 0);
1753                 }
1754             });
1755
1756             for i in 0..100 {
1757                 let d1 = Dropable::new(i);
1758                 let d2 = Dropable::new(i+100);
1759                 m.insert(d1, d2);
1760             }
1761
1762             DROP_VECTOR.with(|v| {
1763                 for i in 0..200 {
1764                     assert_eq!(v.borrow()[i], 1);
1765                 }
1766             });
1767
1768             for i in 0..50 {
1769                 let k = Dropable::new(i);
1770                 let v = m.remove(&k);
1771
1772                 assert!(v.is_some());
1773
1774                 DROP_VECTOR.with(|v| {
1775                     assert_eq!(v.borrow()[i], 1);
1776                     assert_eq!(v.borrow()[i+100], 1);
1777                 });
1778             }
1779
1780             DROP_VECTOR.with(|v| {
1781                 for i in 0..50 {
1782                     assert_eq!(v.borrow()[i], 0);
1783                     assert_eq!(v.borrow()[i+100], 0);
1784                 }
1785
1786                 for i in 50..100 {
1787                     assert_eq!(v.borrow()[i], 1);
1788                     assert_eq!(v.borrow()[i+100], 1);
1789                 }
1790             });
1791         }
1792
1793         DROP_VECTOR.with(|v| {
1794             for i in 0..200 {
1795                 assert_eq!(v.borrow()[i], 0);
1796             }
1797         });
1798     }
1799
1800     #[test]
1801     fn test_move_iter_drops() {
1802         DROP_VECTOR.with(|v| {
1803             *v.borrow_mut() = vec![0; 200];
1804         });
1805
1806         let hm = {
1807             let mut hm = HashMap::new();
1808
1809             DROP_VECTOR.with(|v| {
1810                 for i in 0..200 {
1811                     assert_eq!(v.borrow()[i], 0);
1812                 }
1813             });
1814
1815             for i in 0..100 {
1816                 let d1 = Dropable::new(i);
1817                 let d2 = Dropable::new(i+100);
1818                 hm.insert(d1, d2);
1819             }
1820
1821             DROP_VECTOR.with(|v| {
1822                 for i in 0..200 {
1823                     assert_eq!(v.borrow()[i], 1);
1824                 }
1825             });
1826
1827             hm
1828         };
1829
1830         // By the way, ensure that cloning doesn't screw up the dropping.
1831         drop(hm.clone());
1832
1833         {
1834             let mut half = hm.into_iter().take(50);
1835
1836             DROP_VECTOR.with(|v| {
1837                 for i in 0..200 {
1838                     assert_eq!(v.borrow()[i], 1);
1839                 }
1840             });
1841
1842             for _ in half.by_ref() {}
1843
1844             DROP_VECTOR.with(|v| {
1845                 let nk = (0..100).filter(|&i| {
1846                     v.borrow()[i] == 1
1847                 }).count();
1848
1849                 let nv = (0..100).filter(|&i| {
1850                     v.borrow()[i+100] == 1
1851                 }).count();
1852
1853                 assert_eq!(nk, 50);
1854                 assert_eq!(nv, 50);
1855             });
1856         };
1857
1858         DROP_VECTOR.with(|v| {
1859             for i in 0..200 {
1860                 assert_eq!(v.borrow()[i], 0);
1861             }
1862         });
1863     }
1864
1865     #[test]
1866     fn test_empty_pop() {
1867         let mut m: HashMap<isize, bool> = HashMap::new();
1868         assert_eq!(m.remove(&0), None);
1869     }
1870
1871     #[test]
1872     fn test_lots_of_insertions() {
1873         let mut m = HashMap::new();
1874
1875         // Try this a few times to make sure we never screw up the hashmap's
1876         // internal state.
1877         for _ in 0..10 {
1878             assert!(m.is_empty());
1879
1880             for i in range_inclusive(1, 1000) {
1881                 assert!(m.insert(i, i).is_none());
1882
1883                 for j in range_inclusive(1, i) {
1884                     let r = m.get(&j);
1885                     assert_eq!(r, Some(&j));
1886                 }
1887
1888                 for j in range_inclusive(i+1, 1000) {
1889                     let r = m.get(&j);
1890                     assert_eq!(r, None);
1891                 }
1892             }
1893
1894             for i in range_inclusive(1001, 2000) {
1895                 assert!(!m.contains_key(&i));
1896             }
1897
1898             // remove forwards
1899             for i in range_inclusive(1, 1000) {
1900                 assert!(m.remove(&i).is_some());
1901
1902                 for j in range_inclusive(1, i) {
1903                     assert!(!m.contains_key(&j));
1904                 }
1905
1906                 for j in range_inclusive(i+1, 1000) {
1907                     assert!(m.contains_key(&j));
1908                 }
1909             }
1910
1911             for i in range_inclusive(1, 1000) {
1912                 assert!(!m.contains_key(&i));
1913             }
1914
1915             for i in range_inclusive(1, 1000) {
1916                 assert!(m.insert(i, i).is_none());
1917             }
1918
1919             // remove backwards
1920             for i in (1..1001).rev() {
1921                 assert!(m.remove(&i).is_some());
1922
1923                 for j in range_inclusive(i, 1000) {
1924                     assert!(!m.contains_key(&j));
1925                 }
1926
1927                 for j in range_inclusive(1, i-1) {
1928                     assert!(m.contains_key(&j));
1929                 }
1930             }
1931         }
1932     }
1933
1934     #[test]
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());
1940         let new = 100;
1941         match m.get_mut(&5) {
1942             None => panic!(), Some(x) => *x = new
1943         }
1944         assert_eq!(m.get(&5), Some(&new));
1945     }
1946
1947     #[test]
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);
1954     }
1955
1956     #[test]
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);
1965     }
1966
1967     #[test]
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);
1982     }
1983
1984     #[test]
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());
1991     }
1992
1993     #[test]
1994     fn test_pop() {
1995         let mut m = HashMap::new();
1996         m.insert(1, 2);
1997         assert_eq!(m.remove(&1), Some(2));
1998         assert_eq!(m.remove(&1), None);
1999     }
2000
2001     #[test]
2002     fn test_iterate() {
2003         let mut m = HashMap::with_capacity(4);
2004         for i in 0..32 {
2005             assert!(m.insert(i, i*2).is_none());
2006         }
2007         assert_eq!(m.len(), 32);
2008
2009         let mut observed: u32 = 0;
2010
2011         for (k, v) in &m {
2012             assert_eq!(*v, *k * 2);
2013             observed |= 1 << *k;
2014         }
2015         assert_eq!(observed, 0xFFFF_FFFF);
2016     }
2017
2018     #[test]
2019     fn test_keys() {
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));
2027     }
2028
2029     #[test]
2030     fn test_values() {
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'));
2038     }
2039
2040     #[test]
2041     fn test_find() {
2042         let mut m = HashMap::new();
2043         assert!(m.get(&1).is_none());
2044         m.insert(1, 2);
2045         match m.get(&1) {
2046             None => panic!(),
2047             Some(v) => assert_eq!(*v, 2)
2048         }
2049     }
2050
2051     #[test]
2052     fn test_eq() {
2053         let mut m1 = HashMap::new();
2054         m1.insert(1, 2);
2055         m1.insert(2, 3);
2056         m1.insert(3, 4);
2057
2058         let mut m2 = HashMap::new();
2059         m2.insert(1, 2);
2060         m2.insert(2, 3);
2061
2062         assert!(m1 != m2);
2063
2064         m2.insert(3, 4);
2065
2066         assert_eq!(m1, m2);
2067     }
2068
2069     #[test]
2070     fn test_show() {
2071         let mut map = HashMap::new();
2072         let empty: HashMap<i32, i32> = HashMap::new();
2073
2074         map.insert(1, 2);
2075         map.insert(3, 4);
2076
2077         let map_str = format!("{:?}", map);
2078
2079         assert!(map_str == "{1: 2, 3: 4}" ||
2080                 map_str == "{3: 4, 1: 2}");
2081         assert_eq!(format!("{:?}", empty), "{}");
2082     }
2083
2084     #[test]
2085     fn test_expand() {
2086         let mut m = HashMap::new();
2087
2088         assert_eq!(m.len(), 0);
2089         assert!(m.is_empty());
2090
2091         let mut i = 0;
2092         let old_cap = m.table.capacity();
2093         while old_cap == m.table.capacity() {
2094             m.insert(i, i);
2095             i += 1;
2096         }
2097
2098         assert_eq!(m.len(), i);
2099         assert!(!m.is_empty());
2100     }
2101
2102     #[test]
2103     fn test_behavior_resize_policy() {
2104         let mut m = HashMap::new();
2105
2106         assert_eq!(m.len(), 0);
2107         assert_eq!(m.table.capacity(), 0);
2108         assert!(m.is_empty());
2109
2110         m.insert(0, 0);
2111         m.remove(&0);
2112         assert!(m.is_empty());
2113         let initial_cap = m.table.capacity();
2114         m.reserve(initial_cap);
2115         let cap = m.table.capacity();
2116
2117         assert_eq!(cap, initial_cap * 2);
2118
2119         let mut i = 0;
2120         for _ in 0..cap * 3 / 4 {
2121             m.insert(i, i);
2122             i += 1;
2123         }
2124         // three quarters full
2125
2126         assert_eq!(m.len(), i);
2127         assert_eq!(m.table.capacity(), cap);
2128
2129         for _ in 0..cap / 4 {
2130             m.insert(i, i);
2131             i += 1;
2132         }
2133         // half full
2134
2135         let new_cap = m.table.capacity();
2136         assert_eq!(new_cap, cap * 2);
2137
2138         for _ in 0..cap / 2 - 1 {
2139             i -= 1;
2140             m.remove(&i);
2141             assert_eq!(m.table.capacity(), new_cap);
2142         }
2143         // A little more than one quarter full.
2144         m.shrink_to_fit();
2145         assert_eq!(m.table.capacity(), cap);
2146         // again, a little more than half full
2147         for _ in 0..cap / 2 - 1 {
2148             i -= 1;
2149             m.remove(&i);
2150         }
2151         m.shrink_to_fit();
2152
2153         assert_eq!(m.len(), i);
2154         assert!(!m.is_empty());
2155         assert_eq!(m.table.capacity(), initial_cap);
2156     }
2157
2158     #[test]
2159     fn test_reserve_shrink_to_fit() {
2160         let mut m = HashMap::new();
2161         m.insert(0, 0);
2162         m.remove(&0);
2163         assert!(m.capacity() >= m.len());
2164         for i in 0..128 {
2165             m.insert(i, i);
2166         }
2167         m.reserve(256);
2168
2169         let usable_cap = m.capacity();
2170         for i in 128..(128 + 256) {
2171             m.insert(i, i);
2172             assert_eq!(m.capacity(), usable_cap);
2173         }
2174
2175         for i in 100..(128 + 256) {
2176             assert_eq!(m.remove(&i), Some(i));
2177         }
2178         m.shrink_to_fit();
2179
2180         assert_eq!(m.len(), 100);
2181         assert!(!m.is_empty());
2182         assert!(m.capacity() >= m.len());
2183
2184         for i in 0..100 {
2185             assert_eq!(m.remove(&i), Some(i));
2186         }
2187         m.shrink_to_fit();
2188         m.insert(0, 0);
2189
2190         assert_eq!(m.len(), 1);
2191         assert!(m.capacity() >= m.len());
2192         assert_eq!(m.remove(&0), Some(0));
2193     }
2194
2195     #[test]
2196     fn test_from_iter() {
2197         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2198
2199         let map: HashMap<_, _> = xs.iter().cloned().collect();
2200
2201         for &(k, v) in &xs {
2202             assert_eq!(map.get(&k), Some(&v));
2203         }
2204     }
2205
2206     #[test]
2207     fn test_size_hint() {
2208         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2209
2210         let map: HashMap<_, _>  = xs.iter().cloned().collect();
2211
2212         let mut iter = map.iter();
2213
2214         for _ in iter.by_ref().take(3) {}
2215
2216         assert_eq!(iter.size_hint(), (3, Some(3)));
2217     }
2218
2219     #[test]
2220     fn test_iter_len() {
2221         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2222
2223         let map: HashMap<_, _>  = xs.iter().cloned().collect();
2224
2225         let mut iter = map.iter();
2226
2227         for _ in iter.by_ref().take(3) {}
2228
2229         assert_eq!(iter.len(), 3);
2230     }
2231
2232     #[test]
2233     fn test_mut_size_hint() {
2234         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2235
2236         let mut map: HashMap<_, _>  = xs.iter().cloned().collect();
2237
2238         let mut iter = map.iter_mut();
2239
2240         for _ in iter.by_ref().take(3) {}
2241
2242         assert_eq!(iter.size_hint(), (3, Some(3)));
2243     }
2244
2245     #[test]
2246     fn test_iter_mut_len() {
2247         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2248
2249         let mut map: HashMap<_, _>  = xs.iter().cloned().collect();
2250
2251         let mut iter = map.iter_mut();
2252
2253         for _ in iter.by_ref().take(3) {}
2254
2255         assert_eq!(iter.len(), 3);
2256     }
2257
2258     #[test]
2259     fn test_index() {
2260         let mut map = HashMap::new();
2261
2262         map.insert(1, 2);
2263         map.insert(2, 1);
2264         map.insert(3, 4);
2265
2266         assert_eq!(map[&2], 1);
2267     }
2268
2269     #[test]
2270     #[should_panic]
2271     fn test_index_nonexistent() {
2272         let mut map = HashMap::new();
2273
2274         map.insert(1, 2);
2275         map.insert(2, 1);
2276         map.insert(3, 4);
2277
2278         map[&4];
2279     }
2280
2281     #[test]
2282     fn test_entry(){
2283         let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
2284
2285         let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2286
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);
2293             }
2294         }
2295         assert_eq!(map.get(&1).unwrap(), &100);
2296         assert_eq!(map.len(), 6);
2297
2298
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;
2305                 *v = new_v;
2306             }
2307         }
2308         assert_eq!(map.get(&2).unwrap(), &200);
2309         assert_eq!(map.len(), 6);
2310
2311         // Existing key (take)
2312         match map.entry(3) {
2313             Vacant(_) => unreachable!(),
2314             Occupied(view) => {
2315                 assert_eq!(view.remove(), 30);
2316             }
2317         }
2318         assert_eq!(map.get(&3), None);
2319         assert_eq!(map.len(), 5);
2320
2321
2322         // Inexistent key (insert)
2323         match map.entry(10) {
2324             Occupied(_) => unreachable!(),
2325             Vacant(view) => {
2326                 assert_eq!(*view.insert(1000), 1000);
2327             }
2328         }
2329         assert_eq!(map.get(&10).unwrap(), &1000);
2330         assert_eq!(map.len(), 6);
2331     }
2332
2333     #[test]
2334     fn test_entry_take_doesnt_corrupt() {
2335         #![allow(deprecated)] //rand
2336         // Test for #19292
2337         fn check(m: &HashMap<isize, ()>) {
2338             for k in m.keys() {
2339                 assert!(m.contains_key(k),
2340                         "{} is in keys() but not in the map?", k);
2341             }
2342         }
2343
2344         let mut m = HashMap::new();
2345         let mut rng = thread_rng();
2346
2347         // Populate the map with some items.
2348         for _ in 0..50 {
2349             let x = rng.gen_range(-10, 10);
2350             m.insert(x, ());
2351         }
2352
2353         for i in 0..1000 {
2354             let x = rng.gen_range(-10, 10);
2355             match m.entry(x) {
2356                 Vacant(_) => {},
2357                 Occupied(e) => {
2358                     println!("{}: remove {}", i, x);
2359                     e.remove();
2360                 },
2361             }
2362
2363             check(&m);
2364         }
2365     }
2366
2367     #[test]
2368     fn test_extend_ref() {
2369         let mut a = HashMap::new();
2370         a.insert(1, "one");
2371         let mut b = HashMap::new();
2372         b.insert(2, "two");
2373         b.insert(3, "three");
2374
2375         a.extend(&b);
2376
2377         assert_eq!(a.len(), 3);
2378         assert_eq!(a[&1], "one");
2379         assert_eq!(a[&2], "two");
2380         assert_eq!(a[&3], "three");
2381     }
2382 }