From fcf83dbf9effab3bd98bad2b83b2468b7eb05cfd Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Fri, 9 Jun 2017 11:34:21 -0700 Subject: Initial import of code. --- source/core/hash.h | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 105 insertions(+) create mode 100644 source/core/hash.h (limited to 'source/core/hash.h') diff --git a/source/core/hash.h b/source/core/hash.h new file mode 100644 index 000000000..07327a415 --- /dev/null +++ b/source/core/hash.h @@ -0,0 +1,105 @@ +#ifndef CORELIB_HASH_H +#define CORELIB_HASH_H + +#include "slang-math.h" +#include + +namespace CoreLib +{ + namespace Basic + { + + inline int GetHashCode(double key) + { + return FloatAsInt((float)key); + } + inline int GetHashCode(float key) + { + return FloatAsInt(key); + } + inline int GetHashCode(const char * buffer) + { + if (!buffer) + return 0; + int hash = 0; + int c; + auto str = buffer; + c = *str++; + while (c) + { + hash = c + (hash << 6) + (hash << 16) - hash; + c = *str++; + } + return hash; + } + inline int GetHashCode(char * buffer) + { + return GetHashCode(const_cast(buffer)); + } + + template + class Hash + { + public: + }; + template<> + class Hash<1> + { + public: + template + static int GetHashCode(TKey & key) + { + return (int)key; + } + }; + template<> + class Hash<0> + { + public: + template + static int GetHashCode(TKey & key) + { + return key.GetHashCode(); + } + }; + template + class PointerHash + {}; + template<> + class PointerHash<1> + { + public: + template + static int GetHashCode(TKey const& key) + { + return (int)((CoreLib::PtrInt)key) / 16; // sizeof(typename std::remove_pointer::type); + } + }; + template<> + class PointerHash<0> + { + public: + template + static int GetHashCode(TKey & key) + { + return Hash::value || std::is_enum::value>::GetHashCode(key); + } + }; + + template + int GetHashCode(const TKey & key) + { + return PointerHash::value>::GetHashCode(key); + } + + template + int GetHashCode(TKey & key) + { + return PointerHash::value>::GetHashCode(key); + } + + + } +} + +#endif \ No newline at end of file -- cgit v1.2.3