I know it has probably been asked a million times here, but I couldn't find
any example that I could use.
I want to create an unordered_map whose key is an object that I made
(vector2), what's the best way of implementing a hash function to it? Can I
have an example?
struct vector2
{
vector2();
vector2(const vector2 &v2);
vector2(float fx, float fy);
vector2 operator * (float num) const;
vector2 operator * (const vector2& v2) const;
vector2 operator / (float num) const;
vector2 operator / (const vector2& v2) const;
vector2 operator - (float num) const;
vector2 operator - (const vector2& v2) const;
vector2 operator + (float num) const;
vector2 operator + (const vector2& v2) const;
bool operator == (const vector2& v2) const;
bool operator != (const vector2& v2) const;
vector2& operator += (const vector2& v2);
vector2& operator -= (const vector2& v2);
vector2& operator *= (float f);
vector2& operator /= (float f);
float x,y;
};
boost::unordered_map
On 27 February 2010 19:21, André Santee
I know it has probably been asked a million times here, but I couldn't find any example that I could use. I want to create an unordered_map whose key is an object that I made (vector2), what's the best way of implementing a hash function to it? Can I have an example?
You need to implement 'hash_value' so that Boost.Hash can hash your custom type, take a look at the last example in: http://www.boost.org/doc/html/unordered/hash_equality.html#unordered.hash_eq... It's pretty much what you want. There's more info in the Boost.Hash documentation: http://www.boost.org/doc/html/hash/custom.html Daniel
Thanks,
I solved my problem by doing this:
namespace boost
{
std::size_t hash_value(const GS_VECTOR2 &v2)
{
return ((std::size_t)v2.y*COLUMNS)+(std::size_t)v2.x;
}
};
Is it also ok?
André.
2010/2/27 Daniel James
On 27 February 2010 19:21, André Santee
wrote: I know it has probably been asked a million times here, but I couldn't find any example that I could use. I want to create an unordered_map whose key is an object that I made (vector2), what's the best way of implementing a hash function to it? Can I have an example?
You need to implement 'hash_value' so that Boost.Hash can hash your custom type, take a look at the last example in:
http://www.boost.org/doc/html/unordered/hash_equality.html#unordered.hash_eq...
It's pretty much what you want. There's more info in the Boost.Hash documentation:
http://www.boost.org/doc/html/hash/custom.html
Daniel _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
AMDG André Santee wrote:
I solved my problem by doing this:
namespace boost { std::size_t hash_value(const GS_VECTOR2 &v2) { return ((std::size_t)v2.y*COLUMNS)+(std::size_t)v2.x; } };
Is it also ok?
You might get a better hash using boost::hash_value and boost::hash_combine In Christ, Steven Watanabe
On 27 February 2010 19:37, André Santee
Thanks, I solved my problem by doing this:
namespace boost { std::size_t hash_value(const GS_VECTOR2 &v2) { return ((std::size_t)v2.y*COLUMNS)+(std::size_t)v2.x; } };
Is it also ok?
Yes, but unless you're using a really old compiler that doesn't support argument dependent lookup (ADL), you should put the function in the same namespace as your vector. You can read about ADL at: http://en.wikipedia.org/wiki/Argument_dependent_name_lookup Daniel
participants (3)
-
André Santee
-
Daniel James
-
Steven Watanabe