Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::Lopmap< Key, T, Compare > Class Template Reference

The Lopmap class is a template class that provides an "lopsided" associative array. More...

#include <qx/core/qx-lopmap.h>

Classes

class  const_iterator
 The Lopmap::const_iterator class provides an STL-style const iterator for Lopmap. More...
 
class  const_reverse_iterator
 The Lopmap::const_reverse_iterator class provides an STL-style const reverse iterator for Lopmap. More...
 

Public Types

using ConstIterator = const_iterator
 
using ConstReverseIterator = const_reverse_iterator
 
using difference_type = typename StorageContainer::difference_type
 
using iterator = const_iterator
 
using Iterator = iterator
 
using key_type = Key
 
using mapped_Type = T
 
using reverse_iterator = const_reverse_iterator
 
using ReverseIterator = reverse_iterator
 
using size_type = typename StorageContainer::size_type
 
using value_compare = Compare
 

Public Member Functions

 Lopmap ()
 
 Lopmap (std::initializer_list< std::pair< Key, T > > list)
 
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
 
const_reverse_iterator constReverseBegin () const
 
const_reverse_iterator constReverseEnd () const
 
bool contains (const Key &key) const
 
qsizetype count () const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
std::pair< iterator, iteratorequal_range (const T &value)
 
std::pair< const_iterator, const_iteratorequal_range (const T &value) const
 
iterator erase (const_iterator first, const_iterator last)
 
iterator erase (const_iterator pos)
 
iterator find (const Key &key)
 
const_iterator find (const Key &key) const
 
const T & first () const
 
const Key & firstKey () const
 
iterator insert (const Key &key, const T &value)
 
void insert (const Lopmap &other)
 
iterator insert (const_iterator pos, const Key &key, const T &value)
 
void insert (Lopmap &&other)
 
bool isEmpty () const
 
Key key (const T &value, const Key &defaultKey=Key()) const
 
QList< Key > keys () const
 
QList< Key > keys (const T &value) const
 
const T & last () const
 
const Key & lastKey () const
 
iterator lowerBound (const T &value)
 
const_iterator lowerBound (const T &value) const
 
bool operator!= (const Lopmap &other) const =default
 
bool operator== (const Lopmap &other) const
 
operator[] (const Key &key) const
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
size_type remove (const Key &key)
 
template<typename Predicate >
requires lopmap_predicate<Key, T, Compare, Predicate>
qsizetype removeIf (Predicate pred)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
qsizetype size () const
 
void swap (Lopmap< Key, T > &other)
 
take (const Key &key)
 
iterator upperBound (const T &value)
 
const_iterator upperBound (const T &value) const
 
value (const Key &key, const T &defaultValue=T()) const
 
QList< T > values () const
 

Detailed Description

template<typename Key, typename T, typename Compare>
requires std::predicate<Compare, T, T>
class Qx::Lopmap< Key, T, Compare >

Qx::Lopmap<Key, T, Compare> is like QMap<Key, T>, except that values in the map are sorted by value instead of key, with an order dictated by Compare.

Unlike QMap, iterator is simply an alias for const_iterator as values cannot be modified through lopmap iterators since that would affect ordering. Additionally, some methods that would traditional take Key as an argument, instead take T.

The value type of Lopmap must provide operator<(), or a custom Compare object must be provided specifying a total order.

Member Typedef Documentation

◆ ConstIterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::ConstIterator = const_iterator

Qt-style synonym for Lopmap::const_iterator.

◆ ConstReverseIterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::ConstReverseIterator = const_reverse_iterator

Qt-style synonym for const_reverse_iterator.

◆ difference_type

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::difference_type = typename StorageContainer::difference_type

Typedef for the container's difference type, usually std::ptrdiff_t.

◆ iterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::iterator = const_iterator

Typedef for const_iterator.

◆ Iterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::Iterator = iterator

Qt-style synonym for Lopmap::iterator.

◆ key_type

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::key_type = Key

Typedef for Key.

◆ mapped_Type

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::mapped_Type = T

Typedef for T.

◆ reverse_iterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::reverse_iterator = const_reverse_iterator

Typedef for const_reverse_iterator.

◆ ReverseIterator

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::ReverseIterator = reverse_iterator

Qt-style synonym for reverse_iterator.

