summaryrefslogtreecommitdiff
path: root/libopie2/qt3/opiecore/osortablevaluelist.h
blob: a3f75b45b34fcd7a0fda25a69482015d203114b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
                             This file is part of the Opie Project
                             Originally a part of the KDE Project
                             (C) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
              =.
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       This program is distributed in the hope that
     +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
    : ..    .:,     . . .    without even the implied warranty of
    =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
  _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.
*/

#ifndef OSORTABLEVALUELIST_H
#define OSORTABLEVALUELIST_H

#if QT_VERSION >= 0x030000
#include <qtl.h>
#include <qpair.h>
#else
#include <opie2/otl.h>
#include <opie2/opair.h>
#endif
#include <qvaluelist.h>

template<class T, class Key = int> class OSortableItem : public QPair<Key,T>
{
public:
    OSortableItem( Key i, const T& t ) : QPair<Key, T>( i, t ) {}
    OSortableItem( const OSortableItem<T, Key> &rhs )
        : QPair<Key,T>( rhs.first, rhs.second ) {}

    OSortableItem() {}

    OSortableItem<T, Key> &operator=( const OSortableItem<T, Key>& i ) {
        first  = i.first;
        second = i.second;
        return *this;
    }

    // operators for sorting
    bool operator> ( const OSortableItem<T, Key>& i2 ) const {
        return (i2.first < first);
    }
    bool operator< ( const OSortableItem<T, Key>& i2 ) const {
        return (first < i2.first);
    }
    bool operator>= ( const OSortableItem<T, Key>& i2 ) const {
        return (first >= i2.first);
    }
    bool operator<= ( const OSortableItem<T, Key>& i2 ) const {
        return !(i2.first < first);
    }
    bool operator== ( const OSortableItem<T, Key>& i2 ) const {
        return (first == i2.first);
    }
    bool operator!= ( const OSortableItem<T, Key>& i2 ) const {
        return (first != i2.first);
    }

    T& value() {
        return second;
    }
    const T& value() const {
        return second;
    }

    Key index() const {
        return first;
    }
};


// convenience
template <class T, class Key = int>
class OSortableValueList : public QValueList<OSortableItem<T, Key> >
{
public:
    void insert( Key i, const T& t ) {
        QValueList<OSortableItem<T, Key> >::append( OSortableItem<T, Key>( i, t ) );
    }
    // add more as you please...

    T& operator[]( Key i ) {
        return QValueList<OSortableItem<T, Key> >::operator[]( i ).value();
    }
    const T& operator[]( Key i ) const {
        return QValueList<OSortableItem<T, Key> >::operator[]( i ).value();
    }

    void sort() {
        qHeapSort( *this );
    }
};

// template <class T> class OSortableValueListIterator : public QValueListIterator<OSortableItem<T>  >
// {
// };

#endif // OSORTABLEVALUELIST_H