KCompletion

ksortablelist.h
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 2001 Carsten Pfeiffer <[email protected]>
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  */
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 #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 0)
130  /**
131  * @return the first value (the key)
132  * @deprecated Since 4.0. Use key()
133  */
134  KCOMPLETION_DEPRECATED_VERSION(4, 0, "Use KSortableItem::key()")
135  Key index() const
136  {
137  return this->first;
138  }
139 #endif
140  /**
141  * @return the first value.
142  */
143  Key key() const
144  {
145  return this->first;
146  }
147 };
148 
149 /**
150  * \class KSortableList ksortablelist.h <KSortableList>
151  *
152  * KSortableList is a QList which associates a key with each item in the list.
153  * This key is used for sorting when calling sort().
154  *
155  * This allows to temporarily calculate a key and use it for sorting, without having
156  * to store that key in the items, or calculate that key many times for the same item
157  * during sorting if that calculation is expensive.
158  */
159 template<typename T, typename Key = int>
160 class KSortableList : public QList<KSortableItem<T, Key>>
161 {
162 public:
163  /**
164  * Insert a KSortableItem with the given values.
165  * @param i the first value
166  * @param t the second value
167  */
168  void insert(Key i, const T &t)
169  {
171  }
172  // add more as you please...
173 
174  /**
175  * Returns the first value of the KSortableItem at the given position.
176  * @return the first value of the KSortableItem
177  */
178  T &operator[](Key i)
179  {
180  return QList<KSortableItem<T, Key>>::operator[](i).value();
181  }
182 
183  /**
184  * Returns the first value of the KSortableItem at the given position.
185  * @return the first value of the KSortableItem
186  */
187  const T &operator[](Key i) const
188  {
189  return QList<KSortableItem<T, Key>>::operator[](i).value();
190  }
191 
192  /**
193  * Sorts the KSortableItems.
194  */
195  void sort()
196  {
197  std::sort(this->begin(), this->end());
198  }
199 };
200 
201 #ifdef Q_CC_MSVC
202 template<class T, class K>
203 inline uint qHash(const KSortableItem<T, K> &)
204 {
205  Q_ASSERT(0);
206  return 0;
207 }
208 #endif
209 
210 #endif // KSORTABLELIST_H
void append(const T &value)
bool operator!=(const KSortableItem< T, Key > &i2) const
Compares the two items.
T & operator[](Key i)
Returns the first value of the KSortableItem at the given position.
bool operator>=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:84
const T & operator[](Key i) const
Returns the first value of the KSortableItem at the given position.
bool operator==(const KSortableItem< T, Key > &i2) const
Compares the two items.
bool operator>(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:68
bool operator<=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:92
const T & value() const
KSortableItem(const KSortableItem< T, Key > &rhs)
Creates a new KSortableItem that copies another one.
Definition: ksortablelist.h:41
KSortableItem< T, Key > & operator=(const KSortableItem< T, Key > &i)
Assignment operator, just copies the item.
Definition: ksortablelist.h:56
Key key() const
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
void sort()
Sorts the KSortableItems.
KSortableItem(Key i, const T &t)
Creates a new KSortableItem with the given values.
Definition: ksortablelist.h:33
bool operator<(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:76
void insert(Key i, const T &t)
Insert a KSortableItem with the given values.
KSortableItem()
Creates a new KSortableItem with uninitialized values.
Definition: ksortablelist.h:49
Key index() const
T value(int i) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 04:11:17 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.