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 <QPair>
14 #include <QList>
15 #include <algorithm>
16 
24 template<typename T, typename Key = int> class KSortableItem : public QPair<Key, T>
25 {
26 public:
32  KSortableItem(Key i, const T &t) : QPair<Key, T>(i, t) {}
38  : QPair<Key, T>(rhs.first, rhs.second) {}
39 
44 
49  {
50  this->first = i.first;
51  this->second = i.second;
52  return *this;
53  }
54 
55  // operators for sorting
60  bool operator> (const KSortableItem<T, Key> &i2) const
61  {
62  return (i2.first < this->first);
63  }
68  bool operator< (const KSortableItem<T, Key> &i2) const
69  {
70  return (this->first < i2.first);
71  }
76  bool operator>= (const KSortableItem<T, Key> &i2) const
77  {
78  return (this->first >= i2.first);
79  }
84  bool operator<= (const KSortableItem<T, Key> &i2) const
85  {
86  return !(i2.first < this->first);
87  }
92  bool operator== (const KSortableItem<T, Key> &i2) const
93  {
94  return (this->first == i2.first);
95  }
100  bool operator!= (const KSortableItem<T, Key> &i2) const
101  {
102  return (this->first != i2.first);
103  }
104 
108  T &value()
109  {
110  return this->second;
111  }
112 
116  const T &value() const
117  {
118  return this->second;
119  }
120 
121 #if KCOMPLETION_ENABLE_DEPRECATED_SINCE(4, 0)
122 
126  KCOMPLETION_DEPRECATED_VERSION(4, 0, "Use KSortableItem::key()")
127  Key index() const
128  {
129  return this->first;
130  }
131 #endif
132 
135  Key key() const
136  {
137  return this->first;
138  }
139 };
140 
151 template <typename T, typename Key = int>
152 class KSortableList : public QList<KSortableItem<T, Key> >
153 {
154 public:
160  void insert(Key i, const T &t)
161  {
163  }
164  // add more as you please...
165 
170  T &operator[](Key i)
171  {
172  return QList<KSortableItem<T, Key> >::operator[](i).value();
173  }
174 
179  const T &operator[](Key i) const
180  {
181  return QList<KSortableItem<T, Key> >::operator[](i).value();
182  }
183 
187  void sort()
188  {
189  std::sort(this->begin(), this->end());
190  }
191 };
192 
193 #ifdef Q_CC_MSVC
194 template<class T, class K>
195 inline uint qHash(const KSortableItem<T, K> &)
196 {
197  Q_ASSERT(0);
198  return 0;
199 }
200 #endif
201 
202 #endif // KSORTABLELIST_H
bool operator>(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:60
KSortableItem is a QPair that provides several operators for sorting.
Definition: ksortablelist.h:24
KSortableItem< T, Key > & operator=(const KSortableItem< T, Key > &i)
Assignment operator, just copies the item.
Definition: ksortablelist.h:48
KSortableItem(Key i, const T &t)
Creates a new KSortableItem with the given values.
Definition: ksortablelist.h:32
void insert(Key i, const T &t)
Insert a KSortableItem with the given values.
Key key() const
void sort()
Sorts the KSortableItems.
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.
Key index() const
KCALENDARCORE_EXPORT uint qHash(const KCalendarCore::Period &key)
KSortableList is a QList which associates a key with each item in the list.
KSortableItem(const KSortableItem< T, Key > &rhs)
Creates a new KSortableItem that copies another one.
Definition: ksortablelist.h:37
bool operator>=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:76
bool operator==(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:92
const T & value() const
const T & operator[](Key i) const
Returns the first value of the KSortableItem at the given position.
KSortableItem()
Creates a new KSortableItem with uninitialized values.
Definition: ksortablelist.h:43
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 22:55:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.