00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef QTL_H
00039 #define QTL_H
00040
00041 #ifndef QT_H
00042 #include "qglobal.h"
00043 #include "qtextstream.h"
00044 #include "qstring.h"
00045 #endif // QT_H
00046
00047 #ifndef QT_NO_TEXTSTREAM
00048 template <class T>
00049 class QTextOStreamIterator
00050 {
00051 protected:
00052 QTextOStream& stream;
00053 QString separator;
00054
00055 public:
00056 QTextOStreamIterator( QTextOStream& s) : stream( s ) {}
00057 QTextOStreamIterator( QTextOStream& s, const QString& sep )
00058 : stream( s ), separator( sep ) {}
00059 QTextOStreamIterator<T>& operator= ( const T& x ) {
00060 stream << x;
00061 if ( !separator.isEmpty() )
00062 stream << separator;
00063 return *this;
00064 }
00065 QTextOStreamIterator<T>& operator*() { return *this; }
00066 QTextOStreamIterator<T>& operator++() { return *this; }
00067 QTextOStreamIterator<T>& operator++(int) { return *this; }
00068 };
00069 #endif //QT_NO_TEXTSTREAM
00070
00071 template <class InputIterator, class OutputIterator>
00072 inline OutputIterator qCopy( InputIterator _begin, InputIterator _end,
00073 OutputIterator _dest )
00074 {
00075 while( _begin != _end )
00076 *_dest++ = *_begin++;
00077 return _dest;
00078 }
00079
00080 template <class BiIterator, class BiOutputIterator>
00081 inline BiOutputIterator qCopyBackward( BiIterator _begin, BiIterator _end,
00082 BiOutputIterator _dest )
00083 {
00084 while ( _begin != _end )
00085 *--_dest = *--_end;
00086 return _dest;
00087 }
00088
00089 template <class InputIterator1, class InputIterator2>
00090 inline bool qEqual( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
00091 {
00092
00093 for ( ; first1 != last1; ++first1, ++first2 )
00094 if ( *first1 != *first2 )
00095 return FALSE;
00096 return TRUE;
00097 }
00098
00099 template <class ForwardIterator, class T>
00100 inline void qFill( ForwardIterator first, ForwardIterator last, const T& val )
00101 {
00102 for ( ; first != last; ++first )
00103 *first = val;
00104 }
00105
00106 #if 0
00107 template <class BiIterator, class OutputIterator>
00108 inline OutputIterator qReverseCopy( BiIterator _begin, BiIterator _end,
00109 OutputIterator _dest )
00110 {
00111 while ( _begin != _end ) {
00112 --_end;
00113 *_dest = *_end;
00114 ++_dest;
00115 }
00116 return _dest;
00117 }
00118 #endif
00119
00120
00121 template <class InputIterator, class T>
00122 inline InputIterator qFind( InputIterator first, InputIterator last,
00123 const T& val )
00124 {
00125 while ( first != last && *first != val )
00126 ++first;
00127 return first;
00128 }
00129
00130 template <class InputIterator, class T, class Size>
00131 inline void qCount( InputIterator first, InputIterator last, const T& value,
00132 Size& n )
00133 {
00134 for ( ; first != last; ++first )
00135 if ( *first == value )
00136 ++n;
00137 }
00138
00139 template <class T>
00140 inline void qSwap( T& _value1, T& _value2 )
00141 {
00142 T tmp = _value1;
00143 _value1 = _value2;
00144 _value2 = tmp;
00145 }
00146
00147
00148 template <class InputIterator>
00149 Q_INLINE_TEMPLATES void qBubbleSort( InputIterator b, InputIterator e )
00150 {
00151
00152 InputIterator last = e;
00153 --last;
00154
00155 if ( last == b )
00156 return;
00157
00158
00159 while( b != last ) {
00160 bool swapped = FALSE;
00161 InputIterator swap_pos = b;
00162 InputIterator x = e;
00163 InputIterator y = x;
00164 y--;
00165 do {
00166 --x;
00167 --y;
00168 if ( *x < *y ) {
00169 swapped = TRUE;
00170 qSwap( *x, *y );
00171 swap_pos = y;
00172 }
00173 } while( y != b );
00174 if ( !swapped )
00175 return;
00176 b = swap_pos;
00177 b++;
00178 }
00179 }
00180
00181
00182 template <class Container>
00183 inline void qBubbleSort( Container &c )
00184 {
00185 qBubbleSort( c.begin(), c.end() );
00186 }
00187
00188
00189 template <class Value>
00190 Q_INLINE_TEMPLATES void qHeapSortPushDown( Value* heap, int first, int last )
00191 {
00192 int r = first;
00193 while ( r <= last / 2 ) {
00194 if ( last == 2 * r ) {
00195
00196 if ( heap[2 * r] < heap[r] )
00197 qSwap( heap[r], heap[2 * r] );
00198 r = last;
00199 } else {
00200
00201 if ( heap[2 * r] < heap[r] && !(heap[2 * r + 1] < heap[2 * r]) ) {
00202
00203 qSwap( heap[r], heap[2 * r] );
00204 r *= 2;
00205 } else if ( heap[2 * r + 1] < heap[r]
00206 && heap[2 * r + 1] < heap[2 * r] ) {
00207
00208 qSwap( heap[r], heap[2 * r + 1] );
00209 r = 2 * r + 1;
00210 } else {
00211 r = last;
00212 }
00213 }
00214 }
00215 }
00216
00217
00218 template <class InputIterator, class Value>
00219 Q_INLINE_TEMPLATES void qHeapSortHelper( InputIterator b, InputIterator e, Value, uint n )
00220 {
00221
00222 InputIterator insert = b;
00223 Value* realheap = new Value[n];
00224
00225 Value* heap = realheap - 1;
00226 int size = 0;
00227 for( ; insert != e; ++insert ) {
00228 heap[++size] = *insert;
00229 int i = size;
00230 while( i > 1 && heap[i] < heap[i / 2] ) {
00231 qSwap( heap[i], heap[i / 2] );
00232 i /= 2;
00233 }
00234 }
00235
00236
00237 for( uint i = n; i > 0; i-- ) {
00238 *b++ = heap[1];
00239 if ( i > 1 ) {
00240 heap[1] = heap[i];
00241 qHeapSortPushDown( heap, 1, (int)i - 1 );
00242 }
00243 }
00244
00245 delete[] realheap;
00246 }
00247
00248
00249 template <class InputIterator>
00250 Q_INLINE_TEMPLATES void qHeapSort( InputIterator b, InputIterator e )
00251 {
00252
00253 if ( b == e )
00254 return;
00255
00256
00257 InputIterator it = b;
00258 uint n = 0;
00259 while ( it != e ) {
00260 ++n;
00261 ++it;
00262 }
00263
00264
00265
00266 qHeapSortHelper( b, e, *b, n );
00267 }
00268
00269
00270 template <class Container>
00271 Q_INLINE_TEMPLATES void qHeapSort( Container &c )
00272 {
00273 if ( c.begin() == c.end() )
00274 return;
00275
00276
00277
00278 qHeapSortHelper( c.begin(), c.end(), *(c.begin()), (uint)c.count() );
00279 }
00280
00281 template <class Container>
00282 class QBackInsertIterator
00283 {
00284 public:
00285 Q_EXPLICIT QBackInsertIterator( Container &c )
00286 : container( &c )
00287 {
00288 }
00289
00290 QBackInsertIterator<Container>&
00291 operator=( const Q_TYPENAME Container::value_type &value )
00292 {
00293 container->push_back( value );
00294 return *this;
00295 }
00296
00297 QBackInsertIterator<Container>& operator*()
00298 {
00299 return *this;
00300 }
00301
00302 QBackInsertIterator<Container>& operator++()
00303 {
00304 return *this;
00305 }
00306
00307 QBackInsertIterator<Container>& operator++(int)
00308 {
00309 return *this;
00310 }
00311
00312 protected:
00313 Container *container;
00314 };
00315
00316 template <class Container>
00317 inline QBackInsertIterator<Container> qBackInserter( Container &c )
00318 {
00319 return QBackInsertIterator<Container>( c );
00320 }
00321
00322 #endif