summaryrefslogtreecommitdiff
path: root/libopie2/opiepim/private/opimsortvector.h
Unidiff
Diffstat (limited to 'libopie2/opiepim/private/opimsortvector.h') (more/less context) (show whitespace changes)
-rw-r--r--libopie2/opiepim/private/opimsortvector.h34
1 files changed, 34 insertions, 0 deletions
diff --git a/libopie2/opiepim/private/opimsortvector.h b/libopie2/opiepim/private/opimsortvector.h
index 6c21339..11a40ac 100644
--- a/libopie2/opiepim/private/opimsortvector.h
+++ b/libopie2/opiepim/private/opimsortvector.h
@@ -8,131 +8,165 @@
8.> <`_, > . <= redistribute it and/or modify it under 8.> <`_, > . <= redistribute it and/or modify it under
9:`=1 )Y*s>-.-- : the terms of the GNU Library General Public 9:`=1 )Y*s>-.-- : the terms of the GNU Library General Public
10.="- .-=="i, .._ License as published by the Free Software 10.="- .-=="i, .._ License as published by the Free Software
11 - . .-<_> .<> Foundation; either version 2 of the License, 11 - . .-<_> .<> Foundation; either version 2 of the License,
12 ._= =} : or (at your option) any later version. 12 ._= =} : or (at your option) any later version.
13 .%`+i> _;_. 13 .%`+i> _;_.
14 .i_,=:_. -<s. This program is distributed in the hope that 14 .i_,=:_. -<s. This program is distributed in the hope that
15 + . -:. = it will be useful, but WITHOUT ANY WARRANTY; 15 + . -:. = it will be useful, but WITHOUT ANY WARRANTY;
16 : .. .:, . . . without even the implied warranty of 16 : .. .:, . . . without even the implied warranty of
17 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A 17 =_ + =;=|` MERCHANTABILITY or FITNESS FOR A
18 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU 18 _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU
19..}^=.= = ; Library General Public License for more 19..}^=.= = ; Library General Public License for more
20++= -. .` .: details. 20++= -. .` .: details.
21 : = ...= . :.=- 21 : = ...= . :.=-
22 -. .:....=;==+<; You should have received a copy of the GNU 22 -. .:....=;==+<; You should have received a copy of the GNU
23 -_. . . )=. = Library General Public License along with 23 -_. . . )=. = Library General Public License along with
24 -- :-=` this library; see the file COPYING.LIB. 24 -- :-=` this library; see the file COPYING.LIB.
25 If not, write to the Free Software Foundation, 25 If not, write to the Free Software Foundation,
26 Inc., 59 Temple Place - Suite 330, 26 Inc., 59 Temple Place - Suite 330,
27 Boston, MA 02111-1307, USA. 27 Boston, MA 02111-1307, USA.
28*/ 28*/
29 29
30#ifndef OPIE_PIM_SORT_VECTOR_H 30#ifndef OPIE_PIM_SORT_VECTOR_H
31#define OPIE_PIM_SORT_VECTOR_H 31#define OPIE_PIM_SORT_VECTOR_H
32 32
33#include <opie2/opimglobal.h> 33#include <opie2/opimglobal.h>
34 34
35#include <qvector.h> 35#include <qvector.h>
36 36
37namespace Opie { 37namespace Opie {
38namespace Internal { 38namespace Internal {
39template<class T> 39template<class T>
40struct OPimSortVectorContainer { 40struct OPimSortVectorContainer {
41 T item; 41 T item;
42}; 42};
43 43
44template<class T> 44template<class T>
45class OPimSortVector : public QVector<OPimSortVectorContainer<T> > { 45class OPimSortVector : public QVector<OPimSortVectorContainer<T> > {
46 typedef OPimSortVectorContainer<T> VectorItem; 46 typedef OPimSortVectorContainer<T> VectorItem;
47public: 47public:
48 OPimSortVector( uint size, bool asc, int sort ); 48 OPimSortVector( uint size, bool asc, int sort );
49 int compareItems( QCollection::Item d1, QCollection::Item d2 ); 49 int compareItems( QCollection::Item d1, QCollection::Item d2 );
50 bool insert( uint, const T& t ); 50 bool insert( uint, const T& t );
51 UID uidAt( uint i )const; 51 UID uidAt( uint i )const;
52 52
53protected: 53protected:
54 int testString( const QString&, const QString& )const; 54 int testString( const QString&, const QString& )const;
55 int testDate( const QDate&, const QDate& )const; 55 int testDate( const QDate&, const QDate& )const;
56 int testTime( const QTime&, const QTime& )const;
57 int testDateTime( const QDateTime& left,
58 const QDateTime& right )const;
56protected: 59protected:
57 bool sortAscending()const; 60 bool sortAscending()const;
58 int sortOrder()const; 61 int sortOrder()const;
59 62
60private: 63private:
61 bool m_ascending : 1; 64 bool m_ascending : 1;
62 int m_sort; 65 int m_sort;
63 virtual int compareItems( const T& item1, const T& item2 ) = 0; 66 virtual int compareItems( const T& item1, const T& item2 ) = 0;
64}; 67};
65 68
66template<class T> 69template<class T>
67OPimSortVector<T>::OPimSortVector( uint size, bool asc, int sort ) 70OPimSortVector<T>::OPimSortVector( uint size, bool asc, int sort )
68 : QVector<VectorItem>( size ), m_ascending( asc ), 71 : QVector<VectorItem>( size ), m_ascending( asc ),
69 m_sort( sort ) { 72 m_sort( sort ) {
70 this->setAutoDelete( true ); 73 this->setAutoDelete( true );
71} 74}
72 75
73/** 76/**
74 * Returns: 77 * Returns:
75 * 0 if item1 == item2 78 * 0 if item1 == item2
76 * 79 *
77 * non-zero if item1 != item2 80 * non-zero if item1 != item2
78 * 81 *
79 * This function returns int rather than bool so that reimplementations 82 * This function returns int rather than bool so that reimplementations
80 * can return one of three values and use it to sort by: 83 * can return one of three values and use it to sort by:
81 * 84 *
82 * 0 if item1 == item2 85 * 0 if item1 == item2
83 * 86 *
84 * > 0 (positive integer) if item1 > item2 87 * > 0 (positive integer) if item1 > item2
85 * 88 *
86 * < 0 (negative integer) if item1 < item2 89 * < 0 (negative integer) if item1 < item2
87 * 90 *
88 */ 91 */
89template<class T> 92template<class T>
90int OPimSortVector<T>::compareItems( QCollection::Item d1, QCollection::Item d2 ) { 93int OPimSortVector<T>::compareItems( QCollection::Item d1, QCollection::Item d2 ) {
91 return compareItems( ((VectorItem*)d1)->item, 94 return compareItems( ((VectorItem*)d1)->item,
92 ((VectorItem*)d2)->item ); 95 ((VectorItem*)d2)->item );
93} 96}
94 97
95template<class T> 98template<class T>
96bool OPimSortVector<T>::sortAscending()const { 99bool OPimSortVector<T>::sortAscending()const {
97 return m_ascending; 100 return m_ascending;
98} 101}
99 102
100template<class T> 103template<class T>
101int OPimSortVector<T>::sortOrder()const { 104int OPimSortVector<T>::sortOrder()const {
102 return m_sort; 105 return m_sort;
103} 106}
104 107
105template<class T> 108template<class T>
106bool OPimSortVector<T>::insert( uint i, const T& record ) { 109bool OPimSortVector<T>::insert( uint i, const T& record ) {
107 VectorItem *item = new VectorItem; 110 VectorItem *item = new VectorItem;
108 item->item = record; 111 item->item = record;
109 return QVector<VectorItem>::insert( i, item ); 112 return QVector<VectorItem>::insert( i, item );
110} 113}
111 114
112template<class T> 115template<class T>
113UID OPimSortVector<T>::uidAt( uint index )const { 116UID OPimSortVector<T>::uidAt( uint index )const {
114 return this->at( index )->item.uid(); 117 return this->at( index )->item.uid();
115} 118}
116 119
117template<class T> 120template<class T>
118inline int OPimSortVector<T>::testString( const QString& left, 121inline int OPimSortVector<T>::testString( const QString& left,
119 const QString& right )const { 122 const QString& right )const {
120 return QString::compare( left, right ); 123 return QString::compare( left, right );
121} 124}
122 125
126
123template<class T> 127template<class T>
124inline int OPimSortVector<T>::testDate( const QDate& left, 128inline int OPimSortVector<T>::testDate( const QDate& left,
125 const QDate& right )const { 129 const QDate& right )const {
126 int ret = 0; 130 int ret = 0;
127 if ( !left .isValid() ) ret++; 131 if ( !left .isValid() ) ret++;
128 if ( !right.isValid() ) ret--; 132 if ( !right.isValid() ) ret--;
129 133
130 if ( left.isValid() && right.isValid() ) 134 if ( left.isValid() && right.isValid() )
131 ret += left < right ? -1 : 1; 135 ret += left < right ? -1 : 1;
132 136
133 return ret; 137 return ret;
134} 138}
139
140template<class T>
141inline int OPimSortVector<T>::testTime( const QTime& left,
142 const QTime& right )const {
143 int ret = 0;
144 if ( !left .isValid() ) ret++;
145 if ( !right.isValid() ) ret--;
146
147 if ( left.isValid() && right.isValid() ){
148 ret += left < right ? -1 : 1;
149 }
150
151 return ret;
152}
153
154template<class T>
155inline int OPimSortVector<T>::testDateTime( const QDateTime& left,
156 const QDateTime& right )const {
157 int ret = 0;
158 if ( !left .isValid() ) ret++;
159 if ( !right.isValid() ) ret--;
160
161 if ( left.isValid() && right.isValid() ){
162 ret += left < right ? -1 : 1;
163 }
164
165 return ret;
166
167}
168
135} 169}
136} 170}
137 171
138#endif 172#endif