From 2c62e30ff903710043733d36d61774e1703e4130 Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Mon, 7 Jun 2010 15:09:22 +0200 Subject: [PATCH] [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 --- owlps-positioning/TODO | 7 ------- owlps-positioning/src/calibrationrequest.cc | 14 ++++++++++++++ owlps-positioning/src/calibrationrequest.hh | 3 +++ owlps-positioning/src/direction.cc | 9 +++++++++ owlps-positioning/src/direction.hh | 3 +++ owlps-positioning/src/point3d.cc | 14 ++++++++++++++ owlps-positioning/src/point3d.hh | 3 +++ owlps-positioning/src/referencepoint.cc | 12 ++++++++++++ owlps-positioning/src/referencepoint.hh | 3 +++ owlps-positioning/src/request.cc | 17 +++++++++++++++++ owlps-positioning/src/request.hh | 3 +++ owlps-positioning/src/stock.cc | 13 ------------- owlps-positioning/src/timestamp.cc | 9 +++++++++ owlps-positioning/src/timestamp.hh | 3 +++ 14 files changed, 93 insertions(+), 20 deletions(-) diff --git a/owlps-positioning/TODO b/owlps-positioning/TODO index d900487..ed0e27b 100644 --- a/owlps-positioning/TODO +++ b/owlps-positioning/TODO @@ -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 et - . - - Tests unitaires ° Finir le test de InputDataReader. ° Finir le test de Input. diff --git a/owlps-positioning/src/calibrationrequest.cc b/owlps-positioning/src/calibrationrequest.cc index 9e1f2ba..5a0f8fb 100644 --- a/owlps-positioning/src/calibrationrequest.cc +++ b/owlps-positioning/src/calibrationrequest.cc @@ -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(source)) ; + boost::hash_combine(seed, source.direction) ; + if (source.reference_point != NULL) + boost::hash_combine(seed, *source.reference_point) ; + + return seed ; +} diff --git a/owlps-positioning/src/calibrationrequest.hh b/owlps-positioning/src/calibrationrequest.hh index 96461d0..cb8baca 100644 --- a/owlps-positioning/src/calibrationrequest.hh +++ b/owlps-positioning/src/calibrationrequest.hh @@ -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) ; } ; diff --git a/owlps-positioning/src/direction.cc b/owlps-positioning/src/direction.cc index d033248..2c017d6 100644 --- a/owlps-positioning/src/direction.cc +++ b/owlps-positioning/src/direction.cc @@ -1,6 +1,8 @@ #include "direction.hh" #include "posexcept.hh" +#include + /* *** 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) ; +} diff --git a/owlps-positioning/src/direction.hh b/owlps-positioning/src/direction.hh index 2c89796..7894326 100644 --- a/owlps-positioning/src/direction.hh +++ b/owlps-positioning/src/direction.hh @@ -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) ; } ; diff --git a/owlps-positioning/src/point3d.cc b/owlps-positioning/src/point3d.cc index 9012975..aff35bf 100644 --- a/owlps-positioning/src/point3d.cc +++ b/owlps-positioning/src/point3d.cc @@ -1,6 +1,7 @@ #include "point3d.hh" #include +#include @@ -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 ; +} diff --git a/owlps-positioning/src/point3d.hh b/owlps-positioning/src/point3d.hh index 0e39826..e432c39 100644 --- a/owlps-positioning/src/point3d.hh +++ b/owlps-positioning/src/point3d.hh @@ -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) ; } ; diff --git a/owlps-positioning/src/referencepoint.cc b/owlps-positioning/src/referencepoint.cc index cfdfb09..24ae24f 100644 --- a/owlps-positioning/src/referencepoint.cc +++ b/owlps-positioning/src/referencepoint.cc @@ -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(source)) ; +} diff --git a/owlps-positioning/src/referencepoint.hh b/owlps-positioning/src/referencepoint.hh index 9fba926..aef2c4a 100644 --- a/owlps-positioning/src/referencepoint.hh +++ b/owlps-positioning/src/referencepoint.hh @@ -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) ; } ; diff --git a/owlps-positioning/src/request.cc b/owlps-positioning/src/request.cc index 2114e47..8662093 100644 --- a/owlps-positioning/src/request.cc +++ b/owlps-positioning/src/request.cc @@ -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 ; +} diff --git a/owlps-positioning/src/request.hh b/owlps-positioning/src/request.hh index 109091a..6422714 100644 --- a/owlps-positioning/src/request.hh +++ b/owlps-positioning/src/request.hh @@ -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) ; } ; diff --git a/owlps-positioning/src/stock.cc b/owlps-positioning/src/stock.cc index 7346a05..f32f780 100644 --- a/owlps-positioning/src/stock.cc +++ b/owlps-positioning/src/stock.cc @@ -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(object)) ; -} - -inline size_t hash_value(const Point3D &object) -{ - return boost::hash_value(static_cast(object)) ; -} - - /* *** Attribute definitions *** */ diff --git a/owlps-positioning/src/timestamp.cc b/owlps-positioning/src/timestamp.cc index 3fb8974..7117be5 100644 --- a/owlps-positioning/src/timestamp.cc +++ b/owlps-positioning/src/timestamp.cc @@ -1,5 +1,7 @@ #include "timestamp.hh" +#include + /* *** Constructors *** */ @@ -149,3 +151,10 @@ std::ostream& operator<<(std::ostream &os, const Timestamp &t) os << static_cast(t) ; return os ; } + + + +size_t hash_value(const Timestamp &source) +{ + return boost::hash_value(static_cast(source)) ; +} diff --git a/owlps-positioning/src/timestamp.hh b/owlps-positioning/src/timestamp.hh index 2af52e4..94c4a63 100644 --- a/owlps-positioning/src/timestamp.hh +++ b/owlps-positioning/src/timestamp.hh @@ -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) ; } ;