[Positioning] Define hash_value() for several classes

For object types that we needed to store into an unordered_set, we used
string-based hashes, defined into stock.cc. We now define new hash
functions as friend of target classes, based on boost::hash_combine.

The following classes now have a hash_value() function:
- Request
- CalibrationRequest
- Direction
- Timestamp
- Point3D
- ReferencePoint
This commit is contained in:
Matteo Cypriani 2010-06-07 15:09:22 +02:00
parent aafc277704
commit 2c62e30ff9
14 changed files with 93 additions and 20 deletions

View File

@ -3,13 +3,6 @@
° Pour plus de souplesse, comparer les chaînes sans tenir compte de
la casse.
- Hachages
° Regrouper les surcharges de hash_value() dans un fichier
d'en-tête dédié.
° Éventuellement, ce fichier d'en-tête pourrait être utilisé comme
une interface et inclure directement <unordered_set> et
<unordered_map>.
- Tests unitaires
° Finir le test de InputDataReader.
° Finir le test de Input.

View File

@ -48,3 +48,17 @@ bool CalibrationRequest::operator==(const CalibrationRequest &source)
direction == source.direction &&
reference_point == source.reference_point ;
}
size_t hash_value(const CalibrationRequest &source)
{
size_t seed = 0 ;
boost::hash_combine(seed, static_cast<Request>(source)) ;
boost::hash_combine(seed, source.direction) ;
if (source.reference_point != NULL)
boost::hash_combine(seed, *source.reference_point) ;
return seed ;
}

View File

@ -52,6 +52,9 @@ public:
bool operator==(const CalibrationRequest &source) ;
bool operator!=(const CalibrationRequest &source) ;
//@}
/// Hash a CalibrationRequest
friend std::size_t hash_value(const CalibrationRequest &source) ;
} ;

View File

@ -1,6 +1,8 @@
#include "direction.hh"
#include "posexcept.hh"
#include <boost/functional/hash.hpp>
/* *** Constructors *** */
@ -64,3 +66,10 @@ Direction::operator std::string() const
}
return "Bad direction!" ;
}
size_t hash_value(const Direction &source)
{
return boost::hash_value(source.direction) ;
}

View File

@ -35,6 +35,9 @@ public:
operator int(void) const ;
operator std::string(void) const ;
//@}
// Hash a Direction
friend size_t hash_value(const Direction &source) ;
} ;

View File

@ -1,6 +1,7 @@
#include "point3d.hh"
#include <sstream>
#include <boost/functional/hash.hpp>
@ -97,3 +98,16 @@ std::ostream& operator<<(std::ostream &os, const Point3D &p)
os << "(" << p.x << ";" << p.y << ";" << p.z << ")" ;
return os ;
}
size_t hash_value(const Point3D &source)
{
size_t seed = 0 ;
boost::hash_combine(seed, source.x) ;
boost::hash_combine(seed, source.y) ;
boost::hash_combine(seed, source.z) ;
return seed ;
}

View File

@ -73,6 +73,9 @@ public:
/// Displays a Point3D
friend std::ostream& operator<<(std::ostream &os, const Point3D &p) ;
/// Hash a Point3D
friend std::size_t hash_value(const Point3D &source) ;
} ;

View File

@ -169,3 +169,15 @@ ostream &operator<<(ostream &os, const ReferencePoint &rp)
return os ;
}
/**
* This is a simple call to hash_value(Point3D), because we do not want
* to take care of the CalibrationRequest list to hash the
* ReferencePoint.
*/
size_t hash_value(const ReferencePoint &source)
{
return hash_value(static_cast<Point3D>(source)) ;
}

View File

@ -50,6 +50,9 @@ public:
/// Displays a ReferencePoint
friend std::ostream &operator<<(std::ostream &os, const ReferencePoint &rp) ;
/// Hash a ReferencePoint
friend std::size_t hash_value(const ReferencePoint &source) ;
} ;

View File

@ -90,3 +90,20 @@ ostream& operator<<(ostream &os, const Request &r)
return os ;
}
/**
* The Mobile MAC address and the Timestamp are sufficient to identify
* uniquely a Request.
*/
size_t hash_value(const Request &source)
{
size_t seed = 0 ;
boost::hash_combine(seed, source.time_sent) ;
if (source.mobile != NULL)
boost::hash_combine(seed, source.mobile->get_mac_addr()) ;
return seed ;
}

View File

@ -76,6 +76,9 @@ public:
/// Displays a Request
friend std::ostream& operator<<(std::ostream &os, const Request &r) ;
/// Hash a Request
friend std::size_t hash_value(const Request &source) ;
} ;

View File

@ -6,19 +6,6 @@ using std::tr1::unordered_map ;
using std::tr1::unordered_set ;
/* *** Hash functions, required to put points in a set *** */
inline size_t hash_value(const ReferencePoint &object)
{
return boost::hash_value(static_cast<string>(object)) ;
}
inline size_t hash_value(const Point3D &object)
{
return boost::hash_value(static_cast<string>(object)) ;
}
/* *** Attribute definitions *** */

View File

@ -1,5 +1,7 @@
#include "timestamp.hh"
#include <boost/functional/hash.hpp>
/* *** Constructors *** */
@ -149,3 +151,10 @@ std::ostream& operator<<(std::ostream &os, const Timestamp &t)
os << static_cast<uint64_t>(t) ;
return os ;
}
size_t hash_value(const Timestamp &source)
{
return boost::hash_value(static_cast<uint64_t>(source)) ;
}

View File

@ -87,6 +87,9 @@ public:
/// Display a Timestamp
friend std::ostream& operator<<(std::ostream &os, const Timestamp &t) ;
// Hash a Timestamp
friend size_t hash_value(const Timestamp &source) ;
} ;