| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef TSL_HOPSCOTCH_GROWTH_POLICY_H |
| 25 |
#define TSL_HOPSCOTCH_GROWTH_POLICY_H |
| 26 |
|
| 27 |
|
| 28 |
#include <algorithm> |
| 29 |
#include <array> |
| 30 |
#include <climits> |
| 31 |
#include <cmath> |
| 32 |
#include <cstddef> |
| 33 |
#include <cstdint> |
| 34 |
#include <iterator> |
| 35 |
#include <limits> |
| 36 |
#include <ratio> |
| 37 |
#include <stdexcept> |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
#ifdef TSL_DEBUG |
| 46 |
# define tsl_hh_assert(expr) assert(expr) |
| 47 |
#else |
| 48 |
# define tsl_hh_assert(expr) (static_cast<void>(0)) |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || (defined (_MSC_VER) && defined (_CPPUNWIND))) && !defined(TSL_NO_EXCEPTIONS) |
| 56 |
# define TSL_HH_THROW_OR_TERMINATE(ex, msg) throw ex(msg) |
| 57 |
#else |
| 58 |
# define TSL_HH_NO_EXCEPTIONS |
| 59 |
# ifdef NDEBUG |
| 60 |
# define TSL_HH_THROW_OR_TERMINATE(ex, msg) std::terminate() |
| 61 |
# else |
| 62 |
# include <iostream> |
| 63 |
# define TSL_HH_THROW_OR_TERMINATE(ex, msg) do { std::cerr << msg << std::endl; std::terminate(); } while(0) |
| 64 |
# endif |
| 65 |
#endif |
| 66 |
|
| 67 |
|
| 68 |
namespace tsl { |
| 69 |
namespace hh { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
template<std::size_t GrowthFactor> |
| 78 |
class power_of_two_growth_policy { |
| 79 |
public: |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
explicit power_of_two_growth_policy(std::size_t& min_bucket_count_in_out) { |
| 88 |
if(min_bucket_count_in_out > max_bucket_count()) { |
| 89 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 90 |
} |
| 91 |
|
| 92 |
if(min_bucket_count_in_out > 0) { |
| 93 |
min_bucket_count_in_out = round_up_to_power_of_two(min_bucket_count_in_out); |
| 94 |
m_mask = min_bucket_count_in_out - 1; |
| 95 |
} |
| 96 |
else { |
| 97 |
m_mask = 0; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
std::size_t bucket_for_hash(std::size_t hash) const noexcept { |
| 106 |
return hash & m_mask; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
std::size_t next_bucket_count() const { |
| 113 |
if((m_mask + 1) > max_bucket_count() / GrowthFactor) { |
| 114 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 115 |
} |
| 116 |
|
| 117 |
return (m_mask + 1) * GrowthFactor; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
std::size_t max_bucket_count() const { |
| 124 |
|
| 125 |
return (std::numeric_limits<std::size_t>::max() / 2) + 1; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
void clear() noexcept { |
| 133 |
m_mask = 0; |
| 134 |
} |
| 135 |
|
| 136 |
private: |
| 137 |
static std::size_t round_up_to_power_of_two(std::size_t value) { |
| 138 |
if(is_power_of_two(value)) { |
| 139 |
return value; |
| 140 |
} |
| 141 |
|
| 142 |
if(value == 0) { |
| 143 |
return 1; |
| 144 |
} |
| 145 |
|
| 146 |
--value; |
| 147 |
for(std::size_t i = 1; i < sizeof(std::size_t) * CHAR_BIT; i *= 2) { |
| 148 |
value |= value >> i; |
| 149 |
} |
| 150 |
|
| 151 |
return value + 1; |
| 152 |
} |
| 153 |
|
| 154 |
static constexpr bool is_power_of_two(std::size_t value) { |
| 155 |
return value != 0 && (value & (value - 1)) == 0; |
| 156 |
} |
| 157 |
|
| 158 |
private: |
| 159 |
static_assert(is_power_of_two(GrowthFactor) && GrowthFactor >= 2, "GrowthFactor must be a power of two >= 2."); |
| 160 |
|
| 161 |
std::size_t m_mask; |
| 162 |
}; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
template<class GrowthFactor = std::ratio<3, 2>> |
| 170 |
class mod_growth_policy { |
| 171 |
public: |
| 172 |
explicit mod_growth_policy(std::size_t& min_bucket_count_in_out) { |
| 173 |
if(min_bucket_count_in_out > max_bucket_count()) { |
| 174 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 175 |
} |
| 176 |
|
| 177 |
if(min_bucket_count_in_out > 0) { |
| 178 |
m_mod = min_bucket_count_in_out; |
| 179 |
} |
| 180 |
else { |
| 181 |
m_mod = 1; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
std::size_t bucket_for_hash(std::size_t hash) const noexcept { |
| 186 |
return hash % m_mod; |
| 187 |
} |
| 188 |
|
| 189 |
std::size_t next_bucket_count() const { |
| 190 |
if(m_mod == max_bucket_count()) { |
| 191 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 192 |
} |
| 193 |
|
| 194 |
const double next_bucket_count = std::ceil(double(m_mod) * REHASH_SIZE_MULTIPLICATION_FACTOR); |
| 195 |
if(!std::isnormal(next_bucket_count)) { |
| 196 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 197 |
} |
| 198 |
|
| 199 |
if(next_bucket_count > double(max_bucket_count())) { |
| 200 |
return max_bucket_count(); |
| 201 |
} |
| 202 |
else { |
| 203 |
return std::size_t(next_bucket_count); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
std::size_t max_bucket_count() const { |
| 208 |
return MAX_BUCKET_COUNT; |
| 209 |
} |
| 210 |
|
| 211 |
void clear() noexcept { |
| 212 |
m_mod = 1; |
| 213 |
} |
| 214 |
|
| 215 |
private: |
| 216 |
static constexpr double REHASH_SIZE_MULTIPLICATION_FACTOR = 1.0 * GrowthFactor::num / GrowthFactor::den; |
| 217 |
static const std::size_t MAX_BUCKET_COUNT = |
| 218 |
std::size_t(double( |
| 219 |
std::numeric_limits<std::size_t>::max() / REHASH_SIZE_MULTIPLICATION_FACTOR |
| 220 |
)); |
| 221 |
|
| 222 |
static_assert(REHASH_SIZE_MULTIPLICATION_FACTOR >= 1.1, "Growth factor should be >= 1.1."); |
| 223 |
|
| 224 |
std::size_t m_mod; |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
namespace detail { |
| 230 |
|
| 231 |
#if SIZE_MAX >= ULLONG_MAX |
| 232 |
#define TSL_HH_NB_PRIMES 51 |
| 233 |
#elif SIZE_MAX >= ULONG_MAX |
| 234 |
#define TSL_HH_NB_PRIMES 40 |
| 235 |
#else |
| 236 |
#define TSL_HH_NB_PRIMES 23 |
| 237 |
#endif |
| 238 |
|
| 239 |
static constexpr const std::array<std::size_t, TSL_HH_NB_PRIMES> PRIMES = {{ |
| 240 |
1u, 5u, 17u, 29u, 37u, 53u, 67u, 79u, 97u, 131u, 193u, 257u, 389u, 521u, 769u, 1031u, |
| 241 |
1543u, 2053u, 3079u, 6151u, 12289u, 24593u, 49157u, |
| 242 |
#if SIZE_MAX >= ULONG_MAX |
| 243 |
98317ul, 196613ul, 393241ul, 786433ul, 1572869ul, 3145739ul, 6291469ul, 12582917ul, |
| 244 |
25165843ul, 50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul, 1610612741ul, |
| 245 |
3221225473ul, 4294967291ul, |
| 246 |
#endif |
| 247 |
#if SIZE_MAX >= ULLONG_MAX |
| 248 |
6442450939ull, 12884901893ull, 25769803751ull, 51539607551ull, 103079215111ull, 206158430209ull, |
| 249 |
412316860441ull, 824633720831ull, 1649267441651ull, 3298534883309ull, 6597069766657ull, |
| 250 |
#endif |
| 251 |
}}; |
| 252 |
|
| 253 |
template<unsigned int IPrime> |
| 254 |
static constexpr std::size_t mod(std::size_t hash) { return hash % PRIMES[IPrime]; } |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
static constexpr const std::array<std::size_t(*)(std::size_t), TSL_HH_NB_PRIMES> MOD_PRIME = {{ |
| 259 |
&mod<0>, &mod<1>, &mod<2>, &mod<3>, &mod<4>, &mod<5>, &mod<6>, &mod<7>, &mod<8>, &mod<9>, &mod<10>, |
| 260 |
&mod<11>, &mod<12>, &mod<13>, &mod<14>, &mod<15>, &mod<16>, &mod<17>, &mod<18>, &mod<19>, &mod<20>, |
| 261 |
&mod<21>, &mod<22>, |
| 262 |
#if SIZE_MAX >= ULONG_MAX |
| 263 |
&mod<23>, &mod<24>, &mod<25>, &mod<26>, &mod<27>, &mod<28>, &mod<29>, &mod<30>, &mod<31>, &mod<32>, |
| 264 |
&mod<33>, &mod<34>, &mod<35>, &mod<36>, &mod<37> , &mod<38>, &mod<39>, |
| 265 |
#endif |
| 266 |
#if SIZE_MAX >= ULLONG_MAX |
| 267 |
&mod<40>, &mod<41>, &mod<42>, &mod<43>, &mod<44>, &mod<45>, &mod<46>, &mod<47>, &mod<48>, &mod<49>, |
| 268 |
&mod<50>, |
| 269 |
#endif |
| 270 |
}}; |
| 271 |
|
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
class prime_growth_policy { |
| 299 |
public: |
| 300 |
explicit prime_growth_policy(std::size_t& min_bucket_count_in_out) { |
| 301 |
auto it_prime = std::lower_bound(detail::PRIMES.begin(), |
| 302 |
detail::PRIMES.end(), min_bucket_count_in_out); |
| 303 |
if(it_prime == detail::PRIMES.end()) { |
| 304 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 305 |
} |
| 306 |
|
| 307 |
m_iprime = static_cast<unsigned int>(std::distance(detail::PRIMES.begin(), it_prime)); |
| 308 |
if(min_bucket_count_in_out > 0) { |
| 309 |
min_bucket_count_in_out = *it_prime; |
| 310 |
} |
| 311 |
else { |
| 312 |
min_bucket_count_in_out = 0; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
std::size_t bucket_for_hash(std::size_t hash) const noexcept { |
| 317 |
return detail::MOD_PRIME[m_iprime](hash); |
| 318 |
} |
| 319 |
|
| 320 |
std::size_t next_bucket_count() const { |
| 321 |
if(m_iprime + 1 >= detail::PRIMES.size()) { |
| 322 |
TSL_HH_THROW_OR_TERMINATE(std::length_error, "The hash table exceeds its maximum size."); |
| 323 |
} |
| 324 |
|
| 325 |
return detail::PRIMES[m_iprime + 1]; |
| 326 |
} |
| 327 |
|
| 328 |
std::size_t max_bucket_count() const { |
| 329 |
return detail::PRIMES.back(); |
| 330 |
} |
| 331 |
|
| 332 |
void clear() noexcept { |
| 333 |
m_iprime = 0; |
| 334 |
} |
| 335 |
|
| 336 |
private: |
| 337 |
unsigned int m_iprime; |
| 338 |
|
| 339 |
static_assert(std::numeric_limits<decltype(m_iprime)>::max() >= detail::PRIMES.size(), |
| 340 |
"The type of m_iprime is not big enough."); |
| 341 |
}; |
| 342 |
|
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
#endif |