]> git.lizzy.rs Git - rust.git/blob - src/libstd/collections/hash/map.rs
Auto merge of #31684 - tmiasko:alternate-stack, 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 cmp::max;
17 use fmt::{self, Debug};
18 use hash::{Hash, SipHasher, BuildHasher};
19 use iter::{self, Map, FromIterator};
20 use mem::{self, replace};
21 use ops::{Deref, Index};
22 use rand::{self, Rng};
23
24 use super::table::{
25     self,
26     Bucket,
27     EmptyBucket,
28     FullBucket,
29     FullBucketImm,
30     FullBucketMut,
31     RawTable,
32     SafeHash
33 };
34 use super::table::BucketState::{
35     Empty,
36     Full,
37 };
38
39 const INITIAL_LOG2_CAP: usize = 5;
40 const INITIAL_CAPACITY: usize = 1 << INITIAL_LOG2_CAP; // 2^5
41
42 /// The default behavior of HashMap implements a load factor of 90.9%.
43 /// This behavior is characterized by the following condition:
44 ///
45 /// - if size > 0.909 * capacity: grow the map
46 #[derive(Clone)]
47 struct DefaultResizePolicy;
48
49 impl DefaultResizePolicy {
50     fn new() -> DefaultResizePolicy {
51         DefaultResizePolicy
52     }
53
54     #[inline]
55     fn min_capacity(&self, usable_size: usize) -> usize {
56         // Here, we are rephrasing the logic by specifying the lower limit
57         // on capacity:
58         //
59         // - if `cap < size * 1.1`: grow the map
60         usable_size * 11 / 10
61     }
62
63     /// An inverse of `min_capacity`, approximately.
64     #[inline]
65     fn usable_capacity(&self, cap: usize) -> usize {
66         // As the number of entries approaches usable capacity,
67         // min_capacity(size) must be smaller than the internal capacity,
68         // so that the map is not resized:
69         // `min_capacity(usable_capacity(x)) <= x`.
70         // The left-hand side can only be smaller due to flooring by integer
71         // division.
72         //
73         // This doesn't have to be checked for overflow since allocation size
74         // in bytes will overflow earlier than multiplication by 10.
75         //
76         // As per https://github.com/rust-lang/rust/pull/30991 this is updated
77         // to be: (cap * den + den - 1) / num
78         (cap * 10 + 10 - 1) / 11
79     }
80 }
81
82 #[test]
83 fn test_resize_policy() {
84     let rp = DefaultResizePolicy;
85     for n in 0..1000 {
86         assert!(rp.min_capacity(rp.usable_capacity(n)) <= n);
87         assert!(rp.usable_capacity(rp.min_capacity(n)) <= n);
88     }
89 }
90
91 // The main performance trick in this hashmap is called Robin Hood Hashing.
92 // It gains its excellent performance from one essential operation:
93 //
94 //    If an insertion collides with an existing element, and that element's
95 //    "probe distance" (how far away the element is from its ideal location)
96 //    is higher than how far we've already probed, swap the elements.
97 //
98 // This massively lowers variance in probe distance, and allows us to get very
99 // high load factors with good performance. The 90% load factor I use is rather
100 // conservative.
101 //
102 // > Why a load factor of approximately 90%?
103 //
104 // In general, all the distances to initial buckets will converge on the mean.
105 // At a load factor of α, the odds of finding the target bucket after k
106 // probes is approximately 1-α^k. If we set this equal to 50% (since we converge
107 // on the mean) and set k=8 (64-byte cache line / 8-byte hash), α=0.92. I round
108 // this down to make the math easier on the CPU and avoid its FPU.
109 // Since on average we start the probing in the middle of a cache line, this
110 // strategy pulls in two cache lines of hashes on every lookup. I think that's
111 // pretty good, but if you want to trade off some space, it could go down to one
112 // cache line on average with an α of 0.84.
113 //
114 // > Wait, what? Where did you get 1-α^k from?
115 //
116 // On the first probe, your odds of a collision with an existing element is α.
117 // The odds of doing this twice in a row is approximately α^2. For three times,
118 // α^3, etc. Therefore, the odds of colliding k times is α^k. The odds of NOT
119 // colliding after k tries is 1-α^k.
120 //
121 // The paper from 1986 cited below mentions an implementation which keeps track
122 // of the distance-to-initial-bucket histogram. This approach is not suitable
123 // for modern architectures because it requires maintaining an internal data
124 // structure. This allows very good first guesses, but we are most concerned
125 // with guessing entire cache lines, not individual indexes. Furthermore, array
126 // accesses are no longer linear and in one direction, as we have now. There
127 // is also memory and cache pressure that this would entail that would be very
128 // difficult to properly see in a microbenchmark.
129 //
130 // ## Future Improvements (FIXME!)
131 //
132 // Allow the load factor to be changed dynamically and/or at initialization.
133 //
134 // Also, would it be possible for us to reuse storage when growing the
135 // underlying table? This is exactly the use case for 'realloc', and may
136 // be worth exploring.
137 //
138 // ## Future Optimizations (FIXME!)
139 //
140 // Another possible design choice that I made without any real reason is
141 // parameterizing the raw table over keys and values. Technically, all we need
142 // is the size and alignment of keys and values, and the code should be just as
143 // efficient (well, we might need one for power-of-two size and one for not...).
144 // This has the potential to reduce code bloat in rust executables, without
145 // really losing anything except 4 words (key size, key alignment, val size,
146 // val alignment) which can be passed in to every call of a `RawTable` function.
147 // This would definitely be an avenue worth exploring if people start complaining
148 // about the size of rust executables.
149 //
150 // Annotate exceedingly likely branches in `table::make_hash`
151 // and `search_hashed` to reduce instruction cache pressure
152 // and mispredictions once it becomes possible (blocked on issue #11092).
153 //
154 // Shrinking the table could simply reallocate in place after moving buckets
155 // to the first half.
156 //
157 // The growth algorithm (fragment of the Proof of Correctness)
158 // --------------------
159 //
160 // The growth algorithm is basically a fast path of the naive reinsertion-
161 // during-resize algorithm. Other paths should never be taken.
162 //
163 // Consider growing a robin hood hashtable of capacity n. Normally, we do this
164 // by allocating a new table of capacity `2n`, and then individually reinsert
165 // each element in the old table into the new one. This guarantees that the
166 // new table is a valid robin hood hashtable with all the desired statistical
167 // properties. Remark that the order we reinsert the elements in should not
168 // matter. For simplicity and efficiency, we will consider only linear
169 // reinsertions, which consist of reinserting all elements in the old table
170 // into the new one by increasing order of index. However we will not be
171 // starting our reinsertions from index 0 in general. If we start from index
172 // i, for the purpose of reinsertion we will consider all elements with real
173 // index j < i to have virtual index n + j.
174 //
175 // Our hash generation scheme consists of generating a 64-bit hash and
176 // truncating the most significant bits. When moving to the new table, we
177 // simply introduce a new bit to the front of the hash. Therefore, if an
178 // elements has ideal index i in the old table, it can have one of two ideal
179 // locations in the new table. If the new bit is 0, then the new ideal index
180 // is i. If the new bit is 1, then the new ideal index is n + i. Intuitively,
181 // we are producing two independent tables of size n, and for each element we
182 // independently choose which table to insert it into with equal probability.
183 // However the rather than wrapping around themselves on overflowing their
184 // indexes, the first table overflows into the first, and the first into the
185 // second. Visually, our new table will look something like:
186 //
187 // [yy_xxx_xxxx_xxx|xx_yyy_yyyy_yyy]
188 //
189 // Where x's are elements inserted into the first table, y's are elements
190 // inserted into the second, and _'s are empty sections. We now define a few
191 // key concepts that we will use later. Note that this is a very abstract
192 // perspective of the table. A real resized table would be at least half
193 // empty.
194 //
195 // Theorem: A linear robin hood reinsertion from the first ideal element
196 // produces identical results to a linear naive reinsertion from the same
197 // element.
198 //
199 // FIXME(Gankro, pczarn): review the proof and put it all in a separate README.md
200
201 /// A hash map implementation which uses linear probing with Robin
202 /// Hood bucket stealing.
203 ///
204 /// The hashes are all keyed by the thread-local random number generator
205 /// on creation by default. This means that the ordering of the keys is
206 /// randomized, but makes the tables more resistant to
207 /// denial-of-service attacks (Hash DoS). This behavior can be
208 /// overridden with one of the constructors.
209 ///
210 /// It is required that the keys implement the `Eq` and `Hash` traits, although
211 /// this can frequently be achieved by using `#[derive(PartialEq, Eq, Hash)]`.
212 /// If you implement these yourself, it is important that the following
213 /// property holds:
214 ///
215 /// ```text
216 /// k1 == k2 -> hash(k1) == hash(k2)
217 /// ```
218 ///
219 /// In other words, if two keys are equal, their hashes must be equal.
220 ///
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.
225 ///
226 /// Relevant papers/articles:
227 ///
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/)
233 ///
234 /// # Examples
235 ///
236 /// ```
237 /// use std::collections::HashMap;
238 ///
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();
242 ///
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.");
248 ///
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());
253 /// }
254 ///
255 /// // oops, this review has a lot of spelling mistakes, let's delete it.
256 /// book_reviews.remove("The Adventures of Sherlock Holmes");
257 ///
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 {
261 ///     match book_reviews.get(book) {
262 ///         Some(review) => println!("{}: {}", book, review),
263 ///         None => println!("{} is unreviewed.", book)
264 ///     }
265 /// }
266 ///
267 /// // iterate over everything.
268 /// for (book, review) in &book_reviews {
269 ///     println!("{}: \"{}\"", book, review);
270 /// }
271 /// ```
272 ///
273 /// `HashMap` also implements an [`Entry API`](#method.entry), which allows
274 /// for more complex methods of getting, setting, updating and removing keys and
275 /// their values:
276 ///
277 /// ```
278 /// use std::collections::HashMap;
279 ///
280 /// // type inference lets us omit an explicit type signature (which
281 /// // would be `HashMap<&str, u8>` in this example).
282 /// let mut player_stats = HashMap::new();
283 ///
284 /// fn random_stat_buff() -> u8 {
285 ///     // could actually return some random value here - let's just return
286 ///     // some fixed value for now
287 ///     42
288 /// }
289 ///
290 /// // insert a key only if it doesn't already exist
291 /// player_stats.entry("health").or_insert(100);
292 ///
293 /// // insert a key using a function that provides a new value only if it
294 /// // doesn't already exist
295 /// player_stats.entry("defence").or_insert_with(random_stat_buff);
296 ///
297 /// // update a key, guarding against the key possibly not being set
298 /// let stat = player_stats.entry("attack").or_insert(100);
299 /// *stat += random_stat_buff();
300 /// ```
301 ///
302 /// The easiest way to use `HashMap` with a custom type as key is to derive `Eq` and `Hash`.
303 /// We must also derive `PartialEq`.
304 ///
305 /// ```
306 /// use std::collections::HashMap;
307 ///
308 /// #[derive(Hash, Eq, PartialEq, Debug)]
309 /// struct Viking {
310 ///     name: String,
311 ///     country: String,
312 /// }
313 ///
314 /// impl Viking {
315 ///     /// Create a new Viking.
316 ///     fn new(name: &str, country: &str) -> Viking {
317 ///         Viking { name: name.to_string(), country: country.to_string() }
318 ///     }
319 /// }
320 ///
321 /// // Use a HashMap to store the vikings' health points.
322 /// let mut vikings = HashMap::new();
323 ///
324 /// vikings.insert(Viking::new("Einar", "Norway"), 25);
325 /// vikings.insert(Viking::new("Olaf", "Denmark"), 24);
326 /// vikings.insert(Viking::new("Harald", "Iceland"), 12);
327 ///
328 /// // Use derived implementation to print the status of the vikings.
329 /// for (viking, health) in &vikings {
330 ///     println!("{:?} has {} hp", viking, health);
331 /// }
332 /// ```
333 #[derive(Clone)]
334 #[stable(feature = "rust1", since = "1.0.0")]
335 pub struct HashMap<K, V, S = RandomState> {
336     // All hashes are keyed on these values, to prevent hash collision attacks.
337     hash_builder: S,
338
339     table: RawTable<K, V>,
340
341     resize_policy: DefaultResizePolicy,
342 }
343
344 /// Search for a pre-hashed key.
345 fn search_hashed<K, V, M, F>(table: M,
346                              hash: SafeHash,
347                              mut is_match: F)
348                              -> SearchResult<K, V, M> where
349     M: Deref<Target=RawTable<K, V>>,
350     F: FnMut(&K) -> bool,
351 {
352     // This is the only function where capacity can be zero. To avoid
353     // undefined behavior when Bucket::new gets the raw bucket in this
354     // case, immediately return the appropriate search result.
355     if table.capacity() == 0 {
356         return TableRef(table);
357     }
358
359     let size = table.size();
360     let mut probe = Bucket::new(table, hash);
361     let ib = probe.index();
362
363     while probe.index() != ib + size {
364         let full = match probe.peek() {
365             Empty(b) => return TableRef(b.into_table()), // hit an empty bucket
366             Full(b) => b
367         };
368
369         if full.distance() + ib < full.index() {
370             // We can finish the search early if we hit any bucket
371             // with a lower distance to initial bucket than we've probed.
372             return TableRef(full.into_table());
373         }
374
375         // If the hash doesn't match, it can't be this one..
376         if hash == full.hash() {
377             // If the key doesn't match, it can't be this one..
378             if is_match(full.read().0) {
379                 return FoundExisting(full);
380             }
381         }
382
383         probe = full.next();
384     }
385
386     TableRef(probe.into_table())
387 }
388
389 fn pop_internal<K, V>(starting_bucket: FullBucketMut<K, V>) -> (K, V) {
390     let (empty, retkey, retval) = starting_bucket.take();
391     let mut gap = match empty.gap_peek() {
392         Some(b) => b,
393         None => return (retkey, retval)
394     };
395
396     while gap.full().distance() != 0 {
397         gap = match gap.shift() {
398             Some(b) => b,
399             None => break
400         };
401     }
402
403     // Now we've done all our shifting. Return the value we grabbed earlier.
404     (retkey, retval)
405 }
406
407 /// Perform robin hood bucket stealing at the given `bucket`. You must
408 /// also pass the position of that bucket's initial bucket so we don't have
409 /// to recalculate it.
410 ///
411 /// `hash`, `k`, and `v` are the elements to "robin hood" into the hashtable.
412 fn robin_hood<'a, K: 'a, V: 'a>(mut bucket: FullBucketMut<'a, K, V>,
413                         mut ib: usize,
414                         mut hash: SafeHash,
415                         mut k: K,
416                         mut v: V)
417                         -> &'a mut V {
418     let starting_index = bucket.index();
419     let size = {
420         let table = bucket.table(); // FIXME "lifetime too short".
421         table.size()
422     };
423     // There can be at most `size - dib` buckets to displace, because
424     // in the worst case, there are `size` elements and we already are
425     // `distance` buckets away from the initial one.
426     let idx_end = starting_index + size - bucket.distance();
427
428     loop {
429         let (old_hash, old_key, old_val) = bucket.replace(hash, k, v);
430         loop {
431             let probe = bucket.next();
432             assert!(probe.index() != idx_end);
433
434             let full_bucket = match probe.peek() {
435                 Empty(bucket) => {
436                     // Found a hole!
437                     let b = bucket.put(old_hash, old_key, old_val);
438                     // Now that it's stolen, just read the value's pointer
439                     // right out of the table!
440                     return Bucket::at_index(b.into_table(), starting_index)
441                                .peek()
442                                .expect_full()
443                                .into_mut_refs()
444                                .1;
445                 },
446                 Full(bucket) => bucket
447             };
448
449             let probe_ib = full_bucket.index() - full_bucket.distance();
450
451             bucket = full_bucket;
452
453             // Robin hood! Steal the spot.
454             if ib < probe_ib {
455                 ib = probe_ib;
456                 hash = old_hash;
457                 k = old_key;
458                 v = old_val;
459                 break;
460             }
461         }
462     }
463 }
464
465 /// A result that works like Option<FullBucket<..>> but preserves
466 /// the reference that grants us access to the table in any case.
467 enum SearchResult<K, V, M> {
468     // This is an entry that holds the given key:
469     FoundExisting(FullBucket<K, V, M>),
470
471     // There was no such entry. The reference is given back:
472     TableRef(M)
473 }
474
475 impl<K, V, M> SearchResult<K, V, M> {
476     fn into_option(self) -> Option<FullBucket<K, V, M>> {
477         match self {
478             FoundExisting(bucket) => Some(bucket),
479             TableRef(_) => None
480         }
481     }
482 }
483
484 impl<K, V, S> HashMap<K, V, S>
485     where K: Eq + Hash, S: BuildHasher
486 {
487     fn make_hash<X: ?Sized>(&self, x: &X) -> SafeHash where X: Hash {
488         table::make_hash(&self.hash_builder, x)
489     }
490
491     /// Search for a key, yielding the index if it's found in the hashtable.
492     /// If you already have the hash for the key lying around, use
493     /// search_hashed.
494     fn search<'a, Q: ?Sized>(&'a self, q: &Q) -> Option<FullBucketImm<'a, K, V>>
495         where K: Borrow<Q>, Q: Eq + Hash
496     {
497         let hash = self.make_hash(q);
498         search_hashed(&self.table, hash, |k| q.eq(k.borrow()))
499             .into_option()
500     }
501
502     fn search_mut<'a, Q: ?Sized>(&'a mut self, q: &Q) -> Option<FullBucketMut<'a, K, V>>
503         where K: Borrow<Q>, Q: Eq + Hash
504     {
505         let hash = self.make_hash(q);
506         search_hashed(&mut self.table, hash, |k| q.eq(k.borrow()))
507             .into_option()
508     }
509
510     // The caller should ensure that invariants by Robin Hood Hashing hold.
511     fn insert_hashed_ordered(&mut self, hash: SafeHash, k: K, v: V) {
512         let cap = self.table.capacity();
513         let mut buckets = Bucket::new(&mut self.table, hash);
514         let ib = buckets.index();
515
516         while buckets.index() != ib + cap {
517             // We don't need to compare hashes for value swap.
518             // Not even DIBs for Robin Hood.
519             buckets = match buckets.peek() {
520                 Empty(empty) => {
521                     empty.put(hash, k, v);
522                     return;
523                 }
524                 Full(b) => b.into_bucket()
525             };
526             buckets.next();
527         }
528         panic!("Internal HashMap error: Out of space.");
529     }
530 }
531
532 impl<K: Hash + Eq, V> HashMap<K, V, RandomState> {
533     /// Creates an empty HashMap.
534     ///
535     /// # Examples
536     ///
537     /// ```
538     /// use std::collections::HashMap;
539     /// let mut map: HashMap<&str, isize> = HashMap::new();
540     /// ```
541     #[inline]
542     #[stable(feature = "rust1", since = "1.0.0")]
543     pub fn new() -> HashMap<K, V, RandomState> {
544         Default::default()
545     }
546
547     /// Creates an empty hash map with the given initial capacity.
548     ///
549     /// # Examples
550     ///
551     /// ```
552     /// use std::collections::HashMap;
553     /// let mut map: HashMap<&str, isize> = HashMap::with_capacity(10);
554     /// ```
555     #[inline]
556     #[stable(feature = "rust1", since = "1.0.0")]
557     pub fn with_capacity(capacity: usize) -> HashMap<K, V, RandomState> {
558         HashMap::with_capacity_and_hasher(capacity, Default::default())
559     }
560 }
561
562 impl<K, V, S> HashMap<K, V, S>
563     where K: Eq + Hash, S: BuildHasher
564 {
565     /// Creates an empty hashmap which will use the given hash builder to hash
566     /// keys.
567     ///
568     /// The created map has the default initial capacity.
569     ///
570     /// Warning: `hash_builder` is normally randomly generated, and
571     /// is designed to allow HashMaps to be resistant to attacks that
572     /// cause many collisions and very poor performance. Setting it
573     /// manually using this function can expose a DoS attack vector.
574     ///
575     /// # Examples
576     ///
577     /// ```
578     /// use std::collections::HashMap;
579     /// use std::collections::hash_map::RandomState;
580     ///
581     /// let s = RandomState::new();
582     /// let mut map = HashMap::with_hasher(s);
583     /// map.insert(1, 2);
584     /// ```
585     #[inline]
586     #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
587     pub fn with_hasher(hash_builder: S) -> HashMap<K, V, S> {
588         HashMap {
589             hash_builder: hash_builder,
590             resize_policy: DefaultResizePolicy::new(),
591             table: RawTable::new(0),
592         }
593     }
594
595     /// Deprecated, renamed to `with_hasher`
596     #[inline]
597     #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
598                issue = "27713")]
599     #[rustc_deprecated(since = "1.7.0", reason = "renamed to with_hasher")]
600     pub fn with_hash_state(hash_state: S) -> HashMap<K, V, S> {
601         HashMap::with_hasher(hash_state)
602     }
603
604     /// Creates an empty HashMap with space for at least `capacity`
605     /// elements, using `hasher` to hash the keys.
606     ///
607     /// Warning: `hasher` is normally randomly generated, and
608     /// is designed to allow HashMaps to be resistant to attacks that
609     /// cause many collisions and very poor performance. Setting it
610     /// manually using this function can expose a DoS attack vector.
611     ///
612     /// # Examples
613     ///
614     /// ```
615     /// use std::collections::HashMap;
616     /// use std::collections::hash_map::RandomState;
617     ///
618     /// let s = RandomState::new();
619     /// let mut map = HashMap::with_capacity_and_hasher(10, s);
620     /// map.insert(1, 2);
621     /// ```
622     #[inline]
623     #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
624     pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S)
625                                     -> HashMap<K, V, S> {
626         let resize_policy = DefaultResizePolicy::new();
627         let min_cap = max(INITIAL_CAPACITY, resize_policy.min_capacity(capacity));
628         let internal_cap = min_cap.checked_next_power_of_two().expect("capacity overflow");
629         assert!(internal_cap >= capacity, "capacity overflow");
630         HashMap {
631             hash_builder: hash_builder,
632             resize_policy: resize_policy,
633             table: RawTable::new(internal_cap),
634         }
635     }
636
637     /// Deprecated, renamed to `with_capacity_and_hasher`
638     #[inline]
639     #[unstable(feature = "hashmap_hasher", reason = "hasher stuff is unclear",
640                issue = "27713")]
641     #[rustc_deprecated(since = "1.7.0",
642                        reason = "renamed to with_capacity_and_hasher")]
643     pub fn with_capacity_and_hash_state(capacity: usize, hash_state: S)
644                                         -> HashMap<K, V, S> {
645         HashMap::with_capacity_and_hasher(capacity, hash_state)
646     }
647
648     /// Returns a reference to the map's hasher.
649     #[unstable(feature = "hashmap_public_hasher", reason = "don't want to make insta-stable",
650                issue = "31262")]
651     pub fn hasher(&self) -> &S {
652         &self.hash_builder
653     }
654
655     /// Returns the number of elements the map can hold without reallocating.
656     ///
657     /// This number is a lower bound; the `HashMap<K, V>` might be able to hold
658     /// more, but is guaranteed to be able to hold at least this many.
659     ///
660     /// # Examples
661     ///
662     /// ```
663     /// use std::collections::HashMap;
664     /// let map: HashMap<isize, isize> = HashMap::with_capacity(100);
665     /// assert!(map.capacity() >= 100);
666     /// ```
667     #[inline]
668     #[stable(feature = "rust1", since = "1.0.0")]
669     pub fn capacity(&self) -> usize {
670         self.resize_policy.usable_capacity(self.table.capacity())
671     }
672
673     /// Reserves capacity for at least `additional` more elements to be inserted
674     /// in the `HashMap`. The collection may reserve more space to avoid
675     /// frequent reallocations.
676     ///
677     /// # Panics
678     ///
679     /// Panics if the new allocation size overflows `usize`.
680     ///
681     /// # Examples
682     ///
683     /// ```
684     /// use std::collections::HashMap;
685     /// let mut map: HashMap<&str, isize> = HashMap::new();
686     /// map.reserve(10);
687     /// ```
688     #[stable(feature = "rust1", since = "1.0.0")]
689     pub fn reserve(&mut self, additional: usize) {
690         let new_size = self.len().checked_add(additional).expect("capacity overflow");
691         let min_cap = self.resize_policy.min_capacity(new_size);
692
693         // An invalid value shouldn't make us run out of space. This includes
694         // an overflow check.
695         assert!(new_size <= min_cap);
696
697         if self.table.capacity() < min_cap {
698             let new_capacity = max(min_cap.next_power_of_two(), INITIAL_CAPACITY);
699             self.resize(new_capacity);
700         }
701     }
702
703     /// Resizes the internal vectors to a new capacity. It's your responsibility to:
704     ///   1) Make sure the new capacity is enough for all the elements, accounting
705     ///      for the load factor.
706     ///   2) Ensure new_capacity is a power of two or zero.
707     fn resize(&mut self, new_capacity: usize) {
708         assert!(self.table.size() <= new_capacity);
709         assert!(new_capacity.is_power_of_two() || new_capacity == 0);
710
711         let mut old_table = replace(&mut self.table, RawTable::new(new_capacity));
712         let old_size = old_table.size();
713
714         if old_table.capacity() == 0 || old_table.size() == 0 {
715             return;
716         }
717
718         // Grow the table.
719         // Specialization of the other branch.
720         let mut bucket = Bucket::first(&mut old_table);
721
722         // "So a few of the first shall be last: for many be called,
723         // but few chosen."
724         //
725         // We'll most likely encounter a few buckets at the beginning that
726         // have their initial buckets near the end of the table. They were
727         // placed at the beginning as the probe wrapped around the table
728         // during insertion. We must skip forward to a bucket that won't
729         // get reinserted too early and won't unfairly steal others spot.
730         // This eliminates the need for robin hood.
731         loop {
732             bucket = match bucket.peek() {
733                 Full(full) => {
734                     if full.distance() == 0 {
735                         // This bucket occupies its ideal spot.
736                         // It indicates the start of another "cluster".
737                         bucket = full.into_bucket();
738                         break;
739                     }
740                     // Leaving this bucket in the last cluster for later.
741                     full.into_bucket()
742                 }
743                 Empty(b) => {
744                     // Encountered a hole between clusters.
745                     b.into_bucket()
746                 }
747             };
748             bucket.next();
749         }
750
751         // This is how the buckets might be laid out in memory:
752         // ($ marks an initialized bucket)
753         //  ________________
754         // |$$$_$$$$$$_$$$$$|
755         //
756         // But we've skipped the entire initial cluster of buckets
757         // and will continue iteration in this order:
758         //  ________________
759         //     |$$$$$$_$$$$$
760         //                  ^ wrap around once end is reached
761         //  ________________
762         //  $$$_____________|
763         //    ^ exit once table.size == 0
764         loop {
765             bucket = match bucket.peek() {
766                 Full(bucket) => {
767                     let h = bucket.hash();
768                     let (b, k, v) = bucket.take();
769                     self.insert_hashed_ordered(h, k, v);
770                     {
771                         let t = b.table(); // FIXME "lifetime too short".
772                         if t.size() == 0 { break }
773                     };
774                     b.into_bucket()
775                 }
776                 Empty(b) => b.into_bucket()
777             };
778             bucket.next();
779         }
780
781         assert_eq!(self.table.size(), old_size);
782     }
783
784     /// Shrinks the capacity of the map as much as possible. It will drop
785     /// down as much as possible while maintaining the internal rules
786     /// and possibly leaving some space in accordance with the resize policy.
787     ///
788     /// # Examples
789     ///
790     /// ```
791     /// use std::collections::HashMap;
792     ///
793     /// let mut map: HashMap<isize, isize> = HashMap::with_capacity(100);
794     /// map.insert(1, 2);
795     /// map.insert(3, 4);
796     /// assert!(map.capacity() >= 100);
797     /// map.shrink_to_fit();
798     /// assert!(map.capacity() >= 2);
799     /// ```
800     #[stable(feature = "rust1", since = "1.0.0")]
801     pub fn shrink_to_fit(&mut self) {
802         let min_capacity = self.resize_policy.min_capacity(self.len());
803         let min_capacity = max(min_capacity.next_power_of_two(), INITIAL_CAPACITY);
804
805         // An invalid value shouldn't make us run out of space.
806         debug_assert!(self.len() <= min_capacity);
807
808         if self.table.capacity() != min_capacity {
809             let old_table = replace(&mut self.table, RawTable::new(min_capacity));
810             let old_size = old_table.size();
811
812             // Shrink the table. Naive algorithm for resizing:
813             for (h, k, v) in old_table.into_iter() {
814                 self.insert_hashed_nocheck(h, k, v);
815             }
816
817             debug_assert_eq!(self.table.size(), old_size);
818         }
819     }
820
821     /// Insert a pre-hashed key-value pair, without first checking
822     /// that there's enough room in the buckets. Returns a reference to the
823     /// newly insert value.
824     ///
825     /// If the key already exists, the hashtable will be returned untouched
826     /// and a reference to the existing element will be returned.
827     fn insert_hashed_nocheck(&mut self, hash: SafeHash, k: K, v: V) -> &mut V {
828         self.insert_or_replace_with(hash, k, v, |_, _, _, _| ())
829     }
830
831     fn insert_or_replace_with<'a, F>(&'a mut self,
832                                      hash: SafeHash,
833                                      k: K,
834                                      v: V,
835                                      mut found_existing: F)
836                                      -> &'a mut V where
837         F: FnMut(&mut K, &mut V, K, V),
838     {
839         // Worst case, we'll find one empty bucket among `size + 1` buckets.
840         let size = self.table.size();
841         let mut probe = Bucket::new(&mut self.table, hash);
842         let ib = probe.index();
843
844         loop {
845             let mut bucket = match probe.peek() {
846                 Empty(bucket) => {
847                     // Found a hole!
848                     return bucket.put(hash, k, v).into_mut_refs().1;
849                 }
850                 Full(bucket) => bucket
851             };
852
853             // hash matches?
854             if bucket.hash() == hash {
855                 // key matches?
856                 if k == *bucket.read_mut().0 {
857                     let (bucket_k, bucket_v) = bucket.into_mut_refs();
858                     debug_assert!(k == *bucket_k);
859                     // Key already exists. Get its reference.
860                     found_existing(bucket_k, bucket_v, k, v);
861                     return bucket_v;
862                 }
863             }
864
865             let robin_ib = bucket.index() as isize - bucket.distance() as isize;
866
867             if (ib as isize) < robin_ib {
868                 // Found a luckier bucket than me. Better steal his spot.
869                 return robin_hood(bucket, robin_ib as usize, hash, k, v);
870             }
871
872             probe = bucket.next();
873             assert!(probe.index() != ib + size + 1);
874         }
875     }
876
877     /// An iterator visiting all keys in arbitrary order.
878     /// Iterator element type is `&'a K`.
879     ///
880     /// # Examples
881     ///
882     /// ```
883     /// use std::collections::HashMap;
884     ///
885     /// let mut map = HashMap::new();
886     /// map.insert("a", 1);
887     /// map.insert("b", 2);
888     /// map.insert("c", 3);
889     ///
890     /// for key in map.keys() {
891     ///     println!("{}", key);
892     /// }
893     /// ```
894     #[stable(feature = "rust1", since = "1.0.0")]
895     pub fn keys<'a>(&'a self) -> Keys<'a, K, V> {
896         fn first<A, B>((a, _): (A, B)) -> A { a }
897         let first: fn((&'a K,&'a V)) -> &'a K = first; // coerce to fn ptr
898
899         Keys { inner: self.iter().map(first) }
900     }
901
902     /// An iterator visiting all values in arbitrary order.
903     /// Iterator element type is `&'a V`.
904     ///
905     /// # Examples
906     ///
907     /// ```
908     /// use std::collections::HashMap;
909     ///
910     /// let mut map = HashMap::new();
911     /// map.insert("a", 1);
912     /// map.insert("b", 2);
913     /// map.insert("c", 3);
914     ///
915     /// for val in map.values() {
916     ///     println!("{}", val);
917     /// }
918     /// ```
919     #[stable(feature = "rust1", since = "1.0.0")]
920     pub fn values<'a>(&'a self) -> Values<'a, K, V> {
921         fn second<A, B>((_, b): (A, B)) -> B { b }
922         let second: fn((&'a K,&'a V)) -> &'a V = second; // coerce to fn ptr
923
924         Values { inner: self.iter().map(second) }
925     }
926
927     /// An iterator visiting all key-value pairs in arbitrary order.
928     /// Iterator element type is `(&'a K, &'a V)`.
929     ///
930     /// # Examples
931     ///
932     /// ```
933     /// use std::collections::HashMap;
934     ///
935     /// let mut map = HashMap::new();
936     /// map.insert("a", 1);
937     /// map.insert("b", 2);
938     /// map.insert("c", 3);
939     ///
940     /// for (key, val) in map.iter() {
941     ///     println!("key: {} val: {}", key, val);
942     /// }
943     /// ```
944     #[stable(feature = "rust1", since = "1.0.0")]
945     pub fn iter(&self) -> Iter<K, V> {
946         Iter { inner: self.table.iter() }
947     }
948
949     /// An iterator visiting all key-value pairs in arbitrary order,
950     /// with mutable references to the values.
951     /// Iterator element type is `(&'a K, &'a mut V)`.
952     ///
953     /// # Examples
954     ///
955     /// ```
956     /// use std::collections::HashMap;
957     ///
958     /// let mut map = HashMap::new();
959     /// map.insert("a", 1);
960     /// map.insert("b", 2);
961     /// map.insert("c", 3);
962     ///
963     /// // Update all values
964     /// for (_, val) in map.iter_mut() {
965     ///     *val *= 2;
966     /// }
967     ///
968     /// for (key, val) in &map {
969     ///     println!("key: {} val: {}", key, val);
970     /// }
971     /// ```
972     #[stable(feature = "rust1", since = "1.0.0")]
973     pub fn iter_mut(&mut self) -> IterMut<K, V> {
974         IterMut { inner: self.table.iter_mut() }
975     }
976
977     /// Gets the given key's corresponding entry in the map for in-place manipulation.
978     ///
979     /// # Examples
980     ///
981     /// ```
982     /// use std::collections::HashMap;
983     ///
984     /// let mut letters = HashMap::new();
985     ///
986     /// for ch in "a short treatise on fungi".chars() {
987     ///     let counter = letters.entry(ch).or_insert(0);
988     ///     *counter += 1;
989     /// }
990     ///
991     /// assert_eq!(letters[&'s'], 2);
992     /// assert_eq!(letters[&'t'], 3);
993     /// assert_eq!(letters[&'u'], 1);
994     /// assert_eq!(letters.get(&'y'), None);
995     /// ```
996     #[stable(feature = "rust1", since = "1.0.0")]
997     pub fn entry(&mut self, key: K) -> Entry<K, V> {
998         // Gotta resize now.
999         self.reserve(1);
1000
1001         let hash = self.make_hash(&key);
1002         search_entry_hashed(&mut self.table, hash, key)
1003     }
1004
1005     /// Returns the number of elements in the map.
1006     ///
1007     /// # Examples
1008     ///
1009     /// ```
1010     /// use std::collections::HashMap;
1011     ///
1012     /// let mut a = HashMap::new();
1013     /// assert_eq!(a.len(), 0);
1014     /// a.insert(1, "a");
1015     /// assert_eq!(a.len(), 1);
1016     /// ```
1017     #[stable(feature = "rust1", since = "1.0.0")]
1018     pub fn len(&self) -> usize { self.table.size() }
1019
1020     /// Returns true if the map contains no elements.
1021     ///
1022     /// # Examples
1023     ///
1024     /// ```
1025     /// use std::collections::HashMap;
1026     ///
1027     /// let mut a = HashMap::new();
1028     /// assert!(a.is_empty());
1029     /// a.insert(1, "a");
1030     /// assert!(!a.is_empty());
1031     /// ```
1032     #[inline]
1033     #[stable(feature = "rust1", since = "1.0.0")]
1034     pub fn is_empty(&self) -> bool { self.len() == 0 }
1035
1036     /// Clears the map, returning all key-value pairs as an iterator. Keeps the
1037     /// allocated memory for reuse.
1038     ///
1039     /// # Examples
1040     ///
1041     /// ```
1042     /// use std::collections::HashMap;
1043     ///
1044     /// let mut a = HashMap::new();
1045     /// a.insert(1, "a");
1046     /// a.insert(2, "b");
1047     ///
1048     /// for (k, v) in a.drain().take(1) {
1049     ///     assert!(k == 1 || k == 2);
1050     ///     assert!(v == "a" || v == "b");
1051     /// }
1052     ///
1053     /// assert!(a.is_empty());
1054     /// ```
1055     #[inline]
1056     #[stable(feature = "drain", since = "1.6.0")]
1057     pub fn drain(&mut self) -> Drain<K, V> {
1058         fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1059         let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two; // coerce to fn pointer
1060
1061         Drain {
1062             inner: self.table.drain().map(last_two),
1063         }
1064     }
1065
1066     /// Clears the map, removing all key-value pairs. Keeps the allocated memory
1067     /// for reuse.
1068     ///
1069     /// # Examples
1070     ///
1071     /// ```
1072     /// use std::collections::HashMap;
1073     ///
1074     /// let mut a = HashMap::new();
1075     /// a.insert(1, "a");
1076     /// a.clear();
1077     /// assert!(a.is_empty());
1078     /// ```
1079     #[stable(feature = "rust1", since = "1.0.0")]
1080     #[inline]
1081     pub fn clear(&mut self) {
1082         self.drain();
1083     }
1084
1085     /// Returns a reference to the value corresponding to the key.
1086     ///
1087     /// The key may be any borrowed form of the map's key type, but
1088     /// `Hash` and `Eq` on the borrowed form *must* match those for
1089     /// the key type.
1090     ///
1091     /// # Examples
1092     ///
1093     /// ```
1094     /// use std::collections::HashMap;
1095     ///
1096     /// let mut map = HashMap::new();
1097     /// map.insert(1, "a");
1098     /// assert_eq!(map.get(&1), Some(&"a"));
1099     /// assert_eq!(map.get(&2), None);
1100     /// ```
1101     #[stable(feature = "rust1", since = "1.0.0")]
1102     pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
1103         where K: Borrow<Q>, Q: Hash + Eq
1104     {
1105         self.search(k).map(|bucket| bucket.into_refs().1)
1106     }
1107
1108     /// Returns true if the map contains a value for the specified key.
1109     ///
1110     /// The key may be any borrowed form of the map's key type, but
1111     /// `Hash` and `Eq` on the borrowed form *must* match those for
1112     /// the key type.
1113     ///
1114     /// # Examples
1115     ///
1116     /// ```
1117     /// use std::collections::HashMap;
1118     ///
1119     /// let mut map = HashMap::new();
1120     /// map.insert(1, "a");
1121     /// assert_eq!(map.contains_key(&1), true);
1122     /// assert_eq!(map.contains_key(&2), false);
1123     /// ```
1124     #[stable(feature = "rust1", since = "1.0.0")]
1125     pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
1126         where K: Borrow<Q>, Q: Hash + Eq
1127     {
1128         self.search(k).is_some()
1129     }
1130
1131     /// Returns a mutable reference to the value corresponding to the key.
1132     ///
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
1135     /// the key type.
1136     ///
1137     /// # Examples
1138     ///
1139     /// ```
1140     /// use std::collections::HashMap;
1141     ///
1142     /// let mut map = HashMap::new();
1143     /// map.insert(1, "a");
1144     /// if let Some(x) = map.get_mut(&1) {
1145     ///     *x = "b";
1146     /// }
1147     /// assert_eq!(map[&1], "b");
1148     /// ```
1149     #[stable(feature = "rust1", since = "1.0.0")]
1150     pub fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut V>
1151         where K: Borrow<Q>, Q: Hash + Eq
1152     {
1153         self.search_mut(k).map(|bucket| bucket.into_mut_refs().1)
1154     }
1155
1156     /// Inserts a key-value pair into the map.
1157     ///
1158     /// If the map did not have this key present, `None` is returned.
1159     ///
1160     /// If the map did have this key present, the value is updated, and the old
1161     /// value is returned. The key is not updated, though; this matters for
1162     /// types that can be `==` without being identical. See the [module-level
1163     /// documentation] for more.
1164     ///
1165     /// [module-level documentation]: index.html#insert-and-complex-keys
1166     ///
1167     /// # Examples
1168     ///
1169     /// ```
1170     /// use std::collections::HashMap;
1171     ///
1172     /// let mut map = HashMap::new();
1173     /// assert_eq!(map.insert(37, "a"), None);
1174     /// assert_eq!(map.is_empty(), false);
1175     ///
1176     /// map.insert(37, "b");
1177     /// assert_eq!(map.insert(37, "c"), Some("b"));
1178     /// assert_eq!(map[&37], "c");
1179     /// ```
1180     #[stable(feature = "rust1", since = "1.0.0")]
1181     pub fn insert(&mut self, k: K, v: V) -> Option<V> {
1182         let hash = self.make_hash(&k);
1183         self.reserve(1);
1184
1185         let mut retval = None;
1186         self.insert_or_replace_with(hash, k, v, |_, val_ref, _, val| {
1187             retval = Some(replace(val_ref, val));
1188         });
1189         retval
1190     }
1191
1192     /// Removes a key from the map, returning the value at the key if the key
1193     /// was previously in the map.
1194     ///
1195     /// The key may be any borrowed form of the map's key type, but
1196     /// `Hash` and `Eq` on the borrowed form *must* match those for
1197     /// the key type.
1198     ///
1199     /// # Examples
1200     ///
1201     /// ```
1202     /// use std::collections::HashMap;
1203     ///
1204     /// let mut map = HashMap::new();
1205     /// map.insert(1, "a");
1206     /// assert_eq!(map.remove(&1), Some("a"));
1207     /// assert_eq!(map.remove(&1), None);
1208     /// ```
1209     #[stable(feature = "rust1", since = "1.0.0")]
1210     pub fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<V>
1211         where K: Borrow<Q>, Q: Hash + Eq
1212     {
1213         if self.table.size() == 0 {
1214             return None
1215         }
1216
1217         self.search_mut(k).map(|bucket| pop_internal(bucket).1)
1218     }
1219 }
1220
1221 fn search_entry_hashed<'a, K: Eq, V>(table: &'a mut RawTable<K,V>, hash: SafeHash, k: K)
1222         -> Entry<'a, K, V>
1223 {
1224     // Worst case, we'll find one empty bucket among `size + 1` buckets.
1225     let size = table.size();
1226     let mut probe = Bucket::new(table, hash);
1227     let ib = probe.index();
1228
1229     loop {
1230         let bucket = match probe.peek() {
1231             Empty(bucket) => {
1232                 // Found a hole!
1233                 return Vacant(VacantEntry {
1234                     hash: hash,
1235                     key: k,
1236                     elem: NoElem(bucket),
1237                 });
1238             },
1239             Full(bucket) => bucket
1240         };
1241
1242         // hash matches?
1243         if bucket.hash() == hash {
1244             // key matches?
1245             if k == *bucket.read().0 {
1246                 return Occupied(OccupiedEntry{
1247                     elem: bucket,
1248                 });
1249             }
1250         }
1251
1252         let robin_ib = bucket.index() as isize - bucket.distance() as isize;
1253
1254         if (ib as isize) < robin_ib {
1255             // Found a luckier bucket than me. Better steal his spot.
1256             return Vacant(VacantEntry {
1257                 hash: hash,
1258                 key: k,
1259                 elem: NeqElem(bucket, robin_ib as usize),
1260             });
1261         }
1262
1263         probe = bucket.next();
1264         assert!(probe.index() != ib + size + 1);
1265     }
1266 }
1267
1268 #[stable(feature = "rust1", since = "1.0.0")]
1269 impl<K, V, S> PartialEq for HashMap<K, V, S>
1270     where K: Eq + Hash, V: PartialEq, S: BuildHasher
1271 {
1272     fn eq(&self, other: &HashMap<K, V, S>) -> bool {
1273         if self.len() != other.len() { return false; }
1274
1275         self.iter().all(|(key, value)|
1276             other.get(key).map_or(false, |v| *value == *v)
1277         )
1278     }
1279 }
1280
1281 #[stable(feature = "rust1", since = "1.0.0")]
1282 impl<K, V, S> Eq for HashMap<K, V, S>
1283     where K: Eq + Hash, V: Eq, S: BuildHasher
1284 {}
1285
1286 #[stable(feature = "rust1", since = "1.0.0")]
1287 impl<K, V, S> Debug for HashMap<K, V, S>
1288     where K: Eq + Hash + Debug, V: Debug, S: BuildHasher
1289 {
1290     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1291         f.debug_map().entries(self.iter()).finish()
1292     }
1293 }
1294
1295 #[stable(feature = "rust1", since = "1.0.0")]
1296 impl<K, V, S> Default for HashMap<K, V, S>
1297     where K: Eq + Hash,
1298           S: BuildHasher + Default,
1299 {
1300     fn default() -> HashMap<K, V, S> {
1301         HashMap::with_hasher(Default::default())
1302     }
1303 }
1304
1305 #[stable(feature = "rust1", since = "1.0.0")]
1306 impl<'a, K, Q: ?Sized, V, S> Index<&'a Q> for HashMap<K, V, S>
1307     where K: Eq + Hash + Borrow<Q>,
1308           Q: Eq + Hash,
1309           S: BuildHasher,
1310 {
1311     type Output = V;
1312
1313     #[inline]
1314     fn index(&self, index: &Q) -> &V {
1315         self.get(index).expect("no entry found for key")
1316     }
1317 }
1318
1319 /// HashMap iterator.
1320 #[stable(feature = "rust1", since = "1.0.0")]
1321 pub struct Iter<'a, K: 'a, V: 'a> {
1322     inner: table::Iter<'a, K, V>
1323 }
1324
1325 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1326 #[stable(feature = "rust1", since = "1.0.0")]
1327 impl<'a, K, V> Clone for Iter<'a, K, V> {
1328     fn clone(&self) -> Iter<'a, K, V> {
1329         Iter {
1330             inner: self.inner.clone()
1331         }
1332     }
1333 }
1334
1335 /// HashMap mutable values iterator.
1336 #[stable(feature = "rust1", since = "1.0.0")]
1337 pub struct IterMut<'a, K: 'a, V: 'a> {
1338     inner: table::IterMut<'a, K, V>
1339 }
1340
1341 /// HashMap move iterator.
1342 #[stable(feature = "rust1", since = "1.0.0")]
1343 pub struct IntoIter<K, V> {
1344     inner: iter::Map<table::IntoIter<K, V>, fn((SafeHash, K, V)) -> (K, V)>
1345 }
1346
1347 /// HashMap keys iterator.
1348 #[stable(feature = "rust1", since = "1.0.0")]
1349 pub struct Keys<'a, K: 'a, V: 'a> {
1350     inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K>
1351 }
1352
1353 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1354 #[stable(feature = "rust1", since = "1.0.0")]
1355 impl<'a, K, V> Clone for Keys<'a, K, V> {
1356     fn clone(&self) -> Keys<'a, K, V> {
1357         Keys {
1358             inner: self.inner.clone()
1359         }
1360     }
1361 }
1362
1363 /// HashMap values iterator.
1364 #[stable(feature = "rust1", since = "1.0.0")]
1365 pub struct Values<'a, K: 'a, V: 'a> {
1366     inner: Map<Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V>
1367 }
1368
1369 // FIXME(#19839) Remove in favor of `#[derive(Clone)]`
1370 #[stable(feature = "rust1", since = "1.0.0")]
1371 impl<'a, K, V> Clone for Values<'a, K, V> {
1372     fn clone(&self) -> Values<'a, K, V> {
1373         Values {
1374             inner: self.inner.clone()
1375         }
1376     }
1377 }
1378
1379 /// HashMap drain iterator.
1380 #[stable(feature = "drain", since = "1.6.0")]
1381 pub struct Drain<'a, K: 'a, V: 'a> {
1382     inner: iter::Map<table::Drain<'a, K, V>, fn((SafeHash, K, V)) -> (K, V)>
1383 }
1384
1385 /// A view into a single occupied location in a HashMap.
1386 #[stable(feature = "rust1", since = "1.0.0")]
1387 pub struct OccupiedEntry<'a, K: 'a, V: 'a> {
1388     elem: FullBucket<K, V, &'a mut RawTable<K, V>>,
1389 }
1390
1391 /// A view into a single empty location in a HashMap.
1392 #[stable(feature = "rust1", since = "1.0.0")]
1393 pub struct VacantEntry<'a, K: 'a, V: 'a> {
1394     hash: SafeHash,
1395     key: K,
1396     elem: VacantEntryState<K, V, &'a mut RawTable<K, V>>,
1397 }
1398
1399 /// A view into a single location in a map, which may be vacant or occupied.
1400 #[stable(feature = "rust1", since = "1.0.0")]
1401 pub enum Entry<'a, K: 'a, V: 'a> {
1402     /// An occupied Entry.
1403     #[stable(feature = "rust1", since = "1.0.0")]
1404     Occupied(
1405         #[cfg_attr(not(stage0), stable(feature = "rust1", since = "1.0.0"))] OccupiedEntry<'a, K, V>
1406     ),
1407
1408     /// A vacant Entry.
1409     #[stable(feature = "rust1", since = "1.0.0")]
1410     Vacant(
1411         #[cfg_attr(not(stage0), stable(feature = "rust1", since = "1.0.0"))] VacantEntry<'a, K, V>
1412     ),
1413 }
1414
1415 /// Possible states of a VacantEntry.
1416 enum VacantEntryState<K, V, M> {
1417     /// The index is occupied, but the key to insert has precedence,
1418     /// and will kick the current one out on insertion.
1419     NeqElem(FullBucket<K, V, M>, usize),
1420     /// The index is genuinely vacant.
1421     NoElem(EmptyBucket<K, V, M>),
1422 }
1423
1424 #[stable(feature = "rust1", since = "1.0.0")]
1425 impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
1426     where K: Eq + Hash, S: BuildHasher
1427 {
1428     type Item = (&'a K, &'a V);
1429     type IntoIter = Iter<'a, K, V>;
1430
1431     fn into_iter(self) -> Iter<'a, K, V> {
1432         self.iter()
1433     }
1434 }
1435
1436 #[stable(feature = "rust1", since = "1.0.0")]
1437 impl<'a, K, V, S> IntoIterator for &'a mut HashMap<K, V, S>
1438     where K: Eq + Hash, S: BuildHasher
1439 {
1440     type Item = (&'a K, &'a mut V);
1441     type IntoIter = IterMut<'a, K, V>;
1442
1443     fn into_iter(mut self) -> IterMut<'a, K, V> {
1444         self.iter_mut()
1445     }
1446 }
1447
1448 #[stable(feature = "rust1", since = "1.0.0")]
1449 impl<K, V, S> IntoIterator for HashMap<K, V, S>
1450     where K: Eq + Hash, S: BuildHasher
1451 {
1452     type Item = (K, V);
1453     type IntoIter = IntoIter<K, V>;
1454
1455     /// Creates a consuming iterator, that is, one that moves each key-value
1456     /// pair out of the map in arbitrary order. The map cannot be used after
1457     /// calling this.
1458     ///
1459     /// # Examples
1460     ///
1461     /// ```
1462     /// use std::collections::HashMap;
1463     ///
1464     /// let mut map = HashMap::new();
1465     /// map.insert("a", 1);
1466     /// map.insert("b", 2);
1467     /// map.insert("c", 3);
1468     ///
1469     /// // Not possible with .iter()
1470     /// let vec: Vec<(&str, isize)> = map.into_iter().collect();
1471     /// ```
1472     fn into_iter(self) -> IntoIter<K, V> {
1473         fn last_two<A, B, C>((_, b, c): (A, B, C)) -> (B, C) { (b, c) }
1474         let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two;
1475
1476         IntoIter {
1477             inner: self.table.into_iter().map(last_two)
1478         }
1479     }
1480 }
1481
1482 #[stable(feature = "rust1", since = "1.0.0")]
1483 impl<'a, K, V> Iterator for Iter<'a, K, V> {
1484     type Item = (&'a K, &'a V);
1485
1486     #[inline] fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() }
1487     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1488 }
1489 #[stable(feature = "rust1", since = "1.0.0")]
1490 impl<'a, K, V> ExactSizeIterator for Iter<'a, K, V> {
1491     #[inline] fn len(&self) -> usize { self.inner.len() }
1492 }
1493
1494 #[stable(feature = "rust1", since = "1.0.0")]
1495 impl<'a, K, V> Iterator for IterMut<'a, K, V> {
1496     type Item = (&'a K, &'a mut V);
1497
1498     #[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() }
1499     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1500 }
1501 #[stable(feature = "rust1", since = "1.0.0")]
1502 impl<'a, K, V> ExactSizeIterator for IterMut<'a, K, V> {
1503     #[inline] fn len(&self) -> usize { self.inner.len() }
1504 }
1505
1506 #[stable(feature = "rust1", since = "1.0.0")]
1507 impl<K, V> Iterator for IntoIter<K, V> {
1508     type Item = (K, V);
1509
1510     #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1511     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1512 }
1513 #[stable(feature = "rust1", since = "1.0.0")]
1514 impl<K, V> ExactSizeIterator for IntoIter<K, V> {
1515     #[inline] fn len(&self) -> usize { self.inner.len() }
1516 }
1517
1518 #[stable(feature = "rust1", since = "1.0.0")]
1519 impl<'a, K, V> Iterator for Keys<'a, K, V> {
1520     type Item = &'a K;
1521
1522     #[inline] fn next(&mut self) -> Option<(&'a K)> { self.inner.next() }
1523     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1524 }
1525 #[stable(feature = "rust1", since = "1.0.0")]
1526 impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {
1527     #[inline] fn len(&self) -> usize { self.inner.len() }
1528 }
1529
1530 #[stable(feature = "rust1", since = "1.0.0")]
1531 impl<'a, K, V> Iterator for Values<'a, K, V> {
1532     type Item = &'a V;
1533
1534     #[inline] fn next(&mut self) -> Option<(&'a V)> { self.inner.next() }
1535     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1536 }
1537 #[stable(feature = "rust1", since = "1.0.0")]
1538 impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {
1539     #[inline] fn len(&self) -> usize { self.inner.len() }
1540 }
1541
1542 #[stable(feature = "rust1", since = "1.0.0")]
1543 impl<'a, K, V> Iterator for Drain<'a, K, V> {
1544     type Item = (K, V);
1545
1546     #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() }
1547     #[inline] fn size_hint(&self) -> (usize, Option<usize>) { self.inner.size_hint() }
1548 }
1549 #[stable(feature = "rust1", since = "1.0.0")]
1550 impl<'a, K, V> ExactSizeIterator for Drain<'a, K, V> {
1551     #[inline] fn len(&self) -> usize { self.inner.len() }
1552 }
1553
1554 impl<'a, K, V> Entry<'a, K, V> {
1555     #[stable(feature = "rust1", since = "1.0.0")]
1556     /// Ensures a value is in the entry by inserting the default if empty, and returns
1557     /// a mutable reference to the value in the entry.
1558     pub fn or_insert(self, default: V) -> &'a mut V {
1559         match self {
1560             Occupied(entry) => entry.into_mut(),
1561             Vacant(entry) => entry.insert(default),
1562         }
1563     }
1564
1565     #[stable(feature = "rust1", since = "1.0.0")]
1566     /// Ensures a value is in the entry by inserting the result of the default function if empty,
1567     /// and returns a mutable reference to the value in the entry.
1568     pub fn or_insert_with<F: FnOnce() -> V>(self, default: F) -> &'a mut V {
1569         match self {
1570             Occupied(entry) => entry.into_mut(),
1571             Vacant(entry) => entry.insert(default()),
1572         }
1573     }
1574 }
1575
1576 impl<'a, K, V> OccupiedEntry<'a, K, V> {
1577     /// Gets a reference to the value in the entry.
1578     #[stable(feature = "rust1", since = "1.0.0")]
1579     pub fn get(&self) -> &V {
1580         self.elem.read().1
1581     }
1582
1583     /// Gets a mutable reference to the value in the entry.
1584     #[stable(feature = "rust1", since = "1.0.0")]
1585     pub fn get_mut(&mut self) -> &mut V {
1586         self.elem.read_mut().1
1587     }
1588
1589     /// Converts the OccupiedEntry into a mutable reference to the value in the entry
1590     /// with a lifetime bound to the map itself
1591     #[stable(feature = "rust1", since = "1.0.0")]
1592     pub fn into_mut(self) -> &'a mut V {
1593         self.elem.into_mut_refs().1
1594     }
1595
1596     /// Sets the value of the entry, and returns the entry's old value
1597     #[stable(feature = "rust1", since = "1.0.0")]
1598     pub fn insert(&mut self, mut value: V) -> V {
1599         let old_value = self.get_mut();
1600         mem::swap(&mut value, old_value);
1601         value
1602     }
1603
1604     /// Takes the value out of the entry, and returns it
1605     #[stable(feature = "rust1", since = "1.0.0")]
1606     pub fn remove(self) -> V {
1607         pop_internal(self.elem).1
1608     }
1609 }
1610
1611 impl<'a, K: 'a, V: 'a> VacantEntry<'a, K, V> {
1612     /// Sets the value of the entry with the VacantEntry's key,
1613     /// and returns a mutable reference to it
1614     #[stable(feature = "rust1", since = "1.0.0")]
1615     pub fn insert(self, value: V) -> &'a mut V {
1616         match self.elem {
1617             NeqElem(bucket, ib) => {
1618                 robin_hood(bucket, ib, self.hash, self.key, value)
1619             }
1620             NoElem(bucket) => {
1621                 bucket.put(self.hash, self.key, value).into_mut_refs().1
1622             }
1623         }
1624     }
1625 }
1626
1627 #[stable(feature = "rust1", since = "1.0.0")]
1628 impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
1629     where K: Eq + Hash, S: BuildHasher + Default
1630 {
1631     fn from_iter<T: IntoIterator<Item=(K, V)>>(iterable: T) -> HashMap<K, V, S> {
1632         let iter = iterable.into_iter();
1633         let lower = iter.size_hint().0;
1634         let mut map = HashMap::with_capacity_and_hasher(lower, Default::default());
1635         map.extend(iter);
1636         map
1637     }
1638 }
1639
1640 #[stable(feature = "rust1", since = "1.0.0")]
1641 impl<K, V, S> Extend<(K, V)> for HashMap<K, V, S>
1642     where K: Eq + Hash, S: BuildHasher
1643 {
1644     fn extend<T: IntoIterator<Item=(K, V)>>(&mut self, iter: T) {
1645         for (k, v) in iter {
1646             self.insert(k, v);
1647         }
1648     }
1649 }
1650
1651 #[stable(feature = "hash_extend_copy", since = "1.4.0")]
1652 impl<'a, K, V, S> Extend<(&'a K, &'a V)> for HashMap<K, V, S>
1653     where K: Eq + Hash + Copy, V: Copy, S: BuildHasher
1654 {
1655     fn extend<T: IntoIterator<Item=(&'a K, &'a V)>>(&mut self, iter: T) {
1656         self.extend(iter.into_iter().map(|(&key, &value)| (key, value)));
1657     }
1658 }
1659
1660 /// `RandomState` is the default state for `HashMap` types.
1661 ///
1662 /// A particular instance `RandomState` will create the same instances of
1663 /// `Hasher`, but the hashers created by two different `RandomState`
1664 /// instances are unlikely to produce the same result for the same values.
1665 #[derive(Clone)]
1666 #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
1667 pub struct RandomState {
1668     k0: u64,
1669     k1: u64,
1670 }
1671
1672 impl RandomState {
1673     /// Constructs a new `RandomState` that is initialized with random keys.
1674     #[inline]
1675     #[allow(deprecated)] // rand
1676     #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
1677     pub fn new() -> RandomState {
1678         let mut r = rand::thread_rng();
1679         RandomState { k0: r.gen(), k1: r.gen() }
1680     }
1681 }
1682
1683 #[stable(feature = "hashmap_build_hasher", since = "1.7.0")]
1684 impl BuildHasher for RandomState {
1685     type Hasher = SipHasher;
1686     #[inline]
1687     fn build_hasher(&self) -> SipHasher {
1688         SipHasher::new_with_keys(self.k0, self.k1)
1689     }
1690 }
1691
1692 #[stable(feature = "rust1", since = "1.0.0")]
1693 impl Default for RandomState {
1694     #[inline]
1695     fn default() -> RandomState {
1696         RandomState::new()
1697     }
1698 }
1699
1700 impl<K, S, Q: ?Sized> super::Recover<Q> for HashMap<K, (), S>
1701     where K: Eq + Hash + Borrow<Q>, S: BuildHasher, Q: Eq + Hash
1702 {
1703     type Key = K;
1704
1705     fn get(&self, key: &Q) -> Option<&K> {
1706         self.search(key).map(|bucket| bucket.into_refs().0)
1707     }
1708
1709     fn take(&mut self, key: &Q) -> Option<K> {
1710         if self.table.size() == 0 {
1711             return None
1712         }
1713
1714         self.search_mut(key).map(|bucket| pop_internal(bucket).0)
1715     }
1716
1717     fn replace(&mut self, key: K) -> Option<K> {
1718         let hash = self.make_hash(&key);
1719         self.reserve(1);
1720
1721         let mut retkey = None;
1722         self.insert_or_replace_with(hash, key, (), |key_ref, _, key, _| {
1723             retkey = Some(replace(key_ref, key));
1724         });
1725         retkey
1726     }
1727 }
1728
1729 #[cfg(test)]
1730 mod test_map {
1731     use prelude::v1::*;
1732
1733     use super::HashMap;
1734     use super::Entry::{Occupied, Vacant};
1735     use cell::RefCell;
1736     use rand::{thread_rng, Rng};
1737
1738     #[test]
1739     fn test_create_capacity_zero() {
1740         let mut m = HashMap::with_capacity(0);
1741
1742         assert!(m.insert(1, 1).is_none());
1743
1744         assert!(m.contains_key(&1));
1745         assert!(!m.contains_key(&0));
1746     }
1747
1748     #[test]
1749     fn test_insert() {
1750         let mut m = HashMap::new();
1751         assert_eq!(m.len(), 0);
1752         assert!(m.insert(1, 2).is_none());
1753         assert_eq!(m.len(), 1);
1754         assert!(m.insert(2, 4).is_none());
1755         assert_eq!(m.len(), 2);
1756         assert_eq!(*m.get(&1).unwrap(), 2);
1757         assert_eq!(*m.get(&2).unwrap(), 4);
1758     }
1759
1760     thread_local! { static DROP_VECTOR: RefCell<Vec<isize>> = RefCell::new(Vec::new()) }
1761
1762     #[derive(Hash, PartialEq, Eq)]
1763     struct Dropable {
1764         k: usize
1765     }
1766
1767     impl Dropable {
1768         fn new(k: usize) -> Dropable {
1769             DROP_VECTOR.with(|slot| {
1770                 slot.borrow_mut()[k] += 1;
1771             });
1772
1773             Dropable { k: k }
1774         }
1775     }
1776
1777     impl Drop for Dropable {
1778         fn drop(&mut self) {
1779             DROP_VECTOR.with(|slot| {
1780                 slot.borrow_mut()[self.k] -= 1;
1781             });
1782         }
1783     }
1784
1785     impl Clone for Dropable {
1786         fn clone(&self) -> Dropable {
1787             Dropable::new(self.k)
1788         }
1789     }
1790
1791     #[test]
1792     fn test_drops() {
1793         DROP_VECTOR.with(|slot| {
1794             *slot.borrow_mut() = vec![0; 200];
1795         });
1796
1797         {
1798             let mut m = HashMap::new();
1799
1800             DROP_VECTOR.with(|v| {
1801                 for i in 0..200 {
1802                     assert_eq!(v.borrow()[i], 0);
1803                 }
1804             });
1805
1806             for i in 0..100 {
1807                 let d1 = Dropable::new(i);
1808                 let d2 = Dropable::new(i+100);
1809                 m.insert(d1, d2);
1810             }
1811
1812             DROP_VECTOR.with(|v| {
1813                 for i in 0..200 {
1814                     assert_eq!(v.borrow()[i], 1);
1815                 }
1816             });
1817
1818             for i in 0..50 {
1819                 let k = Dropable::new(i);
1820                 let v = m.remove(&k);
1821
1822                 assert!(v.is_some());
1823
1824                 DROP_VECTOR.with(|v| {
1825                     assert_eq!(v.borrow()[i], 1);
1826                     assert_eq!(v.borrow()[i+100], 1);
1827                 });
1828             }
1829
1830             DROP_VECTOR.with(|v| {
1831                 for i in 0..50 {
1832                     assert_eq!(v.borrow()[i], 0);
1833                     assert_eq!(v.borrow()[i+100], 0);
1834                 }
1835
1836                 for i in 50..100 {
1837                     assert_eq!(v.borrow()[i], 1);
1838                     assert_eq!(v.borrow()[i+100], 1);
1839                 }
1840             });
1841         }
1842
1843         DROP_VECTOR.with(|v| {
1844             for i in 0..200 {
1845                 assert_eq!(v.borrow()[i], 0);
1846             }
1847         });
1848     }
1849
1850     #[test]
1851     fn test_move_iter_drops() {
1852         DROP_VECTOR.with(|v| {
1853             *v.borrow_mut() = vec![0; 200];
1854         });
1855
1856         let hm = {
1857             let mut hm = HashMap::new();
1858
1859             DROP_VECTOR.with(|v| {
1860                 for i in 0..200 {
1861                     assert_eq!(v.borrow()[i], 0);
1862                 }
1863             });
1864
1865             for i in 0..100 {
1866                 let d1 = Dropable::new(i);
1867                 let d2 = Dropable::new(i+100);
1868                 hm.insert(d1, d2);
1869             }
1870
1871             DROP_VECTOR.with(|v| {
1872                 for i in 0..200 {
1873                     assert_eq!(v.borrow()[i], 1);
1874                 }
1875             });
1876
1877             hm
1878         };
1879
1880         // By the way, ensure that cloning doesn't screw up the dropping.
1881         drop(hm.clone());
1882
1883         {
1884             let mut half = hm.into_iter().take(50);
1885
1886             DROP_VECTOR.with(|v| {
1887                 for i in 0..200 {
1888                     assert_eq!(v.borrow()[i], 1);
1889                 }
1890             });
1891
1892             for _ in half.by_ref() {}
1893
1894             DROP_VECTOR.with(|v| {
1895                 let nk = (0..100).filter(|&i| {
1896                     v.borrow()[i] == 1
1897                 }).count();
1898
1899                 let nv = (0..100).filter(|&i| {
1900                     v.borrow()[i+100] == 1
1901                 }).count();
1902
1903                 assert_eq!(nk, 50);
1904                 assert_eq!(nv, 50);
1905             });
1906         };
1907
1908         DROP_VECTOR.with(|v| {
1909             for i in 0..200 {
1910                 assert_eq!(v.borrow()[i], 0);
1911             }
1912         });
1913     }
1914
1915     #[test]
1916     fn test_empty_pop() {
1917         let mut m: HashMap<isize, bool> = HashMap::new();
1918         assert_eq!(m.remove(&0), None);
1919     }
1920
1921     #[test]
1922     fn test_lots_of_insertions() {
1923         let mut m = HashMap::new();
1924
1925         // Try this a few times to make sure we never screw up the hashmap's
1926         // internal state.
1927         for _ in 0..10 {
1928             assert!(m.is_empty());
1929
1930             for i in 1..1001 {
1931                 assert!(m.insert(i, i).is_none());
1932
1933                 for j in 1..i+1 {
1934                     let r = m.get(&j);
1935                     assert_eq!(r, Some(&j));
1936                 }
1937
1938                 for j in i+1..1001 {
1939                     let r = m.get(&j);
1940                     assert_eq!(r, None);
1941                 }
1942             }
1943
1944             for i in 1001..2001 {
1945                 assert!(!m.contains_key(&i));
1946             }
1947
1948             // remove forwards
1949             for i in 1..1001 {
1950                 assert!(m.remove(&i).is_some());
1951
1952                 for j in 1..i+1 {
1953                     assert!(!m.contains_key(&j));
1954                 }
1955
1956                 for j in i+1..1001 {
1957                     assert!(m.contains_key(&j));
1958                 }
1959             }
1960
1961             for i in 1..1001 {
1962                 assert!(!m.contains_key(&i));
1963             }
1964
1965             for i in 1..1001 {
1966                 assert!(m.insert(i, i).is_none());
1967             }
1968
1969             // remove backwards
1970             for i in (1..1001).rev() {
1971                 assert!(m.remove(&i).is_some());
1972
1973                 for j in i..1001 {
1974                     assert!(!m.contains_key(&j));
1975                 }
1976
1977                 for j in 1..i {
1978                     assert!(m.contains_key(&j));
1979                 }
1980             }
1981         }
1982     }
1983
1984     #[test]
1985     fn test_find_mut() {
1986         let mut m = HashMap::new();
1987         assert!(m.insert(1, 12).is_none());
1988         assert!(m.insert(2, 8).is_none());
1989         assert!(m.insert(5, 14).is_none());
1990         let new = 100;
1991         match m.get_mut(&5) {
1992             None => panic!(), Some(x) => *x = new
1993         }
1994         assert_eq!(m.get(&5), Some(&new));
1995     }
1996
1997     #[test]
1998     fn test_insert_overwrite() {
1999         let mut m = HashMap::new();
2000         assert!(m.insert(1, 2).is_none());
2001         assert_eq!(*m.get(&1).unwrap(), 2);
2002         assert!(!m.insert(1, 3).is_none());
2003         assert_eq!(*m.get(&1).unwrap(), 3);
2004     }
2005
2006     #[test]
2007     fn test_insert_conflicts() {
2008         let mut m = HashMap::with_capacity(4);
2009         assert!(m.insert(1, 2).is_none());
2010         assert!(m.insert(5, 3).is_none());
2011         assert!(m.insert(9, 4).is_none());
2012         assert_eq!(*m.get(&9).unwrap(), 4);
2013         assert_eq!(*m.get(&5).unwrap(), 3);
2014         assert_eq!(*m.get(&1).unwrap(), 2);
2015     }
2016
2017     #[test]
2018     fn test_conflict_remove() {
2019         let mut m = HashMap::with_capacity(4);
2020         assert!(m.insert(1, 2).is_none());
2021         assert_eq!(*m.get(&1).unwrap(), 2);
2022         assert!(m.insert(5, 3).is_none());
2023         assert_eq!(*m.get(&1).unwrap(), 2);
2024         assert_eq!(*m.get(&5).unwrap(), 3);
2025         assert!(m.insert(9, 4).is_none());
2026         assert_eq!(*m.get(&1).unwrap(), 2);
2027         assert_eq!(*m.get(&5).unwrap(), 3);
2028         assert_eq!(*m.get(&9).unwrap(), 4);
2029         assert!(m.remove(&1).is_some());
2030         assert_eq!(*m.get(&9).unwrap(), 4);
2031         assert_eq!(*m.get(&5).unwrap(), 3);
2032     }
2033
2034     #[test]
2035     fn test_is_empty() {
2036         let mut m = HashMap::with_capacity(4);
2037         assert!(m.insert(1, 2).is_none());
2038         assert!(!m.is_empty());
2039         assert!(m.remove(&1).is_some());
2040         assert!(m.is_empty());
2041     }
2042
2043     #[test]
2044     fn test_pop() {
2045         let mut m = HashMap::new();
2046         m.insert(1, 2);
2047         assert_eq!(m.remove(&1), Some(2));
2048         assert_eq!(m.remove(&1), None);
2049     }
2050
2051     #[test]
2052     fn test_iterate() {
2053         let mut m = HashMap::with_capacity(4);
2054         for i in 0..32 {
2055             assert!(m.insert(i, i*2).is_none());
2056         }
2057         assert_eq!(m.len(), 32);
2058
2059         let mut observed: u32 = 0;
2060
2061         for (k, v) in &m {
2062             assert_eq!(*v, *k * 2);
2063             observed |= 1 << *k;
2064         }
2065         assert_eq!(observed, 0xFFFF_FFFF);
2066     }
2067
2068     #[test]
2069     fn test_keys() {
2070         let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2071         let map: HashMap<_, _> = vec.into_iter().collect();
2072         let keys: Vec<_> = map.keys().cloned().collect();
2073         assert_eq!(keys.len(), 3);
2074         assert!(keys.contains(&1));
2075         assert!(keys.contains(&2));
2076         assert!(keys.contains(&3));
2077     }
2078
2079     #[test]
2080     fn test_values() {
2081         let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
2082         let map: HashMap<_, _> = vec.into_iter().collect();
2083         let values: Vec<_> = map.values().cloned().collect();
2084         assert_eq!(values.len(), 3);
2085         assert!(values.contains(&'a'));
2086         assert!(values.contains(&'b'));
2087         assert!(values.contains(&'c'));
2088     }
2089
2090     #[test]
2091     fn test_find() {
2092         let mut m = HashMap::new();
2093         assert!(m.get(&1).is_none());
2094         m.insert(1, 2);
2095         match m.get(&1) {
2096             None => panic!(),
2097             Some(v) => assert_eq!(*v, 2)
2098         }
2099     }
2100
2101     #[test]
2102     fn test_eq() {
2103         let mut m1 = HashMap::new();
2104         m1.insert(1, 2);
2105         m1.insert(2, 3);
2106         m1.insert(3, 4);
2107
2108         let mut m2 = HashMap::new();
2109         m2.insert(1, 2);
2110         m2.insert(2, 3);
2111
2112         assert!(m1 != m2);
2113
2114         m2.insert(3, 4);
2115
2116         assert_eq!(m1, m2);
2117     }
2118
2119     #[test]
2120     fn test_show() {
2121         let mut map = HashMap::new();
2122         let empty: HashMap<i32, i32> = HashMap::new();
2123
2124         map.insert(1, 2);
2125         map.insert(3, 4);
2126
2127         let map_str = format!("{:?}", map);
2128
2129         assert!(map_str == "{1: 2, 3: 4}" ||
2130                 map_str == "{3: 4, 1: 2}");
2131         assert_eq!(format!("{:?}", empty), "{}");
2132     }
2133
2134     #[test]
2135     fn test_expand() {
2136         let mut m = HashMap::new();
2137
2138         assert_eq!(m.len(), 0);
2139         assert!(m.is_empty());
2140
2141         let mut i = 0;
2142         let old_cap = m.table.capacity();
2143         while old_cap == m.table.capacity() {
2144             m.insert(i, i);
2145             i += 1;
2146         }
2147
2148         assert_eq!(m.len(), i);
2149         assert!(!m.is_empty());
2150     }
2151
2152     #[test]
2153     fn test_behavior_resize_policy() {
2154         let mut m = HashMap::new();
2155
2156         assert_eq!(m.len(), 0);
2157         assert_eq!(m.table.capacity(), 0);
2158         assert!(m.is_empty());
2159
2160         m.insert(0, 0);
2161         m.remove(&0);
2162         assert!(m.is_empty());
2163         let initial_cap = m.table.capacity();
2164         m.reserve(initial_cap);
2165         let cap = m.table.capacity();
2166
2167         assert_eq!(cap, initial_cap * 2);
2168
2169         let mut i = 0;
2170         for _ in 0..cap * 3 / 4 {
2171             m.insert(i, i);
2172             i += 1;
2173         }
2174         // three quarters full
2175
2176         assert_eq!(m.len(), i);
2177         assert_eq!(m.table.capacity(), cap);
2178
2179         for _ in 0..cap / 4 {
2180             m.insert(i, i);
2181             i += 1;
2182         }
2183         // half full
2184
2185         let new_cap = m.table.capacity();
2186         assert_eq!(new_cap, cap * 2);
2187
2188         for _ in 0..cap / 2 - 1 {
2189             i -= 1;
2190             m.remove(&i);
2191             assert_eq!(m.table.capacity(), new_cap);
2192         }
2193         // A little more than one quarter full.
2194         m.shrink_to_fit();
2195         assert_eq!(m.table.capacity(), cap);
2196         // again, a little more than half full
2197         for _ in 0..cap / 2 - 1 {
2198             i -= 1;
2199             m.remove(&i);
2200         }
2201         m.shrink_to_fit();
2202
2203         assert_eq!(m.len(), i);
2204         assert!(!m.is_empty());
2205         assert_eq!(m.table.capacity(), initial_cap);
2206     }
2207
2208     #[test]
2209     fn test_reserve_shrink_to_fit() {
2210         let mut m = HashMap::new();
2211         m.insert(0, 0);
2212         m.remove(&0);
2213         assert!(m.capacity() >= m.len());
2214         for i in 0..128 {
2215             m.insert(i, i);
2216         }
2217         m.reserve(256);
2218
2219         let usable_cap = m.capacity();
2220         for i in 128..(128 + 256) {
2221             m.insert(i, i);
2222             assert_eq!(m.capacity(), usable_cap);
2223         }
2224
2225         for i in 100..(128 + 256) {
2226             assert_eq!(m.remove(&i), Some(i));
2227         }
2228         m.shrink_to_fit();
2229
2230         assert_eq!(m.len(), 100);
2231         assert!(!m.is_empty());
2232         assert!(m.capacity() >= m.len());
2233
2234         for i in 0..100 {
2235             assert_eq!(m.remove(&i), Some(i));
2236         }
2237         m.shrink_to_fit();
2238         m.insert(0, 0);
2239
2240         assert_eq!(m.len(), 1);
2241         assert!(m.capacity() >= m.len());
2242         assert_eq!(m.remove(&0), Some(0));
2243     }
2244
2245     #[test]
2246     fn test_from_iter() {
2247         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2248
2249         let map: HashMap<_, _> = xs.iter().cloned().collect();
2250
2251         for &(k, v) in &xs {
2252             assert_eq!(map.get(&k), Some(&v));
2253         }
2254     }
2255
2256     #[test]
2257     fn test_size_hint() {
2258         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2259
2260         let map: HashMap<_, _>  = xs.iter().cloned().collect();
2261
2262         let mut iter = map.iter();
2263
2264         for _ in iter.by_ref().take(3) {}
2265
2266         assert_eq!(iter.size_hint(), (3, Some(3)));
2267     }
2268
2269     #[test]
2270     fn test_iter_len() {
2271         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2272
2273         let map: HashMap<_, _>  = xs.iter().cloned().collect();
2274
2275         let mut iter = map.iter();
2276
2277         for _ in iter.by_ref().take(3) {}
2278
2279         assert_eq!(iter.len(), 3);
2280     }
2281
2282     #[test]
2283     fn test_mut_size_hint() {
2284         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2285
2286         let mut map: HashMap<_, _>  = xs.iter().cloned().collect();
2287
2288         let mut iter = map.iter_mut();
2289
2290         for _ in iter.by_ref().take(3) {}
2291
2292         assert_eq!(iter.size_hint(), (3, Some(3)));
2293     }
2294
2295     #[test]
2296     fn test_iter_mut_len() {
2297         let xs = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6)];
2298
2299         let mut map: HashMap<_, _>  = xs.iter().cloned().collect();
2300
2301         let mut iter = map.iter_mut();
2302
2303         for _ in iter.by_ref().take(3) {}
2304
2305         assert_eq!(iter.len(), 3);
2306     }
2307
2308     #[test]
2309     fn test_index() {
2310         let mut map = HashMap::new();
2311
2312         map.insert(1, 2);
2313         map.insert(2, 1);
2314         map.insert(3, 4);
2315
2316         assert_eq!(map[&2], 1);
2317     }
2318
2319     #[test]
2320     #[should_panic]
2321     fn test_index_nonexistent() {
2322         let mut map = HashMap::new();
2323
2324         map.insert(1, 2);
2325         map.insert(2, 1);
2326         map.insert(3, 4);
2327
2328         map[&4];
2329     }
2330
2331     #[test]
2332     fn test_entry(){
2333         let xs = [(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)];
2334
2335         let mut map: HashMap<_, _> = xs.iter().cloned().collect();
2336
2337         // Existing key (insert)
2338         match map.entry(1) {
2339             Vacant(_) => unreachable!(),
2340             Occupied(mut view) => {
2341                 assert_eq!(view.get(), &10);
2342                 assert_eq!(view.insert(100), 10);
2343             }
2344         }
2345         assert_eq!(map.get(&1).unwrap(), &100);
2346         assert_eq!(map.len(), 6);
2347
2348
2349         // Existing key (update)
2350         match map.entry(2) {
2351             Vacant(_) => unreachable!(),
2352             Occupied(mut view) => {
2353                 let v = view.get_mut();
2354                 let new_v = (*v) * 10;
2355                 *v = new_v;
2356             }
2357         }
2358         assert_eq!(map.get(&2).unwrap(), &200);
2359         assert_eq!(map.len(), 6);
2360
2361         // Existing key (take)
2362         match map.entry(3) {
2363             Vacant(_) => unreachable!(),
2364             Occupied(view) => {
2365                 assert_eq!(view.remove(), 30);
2366             }
2367         }
2368         assert_eq!(map.get(&3), None);
2369         assert_eq!(map.len(), 5);
2370
2371
2372         // Inexistent key (insert)
2373         match map.entry(10) {
2374             Occupied(_) => unreachable!(),
2375             Vacant(view) => {
2376                 assert_eq!(*view.insert(1000), 1000);
2377             }
2378         }
2379         assert_eq!(map.get(&10).unwrap(), &1000);
2380         assert_eq!(map.len(), 6);
2381     }
2382
2383     #[test]
2384     fn test_entry_take_doesnt_corrupt() {
2385         #![allow(deprecated)] //rand
2386         // Test for #19292
2387         fn check(m: &HashMap<isize, ()>) {
2388             for k in m.keys() {
2389                 assert!(m.contains_key(k),
2390                         "{} is in keys() but not in the map?", k);
2391             }
2392         }
2393
2394         let mut m = HashMap::new();
2395         let mut rng = thread_rng();
2396
2397         // Populate the map with some items.
2398         for _ in 0..50 {
2399             let x = rng.gen_range(-10, 10);
2400             m.insert(x, ());
2401         }
2402
2403         for i in 0..1000 {
2404             let x = rng.gen_range(-10, 10);
2405             match m.entry(x) {
2406                 Vacant(_) => {},
2407                 Occupied(e) => {
2408                     println!("{}: remove {}", i, x);
2409                     e.remove();
2410                 },
2411             }
2412
2413             check(&m);
2414         }
2415     }
2416
2417     #[test]
2418     fn test_extend_ref() {
2419         let mut a = HashMap::new();
2420         a.insert(1, "one");
2421         let mut b = HashMap::new();
2422         b.insert(2, "two");
2423         b.insert(3, "three");
2424
2425         a.extend(&b);
2426
2427         assert_eq!(a.len(), 3);
2428         assert_eq!(a[&1], "one");
2429         assert_eq!(a[&2], "two");
2430         assert_eq!(a[&3], "three");
2431     }
2432
2433     #[test]
2434     fn test_capacity_not_less_than_len() {
2435         let mut a = HashMap::new();
2436         let mut item = 0;
2437
2438         for _ in 0..116 {
2439             a.insert(item, 0);
2440             item += 1;
2441         }
2442
2443         assert!(a.capacity() > a.len());
2444
2445         let free = a.capacity() - a.len();
2446         for _ in 0..free {
2447             a.insert(item, 0);
2448             item += 1;
2449         }
2450
2451         assert_eq!(a.len(), a.capacity());
2452
2453         // Insert at capacity should cause allocation.
2454         a.insert(item, 0);
2455         assert!(a.capacity() > a.len());
2456     }
2457 }