Template Class bimap

Class Documentation

template<typename KEY, typename VALUE>
class bimap

A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std::map’s, one for keys and another for values. To use this class, insert new pairs KEY<->VALUE with bimap::insert. Then, you can access the KEY->VALUE map with bimap::direct(), and the VALUE->KEY map with bimap::inverse(). The consistency of the two internal maps is assured at any time.

Note that unique values are required for both KEYS and VALUES, hence this class is designed to work with bijective mappings only. An exception will be thrown if this contract is broken.

Note

Defined in #include <mrpt/containers/bimap.h>

Note

This class can be accessed through iterators to the map KEY->VALUE only.

Note

Both typenames KEY and VALUE must be suitable for being employed as keys in a std::map, i.e. they must be comparable through a “< operator”.

Note

To serialize this class with the mrpt::serialization API, include the header #include <mrpt/serialization/bimap_serialization.h> (New in MRPT 2.3.3)

Public Types

using const_iterator = typename std::map<KEY, VALUE>::const_iterator
using iterator = typename std::map<KEY, VALUE>::iterator
using const_iterator_inverse = typename std::map<VALUE, KEY>::const_iterator
using iterator_inverse = typename std::map<VALUE, KEY>::iterator

Public Functions

bimap() = default

Default constructor - does nothing

inline const_iterator begin() const
inline iterator begin()
inline const_iterator end() const
inline iterator end()
inline const_iterator_inverse inverse_begin() const
inline iterator_inverse inverse_begin()
inline const_iterator_inverse inverse_end() const
inline iterator_inverse inverse_end()
inline size_t size() const
inline bool empty() const
inline const std::map<KEY, VALUE> &getDirectMap() const

Return a read-only reference to the internal map KEY->VALUES

inline const std::map<VALUE, KEY> &getInverseMap() const

Return a read-only reference to the internal map KEY->VALUES

inline void clear()

Clear the contents of the bi-map.

inline void insert(const KEY &k, const VALUE &v)

Insert a new pair KEY<->VALUE in the bi-map It is legal to insert the same pair (key,value) more than once, but if a duplicated key is attempted to be inserted with a different value (or viceversa) an exception will be thrown. Remember: this class represents a bijective mapping.

inline bool direct(const KEY &k, VALUE &out_v) const

Get the value associated the given key, KEY->VALUE, returning false if not present.

See also

inverse, hasKey, hasValue

Returns:

false on key not found.

inline bool hasKey(const KEY &k) const

Return true if the given key ‘k’ is in the bi-map

See also

hasValue, direct, inverse

inline bool hasValue(const VALUE &v) const

Return true if the given value ‘v’ is in the bi-map

See also

hasKey, direct, inverse

inline VALUE direct(const KEY &k) const

Get the value associated the given key, KEY->VALUE, raising an exception if not present (equivalent to directMap.at()).

See also

inverse, hasKey, hasValue

Throws:

std::exception – On key not present in the bi-map.

inline bool inverse(const VALUE &v, KEY &out_k) const

Get the key associated the given value, VALUE->KEY, returning false if not present (equivalent to inverseMap.at()).

See also

direct, hasKey, hasValue

Returns:

false on value not found.

inline KEY inverse(const VALUE &v) const

Get the key associated the given value, VALUE->KEY, raising an exception if not present.

See also

direct, hasKey, hasValue

Returns:

false on value not found.

inline const_iterator find_key(const KEY &k) const
inline iterator find_key(const KEY &k)
inline const_iterator_inverse find_value(const VALUE &v) const
inline iterator_inverse find_value(const VALUE &v)
inline void erase_by_key(const KEY &k)

Removes the bijective application between KEY<->VALUE for a given key.

Note

(New in MRPT 2.3.3);

Throws:

std::exception – If the key does not exist.

inline void erase_by_value(const VALUE &v)

Removes the bijective application between KEY<->VALUE for a given value.

Note

(New in MRPT 2.3.3);

Throws:

std::exception – If the value does not exist.