CopperSpice API
1.9.2
|
Template class which provides an unordered container of keys and values, allows duplicate keys. More...
Classes | |
class | const_iterator |
STL style const iterator for QMultiHash More... | |
class | iterator |
STL style non-const iterator for QMultiHash More... | |
Public Typedefs | |
using | const_pointer = const Val * |
using | const_reference = const Val & |
using | difference_type = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::difference_type |
using | hasher = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::hasher |
using | key_equal = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::key_equal |
using | key_type = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::key_type |
using | mapped_type = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::mapped_type |
using | pointer = Val * |
using | reference = Val & |
using | size_type = typename std::unordered_multimap< Key, Val, Hash, KeyEqual >::difference_type |
using | value_type = Val |
Public Methods | |
QMultiHash () = default | |
QMultiHash (const Hash &hash, const KeyEqual &key=KeyEqual ()) | |
QMultiHash (const QMultiHash< Key, Val, Hash, KeyEqual > &other) = default | |
QMultiHash (const std::unordered_multimap< Key, Val, Hash, KeyEqual > &other) | |
template<typename Input_Iterator > | |
QMultiHash (Input_Iterator first, Input_Iterator last, const Hash &hash=Hash (), const KeyEqual &key=KeyEqual ()) | |
QMultiHash (QMultiHash< Key, Val, Hash, KeyEqual > &&other) = default | |
QMultiHash (std::initializer_list< std::pair< const Key, Val >> list, const Hash &hash=Hash (), const KeyEqual &key=KeyEqual ()) | |
QMultiHash (std::unordered_multimap< Key, Val, Hash, KeyEqual > &&other) | |
~QMultiHash () = 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 |
const_iterator | constFind (const Key &key, const Val &value) const |
bool | contains (const Key &key) const |
bool | contains (const Key &key, const Val &value) const |
size_type | count () const |
size_type | count (const Key &key) const |
size_type | count (const Key &key, const Val &value) 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 | find (const Key &key, const Val &value) |
const_iterator | find (const Key &key, const Val &value) const |
iterator | insert (const Key &key, const Val &value) |
iterator | insert (const std::pair< const Key, Val > &data) |
iterator | insertMulti (const Key &key, const Val &value) |
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 QMultiHash< Key, Val, Hash, KeyEqual > &other) const |
QMultiHash | operator+ (const QMultiHash &other) const |
QMultiHash & | operator+= (const QMultiHash &other) |
QMultiHash< Key, Val, Hash, KeyEqual > & | operator= (const QMultiHash< Key, Val, Hash, KeyEqual > &other) = default |
QMultiHash< Key, Val, Hash, KeyEqual > & | operator= (QMultiHash< Key, Val, Hash, KeyEqual > &&other) = default |
bool | operator== (const QMultiHash< Key, Val, Hash, KeyEqual > &other) const |
Val & | operator[] (const Key &key) |
const Val | operator[] (const Key &key) const |
size_type | remove (const Key &key) |
size_type | remove (const Key &key, const Val &value) |
iterator | replace (const Key &key, const Val &value) |
void | reserve (size_type size) |
size_type | size () const |
void | squeeze () |
void | swap (QMultiHash< Key, Val, Hash, KeyEqual > &other) |
Val | take (const Key &key) |
QList< Key > | uniqueKeys () const |
QMultiHash< Key, Val, Hash, KeyEqual > & | unite (const QMultiHash< Key, Val, Hash, KeyEqual > &other) |
Val | value (const Key &key) const |
Val | value (const Key &key, const Val &defaultValue) const |
QList< Val > | values () const |
QList< Val > | values (const Key &key) const |
Related Functions | |
These are not member functions | |
QDataStream & | operator<< (QDataStream &stream, const QMultiHash< Key, Val, Hash, KeyEqual > &hash) |
QDataStream & | operator>> (QDataStream &stream, QMultiHash< Key, Val, Hash, KeyEqual > &hash) |
The QMultiHash class is a template class which provides an unordered container of keys and values, allowing duplicate keys. This container stores the key and value as a pair.
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 examples shows how to create and add elements using a duplicate key.
To retrieve all the values for a given key use values(const Key &key) which will return a QList<T>.
The items which share the same key are guaranteed to be adjacent but unordered within the hash. A more efficient version of the code shown above is to call find() to retrieve an STL style iterator to the first item with a given key, then continue to iterate while the key remains the same.
The QMultiHash<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 QMultiHash 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 QMultiHash. The <QHash>
header file defines qHash() functions which compute an hash values for the following data types.
QMultiHash< Key, Val, Hash, KeyEqual >::const_pointer |
Typedef for const Val *.
QMultiHash< Key, Val, Hash, KeyEqual >::const_reference |
Typedef for const Val &.
QMultiHash< Key, Val, Hash, KeyEqual >::difference_type |
Typedef for integral type used to represent the distance between two elements.
QMultiHash< Key, Val, Hash, KeyEqual >::hasher |
Typedef for the multihash object type.
QMultiHash< Key, Val, Hash, KeyEqual >::key_equal |
Typedef for the key equality comparison object type.
QMultiHash< Key, Val, Hash, KeyEqual >::key_type |
Typedef for Key.
QMultiHash< Key, Val, Hash, KeyEqual >::mapped_type |
Typedef for Val.
QMultiHash< Key, Val, Hash, KeyEqual >::pointer |
Typedef for Val *.
QMultiHash< Key, Val, Hash, KeyEqual >::reference |
Typedef for Val &.
QMultiHash< Key, Val, Hash, KeyEqual >::size_type |
Typedef for a signed integer of the appropriate size for your platform.
QMultiHash< Key, Val, Hash, KeyEqual >::value_type |
Typedef for Val.
|
default |
Constructs an empty QMultiHash.
|
default |
Copy constructs a new QMultiHash from other.
|
default |
Move constructs a new QMultiHash from other.
|
inline |
Constructs a QMultiHash with a copy of each of the elements in the initializer list.
|
inlineexplicit |
Constructs a QMultiHash using the given hash and key.
|
inlineexplicit |
Constructs a new QMultiHash which contains copies of the items in other.
|
inlineexplicit |
Move constructs a new QMultiHash using the items in other.
|
inline |
Constructs a QMultiHash containing copies of the items between the first and last iterators.
|
default |
Destroys the QMultiHash.
|
inline |
Returns a STL-style iterator pointing to the first item in the multihash.
|
inline |
Returns a const STL-style iterator pointing to the first item in the multihash.
|
inline |
|
inline |
Returns a const STL style iterator pointing to the first item in the multihash.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the multihash.
|
inline |
Removes all items in the current QMultiHash.
|
inline |
Returns a const STL style iterator pointing to the first item in the multihash.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the multihash.
|
inline |
Returns a const iterator pointing to the item with the given key. If the multihash contains no matching items, returns constEnd().
|
inline |
Returns a const iterator pointing to the item with the given key and the value. If the multihash contains no matching items, returns constEnd().
|
inline |
Returns true if the multihash contains an item with the given key, otherwise returns false.
|
inline |
Returns true if the multihash contains an item with the given key and value, otherwise returns false.
|
inline |
Returns the number of items in this multihash. Equivalent to calling size().
|
inline |
Returns the number of items which have the given key.
QMultiHash< Key, Val, Hash, KeyEqual >::size_type QMultiHash< Key, Val, Hash, KeyEqual >::count | ( | const Key & | key, |
const Val & | value | ||
) | const |
Returns the number of items which have the given key and value.
|
inline |
Equivalent to calling isEmpty().
|
inline |
Returns an STL-style iterator pointing to the imaginary item after the last item in the multihash.
|
inline |
Returns a const STL-style iterator pointing to the imaginary item after the last item in the multihash.
|
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 const 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 multihash and returns an iterator to the next item in the multihash.
This method assumes the iterator is valid and refers to an element in the multihash. It will not check if iter points past the end or is equal to the end() iterator. This method does not cause QMultiHash 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 multihash.
|
inline |
Returns an iterator pointing to the item with the key in the multihash. If the multihash contains no matching items, returns end().
If the multihash contains multiple items with the same key, this method will return an iterator which points to the most recently inserted value. The other values are accessible by incrementing the iterator. The following code iterates over all the items with the same key.
|
inline |
Returns a const iterator pointing to the item with the key. If the multihash contains no matching item, returns end().
|
inline |
Returns an iterator pointing to the item with the given key and value. If the multihash contains no matching items, returns end(). If the multihash contains multiple items with the same key and value, the iterator returned points to some matching item.
|
inline |
Returns a const iterator pointing to the item with the given key and value. If the multihash contains no matching items, returns end(). If the multihash contains multiple items with the same key and value, the iterator returned points to some matching item.
|
inline |
|
inline |
Inserts a new item with key and value from data, which is an std::pair. If there is already an item with the same key this method will create a new entry. This behavior is very different from replace() which overwrites the value of an existing item.
|
inline |
|
inline |
Returns true if the multihash contains no items, otherwise returns false.
const Key QMultiHash< 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 QMultiHash< 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 > QMultiHash< Key, Val, Hash, KeyEqual >::keys | ( | ) | const |
Returns a list containing all the keys in the multihash in an arbitrary order. Keys which occur multiple times, will also occur multiple times in the list. To obtain a list of unique keys, where each key from the multihash only occurs once, use uniqueKeys().
The order is guaranteed to be the same as when calling values().
QList< Key > QMultiHash< Key, Val, Hash, KeyEqual >::keys | ( | const Val & | value | ) | const |
Returns a list containing all the keys matching the given value in an arbitrary order.
This method can be slow (linear time), because the internal 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==()
.
|
inline |
Returns a QMultiHash which contains all of the items in this multihash plus all the items in other. If a key is common to both, the new multihash will contain the key multiple times.
|
inline |
Inserts all the items in the given other multihash into this multihash, returns a reference to this QMultiHash.
|
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 multihash contains multiple items with the same key, this method will return a reference to the most recently inserted value. 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 |
Removes all the items matching the given key. Returns the number of items removed.
QMultiHash< Key, Val, Hash, KeyEqual >::size_type QMultiHash< Key, Val, Hash, KeyEqual >::remove | ( | const Key & | key, |
const Val & | value | ||
) |
Removes all the items which match the given key and value. Returns the number of items removed.
|
inline |
Inserts a new item with the given key and value.
If there is already an item with the same key, then the value is replaced with the new given value. If there are multiple items with the same key, only the most recently inserted item's value is replaced with the given value.
|
inline |
Ensures the internal hash table consists of at least size buckets. This method is useful for code which 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 have to be prime, because QMultiHash will use a prime number internally. If size is an underestimate, the worst that will happen is that the QMultiHash will be a bit slower. You will rarely need to call this method. The internal hash table automatically shrinks or grows to provide good performance without wasting too much memory.
|
inline |
|
inline |
Reduces the size of the internal multihash table to save memory. The sole purpose of this method is to provide a means of fine tuning the memory usage. You will rarely need to call this method.
|
inline |
Swaps multihash other with this multihash. 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 there are multiple items for the given key, the most recently inserted one is removed. 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.
QList< Key > QMultiHash< Key, Val, Hash, KeyEqual >::uniqueKeys | ( | ) | const |
|
inline |
Inserts all the items in the other multihash into this multihash. If a key is common to both, this multihash will contain the key multiple times.
|
inline |
Returns the value associated with the given key. If there are multiple items for the given key, the value of the most recently inserted item is returned. 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. If there are multiple items for the given key, the value of the most recently inserted item is returned.
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 > QMultiHash< Key, Val, Hash, KeyEqual >::values | ( | ) | const |
QList< Val > QMultiHash< Key, Val, Hash, KeyEqual >::values | ( | const Key & | key | ) | const |
Returns a list of all the values associated with the key from the most recently inserted to the least recently inserted.
|
related |
Writes the given hash to the stream. Returns a reference to the stream. This function 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. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.