Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qbitarray.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QBITARRAY_H
5#define QBITARRAY_H
6
7#include <QtCore/qbytearray.h>
8
10
11class QBitRef;
12class Q_CORE_EXPORT QBitArray
13{
14 Q_CORE_EXPORT friend QBitArray operator&(const QBitArray &a1, const QBitArray &a2);
16 { return a1 &= a2; }
18 { return a2 &= a1; }
20 { return a1 &= a2; }
21
22 Q_CORE_EXPORT friend QBitArray operator|(const QBitArray &a1, const QBitArray &a2);
24 { return a1 |= a2; }
26 { return a2 |= a1; }
28 { return a1 |= a2; }
29
30 Q_CORE_EXPORT friend QBitArray operator^(const QBitArray &a1, const QBitArray &a2);
32 { return a1 ^= a2; }
34 { return a2 ^= a1; }
36 { return a1 ^= a2; }
37
38#ifndef QT_NO_DATASTREAM
39 friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
40 friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
41#endif
42 friend Q_CORE_EXPORT size_t qHash(const QBitArray &key, size_t seed) noexcept;
44 { return std::move(a).inverted_inplace(); }
46
47 QBitArray(QByteArrayData &&dd) : d(std::move(dd)) {}
48
49 template <typename BitArray> static auto bitLocation(BitArray &ba, qsizetype i)
50 {
51 Q_ASSERT(size_t(i) < size_t(ba.size()));
52 struct R {
53 decltype(ba.d[1]) byte;
54 uchar bitMask;
55 };
56 qsizetype byteIdx = i >> 3;
57 qsizetype bitIdx = i & 7;
58 return R{ ba.d[1 + byteIdx], uchar(1U << bitIdx) };
59 }
60
61 QBitArray inverted_inplace() &&;
62
63public:
64 inline QBitArray() noexcept {}
65 explicit QBitArray(qsizetype size, bool val = false);
66 // Rule Of Zero applies
67#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0)
68 QBitArray(const QBitArray &other) noexcept : d(other.d) {}
69 inline QBitArray &operator=(const QBitArray &other) noexcept { d = other.d; return *this; }
70 inline QBitArray(QBitArray &&other) noexcept : d(std::move(other.d)) {}
71 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QBitArray)
72#endif // Qt 6
73
74 void swap(QBitArray &other) noexcept { d.swap(other.d); }
75
76 qsizetype size() const { return qsizetype((size_t(d.size()) << 3) - *d.constData()); }
77 qsizetype count() const { return size(); }
78 qsizetype count(bool on) const;
79
80 inline bool isEmpty() const { return d.isEmpty(); }
81 inline bool isNull() const { return d.isNull(); }
82
83 void resize(qsizetype size);
84
85 inline void detach() { d.detach(); }
86 inline bool isDetached() const { return d.isDetached(); }
87 inline void clear() { d.clear(); }
88
89 bool testBit(qsizetype i) const
90 { auto r = bitLocation(*this, i); return r.byte & r.bitMask; }
92 { auto r = bitLocation(*this, i); r.byte |= r.bitMask; }
93 void setBit(qsizetype i, bool val)
94 { if (val) setBit(i); else clearBit(i); }
96 { auto r = bitLocation(*this, i); r.byte &= ~r.bitMask; }
98 {
99 auto r = bitLocation(*this, i);
100 bool cl = r.byte & r.bitMask;
101 r.byte ^= r.bitMask;
102 return cl;
103 }
104
105 bool at(qsizetype i) const { return testBit(i); }
106 inline QBitRef operator[](qsizetype i);
107 bool operator[](qsizetype i) const { return testBit(i); }
108
109 QBitArray &operator&=(QBitArray &&);
111 QBitArray &operator^=(QBitArray &&);
112 QBitArray &operator&=(const QBitArray &);
114 QBitArray &operator^=(const QBitArray &);
115#if QT_CORE_REMOVED_SINCE(6, 7)
116 QBitArray operator~() const;
117#endif
118
119#if QT_CORE_REMOVED_SINCE(6, 8)
120 inline bool operator==(const QBitArray &other) const { return comparesEqual(d, other.d); }
121 inline bool operator!=(const QBitArray &other) const { return !operator==(other); }
122#endif
123
124 bool fill(bool aval, qsizetype asize = -1)
125 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
126 void fill(bool val, qsizetype first, qsizetype last);
127
128 inline void truncate(qsizetype pos) { if (pos < size()) resize(pos); }
129
130 const char *bits() const { return isEmpty() ? nullptr : d.constData() + 1; }
131 static QBitArray fromBits(const char *data, qsizetype len);
132
133 quint32 toUInt32(QSysInfo::Endian endianness, bool *ok = nullptr) const noexcept;
134
135public:
136 typedef QByteArray::DataPointer DataPtr;
137 inline DataPtr &data_ptr() { return d.data_ptr(); }
138 inline const DataPtr &data_ptr() const { return d.data_ptr(); }
139
140private:
141 friend bool comparesEqual(const QBitArray &lhs, const QBitArray &rhs) noexcept
142 {
143 return lhs.d == rhs.d;
144 }
146};
147
148class QT6_ONLY(Q_CORE_EXPORT) QBitRef
149{
150private:
151 QBitArray &a;
152 qsizetype i;
153 inline QBitRef(QBitArray &array, qsizetype idx) : a(array), i(idx) { }
154 friend class QBitArray;
155
156public:
157 inline operator bool() const { return a.testBit(i); }
158 inline bool operator!() const { return !a.testBit(i); }
159 QBitRef &operator=(const QBitRef &val) { a.setBit(i, val); return *this; }
160 QBitRef &operator=(bool val) { a.setBit(i, val); return *this; }
161};
162
165
166#ifndef QT_NO_DATASTREAM
167Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
169#endif
170
171#ifndef QT_NO_DEBUG_STREAM
172Q_CORE_EXPORT QDebug operator<<(QDebug, const QBitArray &);
173#endif
174
175Q_DECLARE_SHARED(QBitArray)
176
178
179#endif // QBITARRAY_H
\inmodule QtCore
Definition qbitarray.h:13
void truncate(qsizetype pos)
Truncates the bit array at index position pos.
Definition qbitarray.h:128
void setBit(qsizetype i, bool val)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbitarray.h:93
void clear()
Clears the contents of the bit array and makes it empty.
Definition qbitarray.h:87
friend QBitArray operator|(QBitArray &&a1, const QBitArray &a2)
Definition qbitarray.h:23
friend QBitArray operator|(QBitArray &&a1, QBitArray &&a2)
Returns a bit array that is the OR of the bit arrays a1 and a2.
Definition qbitarray.h:27
bool testBit(qsizetype i) const
Returns true if the bit at index position i is 1; otherwise returns false.
Definition qbitarray.h:89
const char * bits() const
Definition qbitarray.h:130
friend QBitArray operator&(QBitArray &&a1, const QBitArray &a2)
Definition qbitarray.h:15
bool at(qsizetype i) const
Returns the value of the bit at index position i.
Definition qbitarray.h:105
friend QBitArray operator^(QBitArray &&a1, QBitArray &&a2)
Returns a bit array that is the XOR of the bit arrays a1 and a2.
Definition qbitarray.h:35
bool fill(bool aval, qsizetype asize=-1)
Sets every bit in the bit array to value, returning true if successful; otherwise returns false.
Definition qbitarray.h:124
friend QBitArray operator|(const QBitArray &a1, QBitArray &&a2)
Definition qbitarray.h:25
friend QBitArray operator^(QBitArray &&a1, const QBitArray &a2)
Definition qbitarray.h:31
qsizetype count() const
Same as size().
Definition qbitarray.h:77
void setBit(qsizetype i)
Sets the bit at index position i to 1.
Definition qbitarray.h:91
bool isEmpty() const
Returns true if this bit array has size 0; otherwise returns false.
Definition qbitarray.h:80
const DataPtr & data_ptr() const
Definition qbitarray.h:138
friend QBitArray operator^(const QBitArray &a1, QBitArray &&a2)
Definition qbitarray.h:33
void detach()
Definition qbitarray.h:85
friend QBitArray operator&(QBitArray &&a1, QBitArray &&a2)
Returns a bit array that is the AND of the bit arrays a1 and a2.
Definition qbitarray.h:19
void clearBit(qsizetype i)
Sets the bit at index position i to 0.
Definition qbitarray.h:95
friend QBitArray operator&(const QBitArray &a1, QBitArray &&a2)
Definition qbitarray.h:17
bool isDetached() const
Definition qbitarray.h:86
bool toggleBit(qsizetype i)
Inverts the value of the bit at index position i, returning the previous value of that bit as either ...
Definition qbitarray.h:97
qsizetype size() const
Returns the number of bits stored in the bit array.
Definition qbitarray.h:76
bool isNull() const
Returns true if this bit array is null; otherwise returns false.
Definition qbitarray.h:81
friend QBitArray operator~(QBitArray a)
Returns a bit array that contains the inverted bits of the bit array a.
Definition qbitarray.h:43
bool operator[](qsizetype i) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qbitarray.h:107
void swap(QBitArray &other) noexcept
Definition qbitarray.h:74
friend bool comparesEqual(const QBitArray &lhs, const QBitArray &rhs) noexcept
Definition qbitarray.h:141
QBitRef operator[](qsizetype i)
Returns the bit at index position i as a modifiable reference.
Definition qbitarray.h:163
QBitArray() noexcept
Constructs an empty bit array.
Definition qbitarray.h:64
\inmodule QtCore \reentrant
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size() const noexcept
Returns the number of bytes in this byte array.
Definition qbytearray.h:494
\inmodule QtCore\reentrant
Definition qdatastream.h:46
\inmodule QtCore
Endian
\value BigEndian Big-endian byte order (also called Network byte order) \value LittleEndian Little-en...
Definition qsysinfo.h:28
a resize(100000)
Combined button and popup list for selecting options.
QBitArray operator^(const QBitArray &a1, const QBitArray &a2)
#define Q_DECLARE_EQUALITY_COMPARABLE(...)
constexpr bool operator!=(const timespec &t1, const timespec &t2)
static bool testBit(long bit, const long *field)
bool comparesEqual(const QDir &lhs, const QDir &rhs)
Definition qdir.cpp:1819
size_t qHash(const QFileSystemWatcherPathKey &key, size_t seed=0)
constexpr QKeyCombination operator|(Qt::Modifier modifier, Qt::Key key) noexcept
GLuint64 key
GLboolean GLboolean GLboolean GLboolean a
[7]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLboolean r
[2]
GLenum GLenum GLsizei count
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLint first
GLuint GLfloat * val
GLenum array
GLenum GLsizei len
const void * data_ptr(const QTransform &t)
Definition qpainter_p.h:48
static Q_CONSTINIT QBasicAtomicInteger< unsigned > seed
Definition qrandom.cpp:196
bool operator==(const QRandomGenerator &rng1, const QRandomGenerator &rng2)
Definition qrandom.cpp:1220
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
static bool operator&(QSortFilterProxyModelPrivate::Direction a, QSortFilterProxyModelPrivate::Direction b)
#define a2
#define a1
unsigned int quint32
Definition qtypes.h:50
unsigned char uchar
Definition qtypes.h:32
ptrdiff_t qsizetype
Definition qtypes.h:165
QUrl::FormattingOptions & operator|=(QUrl::FormattingOptions &i, QUrl::ComponentFormattingOptions f)
Definition qurl.h:301
QByteArray ba
[0]
QDataStream & operator<<(QDataStream &out, const MyClass &myObj)
[4]
QDataStream & operator>>(QDataStream &in, MyClass &myObj)
QObject::connect nullptr
ba setBit(0, true)
ba fill(true)
QBitArray().isNull()
[3]
QSharedPointer< T > other(t)
[5]