From 652475bbbd203ab54b1f609bad5da397d69f97cc Mon Sep 17 00:00:00 2001 From: jwellbelove Date: Fri, 20 May 2016 13:54:33 +0100 Subject: [PATCH] Small improvement to etl::hash --- src/hash.h | 67 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/src/hash.h b/src/hash.h index a01bba8e..b4f511d5 100644 --- a/src/hash.h +++ b/src/hash.h @@ -36,6 +36,7 @@ SOFTWARE. // The default hash calculation. #include "fnv_1.h" #include "type_traits.h" +#include "static_assert.h" ///\defgroup hash Standard hash calculations ///\ingroup maths @@ -75,9 +76,11 @@ namespace etl /// Specialisation for bool. ///\ingroup hash //*************************************************************************** - template <> + template <> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(bool)); + size_t operator ()(bool v) const { return static_cast(v); @@ -88,9 +91,11 @@ namespace etl /// Specialisation for char. ///\ingroup hash //*************************************************************************** - template <> + template <> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(char)); + size_t operator ()(char v) const { return static_cast(v); @@ -104,6 +109,8 @@ namespace etl template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(signed char)); + size_t operator ()(signed char v) const { return static_cast(v); @@ -117,6 +124,8 @@ namespace etl template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned char)); + size_t operator ()(unsigned char v) const { return static_cast(v); @@ -130,6 +139,8 @@ namespace etl template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(wchar_t)); + size_t operator ()(wchar_t v) const { return static_cast(v); @@ -140,9 +151,11 @@ namespace etl /// Specialisation for short. ///\ingroup hash //*************************************************************************** - template<> + template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(short)); + size_t operator ()(short v) const { return static_cast(v); @@ -156,6 +169,8 @@ namespace etl template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof()); + size_t operator ()(unsigned short v) const { return static_cast(v); @@ -169,6 +184,8 @@ namespace etl template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeofint()); + size_t operator ()(int v) const { return static_cast(v); @@ -179,9 +196,11 @@ namespace etl /// Specialisation for unsigned int. ///\ingroup hash //*************************************************************************** - template<> + template<> struct hash { + STATIC_ASSERT(sizeof(size_t) >= sizeof(unsigned int)); + size_t operator ()(unsigned int v) const { return static_cast(v); @@ -195,15 +214,17 @@ namespace etl template<> struct hash { + // If it fits into a size_t. template - typename etl::enable_if::type + typename etl::enable_if= sizeof(T), size_t>::type operator ()(T v) const { return static_cast(v); } + // If it doesn't fit into a size_t. template - typename etl::enable_if::type + typename etl::enable_if::type operator ()(T v) const { uint8_t* p = reinterpret_cast(&v); @@ -218,15 +239,17 @@ namespace etl template<> struct hash { + // If it fits into a size_t. template - typename etl::enable_if::type + typename etl::enable_if= sizeof(T), size_t>::type operator ()(T v) const { return static_cast(v); } + // If it doesn't fit into a size_t. template - typename etl::enable_if::type + typename etl::enable_if::type operator ()(T v) const { uint8_t* p = reinterpret_cast(&v); @@ -241,15 +264,17 @@ namespace etl template<> struct hash { + // If it fits into a size_t. template - typename etl::enable_if::type + typename etl::enable_if= sizeof(T), size_t>::type operator ()(T v) const { return static_cast(v); } + // If it doesn't fit into a size_t. template - typename etl::enable_if::type + typename etl::enable_if::type operator ()(T v) const { uint8_t* p = reinterpret_cast(&v); @@ -264,15 +289,17 @@ namespace etl template<> struct hash { + // If it fits into a size_t. template - typename etl::enable_if::type + typename etl::enable_if= sizeof(T), size_t>::type operator ()(T v) const { return static_cast(v); } + // If it doesn't fit into a size_t. template - typename etl::enable_if::type + typename etl::enable_if::type operator ()(T v) const { uint8_t* p = reinterpret_cast(&v); @@ -287,6 +314,7 @@ namespace etl template<> struct hash { + // If it's the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -294,6 +322,7 @@ namespace etl return *reinterpret_cast(&v); } + // If it's not the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -307,9 +336,10 @@ namespace etl /// Specialisation for double. ///\ingroup hash //*************************************************************************** - template<> + template<> struct hash { + // If it's the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -317,6 +347,7 @@ namespace etl return *reinterpret_cast(&v); } + // If it's not the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -330,9 +361,10 @@ namespace etl /// Specialisation for long double. ///\ingroup hash //*************************************************************************** - template<> + template<> struct hash { + // If it's the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -340,6 +372,7 @@ namespace etl return *reinterpret_cast(&v); } + // If it's not the same size as a size_t. template typename etl::enable_if::type operator ()(T v) const @@ -353,9 +386,10 @@ namespace etl /// Specialisation for pointers. ///\ingroup hash //*************************************************************************** - template + template struct hash { + // If it's the same size as a size_t. template typename etl::enable_if::type operator ()(U* v) const @@ -363,6 +397,7 @@ namespace etl return reinterpret_cast(v); } + // If it's the same size as a size_t. template typename etl::enable_if::type operator ()(const U* v) const @@ -370,6 +405,7 @@ namespace etl return reinterpret_cast(v); } + // If it's not the same size as a size_t. template typename etl::enable_if::type operator ()(U* v) const @@ -378,6 +414,7 @@ namespace etl return __private_hash__::generic_hash(p, p + sizeof(v)); } + // If it's not the same size as a size_t. template typename etl::enable_if::type operator ()(const U* v) const