/* 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_BOUNDEDBITVEC_HPP #define CHILBERT_BOUNDEDBITVEC_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 namespace chilbert { /** A statically allocated bit vector with a dynamic size. * * This can be used to have bit vectors of an arbitrary dynamic size, under * some static bound, without using dynamic allocation. * * @tparam MaxN Maximum number of bits. */ template class BoundedBitVec : public detail::MultiBitVec> { public: using Rack = typename detail::MultiBitVec>::Rack; using detail::MultiBitVec>::bits_per_rack; BoundedBitVec() = default; explicit BoundedBitVec(const size_t bits) : m_size{bits} { assert(bits <= MaxN); } BoundedBitVec(const size_t bits, const Rack value) : BoundedBitVec{bits} { m_racks[0] = value; } /// Return the size in bits size_t size() const { return m_size; } /// 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 size_t data_size() const { return num_racks() * sizeof(Rack); } /// Return the number of racks size_t num_racks() const { return calculate_num_racks(m_size); } private: static constexpr size_t calculate_num_racks(const size_t bits) { return (std::max(bits, size_t{1}) + bits_per_rack - 1U) / bits_per_rack; } std::array m_racks{}; size_t m_size{}; }; namespace detail { template struct is_bitvec> { constexpr static bool value = true; }; template void gray_code(BoundedBitVec& value) { gray_code(static_cast>&>(value)); } template void gray_code_inv(BoundedBitVec& value) { gray_code_inv(static_cast>&>(value)); } } // namespace detail } // namespace chilbert #endif