Currently I have next setup [code] And then I use it I should write combine + fnv1ahash…
- User
Currently I have next setup
cppnamespace util { constexpr std::uint64_t FNV_BASIS = 0xcbf29ce484222325ULL; constexpr std::uint64_t FNV_PRIME = 0x100000001b3ULL; void combine(std::size_t &seed, std::size_t v) noexcept { seed ^= v + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2); }; template <class T> void hash_and_combine(std::size_t &seed, const T &v) noexcept { std::hash<T> hasher; combine(h, hasher(v)); }; template <class T, std::size_t N> [[nodiscard]] constexpr std::enable_if_t<std::is_integral_v<T> && std::is_unsigned_v<T>, std::size_t> fnv1a_hash(const T (&data)[N]) noexcept { std::uint64_t h = FNV_BASIS; for (std::size_t i = 0; i < N; ++i) { h ^= static_cast<std::uint64_t>(data[i]); h *= FNV_PRIME; } return static_cast<std::size_t>(h); } } // namespace utilAnd then I use it I should write combine + fnv1a_hash for arrays or hash_and_combine for others. Is there way to simplify this?
Assistant
You can simplify the usage by creating a single function that can handle both arrays and other types. Here's an example of how you can do it:
cppnamespace util { constexpr std::uint64_t FNV_BASIS = 0xcbf29ce484222325ULL; constexpr std::uint64_t FNV_PRIME = 0x100000001b3ULL; void combine(std::size_t &seed, std::size_t v) noexcept { seed ^= v + 0x9e3779b97f4a7c15ULL + (seed << 6) + (seed >> 2); }; template <class T> void hash_and_combine(std::size_t &seed, const T &v) noexcept { std::hash<T> hasher; combine(seed, hasher(v)); }; template <class T, std::size_t N> void hash_and_combine(std::size_t &seed, const T (&data)[N]) noexcept { std::uint64_t h = FNV_BASIS; for (std::size_t i = 0; i < N; ++i) { h ^= static_cast<std::uint64_t>(data[i]); h *= FNV_PRIME; } combine(seed, static_cast<std::size_t>(h)); }; template <class... Ts> std::size_t hash_combine(const Ts&... args) noexcept { std::size_t seed = 0; (hash_and_combine(seed, args), ...); return seed; } } // namespace util
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.