OSDN Git Service

merge QStdList<T> into QStdVector<T>
authorIvailo Monev <xakepa10@gmail.com>
Mon, 20 Sep 2021 17:03:33 +0000 (20:03 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Mon, 20 Sep 2021 17:03:33 +0000 (20:03 +0300)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/tools/qstdcontainers_p.h

index f713ba0..b735b4a 100644 (file)
@@ -36,7 +36,6 @@
 #include <QtCore/qalgorithms.h>
 
 #include <vector>
-#include <list>
 #include <unordered_set>
 
 QT_BEGIN_NAMESPACE
@@ -99,12 +98,26 @@ public:
 
     typedef typename Data::iterator Iterator;
     typedef typename Data::const_iterator ConstIterator;
-     // type deduction for e.g. qMin()/qMax()
+    // type deduction for e.g. qMin()/qMax()
     inline int size() const { return Data::size(); }
     inline int capacity() const { return Data::capacity(); }
-     // release memory and set capacity to 0
+    // release memory and set capacity to 0
     inline void clear() { Data::clear(); Data::shrink_to_fit(); }
 
+    // QList<T> compat
+    inline int length() const { return Data::size(); }
+    inline void removeFirst() { Q_ASSERT(!isEmpty()); Data::erase(Data::begin()); }
+    inline void removeLast() { Q_ASSERT(!isEmpty()); Data::erase(Data::end()); }
+
+    void append(const QStdVector<T> &l);
+    void removeAt(int i);
+    int removeAll(const T &t);
+    bool removeOne(const T &t);
+    T takeAt(int i);
+    T takeFirst();
+    T takeLast();
+    void move(int from, int to);
+
 #if !defined(Q_NO_USING_KEYWORD)
     using Data::insert;
 #endif
@@ -235,96 +248,9 @@ QStdVector<T> QStdVector<T>::mid(int pos, int length) const
     return copy;
 }
 
-Q_DECLARE_SEQUENTIAL_ITERATOR(StdVector)
-Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(StdVector)
-
-
+// QList<T> compat
 template <typename T>
