Skip to content

Commit

Permalink
Add hash combiner for iterables.
Browse files Browse the repository at this point in the history
The implementation is a generalization of the loop adopted for
hashing geom::Point.
  • Loading branch information
kfindeisen committed Nov 6, 2018
1 parent 72fabf3 commit 86abfe6
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions include/lsst/utils/hashCombine.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,38 @@ std::size_t hashCombine(std::size_t seed, const T& value, Rest... rest) noexcept
return hashCombine(seed, rest...);
}

/**
* Combine hashes in an iterable.
*
* This is provided as a convenience for those who need to hash a container.
*
* @tparam InputIterator an iterator to the objects to be hashed. The
* pointed-to type must have a valid (in particular,
* non-throwing) specialization of std::hash.
*
* @param seed An arbitrary starting value.
* @param begin, end The range to hash.
* @returns A combined hash for all the elements in [begin, end).
*
* @exceptsafe Shall not throw exceptions.
*
* To use it:
*
* // Arbitrary seed; can change to get different hashes of same argument list
* std::size_t seed = 0;
* result = hashIterable(seed, container.begin(), container.end());
*/
// Note: not an overload of hashCombine to avoid ambiguity with hashCombine(size_t, T1, T2)
// WARNING: should not be inline or constexpr; it can cause instantiation-order problems with std::hash<T>
template <typename InputIterator>
std::size_t hashIterable(std::size_t seed, InputIterator begin, InputIterator end) noexcept {
std::size_t result = 0;
for (; begin != end; ++begin) {
result = hashCombine(result, *begin);
}
return result;
}

}} // namespace lsst::utils

#endif

0 comments on commit 86abfe6

Please sign in to comment.