diff options
Diffstat (limited to 'chilbert/DynamicBitVec.hpp')
-rw-r--r-- | chilbert/DynamicBitVec.hpp | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/chilbert/DynamicBitVec.hpp b/chilbert/DynamicBitVec.hpp deleted file mode 100644 index 722b689..0000000 --- a/chilbert/DynamicBitVec.hpp +++ /dev/null @@ -1,157 +0,0 @@ -/* - Copyright (C) 2018 David Robillard <d@drobilla.net> - Copyright (C) 2006-2007 Chris Hamilton <chamilton@cs.dal.ca> - - 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 <https://www.gnu.org/licenses/>. -*/ - -#ifndef CHILBERT_DYNAMICBITVEC_HPP -#define CHILBERT_DYNAMICBITVEC_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 <algorithm> -#include <cstddef> -#include <cstdlib> -#include <cstring> -#include <memory> - -namespace chilbert { - -/** A dynamically allocated bit vector. - * - * This uses dynamic allocation internally and can be constructed with any - * size, assuming sufficient memory is available. - */ -class DynamicBitVec : public detail::MultiBitVec<DynamicBitVec> -{ -public: - struct RacksDeleter - { - void operator()(Rack* const racks) { free(racks); } - }; - - struct NullDeleter - { - void operator()(const Rack* const) {} - }; - - using RacksPtr = std::unique_ptr<Rack[], RacksDeleter>; - using ConstRacksPtr = std::unique_ptr<const Rack[], NullDeleter>; - - explicit DynamicBitVec(const size_t bits) - : m_racks{make_racks(calculate_num_racks(bits))} - , m_size{bits} - { - } - - DynamicBitVec(const size_t bits, const Rack value) - : DynamicBitVec{bits} - { - m_racks[0] = value; - } - - DynamicBitVec(const DynamicBitVec& vec) - : m_racks{make_racks(vec.num_racks())} - , m_size{vec.m_size} - { - if (vec.data()) { - memcpy(data(), vec.data(), data_size()); - } - } - - DynamicBitVec(DynamicBitVec&& vec) = default; - - DynamicBitVec& operator=(const DynamicBitVec& vec) - { - if (num_racks() < vec.num_racks()) { - m_racks = make_racks(vec.num_racks()); - m_size = vec.m_size; - memcpy(data(), vec.data(), data_size()); - } else if (vec.num_racks() > 0) { - m_size = vec.m_size; - memcpy(data(), vec.data(), data_size()); - } else { - m_size = 0; - m_racks.reset(); - } - - return *this; - } - - DynamicBitVec& operator=(DynamicBitVec&& vec) = default; - - /// Return the size in bits - size_t size() const { return m_size; } - - /// Return a reference to the `index`th rack - const Rack& rack(const size_t index) const { return m_racks[index]; } - Rack& rack(const size_t index) { return m_racks[index]; } - - /// Return a raw pointer to the racks - Rack* data() { return m_racks.get(); } - const Rack* data() const { return m_racks.get(); } - - /// 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 size_t calculate_num_racks(const size_t bits) - { - return (std::max(bits, size_t(1)) + bits_per_rack - 1) / bits_per_rack; - } - - static RacksPtr make_racks(const size_t n) - { - return RacksPtr{static_cast<Rack*>(calloc(n, sizeof(Rack)))}; - } - - RacksPtr m_racks; - size_t m_size; -}; - -namespace detail { - -template <> -struct is_bitvec<DynamicBitVec> -{ - constexpr static bool value = true; -}; - -template <> -void -gray_code(DynamicBitVec& value) -{ - gray_code(static_cast<MultiBitVec<DynamicBitVec>&>(value)); -} - -template <> -void -gray_code_inv(DynamicBitVec& value) -{ - gray_code_inv(static_cast<MultiBitVec<DynamicBitVec>&>(value)); -} - -} // namespace detail - -} // namespace chilbert - -#endif |