author | zecke <zecke> | 2004-11-18 21:46:17 (UTC) |
---|---|---|
committer | zecke <zecke> | 2004-11-18 21:46:17 (UTC) |
commit | 41fa1c196965f17f9748f835d90c307b7e594883 (patch) (unidiff) | |
tree | 1ea8a2cfe5bfb71f5c9456c7216dc5ff035dd811 /libopie2/opiepim/private/opimsortvector.h | |
parent | 7484344ff5be1f7c54e51715776d0e3cadeb1ed0 (diff) | |
download | opie-41fa1c196965f17f9748f835d90c307b7e594883.zip opie-41fa1c196965f17f9748f835d90c307b7e594883.tar.gz opie-41fa1c196965f17f9748f835d90c307b7e594883.tar.bz2 |
Add generic sorting implementation based on QVector
for Contact and Todo
Diffstat (limited to 'libopie2/opiepim/private/opimsortvector.h') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/private/opimsortvector.h | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/libopie2/opiepim/private/opimsortvector.h b/libopie2/opiepim/private/opimsortvector.h new file mode 100644 index 0000000..6c21339 --- a/dev/null +++ b/libopie2/opiepim/private/opimsortvector.h | |||
@@ -0,0 +1,138 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) 2004 Holger Freyther <freyther@handhelds.org> | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
29 | |||
30 | #ifndef OPIE_PIM_SORT_VECTOR_H | ||
31 | #define OPIE_PIM_SORT_VECTOR_H | ||
32 | |||
33 | #include <opie2/opimglobal.h> | ||
34 | |||
35 | #include <qvector.h> | ||
36 | |||
37 | namespace Opie { | ||
38 | namespace Internal { | ||
39 | template<class T> | ||
40 | struct OPimSortVectorContainer { | ||
41 | T item; | ||
42 | }; | ||
43 | |||
44 | template<class T> | ||
45 | class OPimSortVector : public QVector<OPimSortVectorContainer<T> > { | ||
46 | typedef OPimSortVectorContainer<T> VectorItem; | ||
47 | public: | ||
48 | OPimSortVector( uint size, bool asc, int sort ); | ||
49 | int compareItems( QCollection::Item d1, QCollection::Item d2 ); | ||
50 | bool insert( uint, const T& t ); | ||
51 | UID uidAt( uint i )const; | ||
52 | |||
53 | protected: | ||
54 | int testString( const QString&, const QString& )const; | ||
55 | int testDate( const QDate&, const QDate& )const; | ||
56 | protected: | ||
57 | bool sortAscending()const; | ||
58 | int sortOrder()const; | ||
59 | |||
60 | private: | ||
61 | bool m_ascending : 1; | ||
62 | int m_sort; | ||
63 | virtual int compareItems( const T& item1, const T& item2 ) = 0; | ||
64 | }; | ||
65 | |||
66 | template<class T> | ||
67 | OPimSortVector<T>::OPimSortVector( uint size, bool asc, int sort ) | ||
68 | : QVector<VectorItem>( size ), m_ascending( asc ), | ||
69 | m_sort( sort ) { | ||
70 | this->setAutoDelete( true ); | ||
71 | } | ||
72 | |||
73 | /** | ||
74 | * Returns: | ||
75 | * 0 if item1 == item2 | ||
76 | * | ||
77 | * non-zero if item1 != item2 | ||
78 | * | ||
79 | * This function returns int rather than bool so that reimplementations | ||
80 | * can return one of three values and use it to sort by: | ||
81 | * | ||
82 | * 0 if item1 == item2 | ||
83 | * | ||
84 | * > 0 (positive integer) if item1 > item2 | ||
85 | * | ||
86 | * < 0 (negative integer) if item1 < item2 | ||
87 | * | ||
88 | */ | ||
89 | template<class T> | ||
90 | int OPimSortVector<T>::compareItems( QCollection::Item d1, QCollection::Item d2 ) { | ||
91 | return compareItems( ((VectorItem*)d1)->item, | ||
92 | ((VectorItem*)d2)->item ); | ||
93 | } | ||
94 | |||
95 | template<class T> | ||
96 | bool OPimSortVector<T>::sortAscending()const { | ||
97 | return m_ascending; | ||
98 | } | ||
99 | |||
100 | template<class T> | ||
101 | int OPimSortVector<T>::sortOrder()const { | ||
102 | return m_sort; | ||
103 | } | ||
104 | |||
105 | template<class T> | ||
106 | bool OPimSortVector<T>::insert( uint i, const T& record ) { | ||
107 | VectorItem *item = new VectorItem; | ||
108 | item->item = record; | ||
109 | return QVector<VectorItem>::insert( i, item ); | ||
110 | } | ||
111 | |||
112 | template<class T> | ||
113 | UID OPimSortVector<T>::uidAt( uint index )const { | ||
114 | return this->at( index )->item.uid(); | ||
115 | } | ||
116 | |||
117 | template<class T> | ||
118 | inline int OPimSortVector<T>::testString( const QString& left, | ||
119 | const QString& right )const { | ||
120 | return QString::compare( left, right ); | ||
121 | } | ||
122 | |||
123 | template<class T> | ||
124 | inline int OPimSortVector<T>::testDate( const QDate& left, | ||
125 | const QDate& right )const { | ||
126 | int ret = 0; | ||
127 | if ( !left .isValid() ) ret++; | ||
128 | if ( !right.isValid() ) ret--; | ||
129 | |||
130 | if ( left.isValid() && right.isValid() ) | ||
131 | ret += left < right ? -1 : 1; | ||
132 | |||
133 | return ret; | ||
134 | } | ||
135 | } | ||
136 | } | ||
137 | |||
138 | #endif | ||