From 9dff809b452b0b69f5583cd0c131c65991631200 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sat, 8 Apr 2017 21:25:42 +0200 Subject: Array-backed containers: add shrink_to_fit for STL compatibility Side note: QHash has squeeze(), but there's no shrink_to_fit on std::unordered_map. [ChangeLog][QtCore][QByteArray] Added shrink_to_fit(). [ChangeLog][QtCore][QString] Added shrink_to_fit(). [ChangeLog][QtCore][QVarLengthArray] Added shrink_to_fit(). [ChangeLog][QtCore][QVector] Added shrink_to_fit(). Change-Id: Ifd7d28c9bed70727be6308f0191a188201784f61 Reviewed-by: Marc Mutz --- src/corelib/tools/qbytearray.cpp | 7 +++++++ src/corelib/tools/qbytearray.h | 1 + src/corelib/tools/qstring.cpp | 9 +++++++++ src/corelib/tools/qstring.h | 1 + src/corelib/tools/qvarlengtharray.h | 1 + src/corelib/tools/qvarlengtharray.qdoc | 6 ++++++ src/corelib/tools/qvector.h | 1 + src/corelib/tools/qvector.qdoc | 7 +++++++ 8 files changed, 33 insertions(+) (limited to 'src') diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 6be28fea5f..c728a05f55 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -1134,6 +1134,13 @@ static inline char qToLower(char c) Same as prepend(\a ch). */ +/*! \fn void QByteArray::shrink_to_fit() + \since 5.10 + + This function is provided for STL compatibility. It is equivalent to + squeeze(). +*/ + /*! \fn QByteArray::QByteArray(const QByteArray &other) Constructs a copy of \a other. diff --git a/src/corelib/tools/qbytearray.h b/src/corelib/tools/qbytearray.h index 76660d9289..3fc6718abb 100644 --- a/src/corelib/tools/qbytearray.h +++ b/src/corelib/tools/qbytearray.h @@ -423,6 +423,7 @@ public: inline void push_front(char c); inline void push_front(const char *c); inline void push_front(const QByteArray &a); + void shrink_to_fit() { squeeze(); } static inline QByteArray fromStdString(const std::string &s); inline std::string toStdString() const; diff --git a/src/corelib/tools/qstring.cpp b/src/corelib/tools/qstring.cpp index 4a92a7f424..07d6376b85 100644 --- a/src/corelib/tools/qstring.cpp +++ b/src/corelib/tools/qstring.cpp @@ -8469,6 +8469,15 @@ bool QString::isRightToLeft() const Appends the given \a ch character onto the end of this string. */ +/*! \fn void QString::shrink_to_fit() + \since 5.10 + + This function is provided for STL compatibility. It is + equivalent to squeeze(). + + \sa squeeze() +*/ + /*! \fn std::string QString::toStdString() const diff --git a/src/corelib/tools/qstring.h b/src/corelib/tools/qstring.h index 05d411d138..f65331f7e4 100644 --- a/src/corelib/tools/qstring.h +++ b/src/corelib/tools/qstring.h @@ -805,6 +805,7 @@ public: inline void push_back(const QString &s) { append(s); } inline void push_front(QChar c) { prepend(c); } inline void push_front(const QString &s) { prepend(s); } + void shrink_to_fit() { squeeze(); } static inline QString fromStdString(const std::string &s); inline std::string toStdString() const; diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index 9879d55967..90311ac55b 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -231,6 +231,7 @@ public: inline const T &front() const { return first(); } inline T &back() { return last(); } inline const T &back() const { return last(); } + void shrink_to_fit() { squeeze(); } private: void realloc(int size, int alloc); diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index 127afcd069..be2bdeda07 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -190,6 +190,12 @@ \overload */ +/*! \fn void QVarLengthArray::shrink_to_fit() + \since 5.10 + + Same as squeeze(). Provided for STL-compatibility. +*/ + /*! \fn bool QVarLengthArray::isEmpty() const Returns \c true if the array has size 0; otherwise returns \c false. diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index fd124d1d59..121187083b 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -268,6 +268,7 @@ public: inline const_reference front() const { return first(); } inline reference back() { return last(); } inline const_reference back() const { return last(); } + void shrink_to_fit() { squeeze(); } // comfort QVector &operator+=(const QVector &l); diff --git a/src/corelib/tools/qvector.qdoc b/src/corelib/tools/qvector.qdoc index 0ea47b1a1a..61fb3d494d 100644 --- a/src/corelib/tools/qvector.qdoc +++ b/src/corelib/tools/qvector.qdoc @@ -1124,6 +1124,13 @@ \overload */ +/*! \fn void QVector::shrink_to_fit() + \since 5.10 + + This function is provided for STL compatibility. It is equivalent + to squeeze(). +*/ + /*! \fn bool QVector::empty() const This function is provided for STL compatibility. It is equivalent -- cgit v1.2.3