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

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • 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 
35 template<typename T, typename Key = int> class KSortableItem : public QPair<Key,T>
36 {
37 public:
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
126 
129  Key key() const { return this->first; }
130 };
131 
132 
143 template <typename T, typename Key = int>
144 class KSortableList : public QList<KSortableItem<T, Key> >
145 {
146 public:
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
183 template<class T, class K>
184 inline uint qHash(const KSortableItem<T,K>&) { Q_ASSERT(0); return 0; }
185 #endif
186 
187 
188 #endif // KSORTABLELIST_H
KSortableItem::operator>
bool operator>(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:70
KSortableItem
KSortableItem is a QPair that provides several operators for sorting.
Definition: ksortablelist.h:35
T
#define T
kdecore_export.h
KSortableItem::operator=
KSortableItem< T, Key > & operator=(const KSortableItem< T, Key > &i)
Assignment operator, just copies the item.
Definition: ksortablelist.h:59
KSortableItem::KSortableItem
KSortableItem(Key i, const T &t)
Creates a new KSortableItem with the given values.
Definition: ksortablelist.h:43
KSortableList::insert
void insert(Key i, const T &t)
Insert a KSortableItem with the given values.
Definition: ksortablelist.h:152
KSortableItem::key
Key key() const
Definition: ksortablelist.h:129
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
KSortableItem::operator!=
bool operator!=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:105
KSortableItem::index
Key index() const
Definition: ksortablelist.h:124
qHash
uint qHash(const KUrl &kurl)
Definition: kurl.cpp:1913
KSortableList
KSortableList is a QList which associates a key with each item in the list.
Definition: ksortablelist.h:144
QPair
KSortableItem::KSortableItem
KSortableItem(const KSortableItem< T, Key > &rhs)
Creates a new KSortableItem that copies another one.
Definition: ksortablelist.h:48
KSortableItem::operator>=
bool operator>=(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:84
KSortableItem::operator==
bool operator==(const KSortableItem< T, Key > &i2) const
Compares the two items.
Definition: ksortablelist.h:98
KSortableItem::value
const T & value() const
Definition: ksortablelist.h:117
KSortableList::operator[]
const T & operator[](Key i) const
Returns the first value of the KSortableItem at the given position.
Definition: ksortablelist.h:169
KSortableItem::KSortableItem
KSortableItem()
Creates a new KSortableItem with uninitialized values.
Definition: ksortablelist.h:54
QList
Definition: kaboutdata.h:33
KSortableItem::value
T & value()
Definition: ksortablelist.h:112
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:09 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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