◆ size_type

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::size_type = typename StorageContainer::size_type

Typedef for the container's size_type, usually std::size_t.

◆ value_compare

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::value_compare = Compare

Typedef for Compare.

Constructor & Destructor Documentation

◆ Lopmap() [1/2]

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::Lopmap ( )
inline

Creates an empty lopmap.

See also
clear().

◆ Lopmap() [2/2]

template<typename Key , typename T , typename Compare >
Qx::Lopmap< Key, T, Compare >::Lopmap ( std::initializer_list< std::pair< Key, T > > list)
inline

Creates a lopmap with a copy of each of the elements in the initializer list list.

Member Function Documentation

◆ begin() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::begin ( )
inline

Same as constBegin().

◆ begin() [2/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::begin ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ cbegin()

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::cbegin ( ) const
inline

Same as constBegin().

◆ cend()

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::cend ( ) const
inline

Same as constEnd().

◆ clear()

template<typename Key , typename T , typename Compare >
void Qx::Lopmap< Key, T, Compare >::clear ( )
inline

Removes all items from the lopmap.

See also
remove().

◆ constBegin()

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::constBegin ( ) const
inline

Returns an STL-style iterator pointing to the first item in the lopmap.

See also
constEnd().

◆ constEnd()

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::constEnd ( ) const
inline

Returns an STL-style iterator pointing to the imaginary item after the last item in the lopmap.

See also
constBegin().

◆ constFind()

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::constFind ( const Key & key) const
inline

Returns an iterator pointing to the item with the key key in the lopmap.

If the lopmaps contains no item with the key key, the function returns constEnd().

◆ constReverseBegin()

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::constReverseBegin ( ) const
inline

Returns an STL-style iterator pointing to the last item in the lopmap.

See also
constReverseEnd().

◆ constReverseEnd()

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::constReverseEnd ( ) const
inline

Returns an STL-style iterator pointing to the imaginary item after the first item in the lopmap.

See also
constReverseBegin().

◆ contains()

template<typename Key , typename T , typename Compare >
bool Qx::Lopmap< Key, T, Compare >::contains ( const Key & key) const
inline

Returns true if the lopmap contains an item with the key key; otherwise, returns false.

See also
count().

◆ count()

template<typename Key , typename T , typename Compare >
qsizetype Qx::Lopmap< Key, T, Compare >::count ( ) const
inline

Same as size().

◆ crbegin()

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::crbegin ( ) const
inline

◆ crend()

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::crend ( ) const
inline

◆ empty()

template<typename Key , typename T , typename Compare >
bool Qx::Lopmap< Key, T, Compare >::empty ( ) const
inline

Same as isEmpty().

◆ end() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::end ( )
inline

Same as constEnd().

◆ end() [2/2]

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::end ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ equal_range() [1/2]

template<typename Key , typename T , typename Compare >
std::pair< iterator, iterator > Qx::Lopmap< Key, T, Compare >::equal_range ( const T & value)
inline

Returns a pair of iterators delimiting the range of values [first, second), that are stored with value.

◆ equal_range() [2/2]

template<typename Key , typename T , typename Compare >
std::pair< const_iterator, const_iterator > Qx::Lopmap< Key, T, Compare >::equal_range ( const T & value) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ erase() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::erase ( const_iterator first,
const_iterator last )
inline

Removes the (key, value) pairs pointed to by the iterator range [first, last) from the lopmap, and returns an iterator to the item in the map following the last removed element.

Note
The range [first, last) must be a valid range in *this.
See also
remove(), and take().

◆ erase() [2/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::erase ( const_iterator pos)
inline

Removes the (key, value) pair pointed to by the iterator pos from the lopmap, and returns an iterator to the next item in the map.

Note
The iterator pos must be valid and dereferenceable.
See also
remove(), and take().

◆ find() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::find ( const Key & key)
inline

Same as constFind().

◆ find() [2/2]

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::find ( const Key & key) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ first()

template<typename Key , typename T , typename Compare >
const T & Qx::Lopmap< Key, T, Compare >::first ( ) const
inline

Returns a reference to the first value in the lopmap.

This function assumes that the map is not empty.

See also
last(), firstKey(), and isEmpty().

◆ firstKey()

template<typename Key , typename T , typename Compare >
const Key & Qx::Lopmap< Key, T, Compare >::firstKey ( ) const
inline

Returns a reference to the first key in the lopmap.

This function assumes that the map is not empty.

See also
lastKey(), first(), and isEmpty().

◆ insert() [1/4]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::insert ( const Key & key,
const T & value )
inline

Inserts a new item with the key key and value of value.

If there is already an item with the key key, that item's value is replaced with value.

Returns an iterator pointing to the new/updated element.

◆ insert() [2/4]

template<typename Key , typename T , typename Compare >
void Qx::Lopmap< Key, T, Compare >::insert ( const Lopmap< Key, T, Compare > & other)
inline

Inserts all the items in the other lopmap into this lopmap.

If a key is common to both maps, its value with be replaced with the value stored in other.

◆ insert() [3/4]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::insert ( const_iterator pos,
const Key & key,
const T & value )
inline

Inserts a new item with the key key and value value and with hint pos suggesting where to do the insert.

If constBegin() is used as hint it indicates that the value should come before any value in the map while constEnd() suggests that the value should (strictly) come after any key in the map. Otherwise, the hint should meet the condition (pos - 1).value() < value <= pos.value() (assuming a Comapre of std::less<Value>). If the hint pos is wrong it is ignored and a regular insert is done.

If there is already an item with the key key, that item's value is replaced with value.

If the hint is correct, the insert executes in amortized constant time.

When creating a map from sorted data inserting the last value first with constBegin() is faster than inserting in sorted order with constEnd(), since constEnd() - 1 (which is needed to check if the hint is valid) needs logarithmic time.

Returns an iterator pointing to the new/updated element.

◆ insert() [4/4]

template<typename Key , typename T , typename Compare >
void Qx::Lopmap< Key, T, Compare >::insert ( Lopmap< Key, T, Compare > && other)
inline

Moves all the items from other into this lopmap.

If a key is common to both maps, its value with be replaced with the value stored in other.

◆ isEmpty()

template<typename Key , typename T , typename Compare >
bool Qx::Lopmap< Key, T, Compare >::isEmpty ( ) const
inline

Returns true if the lopmap contains no items; otherwise, returns false.

See also
size().

◆ key()

template<typename Key , typename T , typename Compare >
Key Qx::Lopmap< Key, T, Compare >::key ( const T & value,
const Key & defaultKey = Key() ) const
inline

Returns the key with value value, or defaultKey if the lopmap contains no item with value. If no defaultKey is provided the functionr returns a default-constructed key.

This function can be slow, because Lopmap's internal data structure is optimized for fast lookup by key, not by value.

See also
value(), and keys().

◆ keys() [1/2]

template<typename Key , typename T , typename Compare >
QList< Key > Qx::Lopmap< Key, T, Compare >::keys ( ) const
inline

Returns a list containing all of the keys in the lopmap in order of their associated values.

The order is guaranteed to be the same as that used by values().

This function creates a new list, in linear time.

See also
values() and key().

◆ keys() [2/2]

template<typename Key , typename T , typename Compare >
QList< Key > Qx::Lopmap< Key, T, Compare >::keys ( const T & values) const
inline

Returns a list containing all of the keys associated with the value value in order of their associated values.

This function creates a new list, in linear time.

◆ last()

template<typename Key , typename T , typename Compare >
const T & Qx::Lopmap< Key, T, Compare >::last ( ) const
inline

Returns a reference to the last value in the lopmap.

This function assumes that the map is not empty.

See also
last(), firstKey(), and isEmpty().

◆ lastKey()

template<typename Key , typename T , typename Compare >
const Key & Qx::Lopmap< Key, T, Compare >::lastKey ( ) const
inline

Returns a reference to the last key in the lopmap.

This function assumes that the map is not empty.

See also
lastKey(), first(), and isEmpty().

◆ lowerBound() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::lowerBound ( const T & value)
inline

Returns an iterator pointing to the first item with value value in the lopmap. If the map contains no item with value value, the function returns an iterator to the nearest item with a greater value, or constEnd() if there is none.

See also
upperBound() and find().

◆ lowerBound() [2/2]

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::lowerBound ( const T & value) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ operator!=()

template<typename Key , typename T , typename Compare >
bool Qx::Lopmap< Key, T, Compare >::operator!= ( const Lopmap< Key, T, Compare > & other) const
default

Returns true if other is not equal to this lopmap; otherwise, returns false.

Two lopmap's are considered equal if they contain the same (key, value) pairs.

This function requires the key and the value types to implement operator==().

See also
operator==().

◆ operator==()

template<typename Key , typename T , typename Compare >
bool Qx::Lopmap< Key, T, Compare >::operator== ( const Lopmap< Key, T, Compare > & other) const
inline

Returns true if other is equal to this lopmap; otherwise, returns false.

Two lopmap's are considered equal if they contain the same (key, value) pairs.

This function requires the key and the value types to implement operator==().

See also
operator!=().

◆ operator[]()

template<typename Key , typename T , typename Compare >
T Qx::Lopmap< Key, T, Compare >::operator[] ( const Key & key) const
inline

Same as value().

◆ rbegin() [1/2]

template<typename Key , typename T , typename Compare >
reverse_iterator Qx::Lopmap< Key, T, Compare >::rbegin ( )
inline

◆ rbegin() [2/2]

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::rbegin ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ remove()

template<typename Key , typename T , typename Compare >
size_type Qx::Lopmap< Key, T, Compare >::remove ( const Key & key)
inline

Remove the item with key key from the map if it exists and returns 1; otherwise, returns 0.

See also
clear() and take().

◆ removeIf()

template<typename Key , typename T , typename Compare >
template<typename Predicate >
requires lopmap_predicate<Key, T, Compare, Predicate>
qsizetype Qx::Lopmap< Key, T, Compare >::removeIf ( Predicate pred)
inline

Removes all elements for which the predicate pred returns true from the lopmap.

The function supports predicates which take either an argument of type Lopmap<Key, T, Compare>::const_iterator, or an argument of type std::pair<const Key&, const T&>.

Returns the number of elements removed, if any.

See also
clear() and take().

◆ rend() [1/2]

template<typename Key , typename T , typename Compare >
reverse_iterator Qx::Lopmap< Key, T, Compare >::rend ( )
inline

◆ rend() [2/2]

template<typename Key , typename T , typename Compare >
const_reverse_iterator Qx::Lopmap< Key, T, Compare >::rend ( ) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ size()

template<typename Key , typename T , typename Compare >
qsizetype Qx::Lopmap< Key, T, Compare >::size ( ) const
inline

Returns the number of items in the lopmap.

See also
isEmpty() and count().

◆ swap()

template<typename Key , typename T , typename Compare >
void Qx::Lopmap< Key, T, Compare >::swap ( Lopmap< Key, T > & other)
inline

Swaps lopmap other with this lopmap. This operation is very fast and never fails.

◆ take()

template<typename Key , typename T , typename Compare >
T Qx::Lopmap< Key, T, Compare >::take ( const Key & key)
inline

Removes the item with the key key from the lopmap and returns the value associated with it.

If this item does not exist in the lopmap, the function simply returns a default-constructed value

If you don't use the return value, remove() is more efficient.

See also
remove().

◆ upperBound() [1/2]

template<typename Key , typename T , typename Compare >
iterator Qx::Lopmap< Key, T, Compare >::upperBound ( const T & value)
inline

Returns an iterator pointing to the item that immediately follows the last item with value value in the lopmap. If the map contains no item with value value, the function returns an iterator to the nearest item with a greater value, or constEnd() if there is none.

See also
lowerBound() and find().

◆ upperBound() [2/2]

template<typename Key , typename T , typename Compare >
const_iterator Qx::Lopmap< Key, T, Compare >::upperBound ( const T & value) const
inline

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ value()

template<typename Key , typename T , typename Compare >
T Qx::Lopmap< Key, T, Compare >::value ( const Key & key,
const T & defaultValue = T() ) const
inline

Returns the value associated with the key key.

If the lopmap contains no item with key key, the function returns defaultValue. If no defaultValue is specified, the function returns a default-constructed value.

See also
key(), values(), contains(), and operator[]().

◆ values()

template<typename Key , typename T , typename Compare >
QList< T > Qx::Lopmap< Key, T, Compare >::values ( ) const
inline

Returns a list containing all of the values in the lopmap in order.

This function creates a new list, in linear time. The time and memory use that entails can be avoided by iterating from begin() to end().

See also
keys() and value().

The documentation for this class was generated from the following files: