Debian Codegraph @

src/lib/tsl/hopscotch_growth_policy.h

raw symbols SCIP
1 /**
2 * MIT License
3 *
4 * Copyright (c) 2018 Thibaut Goetghebuer-Planchon <[email protected]>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
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 * Only activate tsl_hh_assert if TSL_DEBUG is defined.
42 * This way we avoid the performance hit when NDEBUG is not defined with assert as tsl_hh_assert is used a lot
43 * (people usually compile with "-O3" and not "-O3 -DNDEBUG").
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 * If exceptions are enabled, throw the exception passed in parameter, otherwise call std::terminate.
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 * Grow the hash table by a factor of GrowthFactor keeping the bucket count to a power of two. It allows
73 * the table to use a mask operation instead of a modulo operation to map a hash to a bucket.
74 *
75 * GrowthFactor must be a power of two >= 2.
76 */
77 template<std::size_t GrowthFactor>
78 class power_of_two_growth_policy {
79 public:
80 /**
81 * Called on the hash table creation and on rehash. The number of buckets for the table is passed in parameter.
82 * This number is a minimum, the policy may update this value with a higher value if needed (but not lower).
83 *
84 * If 0 is given, min_bucket_count_in_out must still be 0 after the policy creation and
85 * bucket_for_hash must always return 0 in this case.
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 * Return the bucket [0, bucket_count()) to which the hash belongs.
103 * If bucket_count() is 0, it must always return 0.
104 */
105 std::size_t bucket_for_hash(std::size_t hash) const noexcept {
106 return hash & m_mask;
107 }
108
109 /**
110 * Return the bucket count to use when the bucket array grows on rehash.
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 * Return the maximum number of buckets supported by the policy.
122 */
123 std::size_t max_bucket_count() const {
124 // Largest power of two.
125 return (std::numeric_limits<std::size_t>::max() / 2) + 1;
126 }
127
128 /**
129 * Reset the growth policy as if it was created with a bucket count of 0.
130 * After a clear, the policy must always return 0 when bucket_for_hash is called.
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 * Grow the hash table by GrowthFactor::num / GrowthFactor::den and use a modulo to map a hash
167 * to a bucket. Slower but it can be useful if you want a slower growth.
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 // MOD_PRIME[iprime](hash) returns hash % PRIMES[iprime]. This table allows for faster modulo as the
257 // compiler can optimize the modulo code better with a constant known at the compilation.
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 * Grow the hash table by using prime numbers as bucket count. Slower than tsl::hh::power_of_two_growth_policy in
276 * general but will probably distribute the values around better in the buckets with a poor hash function.
277 *
278 * To allow the compiler to optimize the modulo operation, a lookup table is used with constant primes numbers.
279 *
280 * With a switch the code would look like:
281 * \code
282 * switch(iprime) { // iprime is the current prime of the hash table
283 * case 0: hash % 5ul;
284 * break;
285 * case 1: hash % 17ul;
286 * break;
287 * case 2: hash % 29ul;
288 * break;
289 * ...
290 * }
291 * \endcode
292 *
293 * Due to the constant variable in the modulo the compiler is able to optimize the operation
294 * by a series of multiplications, substractions and shifts.
295 *
296 * The 'hash % 5' could become something like 'hash - (hash * 0xCCCCCCCD) >> 34) * 5' in a 64 bits environment.
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