Eneboo - Documentación para desarrolladores
src/qt/src/tools/qtl.h
Ir a la documentación de este archivo.
00001 /****************************************************************************
00002 ** $Id: qt/qtl.h   3.3.8   edited Jan 11 14:38 $
00003 **
00004 ** Definition of Qt template library classes
00005 **
00006 ** Created : 990128
00007 **
00008 ** Copyright (C) 1992-2007 Trolltech ASA.  All rights reserved.
00009 **
00010 ** This file is part of the tools module of the Qt GUI Toolkit.
00011 **
00012 ** This file may be distributed under the terms of the Q Public License
00013 ** as defined by Trolltech ASA of Norway and appearing in the file
00014 ** LICENSE.QPL included in the packaging of this file.
00015 **
00016 ** This file may be distributed and/or modified under the terms of the
00017 ** GNU General Public License version 2 as published by the Free Software
00018 ** Foundation and appearing in the file LICENSE.GPL included in the
00019 ** packaging of this file.
00020 **
00021 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00022 ** licenses may use this file in accordance with the Qt Commercial License
00023 ** Agreement provided with the Software.
00024 **
00025 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00026 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00027 **
00028 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00029 **   information about Qt Commercial License Agreements.
00030 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00031 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00032 **
00033 ** Contact info@trolltech.com if any conditions of this licensing are
00034 ** not clear to you.
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     // ### compare using !(*first1 == *first2) in Qt 4.0
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     // Goto last element;
00152     InputIterator last = e;
00153     --last;
00154     // only one element or no elements ?
00155     if ( last == b )
00156         return;
00157 
00158     // So we have at least two elements in here
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             // node r has only one child
00196             if ( heap[2 * r] < heap[r] )
00197                 qSwap( heap[r], heap[2 * r] );
00198             r = last;
00199         } else {
00200             // node r has two children
00201             if ( heap[2 * r] < heap[r] && !(heap[2 * r + 1] < heap[2 * r]) ) {
00202                 // swap with left child
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                 // swap with right child
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     // Create the heap
00222     InputIterator insert = b;
00223     Value* realheap = new Value[n];
00224     // Wow, what a fake. But I want the heap to be indexed as 1...n
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     // Now do the sorting
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     // Empty ?
00253     if ( b == e )
00254         return;
00255 
00256     // How many entries have to be sorted ?
00257     InputIterator it = b;
00258     uint n = 0;
00259     while ( it != e ) {
00260         ++n;
00261         ++it;
00262     }
00263 
00264     // The second last parameter is a hack to retrieve the value type
00265     // Do the real sorting here
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     // The second last parameter is a hack to retrieve the value type
00277     // Do the real sorting here
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
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'