qmap-h.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 918 行 · 第 1/2 页
HTML
918 行
* Variables */ NodePtr header;};template <class Key, class T>Q_INLINE_TEMPLATES QMapPrivate<Key,T>::QMapPrivate() { header = new Node; header->color = QMapNodeBase::Red; // Mark the header header->parent = 0; header->left = header->right = header;}template <class Key, class T>Q_INLINE_TEMPLATES QMapPrivate<Key,T>::QMapPrivate( const QMapPrivate< Key, T >* _map ) : QMapPrivateBase( _map ) { header = new Node; header->color = QMapNodeBase::Red; // Mark the header if ( _map->header->parent == 0 ) { header->parent = 0; header->left = header->right = header; } else { header->parent = copy( (NodePtr)(_map->header->parent) ); header->parent->parent = header; header->left = header->parent->minimum(); header->right = header->parent->maximum(); }}template <class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMapPrivate<Key,T>::NodePtr QMapPrivate<Key,T>::copy( Q_TYPENAME QMapPrivate<Key,T>::NodePtr p ){ if ( !p ) return 0; NodePtr n = new Node( *p ); n->color = p->color; if ( p->left ) { n->left = copy( (NodePtr)(p->left) ); n->left->parent = n; } else { n->left = 0; } if ( p->right ) { n->right = copy( (NodePtr)(p->right) ); n->right->parent = n; } else { n->right = 0; } return n;}template <class Key, class T>Q_INLINE_TEMPLATES void QMapPrivate<Key,T>::clear(){ clear( (NodePtr)(header->parent) ); header->color = QMapNodeBase::Red; header->parent = 0; header->left = header->right = header; node_count = 0;}template <class Key, class T>Q_INLINE_TEMPLATES void QMapPrivate<Key,T>::clear( Q_TYPENAME QMapPrivate<Key,T>::NodePtr p ){ while ( p != 0 ) { clear( (NodePtr)p->right ); NodePtr y = (NodePtr)p->left; delete p; p = y; }}template <class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMapPrivate<Key,T>::ConstIterator QMapPrivate<Key,T>::find(const Key& k) const{ QMapNodeBase* y = header; // Last node QMapNodeBase* x = header->parent; // Root node. while ( x != 0 ) { // If as k <= key(x) go left if ( !( key(x) < k ) ) { y = x; x = x->left; } else { x = x->right; } } // Was k bigger/smaller then the biggest/smallest // element of the tree ? Return end() if ( y == header || k < key(y) ) return ConstIterator( header ); return ConstIterator( (NodePtr)y );}template <class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMapPrivate<Key,T>::Iterator QMapPrivate<Key,T>::insertSingle( const Key& k ){ // Search correct position in the tree QMapNodeBase* y = header; QMapNodeBase* x = header->parent; bool result = TRUE; while ( x != 0 ) { result = ( k < key(x) ); y = x; x = result ? x->left : x->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->key) < k ) return insert(x, y, k ); // We are going to replace a node return j;}template <class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMapPrivate<Key,T>::Iterator QMapPrivate<Key,T>::insert( QMapNodeBase* x, QMapNodeBase* y, const Key& k ){ NodePtr z = new Node( k ); if (y == header || x != 0 || k < key(y) ) { y->left = z; // also makes leftmost = z when y == header if ( y == header ) { header->parent = z; header->right = z; } else if ( y == header->left ) header->left = z; // maintain leftmost pointing to min node } else { y->right = z; if ( y == header->right ) header->right = z; // maintain rightmost pointing to max node } z->parent = y; z->left = 0; z->right = 0; rebalance( z, header->parent ); ++node_count; return Iterator(z);}#ifdef QT_CHECK_RANGE# if !defined( QT_NO_DEBUG ) && defined( QT_CHECK_MAP_RANGE )# define QT_CHECK_INVALID_MAP_ELEMENT if ( empty() ) qWarning( "QMap: Warning invalid element" )# define QT_CHECK_INVALID_MAP_ELEMENT_FATAL Q_ASSERT( !empty() );# else# define QT_CHECK_INVALID_MAP_ELEMENT# define QT_CHECK_INVALID_MAP_ELEMENT_FATAL# endif#else# define QT_CHECK_INVALID_MAP_ELEMENT# define QT_CHECK_INVALID_MAP_ELEMENT_FATAL#endiftemplate<class Key, class T>class QMap{public: /** * Typedefs */ typedef Key key_type; typedef T mapped_type; typedef QPair<const key_type, mapped_type> value_type; typedef value_type* pointer; typedef const value_type* const_pointer; typedef value_type& reference; typedef const value_type& const_reference;#ifndef QT_NO_STL typedef ptrdiff_t difference_type;#else typedef int difference_type;#endif typedef size_t size_type; typedef QMapIterator<Key,T> iterator; typedef QMapConstIterator<Key,T> const_iterator; typedef QPair<iterator,bool> insert_pair; /** * API */ QMap() { sh = new QMapPrivate< Key, T >; } QMap( const QMap<Key,T>& m ) { sh = m.sh; sh->ref(); }#ifndef QT_NO_STL QMap( const Q_TYPENAME std::map<Key,T>& m ) { sh = new QMapPrivate<Key,T>;#if defined(Q_OS_WIN32) std::map<Key,T>::const_iterator it = m.begin();#else QMapConstIterator<Key,T> it = m.begin();#endif for ( ; it != m.end(); ++it ) { value_type p( (*it).first, (*it).second ); insert( p ); } }#endif ~QMap() { if ( sh->deref() ) delete sh; } QMap<Key,T>& operator= ( const QMap<Key,T>& m );#ifndef QT_NO_STL QMap<Key,T>& operator= ( const Q_TYPENAME std::map<Key,T>& m ) { clear();#if defined(Q_OS_WIN32) std::map<Key,T>::const_iterator it = m.begin();#else QMapConstIterator<Key,T> it = m.begin();#endif for ( ; it != m.end(); ++it ) { value_type p( (*it).first, (*it).second ); insert( p ); } return *this; }#endif iterator begin() { detach(); return sh->begin(); } iterator end() { detach(); return sh->end(); } const_iterator begin() const { return ((const Priv*)sh)->begin(); } const_iterator end() const { return ((const Priv*)sh)->end(); } iterator replace( const Key& k, const T& v ) { remove( k ); return insert( k, v ); } size_type size() const { return sh->node_count; } bool empty() const { return sh->node_count == 0; } QPair<iterator,bool> insert( const value_type& x ); void erase( iterator it ) { detach(); sh->remove( it ); } void erase( const key_type& k ); size_type count( const key_type& k ) const; T& operator[] ( const Key& k ); void clear(); typedef QMapIterator< Key, T > Iterator; typedef QMapConstIterator< Key, T > ConstIterator; typedef T ValueType; typedef QMapPrivate< Key, T > Priv; iterator find ( const Key& k ) { detach(); return iterator( sh->find( k ).node ); } const_iterator find ( const Key& k ) const { return sh->find( k ); } const T& operator[] ( const Key& k ) const { QT_CHECK_INVALID_MAP_ELEMENT; return sh->find( k ).data(); } bool contains ( const Key& k ) const { return find( k ) != end(); } //{ return sh->find( k ) != ((const Priv*)sh)->end(); } size_type count() const { return sh->node_count; } QValueList<Key> keys() const { QValueList<Key> r; for (const_iterator i=begin(); i!=end(); ++i) r.append(i.key()); return r; } QValueList<T> values() const { QValueList<T> r; for (const_iterator i=begin(); i!=end(); ++i) r.append(*i); return r; } bool isEmpty() const { return sh->node_count == 0; } iterator insert( const Key& key, const T& value, bool overwrite = TRUE ); void remove( iterator it ) { detach(); sh->remove( it ); } void remove( const Key& k );#if defined(Q_FULL_TEMPLATE_INSTANTIATION) bool operator==( const QMap<Key,T>& ) const { return FALSE; }#ifndef QT_NO_STL bool operator==( const Q_TYPENAME std::map<Key,T>& ) const { return FALSE; }#endif#endifprotected: /** * Helpers */ void detach() { if ( sh->count > 1 ) detachInternal(); } Priv* sh;private: void detachInternal();};template<class Key, class T>Q_INLINE_TEMPLATES QMap<Key,T>& QMap<Key,T>::operator= ( const QMap<Key,T>& m ){ m.sh->ref(); if ( sh->deref() ) delete sh; sh = m.sh; return *this;}template<class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMap<Key,T>::insert_pair QMap<Key,T>::insert( const Q_TYPENAME QMap<Key,T>::value_type& x ){ detach(); size_type n = size(); iterator it = sh->insertSingle( x.first ); bool inserted = FALSE; if ( n < size() ) { inserted = TRUE; it.data() = x.second; } return QPair<iterator,bool>( it, inserted );}template<class Key, class T>Q_INLINE_TEMPLATES void QMap<Key,T>::erase( const Key& k ){ detach(); iterator it( sh->find( k ).node ); if ( it != end() ) sh->remove( it );}template<class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMap<Key,T>::size_type QMap<Key,T>::count( const Key& k ) const{ const_iterator it( sh->find( k ).node ); if ( it != end() ) { size_type c = 0; while ( it != end() ) { ++it; ++c; } return c; } return 0;}template<class Key, class T>Q_INLINE_TEMPLATES T& QMap<Key,T>::operator[] ( const Key& k ){ detach(); QMapNode<Key,T>* p = sh->find( k ).node; if ( p != sh->end().node ) return p->data; return insert( k, T() ).data();}template<class Key, class T>Q_INLINE_TEMPLATES void QMap<Key,T>::clear(){ if ( sh->count == 1 ) sh->clear(); else { sh->deref(); sh = new QMapPrivate<Key,T>; }}template<class Key, class T>Q_INLINE_TEMPLATES Q_TYPENAME QMap<Key,T>::iterator QMap<Key,T>::insert( const Key& key, const T& value, bool overwrite ){ detach(); size_type n = size(); iterator it = sh->insertSingle( key ); if ( overwrite || n < size() ) it.data() = value; return it;}template<class Key, class T>Q_INLINE_TEMPLATES void QMap<Key,T>::remove( const Key& k ){ detach(); iterator it( sh->find( k ).node ); if ( it != end() ) sh->remove( it );}template<class Key, class T>Q_INLINE_TEMPLATES void QMap<Key,T>::detachInternal(){ sh->deref(); sh = new QMapPrivate<Key,T>( sh );}#ifndef QT_NO_DATASTREAMtemplate<class Key, class T>Q_INLINE_TEMPLATES QDataStream& operator>>( QDataStream& s, QMap<Key,T>& m ) { m.clear(); Q_UINT32 c; s >> c; for( Q_UINT32 i = 0; i < c; ++i ) { Key k; T t; s >> k >> t; m.insert( k, t ); if ( s.atEnd() ) break; } return s;}template<class Key, class T>Q_INLINE_TEMPLATES QDataStream& operator<<( QDataStream& s, const QMap<Key,T>& m ) { s << (Q_UINT32)m.size(); QMapConstIterator<Key,T> it = m.begin(); for( ; it != m.end(); ++it ) s << it.key() << it.data(); return s;}#endif#endif // QMAP_H</pre><!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a><td align=right><div align=right>Qt version 3.0.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?