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

KDECore

ksortablelist.h

Go to the documentation of this file.
00001 /* This file is part of the KDE libraries
00002     Copyright (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Library General Public
00006     License as published by the Free Software Foundation; either
00007     version 2 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KSORTABLELIST_H
00021 #define KSORTABLELIST_H
00022 
00023 #include <kdecore_export.h>
00024 
00025 #include <QtCore/QPair>
00026 #include <QtCore/QList>
00027 
00035 template<typename T, typename Key = int> class KSortableItem : public QPair<Key,T>
00036 {
00037 public:
00043     KSortableItem( Key i, const T& t ) : QPair<Key, T>( i, t ) {}
00048     KSortableItem( const KSortableItem<T, Key> &rhs )
00049         : QPair<Key,T>( rhs.first, rhs.second ) {}
00050 
00054     KSortableItem() {}
00055 
00059     KSortableItem<T, Key> &operator=( const KSortableItem<T, Key>& i ) {
00060         this->first  = i.first;
00061         this->second = i.second;
00062         return *this;
00063     }
00064 
00065     // operators for sorting
00070     bool operator> ( const KSortableItem<T, Key>& i2 ) const {
00071         return (i2.first < this->first);
00072     }
00077     bool operator< ( const KSortableItem<T, Key>& i2 ) const {
00078         return (this->first < i2.first);
00079     }
00084     bool operator>= ( const KSortableItem<T, Key>& i2 ) const {
00085         return (this->first >= i2.first);
00086     }
00091     bool operator<= ( const KSortableItem<T, Key>& i2 ) const {
00092         return !(i2.first < this->first);
00093     }
00098     bool operator== ( const KSortableItem<T, Key>& i2 ) const {
00099         return (this->first == i2.first);
00100     }
00105     bool operator!= ( const KSortableItem<T, Key>& i2 ) const {
00106         return (this->first != i2.first);
00107     }
00108 
00112     T& value() { return this->second; }
00113 
00117     const T& value() const { return this->second; }
00118 
00123     KDE_DEPRECATED Key index() const { return this->first; }
00127     Key key() const { return this->first; }
00128 };
00129 
00130 
00141 template <typename T, typename Key = int>
00142 class KSortableList : public QList<KSortableItem<T, Key> >
00143 {
00144 public:
00150     void insert( Key i, const T& t ) {
00151         QList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) );
00152     }
00153     // add more as you please...
00154 
00159     T& operator[]( Key i ) {
00160         return QList<KSortableItem<T, Key> >::operator[]( i ).value();
00161     }
00162 
00167     const T& operator[]( Key i ) const {
00168         return QList<KSortableItem<T, Key> >::operator[]( i ).value();
00169     }
00170 
00174     void sort() {
00175         qSort( *this );
00176     }
00177 };
00178 
00179 
00180 #ifdef Q_CC_MSVC
00181 template<class T, class K>
00182 inline uint qHash(const KSortableItem<T,K>&) { Q_ASSERT(0); return 0; }
00183 #endif
00184 
00185 
00186 #endif // KSORTABLELIST_H

KDECore

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal