1 /****************************************************************************
4 ** QPtrDict and QPtrDictIterator class documentation
6 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
8 ** This file is part of the Qt GUI Toolkit.
10 ** This file may be distributed under the terms of the Q Public License
11 ** as defined by Trolltech AS of Norway and appearing in the file
12 ** LICENSE.QPL included in the packaging of this file.
14 ** This file may be distributed and/or modified under the terms of the
15 ** GNU General Public License version 2 as published by the Free Software
16 ** Foundation and appearing in the file LICENSE.GPL included in the
17 ** packaging of this file.
19 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20 ** licenses may use this file in accordance with the Qt Commercial License
21 ** Agreement provided with the Software.
23 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27 ** information about Qt Commercial License Agreements.
28 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
29 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
31 ** Contact info@trolltech.com if any conditions of this licensing are
34 **********************************************************************/
37 /*****************************************************************************
38 QPtrDict documentation
39 *****************************************************************************/
42 \class QPtrDict qptrdict.h
43 \brief The QPtrDict class is a template class that provides a dictionary based on \c void* keys.
48 QPtrDict is implemented as a template class. Define a
49 template instance QPtrDict\<X\> to create a dictionary that operates on
52 A dictionary is a collection that associates an item with a key.
53 The key is used for inserting and looking up an item. QPtrDict has
56 The dictionary has very fast insertion and lookup.
70 QPtrDict<char> dict; // maps void* -> char*
72 dict.insert( a, "a is int[12]" ); // describe pointers
73 dict.insert( b, "b is int[10]" );
74 dict.insert( c, "c is int[18]" );
76 printf( "%s\n", dict[a] ); // print descriptions
77 printf( "%s\n", dict[b] );
78 printf( "%s\n", dict[c] );
81 printf( "d not in dictionary\n" );
93 The dictionary in our example maps \c int* keys to \c char* items.
94 QPtrDict implements the \link operator[] [] operator\endlink to lookup
97 QPtrDict is implemented by QGDict as a hash array with a fixed number of
98 entries. Each array entry points to a singly linked list of buckets, in
99 which the dictionary items are stored.
101 When an item is inserted with a key, the key is converted (hashed) to
102 an integer index into the hash array using the \c mod operation. The
103 item is inserted before the first bucket in the list of buckets.
105 Looking up an item is normally very fast. The key is again hashed to an
106 array index. Then QPtrDict scans the list of buckets and returns the item
107 found or null if the item was not found. You cannot insert null pointers
110 The size of the hash array is very important. In order to get good
111 performance, you should use a suitably large \link primes.html prime
112 number\endlink. Suitable means equal to or larger than the maximum
113 expected number of dictionary items.
115 Items with equal keys are allowed. When inserting two items with the
116 same key, only the last inserted item will be visible (last in, first out)
121 #include <qptrdict.h>
126 QPtrDict<char> dict; // maps char* ==> char*
128 double *ptr = new double[28];
129 dict.insert( ptr, "first" );
130 dict.insert( ptr, "second" );
132 printf( "%s\n", dict[ptr] );
134 printf( "%s\n", dict[ptr] );
144 The QPtrDictIterator class can traverse the dictionary contents, but only
145 in an arbitrary order. Multiple iterators may independently traverse the
148 Calling setAutoDelete(TRUE) for a dictionary tells it to delete items
149 that are removed . The default is to not delete items when they are
152 When inserting an item into a dictionary, only the pointer is copied, not
153 the item itself. This is called a shallow copy. It is possible to make the
154 dictionary copy all of the item's data (known as a deep copy) when an
155 item is inserted. insert() calls the virtual function
156 QCollection::newItem() for the item to be inserted.
157 Inherit a dictionary and reimplement it if you want deep copies.
159 When removing a dictionary item, the virtual function
160 QCollection::deleteItem() is called. QPtrDict's default implementation
161 is to delete the item if auto-deletion is enabled.
163 \sa QPtrDictIterator, QDict, QAsciiDict, QIntDict,
164 \link collection.html Collection Classes\endlink
169 \fn QPtrDict::QPtrDict( int size )
170 Constructs a dictionary using an internal hash array with the size
173 Setting \e size to a suitably large \link primes.html prime number\endlink
174 (equal to or greater than the expected number of entries) makes the hash
175 distribution better and hence the lookup faster.
179 \fn QPtrDict::QPtrDict( const QPtrDict<type> &dict )
180 Constructs a copy of \e dict.
182 Each item in \e dict are inserted into this dictionary.
183 Only the pointers are copied (shallow copy).
187 \fn QPtrDict::~QPtrDict()
188 Removes all items from the dictionary and destroys it.
190 All iterators that access this dictionary will be reset.
196 \fn QPtrDict<type> &QPtrDict::operator=(const QPtrDict<type> &dict)
197 Assigns \e dict to this dictionary and returns a reference to this
200 This dictionary is first cleared, then each item in \e dict is inserted
201 into this dictionary.
202 Only the pointers are copied (shallow copy), unless newItem() has been
207 \fn uint QPtrDict::count() const
208 Returns the number of items in the dictionary.
213 \fn uint QPtrDict::size() const
214 Returns the size of the internal hash array (as specified in the
220 \fn void QPtrDict::resize( uint newsize )
221 Changes the size of the hashtable the \a newsize.
222 The contents of the dictionary are preserved,
223 but all iterators on the dictionary become invalid.
227 \fn bool QPtrDict::isEmpty() const
228 Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
234 \fn void QPtrDict::insert( void *key, const type *item )
235 Inserts the \e key with the \e item into the dictionary.
237 The key does not have to be a unique dictionary key. If multiple items
238 are inserted with the same key, only the last item will be visible.
240 Null items are not allowed.
246 \fn void QPtrDict::replace( void *key, const type *item )
247 Replaces an item which has a key equal to \e key with \e item.
249 If the item does not already exist, it will be inserted.
251 Null items are not allowed.
257 if ( dict.find(key) )
259 dict.insert( key, item );
262 If there are two or more items with equal keys, then the last inserted
263 of these will be replaced.
269 \fn bool QPtrDict::remove( void *key )
270 Removes the item associated with \e key from the dictionary.
271 Returns TRUE if successful, or FALSE if the key does not exist in the
274 If there are two or more items with equal keys, then the last inserted
275 of these will be removed.
277 The removed item is deleted if \link QCollection::setAutoDelete()
278 auto-deletion\endlink is enabled.
280 All dictionary iterators that refer to the removed item will be set to
281 point to the next item in the dictionary traversing order.
283 \sa take(), clear(), setAutoDelete()
287 \fn type *QPtrDict::take( void *key )
288 Takes the item associated with \e key out of the dictionary without
289 deleting it (even if \link QCollection::setAutoDelete()
290 auto-deletion\endlink is enabled).
292 If there are two or more items with equal keys, then the last inserted
293 of these will be taken.
295 Returns a pointer to the item taken out, or null if the key does not
296 exist in the dictionary.
298 All dictionary iterators that refer to the taken item will be set to
299 point to the next item in the dictionary traversing order.
301 \sa remove(), clear(), setAutoDelete()
305 \fn void QPtrDict::clear()
306 Removes all items from the dictionary.
308 The removed items are deleted if \link QCollection::setAutoDelete()
309 auto-deletion\endlink is enabled.
311 All dictionary iterators that access this dictionary will be reset.
313 \sa remove(), take(), setAutoDelete()
317 \fn type *QPtrDict::find( void *key ) const
318 Returns the item associated with \e key, or null if the key does not
319 exist in the dictionary.
321 This function uses an internal hashing algorithm to optimize lookup.
323 If there are two or more items with equal keys, then the last inserted
324 of these will be found.
326 Equivalent to the [] operator.
332 \fn type *QPtrDict::operator[]( void *key ) const
333 Returns the item associated with \e key, or null if the key does not
334 exist in the dictionary.
336 This function uses an internal hashing algorithm to optimize lookup.
338 If there are two or more items with equal keys, then the last inserted
339 of these will be found.
341 Equivalent to the find() function.
347 \fn void QPtrDict::statistics() const
348 Debugging-only function that prints out the dictionary distribution
353 /*****************************************************************************
354 QPtrDictIterator documentation
355 *****************************************************************************/
358 \class QPtrDictIterator qptrdict.h
359 \brief The QPtrDictIterator class provides an iterator for QPtrDict collections.
364 QPtrDictIterator is implemented as a template class.
365 Define a template instance QPtrDictIterator\<X\> to create a
366 dictionary iterator that operates on QPtrDict\<X\> (dictionary of X*).
370 #include <qptrdict.h>
375 int *a = new int[12];
376 int *b = new int[10];
377 int *c = new int[18];
378 int *d = new int[13];
380 QPtrDict<char> dict; // maps void* -> char*
382 dict.insert( a, "a is int[12]" ); // describe pointers
383 dict.insert( b, "b is int[10]" );
384 dict.insert( c, "c is int[18]" );
386 QPtrDictIterator<char> it( dict ); // iterator for dict
388 while ( it.current() ) {
389 printf( "%x -> %s\n", it.currentKey(), it.current() );
397 804a788 -> a is int[12]
398 804a7f0 -> c is int[18]
399 804a7c0 -> b is int[10]
402 Note that the traversal order is arbitrary, you are not guaranteed the
405 Multiple iterators may independently traverse the same dictionary.
406 A QPtrDict knows about all iterators that are operating on the dictionary.
407 When an item is removed from the dictionary, QPtrDict update all
408 iterators that are referring the removed item to point to the next item
409 in the traversing order.
411 \sa QPtrDict, \link collection.html Collection Classes\endlink
415 \fn QPtrDictIterator::QPtrDictIterator( const QPtrDict<type> &dict )
416 Constructs an iterator for \e dict. The current iterator item is
417 set to point on the first item in the \e dict.
421 \fn QPtrDictIterator::~QPtrDictIterator()
422 Destroys the iterator.
426 \fn uint QPtrDictIterator::count() const
427 Returns the number of items in the dictionary this iterator operates on.
432 \fn bool QPtrDictIterator::isEmpty() const
433 Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
439 \fn type *QPtrDictIterator::toFirst()
440 Sets the current iterator item to point to the first item in the
441 dictionary and returns a pointer to the item.
442 If the dictionary is empty it sets the current item to null and
447 \fn QPtrDictIterator::operator type *() const
448 Cast operator. Returns a pointer to the current iterator item.
453 \fn type *QPtrDictIterator::current() const
454 Returns a pointer to the current iterator item.
458 \fn void *QPtrDictIterator::currentKey() const
459 Returns the key for the current iterator item.
463 \fn type *QPtrDictIterator::operator()()
464 Makes the succeeding item current and returns the original current item.
466 If the current iterator item was the last item in the dictionary or if it
467 was null, null is returned.
471 \fn type *QPtrDictIterator::operator++()
472 Prefix ++ makes the succeeding item current and returns the new current
475 If the current iterator item was the last item in the dictionary or if it
476 was null, null is returned.
480 \fn type *QPtrDictIterator::operator+=( uint jump )
481 Sets the current item to the item \e jump positions after the current item,
482 and returns a pointer to that item.
484 If that item is beyond the last item or if the dictionary is empty,
485 it sets the current item to null and returns null.