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