|
Boost-Commit : |
From: daniel_james_at_[hidden]
Date: 2007-12-12 19:31:56
Author: danieljames
Date: 2007-12-12 19:31:55 EST (Wed, 12 Dec 2007)
New Revision: 41996
URL: http://svn.boost.org/trac/boost/changeset/41996
Log:
Pull the point examples out into test files - fixing a few bugs in the process.
Added:
sandbox/unordered/libs/unordered/doc/src_code/point1.cpp (contents, props changed)
sandbox/unordered/libs/unordered/doc/src_code/point2.cpp (contents, props changed)
Text files modified:
sandbox/unordered/libs/unordered/doc/hash_equality.qbk | 50 ++++-----------------------------------
1 files changed, 6 insertions(+), 44 deletions(-)
Modified: sandbox/unordered/libs/unordered/doc/hash_equality.qbk
==============================================================================
--- sandbox/unordered/libs/unordered/doc/hash_equality.qbk (original)
+++ sandbox/unordered/libs/unordered/doc/hash_equality.qbk 2007-12-12 19:31:55 EST (Wed, 12 Dec 2007)
@@ -34,59 +34,21 @@
[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<point, std::size_t>
- {
- 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, std::equal_to<point>, 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<point> 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
Added: sandbox/unordered/libs/unordered/doc/src_code/point1.cpp
==============================================================================
--- (empty file)
+++ sandbox/unordered/libs/unordered/doc/src_code/point1.cpp 2007-12-12 19:31:55 EST (Wed, 12 Dec 2007)
@@ -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 <boost/unordered_set.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+//[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<point, std::size_t>
+ {
+ 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, point_hash, std::equal_to<point> >
+ 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();
+}
Added: sandbox/unordered/libs/unordered/doc/src_code/point2.cpp
==============================================================================
--- (empty file)
+++ sandbox/unordered/libs/unordered/doc/src_code/point2.cpp 2007-12-12 19:31:55 EST (Wed, 12 Dec 2007)
@@ -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 <boost/unordered_set.hpp>
+#include <boost/functional/hash.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+//[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<point> 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();
+}
+
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk