1 /****************************************************************************
4 ** QIntDict and QIntDictIterator 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 QIntDict documentation
39 *****************************************************************************/
42 \class QIntDict qintdict.h
43 \brief The QIntDict class is a template class that provides a dictionary based on \c long keys.
48 QIntDict is implemented as a template class. Define a
49 template instance QIntDict\<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. QIntDict has
56 The dictionary has very fast insertion and lookup.
65 QIntDict<char> dict; // maps long ==> char*
67 dict.insert( 33, "France" );
68 dict.insert( 7, "Russia" );
69 dict.insert( 49, "Norway" );
71 printf( "%s\n", dict[49] );
72 printf( "%s\n", dict[33] );
73 printf( "%s\n", dict[7] );
76 printf( "39 not defined\n" );
88 The dictionary in our example maps \c long keys to \c char* items.
89 QIntDict implements the \link operator[] [] operator\endlink to lookup
92 QIntDict is implemented by QGDict as a hash array with a fixed number of
93 entries. Each array entry points to a singly linked list of buckets, in
94 which the dictionary items are stored.
96 When an item is inserted with a key, the key is converted (hashed) to
97 an integer index into the hash array using the \c mod operation. The
98 item is inserted before the first bucket in the list of buckets.
100 Looking up an item is normally very fast. The key is again hashed to an
101 array index. Then QIntDict scans the list of buckets and returns the item
102 found or null if the item was not found. You cannot insert null pointers
105 The size of the hash array is very important. In order to get good
106 performance, you should use a suitably large \link primes.html prime
107 number\endlink. Suitable means equal to or larger than the maximum
108 expected number of dictionary items.
110 Items with equal keys are allowed. When inserting two items with the
111 same key, only the last inserted item will be visible (last in, first out)
116 #include <qintdict.h>
121 QIntDict<char> dict; // maps long ==> char*
123 dict.insert( 7, "Russia" );
124 dict.insert( 7, "USSR" );
126 printf( "%s\n", dict[7] );
127 dict.remove( 7 ); // Gorbie was here
128 printf( "%s\n", dict[7] );
138 The QIntDictIterator class can traverse the dictionary contents, but only
139 in an arbitrary order. Multiple iterators may independently traverse the
142 Calling setAutoDelete(TRUE) for a dictionary tells it to delete items
143 that are removed . The default is to not delete items when they are
146 When inserting an item into a dictionary, only the pointer is copied, not
147 the item itself. This is called a shallow copy. It is possible to make the
148 dictionary copy all of the item's data (known as a deep copy) when an
149 item is inserted. insert() calls the virtual function
150 QCollection::newItem() for the item to be inserted.
151 Inherit a dictionary and reimplement it if you want deep copies.
153 When removing a dictionary item, the virtual function
154 QCollection::deleteItem() is called. QIntDict's default implementation
155 is to delete the item if auto-deletion is enabled.
157 \sa QIntDictIterator, QDict, QAsciiDict, QPtrDict,
158 \link collection.html Collection Classes\endlink
163 \fn QIntDict::QIntDict( int size )
164 Constructs a dictionary using an internal hash array with the size
167 Setting \e size to a suitably large \link primes.html prime number\endlink
168 (equal to or greater than the expected number of entries) makes the hash
169 distribution better and hence the lookup faster.
173 \fn QIntDict::QIntDict( const QIntDict<type> &dict )
174 Constructs a copy of \e dict.
176 Each item in \e dict are inserted into this dictionary.
177 Only the pointers are copied (shallow copy).
181 \fn QIntDict::~QIntDict()
182 Removes all items from the dictionary and destroys it.
184 All iterators that access this dictionary will be reset.
190 \fn QIntDict<type> &QIntDict::operator=(const QIntDict<type> &dict)
191 Assigns \e dict to this dictionary and returns a reference to this
194 This dictionary is first cleared, then each item in \e dict is inserted
195 into this dictionary.
196 Only the pointers are copied (shallow copy), unless newItem() has been
201 \fn uint QIntDict::count() const
202 Returns the number of items in the dictionary.
207 \fn uint QIntDict::size() const
208 Returns the size of the internal hash array (as specified in the
214 \fn void QIntDict::resize( uint newsize )
215 Changes the size of the hashtable the \a newsize.
216 The contents of the dictionary are preserved,
217 but all iterators on the dictionary become invalid.
221 \fn bool QIntDict::isEmpty() const
222 Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
228 \fn void QIntDict::insert( long key, const type *item )
229 Inserts the \e key with the \e item into the dictionary.
231 The key does not have to be a unique dictionary key. If multiple items
232 are inserted with the same key, only the last item will be visible.
234 Null items are not allowed.
240 \fn void QIntDict::replace( long key, const type *item )
241 Replaces an item which has a key equal to \e key with \e item.
243 If the item does not already exist, it will be inserted.
245 Null items are not allowed.
251 if ( dict.find(key) )
253 dict.insert( key, item );
256 If there are two or more items with equal keys, then the last inserted
257 of these will be replaced.
263 \fn bool QIntDict::remove( long key )
264 Removes the item associated with \e key from the dictionary.
265 Returns TRUE if successful, or FALSE if the key does not exist in the
268 If there are two or more items with equal keys, then the last inserted
269 of these will be removed.
271 The removed item is deleted if \link QCollection::setAutoDelete()
272 auto-deletion\endlink is enabled.
274 All dictionary iterators that refer to the removed item will be set to
275 point to the next item in the dictionary traversing order.
277 \sa take(), clear(), setAutoDelete()
281 \fn type *QIntDict::take( long key )
282 Takes the item associated with \e key out of the dictionary without
283 deleting it (even if \link QCollection::setAutoDelete()
284 auto-deletion\endlink is enabled).
286 If there are two or more items with equal keys, then the last inserted
287 of these will be taken.
289 Returns a pointer to the item taken out, or null if the key does not
290 exist in the dictionary.
292 All dictionary iterators that refer to the taken item will be set to
293 point to the next item in the dictionary traversing order.
295 \sa remove(), clear(), setAutoDelete()
299 \fn void QIntDict::clear()
300 Removes all items from the dictionary.
302 The removed items are deleted if \link QCollection::setAutoDelete()
303 auto-deletion\endlink is enabled.
305 All dictionary iterators that access this dictionary will be reset.
307 \sa remove(), take(), setAutoDelete()
311 \fn type *QIntDict::find( long key ) const
312 Returns the item associated with \e key, or null if the key does not
313 exist in the dictionary.
315 This function uses an internal hashing algorithm to optimize lookup.
317 If there are two or more items with equal keys, then the last inserted
318 of these will be found.
320 Equivalent to the [] operator.
326 \fn type *QIntDict::operator[]( long key ) const
327 Returns the item associated with \e key, or null if the key does not
328 exist in the dictionary.
330 This function uses an internal hashing algorithm to optimize lookup.
332 If there are two or more items with equal keys, then the last inserted
333 of these will be found.
335 Equivalent to the find() function.
341 \fn void QIntDict::statistics() const
342 Debugging-only function that prints out the dictionary distribution
347 /*****************************************************************************
348 QIntDictIterator documentation
349 *****************************************************************************/
352 \class QIntDictIterator qintdict.h
353 \brief The QIntDictIterator class provides an iterator for QIntDict collections.
358 QIntDictIterator is implemented as a template class.
359 Define a template instance QIntDictIterator\<X\> to create a
360 dictionary iterator that operates on QIntDict\<X\> (dictionary of X*).
364 #include <qintdict.h>
369 QIntDict<char> dict; // maps long ==> char*
371 dict.insert( 33, "France" );
372 dict.insert( 7, "Russia" );
373 dict.insert( 49, "Norway" );
375 QIntDictIterator<char> it( dict ); // iterator for dict
377 while ( it.current() ) {
378 printf( "%d -> %s\n", it.currentKey(), it.current() );
391 Note that the traversal order is arbitrary, you are not guaranteed the
394 Multiple iterators may independently traverse the same dictionary.
395 A QIntDict knows about all iterators that are operating on the dictionary.
396 When an item is removed from the dictionary, QIntDict update all
397 iterators that are referring the removed item to point to the next item
398 in the traversing order.
400 \sa QIntDict, \link collection.html Collection Classes\endlink
404 \fn QIntDictIterator::QIntDictIterator( const QIntDict<type> &dict )
405 Constructs an iterator for \e dict. The current iterator item is
406 set to point on the first item in the \e dict.
410 \fn QIntDictIterator::~QIntDictIterator()
411 Destroys the iterator.
415 \fn uint QIntDictIterator::count() const
416 Returns the number of items in the dictionary this iterator operates on.
421 \fn bool QIntDictIterator::isEmpty() const
422 Returns TRUE if the dictionary is empty, i.e. count() == 0. Returns FALSE
428 \fn type *QIntDictIterator::toFirst()
429 Sets the current iterator item to point to the first item in the
430 dictionary and returns a pointer to the item.
431 If the dictionary is empty it sets the current item to null and
436 \fn QIntDictIterator::operator type *() const
437 Cast operator. Returns a pointer to the current iterator item.
442 \fn type *QIntDictIterator::current() const
443 Returns a pointer to the current iterator item.
447 \fn long QIntDictIterator::currentKey() const
448 Returns the key for the current iterator item.
452 \fn type *QIntDictIterator::operator()()
453 Makes the succeeding item current and returns the original current item.
455 If the current iterator item was the last item in the dictionary or if it
456 was null, null is returned.
460 \fn type *QIntDictIterator::operator++()
461 Prefix ++ makes the succeeding item current and returns the new current
464 If the current iterator item was the last item in the dictionary or if it
465 was null, null is returned.
469 \fn type *QIntDictIterator::operator+=( uint jump )
470 Sets the current item to the item \e jump positions after the current item,
471 and returns a pointer to that item.
473 If that item is beyond the last item or if the dictionary is empty,
474 it sets the current item to null and returns null.