CopperSpice API
1.9.2
|
Template class which provides an unordered container of keys and values. More...
Classes | |
class | const_iterator |
STL style const iterator for QHash More... | |
class | iterator |
STL style non-const iterator for QHash More... | |
Public Typedefs | |
using | const_pointer = const Val * |
using | const_reference = const Val & |
using | difference_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::difference_type |
using | hasher = typename std::unordered_map< Key, Val, Hash, KeyEqual >::hasher |
using | key_equal = typename std::unordered_map< Key, Val, Hash, KeyEqual >::key_equal |
using | key_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::key_type |
using | mapped_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::mapped_type |
using | pointer = Val * |
using | reference = Val & |
using | size_type = typename std::unordered_map< Key, Val, Hash, KeyEqual >::difference_type |
using | value_type = Val |
Public Methods | |
QHash () = default | |
QHash (const Hash &hash, const KeyEqual &key=KeyEqual ()) | |
QHash (const QHash< Key, Val, Hash, KeyEqual > &other) = default | |
QHash (const std::unordered_map< Key, Val, Hash, KeyEqual > &other) | |
template<typename Input_Iterator > | |
QHash (Input_Iterator first, Input_Iterator last, const Hash &hash=Hash (), const KeyEqual &key=KeyEqual ()) | |
QHash (QHash< Key, Val, Hash, KeyEqual > &&other) = default | |
QHash (std::initializer_list< std::pair< const Key, Val >> list, const Hash &hash=Hash (), const KeyEqual &key=KeyEqual ()) | |
QHash (std::unordered_map< Key, Val, Hash, KeyEqual > &&other) | |
~QHash () = default | |
iterator | begin () |
const_iterator | begin () const |
size_type | capacity () const |
const_iterator | cbegin () const |
const_iterator | cend () const |
void | clear () |
const_iterator | constBegin () const |
const_iterator | constEnd () const |
const_iterator | constFind (const Key &key) const |
bool | contains (const Key &key) const |
size_type | count () const |
size_type | count (const Key &key) const |
bool | empty () const |
iterator | end () |
const_iterator | end () const |
QPair< iterator, iterator > | equal_range (const Key &key) |
QPair< const_iterator, const_iterator > | equal_range (const Key &key) const |
iterator | erase (const_iterator iter) |
iterator | find (const Key &key) |
const_iterator | find (const Key &key) const |
iterator | insert (const Key &key, const Val &value) |
iterator | insert (const std::pair< const Key, Val > &data) |
bool | isEmpty () const |
const Key | key (const Val &value) const |
const Key | key (const Val &value, const Key &defaultKey) const |
QList< Key > | keys () const |
QList< Key > | keys (const Val &value) const |
bool | operator!= (const QHash< Key, Val, Hash, KeyEqual > &other) const |
QHash< Key, Val, Hash, KeyEqual > & | operator= (const QHash< Key, Val, Hash, KeyEqual > &other) = default |
QHash< Key, Val, Hash, KeyEqual > & | operator= (QHash< Key, Val, Hash, KeyEqual > &&other) = default |
bool | operator== (const QHash< Key, Val, Hash, KeyEqual > &other) const |
Val & | operator[] (const Key &key) |
const Val | operator[] (const Key &key) const |
size_type | remove (const Key &key) |
void | reserve (size_type size) |
size_type | size () const |
void | squeeze () |
void | swap (QHash< Key, Val, Hash, KeyEqual > &other) |
Val | take (const Key &key) |
QHash< Key, Val, Hash, KeyEqual > & | unite (const QHash< Key, Val, Hash, KeyEqual > &other) |
Val | value (const Key &key) const |
Val | value (const Key &key, const Val &defaultValue) const |
QList< Val > | values () const |
Related Functions | |
These are not member functions | |
QDataStream & | operator<< (QDataStream &stream, const QHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator>> (QDataStream &stream, QHash< Key, Val, Hash, KeyEqual > &hash) |
uint | qHash (char key) |
uint | qHash (const QBitArray &key) |
uint | qHash (const QByteArray &key) |
uint | qHash (const QHostAddress &key, uint seed=0) |
uint | qHash (const QItemSelectionRange &key, uint seed=0) |
uint | qHash (const QList< T > &key, uint seed=0) |
uint | qHash (const QPair< T1, T2 > &key) |
uint | qHash (const QSharedPointer< T > &ptr, uint seed=0) |
uint | qHash (const QSourceLocation &location) |
uint | qHash (const QSslError &key, uint seed) |
uint | qHash (const QString &key) |
uint | qHash (const QTransform &key, uint seed=0) |
uint | qHash (const QUrl &key, uint seed=0) |
uint | qHash (const QUrlQuery &key, uint seed=0) |
uint | qHash (const QVector< T > &key, uint seed=0) |
uint | qHash (const QXmlName &name) |
uint | qHash (const QXmlNodeModelIndex &index) |
uint | qHash (const T *key) |
uint | qHash (int key) |
uint | qHash (long key) |
uint | qHash (QChar key) |
uint | qHash (qint64 key) |
uint | qHash (QSslEllipticCurve curve, uint seed=0) |
uint | qHash (quint64 key) |
uint | qHash (short key) |
uint | qHash (signed char key) |
uint | qHash (uchar key) |
uint | qHash (uint key) |
uint | qHash (ulong key) |
uint | qHash (ushort key) |
The QHash class is a template class which provides an unordered container of keys and values. This container stores the key and value as a pair. For a given key there is one corresponding value.
For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.
The following code is a simple example showing how to declare a QHash.
To insert a (key, value) pair into the hash use operator[]. This example inserts the following three (key, value) pairs into the QHash: ("one", 1), ("three", 3), and ("seven", 7).
Another way to insert items into the hash is to use the insert() method.
The following are examples to look up a value using the operator[] or value() methods.
If you want to check whether the hash contains a particular key use the method contains().
There is an overload for value() which accepts a second argument as a default value if there is no item with the specified key.
In general use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is because operator[]() will silently insert an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code will create 1000 items in memory.
To avoid this issue, replace hash[i]
with hash.value(i)
in the code above.
To navigate through all (key, value) pairs stored in a QHash use an iterator. QHash provides both Java style iterators (QHashIterator and QMutableHashIterator) and STL style iterators (QHash::const_iterator and QHash::iterator).
The following shows how to iterate over a QHash<QString, int> using a Java style iterator.
Here is the same code using an STL style iterator.
QHash is unordered so an iterator's sequence can not be assumed to be predictable. If ordering by key is required, use a QMap. QHash only allows only one value per key. To store multiple values for a given key use QMultiHash.
To extract the values from a hash and not the keys then use a range based.
Items can be removed from the hash in several ways. One way is to call remove() which will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). To clear the entire hash use clear().
The QHash<Key, Val> should be Default Constructible and Copy Constructible to work with all methods of this class. Data types like QObject are not copy constructible and therefore should never be stored in a container. You can however use a pointer to a QObject or any subclass as the type T.
The QHash Key type support operator==().
There must also be a global qHash() function which returns a hash value for an argument of the key's type. The following is a list of data types which can serve as keys in a QHash. The <QHash>
header file defines qHash() functions which compute hash values for the following data types.
If you want to use other data types as the Key, you will need to ensure you provide operator==() and a qHash() implementation.
The qHash() function computes a numeric value based on a key. It can use any algorithm imaginable, as long as it always returns the same value if given the same argument. In other words, if e1 == e2
, then qHash(e1) == qHash(e2)
must hold as well. However, to obtain good performance, the qHash() function should attempt to return different hash values for different keys to the largest extent possible.
In the example above the CopperSpice global qHash(const QString &) function is used to compute a hash value for the employee's name and it is combined with the day they were born. This will yield a different hash for people with the same name.
Internally, QHash uses a hash table to perform lookups. This hash table automatically grows and shrinks to provide fast lookups. You can control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain. This is not necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.
QHash< Key, Val, Hash, KeyEqual >::const_pointer |
Equivalent to const Val *.
QHash< Key, Val, Hash, KeyEqual >::const_reference |
Equivalent to const Val &.
QHash< Key, Val, Hash, KeyEqual >::difference_type |
Equivalent to integral type used to represent the distance between two elements.
QHash< Key, Val, Hash, KeyEqual >::hasher |
Equivalent to the hash object type.
QHash< Key, Val, Hash, KeyEqual >::key_equal |
Equivalent to the key equality comparison object type.
QHash< Key, Val, Hash, KeyEqual >::key_type |
Equivalent to Key.
QHash< Key, Val, Hash, KeyEqual >::mapped_type |
Equivalent to Val.
QHash< Key, Val, Hash, KeyEqual >::pointer |
Equivalent to Val *.
QHash< Key, Val, Hash, KeyEqual >::reference |
Equivalent to Val &.
QHash< Key, Val, Hash, KeyEqual >::size_type |
Equivalent to a signed integer of the appropriate size for your platform.
QHash< Key, Val, Hash, KeyEqual >::value_type |
Equivalent to Val.
|
default |
Constructs an empty QHash.
|
default |
Copy constructs a new QHash from other.
|
default |
Move constructs a new QHash from other.
|
inline |
Constructs a new QHash with a copy of each of the elements in the initializer list.
|
inlineexplicit |
Constructs a new QHash using the given hash and key objects.
|
inlineexplicit |
Copy constructs a new QHash from other.
|
inlineexplicit |
Move constructs a new QHash from other.
|
inline |
Constructs a hash using the elements between the first and last iterators.
|
default |
Destroys this QHash.
|
inline |
Returns an STL style iterator pointing to the first item in the hash.
|
inline |
Returns a cont STL style iterator pointing to the first item in the hash.
|
inline |
Returns the number of buckets in the QHash's internal hash table.
The purpose of this method is to provide a means of fine tuning QHash's memory usage. In general, you will rarely ever need to call this method. If you want to know how many items are in the hash call size().
|
inline |
|
inline |
|
inline |
Removes all items from the current QHash.
|
inline |
Returns a const STL style iterator pointing to the first item in the hash.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the hash.
|
inline |
Returns an iterator pointing to the item with the key in the hash. If the hash contains no item with the key the method returns constEnd().
|
inline |
Returns true if the hash contains an item with the key, otherwise it returns false.
|
inline |
Returns the number of items in this hash. Equivalent to calling size().
|
inline |
Returns the number of items associated with the key.
|
inline |
This method is identical to isEmpty(), returning true if the hash is empty otherwise returns false.
|
inline |
Returns an STL style iterator pointing to the imaginary item after the last item in the hash.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the hash.
|
inline |
Returns a pair of iterators corresponding to the range of items [first, second) which have the given key. If the range is empty then both iterators will be equal to end().
|
inline |
Returns a pair of iterators corresponding to the range of items [first, second) which have the given key. If the range is empty then both iterators will be equal to end().
|
inline |
Removes the (key, value) pair pointed to by the iterator iter from the hash and returns an iterator to the next item in the hash.
This method assumes the iterator is valid and refers to an element in the hash. It will not check if iter points past the end of the hash or is equal to the end() iterator. This method does not cause QHash to rehash its internal data structure. This means the method can safely be called while iterating and will not affect the order of items in the hash.
|
inline |
Returns an iterator pointing to the item with the key in the hash. If the hash contains no item with the key, the method returns end().
If the hash contains multiple items with the key, this method returns an iterator which points to the most recently inserted value. The other values are accessible by incrementing the iterator. The following is code which iterates over all the items with the same key:
|
inline |
Returns a const iterator pointing to the item with the key in the hash. If the hash contains no item with the key, the method returns end().
|
inline |
Inserts a new item with the key and value. If there is already an item with the key, the value for this item is replaced with value.
|
inline |
Inserts a new item with key and value from data, which is an std::pair. If there is already an item with this key, the value for the item is replaced with value.
|
inline |
Returns true if the hash contains no items, otherwise it returns false.
const Key QHash< Key, Val, Hash, KeyEqual >::key | ( | const Val & | value | ) | const |
Returns the first key mapped to value. If no item with the given value is found then a new value initialized object of type Key is returned. Refer to default constructed elements for additional information.
This method can be slow (linear time) because the internal data structure is optimized for fast lookup by key, not by value.
const Key QHash< Key, Val, Hash, KeyEqual >::key | ( | const Val & | value, |
const Key & | defaultKey | ||
) | const |
Returns the key of the first item matching the given value. Otherwise, returns the given defaultKey.
This method can be slow (linear time) because the internal data structure is optimized for fast lookup by key, not by value.
QList< Key > QHash< Key, Val, Hash, KeyEqual >::keys | ( | ) | const |
QList< Key > QHash< Key, Val, Hash, KeyEqual >::keys | ( | const Val & | value | ) | const |
Returns a list containing all the keys associated with value, in an arbitrary order.
This method can be slow (linear time), because the internal QHash data structure is optimized for fast lookup by key, not by value.
|
inline |
Returns true if other is not equal to this hash otherwise it returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This method requires the type T to support operator==().
|
default |
Copy assigns from other and returns a reference to this object.
|
default |
Move assigns from other and returns a reference to this object.
|
inline |
Returns true if other is equal to this hash, otherwise it returns false. Two hashes are considered equal if they contain the same (key, value) pairs. This method requires the type T to support operator==().
|
inline |
Returns a reference to the value for the element with the given key. If the key was not found then a new value initialized object of type Val with the given key is inserted. A reference to the new element will be returned. Refer to default constructed elements for additional information.
|
inline |
|
inline |
|
inline |
Ensures the internal hash table consists of at least size buckets. This method is useful for code that needs to build a huge hash and wants to avoid repeated reallocation.
Ideally, size should be slightly more than the maximum number of items expected in the hash. The size does not need to be a prime number. If size is underestimate, the QHash will resize automatically.
|
inline |
|
inline |
Reduces the size of the internal hash table to save memory. The purpose of this method is to provide a way to fine tune memory usage. Calling this method is rarely required.
|
inline |
Swaps hash other with this hash. This operation is very fast and never fails.
|
inline |
Removes the item with the given key and returns the value. The element is then erased. If you do not use the return value calling remove() will be more efficient. If the key was not found then a new value initialized object of type Val with the given key is returned. Refer to default constructed elements for additional information.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value and then erase the element. Call operator[] or find() and then access the data by calling iter.value(). Call erase() to remove the element.
|
inline |
Inserts all the items from the other hash into this hash. If a key is common to both hashes, the value in this hash will be retained.
|
inline |
Returns the value associated with the given key. If no item with the given key is found, a new value initialized object of type Val with the given key is returned. Refer to default constructed elements for additional information.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value. Call operator[] or find() and then access the data by calling iter.value().
|
inline |
Returns the value matching the given key. Otherwise, returns the defaultValue.
This method can not be called if the return type for Val is a "move only" type. For move only data types the following steps can be used to retrieve the value. Call find() and check if the key was found.
QList< Val > QHash< Key, Val, Hash, KeyEqual >::values | ( | ) | const |
|
related |
Writes the given hash to the stream. Returns a reference to the stream. Requires the type T to support operator<<().
Refer to Serializing Data Types for additional information.
|
related |
Reads from the stream into the given hash. Returns a reference to the stream. Requires the type T to support operator>>().
Refer to Serializing Data Types for additional information.
|
related |
Returns the hash value for the key.
|
related |
Returns the hash value for the key.
|
related |
Returns a hash of the host address key using seed to seed the calculation.
|
related |
Returns the hash value for key using seed to seed the calculation.
Returns the hash value for key using seed to seed the calculation. Requires qHash() to be overloaded for the value type T.
Returns the hash value for key. Template types T1 and T2 must be supported by qHash().
|
related |
Returns a hash of the shared pointer ptr using seed to seed the calculation.
|
related |
Computes a hash key for the QSourceLocation location.
Returns the hash value for the key using seed to seed the calculation.
|
related |
Returns the hash value for key using seed to seed the calculation.
Returns the hash value for key using seed to seed the calculation.
Returns the hash value for key using seed to seed the calculation.
Returns the hash value for key, using seed to seed the calculation. Requires qHash() to be overloaded for the value type T.
Returns the hash value based on the local name and the namespace URI in name. The prefix in name is not used in the computation.
|
related |
Returns the hash value for index.
The hash is computed on QXmlNodeModelIndex::data(), QXmlNodeModelIndex::additionalData(), and QXmlNodeModelIndex::model(). This means the hash key can be used for node indexes from different node models.
|
related |
Returns the hash value for the key.
|
related |
Returns the hash value for the key.
|
related |
Returns the hash value for the key.
|
related |
Returns an hash value for the curve, using seed to seed the calculation.
|
related |
Returns the hash value for the key.
|
related |
Returns the hash value for the key.