/* 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/FixBitVec.hpp" #include #include #include #include #include #include #include #define FBVS_NEEDED(b) ((std::max(b, size_t(1)) + FBV_BITS - 1) / FBV_BITS) namespace chilbert { class CBigBitVec { public: CBigBitVec(const size_t bits = 0) : m_pcRacks{make_racks(FBVS_NEEDED(bits))} , m_iRacks{bits == 0 ? 0 : FBVS_NEEDED(bits)} { } CBigBitVec(const CBigBitVec& vec) : m_pcRacks{make_racks(vec.m_iRacks)} , m_iRacks{vec.m_iRacks} { if (vec.m_pcRacks) { memcpy(m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(FBV_UINT) * m_iRacks); } } CBigBitVec(CBigBitVec&& vec) = default; CBigBitVec(const CFixBitVec& vec) : m_pcRacks{make_racks(1)} , m_iRacks{1} { m_pcRacks[0] = vec.rack(); } /// Return the size in bits size_t size() const { return m_iRacks * FBV_BITS; } /// Set all bits to zero CBigBitVec& reset() { memset(m_pcRacks.get(), 0, sizeof(CFixBitVec) * m_iRacks); return *this; } /// Set all bits to one CBigBitVec& set() { memset(m_pcRacks.get(), 0xFF, sizeof(CFixBitVec) * m_iRacks); return *this; } /// Truncate to a given precision in bits (zero MSBs) CBigBitVec& truncate(const size_t bits) { assert(bits <= size()); const Ref ref(bits); if (ref.rack >= m_iRacks) { return *this; } // Truncate rack that contains the split point m_pcRacks[ref.rack] &= FBVN1S(ref.bit); for (size_t i = ref.rack + 1; i < m_iRacks; ++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(FBV_UINT)))); } 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 < m_iRacks; ++ri) { size_t i = m_iRacks - 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; } /// Non-resizing assignment CBigBitVec& operator=(const CBigBitVec& vec) { if (m_iRacks < vec.m_iRacks) { m_iRacks = vec.m_iRacks; m_pcRacks = make_racks(m_iRacks); memcpy(m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(CFixBitVec) * m_iRacks); } else if (vec.m_iRacks > 0) { m_iRacks = vec.m_iRacks; memcpy(m_pcRacks.get(), vec.m_pcRacks.get(), sizeof(CFixBitVec) * m_iRacks); } else { m_iRacks = 0; m_pcRacks.reset(); } return *this; } CBigBitVec& operator=(CBigBitVec&& vec) = default; CBigBitVec& operator=(const CFixBitVec& vec) { if (!m_pcRacks) { m_pcRacks = make_racks(1); } m_iRacks = 1; m_pcRacks[0] = vec.rack(); return *this; } CBigBitVec& operator=(const FBV_UINT j) { if (!m_pcRacks) { m_pcRacks = make_racks(1); } m_iRacks = 1; m_pcRacks[0] = j; return *this; } /// Return the value of the `index`th bit bool test(const size_t index) const { assert(index < size()); const Ref ref(index); return testBit(m_pcRacks[ref.rack], ref.bit); } /// Set the `index`th bit to 1 CBigBitVec& set(const size_t index) { assert(index < size()); const Ref ref(index); setBit(m_pcRacks[ref.rack], ref.bit); return *this; } /// Reset the `index`th bit to 0 CBigBitVec& reset(const size_t index) { assert(index < size()); const Ref ref(index); m_pcRacks[ref.rack] &= ~(FBV_UINT{1} << ref.bit); return *this; } /// Set the `index`th bit to `value` CBigBitVec& set(const size_t index, const bool value) { assert(index < size()); const Ref ref(index); setBit(m_pcRacks[ref.rack], ref.bit, value); return *this; } /// Flip the value of the `index`th bit CBigBitVec& flip(const size_t index) { assert(index < size()); const Ref ref(index); m_pcRacks[ref.rack] ^= (FBV1 << ref.bit); return *this; } CBigBitVec& operator&=(const CBigBitVec& vec) { for (size_t i = 0; i < std::min(m_iRacks, vec.m_iRacks); ++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(m_iRacks, vec.m_iRacks); ++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(m_iRacks, vec.m_iRacks); ++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) { assert(bits < size()); // No shift? if (bits == 0) { return *this; } const Ref ref(bits); // All racks? if (ref.rack >= m_iRacks) { reset(); return *this; } // Do rack shifts. if (ref.rack > 0) { for (size_t i = m_iRacks - 1; i >= ref.rack; --i) { m_pcRacks[i] = m_pcRacks[i - ref.rack]; } for (size_t i = 0; i < ref.rack; ++i) { m_pcRacks[i] = 0; } } // Do bit shifts. if (ref.bit > 0) { size_t bi = FBV_BITS - ref.bit; size_t i; for (i = m_iRacks - 1; i >= ref.rack + 1; --i) { m_pcRacks[i] <<= ref.bit; m_pcRacks[i] |= m_pcRacks[i - 1] >> bi; } m_pcRacks[i] <<= ref.bit; } return *this; } CBigBitVec operator<<(const size_t bits) const { CBigBitVec t(*this); t <<= bits; return t; } CBigBitVec& operator>>=(const size_t bits) { assert(bits < size()); // No shift? if (bits == 0) { return *this; } const Ref ref(bits); // All racks? if (ref.rack >= m_iRacks) { reset(); return *this; } // Do rack shifts. if (ref.rack > 0) { size_t i; for (i = 0; i < m_iRacks - ref.rack; ++i) { m_pcRacks[i] = m_pcRacks[i + ref.rack]; } for (; i < m_iRacks; ++i) { m_pcRacks[i] = 0; } } // Do bit shifts. if (ref.bit > 0) { size_t bi = FBV_BITS - ref.bit; size_t i; for (i = 0; i < m_iRacks - ref.rack - 1; ++i) { m_pcRacks[i] >>= ref.bit; m_pcRacks[i] |= m_pcRacks[i + 1] << bi; } m_pcRacks[i] >>= ref.bit; } return *this; } CBigBitVec operator>>(const size_t bits) const { CBigBitVec t(*this); t >>= bits; return t; } /// Right-rotate the least significant `width` bits by `bits` positions CBigBitVec& rotr(const size_t bits, size_t width) { // Fill in the width, if necessary. if (width <= 0) { width = size(); } // Modulo the number of bits. assert(bits < width); if (bits == 0) { return *this; } // Ensure we are truncated appropriately. truncate(width); CBigBitVec t1(*this); (*this) >>= bits; t1 <<= (width - bits); (*this) |= t1; truncate(width); return *this; } /// Left-rotate the least significant `width` bits by `bits` positions CBigBitVec& rotl(const size_t bits, size_t width) { // Fill in the width, if necessary. if (width <= 0) { width = size(); } // Modulo the number of bits. assert(bits < width); if (bits == 0) { return *this; } // Ensure we are truncated appropriately. truncate(width); CBigBitVec t1(*this); (*this) <<= bits; t1 >>= (width - bits); (*this) |= t1; truncate(width); return *this; } /// Return true iff all bits are zero bool none() const { for (size_t i = 0; i < m_iRacks; ++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 < m_iRacks; ++i) { const int j = ffs(m_pcRacks[i]); if (j) { return (i * FBV_BITS) + 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 < m_iRacks; ++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 < m_iRacks; ++i) { m_pcRacks[i] = ~m_pcRacks[i]; } return *this; } /// Return the first rack FBV_UINT& rack() { return m_pcRacks[0]; } FBV_UINT rack() const { return m_pcRacks[0]; } /// Return a pointer to the racks FBV_UINT* racks() { return m_pcRacks.get(); } const FBV_UINT* racks() const { return m_pcRacks.get(); } /// Return the number of racks size_t rackCount() const { return m_iRacks; } private: struct Ref { Ref(const size_t bits) : rack{bits / FBV_BITS} , bit{bits - rack * FBV_BITS} { } size_t rack; size_t bit; }; struct RacksDeleter { void operator()(FBV_UINT* const racks) { free(racks); } }; using RacksPtr = std::unique_ptr; static RacksPtr make_racks(const size_t n) { return RacksPtr{static_cast(calloc(n, sizeof(FBV_UINT)))}; } // Right rotates entire racks (in place). void rackRotr(const size_t k) { assert(k < m_iRacks); if (k == 0) { return; } size_t c = 0; for (size_t v = 0; c < m_iRacks; ++v) { size_t t = v; size_t tp = v + k; const FBV_UINT tmp = m_pcRacks[v]; c++; while (tp != v) { m_pcRacks[t] = m_pcRacks[tp]; t = tp; tp += k; if (tp >= m_iRacks) { tp -= m_iRacks; } c++; } m_pcRacks[t] = tmp; } } RacksPtr m_pcRacks; size_t m_iRacks; }; template <> void grayCode(CBigBitVec& value) { FBV_UINT s = 0; for (intptr_t i = static_cast(value.rackCount() - 1); i >= 0; --i) { const FBV_UINT t = value.racks()[i] & 1; grayCode(value.racks()[i]); value.racks()[i] ^= (s << (FBV_BITS - 1)); s = t; } } template <> void grayCodeInv(CBigBitVec& value) { FBV_UINT s = 0; for (intptr_t i = static_cast(value.rackCount() - 1); i >= 0; --i) { FBV_UINT& 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