diff --git a/doc/hash_equality.qbk b/doc/hash_equality.qbk index 4963e00e..e2efde35 100644 --- a/doc/hash_equality.qbk +++ b/doc/hash_equality.qbk @@ -34,59 +34,21 @@ case-insensitive dictionary: [case_insensitive_functions] [case_insensitive_dictionary] -A more generic version is available at: +This is a simplified version of the example at: [@../../libs/unordered/examples/case_insensitive.hpp /libs/unordered/examples/case_insensitive.hpp] +which supports other locales and string types. [h2 Custom Types] Similarly, a custom hash function can be used for custom types: - struct point { - int x; - int y; - }; - - bool operator==(point const& p1, point const& p2) - { - return p1.x == p2.x && p1.y == p2.y; - } - - struct point_hash - : std::unary_function - { - std::size_t operator()(point const& p) const - { - std::size_t seed = 0; - boost::hash_combine(seed, p.x); - boost::hash_combine(seed, p.y); - return seed; - } - } - - boost::unordered_multiset, point_hash> - points; +[import src_code/point1.cpp] +[point_example1] Although, customising Boost.Hash is probably a better solution: - struct point { - int x; - int y; - }; - - bool operator==(point const& p1, point const& p2) - { - return p1.x == p2.x && p1.y == p2.y; - } - - std::size_t hash_value(point const& x) { - std::size_t seed = 0; - boost::hash_combine(seed, p.x); - boost::hash_combine(seed, p.y); - return seed; - } - - // Now the default functions work. - boost::unordered_multiset points; +[import src_code/point2.cpp] +[point_example2] See the Boost.Hash documentation for more detail on how to do this. Remember that it relies on extensions to the draft standard - so it won't work on other diff --git a/doc/src_code/point1.cpp b/doc/src_code/point1.cpp new file mode 100644 index 00000000..f812dbb9 --- /dev/null +++ b/doc/src_code/point1.cpp @@ -0,0 +1,46 @@ + +// Copyright 2006-2007 Daniel James. +// Distributed under 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 +#include + +//[point_example1 + struct point { + int x; + int y; + }; + + bool operator==(point const& p1, point const& p2) + { + return p1.x == p2.x && p1.y == p2.y; + } + + struct point_hash + : std::unary_function + { + std::size_t operator()(point const& p) const + { + std::size_t seed = 0; + boost::hash_combine(seed, p.x); + boost::hash_combine(seed, p.y); + return seed; + } + }; + + boost::unordered_multiset > + points; +//] + +int main() { + point x[] = {{1,2}, {3,4}, {1,5}, {1,2}}; + for(int i = 0; i < sizeof(x) / sizeof(point); ++i) + points.insert(x[i]); + BOOST_TEST(points.count(x[0]) == 2); + BOOST_TEST(points.count(x[1]) == 1); + point y = {10, 2}; + BOOST_TEST(points.count(y) == 0); + + return boost::report_errors(); +} diff --git a/doc/src_code/point2.cpp b/doc/src_code/point2.cpp new file mode 100644 index 00000000..7fc0be77 --- /dev/null +++ b/doc/src_code/point2.cpp @@ -0,0 +1,43 @@ + +// Copyright 2006-2007 Daniel James. +// Distributed under 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 +#include +#include + +//[point_example2 + struct point { + int x; + int y; + }; + + bool operator==(point const& p1, point const& p2) + { + return p1.x == p2.x && p1.y == p2.y; + } + + 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; + } + + // Now the default functions work. + boost::unordered_multiset points; +//] + +int main() { + point x[] = {{1,2}, {3,4}, {1,5}, {1,2}}; + for(int i = 0; i < sizeof(x) / sizeof(point); ++i) + points.insert(x[i]); + BOOST_TEST(points.count(x[0]) == 2); + BOOST_TEST(points.count(x[1]) == 1); + point y = {10, 2}; + BOOST_TEST(points.count(y) == 0); + + return boost::report_errors(); +} +