From 7302bb4484d517a72cdffbd13ec7a9f2324cde01 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Sat, 29 Oct 2016 11:41:53 +0200 Subject: 8.6 --- zen/stl_tools.h | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'zen/stl_tools.h') diff --git a/zen/stl_tools.h b/zen/stl_tools.h index 07925981..064d5b51 100644 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -61,8 +61,8 @@ template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); -size_t hashBytes(const unsigned char* ptr, size_t len); -size_t hashBytesAppend(size_t hashVal, const unsigned char* ptr, size_t len); +template size_t hashBytes (ByteIterator first, ByteIterator last); +template size_t hashBytesAppend(size_t hashVal, ByteIterator first, ByteIterator last); //support for custom string classes in std::unordered_set/map @@ -72,7 +72,8 @@ struct StringHash size_t operator()(const String& str) const { const auto* strFirst = strBegin(str); - return hashBytes(reinterpret_cast(strFirst), strLength(str) * sizeof(strFirst[0])); + return hashBytes(reinterpret_cast(strFirst), + reinterpret_cast(strFirst + strLength(str))); } }; @@ -211,36 +212,36 @@ bool equal(InputIterator1 first1, InputIterator1 last1, #endif -inline -size_t hashBytes(const unsigned char* ptr, size_t len) +template inline +size_t hashBytes(ByteIterator first, ByteIterator last) { - //http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function + //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, ptr, len); + return hashBytesAppend(basis, first, last); } -inline -size_t hashBytesAppend(size_t hashVal, const unsigned char* ptr, size_t len) +template inline +size_t hashBytesAppend(size_t 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 (size_t i = 0; i < len; ++i) + for (; first != last; ++first) { - hashVal ^= static_cast(ptr[i]); + hashVal ^= static_cast(*first); hashVal *= prime; } return hashVal; } - } #endif //STL_TOOLS_H_84567184321434 -- cgit