Skip to content

Commit 868aebf

Browse files
authored
Merge pull request #187 from Lotterleben/indexDoc
IndexMap, IndexSet: hint to FnvIndex...: Extend explanation, add examples & links
2 parents fdd7b48 + 61b8559 commit 868aebf

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

src/indexmap.rs

+48-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,48 @@ use hash32::{BuildHasher, BuildHasherDefault, FnvHasher, Hash, Hasher};
1212

1313
use crate::Vec;
1414

15-
/// An `IndexMap` using the default FNV hasher
15+
/// A [`heaples::IndexMap`](./struct.IndexMap.html) using the default FNV hasher
16+
///
17+
/// A list of all Methods and Traits available for `FnvIndexMap` can be found in
18+
/// the [`heapless::IndexMap`](./struct.IndexMap.html) documentation.
19+
///
20+
/// # Examples
21+
/// ```
22+
/// use heapless::FnvIndexMap;
23+
/// use heapless::consts::*;
24+
///
25+
/// // A hash map with a capacity of 16 key-value pairs allocated on the stack
26+
/// let mut book_reviews = FnvIndexMap::<_, _, U16>::new();
27+
///
28+
/// // review some books.
29+
/// book_reviews.insert("Adventures of Huckleberry Finn", "My favorite book.").unwrap();
30+
/// book_reviews.insert("Grimms' Fairy Tales", "Masterpiece.").unwrap();
31+
/// book_reviews.insert("Pride and Prejudice", "Very enjoyable.").unwrap();
32+
/// book_reviews.insert("The Adventures of Sherlock Holmes", "Eye lyked it alot.").unwrap();
33+
///
34+
/// // check for a specific one.
35+
/// if !book_reviews.contains_key("Les Misérables") {
36+
/// println!("We've got {} reviews, but Les Misérables ain't one.",
37+
/// book_reviews.len());
38+
/// }
39+
///
40+
/// // oops, this review has a lot of spelling mistakes, let's delete it.
41+
/// book_reviews.remove("The Adventures of Sherlock Holmes");
42+
///
43+
/// // look up the values associated with some keys.
44+
/// let to_find = ["Pride and Prejudice", "Alice's Adventure in Wonderland"];
45+
/// for book in &to_find {
46+
/// match book_reviews.get(book) {
47+
/// Some(review) => println!("{}: {}", book, review),
48+
/// None => println!("{} is unreviewed.", book)
49+
/// }
50+
/// }
51+
///
52+
/// // iterate over everything.
53+
/// for (book, review) in &book_reviews {
54+
/// println!("{}: \"{}\"", book, review);
55+
/// }
56+
/// ```
1657
pub type FnvIndexMap<K, V, N> = IndexMap<K, V, N, BuildHasherDefault<FnvHasher>>;
1758

1859
#[derive(Clone, Copy, Eq, PartialEq)]
@@ -286,9 +327,15 @@ where
286327

287328
/// Fixed capacity [`IndexMap`](https://docs.rs/indexmap/1/indexmap/map/struct.IndexMap.html)
288329
///
330+
/// Note that you cannot use `IndexMap` directly, since it is generic around the hashing algorithm
331+
/// in use. Pick a concrete instantiation like [`FnvIndexMap`](./type.FnvIndexMap.html) instead
332+
/// or create your own.
333+
///
289334
/// Note that the capacity of the `IndexMap` must be a power of 2.
290335
///
291336
/// # Examples
337+
/// Since `IndexMap` cannot be used directly, we're using its `FnvIndexMap` instantiation
338+
/// for this example.
292339
///
293340
/// ```
294341
/// use heapless::FnvIndexMap;

src/indexset.rs

+40-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,52 @@ use hash32::{BuildHasher, BuildHasherDefault, FnvHasher, Hash, Hasher};
55

66
use crate::indexmap::{self, Bucket, IndexMap, Pos};
77

8-
/// An `IndexSet` using the default FNV hasher
8+
/// A [`heapless::IndexSet`](./struct.IndexSet.html) using the
9+
/// default FNV hasher.
10+
/// A list of all Methods and Traits available for `FnvIndexSet` can be found in
11+
/// the [`heapless::IndexSet`](./struct.IndexSet.html) documentation.
12+
///
13+
/// # Examples
14+
/// ```
15+
/// use heapless::FnvIndexSet;
16+
/// use heapless::consts::*;
17+
///
18+
/// // A hash set with a capacity of 16 elements allocated on the stack
19+
/// let mut books = FnvIndexSet::<_, U16>::new();
20+
///
21+
/// // Add some books.
22+
/// books.insert("A Dance With Dragons").unwrap();
23+
/// books.insert("To Kill a Mockingbird").unwrap();
24+
/// books.insert("The Odyssey").unwrap();
25+
/// books.insert("The Great Gatsby").unwrap();
26+
///
27+
/// // Check for a specific one.
28+
/// if !books.contains("The Winds of Winter") {
29+
/// println!("We have {} books, but The Winds of Winter ain't one.",
30+
/// books.len());
31+
/// }
32+
///
33+
/// // Remove a book.
34+
/// books.remove("The Odyssey");
35+
///
36+
/// // Iterate over everything.
37+
/// for book in &books {
38+
/// println!("{}", book);
39+
/// }
40+
/// ```
941
pub type FnvIndexSet<T, N> = IndexSet<T, N, BuildHasherDefault<FnvHasher>>;
1042

11-
/// Fixed capacity [`IndexSet`](https://docs.rs/indexmap/1/indexmap/set/struct.IndexSet.html)
43+
/// Fixed capacity [`IndexSet`](https://docs.rs/indexmap/1/indexmap/set/struct.IndexSet.html).
44+
///
45+
/// Note that you cannot use `IndexSet` directly, since it is generic around the hashing algorithm
46+
/// in use. Pick a concrete instantiation like [`FnvIndexSet`](./type.FnvIndexSet.html) instead
47+
/// or create your own.
1248
///
1349
/// Note that the capacity of the `IndexSet` must be a power of 2.
1450
///
1551
/// # Examples
52+
/// Since `IndexSet` cannot be used directly, we're using its `FnvIndexSet` instantiation
53+
/// for this example.
1654
///
1755
/// ```
1856
/// use heapless::FnvIndexSet;

0 commit comments

Comments
 (0)