File indexing completed on 2024-05-05 04:38:46
0001 /* 0002 SPDX-FileCopyrightText: 2009 David Nolden <david.nolden.kdevelop@art-master.de> 0003 SPDX-FileCopyrightText: 2011 Milian Wolff <mail@milianw.de> 0004 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 #ifndef KDEVPLATFORM_KDEVVARLENGTHARRAY_H 0009 #define KDEVPLATFORM_KDEVVARLENGTHARRAY_H 0010 0011 #include <QVarLengthArray> 0012 #include <QVector> 0013 0014 /** 0015 * Extended QVarLengthArray with additional convenience API. 0016 */ 0017 template<class T, int Prealloc = 256> 0018 class KDevVarLengthArray : public QVarLengthArray<T 0019 , Prealloc> 0020 { 0021 using Base = QVarLengthArray<T, Prealloc>; 0022 0023 public: 0024 using Base::QVarLengthArray; 0025 0026 ///Removes exactly one occurrence of the given value from the array. Returns false if none was found. 0027 inline bool removeOne(const T& value); 0028 0029 /// @return QList of items in this array 0030 QList<T> toList() const; 0031 0032 /// @return QVector of items in this array 0033 QVector<T> toVector() const; 0034 }; 0035 0036 template<class T, int Prealloc> 0037 Q_INLINE_TEMPLATE bool KDevVarLengthArray<T, Prealloc>::removeOne(const T& value) 0038 { 0039 const int idx = Base::indexOf(value); 0040 if (idx == -1) { 0041 return false; 0042 } 0043 Base::remove(idx); 0044 return true; 0045 } 0046 0047 template<class T, int Prealloc> 0048 Q_OUTOFLINE_TEMPLATE QList<T> KDevVarLengthArray<T, Prealloc>::toList() const 0049 { 0050 QList<T> ret; 0051 ret.reserve(Base::size()); 0052 const T* const end = Base::constEnd(); 0053 for (const T* it = Base::constBegin(); it != end; ++it) { 0054 ret << *it; 0055 } 0056 0057 return ret; 0058 } 0059 0060 template<class T, int Prealloc> 0061 Q_OUTOFLINE_TEMPLATE QVector<T> KDevVarLengthArray<T, Prealloc>::toVector() const 0062 { 0063 QVector<T> ret; 0064 ret.reserve(Base::size()); 0065 const T* const end = Base::constEnd(); 0066 for (const T* it = Base::constBegin(); it != end; ++it) { 0067 ret << *it; 0068 } 0069 0070 return ret; 0071 } 0072 0073 #endif // KDEVPLATFORM_KDEVVARLENGTHARRAY_H