Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

otl.h

Go to the documentation of this file.
00001 // qtl minus QT_INLINE_TEMPLATE and QT_EXPLICIT (instead directly using 'inline' directive)
00002 //FIXME: remove and use qtl.h as soon as we're on Qt3
00003 
00004 /****************************************************************************
00005 ** $Id: otl.h,v 1.1 2003/03/28 15:11:52 mickeyl Exp $
00006 **
00007 ** Definition of Qt template library classes
00008 **
00009 ** Created : 990128
00010 **
00011 ** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
00012 **
00013 ** This file is part of the tools module of the Qt GUI Toolkit.
00014 **
00015 ** This file may be distributed under the terms of the Q Public License
00016 ** as defined by Trolltech AS of Norway and appearing in the file
00017 ** LICENSE.QPL included in the packaging of this file.
00018 **
00019 ** This file may be distributed and/or modified under the terms of the
00020 ** GNU General Public License version 2 as published by the Free Software
00021 ** Foundation and appearing in the file LICENSE.GPL included in the
00022 ** packaging of this file.
00023 **
00024 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
00025 ** licenses may use this file in accordance with the Qt Commercial License
00026 ** Agreement provided with the Software.
00027 **
00028 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00029 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00030 **
00031 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
00032 **   information about Qt Commercial License Agreements.
00033 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
00034 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00035 **
00036 ** Contact info@trolltech.com if any conditions of this licensing are
00037 ** not clear to you.
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     // ### compare using !(*first1 == *first2) in Qt 4.0
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     // Goto last element;
00155     InputIterator last = e;
00156     --last;
00157     // only one element or no elements ?
00158     if ( last == b )
00159         return;
00160 
00161     // So we have at least two elements in here
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             // node r has only one child
00199             if ( heap[2 * r] < heap[r] )
00200                 qSwap( heap[r], heap[2 * r] );
00201             r = last;
00202         } else {
00203             // node r has two children
00204             if ( heap[2 * r] < heap[r] && !(heap[2 * r + 1] < heap[2 * r]) ) {
00205                 // swap with left child
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                 // swap with right child
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     // Create the heap
00225     InputIterator insert = b;
00226     Value* realheap = new Value[n];
00227     // Wow, what a fake. But I want the heap to be indexed as 1...n
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     // Now do the sorting
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     // Empty ?
00256     if ( b == e )
00257         return;
00258 
00259     // How many entries have to be sorted ?
00260     InputIterator it = b;
00261     uint n = 0;
00262     while ( it != e ) {
00263         ++n;
00264         ++it;
00265     }
00266 
00267     // The second last parameter is a hack to retrieve the value type
00268     // Do the real sorting here
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     // The second last parameter is a hack to retrieve the value type
00280     // Do the real sorting here
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

Generated on Sat Nov 5 16:16:28 2005 for OPIE by  doxygen 1.4.2