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 
00108     T& value() { return this->second; }
00109 
00113     const T& value() const { return this->second; }
00114 
00118     Key index() const { return this->first; }
00119 };
00120 
00121 
00128 template <class T, class Key = int>
00129 class KSortableValueList : public QValueList<KSortableItem<T, Key> >
00130 {
00131 public:
00137     void insert( Key i, const T& t ) {
00138         QValueList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) );
00139     }
00140     // add more as you please...
00141 
00146     T& operator[]( Key i ) {
00147         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00148     }
00149 
00154     const T& operator[]( Key i ) const {
00155         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00156     }
00157 
00161     void sort() {
00162         qHeapSort( *this );
00163     }
00164 };
00165 
00166 // template <class T> class KSortableValueListIterator : public QValueListIterator<KSortableItem<T>  >
00167 // {
00168 // };
00169 
00170 #endif // KSORTABLEVALUELIST_H
KDE Logo
This file is part of the documentation for kdecore Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Jan 15 13:32:31 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003