Implement a proper iterator for QTypedArrayData

This avoids ambiguities in our API when someone e.g. writes
vector.insert(0, ...).

It requires a slight workaround in qlalr, where std::search()
for libc++ doesn't like that our difference_type is qsizetype.

Change-Id: I40aa1040781ffbdd12d04410078207969b3bde53
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Lars Knoll 2020-05-26 13:30:04 +02:00
parent 0341bf2e97
commit ded37aedc9
2 changed files with 68 additions and 4 deletions

View File

@ -133,8 +133,72 @@ template <class T>
struct QTypedArrayData
: QArrayData
{
typedef T* iterator;
typedef const T* const_iterator;
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 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 bool operator==(const_iterator o) const { return i == o.i; }
inline bool operator!=(const_iterator o) const { return i != o.i; }
inline bool operator<(const_iterator other) const { return i < other.i; }
inline bool operator<=(const_iterator other) const { return i <= other.i; }
inline bool operator>(const_iterator other) const { return i > other.i; }
inline bool operator>=(const_iterator other) const { return i >= other.i; }
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; }
};
class AlignmentDummy { QArrayData header; T data; };

View File

@ -186,7 +186,7 @@ void Compress::operator () (int *table, int row_count, int column_count)
if (pos == info.begin ())
{
// try to find a perfect match
QList<int>::iterator pm = std::search(pos, info.end(), row.beginNonZeros(),
QList<int>::iterator pm = std::search(&*pos, &*info.end(), row.beginNonZeros(),
row.endNonZeros(), _PerfectMatch());
if (pm != info.end ())
@ -196,7 +196,7 @@ void Compress::operator () (int *table, int row_count, int column_count)
}
}
pos = std::search (pos, info.end (), row.beginNonZeros (), row.endNonZeros (), _Fit ());
pos = std::search (&*pos, &*info.end (), row.beginNonZeros (), row.endNonZeros (), _Fit ());
if (pos == info.end ())
break;