Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
DiscontinuousIterator.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
24#include <bitset>
25
26namespace shammath {
44 template<class T>
46 public:
47 constexpr static u32 bitcount = sizeof(T) * 8;
48
49 T offset;
50
51 std::bitset<bitcount> max;
52 T tmax;
53 std::bitset<bitcount> current;
54 int firstbit;
55 bool done;
56
57 DiscontinuousIterator(T tmin, T tmax)
58 : offset(tmin), tmax(tmax - tmin), max(tmax - tmin), current(0) {
59
60 done = !(tmin < tmax);
61
62 for (firstbit = bitcount - 1; firstbit >= 0; firstbit--) {
63 if (max[firstbit]) {
64 break;
65 }
66 }
67 }
68
69 bool is_done() { return done; }
70
71 T next() {
72 T tmp = get();
73 advance_it();
74 return tmp;
75 }
76
77 T get() { return current.to_ullong() + offset; }
78
79 void advance_it() {
80 if (!done) {
81 do {
82 bool carry = true; // backward
83 int pointer = firstbit;
84 while (carry && !done) {
85 if (current[pointer]) {
86 current.flip(pointer);
87 pointer--;
88 if (pointer < 0) {
89 done = true;
90 }
91 } else {
92 carry = false;
93 current.flip(pointer);
94 }
95 }
96 } while (current.to_ullong() >= tmax);
97 }
98 }
99 };
100
101} // namespace shammath
std::uint32_t u32
32 bit unsigned integer
Discontinuous Iterator will iterate over every values in an integer set in the most discontinuous way...
namespace for math utility
Definition AABB.hpp:26