• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.5 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 
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Thu Sep 25 2014 04:18:44 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdelibs-4.11.5 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