QtBase  v6.3.1
qbitarray.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2020 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #ifndef QBITARRAY_H
41 #define QBITARRAY_H
42 
43 #include <QtCore/qbytearray.h>
44 
46 
47 class QBitRef;
48 class Q_CORE_EXPORT QBitArray
49 {
50 #ifndef QT_NO_DATASTREAM
51  friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
52  friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
53 #endif
54  friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
55  QByteArray d;
56 
57 public:
58  inline QBitArray() noexcept {}
59  explicit QBitArray(qsizetype size, bool val = false);
60  QBitArray(const QBitArray &other) : d(other.d) {}
61  inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
62  inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
64 
65  void swap(QBitArray &other) noexcept { d.swap(other.d); }
66 
67  inline qsizetype size() const { return (d.size() << 3) - *d.constData(); }
68  inline qsizetype count() const { return (d.size() << 3) - *d.constData(); }
69  qsizetype count(bool on) const;
70 
71  inline bool isEmpty() const { return d.isEmpty(); }
72  inline bool isNull() const { return d.isNull(); }
73 
74  void resize(qsizetype size);
75 
76  inline void detach() { d.detach(); }
77  inline bool isDetached() const { return d.isDetached(); }
78  inline void clear() { d.clear(); }
79 
80  bool testBit(qsizetype i) const;
81  void setBit(qsizetype i);
82  void setBit(qsizetype i, bool val);
83  void clearBit(qsizetype i);
84  bool toggleBit(qsizetype i);
85 
86  bool at(qsizetype i) const;
87  QBitRef operator[](qsizetype i);
88  bool operator[](qsizetype i) const;
89 
90  QBitArray &operator&=(const QBitArray &);
91  QBitArray &operator|=(const QBitArray &);
92  QBitArray &operator^=(const QBitArray &);
93  QBitArray operator~() const;
94 
95  inline bool operator==(const QBitArray &other) const { return d == other.d; }
96  inline bool operator!=(const QBitArray &other) const { return d != other.d; }
97 
98  inline bool fill(bool val, qsizetype size = -1);
99  void fill(bool val, qsizetype first, qsizetype last);
100 
101  inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
102 
103  const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
104  static QBitArray fromBits(const char *data, qsizetype len);
105 
106  quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
107 
108 public:
109  typedef QByteArray::DataPointer DataPtr;
110  inline DataPtr &data_ptr() { return d.data_ptr(); }
111 };
112 
113 inline bool QBitArray::fill(bool aval, qsizetype asize)
114 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
115 
116 Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
117 Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
118 Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
119 
120 inline bool QBitArray::testBit(qsizetype i) const
121 { Q_ASSERT(size_t(i) < size_t(size()));
122  return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
123 
125 { Q_ASSERT(size_t(i) < size_t(size()));
126  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
127 
129 { Q_ASSERT(size_t(i) < size_t(size()));
130  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
131 
132 inline void QBitArray::setBit(qsizetype i, bool val)
133 { if (val) setBit(i); else clearBit(i); }
134 
136 { Q_ASSERT(size_t(i) < size_t(size()));
137  uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
138  uchar c = uchar(*p&b); *p^=b; return c!=0; }
139 
140 inline bool QBitArray::operator[](qsizetype i) const { return testBit(i); }
141 inline bool QBitArray::at(qsizetype i) const { return testBit(i); }
142 
143 class Q_CORE_EXPORT QBitRef
144 {
145 private:
146  QBitArray &a;
147  qsizetype i;
148  inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
149  friend class QBitArray;
150 
151 public:
152  inline operator bool() const { return a.testBit(i); }
153  inline bool operator!() const { return !a.testBit(i); }
154  QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
155  QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
156 };
157 
159 { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
160 
161 #ifndef QT_NO_DATASTREAM
162 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
164 #endif
165 
166 #ifndef QT_NO_DEBUG_STREAM
167 Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
168 #endif
169 
171 
173 
174 #endif // QBITARRAY_H
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
size_t qHash(const FixStringCacheKey &f)
Definition: cachekeys.h:68
FT_UInt idx
Definition: cffcmap.c:135
The QBitArray class provides an array of bits.
Definition: qbitarray.h:49
void truncate(qsizetype pos)
Definition: qbitarray.h:101
void clear()
Definition: qbitarray.h:78
bool testBit(qsizetype i) const
Definition: qbitarray.h:120
const char * bits() const
Definition: qbitarray.h:103
bool at(qsizetype i) const
Definition: qbitarray.h:141
QBitArray(QBitArray &&other) noexcept
Definition: qbitarray.h:62
bool operator==(const QBitArray &other) const
Definition: qbitarray.h:95
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
Definition: qbitarray.cpp:709
bool operator!=(const QBitArray &other) const
Definition: qbitarray.h:96
qsizetype count() const
Definition: qbitarray.h:68
QBitArray & operator=(const QBitArray &other)
Definition: qbitarray.h:61
bool isEmpty() const
Definition: qbitarray.h:71
void detach()
Definition: qbitarray.h:76
void setBit(qsizetype i)
Definition: qbitarray.h:124
QBitArray operator|(const QBitArray &a1, const QBitArray &a2)
Definition: qbitarray.cpp:686
void clearBit(qsizetype i)
Definition: qbitarray.h:128
bool isDetached() const
Definition: qbitarray.h:77
bool fill(bool val, qsizetype size=-1)
Definition: qbitarray.h:113
bool toggleBit(qsizetype i)
Definition: qbitarray.h:135
qsizetype size() const
Definition: qbitarray.h:67
bool isNull() const
Definition: qbitarray.h:72
QBitArray(const QBitArray &other)
Definition: qbitarray.h:60
QBitArray operator&(const QBitArray &a1, const QBitArray &a2)
Definition: qbitarray.cpp:663
QBitRef operator[](qsizetype i)
Definition: qbitarray.h:158
QBitArray() noexcept
Definition: qbitarray.h:58
The QBitRef class is an internal class, used with QBitArray.
Definition: qbitarray.h:144
QBitRef & operator=(const QBitRef &val)
Definition: qbitarray.h:154
bool operator!() const
Definition: qbitarray.h:153
QBitRef & operator=(bool val)
Definition: qbitarray.h:155
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:66
operator>>(QDataStream &ds, qfloat16 &f)
Definition: qfloat16.cpp:344
operator<<(QDataStream &ds, qfloat16 f)
Definition: qfloat16.cpp:327
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:65
a resize(100000)
auto it unsigned count const
Definition: hb-iter.hh:848
#define inline
Definition: md4c.c:45
void
Definition: png.h:1080
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QBitArray &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QBitArray &)
unsigned int quint32
Definition: qglobal.h:288
QT_BEGIN_INCLUDE_NAMESPACE typedef unsigned char uchar
Definition: qglobal.h:332
ptrdiff_t qsizetype
Definition: qglobal.h:308
#define QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(Class)
Definition: qglobal.h:563
GLboolean GLboolean GLboolean b
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
const GLubyte * c
Definition: qopenglext.h:12701
GLuint GLfloat * val
Definition: qopenglext.h:1513
GLenum array
Definition: qopenglext.h:7028
GLenum GLsizei len
Definition: qopenglext.h:3292
GLfloat GLfloat p
[1]
Definition: qopenglext.h:12698
const void * data_ptr(const QTransform &t)
Definition: qpainter_p.h:84
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
#define Q_DECLARE_SHARED(TYPE)
Definition: qtypeinfo.h:197
QUrl::FormattingOptions & operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
Definition: qurl.h:327
QObject::connect nullptr
ba setBit(0, true)
ba fill(true)
QBitArray().isNull()
[3]
Definition: qbitarray.h:149
QSharedPointer< T > other(t)
[5]
this swap(other)
QAction * at