Blame qtools/qtl.h

Packit 1c1d7e
/****************************************************************************
Packit 1c1d7e
** 
Packit 1c1d7e
**
Packit 1c1d7e
** Definition of Qt template library classes
Packit 1c1d7e
**
Packit 1c1d7e
** Created : 990128
Packit 1c1d7e
**
Packit 1c1d7e
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit 1c1d7e
**
Packit 1c1d7e
** This file is part of the tools module of the Qt GUI Toolkit.
Packit 1c1d7e
**
Packit 1c1d7e
** This file may be distributed under the terms of the Q Public License
Packit 1c1d7e
** as defined by Trolltech AS of Norway and appearing in the file
Packit 1c1d7e
** LICENSE.QPL included in the packaging of this file.
Packit 1c1d7e
**
Packit 1c1d7e
** This file may be distributed and/or modified under the terms of the
Packit 1c1d7e
** GNU General Public License version 2 as published by the Free Software
Packit 1c1d7e
** Foundation and appearing in the file LICENSE.GPL included in the
Packit 1c1d7e
** packaging of this file.
Packit 1c1d7e
**
Packit 1c1d7e
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit 1c1d7e
** licenses may use this file in accordance with the Qt Commercial License
Packit 1c1d7e
** Agreement provided with the Software.
Packit 1c1d7e
**
Packit 1c1d7e
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit 1c1d7e
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit 1c1d7e
**
Packit 1c1d7e
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit 1c1d7e
**   information about Qt Commercial License Agreements.
Packit 1c1d7e
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit 1c1d7e
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit 1c1d7e
**
Packit 1c1d7e
** Contact info@trolltech.com if any conditions of this licensing are
Packit 1c1d7e
** not clear to you.
Packit 1c1d7e
**
Packit 1c1d7e
**********************************************************************/
Packit 1c1d7e
#ifndef QTL_H
Packit 1c1d7e
#define QTL_H
Packit 1c1d7e
Packit 1c1d7e
#ifndef QT_H
Packit 1c1d7e
#include "qtextstream.h"
Packit 1c1d7e
#include "qstring.h"
Packit 1c1d7e
#endif // QT_H
Packit 1c1d7e
Packit 1c1d7e
#ifndef QT_NO_TEXTSTREAM
Packit 1c1d7e
template <class T>
Packit 1c1d7e
class QTextOStreamIterator
Packit 1c1d7e
{
Packit 1c1d7e
protected:
Packit 1c1d7e
    QTextOStream& stream;
Packit 1c1d7e
    QString separator;
Packit 1c1d7e
Packit 1c1d7e
public:
Packit 1c1d7e
    QTextOStreamIterator( QTextOStream& s) : stream( s ) {}
Packit 1c1d7e
    QTextOStreamIterator( QTextOStream& s, const QString& sep )
Packit 1c1d7e
	: stream( s ), separator( sep )  {}
Packit 1c1d7e
    QTextOStreamIterator<T>& operator= ( const T& x ) {
Packit 1c1d7e
	stream << x;
Packit 1c1d7e
	if ( !separator.isEmpty() )
Packit 1c1d7e
	    stream << separator;
Packit 1c1d7e
	return *this;
Packit 1c1d7e
    }
Packit 1c1d7e
    QTextOStreamIterator<T>& operator*() { return *this; }
Packit 1c1d7e
    QTextOStreamIterator<T>& operator++() { return *this; }
Packit 1c1d7e
    QTextOStreamIterator<T>& operator++(int) { return *this; }
Packit 1c1d7e
};
Packit 1c1d7e
#endif //QT_NO_TEXTSTREAM
Packit 1c1d7e
Packit 1c1d7e
template <class InputIterator, class OutputIterator>
Packit 1c1d7e
inline OutputIterator qCopy( InputIterator _begin, InputIterator _end,
Packit 1c1d7e
			     OutputIterator _dest )
