From a92bc67f99b3c5c553e10f6d7e684f491e24dbf9 Mon Sep 17 00:00:00 2001 From: Bart Louwers Date: Tue, 5 Dec 2023 14:42:48 +0100 Subject: [PATCH] Fix order_independent_hash on 32-bit platforms (#1914) --- src/mbgl/util/hash.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/mbgl/util/hash.hpp b/src/mbgl/util/hash.hpp index e49f73cf3..2786d8d46 100644 --- a/src/mbgl/util/hash.hpp +++ b/src/mbgl/util/hash.hpp @@ -25,10 +25,17 @@ template using TIterVal = std::enable_if_t::value, typename TIter::value_type>; } // namespace detail +template +constexpr T factor() { + // Default factor prime value from 64-bit FNV hash. + if constexpr (sizeof(T) == 8) return 1099511628211; + // for 32-bit hash + return 16777619; +} + /// Generate a hash key from a collection of integer values which doesn't depend on their order. /// Adapted from https://stackoverflow.com/a/76993810/135138 -/// Default factor prime value from 64-bit FNV hash. -template , TKey factor = 1099511628211LL> +template , TKey factor = factor()> TKey order_independent_hash(TIter cur, const TIter end) { detail::TIterVal sum = 0, product = 1; for (; cur != end; ++cur) {