Blender V4.3
BLI_probing_strategies.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
7#include <numeric>
8
46#include <limits>
47
48#include "BLI_sys_types.h"
49
50namespace blender {
51
58 private:
59 uint64_t hash_;
60
61 public:
63
64 void next()
65 {
66 hash_++;
67 }
68
69 uint64_t get() const
70 {
71 return hash_;
72 }
73
75 {
76 return std::numeric_limits<int64_t>::max();
77 }
78};
79
92 private:
93 uint64_t original_hash_;
94 uint64_t current_hash_;
95 uint64_t iteration_;
96
97 public:
99 : original_hash_(hash), current_hash_(hash), iteration_(1)
100 {
101 }
102
103 void next()
104 {
105 current_hash_ = original_hash_ + ((iteration_ * iteration_ + iteration_) >> 1);
106 iteration_++;
107 }
108
109 uint64_t get() const
110 {
111 return current_hash_;
112 }
113
115 {
116 return 1;
117 }
118};
119
130template<uint64_t LinearSteps = 1, bool PreShuffle = false> class PythonProbingStrategy {
131 private:
132 uint64_t hash_;
133 uint64_t perturb_;
134
135 public:
136 PythonProbingStrategy(const uint64_t hash) : hash_(hash), perturb_(hash)
137 {
138 if (PreShuffle) {
139 this->next();
140 }
141 }
142
143 void next()
144 {
145 perturb_ >>= 5;
146 hash_ = 5 * hash_ + 1 + perturb_;
147 }
148
149 uint64_t get() const
150 {
151 return hash_;
152 }
153
155 {
156 return LinearSteps;
157 }
158};
159
165template<uint64_t LinearSteps = 2, bool PreShuffle = false> class ShuffleProbingStrategy {
166 private:
167 uint64_t hash_;
168 uint64_t perturb_;
169
170 public:
171 ShuffleProbingStrategy(const uint64_t hash) : hash_(hash), perturb_(hash)
172 {
173 if (PreShuffle) {
174 this->next();
175 }
176 }
177
178 void next()
179 {
180 if (perturb_ != 0) {
181 perturb_ >>= 10;
182 hash_ = ((hash_ >> 16) ^ hash_) * 0x45d9f3b + perturb_;
183 }
184 else {
185 hash_ = 5 * hash_ + 1;
186 }
187 }
188
189 uint64_t get() const
190 {
191 return hash_;
192 }
193
195 {
196 return LinearSteps;
197 }
198};
199
204
205/* Turning off clang format here, because otherwise it will mess up the alignment between the
206 * macros. */
207// clang-format off
208
222#define SLOT_PROBING_BEGIN(PROBING_STRATEGY, HASH, MASK, R_SLOT_INDEX) \
223 PROBING_STRATEGY probing_strategy(HASH); \
224 do { \
225 int64_t linear_offset = 0; \
226 uint64_t current_hash = probing_strategy.get(); \
227 do { \
228 int64_t R_SLOT_INDEX = int64_t((current_hash + uint64_t(linear_offset)) & MASK);
229
230#define SLOT_PROBING_END() \
231 } while (++linear_offset < probing_strategy.linear_steps()); \
232 probing_strategy.next(); \
233 } while (true)
234
235// clang-format on
236
237} // namespace blender
#define hash
Definition noise.c:154
__int64 int64_t
Definition stdint.h:89
unsigned __int64 uint64_t
Definition stdint.h:90