Move the iterator from QTypedArrayData to QList

The low level implementation does not use it at all, so there's no
point having the iterator in QTypedArrayData. Having it in QList removes
and indirection and will lead to clearer error messages.

Change-Id: I4af270c3cdb39620e5e52e835eb8fe1aa659e038
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
This commit is contained in:
Lars Knoll 2020-11-13 12:04:20 +01:00
parent cadfed83ac
commit 7fc302520b
5 changed files with 87 additions and 98 deletions

View File

@ -132,79 +132,6 @@ template <class T>
struct QTypedArrayData struct QTypedArrayData
: QArrayData : QArrayData
{ {
class iterator {
T *i = nullptr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef T value_type;
typedef T *pointer;
typedef T &reference;
inline constexpr iterator() = default;
inline iterator(T *n) : i(n) {}
inline T &operator*() const { return *i; }
inline T *operator->() const { return i; }
inline T &operator[](qsizetype j) const { return *(i + j); }
inline constexpr bool operator==(iterator o) const { return i == o.i; }
inline constexpr bool operator!=(iterator o) const { return i != o.i; }
inline constexpr bool operator<(iterator other) const { return i < other.i; }
inline constexpr bool operator<=(iterator other) const { return i <= other.i; }
inline constexpr bool operator>(iterator other) const { return i > other.i; }
inline constexpr bool operator>=(iterator other) const { return i >= other.i; }
inline constexpr bool operator==(pointer p) const { return i == p; }
inline constexpr bool operator!=(pointer p) const { return i != p; }
inline iterator &operator++() { ++i; return *this; }
inline iterator operator++(int) { T *n = i; ++i; return n; }
inline iterator &operator--() { i--; return *this; }
inline iterator operator--(int) { T *n = i; i--; return n; }
inline iterator &operator+=(qsizetype j) { i+=j; return *this; }
inline iterator &operator-=(qsizetype j) { i-=j; return *this; }
inline iterator operator+(qsizetype j) const { return iterator(i+j); }
inline iterator operator-(qsizetype j) const { return iterator(i-j); }
friend inline iterator operator+(qsizetype j, iterator k) { return k + j; }
inline qsizetype operator-(iterator j) const { return i - j.i; }
inline operator T*() const { return i; }
};
class const_iterator {
const T *i = nullptr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef T value_type;
typedef const T *pointer;
typedef const T &reference;
inline constexpr const_iterator() = default;
inline const_iterator(const T *n) : i(n) {}
inline constexpr const_iterator(iterator o): i(o) {}
inline const T &operator*() const { return *i; }
inline const T *operator->() const { return i; }
inline const T &operator[](qsizetype j) const { return *(i + j); }
inline constexpr bool operator==(const_iterator o) const { return i == o.i; }
inline constexpr bool operator!=(const_iterator o) const { return i != o.i; }
inline constexpr bool operator<(const_iterator other) const { return i < other.i; }
inline constexpr bool operator<=(const_iterator other) const { return i <= other.i; }
inline constexpr bool operator>(const_iterator other) const { return i > other.i; }
inline constexpr bool operator>=(const_iterator other) const { return i >= other.i; }
inline constexpr bool operator==(iterator o) const { return i == const_iterator(o).i; }
inline constexpr bool operator!=(iterator o) const { return i != const_iterator(o).i; }
inline constexpr bool operator==(pointer p) const { return i == p; }
inline constexpr bool operator!=(pointer p) const { return i != p; }
inline const_iterator &operator++() { ++i; return *this; }
inline const_iterator operator++(int) { const T *n = i; ++i; return n; }
inline const_iterator &operator--() { i--; return *this; }
inline const_iterator operator--(int) { const T *n = i; i--; return n; }
inline const_iterator &operator+=(qsizetype j) { i+=j; return *this; }
inline const_iterator &operator-=(qsizetype j) { i-=j; return *this; }
inline const_iterator operator+(qsizetype j) const { return const_iterator(i+j); }
inline const_iterator operator-(qsizetype j) const { return const_iterator(i-j); }
friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; }
inline qsizetype operator-(const_iterator j) const { return i - j.i; }
inline operator const T*() const { return i; }
};
struct AlignmentDummy { QArrayData header; T data; }; struct AlignmentDummy { QArrayData header; T data; };
[[nodiscard]] static QPair<QTypedArrayData *, T *> allocate(qsizetype capacity, AllocationOption option = QArrayData::KeepSize) [[nodiscard]] static QPair<QTypedArrayData *, T *> allocate(qsizetype capacity, AllocationOption option = QArrayData::KeepSize)

View File

@ -635,7 +635,7 @@ public:
{ {
Q_ASSERT(this->isMutable()); Q_ASSERT(this->isMutable());
Q_ASSERT(this->size); Q_ASSERT(this->size);
(--this->end())->~T(); (this->end() - 1)->~T();
--this->size; --this->size;
} }
@ -883,8 +883,6 @@ struct QCommonArrayOps : QArrayOpsSelector<T>::Type
using Data = QTypedArrayData<T>; using Data = QTypedArrayData<T>;
using DataPointer = QArrayDataPointer<T>; using DataPointer = QArrayDataPointer<T>;
using parameter_type = typename Base::parameter_type; using parameter_type = typename Base::parameter_type;
using iterator = typename Base::iterator;
using const_iterator = typename Base::const_iterator;
protected: protected:
using Self = QCommonArrayOps<T>; using Self = QCommonArrayOps<T>;

View File

@ -53,8 +53,6 @@ private:
typedef QArrayDataOps<T> DataOps; typedef QArrayDataOps<T> DataOps;
public: public:
typedef typename Data::iterator iterator;
typedef typename Data::const_iterator const_iterator;
enum { enum {
pass_parameter_by_value = pass_parameter_by_value =
std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_enum<T>::value std::is_arithmetic<T>::value || std::is_pointer<T>::value || std::is_enum<T>::value
@ -142,12 +140,12 @@ public:
T *data() noexcept { return ptr; } T *data() noexcept { return ptr; }
const T *data() const noexcept { return ptr; } const T *data() const noexcept { return ptr; }
iterator begin(iterator = iterator()) noexcept { return data(); } T *begin() noexcept { return data(); }
iterator end(iterator = iterator()) noexcept { return data() + size; } T *end() noexcept { return data() + size; }
const_iterator begin(const_iterator = const_iterator()) const noexcept { return data(); } const T *begin() const noexcept { return data(); }
const_iterator end(const_iterator = const_iterator()) const noexcept { return data() + size; } const T *end() const noexcept { return data() + size; }
const_iterator constBegin(const_iterator = const_iterator()) const noexcept { return data(); } const T *constBegin() const noexcept { return data(); }
const_iterator constEnd(const_iterator = const_iterator()) const noexcept { return data() + size; } const T *constEnd() const noexcept { return data() + size; }
void swap(QArrayDataPointer &other) noexcept void swap(QArrayDataPointer &other) noexcept
{ {

View File

@ -120,17 +120,6 @@ public:
using const_reference = const T &; using const_reference = const T &;
using size_type = qsizetype; using size_type = qsizetype;
using difference_type = qptrdiff; using difference_type = qptrdiff;
#ifndef Q_QDOC
using iterator = typename Data::iterator;
using const_iterator = typename Data::const_iterator;
#else // simplified aliases for QDoc
using iterator = T *;
using const_iterator = const T *;
#endif
using Iterator = iterator;
using ConstIterator = const_iterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
#ifndef Q_QDOC #ifndef Q_QDOC
using parameter_type = typename DataPointer::parameter_type; using parameter_type = typename DataPointer::parameter_type;
using rvalue_ref = typename std::conditional<DataPointer::pass_parameter_by_value, DisableRValueRefs, T &&>::type; using rvalue_ref = typename std::conditional<DataPointer::pass_parameter_by_value, DisableRValueRefs, T &&>::type;
@ -139,6 +128,83 @@ public:
using rvalue_ref = T &&; using rvalue_ref = T &&;
#endif #endif
class iterator {
T *i = nullptr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef T value_type;
typedef T *pointer;
typedef T &reference;
inline constexpr iterator() = default;
inline iterator(T *n) : i(n) {}
inline T &operator*() const { return *i; }
inline T *operator->() const { return i; }
inline T &operator[](qsizetype j) const { return *(i + j); }
inline constexpr bool operator==(iterator o) const { return i == o.i; }
inline constexpr bool operator!=(iterator o) const { return i != o.i; }
inline constexpr bool operator<(iterator other) const { return i < other.i; }
inline constexpr bool operator<=(iterator other) const { return i <= other.i; }
inline constexpr bool operator>(iterator other) const { return i > other.i; }
inline constexpr bool operator>=(iterator other) const { return i >= other.i; }
inline constexpr bool operator==(pointer p) const { return i == p; }
inline constexpr bool operator!=(pointer p) const { return i != p; }
inline iterator &operator++() { ++i; return *this; }
inline iterator operator++(int) { T *n = i; ++i; return n; }
inline iterator &operator--() { i--; return *this; }
inline iterator operator--(int) { T *n = i; i--; return n; }
inline iterator &operator+=(qsizetype j) { i+=j; return *this; }
inline iterator &operator-=(qsizetype j) { i-=j; return *this; }
inline iterator operator+(qsizetype j) const { return iterator(i+j); }
inline iterator operator-(qsizetype j) const { return iterator(i-j); }
friend inline iterator operator+(qsizetype j, iterator k) { return k + j; }
inline qsizetype operator-(iterator j) const { return i - j.i; }
inline operator T*() const { return i; }
};
class const_iterator {
const T *i = nullptr;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef T value_type;
typedef const T *pointer;
typedef const T &reference;
inline constexpr const_iterator() = default;
inline const_iterator(const T *n) : i(n) {}
inline constexpr const_iterator(iterator o): i(o) {}
inline const T &operator*() const { return *i; }
inline const T *operator->() const { return i; }
inline const T &operator[](qsizetype j) const { return *(i + j); }
inline constexpr bool operator==(const_iterator o) const { return i == o.i; }
inline constexpr bool operator!=(const_iterator o) const { return i != o.i; }
inline constexpr bool operator<(const_iterator other) const { return i < other.i; }
inline constexpr bool operator<=(const_iterator other) const { return i <= other.i; }
inline constexpr bool operator>(const_iterator other) const { return i > other.i; }
inline constexpr bool operator>=(const_iterator other) const { return i >= other.i; }
inline constexpr bool operator==(iterator o) const { return i == const_iterator(o).i; }
inline constexpr bool operator!=(iterator o) const { return i != const_iterator(o).i; }
inline constexpr bool operator==(pointer p) const { return i == p; }
inline constexpr bool operator!=(pointer p) const { return i != p; }
inline const_iterator &operator++() { ++i; return *this; }
inline const_iterator operator++(int) { const T *n = i; ++i; return n; }
inline const_iterator &operator--() { i--; return *this; }
inline const_iterator operator--(int) { const T *n = i; i--; return n; }
inline const_iterator &operator+=(qsizetype j) { i+=j; return *this; }
inline const_iterator &operator-=(qsizetype j) { i-=j; return *this; }
inline const_iterator operator+(qsizetype j) const { return const_iterator(i+j); }
inline const_iterator operator-(qsizetype j) const { return const_iterator(i-j); }
friend inline const_iterator operator+(qsizetype j, const_iterator k) { return k + j; }
inline qsizetype operator-(const_iterator j) const { return i - j.i; }
inline operator const T*() const { return i; }
};
using Iterator = iterator;
using ConstIterator = const_iterator;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
private: private:
void resize_internal(qsizetype i); void resize_internal(qsizetype i);
bool isValidIterator(const_iterator i) const bool isValidIterator(const_iterator i) const
@ -758,7 +824,7 @@ typename QList<T>::iterator QList<T>::erase(const_iterator abegin, const_iterato
Q_ASSERT_X(isValidIterator(aend), "QList::erase", "The specified iterator argument 'aend' is invalid"); Q_ASSERT_X(isValidIterator(aend), "QList::erase", "The specified iterator argument 'aend' is invalid");
Q_ASSERT(aend >= abegin); Q_ASSERT(aend >= abegin);
qsizetype i = std::distance(d.constBegin(), abegin); qsizetype i = std::distance(constBegin(), abegin);
qsizetype n = std::distance(abegin, aend); qsizetype n = std::distance(abegin, aend);
remove(i, n); remove(i, n);

View File

@ -44,8 +44,8 @@ private:
public: public:
typedef T value_type; typedef T value_type;
typedef typename Data::iterator iterator; typedef T *iterator;
typedef typename Data::const_iterator const_iterator; typedef const T *const_iterator;
SimpleVector() SimpleVector()
{ {