File indexing completed on 2024-05-12 11:47:11
0001 /* 0002 This file is part of the KDE libraries 0003 SPDX-FileCopyrightText: 2001 Carsten Pfeiffer <pfeiffer@kde.org> 0004 0005 SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #ifndef KSORTABLELIST_H 0009 #define KSORTABLELIST_H 0010 0011 #include <kcompletion_export.h> 0012 0013 #include <QList> 0014 #include <QPair> 0015 #include <algorithm> 0016 0017 /** 0018 * \class KSortableItem ksortablelist.h <KSortableItem> 0019 * 0020 * KSortableItem is a QPair that provides several operators 0021 * for sorting. 0022 * @see KSortableList 0023 */ 0024 template<typename T, typename Key = int> 0025 class KSortableItem : public QPair<Key, T> 0026 { 0027 public: 0028 /** 0029 * Creates a new KSortableItem with the given values. 0030 * @param i the first value (the key) 0031 * @param t the second value (the item) 0032 */ 0033 KSortableItem(Key i, const T &t) 0034 : QPair<Key, T>(i, t) 0035 { 0036 } 0037 /** 0038 * Creates a new KSortableItem that copies another one. 0039 * @param rhs the other item to copy 0040 */ 0041 KSortableItem(const KSortableItem<T, Key> &rhs) 0042 : QPair<Key, T>(rhs.first, rhs.second) 0043 { 0044 } 0045 0046 /** 0047 * Creates a new KSortableItem with uninitialized values. 0048 */ 0049 KSortableItem() 0050 { 0051 } 0052 0053 /** 0054 * Assignment operator, just copies the item. 0055 */ 0056 KSortableItem<T, Key> &operator=(const KSortableItem<T, Key> &i) 0057 { 0058 this->first = i.first; 0059 this->second = i.second; 0060 return *this; 0061 } 0062 0063 // operators for sorting 0064 /** 0065 * Compares the two items. This implementation only compares 0066 * the first value. 0067 */ 0068 bool operator>(const KSortableItem<T, Key> &i2) const 0069 { 0070 return (i2.first < this->first); 0071 } 0072 /** 0073 * Compares the two items. This implementation only compares 0074 * the first value. 0075 */ 0076 bool operator<(const KSortableItem<T, Key> &i2) const 0077 { 0078 return (this->first < i2.first); 0079 } 0080 /** 0081 * Compares the two items. This implementation only compares 0082 * the first value. 0083 */ 0084 bool operator>=(const KSortableItem<T, Key> &i2) const 0085 { 0086 return (this->first >= i2.first); 0087 } 0088 /** 0089 * Compares the two items. This implementation only compares 0090 * the first value. 0091 */ 0092 bool operator<=(const KSortableItem<T, Key> &i2) const 0093 { 0094 return !(i2.first < this->first); 0095 } 0096 /** 0097 * Compares the two items. This implementation only compares 0098 * the first value. 0099 */ 0100 bool operator==(const KSortableItem<T, Key> &i2) const 0101 { 0102 return (this->first == i2.first); 0103 } 0104 /** 0105 * Compares the two items. This implementation only compares 0106 * the first value. 0107 */ 0108 bool operator!=(const KSortableItem<T, Key> &i2) const 0109 { 0110 return (this->first != i2.first); 0111 } 0112 0113 /** 0114 * @return the second value (the item) 0115 */ 0116 T &value() 0117 { 0118 return this->second; 0119 } 0120 0121 /** 0122 * @return the second value (the item) 0123 */ 0124 const T &value() const 0125 { 0126 return this->second; 0127 } 0128 0129 #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 0) 0130 /** 0131 * @return the first value (the key) 0132 * @deprecated Since 4.0. Use key() 0133 */ 0134 KCOMPLETION_DEPRECATED_VERSION(4, 0, "Use KSortableItem::key()") 0135 Key index() const 0136 { 0137 return this->first; 0138 } 0139 #endif 0140 /** 0141 * @return the first value. 0142 */ 0143 Key key() const 0144 { 0145 return this->first; 0146 } 0147 }; 0148 0149 /** 0150 * \class KSortableList ksortablelist.h <KSortableList> 0151 * 0152 * KSortableList is a QList which associates a key with each item in the list. 0153 * This key is used for sorting when calling sort(). 0154 * 0155 * This allows to temporarily calculate a key and use it for sorting, without having 0156 * to store that key in the items, or calculate that key many times for the same item 0157 * during sorting if that calculation is expensive. 0158 */ 0159 template<typename T, typename Key = int> 0160 class KSortableList : public QList<KSortableItem<T, Key>> 0161 { 0162 public: 0163 /** 0164 * Insert a KSortableItem with the given values. 0165 * @param i the first value 0166 * @param t the second value 0167 */ 0168 void insert(Key i, const T &t) 0169 { 0170 QList<KSortableItem<T, Key>>::append(KSortableItem<T, Key>(i, t)); 0171 } 0172 // add more as you please... 0173 0174 /** 0175 * Returns the first value of the KSortableItem at the given position. 0176 * @return the first value of the KSortableItem 0177 */ 0178 T &operator[](Key i) 0179 { 0180 return QList<KSortableItem<T, Key>>::operator[](i).value(); 0181 } 0182 0183 /** 0184 * Returns the first value of the KSortableItem at the given position. 0185 * @return the first value of the KSortableItem 0186 */ 0187 const T &operator[](Key i) const 0188 { 0189 return QList<KSortableItem<T, Key>>::operator[](i).value(); 0190 } 0191 0192 /** 0193 * Sorts the KSortableItems. 0194 */ 0195 void sort() 0196 { 0197 std::sort(this->begin(), this->end()); 0198 } 0199 }; 0200 0201 #ifdef Q_CC_MSVC 0202 template<class T, class K> 0203 inline uint qHash(const KSortableItem<T, K> &) 0204 { 0205 Q_ASSERT(0); 0206 return 0; 0207 } 0208 #endif 0209 0210 #endif // KSORTABLELIST_H