forked from boostorg/container_hash
Add some hash examples.
[SVN r28139]
This commit is contained in:
8
examples/Jamfile.v2
Normal file
8
examples/Jamfile.v2
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
# Copyright Daniel James 2005. Use, modification, and distribution are
|
||||
# subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
exe books : books.cpp ;
|
||||
exe point : point.cpp ;
|
||||
exe portable : portable.cpp ;
|
55
examples/books.cpp
Normal file
55
examples/books.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
|
||||
// Copyright Daniel James 2005. Use, modification, and distribution are
|
||||
// subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
// If we had std::unordered_set.
|
||||
//#include <unordered_set>
|
||||
#include <cassert>
|
||||
|
||||
// This example illustrates how to use boost::hash with a custom hash function.
|
||||
// For full details, see the tutorial.
|
||||
|
||||
namespace library
|
||||
{
|
||||
struct book
|
||||
{
|
||||
int id;
|
||||
std::string author;
|
||||
std::string title;
|
||||
|
||||
book(int i, std::string const& a, std::string const& t)
|
||||
: id(i), author(a), title(t) {}
|
||||
};
|
||||
|
||||
bool operator==(book const& a, book const& b)
|
||||
{
|
||||
return a.id == b.id;
|
||||
}
|
||||
|
||||
std::size_t hash_value(book const& b)
|
||||
{
|
||||
return boost::hash_value(b.id);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
library::book knife(3458, "Zane Grey", "The Hash Knife Outfit");
|
||||
library::book dandelion(1354, "Paul J. Shanley", "Hash & Dandelion Greens");
|
||||
|
||||
boost::hash<library::book> book_hasher;
|
||||
std::size_t knife_hash_value = book_hasher(knife);
|
||||
|
||||
// If we had std::unordered_set:
|
||||
//
|
||||
//std::unordered_set<library::book, boost::hash<library::book> > books;
|
||||
//books.insert(knife);
|
||||
//books.insert(library::book(2443, "Lindgren, Torgny", "Hash"));
|
||||
//books.insert(library::book(1953, "Snyder, Bernadette M.",
|
||||
// "Heavenly Hash: A Tasty Mix of a Mother's Meditations"));
|
||||
|
||||
//assert(books.find(knife) != books.end());
|
||||
//assert(books.find(dandelion) == books.end());
|
||||
}
|
51
examples/point.cpp
Normal file
51
examples/point.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
|
||||
// Copyright Daniel James 2005. Use, modification, and distribution are
|
||||
// subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <cassert>
|
||||
|
||||
// This example illustrates how to use boost::hash_combine to generate a hash
|
||||
// value from the different members of a class. For full details see the hash
|
||||
// tutorial.
|
||||
|
||||
class point
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
public:
|
||||
point() : x(0), y(0) {}
|
||||
point(int x, int y) : x(x), y(y) {}
|
||||
|
||||
bool operator==(point const& other) const
|
||||
{
|
||||
return x = other.x && y == other.y;
|
||||
}
|
||||
|
||||
friend std::size_t hash_value(point const& p)
|
||||
{
|
||||
std::size_t seed = 0;
|
||||
boost::hash_combine(seed, p.x);
|
||||
boost::hash_combine(seed, p.y);
|
||||
|
||||
return seed;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
boost::hash<point> point_hasher;
|
||||
|
||||
point p1(0, 0);
|
||||
point p2(1, 2);
|
||||
point p3(4, 1);
|
||||
point p4 = p1;
|
||||
|
||||
assert(point_hasher(p1) == point_hasher(p4));
|
||||
|
||||
// These tests could legally fail, but if they did it'd be a pretty bad
|
||||
// hash function.
|
||||
assert(point_hasher(p1) != point_hasher(p2));
|
||||
assert(point_hasher(p1) != point_hasher(p3));
|
||||
}
|
56
examples/portable.cpp
Normal file
56
examples/portable.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
|
||||
// Copyright Daniel James 2005. Use, modification, and distribution are
|
||||
// subject to the Boost Software License, Version 1.0. (See accompanying
|
||||
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/functional/hash.hpp>
|
||||
#include <cassert>
|
||||
|
||||
// This example illustrates how to customise boost::hash portably, so that
|
||||
// it'll work on both compilers that don't implement argument dependent lookup
|
||||
// and compilers that implement strict two-phase template instantiation.
|
||||
|
||||
namespace foo
|
||||
{
|
||||
struct custom_type
|
||||
{
|
||||
int value;
|
||||
|
||||
custom_type(int x) : value(x) {}
|
||||
|
||||
#ifndef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
||||
friend inline std::size_t hash_value(custom_type x)
|
||||
{
|
||||
boost::hash<int> hasher;
|
||||
return hasher(x.value);
|
||||
}
|
||||
#else
|
||||
std::size_t hash() const
|
||||
{
|
||||
boost::hash<int> hasher;
|
||||
return hasher(value);
|
||||
}
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
#ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
|
||||
namespace boost
|
||||
{
|
||||
std::size_t hash_value(foo::custom_type x)
|
||||
{
|
||||
return x.hash();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
foo::custom_type x(1), y(2), z(1);
|
||||
|
||||
boost::hash<foo::custom_type> hasher;
|
||||
|
||||
assert(hasher(x) == hasher(x));
|
||||
assert(hasher(x) != hasher(y));
|
||||
assert(hasher(x) == hasher(z));
|
||||
}
|
Reference in New Issue
Block a user