1 | /* |
2 | This file is part of the KDE libraries |
3 | SPDX-FileCopyrightText: 2001 Carsten Pfeiffer <pfeiffer@kde.org> |
4 | |
5 | SPDX-License-Identifier: LGPL-2.0-or-later |
6 | */ |
7 | |
8 | #ifndef KSORTABLELIST_H |
9 | #define KSORTABLELIST_H |
10 | |
11 | #include <kcompletion_export.h> |
12 | |
13 | #include <QList> |
14 | #include <QPair> |
15 | #include <algorithm> |
16 | |
17 | /** |
18 | * \class KSortableItem ksortablelist.h <KSortableItem> |
19 | * |
20 | * KSortableItem is a QPair that provides several operators |
21 | * for sorting. |
22 | * @see KSortableList |
23 | */ |
24 | template<typename T, typename Key = int> |
25 | class KSortableItem : public QPair<Key, T> |
26 | { |
27 | public: |
28 | /** |
29 | * Creates a new KSortableItem with the given values. |
30 | * @param i the first value (the key) |
31 | * @param t the second value (the item) |
32 | */ |
33 | KSortableItem(Key i, const T &t) |
34 | : QPair<Key, T>(i, t) |
35 | { |
36 | } |
37 | /** |
38 | * Creates a new KSortableItem that copies another one. |
39 | * @param rhs the other item to copy |
40 | */ |
41 | KSortableItem(const KSortableItem<T, Key> &rhs) |
42 | : QPair<Key, T>(rhs.first, rhs.second) |
43 | { |
44 | } |
45 | |
46 | /** |
47 | * Creates a new KSortableItem with uninitialized values. |
48 | */ |
49 | KSortableItem() |
50 | { |
51 | } |
52 | |
53 | /** |
54 | * Assignment operator, just copies the item. |
55 | */ |
56 | KSortableItem<T, Key> &operator=(const KSortableItem<T, Key> &i) |
57 | { |
58 | this->first = i.first; |
59 | this->second = i.second; |
60 | return *this; |
61 | } |
62 | |
63 | // operators for sorting |
64 | /** |
65 | * Compares the two items. This implementation only compares |
66 | * the first value. |
67 | */ |
68 | bool operator>(const KSortableItem<T, Key> &i2) const |
69 | { |
70 | return (i2.first < this->first); |
71 | } |
72 | /** |
73 | * Compares the two items. This implementation only compares |
74 | * the first value. |
75 | */ |
76 | bool operator<(const KSortableItem<T, Key> &i2) const |
77 | { |
78 | return (this->first < i2.first); |
79 | } |
80 | /** |
81 | * Compares the two items. This implementation only compares |
82 | * the first value. |
83 | */ |
84 | bool operator>=(const KSortableItem<T, Key> &i2) const |
85 | { |
86 | return (this->first >= i2.first); |
87 | } |
88 | /** |
89 | * Compares the two items. This implementation only compares |
90 | * the first value. |
91 | */ |
92 | bool operator<=(const KSortableItem<T, Key> &i2) const |
93 | { |
94 | return !(i2.first < this->first); |
95 | } |
96 | /** |
97 | * Compares the two items. This implementation only compares |
98 | * the first value. |
99 | */ |
100 | bool operator==(const KSortableItem<T, Key> &i2) const |
101 | { |
102 | return (this->first == i2.first); |
103 | } |
104 | /** |
105 | * Compares the two items. This implementation only compares |
106 | * the first value. |
107 | */ |
108 | bool operator!=(const KSortableItem<T, Key> &i2) const |
109 | { |
110 | return (this->first != i2.first); |
111 | } |
112 | |
113 | /** |
114 | * @return the second value (the item) |
115 | */ |
116 | T &value() |
117 | { |
118 | return this->second; |
119 | } |
120 | |
121 | /** |
122 | * @return the second value (the item) |
123 | */ |
124 | const T &value() const |
125 | { |
126 | return this->second; |
127 | } |
128 | |
129 | /** |
130 | * @return the first value. |
131 | */ |
132 | Key key() const |
133 | { |
134 | return this->first; |
135 | } |
136 | }; |
137 | |
138 | /** |
139 | * \class KSortableList ksortablelist.h <KSortableList> |
140 | * |
141 | * KSortableList is a QList which associates a key with each item in the list. |
142 | * This key is used for sorting when calling sort(). |
143 | * |
144 | * This allows to temporarily calculate a key and use it for sorting, without having |
145 | * to store that key in the items, or calculate that key many times for the same item |
146 | * during sorting if that calculation is expensive. |
147 | */ |
148 | template<typename T, typename Key = int> |
149 | class KSortableList : public QList<KSortableItem<T, Key>> |
150 | { |
151 | public: |
152 | /** |
153 | * Insert a KSortableItem with the given values. |
154 | * @param i the first value |
155 | * @param t the second value |
156 | */ |
157 | void insert(Key i, const T &t) |
158 | { |
159 | QList<KSortableItem<T, Key>>::append(KSortableItem<T, Key>(i, t)); |
160 | } |
161 | // add more as you please... |
162 | |
163 | /** |
164 | * Returns the first value of the KSortableItem at the given position. |
165 | * @return the first value of the KSortableItem |
166 | */ |
167 | T &operator[](Key i) |
168 | { |
169 | return QList<KSortableItem<T, Key>>::operator[](i).value(); |
170 | } |
171 | |
172 | /** |
173 | * Returns the first value of the KSortableItem at the given position. |
174 | * @return the first value of the KSortableItem |
175 | */ |
176 | const T &operator[](Key i) const |
177 | { |
178 | return QList<KSortableItem<T, Key>>::operator[](i).value(); |
179 | } |
180 | |
181 | /** |
182 | * Sorts the KSortableItems. |
183 | */ |
184 | void sort() |
185 | { |
186 | std::sort(this->begin(), this->end()); |
187 | } |
188 | }; |
189 | |
190 | #endif // KSORTABLELIST_H |
191 | |