File size: 13,340 Bytes
d1be154 | 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 | #pragma once
#include <atomic>
#include <chrono>
namespace tf {
/**
* @brief rounds the given 64-bit unsigned integer to the nearest power of 2
*/
template <typename T, std::enable_if_t<
(std::is_unsigned_v<std::decay_t<T>> && sizeof(T) == 8), void
>* = nullptr>
constexpr T next_pow2(T x) {
if(x == 0) return 1;
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x |= x >> 32;
x++;
return x;
}
/**
* @brief rounds the given 32-bit unsigned integer to the nearest power of 2
*/
template <typename T, std::enable_if_t<
(std::is_unsigned_v<std::decay_t<T>> && sizeof(T) == 4), void
>* = nullptr>
constexpr T next_pow2(T y) {
if(y == 0) return 1;
y--;
y |= y >> 1;
y |= y >> 2;
y |= y >> 4;
y |= y >> 8;
y |= y >> 16;
y++;
return y;
}
/**
* @brief checks if the given number is a power of 2
*
* This function determines if the given integer is a power of 2.
*
* @tparam T The type of the input. Must be an integral type.
* @param x The integer to check.
* @return `true` if `x` is a power of 2, otherwise `false`.
*
* @attention This function is constexpr and can be evaluated at compile time.
*
*/
template <typename T, std::enable_if_t<
std::is_integral_v<std::decay_t<T>>, void>* = nullptr
>
constexpr bool is_pow2(const T& x) {
return x && (!(x&(x-1)));
}
/**
* @brief computes the floor of the base-2 logarithm of a number using count-leading-zeros (CTL).
*
* This function efficiently calculates the floor of `log2(n)` for both 32-bit and 64-bit integers.
*
* @tparam T integer type (uint32_t or uint64_t).
* @param n input number.
* @return floor of `log2(n)`
*/
template <typename T>
constexpr size_t floor_log2(T n) {
static_assert(std::is_unsigned_v<T>, "log2 only supports unsigned integer types");
#if defined(_MSC_VER)
unsigned long index;
if constexpr (sizeof(T) == 8) {
_BitScanReverse64(&index, n);
} else {
_BitScanReverse(&index, static_cast<unsigned long>(n));
}
return static_cast<size_t>(index);
#elif defined(__GNUC__) || defined(__clang__)
if constexpr (sizeof(T) == 8) {
return 63 - __builtin_clzll(n);
} else {
return 31 - __builtin_clz(n);
}
#else
// Portable fallback: Uses bit shifts to count leading zeros manually
size_t log = 0;
while (n >>= 1) {
++log;
}
return log;
#endif
}
/**
@brief returns the floor of `log2(N)` at compile time
*/
template<size_t N>
constexpr size_t static_floor_log2() {
return (N < 2) ? 0 : 1 + static_floor_log2<N / 2>();
//auto log = 0;
//while (N >>= 1) {
// ++log;
//}
//return log;
}
/**
* @brief finds the median of three numbers pointed to by iterators using the given comparator
*
* This function determines the median value of the elements pointed to by
* three random-access iterators using the provided comparator.
*
* @tparam RandItr The type of the random-access iterator.
* @tparam C The type of the comparator.
* @param l Iterator to the first element.
* @param m Iterator to the second element.
* @param r Iterator to the third element.
* @param cmp The comparator used to compare the dereferenced iterator values.
* @return The iterator pointing to the median value among the three elements.
*
*/
template <typename RandItr, typename C>
RandItr median_of_three(RandItr l, RandItr m, RandItr r, C cmp) {
return cmp(*l, *m) ? (cmp(*m, *r) ? m : (cmp(*l, *r) ? r : l ))
: (cmp(*r, *m) ? m : (cmp(*r, *l) ? r : l ));
}
/**
* @brief finds the pseudo median of a range of items using a spread of nine numbers
*
* This function computes an approximate median of a range of items by sampling
* nine values spread across the range and finding their median. It uses a
* combination of the `median_of_three` function to determine the pseudo median.
*
* @tparam RandItr The type of the random-access iterator.
* @tparam C The type of the comparator.
* @param beg Iterator to the beginning of the range.
* @param end Iterator to the end of the range.
* @param cmp The comparator used to compare the dereferenced iterator values.
* @return The iterator pointing to the pseudo median of the range.
*
* @attention The pseudo median is an approximation of the true median and may not
* be the exact middle value of the range.
*
*/
template <typename RandItr, typename C>
RandItr pseudo_median_of_nine(RandItr beg, RandItr end, C cmp) {
size_t N = std::distance(beg, end);
size_t offset = N >> 3;
return median_of_three(
median_of_three(beg, beg+offset, beg+(offset*2), cmp),
median_of_three(beg+(offset*3), beg+(offset*4), beg+(offset*5), cmp),
median_of_three(beg+(offset*6), beg+(offset*7), end-1, cmp),
cmp
);
}
/**
* @brief sorts two elements of dereferenced iterators using the given comparison function
*
* This function compares two elements pointed to by iterators and swaps them
* if they are out of order according to the provided comparator.
*
* @tparam Iter The type of the iterator.
* @tparam Compare The type of the comparator.
* @param a Iterator to the first element.
* @param b Iterator to the second element.
* @param comp The comparator used to compare the dereferenced iterator values.
*
*/
template<typename Iter, typename Compare>
void sort2(Iter a, Iter b, Compare comp) {
if (comp(*b, *a)) std::iter_swap(a, b);
}
/**
* @brief Sorts three elements of dereferenced iterators using the given comparison function.
*
* This function sorts three elements pointed to by iterators in ascending order
* according to the provided comparator. The sorting is performed using a sequence
* of calls to the `sort2` function to ensure the correct order of elements.
*
* @tparam Iter The type of the iterator.
* @tparam Compare The type of the comparator.
* @param a Iterator to the first element.
* @param b Iterator to the second element.
* @param c Iterator to the third element.
* @param comp The comparator used to compare the dereferenced iterator values.
*
*/
template<typename Iter, typename Compare>
void sort3(Iter a, Iter b, Iter c, Compare comp) {
sort2(a, b, comp);
sort2(b, c, comp);
sort2(a, b, comp);
}
/**
* @brief generates a program-wide unique ID of the given type in a thread-safe manner
*
* This function provides a globally unique identifier of the specified integral type.
* It uses a static `std::atomic` counter to ensure thread safety and increments the
* counter in a relaxed memory ordering for efficiency.
*
* @tparam T The type of the ID to generate. Must be an integral type.
* @return A unique ID of type `T`.
*
* @attention The uniqueness of the ID is guaranteed only within the program's lifetime.
* @attention The function does not throw exceptions.
*
*/
template <typename T, std::enable_if_t<std::is_integral_v<T>, void>* = nullptr>
T unique_id() {
static std::atomic<T> counter{0};
return counter.fetch_add(1, std::memory_order_relaxed);
}
/**
* @brief updates an atomic variable with the maximum value
*
* This function atomically updates the provided atomic variable `v` to hold
* the maximum of its current value and `max_v`. The update is performed using
* a relaxed memory ordering for efficiency in non-synchronizing contexts.
*
* @tparam T The type of the atomic variable. Must be trivially copyable and comparable.
* @param v The atomic variable to update.
* @param max_v The value to compare with the current value of `v`.
*
* @attention If multiple threads call this function concurrently, the value of `v`
* will be the maximum value seen across all threads.
*
*/
template <typename T>
inline void atomic_max(std::atomic<T>& v, const T& max_v) noexcept {
T prev = v.load(std::memory_order_relaxed);
while(prev < max_v &&
!v.compare_exchange_weak(prev, max_v, std::memory_order_relaxed,
std::memory_order_relaxed)) {
}
}
/**
* @brief updates an atomic variable with the minimum value
*
* This function atomically updates the provided atomic variable `v` to hold
* the minimum of its current value and `min_v`. The update is performed using
* a relaxed memory ordering for efficiency in non-synchronizing contexts.
*
* @tparam T The type of the atomic variable. Must be trivially copyable and comparable.
* @param v The atomic variable to update.
* @param min_v The value to compare with the current value of `v`.
*
* @attention If multiple threads call this function concurrently, the value of `v`
* will be the minimum value seen across all threads.
*
*/
template <typename T>
inline void atomic_min(std::atomic<T>& v, const T& min_v) noexcept {
T prev = v.load(std::memory_order_relaxed);
while(prev > min_v &&
!v.compare_exchange_weak(prev, min_v, std::memory_order_relaxed,
std::memory_order_relaxed)) {
}
}
/**
* @brief generates a random seed based on the current system clock
*
* This function returns a seed value derived from the number of clock ticks
* since the epoch as measured by the system clock. The seed can be used
* to initialize random number generators.
*
* @tparam T The type of the returned seed. Must be an integral type.
* @return A seed value based on the system clock.
*
*/
template <typename T>
inline T seed() noexcept {
return std::chrono::system_clock::now().time_since_epoch().count();
}
/**
* @brief counts the number of trailing zeros in an integer.
*
* This function provides a portable implementation for counting the number of
* trailing zeros across different platforms and integer sizes (32-bit and 64-bit).
*
* @tparam T integer type (32-bit or 64-bit).
* @param x non-zero integer to count trailing zeros from
* @return the number of trailing zeros in @c x
*
* @attention
* The behavior is undefined when @c x is 0.
*/
template <typename T, typename = std::enable_if_t<std::is_unsigned_v<T>>>
auto ctz(T x) {
#if defined(_MSC_VER)
unsigned long index;
if constexpr (sizeof(T) == 8) {
_BitScanForward64(&index, x);
} else {
_BitScanForward(&index, (unsigned long)x);
}
return index;
#elif defined(__GNUC__) || defined(__clang__)
if constexpr (sizeof(T) == 8) {
return __builtin_ctzll(x);
} else {
return __builtin_ctz(x);
}
#else
size_t r = 0;
while ((x & 1) == 0) {
x >>= 1;
r++;
}
return r;
#endif
}
// ------------------------------------------------------------------------------------------------
// coprime
// ------------------------------------------------------------------------------------------------
/**
* @brief computes a coprime of a given number
*
* This function finds the largest number less than N that is coprime (i.e., has a greatest common divisor of 1) with @c N.
* If @c N is less than 3, it returns 1 as a default coprime.
*
* @param N input number for which a coprime is to be found.
* @return the largest number < @c N that is coprime to N
*/
constexpr size_t coprime(size_t N) {
if(N < 3) {
return 1;
}
for (size_t x = N; --x > 0;) {
if (std::gcd(x, N) == 1) {
return x;
}
}
return 1;
}
/**
* @brief generates a compile-time array of coprimes for numbers from 0 to N-1
*
* This function constructs a constexpr array where each element at index `i` contains a coprime of `i`
* (the largest number less than `i` that is coprime to it).
*
* @tparam N the size of the array to generate (should be greater than 0).
* @return a constexpr array of size @c N where each index holds a coprime of its value.
*/
template <size_t N>
constexpr std::array<size_t, N> make_coprime_lut() {
static_assert(N>0, "N must be greater than 0");
std::array<size_t, N> coprimes{};
for (size_t n = 0; n < N; ++n) {
coprimes[n] = coprime(n);
}
return coprimes;
}
//class XorShift64 {
//
// public:
//
// explicit XorShift64(uint64_t seed) : _state(seed) {}
//
// uint64_t next() {
// _state ^= _state >> 12;
// _state ^= _state << 25;
// _state ^= _state >> 27;
// return _state * 0x2545F4914F6CDD1DULL; // Scramble for better randomness
// }
//
// size_t random_range(size_t min, size_t max) {
// return min + (next() % (max - min + 1));
// }
//
// private:
//
// uint64_t _state;
//};
//inline int generate_random_excluding(int worker_id, int W, XorShift64& rng) {
// int random_number = rng.random_range(0, 2 * W - 2); // Range: [0, 2W-2]
// return random_number + (random_number >= worker_id); // Skip worker_id
//}
//
//
//class Xoroshiro128Plus {
//
// public:
//
// explicit Xoroshiro128Plus(uint64_t seed1, uint64_t seed2) : _state{seed1, seed2} {}
//
// uint64_t next() {
// uint64_t s0 = _state[0];
// uint64_t s1 = _state[1];
// uint64_t result = s0 + s1;
//
// s1 ^= s0;
// _state[0] = _rotl(s0, 55) ^ s1 ^ (s1 << 14); // Scramble _state
// _state[1] = _rotl(s1, 36);
//
// return result;
// }
//
// int random_range(int min, int max) {
// return min + (next() % (max - min + 1));
// }
//
// private:
//
// std::array<uint64_t, 2> _state;
//
// static uint64_t _rotl(uint64_t x, int k) {
// return (x << k) | (x >> (64 - k));
// }
//};
} // end of namespace tf -----------------------------------------------------
|