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