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