-class Q_CORE_EXPORT QStdList : public std::vector<T>
-{
-    typedef std::vector<T> Data;
-
-public:
-    QStdList() : Data() { }
-    QStdList(const QStdList &other) : Data() {
-        Data::reserve(other.size());
-        for (size_t i = 0;  i < other.size(); i++)
-            Data::push_back(other.at(i));
-    }
-    explicit QStdList(int size) : Data(size) { }
-#ifdef Q_COMPILER_INITIALIZER_LISTS
-    inline QStdList(std::initializer_list<T> args) : Data(args) { }
-#endif
-
-    inline int length() const { return Data::size(); }
-    inline void removeFirst() { Q_ASSERT(!isEmpty()); Data::erase(Data::begin()); }
-    inline void removeLast() { Q_ASSERT(!isEmpty()); Data::erase(Data::end()); }
-
-    void append(const QStdList<T> &l);
-    void removeAt(int i);
-    int removeAll(const T &t);
-    bool removeOne(const T &t);
-    T takeAt(int i);
-    T takeFirst();
-    T takeLast();
-    void move(int from, int to);
-
-    static inline QStdList<T> fromStdList(const std::list<T> &list)
-    { QStdList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
-    inline std::list<T> toStdList() const
-    { std::list<T> tmp; qCopy(Data::cbegin(), Data::cend(), std::back_inserter(tmp)); return tmp; }
-
-    // almost the same as QVector<T>
-    inline bool isEmpty() const { return Data::empty(); }
-    inline void append(const T &t) { Data::push_back(t); }
-    inline void prepend(const T &t) { Data::insert(Data::begin(), t); }
-    inline void insert(int i, const T &t) { Data::insert(Data::begin() + i, t); }
-    inline void insert(int i, int n, const T &t) { Data::insert(Data::begin() + i, n, t); }
-    inline void push_front(const T &t) { prepend(t); }
-
-    void replace(int i, const T &t);
-    void remove(int i);
-    void remove(int i, int n);
-    int indexOf(const T &t, int from = 0) const;
-    int lastIndexOf(const T &t, int from = -1) const;
-    bool contains(const T &t) const;
-    int count(const T &t) const;
-    T value(int i) const;
-    T value(int i, const T &defaultValue) const;
-    QStdList<T> mid(int pos, int length = -1) const;
-
-    inline int count() const { return Data::size(); }
-    inline T& first() { Q_ASSERT(!isEmpty()); return Data::front(); }
-    inline const T &first() const { Q_ASSERT(!isEmpty()); return Data::front(); }
-    inline T& last() { Q_ASSERT(!isEmpty()); return Data::back(); }
-    inline const T &last() const { Q_ASSERT(!isEmpty()); return Data::back(); }
-    inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
-    inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
-    inline void pop_front() { Q_ASSERT(!isEmpty()); Data::erase(Data::begin()); }
-    inline typename Data::const_iterator constBegin() const { return Data::cbegin(); }
-    inline typename Data::const_iterator constEnd() const { return Data::cend(); }
-
-    // comfort
-    QStdList<T> &operator+=(const QStdList<T> &l);
-    inline QStdList<T> operator+(const QStdList<T> &l) const { QStdList n = *this; n += l; return n; }
-    inline QStdList<T> &operator+=(const T &t) { append(t); return *this; }
-    inline QStdList<T> &operator<<(const T &t) { append(t); return *this; }
-    inline QStdList<T> &operator<<(const QStdList<T> &l) { *this += l; return *this; }
-
-    typedef typename Data::iterator Iterator;
-    typedef typename Data::const_iterator ConstIterator;
-     // type deduction for e.g. qMin()/qMax()
-    inline int size() const { return Data::size(); }
-     // release memory and set capacity to 0
-    inline void clear() { Data::clear(); Data::shrink_to_fit(); }
-
-#if !defined(Q_NO_USING_KEYWORD)
-    using Data::insert;
-#endif
-};
-
-template <typename T>
-inline void QStdList<T>::append(const QStdList<T> &l)
+inline void QStdVector<T>::append(const QStdVector<T> &l)
 {
     Data::reserve(Data::size() + l.size());
     for (size_t i = 0; i < l.size(); i++) {
@@ -333,40 +259,40 @@ inline void QStdList<T>::append(const QStdList<T> &l)
 }
 
 template <typename T>
-inline void QStdList<T>::removeAt(int i)
+inline void QStdVector<T>::removeAt(int i)
 {
     Data::erase(Data::begin() + i);
 }
 
 template <typename T>
-inline T QStdList<T>::takeAt(int i)
+inline T QStdVector<T>::takeAt(int i)
 {
-    Q_ASSERT_X(i >= 0 && i < Data::size(), "QStdList<T>::take", "index out of range");
+    Q_ASSERT_X(i >= 0 && i < Data::size(), "QStdVector<T>::take", "index out of range");
     T t = Data::at(i); removeAt(i); return t;
 }
 
 template <typename T>
-inline T QStdList<T>::takeFirst()
+inline T QStdVector<T>::takeFirst()
 {
     T t = first(); removeFirst(); return t;
 }
 
 template <typename T>
-inline T QStdList<T>::takeLast()
+inline T QStdVector<T>::takeLast()
 {
     T t = last(); removeLast(); return t;
 }
 
 template <typename T>
-inline void QStdList<T>::move(int from, int to)
+inline void QStdVector<T>::move(int from, int to)
 {
     Q_ASSERT_X(from >= 0 && from < Data::size() && to >= 0 && to < Data::size(),
-               "QStdList<T>::move", "index out of range");
+               "QStdVector<T>::move", "index out of range");
     Data::insert(Data::begin() + to, takeAt(from));
 }
 
 template <typename T>
-int QStdList<T>::removeAll(const T &t)
+int QStdVector<T>::removeAll(const T &t)
 {
     int c = 0;
     typename Data::iterator it = Data::begin();
@@ -382,7 +308,7 @@ int QStdList<T>::removeAll(const T &t)
 }
 
 template <typename T>
-bool QStdList<T>::removeOne(const T &t)
+bool QStdVector<T>::removeOne(const T &t)
 {
     for (size_t i = 0; i < Data::size(); i++) {
         if (Data::at(i) == t) {
@@ -393,122 +319,10 @@ bool QStdList<T>::removeOne(const T &t)
     return false;
 }
 
-// copy from QVector<T>
-template <typename T>
-inline void QStdList<T>::remove(int i, int n)
-{
-    Q_ASSERT_X(i >= 0 && n >= 0 && i + n <= Data::size(), "QStdList<T>::remove", "index out of range");
-    Data::erase(Data::begin() + i, Data::begin() + i + n);
-}
-
-template <typename T>
-inline void QStdList<T>::remove(int i)
-{
-    Q_ASSERT_X(i >= 0 && i < Data::size(), "QStdList<T>::remove", "index out of range");
-    Data::erase(Data::begin() + i, Data::begin() + i + 1);
-}
-
-template <typename T>
-inline void QStdList<T>::replace(int i, const T &t)
-{
-    Q_ASSERT_X(i >= 0 && i < Data::size(), "QStdList<T>::replace", "index out of range");
-    Data::data()[i] = t;
-}
-
-template<typename T>
-inline T QStdList<T>::value(int i) const
-{
-    if (i < 0 || i >= Data::size()) {
-        return T();
-    }
-    return Data::at(i);
-}
-template<typename T>
-inline T QStdList<T>::value(int i, const T &defaultValue) const
-{
-    return ((i < 0 || i >= Data::size()) ? defaultValue :  Data::at(i));
-}
-
-template <typename T>
-inline QStdList<T> &QStdList<T>::operator+=(const QStdList &l)
-{
-    append(l);
-    return *this;
-}
-
-template <typename T>
-int QStdList<T>::indexOf(const T &t, int from) const
-{
-    if (from < 0)
-        from = qMax(from + int(Data::size()), 0);
-    if (from < Data::size()) {
-        for (size_t i = from; i < Data::size(); i++) {
-            if (Data::at(i) == t)
-                return i;
-        }
-    }
-    return -1;
-}
-
-template <typename T>
-int QStdList<T>::lastIndexOf(const T &t, int from) const
-{
-    if (from < 0)
-        from += Data::size();
-    else if (from >= Data::size())
-        from = Data::size() - 1;
-    while(from >= 0) {
-        if (Data::at(from) == t)
-            return from;
-        from--;
-    }
-    return -1;
-}
-
-template <typename T>
-bool QStdList<T>::contains(const T &t) const
-{
-    for (size_t i = 0; i < Data::size(); i++) {
-        if (Data::at(i) == t)
-            return true;
-    }
-    return false;
-}
-
-template <typename T>
-int QStdList<T>::count(const T &t) const
-{
-    int c = 0;
-    for (size_t i = 0; i < Data::size(); i++) {
-        if (Data::at(i) == t)
-            c++;
-    }
-    return c;
-}
-
-template <typename T>
-QStdList<T> QStdList<T>::mid(int pos, int length) const
-{
-    if (length < 0)
-        length = Data::size() - pos;
-    if (pos == 0 && length == Data::size())
-        return *this;
-    if (pos + length > Data::size())
-        length = Data::size() - pos;
-    QStdList<T> copy;
-    copy.reserve(length);
-    for (size_t i = pos; i < pos + length; i++)
-        copy.push_back(Data::at(i));
-    return copy;
-}
-
-Q_DECLARE_SEQUENTIAL_ITERATOR(StdList)
-Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(StdList)
-
 
 template<class T>
 struct SetHasher {
-    size_t operator()(const T& k) const { return qHash(k); }
+    inline size_t operator()(const T &t) const { return qHash(t); }
 };
 
 template <class T>