Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::Bimap< Left, Right > Class Template Reference

The Bimap template class offers a rudimentary bi-directional associative map. More...

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

Classes

class  const_iterator
 The Bimap::const_iterator class provides an STL-style const iterator for Bimap. More...
 

Public Types

using ConstIterator = const_iterator
 
using difference_type = typename QHash<Left, const Right*>::difference_type
 
using iterator = const_iterator
 
using left_type = Left
 
using right_type = Right
 
using size_type = typename QHash<Left, const Right*>::size_type
 

Public Member Functions

 Bimap ()
 
 Bimap (std::initializer_list< std::pair< Left, Right > > list)
 
iterator begin ()
 
const_iterator begin () const
 
qsizetype capacity () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_iterator constFind (const Left &l) const
 
const_iterator constFind (const Right &r) const
 
const_iterator constFindLeft (const Left &l) const
 
const_iterator constFindRight (const Right &r) const
 
bool containsLeft (const Left &l) const
 
bool containsRight (const Right &r) const
 
qsizetype count () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
const_iterator erase (const_iterator pos)
 
iterator find (const Left &l)
 
const_iterator find (const Left &l) const
 
iterator find (const Right &r)
 
const_iterator find (const Right &r) const
 
iterator findLeft (const Left &l)
 
const_iterator findLeft (const Left &l) const
 
iterator findRight (const Right &r)
 
const_iterator findRight (const Right &r) const
 
Right from (const Left &l) const
 
Right from (const Left &l, const Right &defaultValue) const
 
Left from (const Right &r) const
 
Left from (const Right &r, const Left &defaultValue) const
 
Right fromLeft (const Left &l) const
 
Right fromLeft (const Left &l, const Right &defaultValue) const
 
Left fromRight (const Right &l) const
 
Left fromRight (const Right &l, const Left &defaultValue) const
 
void insert (const Bimap &other)
 
const_iterator insert (const Left &l, const Right &r)
 
bool isEmpty () const
 
QList< Left > lefts () const
 
float load_factor () const
 
bool operator!= (const Bimap &other) const =default
 
bool operator== (const Bimap &other) const
 
Right operator[] (const Left &l) const
 
Left operator[] (const Right &r) const
 
QList< std::pair< Left, Right > > relationships () const
 
bool remove (const Left &l)
 
bool remove (const Right &r)
 
template<typename Predicate >
requires bimap_predicate<Left, Right, Predicate>
qsizetype removeIf (Predicate pred)
 
bool removeLeft (const Left &l)
 
bool removeRight (const Right &r)
 
void reserve (qsizetype size)
 
QList< Right > rights () const
 
qsizetype size () const
 
void squeeze ()
 
void swap (Bimap< Left, Right > &other)
 
Right take (const Left &l)
 
Left take (const Right &r)
 
Left takeLeft (const Right &r)
 
Right takeRight (const Left &l)
 
Left toLeft (const Right &r) const
 
Left toLeft (const Right &r, const Left &defaultValue) const
 
Right toRight (const Left &l) const
 
Right toRight (const Left &l, const Right &defaultValue) const
 

Detailed Description

template<typename Left, typename Right>
class Qx::Bimap< Left, Right >

Qx::Bimap<Left, Right> is like QHash<Key, T>, except that instead of Key and Value there is Left and Right, meaning that no neither type in any specialization of the container is more significant than the other. Lookup of one of the "side's" values using the other is possible via fromLeft(), fromRight(), and other similarly named functions.

iterator is simply an alias for const_iterator as values cannot be modified through bimap iterators due to technical limitations.

Both the Left and Right types must provide operator==() and a global qHash() overload.

Member Typedef Documentation

◆ ConstIterator

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::ConstIterator = const_iterator

Qt-style synonym for Bimap::const_iterator.

◆ difference_type

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::difference_type = typename QHash<Left, const Right*>::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

◆ iterator

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::iterator = const_iterator

Typedef for const_iterator.

◆ left_type

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::left_type = Left

Typedef for Left.

