/* Copyright (C) 2018 David Robillard Copyright (C) 2006-2007 Chris Hamilton This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ #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) { 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 - 1) / 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