• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • util
ksortablelist.h
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 Copyright (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#ifndef KSORTABLELIST_H
21#define KSORTABLELIST_H
22
23#include <kdecore_export.h>
24
25#include <QtCore/QPair>
26#include <QtCore/QList>
27
35template<typename T, typename Key = int> class KSortableItem : public QPair<Key,T>
36{
37public:
43 KSortableItem( Key i, const T& t ) : QPair<Key, T>( i, t ) {}
48 KSortableItem( const KSortableItem<T, Key> &rhs )
49 : QPair<Key,T>( rhs.first, rhs.second ) {}
50
54 KSortableItem() {}
55
59 KSortableItem<T, Key> &operator=( const KSortableItem<T, Key>& i ) {
60 this->first = i.first;
61 this->second = i.second;
62 return *this;
63 }
64
65 // operators for sorting
70 bool operator> ( const KSortableItem<T, Key>& i2 ) const {
71 return (i2.first < this->first);
72 }
77 bool operator< ( const KSortableItem<T, Key>& i2 ) const {
78 return (this->first < i2.first);
79 }
84 bool operator>= ( const KSortableItem<T, Key>& i2 ) const {
85 return (this->first >= i2.first);
86 }
91 bool operator<= ( const KSortableItem<T, Key>& i2 ) const {
92 return !(i2.first < this->first);
93 }
98 bool operator== ( const KSortableItem<T, Key>& i2 ) const {
99 return (this->first == i2.first);
100 }
105 bool operator!= ( const KSortableItem<T, Key>& i2 ) const {
106 return (this->first != i2.first);
107 }
108
112 T& value() { return this->second; }
113
117 const T& value() const { return this->second; }
118
123#ifndef KDE_NO_DEPRECATED
124 KDE_DEPRECATED Key index() const { return this->first; }
125#endif
129 Key key() const { return this->first; }
130};
131
132
143template <typename T, typename Key = int>
144class KSortableList : public QList<KSortableItem<T, Key> >
145{
146public:
152 void insert( Key i, const T& t ) {
153 QList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) );
154 }
155 // add more as you please...
156
161 T& operator[]( Key i ) {
162 return QList<KSortableItem<T, Key> >::operator[]( i ).value();
163 }
164
169 const T& operator[]( Key i ) const {
170 return QList<KSortableItem<T, Key> >::operator[]( i ).value();
171 }
172
176 void sort() {
177 qSort( *this );
178 }
179};
180
181
182#ifdef Q_CC_MSVC
183template<class T, class K>
184inline uint qHash(const KSortableItem<T,K>&) { Q_ASSERT(0); return 0; }
185#endif
186
187
188#endif // KSORTABLELIST_H
qHash
uint qHash(const KConfigIniBackend::BufferFragment &fragment)
Definition: bufferfragment_p.h:186
KSortableItem
KSortableItem is a QPair that provides several operators for sorting.
Definition: ksortablelist.h:36
KSortableItem::operator=
KSortableItem< T, Key > & operator=(const KSortableItem< T, Key > &i)
Assignment operator, just copies the item.
Definition: ksortablelist.h:59
KSortableItem::key
Key key() const
Definition: ksortablelist.h:129
KSortableItem::KSortableItem
KSortableItem(const KSortableItem< T, Key > &rhs)
Creates a new KSortableItem that copies another one.
Definition: ksortablelist.h:48
KSortableItem::index
Key index() const
Definition: ksortablelist.h:124
KSortableItem::value
const T & value() const
Definition: ksortablelist.h:117
KSortableItem::KSortableItem
KSortableItem(Key i, const T &t)
Creates a new KSortableItem with the given values.
Definition: ksortablelist.h:43
KSortableItem::value
T & value()
Definition: ksortablelist.h:112
KSortableItem::KSortableItem
KSortableItem()
Creates a new KSortableItem with uninitialized values.
Definition: ksortablelist.h:54
KSortableList
KSortableList is a QList which associates a key with each item in the list.
Definition: ksortablelist.h:145
KSortableList::operator[]
const T & operator[](Key i) const
Returns the first value of the KSortableItem at the given position.
Definition: ksortablelist.h:169
KSortableList::sort
void sort()
Sorts the KSortableItems.
Definition: ksortablelist.h:176
KSortableList::operator[]
T & operator[](Key i)
Returns the first value of the KSortableItem at the given position.
Definition: ksortablelist.h:161
KSortableList::insert
void insert(Key i, const T &t)
Insert a KSortableItem with the given values.
Definition: ksortablelist.h:152
QList
Definition: kaboutdata.h:33
QPair
operator==
bool operator==(const KEntry &k1, const KEntry &k2)
Definition: kconfigdata.h:72
operator<
bool operator<(const KEntryKey &k1, const KEntryKey &k2)
Compares two KEntryKeys (needed for QMap).
Definition: kconfigdata.h:124
operator!=
bool operator!=(const KEntry &k1, const KEntry &k2)
Definition: kconfigdata.h:79
kdecore_export.h
T
#define T
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal