To avoid extra dependencies, you can use std::hash
. Here's an example using the code from the link you posted, and updated to use a std::pair<double,double>
:
#include <unordered_map>
#include <cassert>
using namespace std;
class TPoint3D{
public:
TPoint3D(double x, double y, double z) : x(x), y(y), z(z){};
double x, y, z;
};
struct hashFunc{
size_t operator()(const TPoint3D &k) const{
size_t h1 = std::hash<double>()(k.x);
size_t h2 = std::hash<double>()(k.y);
size_t h3 = std::hash<double>()(k.z);
return (h1 ^ (h2 << 1)) ^ h3;
}
};
struct equalsFunc{
bool operator()( const TPoint3D& lhs, const TPoint3D& rhs ) const{
return (lhs.x == rhs.x) && (lhs.y == rhs.y) && (lhs.z == rhs.z);
}
};
typedef unordered_map<TPoint3D, int, hashFunc, equalsFunc> TPoint3DMap;
int main(){
TPoint3DMap myMap;
// test equalsFunc
myMap[TPoint3D(10.0, 20.0, 30.0)] = 100;
myMap[TPoint3D(10.0, 20.0, 30.0)] = 200;
assert(myMap[TPoint3D(10.0, 20.0, 30.0)] == 200);
// test if hashFunc handles well repeated values inside TPoint3D
myMap[TPoint3D(10.0, 10.0, 10.0)] = 1;
myMap[TPoint3D(10.0, 20.0, 10.0)] = 2;
myMap[TPoint3D(10.0, 10.0, 20.0)] = 3;
myMap[TPoint3D(20.0, 10.0, 10.0)] = 4;
assert(myMap[TPoint3D(10.0, 10.0, 10.0)] == 1);
assert(myMap[TPoint3D(10.0, 20.0, 10.0)] == 2);
assert(myMap[TPoint3D(10.0, 10.0, 20.0)] == 3);
assert(myMap[TPoint3D(20.0, 10.0, 10.0)] == 4);
return 0;
}
As I said before, if you wish to use another structure you have to adapt both the pairHash
class and pairEquals
struct operator()
to appropriately hash and compare the new keys, respectively.
Cheers
EDIT :
- Modified code to use custom TPPoint3D class and uniform functor classes definitions (both using struct).
- Added simple tests to validate the hash and equals functors.