From 2a2fb306b8cfc3f386cf9432c4a343c213ee46f7 Mon Sep 17 00:00:00 2001 From: Andrei Golubev Date: Fri, 12 Nov 2021 16:31:06 +0100 Subject: QStack: simplify the class (even more) Most methods just do the same thing that is implemented in QList, so just use QList methods The QStack::pop() implementation should no longer go through the resize which is likely slower, expands to more-code (unless optimized away neatly) than erasure of single element through QList API (which we optimized at some point to be fast) It is also meaningless (afair) to have `inline` keyword when a method is both declared and defined within the class' body, so we can drop that as well Change-Id: If9de3429be7418ed0ae13c571e28556a358eab05 Reviewed-by: Thiago Macieira --- src/corelib/tools/qstack.h | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/corelib/tools/qstack.h b/src/corelib/tools/qstack.h index a0e39f8ece..c43c63cdd2 100644 --- a/src/corelib/tools/qstack.h +++ b/src/corelib/tools/qstack.h @@ -49,26 +49,13 @@ class QStack : public QList { public: // compiler-generated special member functions are fine! - inline void swap(QStack &other) noexcept { QList::swap(other); } // prevent QList<->QStack swaps - inline void push(const T &t) { QList::append(t); } - T pop(); - T &top(); - const T &top() const; + void swap(QStack &other) noexcept { QList::swap(other); } // prevent QList<->QStack swaps + void push(const T &t) { QList::append(t); } + T pop() { return QList::takeLast(); } + T &top() { return QList::last(); } + const T &top() const { return QList::last(); } }; -template -inline T QStack::pop() -{ Q_ASSERT(!this->isEmpty()); T t = this->data()[this->size() -1]; - this->resize(this->size()-1); return t; } - -template -inline T &QStack::top() -{ Q_ASSERT(!this->isEmpty()); this->detach(); return this->data()[this->size()-1]; } - -template -inline const T &QStack::top() const -{ Q_ASSERT(!this->isEmpty()); return this->data()[this->size()-1]; } - QT_END_NAMESPACE #endif // QSTACK_H -- cgit v1.2.3