KCompletion

ksortablelist.h
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 */
24template<typename T, typename Key = int>
25class KSortableItem : public QPair<Key, T>
26{
27public:
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 */
42 : QPair<Key, T>(rhs.first, rhs.second)
43 {
44 }
45
46 /**
47 * Creates a new KSortableItem with uninitialized values.
48 */
50 {
51 }
52
53 /**
54 * Assignment operator, just copies the item.
55 */
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 */
148template<typename T, typename Key = int>
149class KSortableList : public QList<KSortableItem<T, Key>>
150{
151public:
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 {
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
KSortableItem is a QPair that provides several operators for sorting.
KSortableItem< T, Key > & operator=(const KSortableItem< T, Key > &i)
Assignment operator, just copies the item.
bool operator!=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Key key() const
KSortableItem(const KSortableItem< T, Key > &rhs)
Creates a new KSortableItem that copies another one.
bool operator>(const KSortableItem< T, Key > &i2) const
Compares the two items.
bool operator<=(const KSortableItem< T, Key > &i2) const
Compares the two items.
const T & value() const
KSortableItem(Key i, const T &t)
Creates a new KSortableItem with the given values.
bool operator==(const KSortableItem< T, Key > &i2) const
Compares the two items.
KSortableItem()
Creates a new KSortableItem with uninitialized values.
bool operator<(const KSortableItem< T, Key > &i2) const
Compares the two items.
bool operator>=(const KSortableItem< T, Key > &i2) const
Compares the two items.
KSortableList is a QList which associates a key with each item in the list.
const T & operator[](Key i) const
Returns the first value of the KSortableItem at the given position.
void sort()
Sorts the KSortableItems.
T & operator[](Key i)
Returns the first value of the KSortableItem at the given position.
void insert(Key i, const T &t)
Insert a KSortableItem with the given values.
void append(QList< T > &&value)
T value(qsizetype i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:12:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.