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