kdecore Library API Documentation

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., 59 Temple Place - Suite 330,
00017     Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #ifndef KSORTABLEVALUELIST_H
00021 #define KSORTABLEVALUELIST_H
00022 
00023 #include <qpair.h>
00024 #include <qvaluelist.h>
00025 
00031 template<class T, class Key = int> class KSortableItem : public QPair<Key,T>
00032 {
00033 public:
00039     KSortableItem( Key i, const T& t ) : QPair<Key, T>( i, t ) {}
00044     KSortableItem( const KSortableItem<T, Key> &rhs ) 
00045         : QPair<Key,T>( rhs.first, rhs.second ) {}
00046 
00050     KSortableItem() {}
00051 
00055     KSortableItem<T, Key> &operator=( const KSortableItem<T, Key>& i ) {
00056         this->first  = i.first;
00057         this->second = i.second;
00058         return *this;
00059     }
00060 
00061     // operators for sorting
00066     bool operator> ( const KSortableItem<T, Key>& i2 ) const {
00067         return (i2.first < this->first);
00068     }
00073     bool operator< ( const KSortableItem<T, Key>& i2 ) const {
00074         return (this->first < i2.first);
00075     }
00080     bool operator>= ( const KSortableItem<T, Key>& i2 ) const {
00081         return (this->first >= i2.first);
00082     }
00087     bool operator<= ( const KSortableItem<T, Key>& i2 ) const {
00088         return !(i2.first < this->first);
00089     }
00094     bool operator== ( const KSortableItem<T, Key>& i2 ) const {
00095         return (this->first == i2.first);
00096     }
00101     bool operator!= ( const KSortableItem<T, Key>& i2 ) const {
00102         return (this->first != i2.first);
00103     }
00104 
00109     T& value() { return this->second; }
00110 
00114     const T& value() const { return this->second; }
00115 
00120     Key index() const { return this->first; }
00121 };
00122 
00123 
00130 template <class T, class Key = int>
00131 class KSortableValueList : public QValueList<KSortableItem<T, Key> >
00132 {
00133 public:
00139     void insert( Key i, const T& t ) {
00140         QValueList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) );
00141     }
00142     // add more as you please...
00143 
00148     T& operator[]( Key i ) {
00149         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00150     }
00151 
00156     const T& operator[]( Key i ) const {
00157         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00158     }
00159 
00163     void sort() {
00164         qHeapSort( *this );
00165     }
00166 };
00167 
00168 // template <class T> class KSortableValueListIterator : public QValueListIterator<KSortableItem<T>  >
00169 // {
00170 // };
00171 
00172 #endif // KSORTABLEVALUELIST_H
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:56 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001