Skip to content

Commit

Permalink
add hashCombine
Browse files Browse the repository at this point in the history
C++11 added std::hash, but neglected to include a function for
combining multiple hashes.
  • Loading branch information
PaulPrice committed Mar 27, 2018
1 parent ed55179 commit e68ac28
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions include/lsst/utils/hashCombine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef LSST_UTILS_HASH_COMBINE_H
#define LSST_UTILS_HASH_COMBINE_H

#include <functional>

namespace lsst {
namespace utils {

//@{
/** Combine hashes
*
* This is provided as a convenience for those who need to hash a composite.
* C++11 includes std::hash, but neglects to include a facility for
* combining hashes.
*
* To use it:
*
* std::size_t seed = 0;
* result = hashCombine(seed, obj1, obj2, obj3);
*
* This solution is provided by Matteo Italia
* https://stackoverflow.com/a/38140932/834250
*/
inline std::size_t hashCombine(std::size_t seed) { return seed; }

template <typename T, typename... Rest>
std::size_t hashCombine(std::size_t seed, const T& value, Rest... rest) {
std::hash<T> hasher;
seed ^= hasher(value) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return hashCombine(seed, rest...);
}
//@}

}} // namespace lsst::utils

#endif

0 comments on commit e68ac28

Please sign in to comment.