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 197 198 199 200
|
//
// Hash.h
//
// Library: Foundation
// Package: Hashing
// Module: Hash
//
// Definition of the Hash class.
//
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// hashRange Copyright 2005-2014 Daniel James.
// (Extracted from Boost 1.75.0 lib and adapted for poco on 2021-03-31)
//
// SPDX-License-Identifier: BSL-1.0
//
#ifndef Foundation_Hash_INCLUDED
#define Foundation_Hash_INCLUDED
#include "Poco/Foundation.h"
#include "Poco/Types.h"
#include <cstddef>
#if defined(_MSC_VER)
# define POCO_HASH_ROTL32(x, r) _rotl(x,r)
#else
# define POCO_HASH_ROTL32(x, r) (x << r) | (x >> (32 - r))
#endif
namespace Poco {
std::size_t Foundation_API hash(Int8 n);
std::size_t Foundation_API hash(UInt8 n);
std::size_t Foundation_API hash(Int16 n);
std::size_t Foundation_API hash(UInt16 n);
std::size_t Foundation_API hash(Int32 n);
std::size_t Foundation_API hash(UInt32 n);
std::size_t Foundation_API hash(Int64 n);
std::size_t Foundation_API hash(UInt64 n);
std::size_t Foundation_API hash(const std::string& str);
template <class T>
struct Hash
/// A generic hash function.
{
std::size_t operator () (T value) const
/// Returns the hash for the given value.
{
return Poco::hash(value);
}
};
//
// inlines
//
inline std::size_t hash(Int8 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt8 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int16 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt16 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int32 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt32 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(Int64 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
inline std::size_t hash(UInt64 n)
{
return static_cast<std::size_t>(n)*2654435761U;
}
namespace Impl {
template <typename SizeT>
inline void hashCombine(SizeT& seed, SizeT value)
{
seed ^= value + 0x9e3779b9 + (seed<<6) + (seed>>2);
}
inline void hashCombine(Poco::UInt32& h1, Poco::UInt32 k1)
{
const uint32_t c1 = 0xcc9e2d51;
const uint32_t c2 = 0x1b873593;
k1 *= c1;
k1 = POCO_HASH_ROTL32(k1,15);
k1 *= c2;
h1 ^= k1;
h1 = POCO_HASH_ROTL32(h1,13);
h1 = h1*5+0xe6546b64;
}
#if defined(POCO_PTR_IS_64_BIT) && !(defined(__GNUC__) && ULONG_MAX == 0xffffffff)
inline void hashCombine(Poco::UInt64& h, Poco::UInt64 k)
{
const Poco::UInt64 m = UINT64_C(0xc6a4a7935bd1e995);
const int r = 47;
k *= m;
k ^= k >> r;
k *= m;
h ^= k;
h *= m;
// Completely arbitrary number, to
// prevent zeros from hashing to 0.
h += 0xe6546b64;
}
#endif // POCO_PTR_IS_64_BIT
} // namespace Impl
template <class T>
inline void hashCombine(std::size_t& seed, T const& v)
{
Hash<T> hasher;
Impl::hashCombine(seed, hasher(v));
}
template <class It>
inline std::size_t hashRange(It first, It last)
{
std::size_t seed = 0;
for(; first != last; ++first)
{
hashCombine<typename std::iterator_traits<It>::value_type>(seed, *first);
}
return seed;
}
template <class It>
inline void hashRange(std::size_t& seed, It first, It last)
{
for(; first != last; ++first)
{
hashCombine<typename std::iterator_traits<It>::value_type>(seed, *first);
}
}
} // namespace Poco
#endif // Foundation_Hash_INCLUDED
|