QtCore/qbitarray.h Source File

qbitarray.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QBITARRAY_H
43 #define QBITARRAY_H
44 
45 #include <QtCore/qbytearray.h>
46 
48 
50 
51 QT_MODULE(Core)
52 
53 class QBitRef;
54 class Q_CORE_EXPORT QBitArray
55 {
56  friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
57  friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
58  friend Q_CORE_EXPORT uint qHash(const QBitArray &key);
59  QByteArray d;
60 
61 public:
62  inline QBitArray() {}
63  explicit QBitArray(int size, bool val = false);
64  QBitArray(const QBitArray &other) : d(other.d) {}
65  inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
66 #ifdef Q_COMPILER_RVALUE_REFS
67  inline QBitArray &operator=(QBitArray &&other)
68  { qSwap(d, other.d); return *this; }
69 #endif
70 
71  inline void swap(QBitArray &other) { qSwap(d, other.d); }
72 
73  inline int size() const { return (d.size() << 3) - *d.constData(); }
74  inline int count() const { return (d.size() << 3) - *d.constData(); }
75  int count(bool on) const;
76  // ### Qt 5: Store the number of set bits separately
77 
78  inline bool isEmpty() const { return d.isEmpty(); }
79  inline bool isNull() const { return d.isNull(); }
80 
81  void resize(int size);
82 
83  inline void detach() { d.detach(); }
84  inline bool isDetached() const { return d.isDetached(); }
85  inline void clear() { d.clear(); }
86 
87  bool testBit(int i) const;
88  void setBit(int i);
89  void setBit(int i, bool val);
90  void clearBit(int i);
91  bool toggleBit(int i);
92 
93  bool at(int i) const;
94  QBitRef operator[](int i);
95  bool operator[](int i) const;
96  QBitRef operator[](uint i);
97  bool operator[](uint i) const;
98 
99  QBitArray& operator&=(const QBitArray &);
100  QBitArray& operator|=(const QBitArray &);
101  QBitArray& operator^=(const QBitArray &);
102  QBitArray operator~() const;
103 
104  inline bool operator==(const QBitArray& a) const { return d == a.d; }
105  inline bool operator!=(const QBitArray& a) const { return d != a.d; }
106 
107  inline bool fill(bool val, int size = -1);
108  void fill(bool val, int first, int last);
109 
110  inline void truncate(int pos) { if (pos < size()) resize(pos); }
111 
112 public:
114  inline DataPtr &data_ptr() { return d.data_ptr(); }
115 };
116 
117 inline bool QBitArray::fill(bool aval, int asize)
118 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
119 
120 Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
121 Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
122 Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
123 
124 inline bool QBitArray::testBit(int i) const
125 { Q_ASSERT(uint(i) < uint(size()));
126  return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
127 
128 inline void QBitArray::setBit(int i)
129 { Q_ASSERT(uint(i) < uint(size()));
130  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
131 
132 inline void QBitArray::clearBit(int i)
133 { Q_ASSERT(uint(i) < uint(size()));
134  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
135 
136 inline void QBitArray::setBit(int i, bool val)
137 { if (val) setBit(i); else clearBit(i); }
138 
139 inline bool QBitArray::toggleBit(int i)
140 { Q_ASSERT(uint(i) < uint(size()));
141  uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
142  uchar c = uchar(*p&b); *p^=b; return c!=0; }
143 
144 inline bool QBitArray::operator[](int i) const { return testBit(i); }
145 inline bool QBitArray::operator[](uint i) const { return testBit(i); }
146 inline bool QBitArray::at(int i) const { return testBit(i); }
147 
148 class Q_CORE_EXPORT QBitRef
149 {
150 private:
151  QBitArray& a;
152  int i;
153  inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {}
154  friend class QBitArray;
155 public:
156  inline operator bool() const { return a.testBit(i); }
157  inline bool operator!() const { return !a.testBit(i); }
158  QBitRef& operator=(const QBitRef& val) { a.setBit(i, val); return *this; }
159  QBitRef& operator=(bool val) { a.setBit(i, val); return *this; }
160 };
161 
163 { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
165 { return QBitRef(*this, i); }
166 
167 
168 #ifndef QT_NO_DATASTREAM
169 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
170 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
171 #endif
172 
173 Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE);
174 Q_DECLARE_SHARED(QBitArray)
175 
177 
179 
180 #endif // QBITARRAY_H
void setBit(int i)
Definition: qbitarray.h:128
DataPtr & data_ptr()
Definition: qbitarray.h:114
uint PHONON_EXPORT qHash(const Phonon::EffectParameter &param)
#define QT_END_NAMESPACE
Definition: qglobal.h:128
bool toggleBit(int i)
Definition: qbitarray.h:139
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
DataPtr & data_ptr()
Definition: qbytearray.h:397
void detach()
Definition: qbitarray.h:83
QByteArray::DataPtr DataPtr
Definition: qbitarray.h:113
QBitArray(const QBitArray &other)
Definition: qbitarray.h:64
bool isDetached() const
Definition: qbitarray.h:84
void truncate(int pos)
Definition: qbitarray.h:110
int count() const
Definition: qbitarray.h:74
bool operator!() const
Definition: qbitarray.h:157
char * data()
Definition: qbytearray.h:429
Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE)
QBitArray & operator=(const QBitArray &other)
Definition: qbitarray.h:65
const char * constData() const
Definition: qbytearray.h:433
Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &)
Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &)
void clearBit(int i)
Definition: qbitarray.h:132
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
void clear()
Definition: qbitarray.h:85
GLint * first
Definition: GLee.h:1362
bool operator==(const QBitArray &a) const
Definition: qbitarray.h:104
Q_INLINE_TEMPLATE void qSwap(QScopedPointer< T, Cleanup > &p1, QScopedPointer< T, Cleanup > &p2)
int size() const
Definition: qbitarray.h:73
bool operator!=(const QBitArray &a) const
Definition: qbitarray.h:105
QBitRef & operator=(bool val)
Definition: qbitarray.h:159
GLuint GLuint GLsizei count
Definition: GLee.h:872
Q_CORE_EXPORT QDataStream & operator<<(QDataStream &, const QBitArray &)
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QBitArray &)
bool isNull() const
Definition: qbitarray.h:79
bool at(int i) const
Definition: qbitarray.h:146
void detach()
Definition: qbytearray.h:435
GLubyte GLubyte b
Definition: GLee.h:5404
bool isEmpty() const
Definition: qbytearray.h:421
bool fill(bool val, int size=-1)
Definition: qbitarray.h:117
GLfloat GLfloat p
Definition: GLee.h:5416
const GLubyte * c
Definition: GLee.h:5419
QBitRef operator[](int i)
Definition: qbitarray.h:162
GLubyte GLubyte GLubyte a
Definition: GLee.h:5404
Data * DataPtr
Definition: qbytearray.h:396
void clear()
void swap(QBitArray &other)
Definition: qbitarray.h:71
bool testBit(int i) const
Definition: qbitarray.h:124
QBitArray()
Definition: qbitarray.h:62
Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &)
int size() const
Definition: qbytearray.h:402
bool isDetached() const
Definition: qbytearray.h:437
bool isNull() const
#define QT_END_HEADER
Definition: qglobal.h:142
bool isEmpty() const
Definition: qbitarray.h:78
GLsizeiptr size
Definition: GLee.h:1561
QBitRef & operator=(const QBitRef &val)
Definition: qbitarray.h:158