]> git.lizzy.rs Git - rust.git/blob - src/libstd/collections/hash/table.rs
a83ce04480931d8197a805f557b2b9f5ff87d6d1
[rust.git] / src / libstd / collections / hash / table.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 clone::Clone;
14 use cmp;
15 use hash::{Hash, Hasher};
16 use iter::{Iterator, count};
17 use kinds::{Sized, marker};
18 use mem::{min_align_of, size_of};
19 use mem;
20 use num::{Int, UnsignedInt};
21 use ops::{Deref, DerefMut, Drop};
22 use option::{Some, None, Option};
23 use ptr::{RawPtr, copy_nonoverlapping_memory, zero_memory};
24 use ptr;
25 use rt::heap::{allocate, deallocate};
26
27 const EMPTY_BUCKET: u64 = 0u64;
28
29 /// The raw hashtable, providing safe-ish access to the unzipped and highly
30 /// optimized arrays of hashes, keys, and values.
31 ///
32 /// This design uses less memory and is a lot faster than the naive
33 /// `Vec<Option<u64, K, V>>`, because we don't pay for the overhead of an
34 /// option on every element, and we get a generally more cache-aware design.
35 ///
36 /// Essential invariants of this structure:
37 ///
38 ///   - if t.hashes[i] == EMPTY_BUCKET, then `Bucket::at_index(&t, i).raw`
39 ///     points to 'undefined' contents. Don't read from it. This invariant is
40 ///     enforced outside this module with the `EmptyBucket`, `FullBucket`,
41 ///     and `SafeHash` types.
42 ///
43 ///   - An `EmptyBucket` is only constructed at an index with
44 ///     a hash of EMPTY_BUCKET.
45 ///
46 ///   - A `FullBucket` is only constructed at an index with a
47 ///     non-EMPTY_BUCKET hash.
48 ///
49 ///   - A `SafeHash` is only constructed for non-`EMPTY_BUCKET` hash. We get
50 ///     around hashes of zero by changing them to 0x8000_0000_0000_0000,
51 ///     which will likely map to the same bucket, while not being confused
52 ///     with "empty".
53 ///
54 ///   - All three "arrays represented by pointers" are the same length:
55 ///     `capacity`. This is set at creation and never changes. The arrays
56 ///     are unzipped to save space (we don't have to pay for the padding
57 ///     between odd sized elements, such as in a map from u64 to u8), and
58 ///     be more cache aware (scanning through 8 hashes brings in at most
59 ///     2 cache lines, since they're all right beside each other).
60 ///
61 /// You can kind of think of this module/data structure as a safe wrapper
62 /// around just the "table" part of the hashtable. It enforces some
63 /// invariants at the type level and employs some performance trickery,
64 /// but in general is just a tricked out `Vec<Option<u64, K, V>>`.
65 #[unsafe_no_drop_flag]
66 pub struct RawTable<K, V> {
67     capacity: uint,
68     size:     uint,
69     hashes:   *mut u64,
70     // Because K/V do not appear directly in any of the types in the struct,
71     // inform rustc that in fact instances of K and V are reachable from here.
72     marker:   marker::CovariantType<(K,V)>,
73 }
74
75 struct RawBucket<K, V> {
76     hash: *mut u64,
77     key:  *mut K,
78     val:  *mut V
79 }
80
81 pub struct Bucket<K, V, M> {
82     raw:   RawBucket<K, V>,
83     idx:   uint,
84     table: M
85 }
86
87 pub struct EmptyBucket<K, V, M> {
88     raw:   RawBucket<K, V>,
89     idx:   uint,
90     table: M
91 }
92
93 pub struct FullBucket<K, V, M> {
94     raw:   RawBucket<K, V>,
95     idx:   uint,
96     table: M
97 }
98
99 pub type EmptyBucketImm<'table, K, V> = EmptyBucket<K, V, &'table RawTable<K, V>>;
100 pub type  FullBucketImm<'table, K, V> =  FullBucket<K, V, &'table RawTable<K, V>>;
101
102 pub type EmptyBucketMut<'table, K, V> = EmptyBucket<K, V, &'table mut RawTable<K, V>>;
103 pub type  FullBucketMut<'table, K, V> =  FullBucket<K, V, &'table mut RawTable<K, V>>;
104
105 pub enum BucketState<K, V, M> {
106     Empty(EmptyBucket<K, V, M>),
107     Full(FullBucket<K, V, M>),
108 }
109
110 // A GapThenFull encapsulates the state of two consecutive buckets at once.
111 // The first bucket, called the gap, is known to be empty.
112 // The second bucket is full.
113 struct GapThenFull<K, V, M> {
114     gap: EmptyBucket<K, V, ()>,
115     full: FullBucket<K, V, M>,
116 }
117
118 /// A hash that is not zero, since we use a hash of zero to represent empty
119 /// buckets.
120 #[deriving(PartialEq)]
121 pub struct SafeHash {
122     hash: u64,
123 }
124
125 impl SafeHash {
126     /// Peek at the hash value, which is guaranteed to be non-zero.
127     #[inline(always)]
128     pub fn inspect(&self) -> u64 { self.hash }
129 }
130
131 /// We need to remove hashes of 0. That's reserved for empty buckets.
132 /// This function wraps up `hash_keyed` to be the only way outside this
133 /// module to generate a SafeHash.
134 pub fn make_hash<Sized? T: Hash<S>, S, H: Hasher<S>>(hasher: &H, t: &T) -> SafeHash {
135     match hasher.hash(t) {
136         // This constant is exceedingly likely to hash to the same
137         // bucket, but it won't be counted as empty! Just so we can maintain
138         // our precious uniform distribution of initial indexes.
139         EMPTY_BUCKET => SafeHash { hash: 0x8000_0000_0000_0000 },
140         h            => SafeHash { hash: h },
141     }
142 }
143
144 // `replace` casts a `*u64` to a `*SafeHash`. Since we statically
145 // ensure that a `FullBucket` points to an index with a non-zero hash,
146 // and a `SafeHash` is just a `u64` with a different name, this is
147 // safe.
148 //
149 // This test ensures that a `SafeHash` really IS the same size as a
150 // `u64`. If you need to change the size of `SafeHash` (and
151 // consequently made this test fail), `replace` needs to be
152 // modified to no longer assume this.
153 #[test]
154 fn can_alias_safehash_as_u64() {
155     assert_eq!(size_of::<SafeHash>(), size_of::<u64>())
156 }
157
158 impl<K, V> RawBucket<K, V> {
159     unsafe fn offset(self, count: int) -> RawBucket<K, V> {
160         RawBucket {
161             hash: self.hash.offset(count),
162             key:  self.key.offset(count),
163             val:  self.val.offset(count),
164         }
165     }
166 }
167
168 // Buckets hold references to the table.
169 impl<K, V, M> FullBucket<K, V, M> {
170     /// Borrow a reference to the table.
171     pub fn table(&self) -> &M {
172         &self.table
173     }
174     /// Move out the reference to the table.
175     pub fn into_table(self) -> M {
176         self.table
177     }
178     /// Get the raw index.
179     pub fn index(&self) -> uint {
180         self.idx
181     }
182 }
183
184 impl<K, V, M> EmptyBucket<K, V, M> {
185     /// Borrow a reference to the table.
186     pub fn table(&self) -> &M {
187         &self.table
188     }
189     /// Move out the reference to the table.
190     pub fn into_table(self) -> M {
191         self.table
192     }
193 }
194
195 impl<K, V, M> Bucket<K, V, M> {
196     /// Move out the reference to the table.
197     pub fn into_table(self) -> M {
198         self.table
199     }
200     /// Get the raw index.
201     pub fn index(&self) -> uint {
202         self.idx
203     }
204 }
205
206 impl<K, V, M: Deref<RawTable<K, V>>> Bucket<K, V, M> {
207     pub fn new(table: M, hash: &SafeHash) -> Bucket<K, V, M> {
208         Bucket::at_index(table, hash.inspect() as uint)
209     }
210
211     pub fn at_index(table: M, ib_index: uint) -> Bucket<K, V, M> {
212         let ib_index = ib_index & (table.capacity() - 1);
213         Bucket {
214             raw: unsafe {
215                table.first_bucket_raw().offset(ib_index as int)
216             },
217             idx: ib_index,
218             table: table
219         }
220     }
221
222     pub fn first(table: M) -> Bucket<K, V, M> {
223         Bucket {
224             raw: table.first_bucket_raw(),
225             idx: 0,
226             table: table
227         }
228     }
229
230     /// Reads a bucket at a given index, returning an enum indicating whether
231     /// it's initialized or not. You need to match on this enum to get
232     /// the appropriate types to call most of the other functions in
233     /// this module.
234     pub fn peek(self) -> BucketState<K, V, M> {
235         match unsafe { *self.raw.hash } {
236             EMPTY_BUCKET =>
237                 Empty(EmptyBucket {
238                     raw: self.raw,
239                     idx: self.idx,
240                     table: self.table
241                 }),
242             _ =>
243                 Full(FullBucket {
244                     raw: self.raw,
245                     idx: self.idx,
246                     table: self.table
247                 })
248         }
249     }
250
251     /// Modifies the bucket pointer in place to make it point to the next slot.
252     pub fn next(&mut self) {
253         // Branchless bucket iteration step.
254         // As we reach the end of the table...
255         // We take the current idx:          0111111b
256         // Xor it by its increment:        ^ 1000000b
257         //                               ------------
258         //                                   1111111b
259         // Then AND with the capacity:     & 1000000b
260         //                               ------------
261         // to get the backwards offset:      1000000b
262         // ... and it's zero at all other times.
263         let maybe_wraparound_dist = (self.idx ^ (self.idx + 1)) & self.table.capacity();
264         // Finally, we obtain the offset 1 or the offset -cap + 1.
265         let dist = 1i - (maybe_wraparound_dist as int);
266
267         self.idx += 1;
268
269         unsafe {
270             self.raw = self.raw.offset(dist);
271         }
272     }
273 }
274
275 impl<K, V, M: Deref<RawTable<K, V>>> EmptyBucket<K, V, M> {
276     #[inline]
277     pub fn next(self) -> Bucket<K, V, M> {
278         let mut bucket = self.into_bucket();
279         bucket.next();
280         bucket
281     }
282
283     #[inline]
284     pub fn into_bucket(self) -> Bucket<K, V, M> {
285         Bucket {
286             raw: self.raw,
287             idx: self.idx,
288             table: self.table
289         }
290     }
291
292     pub fn gap_peek(self) -> Option<GapThenFull<K, V, M>> {
293         let gap = EmptyBucket {
294             raw: self.raw,
295             idx: self.idx,
296             table: ()
297         };
298
299         match self.next().peek() {
300             Full(bucket) => {
301                 Some(GapThenFull {
302                     gap: gap,
303                     full: bucket
304                 })
305             }
306             Empty(..) => None
307         }
308     }
309 }
310
311 impl<K, V, M: DerefMut<RawTable<K, V>>> EmptyBucket<K, V, M> {
312     /// Puts given key and value pair, along with the key's hash,
313     /// into this bucket in the hashtable. Note how `self` is 'moved' into
314     /// this function, because this slot will no longer be empty when
315     /// we return! A `FullBucket` is returned for later use, pointing to
316     /// the newly-filled slot in the hashtable.
317     ///
318     /// Use `make_hash` to construct a `SafeHash` to pass to this function.
319     pub fn put(mut self, hash: SafeHash, key: K, value: V)
320                -> FullBucket<K, V, M> {
321         unsafe {
322             *self.raw.hash = hash.inspect();
323             ptr::write(self.raw.key, key);
324             ptr::write(self.raw.val, value);
325         }
326
327         self.table.size += 1;
328
329         FullBucket { raw: self.raw, idx: self.idx, table: self.table }
330     }
331 }
332
333 impl<K, V, M: Deref<RawTable<K, V>>> FullBucket<K, V, M> {
334     #[inline]
335     pub fn next(self) -> Bucket<K, V, M> {
336         let mut bucket = self.into_bucket();
337         bucket.next();
338         bucket
339     }
340
341     #[inline]
342     pub fn into_bucket(self) -> Bucket<K, V, M> {
343         Bucket {
344             raw: self.raw,
345             idx: self.idx,
346             table: self.table
347         }
348     }
349
350     /// Get the distance between this bucket and the 'ideal' location
351     /// as determined by the key's hash stored in it.
352     ///
353     /// In the cited blog posts above, this is called the "distance to
354     /// initial bucket", or DIB. Also known as "probe count".
355     pub fn distance(&self) -> uint {
356         // Calculates the distance one has to travel when going from
357         // `hash mod capacity` onwards to `idx mod capacity`, wrapping around
358         // if the destination is not reached before the end of the table.
359         (self.idx - self.hash().inspect() as uint) & (self.table.capacity() - 1)
360     }
361
362     #[inline]
363     pub fn hash(&self) -> SafeHash {
364         unsafe {
365             SafeHash {
366                 hash: *self.raw.hash
367             }
368         }
369     }
370
371     /// Gets references to the key and value at a given index.
372     pub fn read(&self) -> (&K, &V) {
373         unsafe {
374             (&*self.raw.key,
375              &*self.raw.val)
376         }
377     }
378 }
379
380 impl<K, V, M: DerefMut<RawTable<K, V>>> FullBucket<K, V, M> {
381     /// Removes this bucket's key and value from the hashtable.
382     ///
383     /// This works similarly to `put`, building an `EmptyBucket` out of the
384     /// taken bucket.
385     pub fn take(mut self) -> (EmptyBucket<K, V, M>, K, V) {
386         let key = self.raw.key as *const K;
387         let val = self.raw.val as *const V;
388
389         self.table.size -= 1;
390
391         unsafe {
392             *self.raw.hash = EMPTY_BUCKET;
393             (
394                 EmptyBucket {
395                     raw: self.raw,
396                     idx: self.idx,
397                     table: self.table
398                 },
399                 ptr::read(key),
400                 ptr::read(val)
401             )
402         }
403     }
404
405     pub fn replace(&mut self, h: SafeHash, k: K, v: V) -> (SafeHash, K, V) {
406         unsafe {
407             let old_hash = ptr::replace(self.raw.hash as *mut SafeHash, h);
408             let old_key  = ptr::replace(self.raw.key,  k);
409             let old_val  = ptr::replace(self.raw.val,  v);
410
411             (old_hash, old_key, old_val)
412         }
413     }
414
415     /// Gets mutable references to the key and value at a given index.
416     pub fn read_mut(&mut self) -> (&mut K, &mut V) {
417         unsafe {
418             (&mut *self.raw.key,
419              &mut *self.raw.val)
420         }
421     }
422 }
423
424 impl<'t, K, V, M: Deref<RawTable<K, V>> + 't> FullBucket<K, V, M> {
425     /// Exchange a bucket state for immutable references into the table.
426     /// Because the underlying reference to the table is also consumed,
427     /// no further changes to the structure of the table are possible;
428     /// in exchange for this, the returned references have a longer lifetime
429     /// than the references returned by `read()`.
430     pub fn into_refs(self) -> (&'t K, &'t V) {
431         unsafe {
432             (&*self.raw.key,
433              &*self.raw.val)
434         }
435     }
436 }
437
438 impl<'t, K, V, M: DerefMut<RawTable<K, V>> + 't> FullBucket<K, V, M> {
439     /// This works similarly to `into_refs`, exchanging a bucket state
440     /// for mutable references into the table.
441     pub fn into_mut_refs(self) -> (&'t mut K, &'t mut V) {
442         unsafe {
443             (&mut *self.raw.key,
444              &mut *self.raw.val)
445         }
446     }
447 }
448
449 impl<K, V, M> BucketState<K, V, M> {
450     // For convenience.
451     pub fn expect_full(self) -> FullBucket<K, V, M> {
452         match self {
453             Full(full) => full,
454             Empty(..) => panic!("Expected full bucket")
455         }
456     }
457 }
458
459 impl<K, V, M: Deref<RawTable<K, V>>> GapThenFull<K, V, M> {
460     #[inline]
461     pub fn full(&self) -> &FullBucket<K, V, M> {
462         &self.full
463     }
464
465     pub fn shift(mut self) -> Option<GapThenFull<K, V, M>> {
466         unsafe {
467             *self.gap.raw.hash = mem::replace(&mut *self.full.raw.hash, EMPTY_BUCKET);
468             copy_nonoverlapping_memory(self.gap.raw.key, self.full.raw.key as *const K, 1);
469             copy_nonoverlapping_memory(self.gap.raw.val, self.full.raw.val as *const V, 1);
470         }
471
472         let FullBucket { raw: prev_raw, idx: prev_idx, .. } = self.full;
473
474         match self.full.next().peek() {
475             Full(bucket) => {
476                 self.gap.raw = prev_raw;
477                 self.gap.idx = prev_idx;
478
479                 self.full = bucket;
480
481                 Some(self)
482             }
483             Empty(..) => None
484         }
485     }
486 }
487
488
489 /// Rounds up to a multiple of a power of two. Returns the closest multiple
490 /// of `target_alignment` that is higher or equal to `unrounded`.
491 ///
492 /// # Panics
493 ///
494 /// Panics if `target_alignment` is not a power of two.
495 fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint {
496     assert!(target_alignment.is_power_of_two());
497     (unrounded + target_alignment - 1) & !(target_alignment - 1)
498 }
499
500 #[test]
501 fn test_rounding() {
502     assert_eq!(round_up_to_next(0, 4), 0);
503     assert_eq!(round_up_to_next(1, 4), 4);
504     assert_eq!(round_up_to_next(2, 4), 4);
505     assert_eq!(round_up_to_next(3, 4), 4);
506     assert_eq!(round_up_to_next(4, 4), 4);
507     assert_eq!(round_up_to_next(5, 4), 8);
508 }
509
510 // Returns a tuple of (key_offset, val_offset),
511 // from the start of a mallocated array.
512 fn calculate_offsets(hashes_size: uint,
513                      keys_size: uint, keys_align: uint,
514                      vals_align: uint)
515                      -> (uint, uint) {
516     let keys_offset = round_up_to_next(hashes_size, keys_align);
517     let end_of_keys = keys_offset + keys_size;
518
519     let vals_offset = round_up_to_next(end_of_keys, vals_align);
520
521     (keys_offset, vals_offset)
522 }
523
524 // Returns a tuple of (minimum required malloc alignment, hash_offset,
525 // array_size), from the start of a mallocated array.
526 fn calculate_allocation(hash_size: uint, hash_align: uint,
527                         keys_size: uint, keys_align: uint,
528                         vals_size: uint, vals_align: uint)
529                         -> (uint, uint, uint) {
530     let hash_offset = 0;
531     let (_, vals_offset) = calculate_offsets(hash_size,
532                                              keys_size, keys_align,
533                                                         vals_align);
534     let end_of_vals = vals_offset + vals_size;
535
536     let min_align = cmp::max(hash_align, cmp::max(keys_align, vals_align));
537
538     (min_align, hash_offset, end_of_vals)
539 }
540
541 #[test]
542 fn test_offset_calculation() {
543     assert_eq!(calculate_allocation(128, 8, 15, 1, 4,  4), (8, 0, 148));
544     assert_eq!(calculate_allocation(3,   1, 2,  1, 1,  1), (1, 0, 6));
545     assert_eq!(calculate_allocation(6,   2, 12, 4, 24, 8), (8, 0, 48));
546     assert_eq!(calculate_offsets(128, 15, 1, 4), (128, 144));
547     assert_eq!(calculate_offsets(3,   2,  1, 1), (3,   5));
548     assert_eq!(calculate_offsets(6,   12, 4, 8), (8,   24));
549 }
550
551 impl<K, V> RawTable<K, V> {
552     /// Does not initialize the buckets. The caller should ensure they,
553     /// at the very least, set every hash to EMPTY_BUCKET.
554     unsafe fn new_uninitialized(capacity: uint) -> RawTable<K, V> {
555         if capacity == 0 {
556             return RawTable {
557                 size: 0,
558                 capacity: 0,
559                 hashes: 0 as *mut u64,
560                 marker: marker::CovariantType,
561             };
562         }
563         // No need for `checked_mul` before a more restrictive check performed
564         // later in this method.
565         let hashes_size = capacity * size_of::<u64>();
566         let keys_size   = capacity * size_of::< K >();
567         let vals_size   = capacity * size_of::< V >();
568
569         // Allocating hashmaps is a little tricky. We need to allocate three
570         // arrays, but since we know their sizes and alignments up front,
571         // we just allocate a single array, and then have the subarrays
572         // point into it.
573         //
574         // This is great in theory, but in practice getting the alignment
575         // right is a little subtle. Therefore, calculating offsets has been
576         // factored out into a different function.
577         let (malloc_alignment, hash_offset, size) =
578             calculate_allocation(
579                 hashes_size, min_align_of::<u64>(),
580                 keys_size,   min_align_of::< K >(),
581                 vals_size,   min_align_of::< V >());
582
583         // One check for overflow that covers calculation and rounding of size.
584         let size_of_bucket = size_of::<u64>().checked_add(size_of::<K>()).unwrap()
585                                              .checked_add(size_of::<V>()).unwrap();
586         assert!(size >= capacity.checked_mul(size_of_bucket)
587                                 .expect("capacity overflow"),
588                 "capacity overflow");
589
590         let buffer = allocate(size, malloc_alignment);
591         if buffer.is_null() { ::alloc::oom() }
592
593         let hashes = buffer.offset(hash_offset as int) as *mut u64;
594
595         RawTable {
596             capacity: capacity,
597             size:     0,
598             hashes:   hashes,
599             marker:   marker::CovariantType,
600         }
601     }
602
603     fn first_bucket_raw(&self) -> RawBucket<K, V> {
604         let hashes_size = self.capacity * size_of::<u64>();
605         let keys_size = self.capacity * size_of::<K>();
606
607         let buffer = self.hashes as *mut u8;
608         let (keys_offset, vals_offset) = calculate_offsets(hashes_size,
609                                                            keys_size, min_align_of::<K>(),
610                                                            min_align_of::<V>());
611
612         unsafe {
613             RawBucket {
614                 hash: self.hashes,
615                 key:  buffer.offset(keys_offset as int) as *mut K,
616                 val:  buffer.offset(vals_offset as int) as *mut V
617             }
618         }
619     }
620
621     /// Creates a new raw table from a given capacity. All buckets are
622     /// initially empty.
623     #[allow(experimental)]
624     pub fn new(capacity: uint) -> RawTable<K, V> {
625         unsafe {
626             let ret = RawTable::new_uninitialized(capacity);
627             zero_memory(ret.hashes, capacity);
628             ret
629         }
630     }
631
632     /// The hashtable's capacity, similar to a vector's.
633     pub fn capacity(&self) -> uint {
634         self.capacity
635     }
636
637     /// The number of elements ever `put` in the hashtable, minus the number
638     /// of elements ever `take`n.
639     pub fn size(&self) -> uint {
640         self.size
641     }
642
643     fn raw_buckets(&self) -> RawBuckets<K, V> {
644         RawBuckets {
645             raw: self.first_bucket_raw(),
646             hashes_end: unsafe {
647                 self.hashes.offset(self.capacity as int)
648             },
649             marker: marker::ContravariantLifetime,
650         }
651     }
652
653     pub fn iter(&self) -> Entries<K, V> {
654         Entries {
655             iter: self.raw_buckets(),
656             elems_left: self.size(),
657         }
658     }
659
660     pub fn iter_mut(&mut self) -> MutEntries<K, V> {
661         MutEntries {
662             iter: self.raw_buckets(),
663             elems_left: self.size(),
664         }
665     }
666
667     pub fn into_iter(self) -> MoveEntries<K, V> {
668         let RawBuckets { raw, hashes_end, .. } = self.raw_buckets();
669         // Replace the marker regardless of lifetime bounds on parameters.
670         MoveEntries {
671             iter: RawBuckets {
672                 raw: raw,
673                 hashes_end: hashes_end,
674                 marker: marker::ContravariantLifetime,
675             },
676             table: self,
677         }
678     }
679
680     /// Returns an iterator that copies out each entry. Used while the table
681     /// is being dropped.
682     unsafe fn rev_move_buckets(&mut self) -> RevMoveBuckets<K, V> {
683         let raw_bucket = self.first_bucket_raw();
684         RevMoveBuckets {
685             raw: raw_bucket.offset(self.capacity as int),
686             hashes_end: raw_bucket.hash,
687             elems_left: self.size,
688             marker:     marker::ContravariantLifetime,
689         }
690     }
691 }
692
693 /// A raw iterator. The basis for some other iterators in this module. Although
694 /// this interface is safe, it's not used outside this module.
695 struct RawBuckets<'a, K, V> {
696     raw: RawBucket<K, V>,
697     hashes_end: *mut u64,
698     marker: marker::ContravariantLifetime<'a>,
699 }
700
701 impl<'a, K, V> Iterator<RawBucket<K, V>> for RawBuckets<'a, K, V> {
702     fn next(&mut self) -> Option<RawBucket<K, V>> {
703         while self.raw.hash != self.hashes_end {
704             unsafe {
705                 // We are swapping out the pointer to a bucket and replacing
706                 // it with the pointer to the next one.
707                 let prev = ptr::replace(&mut self.raw, self.raw.offset(1));
708                 if *prev.hash != EMPTY_BUCKET {
709                     return Some(prev);
710                 }
711             }
712         }
713
714         None
715     }
716 }
717
718 /// An iterator that moves out buckets in reverse order. It leaves the table
719 /// in an inconsistent state and should only be used for dropping
720 /// the table's remaining entries. It's used in the implementation of Drop.
721 struct RevMoveBuckets<'a, K, V> {
722     raw: RawBucket<K, V>,
723     hashes_end: *mut u64,
724     elems_left: uint,
725     marker: marker::ContravariantLifetime<'a>,
726 }
727
728 impl<'a, K, V> Iterator<(K, V)> for RevMoveBuckets<'a, K, V> {
729     fn next(&mut self) -> Option<(K, V)> {
730         if self.elems_left == 0 {
731             return None;
732         }
733
734         loop {
735             debug_assert!(self.raw.hash != self.hashes_end);
736
737             unsafe {
738                 self.raw = self.raw.offset(-1);
739
740                 if *self.raw.hash != EMPTY_BUCKET {
741                     self.elems_left -= 1;
742                     return Some((
743                         ptr::read(self.raw.key as *const K),
744                         ptr::read(self.raw.val as *const V)
745                     ));
746                 }
747             }
748         }
749     }
750 }
751
752 /// Iterator over shared references to entries in a table.
753 pub struct Entries<'a, K: 'a, V: 'a> {
754     iter: RawBuckets<'a, K, V>,
755     elems_left: uint,
756 }
757
758 /// Iterator over mutable references to entries in a table.
759 pub struct MutEntries<'a, K: 'a, V: 'a> {
760     iter: RawBuckets<'a, K, V>,
761     elems_left: uint,
762 }
763
764 /// Iterator over the entries in a table, consuming the table.
765 pub struct MoveEntries<K, V> {
766     table: RawTable<K, V>,
767     iter: RawBuckets<'static, K, V>
768 }
769
770 impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
771     fn next(&mut self) -> Option<(&'a K, &'a V)> {
772         self.iter.next().map(|bucket| {
773             self.elems_left -= 1;
774             unsafe {
775                 (&*bucket.key,
776                  &*bucket.val)
777             }
778         })
779     }
780
781     fn size_hint(&self) -> (uint, Option<uint>) {
782         (self.elems_left, Some(self.elems_left))
783     }
784 }
785
786 impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
787     fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
788         self.iter.next().map(|bucket| {
789             self.elems_left -= 1;
790             unsafe {
791                 (&*bucket.key,
792                  &mut *bucket.val)
793             }
794         })
795     }
796
797     fn size_hint(&self) -> (uint, Option<uint>) {
798         (self.elems_left, Some(self.elems_left))
799     }
800 }
801
802 impl<K, V> Iterator<(SafeHash, K, V)> for MoveEntries<K, V> {
803     fn next(&mut self) -> Option<(SafeHash, K, V)> {
804         self.iter.next().map(|bucket| {
805             self.table.size -= 1;
806             unsafe {
807                 (
808                     SafeHash {
809                         hash: *bucket.hash,
810                     },
811                     ptr::read(bucket.key as *const K),
812                     ptr::read(bucket.val as *const V)
813                 )
814             }
815         })
816     }
817
818     fn size_hint(&self) -> (uint, Option<uint>) {
819         let size = self.table.size();
820         (size, Some(size))
821     }
822 }
823
824 impl<K: Clone, V: Clone> Clone for RawTable<K, V> {
825     fn clone(&self) -> RawTable<K, V> {
826         unsafe {
827             let mut new_ht = RawTable::new_uninitialized(self.capacity());
828
829             {
830                 let cap = self.capacity();
831                 let mut new_buckets = Bucket::first(&mut new_ht);
832                 let mut buckets = Bucket::first(self);
833                 while buckets.index() != cap {
834                     match buckets.peek() {
835                         Full(full) => {
836                             let (h, k, v) = {
837                                 let (k, v) = full.read();
838                                 (full.hash(), k.clone(), v.clone())
839                             };
840                             *new_buckets.raw.hash = h.inspect();
841                             ptr::write(new_buckets.raw.key, k);
842                             ptr::write(new_buckets.raw.val, v);
843                         }
844                         Empty(..) => {
845                             *new_buckets.raw.hash = EMPTY_BUCKET;
846                         }
847                     }
848                     new_buckets.next();
849                     buckets.next();
850                 }
851             };
852
853             new_ht.size = self.size();
854
855             new_ht
856         }
857     }
858 }
859
860 #[unsafe_destructor]
861 impl<K, V> Drop for RawTable<K, V> {
862     fn drop(&mut self) {
863         if self.hashes.is_null() {
864             return;
865         }
866         // This is done in reverse because we've likely partially taken
867         // some elements out with `.into_iter()` from the front.
868         // Check if the size is 0, so we don't do a useless scan when
869         // dropping empty tables such as on resize.
870         // Also avoid double drop of elements that have been already moved out.
871         unsafe {
872             for _ in self.rev_move_buckets() {}
873         }
874
875         let hashes_size = self.capacity * size_of::<u64>();
876         let keys_size = self.capacity * size_of::<K>();
877         let vals_size = self.capacity * size_of::<V>();
878         let (align, _, size) = calculate_allocation(hashes_size, min_align_of::<u64>(),
879                                                     keys_size, min_align_of::<K>(),
880                                                     vals_size, min_align_of::<V>());
881
882         unsafe {
883             deallocate(self.hashes as *mut u8, size, align);
884             // Remember how everything was allocated out of one buffer
885             // during initialization? We only need one call to free here.
886         }
887     }
888 }