ksortablevaluelist.h
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 KSORTABLEVALUELIST_H 00021 #define KSORTABLEVALUELIST_H 00022 00023 #include <tqpair.h> 00024 #include <tqvaluelist.h> 00025 #include "kdelibs_export.h" 00026 00032 template<class T, class Key = int> class KSortableItem : public TQPair<Key,T> 00033 { 00034 public: 00040 KSortableItem( Key i, const T& t ) : TQPair<Key, T>( i, t ) {} 00045 KSortableItem( const KSortableItem<T, Key> &rhs ) 00046 : TQPair<Key,T>( rhs.first, rhs.second ) {} 00047 00051 KSortableItem() {} 00052 00056 KSortableItem<T, Key> &operator=( const KSortableItem<T, Key>& i ) { 00057 this->first = i.first; 00058 this->second = i.second; 00059 return *this; 00060 } 00061 00062 // operators for sorting 00067 bool operator> ( const KSortableItem<T, Key>& i2 ) const { 00068 return (i2.first < this->first); 00069 } 00074 bool operator< ( const KSortableItem<T, Key>& i2 ) const { 00075 return (this->first < i2.first); 00076 } 00081 bool operator>= ( const KSortableItem<T, Key>& i2 ) const { 00082 return (this->first >= i2.first); 00083 } 00088 bool operator<= ( const KSortableItem<T, Key>& i2 ) const { 00089 return !(i2.first < this->first); 00090 } 00095 bool operator== ( const KSortableItem<T, Key>& i2 ) const { 00096 return (this->first == i2.first); 00097 } 00102 bool operator!= ( const KSortableItem<T, Key>& i2 ) const { 00103 return (this->first != i2.first); 00104 } 00105 00109 T& value() { return this->second; } 00110 00114 const T& value() const { return this->second; } 00115 00119 Key index() const { return this->first; } 00120 }; 00121 00122 00129 template <class T, class Key = int> 00130 class KSortableValueList : public TQValueList<KSortableItem<T, Key> > 00131 { 00132 public: 00138 void insert( Key i, const T& t ) { 00139 TQValueList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) ); 00140 } 00141 // add more as you please... 00142 00147 T& operator[]( Key i ) { 00148 return TQValueList<KSortableItem<T, Key> >::operator[]( i ).value(); 00149 } 00150 00155 const T& operator[]( Key i ) const { 00156 return TQValueList<KSortableItem<T, Key> >::operator[]( i ).value(); 00157 } 00158 00162 void sort() { 00163 qHeapSort( *this ); 00164 } 00165 }; 00166 00167 // template <class T> class KSortableValueListIterator : public TQValueListIterator<KSortableItem<T> > 00168 // { 00169 // }; 00170 00171 #endif // KSORTABLEVALUELIST_H