File indexing completed on 2024-05-12 04:01:31

0001 /*
0002     SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: MIT
0005 */
0006 
0007 #ifndef PRISON_BITVECTOR_P_H
0008 #define PRISON_BITVECTOR_P_H
0009 
0010 #include <QByteArray>
0011 #include <QDebug>
0012 
0013 namespace Prison
0014 {
0015 class BitVector;
0016 }
0017 QDebug operator<<(QDebug dbg, const Prison::BitVector &v);
0018 
0019 namespace Prison
0020 {
0021 /** Vector for working with a set of bits without byte alignment. */
0022 class BitVector
0023 {
0024 public:
0025     BitVector();
0026     ~BitVector();
0027 
0028     class iterator
0029     {
0030     public:
0031         inline bool operator!=(const iterator &other)
0032         {
0033             return m_index != other.m_index;
0034         }
0035         inline bool operator*() const
0036         {
0037             return m_vector->at(m_index);
0038         }
0039         inline iterator operator++()
0040         {
0041             ++m_index;
0042             return *this;
0043         }
0044 
0045     private:
0046         friend class BitVector;
0047         const BitVector *m_vector;
0048         int m_index;
0049     };
0050 
0051     /** Append the lowest @p bits of @p data with the least significant bit first. */
0052     void appendLSB(int data, int bits);
0053     /** Append the lowest @p bits of @p data with the most significant bit first. */
0054     void appendMSB(int data, int bits);
0055     void appendBit(bool bit);
0056     void append(const BitVector &other);
0057     /** Returns the bit at index @p index. */
0058     bool at(int index) const;
0059     void clear();
0060     void reserve(int size);
0061     int size() const;
0062     /** Returns the value starting at @p index of size @p size. */
0063     int valueAtMSB(int index, int size) const;
0064     iterator begin() const;
0065     iterator end() const;
0066 
0067     bool operator==(const BitVector &other) const;
0068     bool operator!=(const BitVector &other) const;
0069 
0070 private:
0071     friend QDebug(::operator<<)(QDebug dbg, const Prison::BitVector &v);
0072     QByteArray m_data;
0073     int m_size = 0;
0074 };
0075 
0076 }
0077 
0078 #endif // PRISON_BITVECTOR_P_H