Classes - Annotated - Tree - Functions - Home - Structure

QAsciiDict Class Reference

The QAsciiDict class is a template class that provides a dictionary based on char* keys. More...

#include <qasciidict.h>

Inherits QPtrCollection.

List of all member functions.

Public Members

Important Inherited Members

Protected Members


Detailed Description

The QAsciiDict class is a template class that provides a dictionary based on char* keys.

QAsciiDict is implemented as a template class. Define a template instance QAsciiDict<X> to create a dictionary that operates on pointers to X, or X*.

A dictionary is a collection that associates an item with a key. The key is used for inserting and looking up an item. QAsciiDict has char* keys. QAsciiDict cannot handle Unicode keys; use the QDict template instead, which uses QString keys. A QDict has the same performace as a QAsciiDict.

The dictionary has very fast insertion and lookup.

Example:

    #include <qdict.h>
    #include <stdio.h>

    int main()
    {
      // Creates a dictionary that maps char* ==> char* (case insensitive)
        QAsciiDict<char> dict( 17, FALSE );

        dict.insert( "France", "Paris" );
        dict.insert( "Russia", "Moscow" );
        dict.insert( "Norway", "Oslo" );

        printf( "%s\n", dict["Norway"] );
        printf( "%s\n", dict["france"] );
        printf( "%s\n", dict["RUSSIA"] );

        dict.remove( "France" );

        if ( !dict["France"] )
            printf( "France undefined\n" );

        return 0;
    }
  

Program output:

    Oslo
    Paris
    Moscow
    France undefined
  

The dictionary in our example maps char* keys to char* items. Note that the mapping is case-insensitive (specified in the constructor). QAsciiDict implements operator[]() to look up an item.

QAsciiDict is implemented by QGDict as a hash array with a fixed number of entries. Each array entry points to a singly linked list of buckets, in which the dictionary items are stored.

When an item is inserted with a key, the key is converted (hashed) to an integer index into the hash array. The item is inserted before the first bucket in the list of buckets.

Looking up an item is normally very fast. The key is again hashed to an array index. Then QAsciiDict scans the list of buckets and returns the item found, or null if the item was not found. You cannot insert null pointers into a dictionary.

The size of the hash array 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.

Items with equal keys are allowed. When inserting two items with the same key, only the last inserted item will be visible (last in, first out) until it is removed.

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

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

When inserting an item into a dictionary, only the pointer is copied, not the item itself. This is called a shallow copy. It is possible to make the dictionary copy all of the item's data (known as a deep copy) when an item is inserted. insert() calls the virtual function QPtrCollection::newItem() for the item to be inserted. Inherit a dictionary and reimplement it if you want deep copies.

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

See also QAsciiDictIterator, QDict, QIntDict, QPtrDict and Collection Classes.


Member Function Documentation

QAsciiDict::QAsciiDict ( int size = 17, bool caseSensitive = TRUE, bool copyKeys = TRUE )

Constructs a dictionary optimized for less than size entries.

We recommend setting size to a suitably large prime number (a bit larger than the expected number of entries). This makes the hash distribution better and hence the lookup faster.

When caseSensitive is TRUE (the default) QAsciiDict treats "abc" and "Abc" as different keys; when it is FALSE "abc" and "Abc" are the same. Case-insensitive comparison includes only the 26 letters in US-ASCII.

If copyKeys is TRUE (the default), the dictionary copies keys using strcpy; if it is FALSE, the dictionary just copies the pointers.

QAsciiDict::QAsciiDict ( const QAsciiDict<type> & dict )

Constructs a copy of dict.

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

QAsciiDict::~QAsciiDict ()

Removes all items from the dictionary and destroys it.

The items are deleted if auto-delete is enabled.

All iterators that access this dictionary will be reset.

See also setAutoDelete().

bool QPtrCollection::autoDelete () const

Returns the setting of the auto-delete option. The default is FALSE.

See also setAutoDelete().

void QAsciiDict::clear () [virtual]

Removes all items from the dictionary.

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

All dictionary iterators that operate on dictionary are reset.

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

Reimplemented from QPtrCollection.

uint QAsciiDict::count () const [virtual]

Returns the number of items in the dictionary.

See also isEmpty().

Reimplemented from QPtrCollection.

type * QAsciiDict::find ( const char * key ) const

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

This function uses an internal hashing algorithm to optimize lookup.

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

Equivalent to the [] operator.

See also operator[]().

void QAsciiDict::insert ( const char * key, const type * item )

Inserts the key with the item into the dictionary.

The key does not have to be a unique dictionary key. If multiple items are inserted with the same key, only the last item will be visible.

Null items are not allowed.

See also replace().

bool QAsciiDict::isEmpty () const

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

See also count().

QAsciiDict<type> & QAsciiDict::operator= ( const QAsciiDict<type> & dict )

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

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

type * QAsciiDict::operator[] ( const char * key ) const

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

This function uses an internal hashing algorithm to optimize lookup.

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

Equivalent to the find() function.

See also find().

QDataStream & QAsciiDict::read ( QDataStream & s, QPtrCollection::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 QAsciiDict::remove ( const char * key )

Removes the item associated with key from the dictionary. Returns TRUE if successful, or FALSE if the key does not exist in the dictionary.

If there are two or more items with equal keys, then the last inserted of these 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 traversal order.

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

void QAsciiDict::replace ( const char * key, const type * item )

Replaces an item that has a key equal to key with item.

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

Null items are not allowed.

Equivalent to:

    QAsciiDict<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 inserted of these will be replaced.

See also insert().

void QAsciiDict::resize ( uint newsize )

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

void QPtrCollection::setAutoDelete ( bool enable )

Sets the collection to auto-delete its contents if enable is TRUE and to never delete them if enable is FALSE.

If auto-deleting is turned on, all the items in a collection are deleted when the collection itself is deleted. This can be quite convenient if the collection has the only pointer to the items.

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.

See also autoDelete().

Examples: grapher/grapher.cpp, scribble/scribble.cpp and table/bigtable/main.cpp.

uint QAsciiDict::size () const

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

See also count().

void QAsciiDict::statistics () const

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

type * QAsciiDict::take ( const char * key )

Takes the item associated 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 inserted of these will be taken.

Returns a pointer to the item taken out, or null 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 & QAsciiDict::write ( QDataStream & s, QPtrCollection::Item ) const [virtual protected]

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

See also read().


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta2