/* 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_BIGBITVEC_HPP #define CHILBERT_BIGBITVEC_HPP #include "chilbert/BitVecIterator.hpp" #include "chilbert/BitVecMask.hpp" #include "chilbert/Operations.hpp" #include #include #include #include #include #include #include #include namespace chilbert { class CBigBitVec { public: using Rack = uintptr_t; using Mask = BitVecMask; using iterator = BitVecIterator; using const_iterator = ConstBitVecIterator; static constexpr size_t bits_per_rack = sizeof(Rack) * CHAR_BIT; explicit CBigBitVec(const size_t bits) : m_pcRacks{make_racks(num_racks(bits))} , m_size{bits} { } CBigBitVec(const size_t bits, const Rack value) : CBigBitVec{bits} { m_pcRacks[0] = value; } CBigBitVec(const CBigBitVec& vec) : m_pcRacks{make_racks(vec.rackCount())} , m_size{vec.m_size} { if (vec.m_pcRacks) { memcpy( m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(Rack) * rackCount()); } } CBigBitVec(CBigBitVec&& vec) = default; /// Return the size in bits size_t size() const { return m_size; } /// Set all bits to zero CBigBitVec& reset() { memset(m_pcRacks.get(), 0, sizeof(Rack) * rackCount()); return *this; } /// Set all bits to one CBigBitVec& set() { if (m_size) { memset(m_pcRacks.get(), 0xFF, sizeof(Rack) * rackCount()); const auto pad = m_size % bits_per_rack; if (pad) { m_pcRacks[rackCount() - 1] |= ~Rack{0} >> pad; } } return *this; } /// Truncate to a given precision in bits (zero MSBs) CBigBitVec& truncate(const size_t bits) { const Mask m = mask(bits); m_pcRacks[m.rack] &= (m.mask - 1); for (size_t i = m.rack + 1; i < rackCount(); ++i) { m_pcRacks[i] = 0; } return *this; } bool operator==(const CBigBitVec& vec) const { return (rackCount() == vec.rackCount() && (rackCount() == 0 || !memcmp(racks(), vec.racks(), rackCount() * sizeof(Rack)))); } bool operator!=(const CBigBitVec& vec) const { return !(*this == vec); } bool operator<(const CBigBitVec& vec) const { assert(size() == vec.size()); for (size_t ri = 0; ri < rackCount(); ++ri) { size_t i = rackCount() - ri - 1; if (m_pcRacks[i] < vec.m_pcRacks[i]) { return true; } else if (m_pcRacks[i] > vec.m_pcRacks[i]) { return false; } } return false; } CBigBitVec& operator=(const CBigBitVec& vec) { if (rackCount() < vec.rackCount()) { m_pcRacks = make_racks(vec.rackCount()); m_size = vec.m_size; memcpy( m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(Rack) * rackCount()); } else if (vec.rackCount() > 0) { m_size = vec.m_size; memcpy( m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(Rack) * rackCount()); } else { m_size = 0; m_pcRacks.reset(); } return *this; } CBigBitVec& operator=(CBigBitVec&& vec) = default; /// Return the value of the bit covered by `mask` bool test(const Mask mask) const { return m_pcRacks[mask.rack] & mask.mask; } /// Return the value of the `index`th bit bool test(const size_t index) const { return test(mask(index)); } /// Set the bit covered by `mask` to 1 CBigBitVec& set(const Mask mask) { m_pcRacks[mask.rack] |= mask.mask; return *this; } /// Set the `index`th bit to 1 CBigBitVec& set(const size_t index) { return set(mask(index)); } /// Reset the bit covered by `mask` to 0 CBigBitVec& reset(const Mask mask) { m_pcRacks[mask.rack] &= ~mask.mask; return *this; } /// Reset the `index`th bit to 0 CBigBitVec& reset(const size_t index) { return reset(mask(index)); } /// Set the bit covered by `mask` to `value` CBigBitVec& set(const Mask mask, const bool value) { auto& rack = m_pcRacks[mask.rack]; rack ^= (-Rack{value} ^ rack) & mask.mask; return *this; } /// Set the `index`th bit to `value` CBigBitVec& set(const size_t index, const bool value) { return set(mask(index), value); } /// Flip the value of the bit covered by `mask` CBigBitVec& flip(const Mask mask) { m_pcRacks[mask.rack] ^= mask.mask; return *this; } /// Flip the value of the `index`th bit CBigBitVec& flip(const size_t index) { return flip(mask(index)); } CBigBitVec& operator&=(const CBigBitVec& vec) { for (size_t i = 0; i < std::min(rackCount(), vec.rackCount()); ++i) { m_pcRacks[i] &= vec.m_pcRacks[i]; } return *this; } CBigBitVec operator&(const CBigBitVec& vec) const { CBigBitVec t(*this); t &= vec; return t; } CBigBitVec& operator|=(const CBigBitVec& vec) { for (size_t i = 0; i < std::min(rackCount(), vec.rackCount()); ++i) { m_pcRacks[i] |= vec.m_pcRacks[i]; } return *this; } CBigBitVec operator|(const CBigBitVec& vec) const { CBigBitVec t(*this); t |= vec; return t; } CBigBitVec& operator^=(const CBigBitVec& vec) { for (size_t i = 0; i < std::min(rackCount(), vec.rackCount()); ++i) { m_pcRacks[i] ^= vec.m_pcRacks[i]; } return *this; } CBigBitVec operator^(const CBigBitVec& vec) const { CBigBitVec t(*this); t ^= vec; return t; } CBigBitVec operator~() const { CBigBitVec t(*this); t.flip(); return t; } CBigBitVec& operator<<=(const size_t bits) { if (bits == 0) { return *this; } else if (bits >= size()) { reset(); return *this; } const Index index{bits}; if (index.rack > 0) { // Shift entire racks for (size_t i = rackCount() - 1; i >= index.rack; --i) { m_pcRacks[i] = m_pcRacks[i - index.rack]; } for (size_t i = 0; i < index.rack; ++i) { m_pcRacks[i] = 0; } } if (index.bit > 0) { // Shift bits within racks size_t bi = bits_per_rack - index.bit; size_t i; for (i = rackCount() - 1; i >= index.rack + 1; --i) { m_pcRacks[i] <<= index.bit; m_pcRacks[i] |= m_pcRacks[i - 1] >> bi; } m_pcRacks[i] <<= index.bit; } return *this; } CBigBitVec operator<<(const size_t bits) const { CBigBitVec t(*this); t <<= bits; return t; } CBigBitVec& operator>>=(const size_t bits) { if (bits == 0) { return *this; } else if (bits >= size()) { reset(); return *this; } const Index index{bits}; if (index.rack > 0) { // Shift entire racks size_t i; for (i = 0; i < rackCount() - index.rack; ++i) { m_pcRacks[i] = m_pcRacks[i + index.rack]; } for (; i < rackCount(); ++i) { m_pcRacks[i] = 0; } } if (index.bit > 0) { // Shift bits within racks size_t bi = bits_per_rack - index.bit; size_t i; for (i = 0; i < rackCount() - index.rack - 1; ++i) { m_pcRacks[i] >>= index.bit; m_pcRacks[i] |= m_pcRacks[i + 1] << bi; } m_pcRacks[i] >>= index.bit; } return *this; } CBigBitVec operator>>(const size_t bits) const { CBigBitVec t(*this); t >>= bits; return t; } /// Right-rotate by `bits` positions CBigBitVec& rotr(const size_t bits) { assert(bits <= size()); if (bits == 0 || bits == size()) { return *this; } CBigBitVec t1(*this); (*this) >>= bits; t1 <<= (size() - bits); (*this) |= t1; truncate(size()); return *this; } /// Left-rotate by `bits` positions CBigBitVec& rotl(const size_t bits) { assert(bits <= size()); if (bits == 0 || bits == size()) { return *this; } CBigBitVec t1(*this); (*this) <<= bits; t1 >>= (size() - bits); (*this) |= t1; truncate(size()); return *this; } /// Return true iff all bits are zero bool none() const { for (size_t i = 0; i < rackCount(); ++i) { if (m_pcRacks[i]) { return false; } } return true; } /// Return 1 + the index of the first set bit, or 0 if there are none size_t find_first() const { for (size_t i = 0; i < rackCount(); ++i) { const int j = ffs(m_pcRacks[i]); if (j) { return (i * bits_per_rack) + static_cast(j); } } return 0; } /// Return the number of set bits size_t count() const { size_t c = 0; for (size_t i = 0; i < rackCount(); ++i) { c += static_cast(pop_count(m_pcRacks[i])); } return c; } /// Flip all bits (one's complement) CBigBitVec& flip() { for (size_t i = 0; i < rackCount(); ++i) { m_pcRacks[i] = ~m_pcRacks[i]; } return *this; } /// Return the first rack Rack& rack() { return m_pcRacks[0]; } Rack rack() const { return m_pcRacks[0]; } /// Return a pointer to the racks Rack* racks() { return m_pcRacks.get(); } const Rack* racks() const { return m_pcRacks.get(); } /// Return the number of racks size_t rackCount() const { return num_racks(m_size); } Mask mask(const size_t i = 0) const { assert(i <= size()); return Mask{i}; } iterator begin(const size_t i = 0) { return iterator(*this, i); } iterator end() { return iterator(*this, size()); } const_iterator begin(const size_t i = 0) const { return const_iterator(*this, i); } const_iterator end() const { return const_iterator(*this, size()); } private: struct Index { Index(const size_t bits) : rack{bits / bits_per_rack} , bit{bits - rack * bits_per_rack} { assert(bit < bits_per_rack); } size_t rack; size_t bit; }; struct RacksDeleter { void operator()(Rack* const racks) { free(racks); } }; using RacksPtr = std::unique_ptr; static size_t 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(calloc(n, sizeof(Rack)))}; } RacksPtr m_pcRacks; size_t m_size; }; template <> void grayCode(CBigBitVec& value) { CBigBitVec::Rack s = 0; for (intptr_t i = static_cast(value.rackCount() - 1); i >= 0; --i) { const auto t = value.racks()[i] & 1; grayCode(value.racks()[i]); value.racks()[i] ^= (s << (CBigBitVec::bits_per_rack - 1)); s = t; } } template <> void grayCodeInv(CBigBitVec& value) { CBigBitVec::Rack s = 0; for (intptr_t i = static_cast(value.rackCount() - 1); i >= 0; --i) { auto& rack = value.racks()[i]; grayCodeInv(rack); if (s) { rack = ~rack; } s = rack & 1; } } inline std::ostream& operator<<(std::ostream& os, const CBigBitVec& vec) { for (size_t i = 0; i < vec.size(); ++i) { os << vec.test(vec.size() - i - 1); } return os; } } // namespace chilbert #endif