1 | |
---|
2 | // Copyright Daniel James 2005. Use, modification, and distribution are |
---|
3 | // subject to the Boost Software License, Version 1.0. (See accompanying |
---|
4 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
---|
5 | |
---|
6 | #include "./books.hpp" |
---|
7 | #include <boost/functional/hash.hpp> |
---|
8 | #include <cassert> |
---|
9 | |
---|
10 | // If std::unordered_set was available: |
---|
11 | //#include <unordered_set> |
---|
12 | |
---|
13 | // This example illustrates how to use boost::hash with a custom hash function. |
---|
14 | // For full details, see the tutorial. |
---|
15 | |
---|
16 | int main() |
---|
17 | { |
---|
18 | library::book knife(3458, "Zane Grey", "The Hash Knife Outfit"); |
---|
19 | library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens"); |
---|
20 | |
---|
21 | boost::hash<library::book> book_hasher; |
---|
22 | std::size_t knife_hash_value = book_hasher(knife); |
---|
23 | |
---|
24 | // If std::unordered_set was available: |
---|
25 | // |
---|
26 | //std::unordered_set<library::book, boost::hash<library::book> > books; |
---|
27 | //books.insert(knife); |
---|
28 | //books.insert(library::book(2443, "Lindgren, Torgny", "Hash")); |
---|
29 | //books.insert(library::book(1953, "Snyder, Bernadette M.", |
---|
30 | // "Heavenly Hash: A Tasty Mix of a Mother's Meditations")); |
---|
31 | |
---|
32 | //assert(books.find(knife) != books.end()); |
---|
33 | //assert(books.find(dandelion) == books.end()); |
---|
34 | } |
---|
35 | |
---|
36 | namespace library |
---|
37 | { |
---|
38 | bool operator==(book const& a, book const& b) |
---|
39 | { |
---|
40 | return a.id == b.id; |
---|
41 | } |
---|
42 | |
---|
43 | std::size_t hash_value(book const& b) |
---|
44 | { |
---|
45 | boost::hash<int> hasher; |
---|
46 | return hasher(b.id); |
---|
47 | } |
---|
48 | } |
---|