Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

Q3Dict Class

TheQ3Dict class is a template class that provides a dictionary based onQString keys.More...

Header:#include <Q3Dict>
Inherits:Q3PtrCollection

Public Functions

Q3Dict(int size = 17, bool caseSensitive = true)
Q3Dict(const Q3Dict<type> & dict)
~Q3Dict()
type *find(const QString & key) const
voidinsert(const QString & key, const type * item)
boolisEmpty() const
boolremove(const QString & key)
voidreplace(const QString & key, const type * item)
voidresize(uint newsize)
uintsize() const
voidstatistics() const
type *take(const QString & key)
Q3Dict<type> &operator=(const Q3Dict<type> & dict)
type *operator[](const QString & key) const

Reimplemented Public Functions

virtual voidclear()
virtual uintcount() const

Protected Functions

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

Detailed Description

TheQ3Dict class is a template class that provides a dictionary based onQString keys.

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

A dictionary is a collection of key-value pairs. The key is aQString 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-bitchar* keys, use theQ3AsciiDict template. AQ3Dict has the same performance as aQ3AsciiDict. If you want to have a dictionary that maps QStrings to QStrings useQMap.

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

Items are inserted withinsert(); 0 pointers cannot be inserted. Items are removed withremove(). All the items in a dictionary can be removed withclear(). The number of items in the dictionary is returned bycount(). If the dictionary contains no itemsisEmpty() returns TRUE. You can change an item's value withreplace(). Items are looked up with operator[](), or withfind() 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 withtake().

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

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

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

See alsoQ3DictIterator,Q3AsciiDict,Q3IntDict, andQ3PtrDict.

Member Function Documentation

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

Constructs a dictionary optimized for less thansize entries.

We recommend settingsize 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.

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

Q3Dict::Q3Dict(constQ3Dict<type> & dict)

Constructs a copy ofdict.

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

Q3Dict::~Q3Dict()

Removes all items from the dictionary and destroys it. IfsetAutoDelete() is TRUE, each value is deleted. All iterators that access this dictionary will be reset.

See alsosetAutoDelete().

[virtual]void Q3Dict::clear()

Reimplemented fromQ3PtrCollection::clear().

Removes all items from the dictionary.

The removed items are deleted ifauto-deletion is enabled.

All dictionary iterators that operate on the dictionary are reset.

See alsoremove(),take(), andsetAutoDelete().

[virtual]uint Q3Dict::count() const

Reimplemented fromQ3PtrCollection::count().

Returns the number of items in the dictionary.

See alsoisEmpty().

type * Q3Dict::find(constQString & key) const

Returns the item with keykey, 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 alsooperator[]().

void Q3Dict::insert(constQString & key, consttype * item)

Inserts the keykey with valueitem into the dictionary.

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

item may not be 0.

See alsoreplace().

bool Q3Dict::isEmpty() const

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

See alsocount().

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

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

The default implementation setsitem to 0.

See alsowrite().

bool Q3Dict::remove(constQString & key)

Removes the item withkey 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 ifauto-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 alsotake(),clear(), andsetAutoDelete().

void Q3Dict::replace(constQString & key, consttype * item)

Replaces the value of the key,key withitem.

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

void Q3Dict::resize(uint newsize)

Changes the size of the hash table tonewsize. 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 alsocount().

void Q3Dict::statistics() const

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

type * Q3Dict::take(constQString & key)

Takes the item withkey out of the dictionary without deleting it (even ifauto-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 alsoremove(),clear(), andsetAutoDelete().

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

Writes a dictionaryitem to the streams and returns a reference to the stream.

See alsoread().

Q3Dict<type> & Q3Dict::operator=(constQ3Dict<type> & dict)

Assignsdict to this dictionary and returns a reference to this dictionary.

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

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

Returns the item with keykey, 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 thefind() function.

See alsofind().

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp