Blame qtools/qmap.h

Packit Service 50c9f2
/****************************************************************************
Packit Service 50c9f2
** 
Packit Service 50c9f2
**
Packit Service 50c9f2
** Definition of QMap class
Packit Service 50c9f2
**
Packit Service 50c9f2
** Created : 990406
Packit Service 50c9f2
**
Packit Service 50c9f2
** Copyright (C) 1992-2000 Trolltech AS.  All rights reserved.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is part of the tools module of the Qt GUI Toolkit.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed under the terms of the Q Public License
Packit Service 50c9f2
** as defined by Trolltech AS of Norway and appearing in the file
Packit Service 50c9f2
** LICENSE.QPL included in the packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file may be distributed and/or modified under the terms of the
Packit Service 50c9f2
** GNU General Public License version 2 as published by the Free Software
Packit Service 50c9f2
** Foundation and appearing in the file LICENSE.GPL included in the
Packit Service 50c9f2
** packaging of this file.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
Packit Service 50c9f2
** licenses may use this file in accordance with the Qt Commercial License
Packit Service 50c9f2
** Agreement provided with the Software.
Packit Service 50c9f2
**
Packit Service 50c9f2
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
Packit Service 50c9f2
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Packit Service 50c9f2
**
Packit Service 50c9f2
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
Packit Service 50c9f2
**   information about Qt Commercial License Agreements.
Packit Service 50c9f2
** See http://www.trolltech.com/qpl/ for QPL licensing information.
Packit Service 50c9f2
** See http://www.trolltech.com/gpl/ for GPL licensing information.
Packit Service 50c9f2
**
Packit Service 50c9f2
** Contact info@trolltech.com if any conditions of this licensing are
Packit Service 50c9f2
** not clear to you.
Packit Service 50c9f2
**
Packit Service 50c9f2
**********************************************************************/
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef QMAP_H
Packit Service 50c9f2
#define QMAP_H
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef QT_H
Packit Service 50c9f2
#include "qshared.h"
Packit Service 50c9f2
#include "qdatastream.h"
Packit Service 50c9f2
#endif // QT_H
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
struct QMapNodeBase
Packit Service 50c9f2
{
Packit Service 50c9f2
    enum Color { Red, Black };
Packit Service 50c9f2
Packit Service 50c9f2
    QMapNodeBase* left;
Packit Service 50c9f2
    QMapNodeBase* right;
Packit Service 50c9f2
    QMapNodeBase* parent;
Packit Service 50c9f2
Packit Service 50c9f2
    Color color;
Packit Service 50c9f2
Packit Service 50c9f2
    QMapNodeBase* minimum() {
Packit Service 50c9f2
	QMapNodeBase* x = this;
Packit Service 50c9f2
	while ( x->left )
Packit Service 50c9f2
	    x = x->left;
Packit Service 50c9f2
	return x;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapNodeBase* maximum() {
Packit Service 50c9f2
	QMapNodeBase* x = this;
Packit Service 50c9f2
	while ( x->right )
Packit Service 50c9f2
	    x = x->right;
Packit Service 50c9f2
	return x;
Packit Service 50c9f2
    }
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
template <class K, class T>
Packit Service 50c9f2
struct QMapNode : public QMapNodeBase
Packit Service 50c9f2
{
Packit Service 50c9f2
    QMapNode( const K& _key, const T& _data ) { data = _data; key = _key; }
Packit Service 50c9f2
    QMapNode( const K& _key )	   { key = _key; }
Packit Service 50c9f2
    QMapNode( const QMapNode<K,T>& _n ) { key = _n.key; data = _n.data; }
Packit Service 50c9f2
    QMapNode() { }
Packit Service 50c9f2
    T data;
Packit Service 50c9f2
    K key;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
template<class K, class T>
Packit Service 50c9f2
class Q_EXPORT QMapIterator
Packit Service 50c9f2
{
Packit Service 50c9f2
 public:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Typedefs
Packit Service 50c9f2
     */
Packit Service 50c9f2
    typedef QMapNode< K, T >* NodePtr;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Variables
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMapNode<K,T>* node;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Functions
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMapIterator() : node( 0 ) {}
Packit Service 50c9f2
    QMapIterator( QMapNode<K,T>* p ) : node( p ) {}
Packit Service 50c9f2
    QMapIterator( const QMapIterator<K,T>& it ) : node( it.node ) {}
Packit Service 50c9f2
Packit Service 50c9f2
    bool operator==( const QMapIterator<K,T>& it ) const { return node == it.node; }
Packit Service 50c9f2
    bool operator!=( const QMapIterator<K,T>& it ) const { return node != it.node; }
Packit Service 50c9f2
    T& operator*() { return node->data; }
Packit Service 50c9f2
    const T& operator*() const { return node->data; }
Packit Service 50c9f2
Packit Service 50c9f2
    // Cannot have this - some compilers are too stupid
Packit Service 50c9f2
    //T* operator->() const { return &(node->data); }
Packit Service 50c9f2
Packit Service 50c9f2
    const K& key() const { return node->key; }
Packit Service 50c9f2
    T& data() { return node->data; }
Packit Service 50c9f2
    const T& data() const { return node->data; }
Packit Service 50c9f2
Packit Service 50c9f2
private:
Packit Service 50c9f2
    int inc() {
Packit Service 50c9f2
	QMapNodeBase* tmp = node;
Packit Service 50c9f2
	if ( tmp->right ) {
Packit Service 50c9f2
	    tmp = tmp->right;
Packit Service 50c9f2
	    while ( tmp->left )
Packit Service 50c9f2
		tmp = tmp->left;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->parent;
Packit Service 50c9f2
	    while (tmp == y->right) {
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
		y = y->parent;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	    if (tmp->right != y)
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	node = (NodePtr)tmp;
Packit Service 50c9f2
	return 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    int dec() {
Packit Service 50c9f2
	QMapNodeBase* tmp = node;
Packit Service 50c9f2
	if (tmp->color == QMapNodeBase::Red &&
Packit Service 50c9f2
	    tmp->parent->parent == tmp ) {
Packit Service 50c9f2
	    tmp = tmp->right;
Packit Service 50c9f2
	} else if (tmp->left != 0) {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->left;
Packit Service 50c9f2
	    while ( y->right )
Packit Service 50c9f2
		y = y->right;
Packit Service 50c9f2
	    tmp = y;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->parent;
Packit Service 50c9f2
	    while (tmp == y->left) {
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
		y = y->parent;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	    tmp = y;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	node = (NodePtr)tmp;
Packit Service 50c9f2
	return 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
public:
Packit Service 50c9f2
    QMapIterator<K,T>& operator++() {
Packit Service 50c9f2
	inc();
Packit Service 50c9f2
	return *this;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapIterator<K,T> operator++(int) {
Packit Service 50c9f2
	QMapIterator<K,T> tmp = *this;
Packit Service 50c9f2
	inc();
Packit Service 50c9f2
	return tmp;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapIterator<K,T>& operator--() {
Packit Service 50c9f2
	dec();
Packit Service 50c9f2
	return *this;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapIterator<K,T> operator--(int) {
Packit Service 50c9f2
	QMapIterator<K,T> tmp = *this;
Packit Service 50c9f2
	dec();
Packit Service 50c9f2
	return tmp;
Packit Service 50c9f2
    }
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
template<class K, class T>
Packit Service 50c9f2
class Q_EXPORT QMapConstIterator
Packit Service 50c9f2
{
Packit Service 50c9f2
 public:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Typedefs
Packit Service 50c9f2
     */
Packit Service 50c9f2
    typedef QMapNode< K, T >* NodePtr;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Variables
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMapNode<K,T>* node;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Functions
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMapConstIterator() : node( 0 ) {}
Packit Service 50c9f2
    QMapConstIterator( QMapNode<K,T>* p ) : node( p ) {}
Packit Service 50c9f2
    QMapConstIterator( const QMapConstIterator<K,T>& it ) : node( it.node ) {}
Packit Service 50c9f2
    QMapConstIterator( const QMapIterator<K,T>& it ) : node( it.node ) {}
Packit Service 50c9f2
Packit Service 50c9f2
    bool operator==( const QMapConstIterator<K,T>& it ) const { return node == it.node; }
Packit Service 50c9f2
    bool operator!=( const QMapConstIterator<K,T>& it ) const { return node != it.node; }
Packit Service 50c9f2
    const T& operator*()  const { return node->data; }
Packit Service 50c9f2
Packit Service 50c9f2
    // Cannot have this - some compilers are too stupid
Packit Service 50c9f2
    //const T* operator->() const { return &(node->data); }
Packit Service 50c9f2
Packit Service 50c9f2
    const K& key() const { return node->key; }
Packit Service 50c9f2
    const T& data() const { return node->data; }
Packit Service 50c9f2
Packit Service 50c9f2
private:
Packit Service 50c9f2
    int inc() {
Packit Service 50c9f2
        QMapNodeBase* tmp = node;
Packit Service 50c9f2
	if ( tmp->right ) {
Packit Service 50c9f2
	    tmp = tmp->right;
Packit Service 50c9f2
	    while ( tmp->left )
Packit Service 50c9f2
		tmp = tmp->left;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->parent;
Packit Service 50c9f2
	    while (tmp == y->right) {
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
		y = y->parent;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	    if (tmp->right != y)
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	node = (NodePtr)tmp;
Packit Service 50c9f2
	return 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    int dec() {
Packit Service 50c9f2
	QMapNodeBase* tmp = node;
Packit Service 50c9f2
	if (tmp->color == QMapNodeBase::Red &&
Packit Service 50c9f2
	    tmp->parent->parent == tmp ) {
Packit Service 50c9f2
	    tmp = tmp->right;
Packit Service 50c9f2
	} else if (tmp->left != 0) {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->left;
Packit Service 50c9f2
	    while ( y->right )
Packit Service 50c9f2
		y = y->right;
Packit Service 50c9f2
	    tmp = y;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    QMapNodeBase* y = tmp->parent;
Packit Service 50c9f2
	    while (tmp == y->left) {
Packit Service 50c9f2
		tmp = y;
Packit Service 50c9f2
		y = y->parent;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	    tmp = y;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	node = (NodePtr)tmp;
Packit Service 50c9f2
	return 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
public:
Packit Service 50c9f2
    QMapConstIterator<K,T>& operator++() {
Packit Service 50c9f2
	inc();
Packit Service 50c9f2
	return *this;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapConstIterator<K,T> operator++(int) {
Packit Service 50c9f2
	QMapConstIterator<K,T> tmp = *this;
Packit Service 50c9f2
	inc();
Packit Service 50c9f2
	return tmp;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapConstIterator<K,T>& operator--() {
Packit Service 50c9f2
	dec();
Packit Service 50c9f2
	return *this;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    QMapConstIterator<K,T> operator--(int) {
Packit Service 50c9f2
	QMapConstIterator<K,T> tmp = *this;
Packit Service 50c9f2
	dec();
Packit Service 50c9f2
	return tmp;
Packit Service 50c9f2
    }
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
class Q_EXPORT QMapPrivateBase : public QShared
Packit Service 50c9f2
{
Packit Service 50c9f2
public:
Packit Service 50c9f2
    QMapPrivateBase() {
Packit Service 50c9f2
	node_count = 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    QMapPrivateBase( const QMapPrivateBase* _map) {
Packit Service 50c9f2
	node_count = _map->node_count;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Implementations of basic tree algorithms
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void rotateLeft( QMapNodeBase* x, QMapNodeBase*& root);
Packit Service 50c9f2
    void rotateRight( QMapNodeBase* x, QMapNodeBase*& root );
Packit Service 50c9f2
    void rebalance( QMapNodeBase* x, QMapNodeBase*& root );
Packit Service 50c9f2
    QMapNodeBase* removeAndRebalance( QMapNodeBase* z, QMapNodeBase*& root,
Packit Service 50c9f2
				      QMapNodeBase*& leftmost,
Packit Service 50c9f2
				      QMapNodeBase*& rightmost );
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Variables
Packit Service 50c9f2
     */
Packit Service 50c9f2
    int node_count;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
template <class Key, class T>
Packit Service 50c9f2
class QMapPrivate : public QMapPrivateBase
Packit Service 50c9f2
{
Packit Service 50c9f2
public:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Typedefs
Packit Service 50c9f2
     */
Packit Service 50c9f2
    typedef QMapIterator< Key, T > Iterator;
Packit Service 50c9f2
    typedef QMapConstIterator< Key, T > ConstIterator;
Packit Service 50c9f2
    typedef QMapNode< Key, T > Node;
Packit Service 50c9f2
    typedef QMapNode< Key, T >* NodePtr;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Functions
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMapPrivate() {
Packit Service 50c9f2
	header = new Node;
Packit Service 50c9f2
	header->color = QMapNodeBase::Red; // Mark the header
Packit Service 50c9f2
	header->parent = 0;
Packit Service 50c9f2
	header->left = header->right = header;
Packit Service 50c9f2
    }
Packit Service 50c9f2
    QMapPrivate( const QMapPrivate< Key, T >* _map ) : QMapPrivateBase( _map ) {
Packit Service 50c9f2
	header = new Node;
Packit Service 50c9f2
	header->color = QMapNodeBase::Red; // Mark the header
Packit Service 50c9f2
	if ( _map->header->parent == 0 ) {
Packit Service 50c9f2
	    header->parent = 0;
Packit Service 50c9f2
	    header->left = header->right = header;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    header->parent = copy( (NodePtr)(_map->header->parent) );
Packit Service 50c9f2
	    header->parent->parent = header;
Packit Service 50c9f2
	    header->left = header->parent->minimum();
Packit Service 50c9f2
	    header->right = header->parent->maximum();
Packit Service 50c9f2
	}
Packit Service 50c9f2
    }
Packit Service 50c9f2
    ~QMapPrivate() { clear(); delete header; }
Packit Service 50c9f2
Packit Service 50c9f2
    NodePtr copy( NodePtr p ) {
Packit Service 50c9f2
	if ( !p )
Packit Service 50c9f2
	    return 0;
Packit Service 50c9f2
	NodePtr n = new Node( *p );
Packit Service 50c9f2
	n->color = p->color;
Packit Service 50c9f2
	if ( p->left ) {
Packit Service 50c9f2
	    n->left = copy( (NodePtr)(p->left) );
Packit Service 50c9f2
	    n->left->parent = n;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    n->left = 0;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	if ( p->right ) {
Packit Service 50c9f2
	    n->right = copy( (NodePtr)(p->right) );
Packit Service 50c9f2
	    n->right->parent = n;
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    n->right = 0;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	return n;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void clear() {
Packit Service 50c9f2
	clear( (NodePtr)(header->parent) );
Packit Service 50c9f2
	header->color = QMapNodeBase::Red;
Packit Service 50c9f2
	header->parent = 0;
Packit Service 50c9f2
	header->left = header->right = header;
Packit Service 50c9f2
	node_count = 0;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void clear( NodePtr p ) {
Packit Service 50c9f2
	while ( p != 0 ) {
Packit Service 50c9f2
	    clear( (NodePtr)p->right );
Packit Service 50c9f2
	    NodePtr y = (NodePtr)p->left;
Packit Service 50c9f2
	    delete p;
Packit Service 50c9f2
	    p = y;
Packit Service 50c9f2
	}
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator begin()	{ return Iterator( (NodePtr)(header->left ) ); }
Packit Service 50c9f2
    Iterator end()	{ return Iterator( header ); }
Packit Service 50c9f2
    ConstIterator begin() const { return ConstIterator( (NodePtr)(header->left ) ); }
Packit Service 50c9f2
    ConstIterator end() const { return ConstIterator( header ); }
Packit Service 50c9f2
Packit Service 50c9f2
    ConstIterator find(const Key& k) const {
Packit Service 50c9f2
	QMapNodeBase* y = header;        // Last node
Packit Service 50c9f2
	QMapNodeBase* x = header->parent; // Root node.
Packit Service 50c9f2
Packit Service 50c9f2
	while ( x != 0 ) {
Packit Service 50c9f2
	    // If as k <= key(x) go left
Packit Service 50c9f2
	    if ( !( key(x) < k ) ) {
Packit Service 50c9f2
		y = x;
Packit Service 50c9f2
		x = x->left;
Packit Service 50c9f2
	    } else {
Packit Service 50c9f2
		x = x->right;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	}
Packit Service 50c9f2
Packit Service 50c9f2
	// Was k bigger/smaller then the biggest/smallest
Packit Service 50c9f2
	// element of the tree ? Return end()
Packit Service 50c9f2
	if ( y == header || k < key(y) )
Packit Service 50c9f2
	    return ConstIterator( header );
Packit Service 50c9f2
	return ConstIterator( (NodePtr)y );
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void remove( Iterator it ) {
Packit Service 50c9f2
	NodePtr del = (NodePtr) removeAndRebalance( it.node, header->parent, header->left, header->right );
Packit Service 50c9f2
	delete del;
Packit Service 50c9f2
	--node_count;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
#ifdef QT_QMAP_DEBUG
Packit Service 50c9f2
    void inorder( QMapNodeBase* x = 0, int level = 0 ){
Packit Service 50c9f2
	if ( !x )
Packit Service 50c9f2
	    x = header->parent;
Packit Service 50c9f2
	if ( x->left )
Packit Service 50c9f2
	    inorder( x->left, level + 1 );
Packit Service 50c9f2
    //cout << level << " Key=" << key(x) << " Value=" << ((NodePtr)x)->data << endl;
Packit Service 50c9f2
	if ( x->right )
Packit Service 50c9f2
	    inorder( x->right, level + 1 );
Packit Service 50c9f2
    }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator insertMulti(const Key& v){
Packit Service 50c9f2
	QMapNodeBase* y = header;
Packit Service 50c9f2
	QMapNodeBase* x = header->parent;
Packit Service 50c9f2
	while (x != 0){
Packit Service 50c9f2
	    y = x;
Packit Service 50c9f2
	    x = ( v < key(x) ) ? x->left : x->right;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	return insert(x, y, v);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator insertSingle( const Key& k ) {
Packit Service 50c9f2
	// Search correct position in the tree
Packit Service 50c9f2
	QMapNodeBase* y = header;
Packit Service 50c9f2
	QMapNodeBase* x = header->parent;
Packit Service 50c9f2
	bool result = TRUE;
Packit Service 50c9f2
	while ( x != 0 ) {
Packit Service 50c9f2
	    result = ( k < key(x) );
Packit Service 50c9f2
	    y = x;
Packit Service 50c9f2
	    x = result ? x->left : x->right;
Packit Service 50c9f2
	}
Packit Service 50c9f2
	// Get iterator on the last not empty one
Packit Service 50c9f2
	Iterator j( (NodePtr)y );
Packit Service 50c9f2
	if ( result ) {
Packit Service 50c9f2
	    // Smaller then the leftmost one ?
Packit Service 50c9f2
	    if ( j == begin() ) {
Packit Service 50c9f2
		return insert(x, y, k );
Packit Service 50c9f2
	    } else {
Packit Service 50c9f2
		// Perhaps daddy is the right one ?
Packit Service 50c9f2
		--j;
Packit Service 50c9f2
	    }
Packit Service 50c9f2
	}
Packit Service 50c9f2
	// Really bigger ?
Packit Service 50c9f2
	if ( (j.node->key) < k )
Packit Service 50c9f2
	    return insert(x, y, k );
Packit Service 50c9f2
	// We are going to replace a node
Packit Service 50c9f2
	return j;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator insert( QMapNodeBase* x, QMapNodeBase* y, const Key& k ) {
Packit Service 50c9f2
	NodePtr z = new Node( k );
Packit Service 50c9f2
	if (y == header || x != 0 || k < key(y) ) {
Packit Service 50c9f2
	    y->left = z;                // also makes leftmost = z when y == header
Packit Service 50c9f2
	    if ( y == header ) {
Packit Service 50c9f2
		header->parent = z;
Packit Service 50c9f2
		header->right = z;
Packit Service 50c9f2
	    } else if ( y == header->left )
Packit Service 50c9f2
		header->left = z;           // maintain leftmost pointing to min node
Packit Service 50c9f2
	} else {
Packit Service 50c9f2
	    y->right = z;
Packit Service 50c9f2
	    if ( y == header->right )
Packit Service 50c9f2
		header->right = z;          // maintain rightmost pointing to max node
Packit Service 50c9f2
	}
Packit Service 50c9f2
	z->parent = y;
Packit Service 50c9f2
	z->left = 0;
Packit Service 50c9f2
	z->right = 0;
Packit Service 50c9f2
	rebalance( z, header->parent );
Packit Service 50c9f2
	++node_count;
Packit Service 50c9f2
	return Iterator(z);
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
protected:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Helpers
Packit Service 50c9f2
     */
Packit Service 50c9f2
    const Key& key( QMapNodeBase* b ) const { return ((NodePtr)b)->key; }
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Variables
Packit Service 50c9f2
     */
Packit Service 50c9f2
    NodePtr header;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
template<class Key, class T>
Packit Service 50c9f2
class Q_EXPORT QMap
Packit Service 50c9f2
{
Packit Service 50c9f2
public:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Typedefs
Packit Service 50c9f2
     */
Packit Service 50c9f2
    typedef QMapIterator< Key, T > Iterator;
Packit Service 50c9f2
    typedef QMapConstIterator< Key, T > ConstIterator;
Packit Service 50c9f2
    typedef T ValueType;
Packit Service 50c9f2
    typedef QMapPrivate< Key, T > Priv;
Packit Service 50c9f2
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * API
Packit Service 50c9f2
     */
Packit Service 50c9f2
    QMap() { sh = new QMapPrivate< Key, T >; }
Packit Service 50c9f2
    QMap( const QMap<Key,T>& m ) { sh = m.sh; sh->ref(); }
Packit Service 50c9f2
    ~QMap() { if ( sh->deref() ) delete sh; }
Packit Service 50c9f2
Packit Service 50c9f2
    QMap<Key,T>& operator= ( const QMap<Key,T>& m )
Packit Service 50c9f2
      { m.sh->ref(); if ( sh->deref() ) delete sh; sh = m.sh; return *this; }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator begin() { detach(); return sh->begin(); }
Packit Service 50c9f2
    Iterator end() { detach(); return sh->end(); }
Packit Service 50c9f2
    ConstIterator begin() const { return ((const Priv*)sh)->begin(); }
Packit Service 50c9f2
    ConstIterator end() const { return ((const Priv*)sh)->end(); }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator find ( const Key& k )
Packit Service 50c9f2
	{ detach(); return Iterator( sh->find( k ).node ); }
Packit Service 50c9f2
    ConstIterator find ( const Key& k ) const
Packit Service 50c9f2
	{ return sh->find( k ); }
Packit Service 50c9f2
    T& operator[] ( const Key& k ) {
Packit Service 50c9f2
	detach(); QMapNode<Key,T>* p = sh->find( k ).node;
Packit Service 50c9f2
	if ( p != sh->end().node ) return p->data;
Packit Service 50c9f2
	return insert( k, T() ).data(); }
Packit Service 50c9f2
    const T& operator[] ( const Key& k ) const
Packit Service 50c9f2
	{ return sh->find( k ).data(); }
Packit Service 50c9f2
    bool contains ( const Key& k ) const
Packit Service 50c9f2
	{ return find( k ) != end(); }
Packit Service 50c9f2
	//{ return sh->find( k ) != ((const Priv*)sh)->end(); }
Packit Service 50c9f2
Packit Service 50c9f2
    uint count() const { return sh->node_count; }
Packit Service 50c9f2
Packit Service 50c9f2
    bool isEmpty() const { return sh->node_count == 0; }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator insert( const Key& key, const T& value ) {
Packit Service 50c9f2
        detach();
Packit Service 50c9f2
        Iterator it = sh->insertSingle( key );
Packit Service 50c9f2
        it.data() = value;
Packit Service 50c9f2
        return it;
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void remove( Iterator it ) { detach(); sh->remove( it ); }
Packit Service 50c9f2
    void remove( const Key& k ) {
Packit Service 50c9f2
        detach();
Packit Service 50c9f2
        Iterator it( sh->find( k ).node );
Packit Service 50c9f2
        if ( it != end() )
Packit Service 50c9f2
            sh->remove( it );
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    Iterator replace( const Key& k, const T& v ) {
Packit Service 50c9f2
	remove( k );
Packit Service 50c9f2
	return insert( k, v );
Packit Service 50c9f2
    }
Packit Service 50c9f2
Packit Service 50c9f2
    void clear() { if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QMapPrivate<Key,T>; } }
Packit Service 50c9f2
Packit Service 50c9f2
#if defined(Q_FULL_TEMPLATE_INSTANTIATION)
Packit Service 50c9f2
    bool operator==( const QMap<Key,T>& ) const { return FALSE; }
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
protected:
Packit Service 50c9f2
    /**
Packit Service 50c9f2
     * Helpers
Packit Service 50c9f2
     */
Packit Service 50c9f2
    void detach() { if ( sh->count > 1 ) { sh->deref(); sh = new QMapPrivate<Key,T>( sh ); } }
Packit Service 50c9f2
Packit Service 50c9f2
    Priv* sh;
Packit Service 50c9f2
};
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
#ifndef QT_NO_DATASTREAM
Packit Service 50c9f2
template<class Key, class T>
Packit Service 50c9f2
inline QDataStream& operator>>( QDataStream& s, QMap<Key,T>& m ) {
Packit Service 50c9f2
    m.clear();
Packit Service 50c9f2
    Q_UINT32 c;
Packit Service 50c9f2
    s >> c;
Packit Service 50c9f2
    for( Q_UINT32 i = 0; i < c; ++i ) {
Packit Service 50c9f2
	Key k; T t;
Packit Service 50c9f2
	s >> k >> t;
Packit Service 50c9f2
	m.insert( k, t );
Packit Service 50c9f2
    }
Packit Service 50c9f2
    return s;
Packit Service 50c9f2
}
Packit Service 50c9f2
Packit Service 50c9f2
Packit Service 50c9f2
template<class Key, class T>
Packit Service 50c9f2
inline QDataStream& operator<<( QDataStream& s, const QMap<Key,T>& m ) {
Packit Service 50c9f2
    s << (Q_UINT32)m.count();
Packit Service 50c9f2
    QMapConstIterator<Key,T> it = m.begin();
Packit Service 50c9f2
    for( ; it != m.end(); ++it )
Packit Service 50c9f2
	s << it.key() << it.data();
Packit Service 50c9f2
    return s;
Packit Service 50c9f2
}
Packit Service 50c9f2
#endif
Packit Service 50c9f2
Packit Service 50c9f2
#endif // QMAP_H