Skip to content

Commit 6c895d1

Browse files
committed
auto merge of #12864 : huonw/rust/hash-docs, r=alexcrichton
collections: move hashmap's example to the struct. Most people go straight to the struct, not looking at the module, so the example was well hidden.
2 parents b35e8fb + d062669 commit 6c895d1

File tree

1 file changed

+53
-53
lines changed

1 file changed

+53
-53
lines changed

src/libcollections/hashmap.rs

Lines changed: 53 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,6 @@
99
// except according to those terms.
1010

1111
//! Unordered containers, implemented as hash-tables (`HashSet` and `HashMap` types)
12-
//!
13-
//! The tables use a keyed hash with new random keys generated for each container, so the ordering
14-
//! of a set of keys in a hash table is randomized.
15-
//!
16-
//! This is currently implemented with robin hood hashing, as described in [1][2][3].
17-
//!
18-
//! # Example
19-
//!
20-
//! ```rust
21-
//! use collections::HashMap;
22-
//!
23-
//! // type inference lets us omit an explicit type signature (which
24-
//! // would be `HashMap<&str, &str>` in this example).
25-
//! let mut book_reviews = HashMap::new();
26-
//!
27-
//! // review some books.
28-
//! book_reviews.insert("Adventures of Hucklebury Fin", "My favorite book.");
29-
//! book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
30-
//! book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
31-
//! book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
32-
//!
33-
//! // check for a specific one.
34-
//! if !book_reviews.contains_key(& &"Les Misérables") {
35-
//! println!("We've got {} reviews, but Les Misérables ain't one.",
36-
//! book_reviews.len());
37-
//! }
38-
//!
39-
//! // oops, this review has a lot of spelling mistakes, let's delete it.
40-
//! book_reviews.remove(& &"The Adventures of Sherlock Holmes");
41-
//!
42-
//! // look up the values associated with some keys.
43-
//! let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
44-
//! for book in to_find.iter() {
45-
//! match book_reviews.find(book) {
46-
//! Some(review) => println!("{}: {}", *book, *review),
47-
//! None => println!("{} is unreviewed.", *book)
48-
//! }
49-
//! }
50-
//!
51-
//! // iterate over everything.
52-
//! for (book, review) in book_reviews.iter() {
53-
//! println!("{}: \"{}\"", *book, *review);
54-
//! }
55-
//! ```
56-
//!
57-
//! Relevant papers/articles:
58-
//!
59-
//! [1]: Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
60-
//! [2]: (http://codecapsule.com/2013/11/11/robin-hood-hashing/)
61-
//! [3]: (http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
6212
6313
use std::container::{Container, Mutable, Map, MutableMap, Set, MutableSet};
6414
use std::clone::Clone;
@@ -667,14 +617,64 @@ static INITIAL_LOAD_FACTOR: Fraction = (9, 10);
667617
// `table::RawTable::new`, but I'm not confident it works for all sane alignments,
668618
// especially if a type needs more alignment than `malloc` provides.
669619

670-
/// A hash map implementation which uses linear probing with Robin Hood bucket
671-
/// stealing.
620+
/// A hash map implementation which uses linear probing with Robin
621+
/// Hood bucket stealing.
672622
///
673623
/// The hashes are all keyed by the task-local random number generator
674-
/// on creation by default. This can be overriden with one of the constructors.
624+
/// on creation by default, this means the ordering of the keys is
625+
/// randomized, but makes the tables more resistant to
626+
/// denial-of-service attacks (Hash DoS). This behaviour can be
627+
/// overriden with one of the constructors.
675628
///
676629
/// It is required that the keys implement the `Eq` and `Hash` traits, although
677630
/// this can frequently be achieved by using `#[deriving(Eq, Hash)]`.
631+
///
632+
/// Relevant papers/articles:
633+
///
634+
/// 1. Pedro Celis. ["Robin Hood Hashing"](https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf)
635+
/// 2. Emmanuel Goossaert. ["Robin Hood
636+
/// hashing"](http://codecapsule.com/2013/11/11/robin-hood-hashing/)
637+
/// 3. Emmanuel Goossaert. ["Robin Hood hashing: backward shift
638+
/// deletion"](http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/)
639+
///
640+
/// # Example
641+
///
642+
/// ```rust
643+
/// use collections::HashMap;
644+
///
645+
/// // type inference lets us omit an explicit type signature (which
646+
/// // would be `HashMap<&str, &str>` in this example).
647+
/// let mut book_reviews = HashMap::new();
648+
///
649+
/// // review some books.
650+
/// book_reviews.insert("Adventures of Hucklebury Fin", "My favorite book.");
651+
/// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.");
652+
/// book_reviews.insert("Pride and Prejudice", "Very enjoyable.");
653+
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.");
654+
///
655+
/// // check for a specific one.
656+
/// if !book_reviews.contains_key(& &"Les Misérables") {
657+
/// println!("We've got {} reviews, but Les Misérables ain't one.",
658+
/// book_reviews.len());
659+
/// }
660+
///
661+
/// // oops, this review has a lot of spelling mistakes, let's delete it.
662+
/// book_reviews.remove(& &"The Adventures of Sherlock Holmes");
663+
///
664+
/// // look up the values associated with some keys.
665+
/// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
666+
/// for book in to_find.iter() {
667+
/// match book_reviews.find(book) {
668+
/// Some(review) => println!("{}: {}", *book, *review),
669+
/// None => println!("{} is unreviewed.", *book)
670+
/// }
671+
/// }
672+
///
673+
/// // iterate over everything.
674+
/// for (book, review) in book_reviews.iter() {
675+
/// println!("{}: \"{}\"", *book, *review);
676+
/// }
677+
/// ```
678678
#[deriving(Clone)]
679679
pub struct HashMap<K, V, H = sip::SipHasher> {
680680
// All hashes are keyed on these values, to prevent hash collision attacks.

0 commit comments

Comments
 (0)