diff options
author | Olivier Goffart <olivier.goffart@nokia.com> | 2010-11-02 12:35:20 (GMT) |
---|---|---|
committer | Olivier Goffart <olivier.goffart@nokia.com> | 2010-11-02 12:44:23 (GMT) |
commit | ca7a4269104cbca572460791c3343778c0a3317b (patch) | |
tree | 04dfac50e982282e30f8698544eed758b20b378d /src/corelib/tools | |
parent | 57ac6015ab50d96a180a82676e9e1c3b702c0678 (diff) | |
download | Qt-ca7a4269104cbca572460791c3343778c0a3317b.zip Qt-ca7a4269104cbca572460791c3343778c0a3317b.tar.gz Qt-ca7a4269104cbca572460791c3343778c0a3317b.tar.bz2 |
QVarLenghtArray: Implement more API from QVector
... for consistency and to allow easy replacement of one by the other.
In particular this change allows to use QVarLenghtArray inside foreach
Reviewed-by: Gabriel
Task-number: QTBUG-14010
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qvarlengtharray.h | 124 | ||||
-rw-r--r-- | src/corelib/tools/qvarlengtharray.qdoc | 208 |
2 files changed, 332 insertions, 0 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 4a6bb4b..3882323 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -44,7 +44,10 @@ #include <QtCore/qcontainerfwd.h> #include <QtCore/qglobal.h> +#include <QtCore/qalgorithms.h> + #include <new> +#include <string.h> QT_BEGIN_HEADER @@ -123,6 +126,18 @@ public: } } void append(const T *buf, int size); + inline QVarLengthArray<T, Prealloc> &operator<<(const T &t) + { append(t); return *this; } + inline QVarLengthArray<T, Prealloc> &operator+=(const T &t) + { append(t); return *this; } + + void prepend(const T &t); + void insert(int i, const T &t); + void insert(int i, int n, const T &t); + void replace(int i, const T &t); + void remove(int i); + void remove(int i, int n); + inline T *data() { return ptr; } inline const T *data() const { return ptr; } @@ -135,6 +150,21 @@ public: typedef const value_type &const_reference; typedef qptrdiff difference_type; + + typedef T* iterator; + typedef const T* const_iterator; + + inline iterator begin() { return ptr; } + inline const_iterator begin() const { return ptr; } + inline const_iterator constBegin() const { return ptr; } + inline iterator end() { return ptr + s; } + inline const_iterator end() const { return ptr + s; } + inline const_iterator constEnd() const { return ptr + s; } + iterator insert(iterator before, int n, const T &x); + inline iterator insert(iterator before, const T &x) { return insert(before, 1, x); } + iterator erase(iterator begin, iterator end); + inline iterator erase(iterator pos) { return erase(pos, pos+1); } + private: friend class QPodList<T, Prealloc>; void realloc(int size, int alloc); @@ -272,6 +302,100 @@ Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defau return (i < 0 || i >= size()) ? defaultValue : at(i); } +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::insert(int i, const T &t) +{ Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range"); + insert(begin() + i, 1, t); } +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::insert(int i, int n, const T &t) +{ Q_ASSERT_X(i >= 0 && i <= s, "QVarLengthArray::insert", "index out of range"); + insert(begin() + i, n, t); } +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::remove(int i, int n) +{ Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= s, "QVarLengthArray::remove", "index out of range"); + erase(begin() + i, begin() + i + n); } +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::remove(int i) +{ Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::remove", "index out of range"); + erase(begin() + i, begin() + i + 1); } +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::prepend(const T &t) +{ insert(begin(), 1, t); } + +template <class T, int Prealloc> +inline void QVarLengthArray<T, Prealloc>::replace(int i, const T &t) +{ + Q_ASSERT_X(i >= 0 && i < s, "QVarLengthArray::replace", "index out of range"); + const T copy(t); + data()[i] = copy; +} + + +template <class T, int Prealloc> +Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::insert(iterator before, size_type n, const T &t) +{ + int offset = int(before - ptr); + if (n != 0) { + resize(s + n); + const T copy(t); + if (QTypeInfo<T>::isStatic) { + T *b = ptr + offset; + T *j = ptr + s; + T *i = j - n; + while (i != b) + *--j = *--i; + i = b + n; + while (i != b) + *--i = copy; + } else { + T *b = ptr + offset; + T *i = b + n; + memmove(i, b, (s - offset - n) * sizeof(T)); + while (i != b) + new (--i) T(copy); + } + } + return ptr + offset; +} + +template <class T, int Prealloc> +Q_OUTOFLINE_TEMPLATE typename QVarLengthArray<T, Prealloc>::iterator QVarLengthArray<T, Prealloc>::erase(iterator abegin, iterator aend) +{ + int f = int(abegin - ptr); + int l = int(aend - ptr); + int n = l - f; + if (QTypeInfo<T>::isComplex) { + qCopy(ptr + l, ptr + s, ptr + f); + T *i = ptr + s; + T *b = ptr + s - n; + while (i != b) { + --i; + i->~T(); + } + } else { + memmove(ptr + f, ptr + l, (s - l) * sizeof(T)); + } + s -= n; + return ptr + f; +} + +template <typename T, int Prealloc1, int Prealloc2> +bool operator==(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r) +{ + if (l.size() != r.size()) + return false; + for (int i = 0; i < l.size(); i++) { + if (l.at(i) != r.at(i)) + return false; + } + return true; +} + +template <typename T, int Prealloc1, int Prealloc2> +bool operator!=(const QVarLengthArray<T, Prealloc1> &l, const QVarLengthArray<T, Prealloc2> &r) +{ + return !(l == r); +} QT_END_NAMESPACE diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 6f91bcc..4d5d5d1 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -337,3 +337,211 @@ Typedef for const T &. Provided for STL compatibility. */ +/*! \fn void QVarLengthArray::prepend(const T &value) + + \since 4.8 + Inserts \a value at the beginning of the array. + + + This is the same as vector.insert(0, \a value). + + For large arrays, this operation can be slow (\l{linear time}), + because it requires moving all the items in the vector by one + position further in memory. If you want a container class that + provides a fast prepend() function, use QList or QLinkedList + instead. + + \sa append(), insert() +*/ + +/*! \fn void QVarLengthArray::replace(int i, const T &value) + + \since 4.8 + Replaces the item at index position \a i with \a value. + + \a i must be a valid index position in the array (i.e., 0 <= \a + i < size()). + + \sa operator[](), remove() +*/ + +/*! \fn void QVarLengthArray::remove(int i) + + \overload + \since 4.8 + + Removes the element at index position \a i. + + \sa insert(), replace() +*/ + +/*! \fn void QVarLengthArray::remove(int i, int count) + + \overload + \since 4.8 + + Removes \a count elements from the middle of the array, starting at + index position \a i. + + \sa insert(), replace() +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::begin() + \since 4.8 + + Returns an \l{STL-style iterator} pointing to the first item in + the array. + + \sa constBegin(), end() +*/ + +/*! \fn QVarLengthArray::const_iterator QVarLengthArray::begin() const + \since 4.8 + \overload +*/ + +/*! \fn QVarLengthArray::const_iterator QVarLengthArray::constBegin() const + \since 4.8 + + Returns a const \l{STL-style iterator} pointing to the first item + in the array. + + \sa begin(), constEnd() +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::end() + \since 4.8 + + Returns an \l{STL-style iterator} pointing to the imaginary item + after the last item in the array. + + \sa begin(), constEnd() +*/ + +/*! \fn QVarLengthArray::const_iterator QVarLengthArray::end() const + \since 4.8 + + \overload +*/ + +/*! \fn QVarLengthArray::const_iterator QVarLengthArray::constEnd() const + \since 4.8 + + Returns a const \l{STL-style iterator} pointing to the imaginary + item after the last item in the array. + + \sa constBegin(), end() +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator pos) + \since 4.8 + + Removes the item pointed to by the iterator \a pos from the + vector, and returns an iterator to the next item in the vector + (which may be end()). + + \sa insert(), remove() +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::erase(iterator begin, iterator end) + + \overload + \since 4.8 + + Removes all the items from \a begin up to (but not including) \a + end. Returns an iterator to the same item that \a end referred to + before the call. +*/ + +/*! \fn void QVarLengthArray::insert(int i, const T &value) + \since 4.8 + + Inserts \a value at index position \a i in the array. If \a i is + 0, the value is prepended to the vector. If \a i is size(), the + value is appended to the vector. + + For large arrays, this operation can be slow (\l{linear time}), + because it requires moving all the items at indexes \a i and + above by one position further in memory. If you want a container + class that provides a fast insert() function, use QLinkedList + instead. + + \sa remove() +*/ + +/*! \fn void QVarLengthArray::insert(int i, int count, const T &value) + + \overload + \since 4.8 + + Inserts \a count copies of \a value at index position \a i in the + vector. +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, const T &value) + + \overload + \since 4.8 + + Inserts \a value in front of the item pointed to by the iterator + \a before. Returns an iterator pointing at the inserted item. +*/ + +/*! \fn QVarLengthArray::iterator QVarLengthArray::insert(iterator before, int count, const T &value) + + \since 4.8 + Inserts \a count copies of \a value in front of the item pointed to + by the iterator \a before. Returns an iterator pointing at the + first of the inserted items. +*/ + + + +/*! \fn bool operator==(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right) + + \relates QVarLengthArray + \since 4.8 + Returns true if the two array are equal; + + Two arrays are considered equal if they contain the same values + in the same order. + + This function requires the value type to have an implementation + of \c operator==(). + + \sa operator!=() +*/ + +/*! \fn bool operator!=(const QVarLengthArray<T, Prealloc1> &left, const QVarLengthArray<T, Prealloc2> &right) + + \relates QVarLengthArray + \since 4.8 + Returns true if the two array are different; + + Two arrays are considered equal if they contain the same values + in the same order. + + This function requires the value type to have an implementation + of \c operator==(). + + \sa operator==() +*/ + +/*! \fn QVarLengthArray &QVarLengthArray::operator<<(const T &value) + + \since 4.8 + Appends \a value to the array and returns a reference to this + vector. + + \sa append(), operator+=() +*/ + +/*! \fn QVarLengthArray &QVarLengthArray::operator+=(const T &value) + + \since 4.8 + Appends \a value to the array and returns a reference to this + vector. + + \sa append(), operator<<() +*/ + |