◆ right_type

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::right_type = Right

Typedef for Right.

◆ size_type

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::size_type = typename QHash<Left, const Right*>::size_type

Typedef for int. Provided for STL compatibility.

Constructor & Destructor Documentation

◆ Bimap() [1/2]

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::Bimap ( )
inline

Creates an empty bimap.

See also
clear().

◆ Bimap() [2/2]

template<typename Left , typename Right >
Qx::Bimap< Left, Right >::Bimap ( std::initializer_list< std::pair< Left, Right > > list)
inline

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

Member Function Documentation

◆ begin() [1/2]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::begin ( )
inline

Same as constBegin().

◆ begin() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::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.

◆ capacity()

template<typename Left , typename Right >
qsizetype Qx::Bimap< Left, Right >::capacity ( ) const
inline

Returns the number of buckets in the bimap's internal table.

The sole purpose of this function is to provide a means of fine tuning Bimap's memory usage. In general, you will rarely ever need to call this function. If you want to know how many items are in the bimap, call size().

See also
reserve() and squeeze().

◆ cbegin()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::cbegin ( ) const
inline

Same as constBegin().

◆ cend()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::cend ( ) const
inline

Same as constEnd().

◆ clear()

template<typename Left , typename Right >
void Qx::Bimap< Left, Right >::clear ( )
inline

Removes all relations from the bimap and frees up all memory used by it.

See also
remove().

◆ constBegin()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constBegin ( ) const
inline

Returns an STL-style iterator pointing to the first relationship in the bimap.

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.
See also
constEnd().

◆ constEnd()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constEnd ( ) const
inline

Returns an STL-style iterator pointing to the last relationship in the bimap.

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.
See also
constBegin().

