43 #include "qdatastream.h" 49 enum Color { Red, Black };
73 template <
class K,
class T>
76 QMapNode(
const K& _key,
const T& _data ) { data = _data; key = _key; }
77 QMapNode(
const K& _key ) { key = _key; }
85 template<
class K,
class T>
108 T& operator*() {
return node->data; }
109 const T& operator*()
const {
return node->data; }
114 const K& key()
const {
return node->key; }
115 T& data() {
return node->data; }
116 const T& data()
const {
return node->data; }
127 while (tmp == y->right) {
140 if (tmp->color == QMapNodeBase::Red &&
141 tmp->parent->parent == tmp ) {
143 }
else if (tmp->left != 0) {
150 while (tmp == y->left) {
184 template<
class K,
class T>
208 const T& operator*()
const {
return node->data; }
213 const K& key()
const {
return node->key; }
214 const T& data()
const {
return node->data; }
225 while (tmp == y->right) {
238 if (tmp->color == QMapNodeBase::Red &&
239 tmp->parent->parent == tmp ) {
241 }
else if (tmp->left != 0) {
248 while (tmp == y->left) {
310 template <
class Key,
class T>
327 header->color = QMapNodeBase::Red;
329 header->left = header->right = header;
333 header->color = QMapNodeBase::Red;
334 if ( _map->
header->parent == 0 ) {
336 header->left = header->right = header;
338 header->parent = copy( (NodePtr)(_map->
header->parent) );
339 header->parent->parent = header;
340 header->left = header->parent->minimum();
341 header->right = header->parent->maximum();
346 NodePtr copy( NodePtr p ) {
349 NodePtr n =
new Node( *p );
352 n->left = copy( (NodePtr)(p->left) );
358 n->right = copy( (NodePtr)(p->right) );
359 n->right->parent = n;
367 clear( (NodePtr)(header->parent) );
368 header->color = QMapNodeBase::Red;
370 header->left = header->right = header;
374 void clear( NodePtr p ) {
376 clear( (NodePtr)p->right );
377 NodePtr y = (NodePtr)p->left;
383 Iterator begin() {
return Iterator( (NodePtr)(header->left ) ); }
384 Iterator end() {
return Iterator( header ); }
385 ConstIterator begin()
const {
return ConstIterator( (NodePtr)(header->left ) ); }
386 ConstIterator end()
const {
return ConstIterator( header ); }
388 ConstIterator find(
const Key& k)
const {
394 if ( !( key(x) < k ) ) {
404 if ( y == header || k < key(y) )
405 return ConstIterator( header );
406 return ConstIterator( (NodePtr)y );
409 void remove( Iterator it ) {
410 NodePtr del = (NodePtr) removeAndRebalance( it.node, header->parent, header->left, header->right );
420 inorder( x->left, level + 1 );
423 inorder( x->right, level + 1 );
427 Iterator insertMulti(
const Key& v){
432 x = ( v < key(x) ) ? x->left : x->right;
434 return insert(x, y, v);
437 Iterator insertSingle(
const Key& k ) {
443 result = ( k < key(x) );
445 x = result ? x->left : x->right;
448 Iterator j( (NodePtr)y );
451 if ( j == begin() ) {
452 return insert(x, y, k );
459 if ( (j.
node->key) < k )
460 return insert(x, y, k );
466 NodePtr z =
new Node( k );
467 if (y == header || x != 0 || k < key(y) ) {
472 }
else if ( y == header->left )
476 if ( y == header->right )
482 rebalance( z, header->parent );
500 template<
class Key,
class T>
517 ~
QMap() {
if ( sh->deref() )
delete sh; }
520 { m.sh->ref();
if ( sh->deref() )
delete sh; sh = m.sh;
return *
this; }
522 Iterator begin() { detach();
return sh->begin(); }
523 Iterator end() { detach();
return sh->end(); }
524 ConstIterator begin()
const {
return ((
const Priv*)sh)->begin(); }
525 ConstIterator end()
const {
return ((
const Priv*)sh)->end(); }
527 Iterator find (
const Key& k )
528 { detach();
return Iterator( sh->find( k ).node ); }
529 ConstIterator find (
const Key& k )
const 530 {
return sh->find( k ); }
531 T& operator[] (
const Key& k ) {
533 if ( p != sh->end().node )
return p->data;
534 return insert( k, T() ).data(); }
535 const T& operator[] (
const Key& k )
const 536 {
return sh->find( k ).data(); }
537 bool contains (
const Key& k )
const 538 {
return find( k ) != end(); }
541 uint count()
const {
return sh->node_count; }
543 bool isEmpty()
const {
return sh->node_count == 0; }
545 Iterator insert(
const Key& key,
const T& value ) {
547 Iterator it = sh->insertSingle( key );
552 void remove( Iterator it ) { detach(); sh->remove( it ); }
553 void remove(
const Key& k ) {
555 Iterator it( sh->find( k ).node );
560 Iterator replace(
const Key& k,
const T& v ) {
562 return insert( k, v );
565 void clear() {
if ( sh->count == 1 ) sh->clear();
else { sh->deref(); sh =
new QMapPrivate<Key,T>; } }
567 #if defined(Q_FULL_TEMPLATE_INSTANTIATION) 568 bool operator==(
const QMap<Key,T>& )
const {
return FALSE; }
581 #ifndef QT_NO_DATASTREAM 582 template<
class Key,
class T>
587 for( Q_UINT32 i = 0; i < c; ++i ) {
596 template<
class Key,
class T>
597 inline QDataStream& operator<<( QDataStream& s, const QMap<Key,T>& m ) {
598 s << (Q_UINT32)m.count();
600 for( ; it != m.end(); ++it )
601 s << it.key() << it.data();
QMapIterator< Key, T > Iterator
Typedefs.
Definition: qmap.h:507
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
Helper struct representing a RGBA color.
Definition: image.cpp:28
QMapIterator< Key, T > Iterator
Typedefs.
Definition: qmap.h:317
QMapNode< K, T > * node
Variables.
Definition: qmap.h:97
QMapNode< K, T > * NodePtr
Typedefs.
Definition: qmap.h:191
NodePtr header
Variables.
Definition: qmap.h:496
QMap()
API.
Definition: qmap.h:515
void detach()
Helpers.
Definition: qmap.h:575
QMapPrivate()
Functions.
Definition: qmap.h:325
int node_count
Variables.
Definition: qmap.h:306
const Key & key(QMapNodeBase *b) const
Helpers.
Definition: qmap.h:491
QMapNode< K, T > * node
Variables.
Definition: qmap.h:196
The QShared struct is internally used for implementing shared classes.
Definition: qshared.h:46
QMapIterator()
Functions.
Definition: qmap.h:102
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:47
QMapConstIterator()
Functions.
Definition: qmap.h:201
QMapNode< K, T > * NodePtr
Typedefs.
Definition: qmap.h:92