mirror of
https://github.com/boostorg/unordered.git
synced 2025-05-09 23:23:59 +00:00
Pull the point examples out into test files - fixing a few bugs in the process.
[SVN r41996]
This commit is contained in:
parent
f6dcb589f8
commit
27e055b228
@ -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<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
|
||||
|
46
doc/src_code/point1.cpp
Normal file
46
doc/src_code/point1.cpp
Normal file
@ -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();
|
||||
}
|
43
doc/src_code/point2.cpp
Normal file
43
doc/src_code/point2.cpp
Normal file
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user