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 #ifndef KDE_NO_DEPRECATED 00124 KDE_DEPRECATED Key index() const { return this->first; } 00125 #endif 00126 00129 Key key() const { return this->first; } 00130 }; 00131 00132 00143 template <typename T, typename Key = int> 00144 class KSortableList : public QList<KSortableItem<T, Key> > 00145 { 00146 public: 00152 void insert( Key i, const T& t ) { 00153 QList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) ); 00154 } 00155 // add more as you please... 00156 00161 T& operator[]( Key i ) { 00162 return QList<KSortableItem<T, Key> >::operator[]( i ).value(); 00163 } 00164 00169 const T& operator[]( Key i ) const { 00170 return QList<KSortableItem<T, Key> >::operator[]( i ).value(); 00171 } 00172 00176 void sort() { 00177 qSort( *this ); 00178 } 00179 }; 00180 00181 00182 #ifdef Q_CC_MSVC 00183 template<class T, class K> 00184 inline uint qHash(const KSortableItem<T,K>&) { Q_ASSERT(0); return 0; } 00185 #endif 00186 00187 00188 #endif // KSORTABLELIST_H
KDE 4.6 API Reference