File indexing completed on 2024-05-05 04:38:47
0001 /* 0002 SPDX-FileCopyrightText: 2015 Kevin Funk <kfunk@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #ifndef KDEVELOP_STACK_H 0008 #define KDEVELOP_STACK_H 0009 0010 #include <QVarLengthArray> 0011 0012 namespace KDevelop { 0013 0014 /** 0015 * @brief Implementation of a stack based on QVarLengthArray 0016 * 0017 * Since stacks are usually short-lived containers, it make sense to optimize their memory usage 0018 * 0019 * Internally using QVarLengthArray. The first @p Prealloc items are placed on the stack. 0020 * If the size of the stack exceeds @p Prealloc, the contents are moved to the heap. 0021 * 0022 * @note Make sure to pass a sensible amount for @p Prealloc; avoiding stack overflows 0023 * 0024 * The default value for Prealloc, 32, 0025 * seems to be a good candidate for between conserving stack space and keeping heap allocations low 0026 * (verified by a few heaptrack runs of duchainify) 0027 * 0028 * @sa QVarLengthArray 0029 */ 0030 template<class T, int Prealloc = 32> 0031 class Stack : public QVarLengthArray<T 0032 , Prealloc> 0033 { 0034 using Base = QVarLengthArray<T, Prealloc>; 0035 0036 public: 0037 using Base::QVarLengthArray; 0038 0039 inline void swap(Stack<T>& other) 0040 { 0041 // prevent Stack<->QVarLengthArray swaps 0042 Base::swap(other); 0043 } 0044 inline void push(const T& t) 0045 { 0046 Base::append(t); 0047 } 0048 0049 inline T pop() 0050 { 0051 T r = Base::last(); 0052 Base::removeLast(); 0053 return r; 0054 } 0055 inline T& top() 0056 { 0057 return Base::last(); 0058 } 0059 inline const T& top() const 0060 { 0061 return Base::last(); 0062 } 0063 }; 0064 0065 } 0066 0067 #endif // KDEVELOP_STACK_H