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