Jeff Thompson | a28eed8 | 2013-08-22 16:21:10 -0700 | [diff] [blame^] | 1 | |
| 2 | // Copyright 2005 Daniel James. |
| 3 | // Distributed under 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 <boost/functional/hash.hpp> |
| 7 | #include <cassert> |
| 8 | |
| 9 | // This example illustrates how to use ndnboost::hash_combine to generate a hash |
| 10 | // value from the different members of a class. For full details see the hash |
| 11 | // tutorial. |
| 12 | |
| 13 | class point |
| 14 | { |
| 15 | int x; |
| 16 | int y; |
| 17 | public: |
| 18 | point() : x(0), y(0) {} |
| 19 | point(int x, int y) : x(x), y(y) {} |
| 20 | |
| 21 | bool operator==(point const& other) const |
| 22 | { |
| 23 | return x == other.x && y == other.y; |
| 24 | } |
| 25 | |
| 26 | friend std::size_t hash_value(point const& p) |
| 27 | { |
| 28 | std::size_t seed = 0; |
| 29 | ndnboost::hash_combine(seed, p.x); |
| 30 | ndnboost::hash_combine(seed, p.y); |
| 31 | |
| 32 | return seed; |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | int main() |
| 37 | { |
| 38 | ndnboost::hash<point> point_hasher; |
| 39 | |
| 40 | point p1(0, 0); |
| 41 | point p2(1, 2); |
| 42 | point p3(4, 1); |
| 43 | point p4 = p1; |
| 44 | |
| 45 | assert(point_hasher(p1) == point_hasher(p4)); |
| 46 | |
| 47 | // These tests could legally fail, but if they did it'd be a pretty bad |
| 48 | // hash function. |
| 49 | assert(point_hasher(p1) != point_hasher(p2)); |
| 50 | assert(point_hasher(p1) != point_hasher(p3)); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |