Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/functional/hash/examples/point.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 17 years ago

added boost

File size: 1.2 KB
Line 
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 <boost/functional/hash.hpp>
7#include <cassert>
8
9// This example illustrates how to use boost::hash_combine to generate a hash
10// value from the different members of a class. For full details see the hash
11// tutorial.
12
13class point
14{
15    int x;
16    int y;
17public:
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        boost::hash_combine(seed, p.x);
30        boost::hash_combine(seed, p.y);
31
32        return seed;
33    }
34};
35
36int main()
37{
38    boost::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
Note: See TracBrowser for help on using the repository browser.