'\" t .TH TQDict 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*- .\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the .\" license file included in the distribution for a complete license .\" statement. .\" .ad l .nh .SH NAME TQDict \- Template class that provides a dictionary based on TQString keys .SH SYNOPSIS \fC#include \fR .PP Inherits TQPtrCollection. .PP .SS "Public Members" .in +1c .ti -1c .BI "\fBTQDict\fR ( int size = 17, bool caseSensitive = TRUE )" .br .ti -1c .BI "\fBTQDict\fR ( const TQDict & dict )" .br .ti -1c .BI "\fB~TQDict\fR ()" .br .ti -1c .BI "TQDict & \fBoperator=\fR ( const TQDict & dict )" .br .ti -1c .BI "virtual uint \fBcount\fR () const" .br .ti -1c .BI "uint \fBsize\fR () const" .br .ti -1c .BI "bool \fBisEmpty\fR () const" .br .ti -1c .BI "void \fBinsert\fR ( const TQString & key, const type * item )" .br .ti -1c .BI "void \fBreplace\fR ( const TQString & key, const type * item )" .br .ti -1c .BI "bool \fBremove\fR ( const TQString & key )" .br .ti -1c .BI "type * \fBtake\fR ( const TQString & key )" .br .ti -1c .BI "type * \fBfind\fR ( const TQString & key ) const" .br .ti -1c .BI "type * \fBoperator[]\fR ( const TQString & key ) const" .br .ti -1c .BI "virtual void \fBclear\fR ()" .br .ti -1c .BI "void \fBresize\fR ( uint newsize )" .br .ti -1c .BI "void \fBstatistics\fR () const" .br .in -1c .SS "Important Inherited Members" .in +1c .ti -1c .BI "bool \fBautoDelete\fR () const" .br .ti -1c .BI "void \fBsetAutoDelete\fR ( bool enable )" .br .in -1c .SS "Protected Members" .in +1c .ti -1c .BI "virtual TQDataStream & \fBread\fR ( TQDataStream & s, TQPtrCollection::Item & item )" .br .ti -1c .BI "virtual TQDataStream & \fBwrite\fR ( TQDataStream & s, TQPtrCollection::Item ) const" .br .in -1c .SH DESCRIPTION The TQDict class is a template class that provides a dictionary based on TQString keys. .PP TQMap is an STL-compatible alternative to this class. .PP TQDict is implemented as a template class. Define a template instance TQDict to create a dictionary that operates on pointers to X (X *). .PP A dictionary is a collection of key-value pairs. The key is a TQString used for insertion, removal and lookup. The value is a pointer. Dictionaries provide very fast insertion and lookup. .PP If you want to use non-Unicode, plain 8-bit \fCchar*\fR keys, use the TQAsciiDict template. A TQDict has the same performance as a TQAsciiDict. If you want to have a dictionary that maps TQStrings to TQStrings use TQMap. .PP The size() of the dictionary is very important. In order to get good performance, you should use a suitably large prime number. Suitable means equal to or larger than the maximum expected number of dictionary items. Size is set in the constructor but may be changed with resize(). .PP Items are inserted with insert(); 0 pointers cannot be inserted. Items are removed with remove(). All the items in a dictionary can be removed with clear(). The number of items in the dictionary is returned by count(). If the dictionary contains no items isEmpty() returns TRUE. You can change an item's value with replace(). Items are looked up with operator[](), or with find() which return a pointer to the value or 0 if the given key does not exist. You can take an item out of the dictionary with take(). .PP Calling setAutoDelete(TRUE) for a dictionary tells it to delete items that are removed. The default behaviour is not to delete items when they are removed. .PP When an item is inserted, the key is converted (hashed) to an integer index into an internal hash array. This makes lookup very fast. .PP Items with equal keys are allowed. When inserting two items with the same key, only the last inserted item will be accessible (last in, first out) until it is removed. .PP The TQDictIterator class can traverse the dictionary, but only in an arbitrary order. Multiple iterators may independently traverse the same dictionary. .PP When inserting an item into a dictionary, only the pointer is copied, not the item itself, i.e. a shallow copy is made. It is possible to make the dictionary copy all of the item's data (a deep copy) when an item is inserted. insert() calls the virtual function TQPtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement newItem() if you want deep copies. .PP When removing a dictionary item, the virtual function TQPtrCollection::deleteItem() is called. TQDict's default implementation is to delete the item if auto-deletion is enabled. .PP Example #1: .PP .nf .br TQDict fields; // TQString keys, TQLineEdit* values .br fields.insert( "forename", new TQLineEdit( this ) ); .br fields.insert( "surname", new TQLineEdit( this ) ); .br .br fields["forename"]->setText( "Homer" ); .br fields["surname"]->setText( "Simpson" ); .br .br TQDictIterator it( fields ); // See TQDictIterator .br for( ; it.current(); ++it ) .br cout << it.currentKey() << ": " << it.current()->text() << endl; .br cout << endl; .br .br if ( fields["forename"] && fields["surname"] ) .br cout << fields["forename"]->text() << " " .br .br << fields["surname"]->text() << endl; // Prints "Homer Simpson" .br .br fields.remove( "forename" ); // Does not delete the line edit .br if ( ! fields["forename"] ) .br cout << "forename is not in the dictionary" << endl; .br .fi In this example we use a dictionary to keep track of the line edits we're using. We insert each line edit into the dictionary with a unique name and then access the line edits via the dictionary. .PP Example #2: .PP .nf .br TQStringList styleList = TQStyleFactory::styles(); .br styleList.sort(); .br TQDict letterDict( 17, FALSE ); .br for ( TQStringList::Iterator it = styleList.begin(); it != styleList.end(); ++it ) { .br TQString styleName = *it; .br TQString styleAccel = styleName; .br if ( letterDict[styleAccel.left(1)] ) { .br for ( uint i = 0; i < styleAccel.length(); i++ ) { .br if ( ! letterDict[styleAccel.mid( i, 1 )] ) { .br styleAccel = styleAccel.insert( i, '&' ); .br letterDict.insert(styleAccel.mid( i, 1 ), (const int *)1); .br break; .br } .br } .br } else { .br styleAccel = "&" + styleAccel; .br letterDict.insert(styleAccel.left(1), (const int *)1); .br } .br (void) new TQAction( styleName, TQIconSet(), styleAccel, parent ); .br } .br .fi In the example we are using the dictionary to provide fast random access to the keys, and we don't care what the values are. The example is used to generate a menu of TQStyles, each with a unique accelerator key (or no accelerator if there are no unused letters left). .PP We first obtain the list of available styles, then sort them so that the menu items will be ordered alphabetically. Next we create a dictionary of int pointers. The keys in the dictionary are each one character long, representing letters that have been used for accelerators. We iterate through our list of style names. If the first letter of the style name is in the dictionary, i.e. has been used, we iterate over all the characters in the style name to see if we can find a letter that hasn't been used. If we find an unused letter we put the accelerator ampersand (&) in front of it and add that letter to the dictionary. If we can't find an unused letter the style will simply have no accelerator. If the first letter of the style name is not in the dictionary we use it for the accelerator and add it to the dictionary. Finally we create a TQAction for each style. .PP See also TQDictIterator, TQAsciiDict, TQIntDict, TQPtrDict, Collection Classes, and Non-GUI Classes. .SH MEMBER FUNCTION DOCUMENTATION .SH "TQDict::TQDict ( int size = 17, bool caseSensitive = TRUE )" Constructs a dictionary optimized for less than \fIsize\fR entries. .PP We recommend setting \fIsize\fR to a suitably large prime number (e.g. a prime that's slightly larger than the expected number of entries). This makes the hash distribution better which will lead to faster lookup. .PP If \fIcaseSensitive\fR is TRUE (the default), keys which differ only by case are considered different. .SH "TQDict::TQDict ( const TQDict & dict )" Constructs a copy of \fIdict\fR. .PP Each item in \fIdict\fR is inserted into this dictionary. Only the pointers are copied (shallow copy). .SH "TQDict::~TQDict ()" Removes all items from the dictionary and destroys it. If setAutoDelete() is TRUE, each value is deleted. All iterators that access this dictionary will be reset. .PP See also setAutoDelete(). .SH "bool TQPtrCollection::autoDelete () const" Returns the setting of the auto-delete option. The default is FALSE. .PP See also setAutoDelete(). .SH "void TQDict::clear ()\fC [virtual]\fR" Removes all items from the dictionary. .PP The removed items are deleted if auto-deletion is enabled. .PP All dictionary iterators that operate on the dictionary are reset. .PP See also remove(), take(), and setAutoDelete(). .PP Reimplemented from TQPtrCollection. .SH "uint TQDict::count () const\fC [virtual]\fR" Returns the number of items in the dictionary. .PP See also isEmpty(). .PP Reimplemented from TQPtrCollection. .SH "type * TQDict::find ( const TQString & key ) const" Returns the item with key \fIkey\fR, or 0 if the key does not exist in the dictionary. .PP If there are two or more items with equal keys, then the most recently inserted item will be found. .PP Equivalent to the [] operator. .PP See also operator[](). .SH "void TQDict::insert ( const TQString & key, const type * item )" Inserts the key \fIkey\fR with value \fIitem\fR into the dictionary. .PP Multiple items can have the same key, in which case only the last item will be accessible using operator[](). .PP \fIitem\fR may not be 0. .PP See also replace(). .PP Example: themes/themes.cpp. .SH "bool TQDict::isEmpty () const" Returns TRUE if the dictionary is empty, i.e. count() == 0; otherwise returns FALSE. .PP See also count(). .SH "TQDict & TQDict::operator= ( const TQDict & dict )" Assigns \fIdict\fR to this dictionary and returns a reference to this dictionary. .PP This dictionary is first cleared, then each item in \fIdict\fR is inserted into this dictionary. Only the pointers are copied (shallow copy), unless newItem() has been reimplemented. .SH "type * TQDict::operator[] ( const TQString & key ) const" Returns the item with key \fIkey\fR, or 0 if the key does not exist in the dictionary. .PP If there are two or more items with equal keys, then the most recently inserted item will be found. .PP Equivalent to the find() function. .PP See also find(). .SH "TQDataStream & TQDict::read ( TQDataStream & s, TQPtrCollection::Item & item )\fC [virtual protected]\fR" Reads a dictionary item from the stream \fIs\fR and returns a reference to the stream. .PP The default implementation sets \fIitem\fR to 0. .PP See also write(). .SH "bool TQDict::remove ( const TQString & key )" Removes the item with \fIkey\fR from the dictionary. Returns TRUE if successful, i.e. if the item is in the dictionary; otherwise returns FALSE. .PP If there are two or more items with equal keys, then the last item that was inserted will be removed. .PP The removed item is deleted if auto-deletion is enabled. .PP All dictionary iterators that refer to the removed item will be set to point to the next item in the dictionary's traversal order. .PP See also take(), clear(), and setAutoDelete(). .SH "void TQDict::replace ( const TQString & key, const type * item )" Replaces the value of the key, \fIkey\fR with \fIitem\fR. .PP If the item does not already exist, it will be inserted. .PP \fIitem\fR may not be 0. .PP Equivalent to: .PP .nf .br TQDict dict; .br ... .br if ( dict.find( key ) ) .br dict.remove( key ); .br dict.insert( key, item ); .br .fi .PP If there are two or more items with equal keys, then the last item that was inserted will be replaced. .PP See also insert(). .SH "void TQDict::resize ( uint newsize )" Changes the size of the hash table to \fInewsize\fR. The contents of the dictionary are preserved, but all iterators on the dictionary become invalid. .SH "void TQPtrCollection::setAutoDelete ( bool enable )" Sets the collection to auto-delete its contents if \fIenable\fR is TRUE and to never delete them if \fIenable\fR is FALSE. .PP If auto-deleting is turned on, all the items in a collection are deleted when the collection itself is deleted. This is convenient if the collection has the only pointer to the items. .PP The default setting is FALSE, for safety. If you turn it on, be careful about copying the collection - you might find yourself with two collections deleting the same items. .PP Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item. .PP See also autoDelete(). .PP Examples: .)l grapher/grapher.cpp, scribble/scribble.cpp, and table/bigtable/main.cpp. .SH "uint TQDict::size () const" Returns the size of the internal hash array (as specified in the constructor). .PP See also count(). .SH "void TQDict::statistics () const" Debugging-only function that prints out the dictionary distribution using tqDebug(). .SH "type * TQDict::take ( const TQString & key )" Takes the item with \fIkey\fR out of the dictionary without deleting it (even if auto-deletion is enabled). .PP If there are two or more items with equal keys, then the last item that was inserted will be taken. .PP Returns a pointer to the item taken out, or 0 if the key does not exist in the dictionary. .PP All dictionary iterators that refer to the taken item will be set to point to the next item in the dictionary traversal order. .PP See also remove(), clear(), and setAutoDelete(). .SH "TQDataStream & TQDict::write ( TQDataStream & s, TQPtrCollection::Item ) const\fC [virtual protected]\fR" Writes a dictionary item to the stream \fIs\fR and returns a reference to the stream. .PP See also read(). .SH "SEE ALSO" .BR http://doc.trolltech.com/tqdict.html .BR http://www.trolltech.com/faq/tech.html .SH COPYRIGHT Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement. .SH AUTHOR Generated automatically from the source code. .SH BUGS If you find a bug in Qt, please report it as described in .BR http://doc.trolltech.com/bughowto.html . Good bug reports help us to help you. Thank you. .P The definitive TQt documentation is provided in HTML format; it is located at $TQTDIR/doc/html and can be read using TQt Assistant or with a web browser. This man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. .P If you find errors in this manual page, please report them to .BR qt-bugs@trolltech.com . Please include the name of the manual page (tqdict.3qt) and the Qt version (3.3.8).