|  | Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |

# Q3Dict Class Reference
[Qt3Support module]

The Q3Dict class is a template class that provides a dictionary based on QString keys. More...

     #include <Q3Dict>

This class is part of the Qt 3 support library. It is provided to keep old source code working. We strongly advise against using it in new code. See Porting to Qt 4 for more information.

Inherits Q3PtrCollection.

- List of all members, including inherited members

### Public Functions

- Q3Dict ( int size = 17, bool caseSensitive = true )
- Q3Dict ( const Q3Dict<type> & dict )
- ~Q3Dict ()
- virtual void clear ()
- virtual uint count () const
- type * find ( const QString & key ) const
- void insert ( const QString & key, const type * item )
- bool isEmpty () const
- bool remove ( const QString & key )
- void replace ( const QString & key, const type * item )
- void resize ( uint newsize )
- uint size () const
- void statistics () const
- type * take ( const QString & key )
- Q3Dict<type> & operator= ( const Q3Dict<type> & dict )
- type * operator[] ( const QString & key ) const

- 4 public functions inherited from Q3PtrCollection

### Protected Functions

- virtual QDataStream & read ( QDataStream & s, Q3PtrCollection::Item & item )
- virtual QDataStream & write ( QDataStream & s, Q3PtrCollection::Item item ) const

- 2 protected functions inherited from Q3PtrCollection

## Detailed Description

The Q3Dict class is a template class that provides a dictionary based on QString keys.

Q3Dict is implemented as a template class. Define a template instance Q3Dict<X> to create a dictionary that operates on pointers to X (X *).

A dictionary is a collection of key-value pairs. The key is a QString used for insertion, removal and lookup. The value is a pointer. Dictionaries provide very fast insertion and lookup.

If you want to use non-Unicode, plain 8-bit `char*` keys, use the Q3AsciiDict template. A Q3Dict has the same performance as a Q3AsciiDict. If you want to have a dictionary that maps QStrings to QStrings use QMap.

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().

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().

Calling setAutoDelete(TRUE) for a dictionary tells it to delete items that are removed. The default behavior is not to delete items when they are removed.

When an item is inserted, the key is converted (hashed) to an integer index into an internal hash array. This makes lookup very fast.

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.

The Q3DictIterator class can traverse the dictionary, but only in an arbitrary order. Multiple iterators may independently traverse the same dictionary.

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 Q3PtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement newItem() if you want deep copies.

When removing a dictionary item, the virtual function Q3PtrCollection::deleteItem() is called. Q3Dict's default implementation is to delete the item if auto-deletion is enabled.

See also Q3DictIterator, Q3AsciiDict, Q3IntDict, and Q3PtrDict.

## Member Function Documentation

### Q3Dict::Q3Dict ( int size = 17, bool caseSensitive = true )

Constructs a dictionary optimized for less than size entries.

We recommend setting size 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.

If caseSensitive is TRUE (the default), keys which differ only by case are considered different.

### Q3Dict::Q3Dict ( const Q3Dict<type> & dict )

Constructs a copy of dict.

Each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy).

### Q3Dict::~Q3Dict ()

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.

See also setAutoDelete().

### void Q3Dict::clear () `[virtual]`

Removes all items from the dictionary.

The removed items are deleted if auto-deletion is enabled.

All dictionary iterators that operate on the dictionary are reset.

Reimplemented from Q3PtrCollection.

See also remove(), take(), and setAutoDelete().

### uint Q3Dict::count () const `[virtual]`

Returns the number of items in the dictionary.

Reimplemented from Q3PtrCollection.

See also isEmpty().

### type * Q3Dict::find ( const QString & key ) const

Returns the item with key key, or 0 if the key does not exist in the dictionary.

If there are two or more items with equal keys, then the most recently inserted item will be found.

Equivalent to the [] operator.

See also operator[]().

### void Q3Dict::insert ( const QString & key, const type * item )

Inserts the key key with value item into the dictionary.

Multiple items can have the same key, in which case only the last item will be accessible using operator[]().

item may not be 0.

See also replace().

### bool Q3Dict::isEmpty () const

Returns TRUE if the dictionary is empty, i.e. count() == 0; otherwise returns FALSE.

See also count().

### QDataStream & Q3Dict::read ( QDataStream & s, Q3PtrCollection::Item & item ) `[virtual protected]`

Reads a dictionary item from the stream s and returns a reference to the stream.

The default implementation sets item to 0.

See also write().

### bool Q3Dict::remove ( const QString & key )

Removes the item with key from the dictionary. Returns TRUE if successful, i.e. if the item is in the dictionary; otherwise returns FALSE.

If there are two or more items with equal keys, then the last item that was inserted will be removed.

The removed item is deleted if auto-deletion is enabled.

All dictionary iterators that refer to the removed item will be set to point to the next item in the dictionary's traversal order.

See also take(), clear(), and setAutoDelete().

### void Q3Dict::replace ( const QString & key, const type * item )

Replaces the value of the key, key with item.

If the item does not already exist, it will be inserted.

item may not be 0.

Equivalent to:

             Q3Dict<char> dict;
                 ...
             if ( dict.find( key ) )
                 dict.remove( key );
             dict.insert( key, item );

If there are two or more items with equal keys, then the last item that was inserted will be replaced.

See also insert().

### void Q3Dict::resize ( uint newsize )

Changes the size of the hash table to newsize. The contents of the dictionary are preserved, but all iterators on the dictionary become invalid.

### uint Q3Dict::size () const

Returns the size of the internal hash array (as specified in the constructor).

See also count().

### void Q3Dict::statistics () const

Debugging-only function that prints out the dictionary distribution using qDebug().

### type * Q3Dict::take ( const QString & key )

Takes the item with key out of the dictionary without deleting it (even if auto-deletion is enabled).

If there are two or more items with equal keys, then the last item that was inserted will be taken.

Returns a pointer to the item taken out, or 0 if the key does not exist in the dictionary.

All dictionary iterators that refer to the taken item will be set to point to the next item in the dictionary traversal order.

See also remove(), clear(), and setAutoDelete().

### QDataStream & Q3Dict::write ( QDataStream & s, Q3PtrCollection::Item item ) const `[virtual protected]`

Writes a dictionary item to the stream s and returns a reference to the stream.

See also read().

### Q3Dict<type> & Q3Dict::operator= ( const Q3Dict<type> & dict )

Assigns dict to this dictionary and returns a reference to this dictionary.

This dictionary is first cleared, then each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy), unless newItem() has been reimplemented.

### type * Q3Dict::operator[] ( const QString & key ) const

Returns the item with key key, or 0 if the key does not exist in the dictionary.

If there are two or more items with equal keys, then the most recently inserted item will be found.

Equivalent to the find() function.

See also find().