chore: add stdlib helper/other utils.

This commit is contained in:
2025-01-20 00:43:17 +08:00
parent 679764c2bf
commit 7c63bba8a2
2 changed files with 56 additions and 0 deletions

46
AskRVA/src/nonstd.h Normal file
View File

@@ -0,0 +1,46 @@
#pragma once
// From:
// https://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
namespace std {
namespace {
// Code from boost
// Reciprocal of the golden ratio helps spread entropy
// and handles duplicates.
// See Mike Seymour in magic-numbers-in-boosthash-combine:
// http://stackoverflow.com/questions/4948780
template <class T>
inline void hash_combine(std::size_t& seed, T const& v) {
seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
// Recursive template code derived from Matthieu M.
template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
struct HashValueImpl {
static void apply(size_t& seed, Tuple const& tuple) {
HashValueImpl<Tuple, Index - 1>::apply(seed, tuple);
hash_combine(seed, std::get<Index>(tuple));
}
};
template <class Tuple>
struct HashValueImpl<Tuple, 0> {
static void apply(size_t& seed, Tuple const& tuple) { hash_combine(seed, std::get<0>(tuple)); }
};
} // namespace
template <typename... TT>
struct hash<std::tuple<TT...>> {
size_t operator()(std::tuple<TT...> const& tt) const {
size_t seed = 0;
HashValueImpl<std::tuple<TT...>>::apply(seed, tt);
return seed;
}
};
} // namespace std

10
AskRVA/src/util/string.h Normal file
View File

@@ -0,0 +1,10 @@
#pragma once
namespace util::string {
constexpr unsigned int H(std::string_view str, unsigned int hash = 0) {
for (char c : str) hash = hash * 31 + static_cast<unsigned int>(c);
return hash;
}
} // namespace util::string