// Copyright 2018-2022 David Robillard // Copyright 2006-2007 Chris Hamilton // SPDX-License-Identifier: GPL-2.0-or-later #ifndef CHILBERT_STATICBITVEC_HPP #define CHILBERT_STATICBITVEC_HPP #include "chilbert/detail/BitVecIndex.hpp" #include "chilbert/detail/BitVecIterator.hpp" #include "chilbert/detail/BitVecMask.hpp" #include "chilbert/detail/MultiBitVec.hpp" #include "chilbert/detail/operations.hpp" #include #include #include #include #include #include #include namespace chilbert { /** A statically sized bit vector. * * This has a static number of bits encoded in the type, like std::bitset, and * does not use dynamic allocation or store a dynamic size. * * @tparam N Number of bits. */ template class StaticBitVec : public detail::MultiBitVec> { public: using Rack = typename detail::MultiBitVec>::Rack; using detail::MultiBitVec>::bits_per_rack; StaticBitVec() = default; /// Constructor for compatibility with DynamicBitVec explicit StaticBitVec(const size_t bits) { (void)bits; assert(bits == size()); } /// Constructor for compatibility with DynamicBitVec StaticBitVec(const size_t bits, const Rack value) : StaticBitVec{bits} { m_racks[0] = value; } /// Return the size in bits size_t size() const { return N; } /// Return a reference to the `index`th rack #ifndef NDEBUG const auto& rack(const size_t index) const { return m_racks.at(index); } auto& rack(const size_t index) { return m_racks.at(index); } #else const auto& rack(const size_t index) const { return m_racks[index]; } auto& rack(const size_t index) { return m_racks[index]; } #endif /// Return a raw pointer to the racks Rack* data() { return m_racks.data(); } const Rack* data() const { return m_racks.data(); } /// Return the total size of all racks in bytes static constexpr size_t data_size() { return num_racks() * sizeof(Rack); } /// Return the number of racks static constexpr size_t num_racks() { return (std::max(N, size_t{1}) + bits_per_rack - 1U) / bits_per_rack; } private: std::array m_racks{}; }; namespace detail { template struct is_bitvec> { constexpr static bool value = true; }; template void gray_code(StaticBitVec& value) { gray_code(static_cast>&>(value)); } template void gray_code_inv(StaticBitVec& value) { gray_code_inv(static_cast>&>(value)); } } // namespace detail } // namespace chilbert #endif