libhatchet
Loading...
Searching...
No Matches
hxbitset.hpp
Go to the documentation of this file.
1#pragma once
2// SPDX-FileCopyrightText: © 2025 Adrian Johnston.
3// SPDX-License-Identifier: MIT
4// This file is licensed under the MIT license found in the LICENSE.md file.
5
9
10#include "libhatchet.h"
11
12// HX_USE_MACROS_WITH_MODULE allows including macros alongside the module.
13#if HX_USE_MACROS_WITH_MODULE
14#error Header does not provide macros alone.
15#endif
16
18
19HX_NS_BEGIN_
20
25template<size_t bit_count_>
26class hxbitset {
27public:
29 hxconstexpr hxbitset(void) { this->reset(); }
30
34 hxconstexpr explicit hxbitset(size_t val_);
35
39
47 template<typename int_t_>
48 hxconstexpr hxbitset(std::initializer_list<int_t_> x_);
49
53
56 hxattr_nodiscard hxconstexpr bool operator[](size_t pos_) const;
57
61
65
69
73
77
81
82#if HX_CPLUSPLUS < 202002L // C++20 defaults != from ==.
85 hxattr_nodiscard hxinline hxconstexpr bool operator!=(const hxbitset& x_) const;
86#endif
87
90
93
95 hxattr_nodiscard static hxinline constexpr size_t bytes(void) { return s_words_ * sizeof(size_t); }
96
98 hxattr_nodiscard hxinline hxconstexpr const size_t* data(void) const { return m_data_; }
99
100 hxattr_nodiscard hxinline hxconstexpr size_t* data(void) { return m_data_; }
101
104
107 hxconstexpr hxbitset& flip(size_t pos_);
108
110 hxattr_nodiscard hxinline hxconstexpr bool none(void) const { return !this->any(); }
111
114
118
121
125 hxconstexpr hxbitset& set(size_t pos_, bool value_=true);
126
128 hxattr_nodiscard static hxinline constexpr size_t size(void) { return bit_count_; }
129
132 hxattr_nodiscard hxconstexpr bool test(size_t pos_) const;
133
134private:
135 static_assert(bit_count_ > 0, "hxbitset requires bit_count > 0");
136 static hxinline_constexpr size_t s_bits_per_word_ = sizeof(size_t) * 8u;
137 static hxinline_constexpr size_t s_log2_bits_per_word_ = (s_bits_per_word_ > 32u) ? 6u : 5u;
138 static hxinline_constexpr size_t s_bit_mask_ = s_bits_per_word_ - 1u;
139 static hxinline_constexpr size_t s_words_ = (bit_count_ + s_bit_mask_) >> s_log2_bits_per_word_;
140 static hxinline_constexpr size_t s_trailing_bits_ = bit_count_ & s_bit_mask_;
141 static hxinline_constexpr size_t s_trailing_mask_ = s_trailing_bits_ != 0u
142 ? (static_cast<size_t>(1u) << s_trailing_bits_) - 1u
143 : ~static_cast<size_t>(0u);
144
145 size_t m_data_[s_words_];
146};
147
148#include "detail/hxbitset.inl"
149HX_NS_END_
constexpr void operator=(const hxbitset &x_)
Assigns the bits of x to this hxbitset.
static constexpr size_t bytes(void)
Returns the size of the underlying storage in bytes.
Definition hxbitset.hpp:95
constexpr hxbitset & reset(size_t pos_)
Clears the bit at position pos.
constexpr hxbitset & operator^=(const hxbitset &x_)
Applies bitwise XOR with x in place.
constexpr size_t * data(void)
Definition hxbitset.hpp:100
constexpr hxbitset & operator<<=(size_t count_)
Shifts all bits left by count positions, filling vacated bits with 0.
constexpr hxbitset(size_t val_)
Constructs a hxbitset from a single size_t value.
constexpr bool none(void) const
Returns true if no bits are set.
Definition hxbitset.hpp:110
constexpr hxbitset & reset(void)
Clears all bits to 0.
constexpr bool test(size_t pos_) const
Returns the value of the bit at position pos.
constexpr bool all(void) const
Returns true if all bits are set.
constexpr hxbitset & set(void)
Sets all bits to 1.
static constexpr size_t size(void)
Returns the number of bits.
Definition hxbitset.hpp:128
constexpr hxbitset & set(size_t pos_, bool value_=true)
Sets or clears the bit at position pos.
constexpr hxbitset & flip(size_t pos_)
Flips the bit at position pos.
constexpr hxbitset(std::initializer_list< int_t_ > x_)
Constructs a hxbitset from a list of integers, packed low element first within each size_t word and l...
constexpr hxbitset(void)
Constructs a zero-initialized hxbitset.
Definition hxbitset.hpp:29
constexpr bool operator[](size_t pos_) const
Returns the value of the bit at position pos.
constexpr bool operator==(const hxbitset &x_) const
Returns true if all bits compare equal to those of x.
constexpr hxbitset & operator>>=(size_t count_)
Shifts all bits right by count positions, filling vacated bits with 0.
constexpr hxbitset & operator&=(const hxbitset &x_)
Applies bitwise AND with x in place.
constexpr hxbitset & operator|=(const hxbitset &x_)
Applies bitwise OR with x in place.
constexpr const size_t * data(void) const
Returns a const pointer to the underlying word storage.
Definition hxbitset.hpp:98
constexpr hxbitset & flip(void)
Flips all bits.
constexpr bool any(void) const
Returns true if at least one bit is set.
constexpr hxbitset(const hxbitset &x_)
Constructs a hxbitset as a copy of x.
Implements or includes std::initializer_list depending on HX_USE_LIBCXX.
#define hxinline
hxinline - Force a function to be inlined into its callers.
Definition hxsettings.h:125
#define hxinline_constexpr
hxinline_constexpr - Enables C++17 compatable "inline constexpr" usage for variables so they can be e...
Definition hxsettings.h:275
#define hxattr_nodiscard
hxattr_nodiscard - Indicates the caller should not discard the return value.
Definition hxsettings.h:98
#define hxconstexpr
hxconstexpr - Enables C++23 compatable constexpr usage for functions as that has support for destruct...
Definition hxsettings.h:266
Provides core macros, memory management and feature detection.