From 2dd6739826c86ca96a6a1548fd2e0fb7c0eb8dd0 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Wed, 9 May 2018 00:13:16 +0200 Subject: 10.0 --- zen/stl_tools.h | 44 +++++++++++++++++++------------------------- 1 file changed, 19 insertions(+), 25 deletions(-) (limited to 'zen/stl_tools.h') diff --git a/zen/stl_tools.h b/zen/stl_tools.h index ba4a6c89..f09639e1 100755 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -58,9 +58,8 @@ template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); -template size_t hashBytes (ByteIterator first, ByteIterator last); -template size_t hashBytesAppend(size_t hashVal, ByteIterator first, ByteIterator last); - +template Num hashBytes (ByteIterator first, ByteIterator last); +template Num hashBytesAppend(Num hashVal, ByteIterator first, ByteIterator last); //support for custom string classes in std::unordered_set/map struct StringHash @@ -69,7 +68,7 @@ struct StringHash size_t operator()(const String& str) const { const auto* strFirst = strBegin(str); - return hashBytes(reinterpret_cast(strFirst), + return hashBytes(reinterpret_cast(strFirst), reinterpret_cast(strFirst + strLength(str))); } }; @@ -190,34 +189,29 @@ bool equal(InputIterator1 first1, InputIterator1 last1, } + - -template inline -size_t hashBytes(ByteIterator first, ByteIterator last) +//FNV-1a: http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function +template inline +Num hashBytes(ByteIterator first, ByteIterator last) { - //FNV-1a: http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function -#ifdef ZEN_BUILD_32BIT - const size_t basis = 2166136261U; -#elif defined ZEN_BUILD_64BIT - const size_t basis = 14695981039346656037ULL; -#endif - return hashBytesAppend(basis, first, last); + static_assert(std::is_integral::value, ""); + static_assert(sizeof(Num) == 4 || sizeof(Num) == 8, ""); //macOS: size_t is "unsigned long" + const Num base = sizeof(Num) == 4 ? 2166136261U : 14695981039346656037ULL; + + return hashBytesAppend(base, first, last); } -template inline -size_t hashBytesAppend(size_t hashVal, ByteIterator first, ByteIterator last) +template inline +Num hashBytesAppend(Num hashVal, ByteIterator first, ByteIterator last) { -#ifdef ZEN_BUILD_32BIT - const size_t prime = 16777619U; -#elif defined ZEN_BUILD_64BIT - const size_t prime = 1099511628211ULL; -#endif - static_assert(sizeof(typename std::iterator_traits::value_type) == 1, ""); - - for (; first != last; ++first) + static_assert(sizeof(typename std::iterator_traits::value_type) == 1, ""); + const Num prime = sizeof(Num) == 4 ? 16777619U : 1099511628211ULL; + + for (; first != last; ++first) { - hashVal ^= static_cast(*first); + hashVal ^= static_cast(*first); hashVal *= prime; } return hashVal; -- cgit