rexlib 0.1.0
Device-agnostic array computing library for CryoEM and CryoET image processing, with a pluggable backend architecture
Loading...
Searching...
No Matches
flagset.hpp
1// SPDX-License-Identifier: GPL-3.0-only
2
3#pragma once
4
5#include "../platform/attributes.hpp"
6#include "../platform/constexpr.hpp"
7
8#include <initializer_list>
9#include <type_traits>
10#include <functional>
11
12namespace rexlib
13{
14
23template<typename B>
24class flagset {
25public:
30 using bit_type = B;
31
36 using underlying_type = typename std::underlying_type<bit_type>::type;
37
42 using unsigned_type = typename std::make_unsigned<underlying_type>::type;
43
49 REXLIB_CONSTEXPR flagset() noexcept;
50
57 REXLIB_CONSTEXPR flagset(bit_type bit) noexcept;
58
64 explicit REXLIB_CONSTEXPR flagset(underlying_type data) noexcept;
65
75 template<typename It>
76 REXLIB_CONSTEXPR flagset(It first, It last) noexcept;
77
84 REXLIB_CONSTEXPR flagset(std::initializer_list<bit_type> bits) noexcept;
85
91 flagset(const flagset &other) = default;
92
98 flagset(flagset &&other) noexcept = default;
99
104 ~flagset() = default;
105
112 flagset& operator=(const flagset &other) = default;
113
120 flagset& operator=(flagset &&other) noexcept = default;
121
128 REXLIB_CONSTEXPR operator bool() const noexcept;
129
136 explicit REXLIB_CONSTEXPR operator underlying_type() const noexcept;
137
144 REXLIB_CONSTEXPR flagset& operator|=(const flagset& rhs) noexcept;
145
152 REXLIB_CONSTEXPR flagset& operator&=(const flagset& rhs) noexcept;
153
160 REXLIB_CONSTEXPR flagset& operator^=(const flagset& rhs) noexcept;
161
170 friend REXLIB_CONSTEXPR
171 bool operator==(const flagset& lhs, const flagset& rhs) noexcept
172 {
173 return lhs.m_data == rhs.m_data;
174 }
175
184 friend REXLIB_CONSTEXPR
185 bool operator!=(const flagset& lhs, const flagset& rhs) noexcept
186 {
187 return lhs.m_data != rhs.m_data;
188 }
189
195 REXLIB_CONSTEXPR underlying_type get_bits() const noexcept;
196
204 REXLIB_CONSTEXPR bool contains(const bit_type& bit) const noexcept;
205
213 REXLIB_CONSTEXPR bool all_of(const flagset& other) const noexcept;
214
222 REXLIB_CONSTEXPR bool any_of(const flagset& other) const noexcept;
223
231 REXLIB_CONSTEXPR bool none_of(const flagset& other) const noexcept;
232
240 REXLIB_CONSTEXPR bool only_of(const flagset& other) const noexcept;
241
247 REXLIB_CONSTEXPR int count() const noexcept;
248
255 REXLIB_CONSTEXPR bool parity() const noexcept;
256
263 REXLIB_CONSTEXPR bool has_single_bit() const noexcept;
264
272 REXLIB_CONSTEXPR flagset& set(const flagset& other, bool value) noexcept;
273
280 REXLIB_CONSTEXPR flagset& set(const flagset& other) noexcept;
281
287 REXLIB_CONSTEXPR flagset& clear() noexcept;
288
295 REXLIB_CONSTEXPR flagset& clear(const flagset& other) noexcept;
296
303 REXLIB_CONSTEXPR flagset& toggle(const flagset& other) noexcept;
304
305 friend REXLIB_CONSTEXPR
306 flagset operator|(const flagset& lhs, const flagset& rhs) noexcept
307 {
308 return flagset(lhs.m_data | rhs.m_data);
309 }
310
311 friend REXLIB_CONSTEXPR
312 flagset operator&(const flagset& lhs, const flagset& rhs) noexcept
313 {
314 return flagset(lhs.m_data & rhs.m_data);
315 }
316
317 friend REXLIB_CONSTEXPR
318 flagset operator^(const flagset& lhs, const flagset& rhs) noexcept
319 {
320 return flagset(lhs.m_data ^ rhs.m_data);
321 }
322
323private:
324 unsigned_type m_data;
325};
326
327} // namespace rexlib
328
329template <typename B>
330struct std::hash<rexlib::flagset<B>>
331{
332 REXLIB_CONSTEXPR size_t operator()(rexlib::flagset<B> b) const noexcept;
333};
334
335#include "flagset.inl"
Stores a set of bits in the form of flags Similar to std::bitset but instead of a index based interfa...
Definition flagset.hpp:24
REXLIB_CONSTEXPR flagset & set(const flagset &other, bool value) noexcept
Set a particular set of flags to the desired value.
B bit_type
The provided enum declaration of the flag bits.
Definition flagset.hpp:30
REXLIB_CONSTEXPR bool none_of(const flagset &other) const noexcept
Checks if none of a set of flags are present in this.
REXLIB_CONSTEXPR bool contains(const bit_type &bit) const noexcept
Tests if the requested flag is set.
REXLIB_CONSTEXPR bool parity() const noexcept
Computes the parity of the stored flags.
typename std::underlying_type< bit_type >::type underlying_type
The underlying type of the provided enum.
Definition flagset.hpp:36
REXLIB_CONSTEXPR bool all_of(const flagset &other) const noexcept
Checks if all of a set of flags are present in this.
REXLIB_CONSTEXPR bool any_of(const flagset &other) const noexcept
Checks if any of a set of flags are present in this.
typename std::make_unsigned< underlying_type >::type unsigned_type
Unsigned underlying type of the provided enum.
Definition flagset.hpp:42
REXLIB_CONSTEXPR flagset & clear() noexcept
Clears all flags.
REXLIB_CONSTEXPR underlying_type get_bits() const noexcept
Obtain the underlying integer representation of the flags.
REXLIB_CONSTEXPR bool only_of(const flagset &other) const noexcept
Checks if only a set of flags are present in this.
REXLIB_CONSTEXPR int count() const noexcept
Counts the number of flags that are present.
REXLIB_CONSTEXPR bool has_single_bit() const noexcept
Checks if only one flag is present.
REXLIB_CONSTEXPR flagset & toggle(const flagset &other) noexcept
Toggles a particular set of flags.
friend REXLIB_CONSTEXPR bool operator!=(const flagset &lhs, const flagset &rhs) noexcept
Compares for inequality with another flagset.
Definition flagset.hpp:185
REXLIB_CONSTEXPR flagset() noexcept
Empty constructor. It initializes the set as none of the flags were set.
Arrays, the operations on them and the devices that run those operations.
Definition cpu_device.hpp:10
REXLIB_NODISCARD REXLIB_CONSTEXPR std::size_t bit(std::size_t i) noexcept
Returns a std::size_t type with a single bit set at the provided index.