template class mrpt::containers::bimap

Overview

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.

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

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

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”.

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

#include <mrpt/containers/bimap.h>

template <typename KEY, typename VALUE>
class bimap
{
public:
    // typedefs

    typedef typename std::map<KEY, VALUE>::const_iterator const_iterator;
    typedef typename std::map<KEY, VALUE>::iterator iterator;
    typedef typename std::map<VALUE, KEY>::const_iterator const_iterator_inverse;
    typedef typename std::map<VALUE, KEY>::iterator iterator_inverse;

    // construction

    bimap();

    // methods

    const_iterator begin() const;
    iterator begin();
    const_iterator end() const;
    iterator end();
    const_iterator_inverse inverse_begin() const;
    iterator_inverse inverse_begin();
    const_iterator_inverse inverse_end() const;
    iterator_inverse inverse_end();
    size_t size() const;
    bool empty() const;
    const std::map<KEY, VALUE>& getDirectMap() const;
    const std::map<VALUE, KEY>& getInverseMap() const;
    void clear();
    void insert(const KEY& k, const VALUE& v);
    bool direct(const KEY& k, VALUE& out_v) const;
    bool hasKey(const KEY& k) const;
    bool hasValue(const VALUE& v) const;
    VALUE direct(const KEY& k) const;
    bool inverse(const VALUE& v, KEY& out_k) const;
    KEY inverse(const VALUE& v) const;
    const_iterator find_key(const KEY& k) const;
    iterator find_key(const KEY& k);
    const_iterator_inverse find_value(const VALUE& v) const;
    iterator_inverse find_value(const VALUE& v);
    void erase_by_key(const KEY& k);
    void erase_by_value(const VALUE& v);
};

Construction

bimap()

Default constructor - does nothing.

Methods

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

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

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

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

void clear()

Clear the contents of the bi-map.

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.

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

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

Returns:

false on key not found.

See also:

inverse, hasKey, hasValue

bool hasKey(const KEY& k) const

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

See also:

hasValue, direct, inverse

bool hasValue(const VALUE& v) const

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

See also:

hasKey, direct, inverse

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()).

Parameters:

std::exception

On key not present in the bi-map.

See also:

inverse, hasKey, hasValue

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()).

Returns:

false on value not found.

See also:

direct, hasKey, hasValue

KEY inverse(const VALUE& v) const

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

Returns:

false on value not found.

See also:

direct, hasKey, hasValue

void erase_by_key(const KEY& k)

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

(New in MRPT 2.3.3);

Parameters:

std::exception

If the key does not exist.

void erase_by_value(const VALUE& v)

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

(New in MRPT 2.3.3);

Parameters:

std::exception

If the value does not exist.