The Q3Dict class is a template class that provides a dictionary based on QString keys. 更多...
| 头: | #include <Q3Dict> |
| 继承: | Q3PtrCollection |
| Q3Dict (int size = 17, bool caseSensitive = true) | |
| Q3Dict (const Q3Dict<type> & dict ) | |
| ~Q3Dict () | |
| 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 |
| virtual void | clear () |
| virtual uint | count () const |
| virtual QDataStream & | read (QDataStream & s , Q3PtrCollection::Item & item ) |
| virtual QDataStream & | write (QDataStream & s , Q3PtrCollection::Item item ) const |
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 ().
调用 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 () 被调用。 Q3Dict 's default implementation is to delete the item if auto-deletion is enabled.
另请参阅 Q3DictIterator , Q3AsciiDict , Q3IntDict ,和 Q3PtrDict .
Constructs a dictionary optimized for less than size 条目。
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.
若 caseSensitive is TRUE (the default), keys which differ only by case are considered different.
构造副本为 dict .
Each item in dict is inserted into this dictionary. Only the pointers are copied (shallow copy).
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.
另请参阅 setAutoDelete ().
[虚拟]
void
Q3Dict::
clear
()
重实现自 Q3PtrCollection::clear ().
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.
另请参阅 remove (), take (),和 setAutoDelete ().
[虚拟]
uint
Q3Dict::
count
() const
重实现自 Q3PtrCollection::count ().
Returns the number of items in the dictionary.
另请参阅 isEmpty ().
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.
另请参阅 operator[] ().
Inserts the key key 采用值 item into the dictionary.
Multiple items can have the same key, in which case only the last item will be accessible using operator[] ().
item 不可以为 0。
另请参阅 replace ().
Returns TRUE if the dictionary is empty, i.e. count () == 0; otherwise returns FALSE.
另请参阅 count ().
[virtual protected]
QDataStream
& Q3Dict::
read
(
QDataStream
&
s
,
Q3PtrCollection::Item
&
item
)
Reads a dictionary item from the stream s 并返回流引用。
The default implementation sets item 为 0。
另请参阅 write ().
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.
另请参阅 take (), clear (),和 setAutoDelete ().
Replaces the value of the key, key with item .
If the item does not already exist, it will be inserted.
item 不可以为 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.
另请参阅 insert ().
Changes the size of the hash table to newsize . The contents of the dictionary are preserved, but all iterators on the dictionary become invalid.
Returns the size of the internal hash array (as specified in the constructor).
另请参阅 count ().
Debugging-only function that prints out the dictionary distribution using qDebug ().
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.
另请参阅 remove (), clear (),和 setAutoDelete ().
[virtual protected]
QDataStream
& Q3Dict::
write
(
QDataStream
&
s
,
Q3PtrCollection::Item
item
) const
Writes a dictionary item 到流 s 并返回流引用。
另请参阅 read ().
赋值 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.
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 () 函数。
另请参阅 find ().