|
Packit |
1c1d7e |
/****************************************************************************
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** QPtrDict and QPtrDictIterator class documentation
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
|
Packit |
1c1d7e |
**
|
|
Packit |
1c1d7e |
** This file is part 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 |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*****************************************************************************
|
|
Packit |
1c1d7e |
QPtrDict documentation
|
|
Packit |
1c1d7e |
*****************************************************************************/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\class QPtrDict qptrdict.h
|
|
Packit |
1c1d7e |
\brief The QPtrDict class is a template class that provides a dictionary based on \c void* keys.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\ingroup collection
|
|
Packit |
1c1d7e |
\ingroup tools
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDict is implemented as a template class. Define a
|
|
Packit |
1c1d7e |
template instance QPtrDict\<X\> to create a dictionary that operates on
|
|
Packit |
1c1d7e |
pointers to X, or X*.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
A dictionary is a collection that associates an item with a key.
|
|
Packit |
1c1d7e |
The key is used for inserting and looking up an item. QPtrDict has
|
|
Packit |
1c1d7e |
\c void* keys.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The dictionary has very fast insertion and lookup.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Example:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
#include <qptrdict.h>
|
|
Packit |
1c1d7e |
#include <stdio.h>
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void main()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
int *a = new int[12];
|
|
Packit |
1c1d7e |
int *b = new int[10];
|
|
Packit |
1c1d7e |
int *c = new int[18];
|
|
Packit |
1c1d7e |
int *d = new int[13];
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDict<char> dict; // maps void* -> char*
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
dict.insert( a, "a is int[12]" ); // describe pointers
|
|
Packit |
1c1d7e |
dict.insert( b, "b is int[10]" );
|
|
Packit |
1c1d7e |
dict.insert( c, "c is int[18]" );
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
printf( "%s\n", dict[a] ); // print descriptions
|
|
Packit |
1c1d7e |
printf( "%s\n", dict[b] );
|
|
Packit |
1c1d7e |
printf( "%s\n", dict[c] );
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
if ( !dict[d] )
|
|
Packit |
1c1d7e |
printf( "d not in dictionary\n" );
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Program output:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
a is int[12]
|
|
Packit |
1c1d7e |
b is int[10]
|
|
Packit |
1c1d7e |
c is int[18]
|
|
Packit |
1c1d7e |
d not in dictionary
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The dictionary in our example maps \c int* keys to \c char* items.
|
|
Packit |
1c1d7e |
QPtrDict implements the \link operator[] [] operator\endlink to lookup
|
|
Packit |
1c1d7e |
an item.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDict is implemented by QGDict as a hash array with a fixed number of
|
|
Packit |
1c1d7e |
entries. Each array entry points to a singly linked list of buckets, in
|
|
Packit |
1c1d7e |
which the dictionary items are stored.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
When an item is inserted with a key, the key is converted (hashed) to
|
|
Packit |
1c1d7e |
an integer index into the hash array using the \c mod operation. The
|
|
Packit |
1c1d7e |
item is inserted before the first bucket in the list of buckets.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Looking up an item is normally very fast. The key is again hashed to an
|
|
Packit |
1c1d7e |
array index. Then QPtrDict scans the list of buckets and returns the item
|
|
Packit |
1c1d7e |
found or null if the item was not found. You cannot insert null pointers
|
|
Packit |
1c1d7e |
into a dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The size of the hash array is very important. In order to get good
|
|
Packit |
1c1d7e |
performance, you should use a suitably large \link primes.html prime
|
|
Packit |
1c1d7e |
number\endlink. Suitable means equal to or larger than the maximum
|
|
Packit |
1c1d7e |
expected number of dictionary items.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Items with equal keys are allowed. When inserting two items with the
|
|
Packit |
1c1d7e |
same key, only the last inserted item will be visible (last in, first out)
|
|
Packit |
1c1d7e |
until it is removed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Example:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
#include <qptrdict.h>
|
|
Packit |
1c1d7e |
#include <stdio.h>
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void main()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
QPtrDict<char> dict; // maps char* ==> char*
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
double *ptr = new double[28];
|
|
Packit |
1c1d7e |
dict.insert( ptr, "first" );
|
|
Packit |
1c1d7e |
dict.insert( ptr, "second" );
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
printf( "%s\n", dict[ptr] );
|
|
Packit |
1c1d7e |
dict.remove( ptr );
|
|
Packit |
1c1d7e |
printf( "%s\n", dict[ptr] );
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Program output:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
second
|
|
Packit |
1c1d7e |
first
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The QPtrDictIterator class can traverse the dictionary contents, but only
|
|
Packit |
1c1d7e |
in an arbitrary order. Multiple iterators may independently traverse the
|
|
Packit |
1c1d7e |
same dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Calling setAutoDelete(TRUE) for a dictionary tells it to delete items
|
|
Packit |
1c1d7e |
that are removed . The default is to not delete items when they are
|
|
Packit |
1c1d7e |
removed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
When inserting an item into a dictionary, only the pointer is copied, not
|
|
Packit |
1c1d7e |
the item itself. This is called a shallow copy. It is possible to make the
|
|
Packit |
1c1d7e |
dictionary copy all of the item's data (known as a deep copy) when an
|
|
Packit |
1c1d7e |
item is inserted. insert() calls the virtual function
|
|
Packit |
1c1d7e |
QCollection::newItem() for the item to be inserted.
|
|
Packit |
1c1d7e |
Inherit a dictionary and reimplement it if you want deep copies.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
When removing a dictionary item, the virtual function
|
|
Packit |
1c1d7e |
QCollection::deleteItem() is called. QPtrDict's default implementation
|
|
Packit |
1c1d7e |
is to delete the item if auto-deletion is enabled.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa QPtrDictIterator, QDict, QAsciiDict, QIntDict,
|
|
Packit |
1c1d7e |
\link collection.html Collection Classes\endlink
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDict::QPtrDict( int size )
|
|
Packit |
1c1d7e |
Constructs a dictionary using an internal hash array with the size
|
|
Packit |
1c1d7e |
\e size.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Setting \e size to a suitably large \link primes.html prime number\endlink
|
|
Packit |
1c1d7e |
(equal to or greater than the expected number of entries) makes the hash
|
|
Packit |
1c1d7e |
distribution better and hence the loopup faster.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDict::QPtrDict( const QPtrDict<type> &dict )
|
|
Packit |
1c1d7e |
Constructs a copy of \e dict.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Each item in \e dict are inserted into this dictionary.
|
|
Packit |
1c1d7e |
Only the pointers are copied (shallow copy).
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDict::~QPtrDict()
|
|
Packit |
1c1d7e |
Removes all items from the dictionary and destroys it.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
All iterators that access this dictionary will be reset.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa setAutoDelete()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDict<type> &QPtrDict::operator=(const QPtrDict<type> &dict)
|
|
Packit |
1c1d7e |
Assigns \e dict to this dictionary and returns a reference to this
|
|
Packit |
1c1d7e |
dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This dictionary is first cleared, then each item in \e dict is inserted
|
|
Packit |
1c1d7e |
into this dictionary.
|
|
Packit |
1c1d7e |
Only the pointers are copied (shallow copy), unless newItem() has been
|
|
Packit |
1c1d7e |
reimplemented().
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn uint QPtrDict::count() const
|
|
Packit |
1c1d7e |
Returns the number of items in the dictionary.
|
|
Packit |
1c1d7e |
\sa isEmpty()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn uint QPtrDict::size() const
|
|
Packit |
1c1d7e |
Returns the size of the internal hash array (as specified in the
|
|
Packit |
1c1d7e |
constructor).
|
|
Packit |
1c1d7e |
\sa count()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QPtrDict::resize( uint newsize )
|
|
Packit |
1c1d7e |
Changes the size of the hashtable the \a newsize.
|
|
Packit |
1c1d7e |
The contents of the dictionary are preserved,
|
|
Packit |
1c1d7e |
but all iterators on the dictionary become invalid.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QPtrDict::isEmpty() const
|
|
Packit |
1c1d7e |
Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
|
|
Packit |
1c1d7e |
otherwise.
|
|
Packit |
1c1d7e |
\sa count()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QPtrDict::insert( void *key, const type *item )
|
|
Packit |
1c1d7e |
Inserts the \e key with the \e item into the dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The key does not have to be a unique dictionary key. If multiple items
|
|
Packit |
1c1d7e |
are inserted with the same key, only the last item will be visible.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Null items are not allowed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa replace()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QPtrDict::replace( void *key, const type *item )
|
|
Packit |
1c1d7e |
Replaces an item which has a key equal to \e key with \e item.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If the item does not already exist, it will be inserted.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Null items are not allowed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Equivalent to:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
QPtrDict<char> dict;
|
|
Packit |
1c1d7e |
...
|
|
Packit |
1c1d7e |
if ( dict.find(key) )
|
|
Packit |
1c1d7e |
dict.remove( key );
|
|
Packit |
1c1d7e |
dict.insert( key, item );
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If there are two or more items with equal keys, then the last inserted
|
|
Packit |
1c1d7e |
of these will be replaced.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa insert()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QPtrDict::remove( void *key )
|
|
Packit |
1c1d7e |
Removes the item associated with \e key from the dictionary.
|
|
Packit |
1c1d7e |
Returns TRUE if successful, or FALSE if the key does not exist in the
|
|
Packit |
1c1d7e |
dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If there are two or more items with equal keys, then the last inserted
|
|
Packit |
1c1d7e |
of these will be removed.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The removed item is deleted if \link QCollection::setAutoDelete()
|
|
Packit |
1c1d7e |
auto-deletion\endlink is enabled.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
All dictionary iterators that refer to the removed item will be set to
|
|
Packit |
1c1d7e |
point to the next item in the dictionary traversing order.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa take(), clear(), setAutoDelete()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDict::take( void *key )
|
|
Packit |
1c1d7e |
Takes the item associated with \e key out of the dictionary without
|
|
Packit |
1c1d7e |
deleting it (even if \link QCollection::setAutoDelete()
|
|
Packit |
1c1d7e |
auto-deletion\endlink is enabled).
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If there are two or more items with equal keys, then the last inserted
|
|
Packit |
1c1d7e |
of these will be taken.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Returns a pointer to the item taken out, or null if the key does not
|
|
Packit |
1c1d7e |
exist in the dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
All dictionary iterators that refer to the taken item will be set to
|
|
Packit |
1c1d7e |
point to the next item in the dictionary traversing order.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa remove(), clear(), setAutoDelete()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QPtrDict::clear()
|
|
Packit |
1c1d7e |
Removes all items from the dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
The removed items are deleted if \link QCollection::setAutoDelete()
|
|
Packit |
1c1d7e |
auto-deletion\endlink is enabled.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
All dictionary iterators that access this dictionary will be reset.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa remove(), take(), setAutoDelete()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDict::find( void *key ) const
|
|
Packit |
1c1d7e |
Returns the item associated with \e key, or null if the key does not
|
|
Packit |
1c1d7e |
exist in the dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This function uses an internal hashing algorithm to optimize lookup.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If there are two or more items with equal keys, then the last inserted
|
|
Packit |
1c1d7e |
of these will be found.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Equivalent to the [] operator.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa operator[]()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDict::operator[]( void *key ) const
|
|
Packit |
1c1d7e |
Returns the item associated with \e key, or null if the key does not
|
|
Packit |
1c1d7e |
exist in the dictionary.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
This function uses an internal hashing algorithm to optimize lookup.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If there are two or more items with equal keys, then the last inserted
|
|
Packit |
1c1d7e |
of these will be found.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Equivalent to the find() function.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa find()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void QPtrDict::statistics() const
|
|
Packit |
1c1d7e |
Debugging-only function that prints out the dictionary distribution
|
|
Packit |
1c1d7e |
using qDebug().
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*****************************************************************************
|
|
Packit |
1c1d7e |
QPtrDictIterator documentation
|
|
Packit |
1c1d7e |
*****************************************************************************/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\class QPtrDictIterator qptrdict.h
|
|
Packit |
1c1d7e |
\brief The QPtrDictIterator class provides an iterator for QPtrDict collections.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\ingroup collection
|
|
Packit |
1c1d7e |
\ingroup tools
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDictIterator is implemented as a template class.
|
|
Packit |
1c1d7e |
Define a template instance QPtrDictIterator\<X\> to create a
|
|
Packit |
1c1d7e |
dictionary iterator that operates on QPtrDict\<X\> (dictionary of X*).
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Example:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
#include <qptrdict.h>
|
|
Packit |
1c1d7e |
#include <stdio.h>
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
void main()
|
|
Packit |
1c1d7e |
{
|
|
Packit |
1c1d7e |
int *a = new int[12];
|
|
Packit |
1c1d7e |
int *b = new int[10];
|
|
Packit |
1c1d7e |
int *c = new int[18];
|
|
Packit |
1c1d7e |
int *d = new int[13];
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDict<char> dict; // maps void* -> char*
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
dict.insert( a, "a is int[12]" ); // describe pointers
|
|
Packit |
1c1d7e |
dict.insert( b, "b is int[10]" );
|
|
Packit |
1c1d7e |
dict.insert( c, "c is int[18]" );
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
QPtrDictIterator<char> it( dict ); // iterator for dict
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
while ( it.current() ) {
|
|
Packit |
1c1d7e |
printf( "%x -> %s\n", it.currentKey(), it.current() );
|
|
Packit |
1c1d7e |
++it;
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
}
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Program output:
|
|
Packit |
1c1d7e |
\code
|
|
Packit |
1c1d7e |
804a788 -> a is int[12]
|
|
Packit |
1c1d7e |
804a7f0 -> c is int[18]
|
|
Packit |
1c1d7e |
804a7c0 -> b is int[10]
|
|
Packit |
1c1d7e |
\endcode
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Note that the traversal order is arbitrary, you are not guaranteed the
|
|
Packit |
1c1d7e |
order above.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
Multiple iterators may independently traverse the same dictionary.
|
|
Packit |
1c1d7e |
A QPtrDict knows about all iterators that are operating on the dictionary.
|
|
Packit |
1c1d7e |
When an item is removed from the dictionary, QPtrDict update all
|
|
Packit |
1c1d7e |
iterators that are referring the removed item to point to the next item
|
|
Packit |
1c1d7e |
in the traversing order.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
\sa QPtrDict, \link collection.html Collection Classes\endlink
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDictIterator::QPtrDictIterator( const QPtrDict<type> &dict )
|
|
Packit |
1c1d7e |
Constructs an iterator for \e dict. The current iterator item is
|
|
Packit |
1c1d7e |
set to point on the first item in the \e dict.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDictIterator::~QPtrDictIterator()
|
|
Packit |
1c1d7e |
Destroys the iterator.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn uint QPtrDictIterator::count() const
|
|
Packit |
1c1d7e |
Returns the number of items in the dictionary this iterator operates on.
|
|
Packit |
1c1d7e |
\sa isEmpty()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn bool QPtrDictIterator::isEmpty() const
|
|
Packit |
1c1d7e |
Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
|
|
Packit |
1c1d7e |
otherwise.
|
|
Packit |
1c1d7e |
\sa count()
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDictIterator::toFirst()
|
|
Packit |
1c1d7e |
Sets the current iterator item to point to the first item in the
|
|
Packit |
1c1d7e |
dictionary and returns a pointer to the item.
|
|
Packit |
1c1d7e |
If the dictionary is empty it sets the current item to null and
|
|
Packit |
1c1d7e |
returns null.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn QPtrDictIterator::operator type *() const
|
|
Packit |
1c1d7e |
Cast operator. Returns a pointer to the current iterator item.
|
|
Packit |
1c1d7e |
Same as current().
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDictIterator::current() const
|
|
Packit |
1c1d7e |
Returns a pointer to the current iterator item.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn void *QPtrDictIterator::currentKey() const
|
|
Packit |
1c1d7e |
Returns the key for the current iterator item.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDictIterator::operator()()
|
|
Packit |
1c1d7e |
Makes the succeeding item current and returns the original current item.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If the current iterator item was the last item in the dictionary or if it
|
|
Packit |
1c1d7e |
was null, null is returned.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDictIterator::operator++()
|
|
Packit |
1c1d7e |
Prefix ++ makes the succeeding item current and returns the new current
|
|
Packit |
1c1d7e |
item.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If the current iterator item was the last item in the dictionary or if it
|
|
Packit |
1c1d7e |
was null, null is returned.
|
|
Packit |
1c1d7e |
*/
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
/*!
|
|
Packit |
1c1d7e |
\fn type *QPtrDictIterator::operator+=( uint jump )
|
|
Packit |
1c1d7e |
Sets the current item to the item \e jump positions after the current item,
|
|
Packit |
1c1d7e |
and returns a pointer to that item.
|
|
Packit |
1c1d7e |
|
|
Packit |
1c1d7e |
If that item is beyond the last item or if the dictionary is empty,
|
|
Packit |
1c1d7e |
it sets the current item to null and returns null.
|
|
Packit |
1c1d7e |
*/
|