Struct qt_core::QHashOfIntQByteArray
source · #[repr(C)]pub struct QHashOfIntQByteArray { /* private fields */ }
Expand description
The QHash class is a template class that provides a hash-table-based dictionary.
C++ class: QHash<int, QByteArray>
.
The QHash class is a template class that provides a hash-table-based dictionary.
QHash<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
QHash provides very similar functionality to QMap. The differences are:
- QHash provides faster lookups than QMap. (See Algorithmic Complexity for details.)
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
- The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash() (see qHash).
Here's an example QHash with QString keys and int
values:
To insert a (key, value) pair into the hash, you can use operator[]():
hash[“one”] = 1; hash[“three”] = 3; hash[“seven”] = 7;
This 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 insert():
hash.insert(“twelve”, 12);
To look up a value, use operator[]() or value():
int num1 = hash[“thirteen”]; int num2 = hash.value(“thirteen”);
If there is no item with the specified key in the hash, these functions return a default-constructed value.
If you want to check whether the hash contains a particular key, use contains():
int timeout = 30; if (hash.contains(“TIMEOUT”)) timeout = hash.value(“TIMEOUT”);
There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:
int timeout = hash.value(“TIMEOUT”, 30);
In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
// WRONG QHash<int, QWidget *> hash; ... for (int i = 0; i < 1000; ++i) { if (hash[i] == okButton) cout << “Found button at index “ << i << endl; }
To avoid this problem, replace hash[i]
with hash.value(i)
in the code above.
Internally, QHash uses a hash table to perform lookups. Unlike Qt 3's QDict
class, which needed to be initialized with a prime number, QHash's hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.
If you want to navigate through all the (key, value) pairs stored in a QHash, you can use an iterator. QHash provides both Java-style iterators (QHashIterator and QMutableHashIterator) and STL-style iterators (QHash::const_iterator and QHash::iterator). Here's how to iterate over a QHash<QString, int> using a Java-style iterator:
QHashIterator<QString, int> i(hash); while (i.hasNext()) { i.next(); cout << i.key() << “: “ << i.value() << endl; }
Here's the same code, but using an STL-style iterator:
QHash<QString, int>::const_iterator i = hash.constBegin(); while (i != hash.constEnd()) { cout << i.key() << “: “ << i.value() << endl; ++i; }
QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.
Normally, a QHash allows only one value per key. If you call insert() with a key that already exists in the QHash, the previous value is erased. For example:
hash.insert(“plenty”, 100); hash.insert(“plenty”, 2000); // hash.value(“plenty”) == 2000
However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiHash). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:
QList<int> values = hash.values(“plenty”); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl;
The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find() to get the iterator for the first item with a key and iterate from there:
QHash<QString, int>::iterator i = hash.find(“plenty”); while (i != hash.end() && i.key() == “plenty”) { cout << i.value() << endl; ++i; }
If you only need to extract the values from a hash (not the keys), you can also use foreach:
QHash<QString, int> hash; ... foreach (int value, hash) cout << value << endl;
Items can be removed from the hash in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). In addition, you can clear the entire hash using clear().
QHash's key and value data types must be assignable data types. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.
The qHash() hashing function
A QHash's key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a qHash() function in the type's namespace that returns a hash value for an argument of the key's type.
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.
For a key type K
, the qHash function must have one of these signatures:
uint qHash(K key); uint qHash(const K &key);
uint qHash(K key, uint seed); uint qHash(const K &key, uint seed);
The two-arguments overloads take an unsigned integer that should be used to seed the calculation of the hash function. This seed is provided by QHash in order to prevent a family of algorithmic complexity attacks. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter).
Here's a partial list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the <QHash>
header defines a qHash() function that computes an adequate hash value. Many other Qt classes also declare a qHash overload for their type; please refer to the documentation of each class.
If you want to use other types as the key, make sure that you provide operator==() and a qHash() implementation.
Example:
#ifndef EMPLOYEE_H #define EMPLOYEE_H
class Employee { public: Employee() {} Employee(const QString &name, const QDate &dateOfBirth); ...
private: QString myName; QDate myDateOfBirth; };
inline bool operator==(const Employee &e1, const Employee &e2) { return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth(); }
inline uint qHash(const Employee &key, uint seed) { return qHash(key.name(), seed) ^ key.dateOfBirth().day(); }
#endif // EMPLOYEE_H
In the example above, we've relied on Qt's global qHash(const QString &, uint) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.
Note that the implementation of the qHash() overloads offered by Qt may change at any time. You must not rely on the fact that qHash() will give the same results (for the same inputs) across different Qt versions.
Algorithmic complexity attacks
All hash tables are vulnerable to a particular class of denial of service attacks, in which the attacker carefully pre-computes a set of different keys that are going to be hashed in the same bucket of a hash table (or even have the very same hash value). The attack aims at getting the worst-case algorithmic behavior (O(n) instead of amortized O(1), see Algorithmic Complexity for the details) when the data is fed into the table.
In order to avoid this worst-case behavior, the calculation of the hash value done by qHash() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by QHash once per process, and then passed by QHash as the second argument of the two-arguments overload of the qHash() function.
This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED
. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function.
Implementations§
source§impl QHashOfIntQByteArray
impl QHashOfIntQByteArray
sourcepub unsafe fn begin_mut(&self) -> CppBox<Iterator>
pub unsafe fn begin_mut(&self) -> CppBox<Iterator>
Returns an STL-style iterator pointing to the first item in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::begin()
.
Returns an STL-style iterator pointing to the first item in the hash.
See also constBegin() and end().
sourcepub unsafe fn begin(&self) -> CppBox<ConstIterator>
pub unsafe fn begin(&self) -> CppBox<ConstIterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::begin() const
.
This is an overloaded function.
sourcepub unsafe fn capacity(&self) -> c_int
pub unsafe fn capacity(&self) -> c_int
Returns the number of buckets in the QHash's internal hash table.
Calls C++ function: int QHash<int, QByteArray>::capacity() const
.
sourcepub unsafe fn cbegin(&self) -> CppBox<ConstIterator>
pub unsafe fn cbegin(&self) -> CppBox<ConstIterator>
Returns a const STL-style iterator pointing to the first item in the hash.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::cbegin() const
.
Returns a const STL-style iterator pointing to the first item in the hash.
This function was introduced in Qt 5.0.
sourcepub unsafe fn cend(&self) -> CppBox<ConstIterator>
pub unsafe fn cend(&self) -> CppBox<ConstIterator>
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::cend() const
.
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash.
This function was introduced in Qt 5.0.
sourcepub unsafe fn clear(&self)
pub unsafe fn clear(&self)
Removes all items from the hash.
Calls C++ function: void QHash<int, QByteArray>::clear()
.
Removes all items from the hash.
See also remove().
sourcepub unsafe fn const_begin(&self) -> CppBox<ConstIterator>
pub unsafe fn const_begin(&self) -> CppBox<ConstIterator>
Returns a const STL-style iterator pointing to the first item in the hash.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::constBegin() const
.
Returns a const STL-style iterator pointing to the first item in the hash.
sourcepub unsafe fn const_end(&self) -> CppBox<ConstIterator>
pub unsafe fn const_end(&self) -> CppBox<ConstIterator>
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::constEnd() const
.
Returns a const STL-style iterator pointing to the imaginary item after the last item in the hash.
See also constBegin() and end().
sourcepub unsafe fn const_find(&self, key: *const c_int) -> CppBox<ConstIterator>
pub unsafe fn const_find(&self, key: *const c_int) -> CppBox<ConstIterator>
Returns an iterator pointing to the item with the key in the hash.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::constFind(const int& key) const
.
Returns an iterator pointing to the item with the key in the hash.
If the hash contains no item with the key, the function returns constEnd().
This function was introduced in Qt 4.1.
See also find() and QMultiHash::constFind().
sourcepub unsafe fn contains(&self, key: *const c_int) -> bool
pub unsafe fn contains(&self, key: *const c_int) -> bool
Returns true
if the hash contains an item with the key; otherwise returns false
.
Calls C++ function: bool QHash<int, QByteArray>::contains(const int& key) const
.
Returns true
if the hash contains an item with the key; otherwise returns false
.
See also count() and QMultiHash::contains().
sourcepub unsafe fn copy_from(
&self,
other: impl CastInto<Ref<QHashOfIntQByteArray>>
) -> Ref<QHashOfIntQByteArray>
pub unsafe fn copy_from( &self, other: impl CastInto<Ref<QHashOfIntQByteArray>> ) -> Ref<QHashOfIntQByteArray>
The QHash class is a template class that provides a hash-table-based dictionary.
Calls C++ function: QHash<int, QByteArray>& QHash<int, QByteArray>::operator=(const QHash<int, QByteArray>& other)
.
The QHash class is a template class that provides a hash-table-based dictionary.
QHash<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
QHash provides very similar functionality to QMap. The differences are:
- QHash provides faster lookups than QMap. (See Algorithmic Complexity for details.)
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
- The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash() (see qHash).
Here's an example QHash with QString keys and int
values:
To insert a (key, value) pair into the hash, you can use operator[]():
hash[“one”] = 1; hash[“three”] = 3; hash[“seven”] = 7;
This 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 insert():
hash.insert(“twelve”, 12);
To look up a value, use operator[]() or value():
int num1 = hash[“thirteen”]; int num2 = hash.value(“thirteen”);
If there is no item with the specified key in the hash, these functions return a default-constructed value.
If you want to check whether the hash contains a particular key, use contains():
int timeout = 30; if (hash.contains(“TIMEOUT”)) timeout = hash.value(“TIMEOUT”);
There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:
int timeout = hash.value(“TIMEOUT”, 30);
In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
// WRONG QHash<int, QWidget *> hash; ... for (int i = 0; i < 1000; ++i) { if (hash[i] == okButton) cout << “Found button at index “ << i << endl; }
To avoid this problem, replace hash[i]
with hash.value(i)
in the code above.
Internally, QHash uses a hash table to perform lookups. Unlike Qt 3's QDict
class, which needed to be initialized with a prime number, QHash's hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.
If you want to navigate through all the (key, value) pairs stored in a QHash, you can use an iterator. QHash provides both Java-style iterators (QHashIterator and QMutableHashIterator) and STL-style iterators (QHash::const_iterator and QHash::iterator). Here's how to iterate over a QHash<QString, int> using a Java-style iterator:
QHashIterator<QString, int> i(hash); while (i.hasNext()) { i.next(); cout << i.key() << “: “ << i.value() << endl; }
Here's the same code, but using an STL-style iterator:
QHash<QString, int>::const_iterator i = hash.constBegin(); while (i != hash.constEnd()) { cout << i.key() << “: “ << i.value() << endl; ++i; }
QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.
Normally, a QHash allows only one value per key. If you call insert() with a key that already exists in the QHash, the previous value is erased. For example:
hash.insert(“plenty”, 100); hash.insert(“plenty”, 2000); // hash.value(“plenty”) == 2000
However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiHash). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:
QList<int> values = hash.values(“plenty”); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl;
The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find() to get the iterator for the first item with a key and iterate from there:
QHash<QString, int>::iterator i = hash.find(“plenty”); while (i != hash.end() && i.key() == “plenty”) { cout << i.value() << endl; ++i; }
If you only need to extract the values from a hash (not the keys), you can also use foreach:
QHash<QString, int> hash; ... foreach (int value, hash) cout << value << endl;
Items can be removed from the hash in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). In addition, you can clear the entire hash using clear().
QHash's key and value data types must be assignable data types. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.
The qHash() hashing function
A QHash's key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a qHash() function in the type's namespace that returns a hash value for an argument of the key's type.
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.
For a key type K
, the qHash function must have one of these signatures:
uint qHash(K key); uint qHash(const K &key);
uint qHash(K key, uint seed); uint qHash(const K &key, uint seed);
The two-arguments overloads take an unsigned integer that should be used to seed the calculation of the hash function. This seed is provided by QHash in order to prevent a family of algorithmic complexity attacks. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter).
Here's a partial list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the <QHash>
header defines a qHash() function that computes an adequate hash value. Many other Qt classes also declare a qHash overload for their type; please refer to the documentation of each class.
If you want to use other types as the key, make sure that you provide operator==() and a qHash() implementation.
Example:
#ifndef EMPLOYEE_H #define EMPLOYEE_H
class Employee { public: Employee() {} Employee(const QString &name, const QDate &dateOfBirth); ...
private: QString myName; QDate myDateOfBirth; };
inline bool operator==(const Employee &e1, const Employee &e2) { return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth(); }
inline uint qHash(const Employee &key, uint seed) { return qHash(key.name(), seed) ^ key.dateOfBirth().day(); }
#endif // EMPLOYEE_H
In the example above, we've relied on Qt's global qHash(const QString &, uint) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.
Note that the implementation of the qHash() overloads offered by Qt may change at any time. You must not rely on the fact that qHash() will give the same results (for the same inputs) across different Qt versions.
Algorithmic complexity attacks
All hash tables are vulnerable to a particular class of denial of service attacks, in which the attacker carefully pre-computes a set of different keys that are going to be hashed in the same bucket of a hash table (or even have the very same hash value). The attack aims at getting the worst-case algorithmic behavior (O(n) instead of amortized O(1), see Algorithmic Complexity for the details) when the data is fed into the table.
In order to avoid this worst-case behavior, the calculation of the hash value done by qHash() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by QHash once per process, and then passed by QHash as the second argument of the two-arguments overload of the qHash() function.
This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED
. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function.
sourcepub unsafe fn count_1a(&self, key: *const c_int) -> c_int
pub unsafe fn count_1a(&self, key: *const c_int) -> c_int
Returns the number of items associated with the key.
Calls C++ function: int QHash<int, QByteArray>::count(const int& key) const
.
Returns the number of items associated with the key.
See also contains() and insertMulti().
sourcepub unsafe fn count_0a(&self) -> c_int
pub unsafe fn count_0a(&self) -> c_int
This is an overloaded function.
Calls C++ function: int QHash<int, QByteArray>::count() const
.
This is an overloaded function.
Same as size().
sourcepub unsafe fn empty(&self) -> bool
pub unsafe fn empty(&self) -> bool
This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the hash is empty; otherwise returns false
.
Calls C++ function: bool QHash<int, QByteArray>::empty() const
.
This function is provided for STL compatibility. It is equivalent to isEmpty(), returning true if the hash is empty; otherwise returns false
.
sourcepub unsafe fn end_mut(&self) -> CppBox<Iterator>
pub unsafe fn end_mut(&self) -> CppBox<Iterator>
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::end()
.
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash.
sourcepub unsafe fn end(&self) -> CppBox<ConstIterator>
pub unsafe fn end(&self) -> CppBox<ConstIterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::end() const
.
This is an overloaded function.
sourcepub unsafe fn erase_iterator(
&self,
it: impl CastInto<Ref<Iterator>>
) -> CppBox<Iterator>
pub unsafe fn erase_iterator( &self, it: impl CastInto<Ref<Iterator>> ) -> CppBox<Iterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::erase(QHash<int, QByteArray>::iterator it)
.
This is an overloaded function.
sourcepub unsafe fn erase_const_iterator(
&self,
it: impl CastInto<Ref<ConstIterator>>
) -> CppBox<Iterator>
pub unsafe fn erase_const_iterator( &self, it: impl CastInto<Ref<ConstIterator>> ) -> CppBox<Iterator>
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::erase(QHash<int, QByteArray>::const_iterator it)
.
Removes the (key, value) pair associated with the iterator pos from the hash, and returns an iterator to the next item in the hash.
Unlike remove() and take(), this function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash. For example:
QHash<QObject , int> objectHash; ... QHash<QObject , int>::iterator i = objectHash.find(obj); while (i != objectHash.end() && i.key() == obj) { if (i.value() == 0) { i = objectHash.erase(i); } else { ++i; } }
This function was introduced in Qt 5.7.
sourcepub unsafe fn find_mut(&self, key: *const c_int) -> CppBox<Iterator>
pub unsafe fn find_mut(&self, key: *const c_int) -> CppBox<Iterator>
Returns an iterator pointing to the item with the key in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::find(const int& key)
.
Returns an iterator pointing to the item with the key in the hash.
If the hash contains no item with the key, the function returns end().
If the hash contains multiple items with the key, this function returns an iterator that points to the most recently inserted value. The other values are accessible by incrementing the iterator. For example, here's some code that iterates over all the items with the same key:
QHash<QString, int> hash; ... QHash<QString, int>::const_iterator i = hash.find(“HDR”); while (i != hash.end() && i.key() == “HDR”) { cout << i.value() << endl; ++i; }
See also value(), values(), and QMultiHash::find().
sourcepub unsafe fn find(&self, key: *const c_int) -> CppBox<ConstIterator>
pub unsafe fn find(&self, key: *const c_int) -> CppBox<ConstIterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::find(const int& key) const
.
This is an overloaded function.
sourcepub unsafe fn index_mut(&self, key: *const c_int) -> Ref<QByteArray>
pub unsafe fn index_mut(&self, key: *const c_int) -> Ref<QByteArray>
Returns the value associated with the key as a modifiable reference.
Calls C++ function: QByteArray& QHash<int, QByteArray>::operator[](const int& key)
.
Returns the value associated with the key as a modifiable reference.
If the hash contains no item with the key, the function inserts a default-constructed value into the hash with the key, and returns a reference to it. If the hash contains multiple items with the key, this function returns a reference to the most recently inserted value.
sourcepub unsafe fn index(&self, key: *const c_int) -> CppBox<QByteArray>
pub unsafe fn index(&self, key: *const c_int) -> CppBox<QByteArray>
This is an overloaded function.
Calls C++ function: QByteArray QHash<int, QByteArray>::operator[](const int& key) const
.
This is an overloaded function.
Same as value().
sourcepub unsafe fn insert(
&self,
key: *const c_int,
value: impl CastInto<Ref<QByteArray>>
) -> CppBox<Iterator>
pub unsafe fn insert( &self, key: *const c_int, value: impl CastInto<Ref<QByteArray>> ) -> CppBox<Iterator>
Inserts a new item with the key and a value of value.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::insert(const int& key, const QByteArray& value)
.
Inserts a new item with the key and a value of value.
If there is already an item with the key, that item's value is replaced with value.
If there are multiple items with the key, the most recently inserted item's value is replaced with value.
See also insertMulti().
sourcepub unsafe fn insert_multi(
&self,
key: *const c_int,
value: impl CastInto<Ref<QByteArray>>
) -> CppBox<Iterator>
pub unsafe fn insert_multi( &self, key: *const c_int, value: impl CastInto<Ref<QByteArray>> ) -> CppBox<Iterator>
Inserts a new item with the key and a value of value.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::insertMulti(const int& key, const QByteArray& value)
.
sourcepub unsafe fn is_detached(&self) -> bool
pub unsafe fn is_detached(&self) -> bool
Calls C++ function: bool QHash<int, QByteArray>::isDetached() const
.
sourcepub unsafe fn is_empty(&self) -> bool
pub unsafe fn is_empty(&self) -> bool
Returns true
if the hash contains no items; otherwise returns false.
Calls C++ function: bool QHash<int, QByteArray>::isEmpty() const
.
Returns true
if the hash contains no items; otherwise returns false.
See also size().
sourcepub unsafe fn key_1a(&self, value: impl CastInto<Ref<QByteArray>>) -> c_int
pub unsafe fn key_1a(&self, value: impl CastInto<Ref<QByteArray>>) -> c_int
Returns the first key mapped to value.
Calls C++ function: int QHash<int, QByteArray>::key(const QByteArray& value) const
.
Returns the first key mapped to value.
If the hash contains no item with the value, the function returns a default-constructed key.
This function can be slow (linear time), because QHash's internal data structure is optimized for fast lookup by key, not by value.
sourcepub unsafe fn key_2a(
&self,
value: impl CastInto<Ref<QByteArray>>,
default_key: *const c_int
) -> c_int
pub unsafe fn key_2a( &self, value: impl CastInto<Ref<QByteArray>>, default_key: *const c_int ) -> c_int
This is an overloaded function.
Calls C++ function: int QHash<int, QByteArray>::key(const QByteArray& value, const int& defaultKey) const
.
This is an overloaded function.
Returns the first key mapped to value, or defaultKey if the hash contains no item mapped to value.
This function can be slow (linear time), because QHash's internal data structure is optimized for fast lookup by key, not by value.
This function was introduced in Qt 4.3.
sourcepub unsafe fn key_begin(&self) -> CppBox<KeyIterator>
pub unsafe fn key_begin(&self) -> CppBox<KeyIterator>
Returns a const STL-style iterator pointing to the first key in the hash.
Calls C++ function: QHash<int, QByteArray>::key_iterator QHash<int, QByteArray>::keyBegin() const
.
Returns a const STL-style iterator pointing to the first key in the hash.
This function was introduced in Qt 5.6.
See also keyEnd().
sourcepub unsafe fn key_end(&self) -> CppBox<KeyIterator>
pub unsafe fn key_end(&self) -> CppBox<KeyIterator>
Returns a const STL-style iterator pointing to the imaginary item after the last key in the hash.
Calls C++ function: QHash<int, QByteArray>::key_iterator QHash<int, QByteArray>::keyEnd() const
.
Returns a const STL-style iterator pointing to the imaginary item after the last key in the hash.
This function was introduced in Qt 5.6.
See also keyBegin().
sourcepub unsafe fn keys_0a(&self) -> CppBox<QListOfInt>
pub unsafe fn keys_0a(&self) -> CppBox<QListOfInt>
Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (because items were inserted with insertMulti(), or unite() was used) also occur multiple times in the list.
Calls C++ function: QList<int> QHash<int, QByteArray>::keys() const
.
Returns a list containing all the keys in the hash, in an arbitrary order. Keys that occur multiple times in the hash (because items were inserted with insertMulti(), or unite() was used) also occur multiple times in the list.
To obtain a list of unique keys, where each key from the map only occurs once, use uniqueKeys().
The order is guaranteed to be the same as that used by values().
See also uniqueKeys(), values(), and key().
sourcepub unsafe fn keys_1a(
&self,
value: impl CastInto<Ref<QByteArray>>
) -> CppBox<QListOfInt>
pub unsafe fn keys_1a( &self, value: impl CastInto<Ref<QByteArray>> ) -> CppBox<QListOfInt>
This is an overloaded function.
Calls C++ function: QList<int> QHash<int, QByteArray>::keys(const QByteArray& value) const
.
This is an overloaded function.
Returns a list containing all the keys associated with value value, in an arbitrary order.
This function can be slow (linear time), because QHash's internal data structure is optimized for fast lookup by key, not by value.
sourcepub unsafe fn new() -> CppBox<QHashOfIntQByteArray>
pub unsafe fn new() -> CppBox<QHashOfIntQByteArray>
Constructs an empty hash.
Calls C++ function: [constructor] void QHash<int, QByteArray>::QHash()
.
Constructs an empty hash.
See also clear().
sourcepub unsafe fn new_copy(
other: impl CastInto<Ref<QHashOfIntQByteArray>>
) -> CppBox<QHashOfIntQByteArray>
pub unsafe fn new_copy( other: impl CastInto<Ref<QHashOfIntQByteArray>> ) -> CppBox<QHashOfIntQByteArray>
The QHash class is a template class that provides a hash-table-based dictionary.
Calls C++ function: [constructor] void QHash<int, QByteArray>::QHash(const QHash<int, QByteArray>& other)
.
The QHash class is a template class that provides a hash-table-based dictionary.
QHash<Key, T> is one of Qt's generic container classes. It stores (key, value) pairs and provides very fast lookup of the value associated with a key.
QHash provides very similar functionality to QMap. The differences are:
- QHash provides faster lookups than QMap. (See Algorithmic Complexity for details.)
- When iterating over a QMap, the items are always sorted by key. With QHash, the items are arbitrarily ordered.
- The key type of a QMap must provide operator<(). The key type of a QHash must provide operator==() and a global hash function called qHash() (see qHash).
Here's an example QHash with QString keys and int
values:
To insert a (key, value) pair into the hash, you can use operator[]():
hash[“one”] = 1; hash[“three”] = 3; hash[“seven”] = 7;
This 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 insert():
hash.insert(“twelve”, 12);
To look up a value, use operator[]() or value():
int num1 = hash[“thirteen”]; int num2 = hash.value(“thirteen”);
If there is no item with the specified key in the hash, these functions return a default-constructed value.
If you want to check whether the hash contains a particular key, use contains():
int timeout = 30; if (hash.contains(“TIMEOUT”)) timeout = hash.value(“TIMEOUT”);
There is also a value() overload that uses its second argument as a default value if there is no item with the specified key:
int timeout = hash.value(“TIMEOUT”, 30);
In general, we recommend that you use contains() and value() rather than operator[]() for looking up a key in a hash. The reason is that operator[]() silently inserts an item into the hash if no item exists with the same key (unless the hash is const). For example, the following code snippet will create 1000 items in memory:
// WRONG QHash<int, QWidget *> hash; ... for (int i = 0; i < 1000; ++i) { if (hash[i] == okButton) cout << “Found button at index “ << i << endl; }
To avoid this problem, replace hash[i]
with hash.value(i)
in the code above.
Internally, QHash uses a hash table to perform lookups. Unlike Qt 3's QDict
class, which needed to be initialized with a prime number, QHash's hash table automatically grows and shrinks to provide fast lookups without wasting too much memory. You can still control the size of the hash table by calling reserve() if you already know approximately how many items the QHash will contain, but this isn't necessary to obtain good performance. You can also call capacity() to retrieve the hash table's size.
If you want to navigate through all the (key, value) pairs stored in a QHash, you can use an iterator. QHash provides both Java-style iterators (QHashIterator and QMutableHashIterator) and STL-style iterators (QHash::const_iterator and QHash::iterator). Here's how to iterate over a QHash<QString, int> using a Java-style iterator:
QHashIterator<QString, int> i(hash); while (i.hasNext()) { i.next(); cout << i.key() << “: “ << i.value() << endl; }
Here's the same code, but using an STL-style iterator:
QHash<QString, int>::const_iterator i = hash.constBegin(); while (i != hash.constEnd()) { cout << i.key() << “: “ << i.value() << endl; ++i; }
QHash is unordered, so an iterator's sequence cannot be assumed to be predictable. If ordering by key is required, use a QMap.
Normally, a QHash allows only one value per key. If you call insert() with a key that already exists in the QHash, the previous value is erased. For example:
hash.insert(“plenty”, 100); hash.insert(“plenty”, 2000); // hash.value(“plenty”) == 2000
However, you can store multiple values per key by using insertMulti() instead of insert() (or using the convenience subclass QMultiHash). If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a QList<T>:
QList<int> values = hash.values(“plenty”); for (int i = 0; i < values.size(); ++i) cout << values.at(i) << endl;
The items that share the same key are available from most recently to least recently inserted. A more efficient approach is to call find() to get the iterator for the first item with a key and iterate from there:
QHash<QString, int>::iterator i = hash.find(“plenty”); while (i != hash.end() && i.key() == “plenty”) { cout << i.value() << endl; ++i; }
If you only need to extract the values from a hash (not the keys), you can also use foreach:
QHash<QString, int> hash; ... foreach (int value, hash) cout << value << endl;
Items can be removed from the hash in several ways. One way is to call remove(); this will remove any item with the given key. Another way is to use QMutableHashIterator::remove(). In addition, you can clear the entire hash using clear().
QHash's key and value data types must be assignable data types. You cannot, for example, store a QWidget as a value; instead, store a QWidget *.
The qHash() hashing function
A QHash's key type has additional requirements other than being an assignable data type: it must provide operator==(), and there must also be a qHash() function in the type's namespace that returns a hash value for an argument of the key's type.
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.
For a key type K
, the qHash function must have one of these signatures:
uint qHash(K key); uint qHash(const K &key);
uint qHash(K key, uint seed); uint qHash(const K &key, uint seed);
The two-arguments overloads take an unsigned integer that should be used to seed the calculation of the hash function. This seed is provided by QHash in order to prevent a family of algorithmic complexity attacks. If both a one-argument and a two-arguments overload are defined for a key type, the latter is used by QHash (note that you can simply define a two-arguments version, and use a default value for the seed parameter).
Here's a partial list of the C++ and Qt types that can serve as keys in a QHash: any integer type (char, unsigned long, etc.), any pointer type, QChar, QString, and QByteArray. For all of these, the <QHash>
header defines a qHash() function that computes an adequate hash value. Many other Qt classes also declare a qHash overload for their type; please refer to the documentation of each class.
If you want to use other types as the key, make sure that you provide operator==() and a qHash() implementation.
Example:
#ifndef EMPLOYEE_H #define EMPLOYEE_H
class Employee { public: Employee() {} Employee(const QString &name, const QDate &dateOfBirth); ...
private: QString myName; QDate myDateOfBirth; };
inline bool operator==(const Employee &e1, const Employee &e2) { return e1.name() == e2.name() && e1.dateOfBirth() == e2.dateOfBirth(); }
inline uint qHash(const Employee &key, uint seed) { return qHash(key.name(), seed) ^ key.dateOfBirth().day(); }
#endif // EMPLOYEE_H
In the example above, we've relied on Qt's global qHash(const QString &, uint) to give us a hash value for the employee's name, and XOR'ed this with the day they were born to help produce unique hashes for people with the same name.
Note that the implementation of the qHash() overloads offered by Qt may change at any time. You must not rely on the fact that qHash() will give the same results (for the same inputs) across different Qt versions.
Algorithmic complexity attacks
All hash tables are vulnerable to a particular class of denial of service attacks, in which the attacker carefully pre-computes a set of different keys that are going to be hashed in the same bucket of a hash table (or even have the very same hash value). The attack aims at getting the worst-case algorithmic behavior (O(n) instead of amortized O(1), see Algorithmic Complexity for the details) when the data is fed into the table.
In order to avoid this worst-case behavior, the calculation of the hash value done by qHash() can be salted by a random seed, that nullifies the attack's extent. This seed is automatically generated by QHash once per process, and then passed by QHash as the second argument of the two-arguments overload of the qHash() function.
This randomization of QHash is enabled by default. Even though programs should never depend on a particular QHash ordering, there may be situations where you temporarily need deterministic behavior, for example for debugging or regression testing. To disable the randomization, define the environment variable QT_HASH_SEED
. The contents of that variable, interpreted as a decimal value, will be used as the seed for qHash(). Alternatively, you can call the qSetGlobalQHashSeed() function.
sourcepub unsafe fn remove(&self, key: *const c_int) -> c_int
pub unsafe fn remove(&self, key: *const c_int) -> c_int
Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn't in the hash, or greater than 1 if insertMulti() has been used with the key.
Calls C++ function: int QHash<int, QByteArray>::remove(const int& key)
.
Removes all the items that have the key from the hash. Returns the number of items removed which is usually 1 but will be 0 if the key isn’t in the hash, or greater than 1 if insertMulti() has been used with the key.
See also clear(), take(), and QMultiHash::remove().
sourcepub unsafe fn reserve(&self, size: c_int)
pub unsafe fn reserve(&self, size: c_int)
Ensures that the QHash's internal hash table consists of at least size buckets.
Calls C++ function: void QHash<int, QByteArray>::reserve(int size)
.
Ensures that the QHash’s internal hash table consists of at least size buckets.
This function is useful for code that needs to build a huge hash and wants to avoid repeated reallocation. For example:
QHash<QString, int> hash; hash.reserve(20000); for (int i = 0; i < 20000; ++i) hash.insert(keys[i], values[i]);
Ideally, size should be slightly more than the maximum number of items expected in the hash. size doesn't have to be prime, because QHash will use a prime number internally anyway. If size is an underestimate, the worst that will happen is that the QHash will be a bit slower.
In general, you will rarely ever need to call this function. QHash's internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.
sourcepub unsafe fn set_sharable(&self, sharable: bool)
pub unsafe fn set_sharable(&self, sharable: bool)
Calls C++ function: void QHash<int, QByteArray>::setSharable(bool sharable)
.
sourcepub unsafe fn size(&self) -> c_int
pub unsafe fn size(&self) -> c_int
Returns the number of items in the hash.
Calls C++ function: int QHash<int, QByteArray>::size() const
.
sourcepub unsafe fn squeeze(&self)
pub unsafe fn squeeze(&self)
Reduces the size of the QHash's internal hash table to save memory.
Calls C++ function: void QHash<int, QByteArray>::squeeze()
.
sourcepub unsafe fn take(&self, key: *const c_int) -> CppBox<QByteArray>
pub unsafe fn take(&self, key: *const c_int) -> CppBox<QByteArray>
Removes the item with the key from the hash and returns the value associated with it.
Calls C++ function: QByteArray QHash<int, QByteArray>::take(const int& key)
.
Removes the item with the key from the hash and returns the value associated with it.
If the item does not exist in the hash, the function simply returns a default-constructed value. If there are multiple items for key in the hash, only the most recently inserted one is removed.
If you don't use the return value, remove() is more efficient.
See also remove().
sourcepub unsafe fn unique_keys(&self) -> CppBox<QListOfInt>
pub unsafe fn unique_keys(&self) -> CppBox<QListOfInt>
Returns a list containing all the keys in the map. Keys that occur multiple times in the map (because items were inserted with insertMulti(), or unite() was used) occur only once in the returned list.
Calls C++ function: QList<int> QHash<int, QByteArray>::uniqueKeys() const
.
Returns a list containing all the keys in the map. Keys that occur multiple times in the map (because items were inserted with insertMulti(), or unite() was used) occur only once in the returned list.
This function was introduced in Qt 4.2.
sourcepub unsafe fn value_1a(&self, key: *const c_int) -> CppBox<QByteArray>
pub unsafe fn value_1a(&self, key: *const c_int) -> CppBox<QByteArray>
Returns the value associated with the key.
Calls C++ function: QByteArray QHash<int, QByteArray>::value(const int& key) const
.
Returns the value associated with the key.
If the hash contains no item with the key, the function returns a default-constructed value. If there are multiple items for the key in the hash, the value of the most recently inserted one is returned.
See also key(), values(), contains(), and operator[]().
sourcepub unsafe fn value_2a(
&self,
key: *const c_int,
default_value: impl CastInto<Ref<QByteArray>>
) -> CppBox<QByteArray>
pub unsafe fn value_2a( &self, key: *const c_int, default_value: impl CastInto<Ref<QByteArray>> ) -> CppBox<QByteArray>
This is an overloaded function.
Calls C++ function: QByteArray QHash<int, QByteArray>::value(const int& key, const QByteArray& defaultValue) const
.
This is an overloaded function.
If the hash contains no item with the given key, the function returns defaultValue.
sourcepub unsafe fn values_0a(&self) -> CppBox<QListOfQByteArray>
pub unsafe fn values_0a(&self) -> CppBox<QListOfQByteArray>
Returns a list containing all the values in the hash, in an arbitrary order. If a key is associated with multiple values, all of its values will be in the list, and not just the most recently inserted one.
Calls C++ function: QList<QByteArray> QHash<int, QByteArray>::values() const
.
sourcepub unsafe fn values_1a(&self, key: *const c_int) -> CppBox<QListOfQByteArray>
pub unsafe fn values_1a(&self, key: *const c_int) -> CppBox<QListOfQByteArray>
This is an overloaded function.
Calls C++ function: QList<QByteArray> QHash<int, QByteArray>::values(const int& key) const
.
This is an overloaded function.
Returns a list of all the values associated with the key, from the most recently inserted to the least recently inserted.
See also count() and insertMulti().
Trait Implementations§
source§impl Begin for QHashOfIntQByteArray
impl Begin for QHashOfIntQByteArray
source§unsafe fn begin(&self) -> CppBox<ConstIterator>
unsafe fn begin(&self) -> CppBox<ConstIterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::begin() const
.
This is an overloaded function.
§type Output = CppBox<ConstIterator>
type Output = CppBox<ConstIterator>
source§impl BeginMut for QHashOfIntQByteArray
impl BeginMut for QHashOfIntQByteArray
source§unsafe fn begin_mut(&self) -> CppBox<Iterator>
unsafe fn begin_mut(&self) -> CppBox<Iterator>
Returns an STL-style iterator pointing to the first item in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::begin()
.
Returns an STL-style iterator pointing to the first item in the hash.
See also constBegin() and end().
source§impl CppDeletable for QHashOfIntQByteArray
impl CppDeletable for QHashOfIntQByteArray
source§unsafe fn delete(&self)
unsafe fn delete(&self)
Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.
Calls C++ function: [destructor] void QHash<int, QByteArray>::~QHash()
.
Destroys the hash. References to the values in the hash and all iterators of this hash become invalid.
source§impl End for QHashOfIntQByteArray
impl End for QHashOfIntQByteArray
source§unsafe fn end(&self) -> CppBox<ConstIterator>
unsafe fn end(&self) -> CppBox<ConstIterator>
This is an overloaded function.
Calls C++ function: QHash<int, QByteArray>::const_iterator QHash<int, QByteArray>::end() const
.
This is an overloaded function.
§type Output = CppBox<ConstIterator>
type Output = CppBox<ConstIterator>
source§impl EndMut for QHashOfIntQByteArray
impl EndMut for QHashOfIntQByteArray
source§unsafe fn end_mut(&self) -> CppBox<Iterator>
unsafe fn end_mut(&self) -> CppBox<Iterator>
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash.
Calls C++ function: QHash<int, QByteArray>::iterator QHash<int, QByteArray>::end()
.
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash.