- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files
- Directories
- Examples

- File List
- File Members

# qbitarray.h

Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
    00004 ** All rights reserved.
    00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
    00006 **
    00007 ** This file is part of the QtCore module of the Qt Toolkit.
    00008 **
    00009 ** $QT_BEGIN_LICENSE:LGPL$
    00010 ** Commercial Usage
    00011 ** Licensees holding valid Qt Commercial licenses may use this file in
    00012 ** accordance with the Qt Commercial License Agreement provided with the
    00013 ** Software or, alternatively, in accordance with the terms contained in
    00014 ** a written agreement between you and Nokia.
    00015 **
    00016 ** GNU Lesser General Public License Usage
    00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
    00018 ** General Public License version 2.1 as published by the Free Software
    00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
    00020 ** packaging of this file.  Please review the following information to
    00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
    00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
    00023 **
    00024 ** In addition, as a special exception, Nokia gives you certain additional
    00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
    00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
    00027 **
    00028 ** GNU General Public License Usage
    00029 ** Alternatively, this file may be used under the terms of the GNU
    00030 ** General Public License version 3.0 as published by the Free Software
    00031 ** Foundation and appearing in the file LICENSE.GPL included in the
    00032 ** packaging of this file.  Please review the following information to
    00033 ** ensure the GNU General Public License version 3.0 requirements will be
    00034 ** met: http://www.gnu.org/copyleft/gpl.html.
    00035 **
    00036 ** If you have questions regarding the use of this file, please contact
    00037 ** Nokia at qt-info@nokia.com.
    00038 ** $QT_END_LICENSE$
    00039 **
    00040 ****************************************************************************/
    00041
    00042 #ifndef QBITARRAY_H
    00043 #define QBITARRAY_H
    00044
    00045 #include <QtCore/qbytearray.h>
    00046
    00047 QT_BEGIN_HEADER
    00048
    00049 QT_BEGIN_NAMESPACE
    00050
    00051 QT_MODULE(Core)
    00052
    00053 class QBitRef;
    00054 class Q_CORE_EXPORT QBitArray
    00055 {
    00056     friend Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
    00057     friend Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
    00058     friend Q_CORE_EXPORT uint qHash(const QBitArray &key);
    00059     QByteArray d;
    00060
    00061 public:
    00062     inline QBitArray() {}
    00063     explicit QBitArray(int size, bool val = false);
    00064     QBitArray(const QBitArray &other) : d(other.d) {}
    00065     inline QBitArray &operator=(const QBitArray &other) { d = other.d; return *this; }
    00066
    00067     inline int size() const { return (d.size() << 3) - *d.constData(); }
    00068     inline int count() const { return (d.size() << 3) - *d.constData(); }
    00069     int count(bool on) const;
    00070     // ### Qt 5: Store the number of set bits separately
    00071
    00072     inline bool isEmpty() const { return d.isEmpty(); }
    00073     inline bool isNull() const { return d.isNull(); }
    00074
    00075     void resize(int size);
    00076
    00077     inline void detach() { d.detach(); }
    00078     inline bool isDetached() const { return d.isDetached(); }
    00079     inline void clear() { d.clear(); }
    00080
    00081     bool testBit(int i) const;
    00082     void setBit(int i);
    00083     void setBit(int i, bool val);
    00084     void clearBit(int i);
    00085     bool toggleBit(int i);
    00086
    00087     bool at(int i) const;
    00088     QBitRef operator[](int i);
    00089     bool operator[](int i) const;
    00090     QBitRef operator[](uint i);
    00091     bool operator[](uint i) const;
    00092
    00093     QBitArray& operator&=(const QBitArray &);
    00094     QBitArray& operator|=(const QBitArray &);
    00095     QBitArray& operator^=(const QBitArray &);
    00096     QBitArray  operator~() const;
    00097
    00098     inline bool operator==(const QBitArray& a) const { return d == a.d; }
    00099     inline bool operator!=(const QBitArray& a) const { return d != a.d; }
    00100
    00101     inline bool fill(bool val, int size = -1);
    00102     void fill(bool val, int first, int last);
    00103
    00104     inline void truncate(int pos) { if (pos < size()) resize(pos); }
    00105
    00106 public:
    00107     typedef QByteArray::DataPtr DataPtr;
    00108     inline DataPtr &data_ptr() { return d.data_ptr(); }
    00109 };
    00110
    00111 inline bool QBitArray::fill(bool aval, int asize)
    00112 { *this = QBitArray((asize < 0 ? this->size() : asize), aval); return true; }
    00113
    00114 Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &);
    00115 Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &);
    00116 Q_CORE_EXPORT QBitArray operator^(const QBitArray &, const QBitArray &);
    00117
    00118 inline bool QBitArray::testBit(int i) const
    00119 { Q_ASSERT(i >= 0 && i < size());
    00120  return (*(reinterpret_cast<const uchar*>(d.constData())+1+(i>>3)) & (1 << (i & 7))) != 0; }
    00121
    00122 inline void QBitArray::setBit(int i)
    00123 { Q_ASSERT(i >= 0 && i < size());
    00124  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) |= uchar(1 << (i & 7)); }
    00125
    00126 inline void QBitArray::clearBit(int i)
    00127 { Q_ASSERT(i >= 0 && i < size());
    00128  *(reinterpret_cast<uchar*>(d.data())+1+(i>>3)) &= ~uchar(1 << (i & 7)); }
    00129
    00130 inline void QBitArray::setBit(int i, bool val)
    00131 { if (val) setBit(i); else clearBit(i); }
    00132
    00133 inline bool QBitArray::toggleBit(int i)
    00134 { Q_ASSERT(i >= 0 &&  i < size());
    00135  uchar b = uchar(1<<(i&7)); uchar* p = reinterpret_cast<uchar*>(d.data())+1+(i>>3);
    00136  uchar c = uchar(*p&b); *p^=b; return c!=0; }
    00137
    00138 inline bool QBitArray::operator[](int i) const { return testBit(i); }
    00139 inline bool QBitArray::operator[](uint i) const { return testBit(i); }
    00140 inline bool QBitArray::at(int i) const { return testBit(i); }
    00141
    00142 class Q_CORE_EXPORT QBitRef
    00143 {
    00144 private:
    00145     QBitArray& a;
    00146     int i;
    00147     inline QBitRef(QBitArray& array, int idx) : a(array), i(idx) {}
    00148     friend class QBitArray;
    00149 public:
    00150     inline operator bool() const { return a.testBit(i); }
    00151     inline bool operator!() const { return !a.testBit(i); }
    00152     QBitRef& operator=(const QBitRef& val) { a.setBit(i, val); return *this; }
    00153     QBitRef& operator=(bool val) { a.setBit(i, val); return *this; }
    00154 };
    00155
    00156 inline QBitRef QBitArray::operator[](int i)
    00157 { Q_ASSERT(i >= 0); return QBitRef(*this, i); }
    00158 inline QBitRef QBitArray::operator[](uint i)
    00159 { return QBitRef(*this, i); }
    00160
    00161
    00162 #ifndef QT_NO_DATASTREAM
    00163 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QBitArray &);
    00164 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QBitArray &);
    00165 #endif
    00166
    00167 Q_DECLARE_TYPEINFO(QBitArray, Q_MOVABLE_TYPE);
    00168 Q_DECLARE_SHARED(QBitArray)
    00169
    00170 QT_END_NAMESPACE
    00171
    00172 QT_END_HEADER
    00173
    00174 #endif // QBITARRAY_H