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         first  = i.first;
00057         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 < first);
00068     }
00073     bool operator< ( const KSortableItem<T, Key>& i2 ) const {
00074         return (first < i2.first);
00075     }
00080     bool operator>= ( const KSortableItem<T, Key>& i2 ) const {
00081         return (first >= i2.first);
00082     }
00087     bool operator<= ( const KSortableItem<T, Key>& i2 ) const {
00088         return !(i2.first < first);
00089     }
00094     bool operator== ( const KSortableItem<T, Key>& i2 ) const {
00095         return (first == i2.first);
00096     }
00101     bool operator!= ( const KSortableItem<T, Key>& i2 ) const {
00102         return (first != i2.first);
00103     }
00104 
00109     T& value() {
00110         return second;
00111     }
00112 
00116     const T& value() const {
00117         return second;
00118     }
00119 
00124     Key index() const {
00125         return first;
00126     }
00127 };
00128 
00129 
00136 template <class T, class Key = int>
00137 class KSortableValueList : public QValueList<KSortableItem<T, Key> >
00138 {
00139 public:
00145     void insert( Key i, const T& t ) {
00146         QValueList<KSortableItem<T, Key> >::append( KSortableItem<T, Key>( i, t ) );
00147     }
00148     // add more as you please...
00149 
00154     T& operator[]( Key i ) {
00155         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00156     }
00157 
00162     const T& operator[]( Key i ) const {
00163         return QValueList<KSortableItem<T, Key> >::operator[]( i ).value();
00164     }
00165 
00169     void sort() {
00170         qHeapSort( *this );
00171     }
00172 };
00173 
00174 // template <class T> class KSortableValueListIterator : public QValueListIterator<KSortableItem<T>  >
00175 // {
00176 // };
00177 
00178 #endif // KSORTABLEVALUELIST_H
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.0.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Oct 8 12:20:42 2003 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2001