◆ constFind() [1/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constFind ( const Left & l) const
inline

Same as constFindLeft().

◆ constFind() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constFind ( const Right & l) const
inline

Same as constFindRight().

◆ constFindLeft()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constFindLeft ( const Left & l) const
inline

Returns an iterator pointing to the relationship with the Left value l in the bimap.

If the bimaps contains no relationship with the Left value, the function returns constEnd().

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.

◆ constFindRight()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::constFindRight ( const Right & r) const
inline

Returns an iterator pointing to the relationship with the Right value r in the bimap.

If the bimaps contains no relationship with the Right value, the function returns constEnd().

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.

◆ containsLeft()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::containsLeft ( const Left & l) const
inline

Returns true if the bimap contains a relationship with the Left value l; otherwise, returns false.

See also
containsRight() and count().

◆ containsRight()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::containsRight ( const Right & r) const
inline

Returns true if the bimap contains a relationship with the Right value r; otherwise, returns false.

See also
containsLeft() and count().

◆ count()

template<typename Left , typename Right >
qsizetype Qx::Bimap< Left, Right >::count ( ) const
inline

Same as size().

◆ empty()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::empty ( ) const
inline

Same as isEmpty().

◆ end() [1/2]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::end ( )
inline

Same as constEnd().

◆ end() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::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.

◆ erase()

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::erase ( const_iterator pos)
inline

Removes the (Left, Right) pair associated with the iterator pos from the bimap, and returns an iterator to the next relationship in the bimap.

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.
See also
remove(), take() and find().

◆ find() [1/4]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::find ( const Left & l)
inline

Same as constFindLeft().

◆ find() [2/4]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::find ( const Left & l) const
inline

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

◆ find() [3/4]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::find ( const Right & r)
inline

Same as constFindRight().

◆ find() [4/4]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::find ( const Right & r) const
inline

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

◆ findLeft() [1/2]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::findLeft ( const Left & r)
inline

Same as constFindLeft().

◆ findLeft() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::findLeft ( const Left & r) const
inline

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

◆ findRight() [1/2]

template<typename Left , typename Right >
iterator Qx::Bimap< Left, Right >::findRight ( const Right & r)
inline

Same as constFindRight().

◆ findRight() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::findRight ( const Right & r) const
inline

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

◆ from() [1/4]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::from ( const Left & l) const
inline

Same as fromLeft().

◆ from() [2/4]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::from ( const Left & l,
const Right & defaultValue ) const
inline

Same as fromLeft(const Left&, const Right&).

◆ from() [3/4]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::from ( const Right & r) const
inline

Same as fromRight().

◆ from() [4/4]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::from ( const Right & r,
const Left & defaultValue ) const
inline

Same as fromLeft(const Right&, const Left&).

◆ fromLeft() [1/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::fromLeft ( const Left & l) const
inline

Returns the Right value associated with Left value l.

If the bimap does not contain a relationship with l, a default constructed Right value is returned.

See also
fromRight().

◆ fromLeft() [2/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::fromLeft ( const Left & l,
const Right & defaultValue ) const
inline

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

Returns the Right value associated with Left value l.

If the bimap does not contain a relationship with l, defaultValue is returned.

◆ fromRight() [1/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::fromRight ( const Right & r) const
inline

Returns the Left value associated with Right value r.

If the bimap does not contain a relationship with r, a default constructed Left value is returned.

See also
fromLeft().

◆ fromRight() [2/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::fromRight ( const Right & r,
const Left & defaultValue ) const
inline

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

Returns the Left value associated with Right value r.

If the bimap does not contain a relationship with r, defaultValue is returned.

◆ insert() [1/2]

template<typename Left , typename Right >
void Qx::Bimap< Left, Right >::insert ( const Bimap< Left, Right > & other)
inline

Inserts all the relationships in the other bimap into this bimap.

If a Left or Right value from any relationship is common to both bimaps, the relationship containing then will be replaced with the relation that contains said value(s) stored in other.

◆ insert() [2/2]

template<typename Left , typename Right >
const_iterator Qx::Bimap< Left, Right >::insert ( const Left & l,
const Right & r )
inline

Inserts a new relationship between the Left value l and Right value r.

If there is already a relationship for either value, that relationship is removed, effectively replacing it with the new relationship.

Returns an iterator pointing to the new relationship.

Warning
Returned iterators/references should be considered invalidated the next time you call a non-const function on the bimap, or when the bimap is destroyed.

◆ isEmpty()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::isEmpty ( ) const
inline

Returns true if the bimap contains no relations; otherwise, returns false.

See also
size().

◆ lefts()

template<typename Left , typename Right >
QList< Left > Qx::Bimap< Left, Right >::lefts ( ) const
inline

Returns a list containing all of the Left values in the bimap, in an arbitrary 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
rights() and fromRight().

◆ load_factor()

template<typename Left , typename Right >
float Qx::Bimap< Left, Right >::load_factor ( ) const
inline

Returns the current load factor of the Bimap's internal table. This is the same as capacity()/size(). The implementation used will aim to keep the load factor between 0.25 and 0.5. This avoids having too many table collisions that would degrade performance.

Even with a low load factor, the implementation of the bimap table has a very low memory overhead.

This method purely exists for diagnostic purposes and you should rarely need to call it yourself.

See also
reserve() and squeeze().

◆ operator!=()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::operator!= ( const Bimap< Left, Right > & other) const
default

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

Two bimap's are considered equal if they contain the same (right, left) relationships.

This function requires the Right and Left types to implement operator==().

See also
operator==().

◆ operator==()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::operator== ( const Bimap< Left, Right > & other) const
inline

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

Two bimap's are considered equal if they contain the same (right, left) relationships.

This function requires the Right and Left types to implement operator==().

See also
operator!=().

◆ operator[]() [1/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::operator[] ( const Left & l) const
inline

Returns the Right value associated with the Left value l.

Throws std::invalid_argument() if the bimap does not contain a relationship with the Left value l.

See also
fromLeft() and fromRight().

◆ operator[]() [2/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::operator[] ( const Right & r) const
inline

Returns the Left value associated with the Right value r.

Throws std::invalid_argument() if the bimap does not contain a relationship with the Right value r.

See also
fromLeft() and fromRight().

◆ relationships()

template<typename Left , typename Right >
QList< std::pair< Left, Right > > Qx::Bimap< Left, Right >::relationships ( ) const
inline

Returns a list containing all of relationships in the bimap, in an arbitrary 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
lefts() and rights().

◆ remove() [1/2]

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::remove ( const Left & l)
inline

Same as removeLeft().

◆ remove() [2/2]

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::remove ( const Right & r)
inline

Same as removeRight().

◆ removeIf()

template<typename Left , typename Right >
template<typename Predicate >
requires bimap_predicate<Left, Right, Predicate>
qsizetype Qx::Bimap< Left, Right >::removeIf ( Predicate pred)
inline

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

The function supports predicates which take either an argument of type Bimap<Left, Right>::const_iterator, or an argument of type std::pair<const Left&, const Right&>.

Returns the number of elements removed, if any.

See also
clear() and take().

◆ removeLeft()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::removeLeft ( const Left & l)
inline

Removes the relationship containing the Left value l from the bimap if present and returns true; otherwise, returns false.

See also
removeRight() and clear().

◆ removeRight()

template<typename Left , typename Right >
bool Qx::Bimap< Left, Right >::removeRight ( const Right & r)
inline

Removes the relationship containing the Right value r from the bimap if present and returns true; otherwise, returns false.

See also
removeLeft() and clear().

◆ reserve()

template<typename Left , typename Right >
void Qx::Bimap< Left, Right >::reserve ( qsizetype size)
inline

Ensures that the bimap's internal table has space to store at least size items without having to grow the table.

This function is useful for code that needs to build a huge bimap and wants to avoid repeated reallocation.

In general, you will rarely ever need to call this function. Bimap's internal table automatically grows to provide good performance without wasting too much memory.

See also
squeeze() and capacity().

◆ rights()

template<typename Left , typename Right >
QList< Right > Qx::Bimap< Left, Right >::rights ( ) const
inline

Returns a list containing all of the Right values in the bimap, in an arbitrary 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
lefts() and fromLeft().

◆ size()

template<typename Left , typename Right >
qsizetype Qx::Bimap< Left, Right >::size ( ) const
inline

Returns the number of relations in the bimap.

See also
isEmpty() and count().

◆ squeeze()

template<typename Left , typename Right >
void Qx::Bimap< Left, Right >::squeeze ( )
inline

Reduces the size of the Bimap's internal table to save memory.

The sole purpose of this function is to provide a means of fine tuning Bimap's memory usage. In general, you will rarely ever need to call this function.

See also
reserve() and capacity().

◆ swap()

template<typename Left , typename Right >
void Qx::Bimap< Left, Right >::swap ( Bimap< Left, Right > & other)
inline

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

◆ take() [1/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::take ( const Left & l)
inline

Same as takeRight().

◆ take() [2/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::take ( const Right & r)
inline

Same as takeLeft().

◆ takeLeft()

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::takeLeft ( const Right & r)
inline

Removes the relationship with the Right value r from the bimap and returns the Left value associated with it.

If such a relationship does not exist in the bimap, the function simply returns a default-constructed value

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

See also
remove().

◆ takeRight()

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::takeRight ( const Left & l)
inline

Removes the relationship with the Left value l from the bimap and returns the Right value associated with it.

If such a relationship does not exist in the bimap, the function simply returns a default-constructed value

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

See also
remove().

◆ toLeft() [1/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::toLeft ( const Right & r) const
inline

Same as fromRight().

See also
toRight().

◆ toLeft() [2/2]

template<typename Left , typename Right >
Left Qx::Bimap< Left, Right >::toLeft ( const Right & r,
const Left & defaultValue ) const
inline

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

◆ toRight() [1/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::toRight ( const Left & l) const
inline

Same as fromLeft().

See also
toLeft().

◆ toRight() [2/2]

template<typename Left , typename Right >
Right Qx::Bimap< Left, Right >::toRight ( const Left & l,
const Right & defaultValue ) const
inline

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


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