CopperSpice API
1.9.2
|
Template class which uses a vector to implement a sorted map. More...
Classes | |
class | const_iterator |
STL style const iterator for QFlatMap More... | |
class | iterator |
STL style iterator for QFlatMap More... | |
Public Typedefs | |
using | allocator_type = typename std::vector< std::pair< Key, Val > >::allocator_type |
using | const_pointer = const Val * |
using | const_reference = const Val & |
using | const_reverse_iterator = std::reverse_iterator< const_iterator > |
using | difference_type = typename std::vector< std::pair< Key, Val > >::difference_type |
using | key_compare = typename std::map< Key, Val, C >::key_compare |
using | key_type = typename std::map< Key, Val, C >::key_type |
using | mapped_type = typename std::map< Key, Val, C >::mapped_type |
using | pointer = Val * |
using | reference = Val & |
using | reverse_iterator = std::reverse_iterator< iterator > |
using | size_type = typename std::vector< std::pair< Key, Val > >::difference_type |
using | value_type = Val |
Public Methods | |
QFlatMap () = default | |
QFlatMap (C compare) | |
QFlatMap (const QFlatMap< Key, Val, C > &other) = default | |
QFlatMap (const std::map< Key, Val, C > &other) | |
QFlatMap (const_iterator first, const_iterator last, const C &compare=C ()) | |
template<typename Input_Iterator > | |
QFlatMap (Input_Iterator first, Input_Iterator last, const C &compare=C ()) | |
QFlatMap (QFlatMap< Key, Val, C > &&other) = default | |
QFlatMap (std::initializer_list< std::pair< const Key, Val >> list, const C &compare=C ()) | |
~QFlatMap () = default | |
iterator | begin () |
const_iterator | begin () 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 |
const_reverse_iterator | crbegin () const |
const_reverse_iterator | crend () 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 |
Val & | first () |
const Val & | first () const |
const Key & | firstKey () const |
iterator | insert (const Key &key, const Val &value) |
iterator | insert (const_iterator hint, const Key &key, const Val &value) |
bool | isEmpty () const |
const Key | key (const Val &value, const Key &defaultKey=Key ()) const |
QList< Key > | keys () const |
QList< Key > | keys (const Val &value) const |
Val & | last () |
const Val & | last () const |
const Key & | lastKey () const |
iterator | lowerBound (const Key &key) |
const_iterator | lowerBound (const Key &key) const |
bool | operator!= (const QFlatMap< Key, Val, C > &other) const |
QFlatMap< Key, Val, C > & | operator= (const QFlatMap< Key, Val, C > &other) = default |
QFlatMap< Key, Val, C > & | operator= (QFlatMap< Key, Val, C > &&other) = default |
bool | operator== (const QFlatMap< Key, Val, C > &other) const |
Val & | operator[] (const Key &key) |
const Val | operator[] (const Key &key) const |
reverse_iterator | rbegin () |
const_reverse_iterator | rbegin () const |
size_type | remove (const Key &key) |
reverse_iterator | rend () |
const_reverse_iterator | rend () const |
size_type | size () const |
void | swap (QFlatMap< Key, Val, C > &other) |
Val | take (const Key &key) |
QList< Key > | uniqueKeys () const |
QFlatMap< Key, Val, C > & | unite (const QFlatMap< Key, Val, C > &other) |
iterator | upperBound (const Key &key) |
const_iterator | upperBound (const Key &key) const |
const Val | value (const Key &key, const Val &defaultValue=Val ()) const |
QList< Val > | values () const |
Related Functions | |
These are not member functions | |
QDataStream & | operator<< (QDataStream &stream, const QFlatMap< Key, Val, C > &flatmap) |
QDataStream & | operator>> (QDataStream &stream, QFlatMap< Key, Val, C > &flatmap) |
The QFlatMap class is a template class which uses a vector to implement a sorted map. This container stores the key and value as a pair. This class is useful when the number of elements stored in the container will be relatively small and read access is more common than write access.
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 QFlatMap.
To insert a (key, value) pair into this flat map use operator[]() or insert(). This following code uses insert to add three (key, value) pairs into the flat map.
To look up a value by key in the flat map, use operator[]() or value(). If there is no entry with the specified key in the flat map, a default constructed element will be returned.
To check whether the flat map contains a specific key you can use contains().
An overloaded value() method can be used which takes a second argument as the default value which will be returned if there is no item with the specified key.
The following code looks like operator[]() is just doing a look up. There is a side effect of using this operator. If the key is not found then an implicit insert will occur and a (key, value) pair will be added with a default constructed value.
To avoid this problem replace flatmap[i]
with flatmap.value(i)
.
To navigate through all (key, value) pairs stored in a QFlatMap use an iterator. QFlatMap provides both Java style iterators (QFlatMapIterator and QMutableFlatMapIterator) and STL style iterators (QFlatMap::const_iterator and QFlatMap::iterator).
The following shows how to iterate over a QFlatMap<QString, QString> using a Java style iterator.
Here is the same code using an STL style iterator.
The items are traversed in ascending key order. QFlatMap allows only one value per key. If you call insert() with a key which already exists in the QFlatMap, the previous value will be replaced with the new value.
To extract the values from a flat map and not the keys use a range based for.
Items can be removed from the flat map in several ways. One way is to call remove() which will remove any item with the given key. Another way is to use QMutableFlatMapIterator::remove(). In addition, the entire map can be cleared using the clear() method.
The QFlatMap<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 QFlatMap key type must provide operator<(). QFlatMap uses the comparison operator to sort. Two keys x
and y
are equal if "x is not less than y" and "y is not less than x".
The third template parameter is used to define a sort order for QFlatMap, which is optional. If no user defined sort or compare parameter is provided, the default will be used. The following code is the default QFlatMap Compare template.
The following is an example of an user defined compare class which provides case insensitive sorting.
QFlatMap< Key, Val, C >::allocator_type |
Equivalent to allocator used by the container.
QFlatMap< Key, Val, C >::const_pointer |
Equivalent to const Val *.
QFlatMap< Key, Val, C >::const_reference |
Equivalent to const Val &.
QFlatMap< Key, Val, C >::const_reverse_iterator |
Equivalent to an STL style const reverse iterator.
QFlatMap< Key, Val, C >::difference_type |
Equivalent to integral type used to represent the distance between two elements.
QFlatMap< Key, Val, C >::key_compare |
Equivalent to the key lessthan comparison object type.
QFlatMap< Key, Val, C >::key_type |
Equivalent to Key.
QFlatMap< Key, Val, C >::mapped_type |
Equivalent to Val.
QFlatMap< Key, Val, C >::pointer |
Equivalent to Val *.
QFlatMap< Key, Val, C >::reference |
Equivalent to Val &.
QFlatMap< Key, Val, C >::reverse_iterator |
Equivalent to an STL style reverse iterator.
QFlatMap< Key, Val, C >::size_type |
Equivalent to a signed integer of the appropriate size for your platform.
QFlatMap< Key, Val, C >::value_type |
Equivalent to Val.
|
default |
Constructs an empty QFlatMap.
|
default |
Copy constructs a new QFlatMap from other.
|
default |
Move constructs a new QFlatMap from other.
|
inline |
Constructs a flat map with a copy of each of the elements in the specified initializer list. If compare is specified it will be used to sort the flat map.
|
inlineexplicit |
Constructs an empty flat map using the specified comparison object compare.
|
inlineexplicit |
Constructs a copy of other.
|
inline |
Constructs a flat map using the specified iterator range. These iterators can be any iterator which returns a Pair when dereferenced.
|
inline |
Constructs a flat map using the specified iterator range. These iterators are const QFlatMap iterators.
|
default |
Destroys the flat map. References to the values in the flat map and all iterators over this flat map become invalid.
|
inline |
Returns an STL style iterator pointing to the first item in the flat map.
|
inline |
This is an overloaded method.
|
inline |
|
inline |
|
inline |
Removes all items from the flat map.
|
inline |
Returns a const STL style iterator pointing to the first item in the flat map.
|
inline |
Returns a const STL style iterator pointing to the imaginary item after the last item in the flat map.
|
inline |
Returns a const iterator pointing to the item with the specified key. If the flat map contains no item with key, the method returns constEnd().
|
inline |
Returns true if the flat map contains an item with the specified key, otherwise it returns false.
|
inline |
Equivalent to calling size().
|
inline |
Returns the number of items associated with the specified key.
|
inline |
|
inline |
|
inline |
Equivalent to calling isEmpty().
|
inline |
Returns an STL style iterator pointing to the imaginary item after the last item in the flat map.
|
inline |
This is an overloaded method.
|
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 flat map and returns an iterator to the next item in the flat map.
This method assumes the iterator is valid and refers to an element in the flat map. It will not check if iter points past the end of the flat map or is equal to the end() iterator.
|
inline |
Returns an iterator pointing to the item with the specified key. If the flat map contains no item with this key, the method returns end().
|
inline |
This is an overloaded method.
|
inline |
Returns a reference to the first value in the flat map. This method assumes the flat map is not empty.
|
inline |
Returns a const reference to the first value in the flat map. This method assumes the flat map is not empty.
|
inline |
Returns a const reference to the first key in the flat map. This method assumes the flat map is not empty.
|
inline |
Inserts a new item with the given key and value. If there is already an item with the specified key, the item's value is replaced with value.
|
inline |
Inserts a new item with the key and value using hint as a suggestion about where to insert. If there is already an item with the specified key, the item's value is replaced with value.
|
inline |
Returns true if the flat map contains no items, otherwise returns false.
const Key QFlatMap< Key, Val, C >::key | ( | const Val & | value, |
const Key & | defaultKey = Key() |
||
) | const |
Returns the first key with the specified value or the defaultKey if the flat map contains no such item. If no defaultKey was passed it will be value initialized using the data type of Key. 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.
QList< Key > QFlatMap< Key, Val, C >::keys | ( | ) | const |
Returns a list containing all the keys in the flat map in ascending order. To obtain a list of unique keys where each key from the flat map only occurs once, use uniqueKeys(). The order is guaranteed to be the same as returned by values().
QList< Key > QFlatMap< Key, Val, C >::keys | ( | const Val & | value | ) | const |
Returns a list containing all the keys associated with specified value in ascending order.
This method can be slow (linear time) because QFlatMap's internal data structure is optimized for fast lookup by key, not by value.
|
inline |
Returns a reference to the last value in the flat map. This method assumes the flat map is not empty.
|
inline |
Returns a const reference to the last value in the flat map. This method assumes the flat map is not empty.
|
inline |
Returns a const reference to the last key in the flat map. This method assumes the flat map is not empty.
|
inline |
Returns an iterator pointing to the first item specified by key. If the flat map contains no item with key, the method returns an iterator to the nearest item with a greater key.
|
inline |
This is an overloaded method.
|
inline |
Returns true if other is not equal to this flat map, otherwise returns false. Two flat maps 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 flat map, otherwise returns false. Two flat maps are considered equal if they contain the same (key, value) pairs.
This method requires the type T to support operator==()
.
Val & QFlatMap< Key, Val, C >::operator[] | ( | const Key & | key | ) |
Returns a reference to the value for the item 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. Refer to default constructed elements for additional information.
const Val QFlatMap< Key, Val, C >::operator[] | ( | const Key & | key | ) | const |
|
inline |
This is an overloaded method.
|
inline |
|
inline |
|
inline |
|
inline |
This is an overloaded method.
|
inline |
|
inline |
Swaps other with this flat map. 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 are not using the return value calling remove() will be more efficient. If the key was not found then a value initialized object of type Val will be 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 > QFlatMap< Key, Val, C >::uniqueKeys | ( | ) | const |
Returns a list of every key in the flat map with out duplicates.
|
inline |
Inserts all the items in the other flat map into this flat map. If a key is common to both flat maps the resulting flat map will contain the old value.
|
inline |
Returns an iterator pointing to the item which immediately follows the last item with the specified key. If the flat map contains no item with the specified key, the method returns an iterator to the nearest item with a greater key.
|
inline |
This is an overloaded method.
const Val QFlatMap< Key, Val, C >::value | ( | const Key & | key, |
const Val & | defaultValue = Val() |
||
) | const |
Returns the value associated with the specified key. If the flat map contains no element with the specified key, the defaultValue is returned. If no defaultValue was passed it will be value initialized using the data type of Val. 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().
QList< Val > QFlatMap< Key, Val, C >::values | ( | ) | const |
|
related |
Writes the given flatmap 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 flatmap. Returns a reference to the stream. This function requires the type T to support operator>>()
.
Refer to Serializing Data Types for additional information.