Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

osortablevaluelist.h

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Originally a part of the KDE Project
00004                              (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022  :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #ifndef OSORTABLEVALUELIST_H
00032 #define OSORTABLEVALUELIST_H
00033 
00034 #if QT_VERSION >= 0x030000
00035 #include <qtl.h>
00036 #include <qpair.h>
00037 #else
00038 #include <opie2/otl.h>
00039 #include <opie2/opair.h>
00040 #endif
00041 #include <qvaluelist.h>
00042 
00043 template<class T, class Key = int> class OSortableItem : public QPair<Key,T>
00044 {
00045 public:
00046     OSortableItem( Key i, const T& t ) : QPair<Key, T>( i, t ) {}
00047     OSortableItem( const OSortableItem<T, Key> &rhs )
00048         : QPair<Key,T>( rhs.first, rhs.second ) {}
00049 
00050     OSortableItem() {}
00051 
00052     OSortableItem<T, Key> &operator=( const OSortableItem<T, Key>& i ) {
00053         first  = i.first;
00054         second = i.second;
00055         return *this;
00056     }
00057 
00058     // operators for sorting
00059     bool operator> ( const OSortableItem<T, Key>& i2 ) const {
00060         return (i2.first < first);
00061     }
00062     bool operator< ( const OSortableItem<T, Key>& i2 ) const {
00063         return (first < i2.first);
00064     }
00065     bool operator>= ( const OSortableItem<T, Key>& i2 ) const {
00066         return (first >= i2.first);
00067     }
00068     bool operator<= ( const OSortableItem<T, Key>& i2 ) const {
00069         return !(i2.first < first);
00070     }
00071     bool operator== ( const OSortableItem<T, Key>& i2 ) const {
00072         return (first == i2.first);
00073     }
00074     bool operator!= ( const OSortableItem<T, Key>& i2 ) const {
00075         return (first != i2.first);
00076     }
00077 
00078     T& value() {
00079         return second;
00080     }
00081     const T& value() const {
00082         return second;
00083     }
00084 
00085     Key index() const {
00086         return first;
00087     }
00088 };
00089 
00090 
00091 // convenience
00092 template <class T, class Key = int>
00093 class OSortableValueList : public QValueList<OSortableItem<T, Key> >
00094 {
00095 public:
00096     void insert( Key i, const T& t ) {
00097         QValueList<OSortableItem<T, Key> >::append( OSortableItem<T, Key>( i, t ) );
00098     }
00099     // add more as you please...
00100 
00101     T& operator[]( Key i ) {
00102         return QValueList<OSortableItem<T, Key> >::operator[]( i ).value();
00103     }
00104     const T& operator[]( Key i ) const {
00105         return QValueList<OSortableItem<T, Key> >::operator[]( i ).value();
00106     }
00107 
00108     void sort() {
00109         qHeapSort( *this );
00110     }
00111 };
00112 
00113 // template <class T> class OSortableValueListIterator : public QValueListIterator<OSortableItem<T>  >
00114 // {
00115 // };
00116 
00117 #endif // OSORTABLEVALUELIST_H

Generated on Sat Nov 5 16:16:28 2005 for OPIE by  doxygen 1.4.2