Packit 1c1d7e
{
Packit 1c1d7e
    while( _begin != _end )
Packit 1c1d7e
	*_dest++ = *_begin++;
Packit 1c1d7e
    return _dest;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class T>
Packit 1c1d7e
inline void qSwap( T& _value1, T& _value2 )
Packit 1c1d7e
{
Packit 1c1d7e
    T tmp = _value1;
Packit 1c1d7e
    _value1 = _value2;
Packit 1c1d7e
    _value2 = tmp;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class InputIterator>
Packit 1c1d7e
inline void qBubbleSort( InputIterator b, InputIterator e )
Packit 1c1d7e
{
Packit 1c1d7e
    // Goto last element;
Packit 1c1d7e
    InputIterator last = e;
Packit 1c1d7e
    --last;
Packit 1c1d7e
    // only one element or no elements ?
Packit 1c1d7e
    if ( last == b )
Packit 1c1d7e
	return;
Packit 1c1d7e
Packit 1c1d7e
    // So we have at least two elements in here
Packit 1c1d7e
    while( b != last ) {
Packit 1c1d7e
	bool swapped = FALSE;
Packit 1c1d7e
	InputIterator swap_pos = b;
Packit 1c1d7e
	InputIterator x = e;
Packit 1c1d7e
	InputIterator y = x;
Packit 1c1d7e
	y--;
Packit 1c1d7e
	do {
Packit 1c1d7e
	    --x;
Packit 1c1d7e
	    --y;
Packit 1c1d7e
	    if ( *x < *y ) {
Packit 1c1d7e
		swapped = TRUE;
Packit 1c1d7e
		qSwap( *x, *y );
Packit 1c1d7e
		swap_pos = y;
Packit 1c1d7e
	    }
Packit 1c1d7e
	} while( y != b );
Packit 1c1d7e
	if ( !swapped )
Packit 1c1d7e
	    return;
Packit 1c1d7e
	b = swap_pos;
Packit 1c1d7e
	b++;
Packit 1c1d7e
    }
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class Container>
Packit 1c1d7e
inline void qBubbleSort( Container &c )
Packit 1c1d7e
{
Packit 1c1d7e
  qBubbleSort( c.begin(), c.end() );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class Value>
Packit 1c1d7e
inline void qHeapSortPushDown( Value* heap, int first, int last )
Packit 1c1d7e
{
Packit 1c1d7e
    int r = first;
Packit 1c1d7e
    while( r <= last/2 ) {
Packit 1c1d7e
	// Node r has only one child ?
Packit 1c1d7e
	if ( last == 2*r ) {
Packit 1c1d7e
	    // Need for swapping ?
Packit 1c1d7e
	    if ( heap[r] > heap[ 2*r ] )
Packit 1c1d7e
		qSwap( heap[r], heap[ 2*r ] );
Packit 1c1d7e
	    // That's it ...
Packit 1c1d7e
	    r = last;
Packit 1c1d7e
	} else { // Node has two children
Packit 1c1d7e
	    if ( heap[r] > heap[ 2*r ] && heap[ 2*r ] <= heap[ 2*r+1 ] ) {
Packit 1c1d7e
		// Swap with left child
Packit 1c1d7e
		qSwap( heap[r], heap[ 2*r ] );
Packit 1c1d7e
		r *= 2;
Packit 1c1d7e
	    } else if ( heap[r] > heap[ 2*r+1 ] &&
Packit 1c1d7e
			heap[ 2*r+1 ] < heap[ 2*r ] ) {
Packit 1c1d7e
		// Swap with right child
Packit 1c1d7e
		qSwap( heap[r], heap[ 2*r+1 ] );
Packit 1c1d7e
		r = 2*r+1;
Packit 1c1d7e
	    } else {
Packit 1c1d7e
		// We are done
Packit 1c1d7e
		r = last;
Packit 1c1d7e
	    }
Packit 1c1d7e
	}
Packit 1c1d7e
    }
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class InputIterator, class Value>
Packit 1c1d7e
inline void qHeapSortHelper( InputIterator b, InputIterator e, Value, uint n )
Packit 1c1d7e
{
Packit 1c1d7e
    // Create the heap
Packit 1c1d7e
    InputIterator insert = b;
Packit 1c1d7e
    Value* realheap = new Value[ n ];
Packit 1c1d7e
    // Wow, what a fake. But I want the heap to be indexed as 1...n
Packit 1c1d7e
    Value* heap = realheap - 1;
Packit 1c1d7e
    int size = 0;
Packit 1c1d7e
    for( ; insert != e; ++insert ) {
Packit 1c1d7e
	heap[++size] = *insert;
Packit 1c1d7e
	int i = size;
Packit 1c1d7e
	while( i > 1 && heap[i] < heap[ i / 2 ] ) {
Packit 1c1d7e
	    qSwap( heap[i], heap[ i / 2 ] );
Packit 1c1d7e
	    i /= 2;
Packit 1c1d7e
	}
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    // Now do the sorting
Packit 1c1d7e
    for( uint i = n; i > 0; i-- ) {
Packit 1c1d7e
	*b++ = heap[1];
Packit 1c1d7e
	if ( i > 1 ) {
Packit 1c1d7e
	    heap[1] = heap[i];
Packit 1c1d7e
	    qHeapSortPushDown( heap, 1, (int)i - 1 );
Packit 1c1d7e
	}
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    delete[] realheap;
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class InputIterator>
Packit 1c1d7e
inline void qHeapSort( InputIterator b, InputIterator e )
Packit 1c1d7e
{
Packit 1c1d7e
    // Empty ?
Packit 1c1d7e
    if ( b == e )
Packit 1c1d7e
	return;
Packit 1c1d7e
Packit 1c1d7e
    // How many entries have to be sorted ?
Packit 1c1d7e
    InputIterator it = b;
Packit 1c1d7e
    uint n = 0;
Packit 1c1d7e
    while ( it != e ) {
Packit 1c1d7e
	++n;
Packit 1c1d7e
	++it;
Packit 1c1d7e
    }
Packit 1c1d7e
Packit 1c1d7e
    // The second last parameter is a hack to retrieve the value type
Packit 1c1d7e
    // Do the real sorting here
Packit 1c1d7e
    qHeapSortHelper( b, e, *b, n );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
Packit 1c1d7e
template <class Container>
Packit 1c1d7e
inline void qHeapSort( Container &c )
Packit 1c1d7e
{
Packit 1c1d7e
    if ( c.isEmpty() )
Packit 1c1d7e
	return;
Packit 1c1d7e
Packit 1c1d7e
    // The second last parameter is a hack to retrieve the value type
Packit 1c1d7e
    // Do the real sorting here
Packit 1c1d7e
    qHeapSortHelper( c.begin(), c.end(), *(c.begin()), c.count() );
Packit 1c1d7e
}
Packit 1c1d7e
Packit 1c1d7e
#endif