| 1 | /****************************************************************************
|
|---|
| 2 | ** $Id: qtl.h 2 2005-11-16 15:49:26Z dmik $
|
|---|
| 3 | **
|
|---|
| 4 | ** Definition of Qt template library classes
|
|---|
| 5 | **
|
|---|
| 6 | ** Created : 990128
|
|---|
| 7 | **
|
|---|
| 8 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|---|
| 9 | **
|
|---|
| 10 | ** This file is part of the tools module of the Qt GUI Toolkit.
|
|---|
| 11 | **
|
|---|
| 12 | ** This file may be distributed under the terms of the Q Public License
|
|---|
| 13 | ** as defined by Trolltech AS of Norway and appearing in the file
|
|---|
| 14 | ** LICENSE.QPL included in the packaging of this file.
|
|---|
| 15 | **
|
|---|
| 16 | ** This file may be distributed and/or modified under the terms of the
|
|---|
| 17 | ** GNU General Public License version 2 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 19 | ** packaging of this file.
|
|---|
| 20 | **
|
|---|
| 21 | ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
|
|---|
| 22 | ** licenses may use this file in accordance with the Qt Commercial License
|
|---|
| 23 | ** Agreement provided with the Software.
|
|---|
| 24 | **
|
|---|
| 25 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|---|
| 26 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|---|
| 27 | **
|
|---|
| 28 | ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
|
|---|
| 29 | ** information about Qt Commercial License Agreements.
|
|---|
| 30 | ** See http://www.trolltech.com/qpl/ for QPL licensing information.
|
|---|
| 31 | ** See http://www.trolltech.com/gpl/ for GPL licensing information.
|
|---|
| 32 | **
|
|---|
| 33 | ** Contact info@trolltech.com if any conditions of this licensing are
|
|---|
| 34 | ** not clear to you.
|
|---|
| 35 | **
|
|---|
| 36 | **********************************************************************/
|
|---|
| 37 |
|
|---|
| 38 | #ifndef QTL_H
|
|---|
| 39 | #define QTL_H
|
|---|
| 40 |
|
|---|
| 41 | #ifndef QT_H
|
|---|
| 42 | #include "qglobal.h"
|
|---|
| 43 | #include "qtextstream.h"
|
|---|
| 44 | #include "qstring.h"
|
|---|
| 45 | #endif // QT_H
|
|---|
| 46 |
|
|---|
| 47 | #ifndef QT_NO_TEXTSTREAM
|
|---|
| 48 | template <class T>
|
|---|
| 49 | class QTextOStreamIterator
|
|---|
| 50 | {
|
|---|
| 51 | protected:
|
|---|
| 52 | QTextOStream& stream;
|
|---|
| 53 | QString separator;
|
|---|
| 54 |
|
|---|
| 55 | public:
|
|---|
| 56 | QTextOStreamIterator( QTextOStream& s) : stream( s ) {}
|
|---|
| 57 | QTextOStreamIterator( QTextOStream& s, const QString& sep )
|
|---|
| 58 | : stream( s ), separator( sep ) {}
|
|---|
| 59 | QTextOStreamIterator<T>& operator= ( const T& x ) {
|
|---|
| 60 | stream << x;
|
|---|
| 61 | if ( !separator.isEmpty() )
|
|---|
| 62 | stream << separator;
|
|---|
| 63 | return *this;
|
|---|
| 64 | }
|
|---|
| 65 | QTextOStreamIterator<T>& operator*() { return *this; }
|
|---|
| 66 | QTextOStreamIterator<T>& operator++() { return *this; }
|
|---|
| 67 | QTextOStreamIterator<T>& operator++(int) { return *this; }
|
|---|
| 68 | };
|
|---|
| 69 | #endif //QT_NO_TEXTSTREAM
|
|---|
| 70 |
|
|---|
| 71 | template <class InputIterator, class OutputIterator>
|
|---|
| 72 | inline OutputIterator qCopy( InputIterator _begin, InputIterator _end,
|
|---|
| 73 | OutputIterator _dest )
|
|---|
| 74 | {
|
|---|
| 75 | while( _begin != _end )
|
|---|
| 76 | *_dest++ = *_begin++;
|
|---|
| 77 | return _dest;
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | template <class BiIterator, class BiOutputIterator>
|
|---|
| 81 | inline BiOutputIterator qCopyBackward( BiIterator _begin, BiIterator _end,
|
|---|
| 82 | BiOutputIterator _dest )
|
|---|
| 83 | {
|
|---|
| 84 | while ( _begin != _end )
|
|---|
| 85 | *--_dest = *--_end;
|
|---|
| 86 | return _dest;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | template <class InputIterator1, class InputIterator2>
|
|---|
| 90 | inline bool qEqual( InputIterator1 first1, InputIterator1 last1, InputIterator2 first2 )
|
|---|
| 91 | {
|
|---|
| 92 | // ### compare using !(*first1 == *first2) in Qt 4.0
|
|---|
| 93 | for ( ; first1 != last1; ++first1, ++first2 )
|
|---|
| 94 | if ( *first1 != *first2 )
|
|---|
| 95 | return FALSE;
|
|---|
| 96 | return TRUE;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | template <class ForwardIterator, class T>
|
|---|
| 100 | inline void qFill( ForwardIterator first, ForwardIterator last, const T& val )
|
|---|
| 101 | {
|
|---|
| 102 | for ( ; first != last; ++first )
|
|---|
| 103 | *first = val;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | #if 0
|
|---|
| 107 | template <class BiIterator, class OutputIterator>
|
|---|
| 108 | inline OutputIterator qReverseCopy( BiIterator _begin, BiIterator _end,
|
|---|
| 109 | OutputIterator _dest )
|
|---|
| 110 | {
|
|---|
| 111 | while ( _begin != _end ) {
|
|---|
| 112 | --_end;
|
|---|
| 113 | *_dest = *_end;
|
|---|
| 114 | ++_dest;
|
|---|
| 115 | }
|
|---|
| 116 | return _dest;
|
|---|
| 117 | }
|
|---|
| 118 | #endif
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | template <class InputIterator, class T>
|
|---|
| 122 | inline InputIterator qFind( InputIterator first, InputIterator last,
|
|---|
| 123 | const T& val )
|
|---|
| 124 | {
|
|---|
| 125 | while ( first != last && *first != val )
|
|---|
| 126 | ++first;
|
|---|
| 127 | return first;
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | template <class InputIterator, class T, class Size>
|
|---|
| 131 | inline void qCount( InputIterator first, InputIterator last, const T& value,
|
|---|
| 132 | Size& n )
|
|---|
| 133 | {
|
|---|
| 134 | for ( ; first != last; ++first )
|
|---|
| 135 | if ( *first == value )
|
|---|
| 136 | ++n;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | template <class T>
|
|---|
| 140 | inline void qSwap( T& _value1, T& _value2 )
|
|---|
| 141 | {
|
|---|
| 142 | T tmp = _value1;
|
|---|
| 143 | _value1 = _value2;
|
|---|
| 144 | _value2 = tmp;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 | template <class InputIterator>
|
|---|
| 149 | Q_INLINE_TEMPLATES void qBubbleSort( InputIterator b, InputIterator e )
|
|---|
| 150 | {
|
|---|
| 151 | // Goto last element;
|
|---|
| 152 | InputIterator last = e;
|
|---|
| 153 | --last;
|
|---|
| 154 | // only one element or no elements ?
|
|---|
| 155 | if ( last == b )
|
|---|
| 156 | return;
|
|---|
| 157 |
|
|---|
| 158 | // So we have at least two elements in here
|
|---|
| 159 | while( b != last ) {
|
|---|
| 160 | bool swapped = FALSE;
|
|---|
| 161 | InputIterator swap_pos = b;
|
|---|
| 162 | InputIterator x = e;
|
|---|
| 163 | InputIterator y = x;
|
|---|
| 164 | y--;
|
|---|
| 165 | do {
|
|---|
| 166 | --x;
|
|---|
| 167 | --y;
|
|---|
| 168 | if ( *x < *y ) {
|
|---|
| 169 | swapped = TRUE;
|
|---|
| 170 | qSwap( *x, *y );
|
|---|
| 171 | swap_pos = y;
|
|---|
| 172 | }
|
|---|
| 173 | } while( y != b );
|
|---|
| 174 | if ( !swapped )
|
|---|
| 175 | return;
|
|---|
| 176 | b = swap_pos;
|
|---|
| 177 | b++;
|
|---|
| 178 | }
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 |
|
|---|
| 182 | template <class Container>
|
|---|
| 183 | inline void qBubbleSort( Container &c )
|
|---|
| 184 | {
|
|---|
| 185 | qBubbleSort( c.begin(), c.end() );
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 |
|
|---|
| 189 | template <class Value>
|
|---|
| 190 | Q_INLINE_TEMPLATES void qHeapSortPushDown( Value* heap, int first, int last )
|
|---|
| 191 | {
|
|---|
| 192 | int r = first;
|
|---|
| 193 | while ( r <= last / 2 ) {
|
|---|
| 194 | if ( last == 2 * r ) {
|
|---|
| 195 | // node r has only one child
|
|---|
| 196 | if ( heap[2 * r] < heap[r] )
|
|---|
| 197 | qSwap( heap[r], heap[2 * r] );
|
|---|
| 198 | r = last;
|
|---|
| 199 | } else {
|
|---|
| 200 | // node r has two children
|
|---|
| 201 | if ( heap[2 * r] < heap[r] && !(heap[2 * r + 1] < heap[2 * r]) ) {
|
|---|
| 202 | // swap with left child
|
|---|
| 203 | qSwap( heap[r], heap[2 * r] );
|
|---|
| 204 | r *= 2;
|
|---|
| 205 | } else if ( heap[2 * r + 1] < heap[r]
|
|---|
| 206 | && heap[2 * r + 1] < heap[2 * r] ) {
|
|---|
| 207 | // swap with right child
|
|---|
| 208 | qSwap( heap[r], heap[2 * r + 1] );
|
|---|
| 209 | r = 2 * r + 1;
|
|---|
| 210 | } else {
|
|---|
| 211 | r = last;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 | template <class InputIterator, class Value>
|
|---|
| 219 | Q_INLINE_TEMPLATES void qHeapSortHelper( InputIterator b, InputIterator e, Value, uint n )
|
|---|
| 220 | {
|
|---|
| 221 | // Create the heap
|
|---|
| 222 | InputIterator insert = b;
|
|---|
| 223 | Value* realheap = new Value[n];
|
|---|
| 224 | // Wow, what a fake. But I want the heap to be indexed as 1...n
|
|---|
| 225 | Value* heap = realheap - 1;
|
|---|
| 226 | int size = 0;
|
|---|
| 227 | for( ; insert != e; ++insert ) {
|
|---|
| 228 | heap[++size] = *insert;
|
|---|
| 229 | int i = size;
|
|---|
| 230 | while( i > 1 && heap[i] < heap[i / 2] ) {
|
|---|
| 231 | qSwap( heap[i], heap[i / 2] );
|
|---|
| 232 | i /= 2;
|
|---|
| 233 | }
|
|---|
| 234 | }
|
|---|
| 235 |
|
|---|
| 236 | // Now do the sorting
|
|---|
| 237 | for( uint i = n; i > 0; i-- ) {
|
|---|
| 238 | *b++ = heap[1];
|
|---|
| 239 | if ( i > 1 ) {
|
|---|
| 240 | heap[1] = heap[i];
|
|---|
| 241 | qHeapSortPushDown( heap, 1, (int)i - 1 );
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|
| 244 |
|
|---|
| 245 | delete[] realheap;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 |
|
|---|
| 249 | template <class InputIterator>
|
|---|
| 250 | Q_INLINE_TEMPLATES void qHeapSort( InputIterator b, InputIterator e )
|
|---|
| 251 | {
|
|---|
| 252 | // Empty ?
|
|---|
| 253 | if ( b == e )
|
|---|
| 254 | return;
|
|---|
| 255 |
|
|---|
| 256 | // How many entries have to be sorted ?
|
|---|
| 257 | InputIterator it = b;
|
|---|
| 258 | uint n = 0;
|
|---|
| 259 | while ( it != e ) {
|
|---|
| 260 | ++n;
|
|---|
| 261 | ++it;
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | // The second last parameter is a hack to retrieve the value type
|
|---|
| 265 | // Do the real sorting here
|
|---|
| 266 | qHeapSortHelper( b, e, *b, n );
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 |
|
|---|
| 270 | template <class Container>
|
|---|
| 271 | Q_INLINE_TEMPLATES void qHeapSort( Container &c )
|
|---|
| 272 | {
|
|---|
| 273 | if ( c.begin() == c.end() )
|
|---|
| 274 | return;
|
|---|
| 275 |
|
|---|
| 276 | // The second last parameter is a hack to retrieve the value type
|
|---|
| 277 | // Do the real sorting here
|
|---|
| 278 | qHeapSortHelper( c.begin(), c.end(), *(c.begin()), (uint)c.count() );
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | template <class Container>
|
|---|
| 282 | class QBackInsertIterator
|
|---|
| 283 | {
|
|---|
| 284 | public:
|
|---|
| 285 | Q_EXPLICIT QBackInsertIterator( Container &c )
|
|---|
| 286 | : container( &c )
|
|---|
| 287 | {
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | QBackInsertIterator<Container>&
|
|---|
| 291 | operator=( const Q_TYPENAME Container::value_type &value )
|
|---|
| 292 | {
|
|---|
| 293 | container->push_back( value );
|
|---|
| 294 | return *this;
|
|---|
| 295 | }
|
|---|
| 296 |
|
|---|
| 297 | QBackInsertIterator<Container>& operator*()
|
|---|
| 298 | {
|
|---|
| 299 | return *this;
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | QBackInsertIterator<Container>& operator++()
|
|---|
| 303 | {
|
|---|
| 304 | return *this;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | QBackInsertIterator<Container>& operator++(int)
|
|---|
| 308 | {
|
|---|
| 309 | return *this;
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | protected:
|
|---|
| 313 | Container *container;
|
|---|
| 314 | };
|
|---|
| 315 |
|
|---|
| 316 | template <class Container>
|
|---|
| 317 | inline QBackInsertIterator<Container> qBackInserter( Container &c )
|
|---|
| 318 | {
|
|---|
| 319 | return QBackInsertIterator<Container>( c );
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | #endif
|
|---|