⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qmap-h.html

📁 qtopiaphone英文帮助,用于初学者和开发人员,初学者可以用来学习,开发人员可以用来资料查询.
💻 HTML
📖 第 1 页 / 共 2 页
字号:
    }    /**     * Implementations of basic tree algorithms     */    void rotateLeft( QMapNodeBase* x, QMapNodeBase*&amp; root);    void rotateRight( QMapNodeBase* x, QMapNodeBase*&amp; root );    void rebalance( QMapNodeBase* x, QMapNodeBase*&amp; root );    QMapNodeBase* removeAndRebalance( QMapNodeBase* z, QMapNodeBase*&amp; root,                                      QMapNodeBase*&amp; leftmost,                                      QMapNodeBase*&amp; rightmost );    /**     * Variables     */    int node_count;};template &lt;class Key, class T&gt;class QMapPrivate : public QMapPrivateBase{public:    /**     * Typedefs     */    typedef QMapIterator&lt; Key, T &gt; Iterator;    typedef QMapConstIterator&lt; Key, T &gt; ConstIterator;    typedef QMapNode&lt; Key, T &gt; Node;    typedef QMapNode&lt; Key, T &gt;* NodePtr;    /**     * Functions     */    QMapPrivate() {        header = new Node;        header-&gt;color = QMapNodeBase::Red; // Mark the header        header-&gt;parent = 0;        header-&gt;left = header-&gt;right = header;    }    QMapPrivate( const QMapPrivate&lt; Key, T &gt;* _map ) : QMapPrivateBase( _map ) {        header = new Node;        header-&gt;color = QMapNodeBase::Red; // Mark the header        if ( _map-&gt;header-&gt;parent == 0 ) {            header-&gt;parent = 0;            header-&gt;left = header-&gt;right = header;        } else {            header-&gt;parent = copy( (NodePtr)(_map-&gt;header-&gt;parent) );            header-&gt;parent-&gt;parent = header;            header-&gt;left = header-&gt;parent-&gt;minimum();            header-&gt;right = header-&gt;parent-&gt;maximum();        }    }    ~QMapPrivate() { clear(); delete header; }    NodePtr copy( NodePtr p ) {        if ( !p )            return 0;        NodePtr n = new Node( *p );        n-&gt;color = p-&gt;color;        if ( p-&gt;left ) {            n-&gt;left = copy( (NodePtr)(p-&gt;left) );            n-&gt;left-&gt;parent = n;        } else {            n-&gt;left = 0;        }        if ( p-&gt;right ) {            n-&gt;right = copy( (NodePtr)(p-&gt;right) );            n-&gt;right-&gt;parent = n;        } else {            n-&gt;right = 0;        }        return n;    }    void clear() {        clear( (NodePtr)(header-&gt;parent) );        header-&gt;color = QMapNodeBase::Red;        header-&gt;parent = 0;        header-&gt;left = header-&gt;right = header;        node_count = 0;    }    void clear( NodePtr p ) {        while ( p != 0 ) {            clear( (NodePtr)p-&gt;right );            NodePtr y = (NodePtr)p-&gt;left;            delete p;            p = y;        }    }    Iterator begin()    { return Iterator( (NodePtr)(header-&gt;left ) ); }    Iterator end()      { return Iterator( header ); }    ConstIterator begin() const { return ConstIterator( (NodePtr)(header-&gt;left ) ); }    ConstIterator end() const { return ConstIterator( header ); }    ConstIterator find(const Key&amp; k) const {        QMapNodeBase* y = header;        // Last node        QMapNodeBase* x = header-&gt;parent; // Root node.        while ( x != 0 ) {            // If as k &lt;= key(x) go left            if ( !( key(x) &lt; k ) ) {                y = x;                x = x-&gt;left;            } else {                x = x-&gt;right;            }        }        // Was k bigger/smaller then the biggest/smallest        // element of the tree ? Return end()        if ( y == header || k &lt; key(y) )            return ConstIterator( header );        return ConstIterator( (NodePtr)y );    }    void remove( Iterator it ) {        NodePtr del = (NodePtr) removeAndRebalance( it.node, header-&gt;parent, header-&gt;left, header-&gt;right );        delete del;        --node_count;    }#ifdef QT_QMAP_DEBUG    void inorder( QMapNodeBase* x = 0, int level = 0 ){        if ( !x )            x = header-&gt;parent;        if ( x-&gt;left )            inorder( x-&gt;left, level + 1 );    //cout &lt;&lt; level &lt;&lt; " Key=" &lt;&lt; key(x) &lt;&lt; " Value=" &lt;&lt; ((NodePtr)x)-&gt;data &lt;&lt; endl;        if ( x-&gt;right )            inorder( x-&gt;right, level + 1 );    }#endif    Iterator insertMulti(const Key&amp; v){        QMapNodeBase* y = header;        QMapNodeBase* x = header-&gt;parent;        while (x != 0){            y = x;            x = ( v &lt; key(x) ) ? x-&gt;left : x-&gt;right;        }        return insert(x, y, v);    }    Iterator insertSingle( const Key&amp; k ) {        // Search correct position in the tree        QMapNodeBase* y = header;        QMapNodeBase* x = header-&gt;parent;        bool result = TRUE;        while ( x != 0 ) {            result = ( k &lt; key(x) );            y = x;            x = result ? x-&gt;left : x-&gt;right;        }        // Get iterator on the last not empty one        Iterator j( (NodePtr)y );        if ( result ) {            // Smaller then the leftmost one ?            if ( j == begin() ) {                return insert(x, y, k );            } else {                // Perhaps daddy is the right one ?                --j;            }        }        // Really bigger ?        if ( (j.node-&gt;key) &lt; k )            return insert(x, y, k );        // We are going to replace a node        return j;    }    Iterator insert( QMapNodeBase* x, QMapNodeBase* y, const Key&amp; k ) {        NodePtr z = new Node( k );        if (y == header || x != 0 || k &lt; key(y) ) {            y-&gt;left = z;                // also makes leftmost = z when y == header            if ( y == header ) {                header-&gt;parent = z;                header-&gt;right = z;            } else if ( y == header-&gt;left )                header-&gt;left = z;           // maintain leftmost pointing to min node        } else {            y-&gt;right = z;            if ( y == header-&gt;right )                header-&gt;right = z;          // maintain rightmost pointing to max node        }        z-&gt;parent = y;        z-&gt;left = 0;        z-&gt;right = 0;        rebalance( z, header-&gt;parent );        ++node_count;        return Iterator(z);    }protected:    /**     * Helpers     */    const Key&amp; key( QMapNodeBase* b ) const { return ((NodePtr)b)-&gt;key; }    /**     * Variables     */    NodePtr header;};template&lt;class Key, class T&gt;class Q_EXPORT <a href="qmap.html">QMap</a>{public:    /**     * Typedefs     */    typedef QMapIterator&lt; Key, T &gt; Iterator;    typedef QMapConstIterator&lt; Key, T &gt; ConstIterator;    typedef T ValueType;    typedef QMapPrivate&lt; Key, T &gt; Priv;    /**     * API     */    QMap() { sh = new QMapPrivate&lt; Key, T &gt;; }    QMap( const QMap&lt;Key,T&gt;&amp; m ) { sh = m.sh; sh-&gt;ref(); }    ~QMap() { if ( sh-&gt;deref() ) delete sh; }    QMap&lt;Key,T&gt;&amp; operator= ( const QMap&lt;Key,T&gt;&amp; m )      { m.sh-&gt;ref(); if ( sh-&gt;deref() ) delete sh; sh = m.sh; return *this; }    Iterator begin() { detach(); return sh-&gt;begin(); }    Iterator end() { detach(); return sh-&gt;end(); }    ConstIterator begin() const { return ((const Priv*)sh)-&gt;begin(); }    ConstIterator end() const { return ((const Priv*)sh)-&gt;end(); }    Iterator find ( const Key&amp; k )        { detach(); return Iterator( sh-&gt;find( k ).node ); }    ConstIterator find ( const Key&amp; k ) const        { return sh-&gt;find( k ); }    T&amp; operator[] ( const Key&amp; k ) {        detach(); QMapNode&lt;Key,T&gt;* p = sh-&gt;find( k ).node;        if ( p != sh-&gt;end().node ) return p-&gt;data;        return insert( k, T() ).data(); }    const T&amp; operator[] ( const Key&amp; k ) const        { return sh-&gt;find( k ).data(); }    bool contains ( const Key&amp; k ) const        { return find( k ) != end(); }        //{ return sh-&gt;find( k ) != ((const Priv*)sh)-&gt;end(); }    uint count() const { return sh-&gt;node_count; }    bool isEmpty() const { return sh-&gt;node_count == 0; }    Iterator insert( const Key&amp; key, const T&amp; value ) {        detach();        Iterator it = sh-&gt;insertSingle( key );        it.data() = value;        return it;    }    void remove( Iterator it ) { detach(); sh-&gt;remove( it ); }    void remove( const Key&amp; k ) {        detach();        Iterator it( sh-&gt;find( k ).node );        if ( it != end() )            sh-&gt;remove( it );    }    Iterator replace( const Key&amp; k, const T&amp; v ) {        remove( k );        return insert( k, v );    }    void clear() { if ( sh-&gt;count == 1 ) sh-&gt;clear(); else { sh-&gt;deref(); sh = new QMapPrivate&lt;Key,T&gt;; } }#if defined(Q_FULL_TEMPLATE_INSTANTIATION)    bool operator==( const QMap&lt;Key,T&gt;&amp; ) const { return FALSE; }#endifprotected:    /**     * Helpers     */    void detach() { if ( sh-&gt;count &gt; 1 ) { sh-&gt;deref(); sh = new QMapPrivate&lt;Key,T&gt;( sh ); } }    Priv* sh;};#ifndef QT_NO_DATASTREAMtemplate&lt;class Key, class T&gt;inline QDataStream&amp; operator&gt;&gt;( QDataStream&amp; s, QMap&lt;Key,T&gt;&amp; m ) {    m.clear();    Q_UINT32 c;    s &gt;&gt; c;    for( Q_UINT32 i = 0; i &lt; c; ++i ) {        Key k; T t;        s &gt;&gt; k &gt;&gt; t;        m.insert( k, t );        if ( s.atEnd() )            break;    }    return s;}template&lt;class Key, class T&gt;inline QDataStream&amp; operator&lt;&lt;( QDataStream&amp; s, const QMap&lt;Key,T&gt;&amp; m ) {    s &lt;&lt; (Q_UINT32)m.count();    QMapConstIterator&lt;Key,T&gt; it = m.begin();    for( ; it != m.end(); ++it )        s &lt;&lt; it.key() &lt;&lt; it.data();    return s;}#endif#endif // QMAP_H</pre><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -