1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#pragma once
#include <cstdint>
#include <cstring>
#include <Containers/Pair.h>
#include <Containers/String.h>
#include <Cryptography/xxHash.h>
using namespace Death::Containers;
using namespace Death::Cryptography;
namespace nCine
{
using hash_t = std::uint32_t;
using hash64_t = std::uint64_t;
/** @brief Invalid hash value */
const hash_t NullHash = static_cast<hash_t>(~0);
/// Fowler-Noll-Vo Hash (FNV-1a)
/*!
* For more information: http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
*/
template<class K>
class FNV1aHashFunc
{
public:
hash_t operator()(const K& key) const
{
const unsigned char* bytes = reinterpret_cast<const unsigned char*>(&key);
hash_t hash = static_cast<hash_t>(Seed);
for (unsigned int i = 0; i < sizeof(K); i++) {
hash = fnv1a(bytes[i], hash);
}
return hash;
}
private:
static const hash_t Prime = 0x01000193; // 16777619
static const hash_t Seed = 0x811C9DC5; // 2166136261
inline hash_t fnv1a(const unsigned char oneByte, hash_t hash = Seed) const
{
return (oneByte ^ hash) * Prime;
}
};
#ifndef DOXYGEN_GENERATING_OUTPUT
template<>
class FNV1aHashFunc<char*>
{
public:
hash_t operator()(const char*& key) const
{
const unsigned int length = (unsigned int)strlen(key);
hash_t hash = static_cast<hash_t>(Seed);
for (unsigned int i = 0; i < length; i++) {
hash = fnv1a(key[i], hash);
}
return hash;
}
private:
static const hash_t Prime = 0x01000193; // 16777619
static const hash_t Seed = 0x811C9DC5; // 2166136261
inline hash_t fnv1a(const char oneByte, hash_t hash = Seed) const
{
return (oneByte ^ hash) * Prime;
}
};
template<>
class FNV1aHashFunc<String>
{
public:
hash_t operator()(const String& string) const
{
const unsigned int length = (unsigned int)string.size();
hash_t hash = static_cast<hash_t>(Seed);
for (unsigned int i = 0; i < length; i++) {
hash = fnv1a(static_cast<hash_t>(string[i]), hash);
}
return hash;
}
private:
static const hash_t Prime = 0x01000193; // 16777619
static const hash_t Seed = 0x811C9DC5; // 2166136261
inline hash_t fnv1a(unsigned char oneByte, hash_t hash = Seed) const
{
return (oneByte ^ hash) * Prime;
}
};
template<class F, class S>
class FNV1aHashFunc<Pair<F, S>>
{
public:
hash_t operator()(const Pair<F, S>& pair) const
{
return FNV1aHashFunc<F>()(pair.first()) ^ FNV1aHashFunc<S>()(pair.second());
}
};
#endif
/// xxHash hash function (32-bit)
template<class K>
class xxHash32Func
{
public:
hash_t operator()(const K& key) const
{
return hash_t(xxHash3(reinterpret_cast<const char*>(&key), sizeof(K)));
}
};
#ifndef DOXYGEN_GENERATING_OUTPUT
template<>
class xxHash32Func<char*>
{
public:
hash64_t operator()(const char*& key) const
{
const unsigned int length = (unsigned int)strlen(key);
return hash_t(xxHash3(key, length));
}
};
template<>
class xxHash32Func<String>
{
public:
hash_t operator()(const String& string) const
{
return hash_t(xxHash3(string.data(), string.size()));
}
};
template<class F, class S>
class xxHash32Func<Pair<F, S>>
{
public:
hash_t operator()(const Pair<F, S>& pair) const
{
return xxHash32Func<F>()(pair.first()) ^ xxHash32Func<S>()(pair.second());
}
};
#endif
/// xxHash hash function (64-bit)
template<class K>
class xxHash64Func
{
public:
hash64_t operator()(const K& key) const
{
return hash64_t(xxHash3(reinterpret_cast<const char*>(&key), sizeof(K)));
}
};
#ifndef DOXYGEN_GENERATING_OUTPUT
template<>
class xxHash64Func<char*>
{
public:
hash64_t operator()(const char*& key) const
{
const unsigned int length = (unsigned int)strlen(key);
return hash64_t(xxHash3(key, length));
}
};
template<>
class xxHash64Func<String>
{
public:
hash64_t operator()(const String& string) const
{
return hash64_t(xxHash3(string.data(), string.size()));
}
};
template<class F, class S>
class xxHash64Func<Pair<F, S>>
{
public:
hash64_t operator()(const Pair<F, S>& pair) const
{
return xxHash64Func<F>()(pair.first()) ^ xxHash64Func<S>()(pair.second());
}
};
#endif
}
|