aboutsummaryrefslogtreecommitdiffstats
path: root/include/chilbert/BoundedBitVec.hpp
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-09-16 15:39:48 -0400
committerDavid Robillard <d@drobilla.net>2022-09-16 22:31:06 -0400
commit49dab5622b31421eb6af84eae376d73fae1cd4a0 (patch)
tree86290707551320ab35952bccc11c66df05714b26 /include/chilbert/BoundedBitVec.hpp
parent342a22b6d75597ee22c195b60607402e3ed028b2 (diff)
downloadchilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.tar.gz
chilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.tar.bz2
chilbert-49dab5622b31421eb6af84eae376d73fae1cd4a0.zip
Switch to meson build system
Diffstat (limited to 'include/chilbert/BoundedBitVec.hpp')
-rw-r--r--include/chilbert/BoundedBitVec.hpp125
1 files changed, 125 insertions, 0 deletions
diff --git a/include/chilbert/BoundedBitVec.hpp b/include/chilbert/BoundedBitVec.hpp
new file mode 100644
index 0000000..fa414ca
--- /dev/null
+++ b/include/chilbert/BoundedBitVec.hpp
@@ -0,0 +1,125 @@
+/*
+ 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_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 <algorithm>
+#include <array>
+#include <cassert>
+#include <cstddef>
+#include <cstdlib>
+#include <cstring>
+
+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 <size_t MaxN>
+class BoundedBitVec : public detail::MultiBitVec<BoundedBitVec<MaxN>>
+{
+public:
+ using Rack = typename detail::MultiBitVec<BoundedBitVec<MaxN>>::Rack;
+
+ using detail::MultiBitVec<BoundedBitVec<MaxN>>::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 - 1) / bits_per_rack;
+ }
+
+ std::array<Rack, calculate_num_racks(MaxN)> m_racks{};
+ size_t m_size;
+};
+
+namespace detail {
+
+template <size_t MaxN>
+struct is_bitvec<BoundedBitVec<MaxN>>
+{
+ constexpr static bool value = true;
+};
+
+template <size_t MaxN>
+void
+gray_code(BoundedBitVec<MaxN>& value)
+{
+ gray_code(static_cast<detail::MultiBitVec<BoundedBitVec<MaxN>>&>(value));
+}
+
+template <size_t MaxN>
+void
+gray_code_inv(BoundedBitVec<MaxN>& value)
+{
+ gray_code_inv(
+ static_cast<detail::MultiBitVec<BoundedBitVec<MaxN>>&>(value));
+}
+
+} // namespace detail
+
+} // namespace chilbert
+
+#endif