From ad4e3d2c55e75193c41356c23619f80add41db18 Mon Sep 17 00:00:00 2001 From: Daniel Wilhelm Date: Fri, 2 Oct 2015 14:57:46 +0200 Subject: 7.5 --- zen/stl_tools.h | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'zen/stl_tools.h') diff --git a/zen/stl_tools.h b/zen/stl_tools.h index e949c5c9..1e38f1b0 100644 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -13,6 +13,7 @@ #include #include #include "type_tools.h" +#include "build_info.h" //enhancements for namespace zen @@ -59,12 +60,19 @@ template bool equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2); -//until std::make_unique is available in GCC: -template inline -std::unique_ptr make_unique(Args&& ... args) { return std::unique_ptr(new T(std::forward(args)...)); } - +size_t hashBytes(const unsigned char* ptr, size_t len); +//support for custom string classes in std::unordered_set/map +struct StringHash +{ + template + size_t operator()(const String& str) const + { + const auto* strFirst = strBegin(str); + return hashBytes(reinterpret_cast(strFirst), strLength(str) * sizeof(strFirst[0])); + } +}; @@ -199,6 +207,28 @@ bool equal(InputIterator1 first1, InputIterator1 last1, #if defined _MSC_VER && _MSC_VER <= 1600 static_assert(false, "VS2010 performance bug in std::unordered_set<>: http://drdobbs.com/blogs/cpp/232200410 -> should be fixed in VS11"); #endif + + +inline +size_t hashBytes(const unsigned char* ptr, size_t len) +{ + //http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function +#ifdef ZEN_BUILD_32BIT + const size_t basis = 2166136261U; + const size_t prime = 16777619U; +#elif defined ZEN_BUILD_64BIT + const size_t basis = 14695981039346656037ULL; + const size_t prime = 1099511628211ULL; +#endif + + size_t val = basis; + for (size_t i = 0; i < len; ++i) + { + val ^= static_cast(ptr[i]); + val *= prime; + } + return val; +} } #endif //STL_TOOLS_HEADER_84567184321434 -- cgit