Main MRPT website > C++ reference for MRPT 1.9.9
bimap.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <map>
12 
13 namespace mrpt
14 {
15 namespace containers
16 {
17 /** A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which
18  * actually contains two std::map's, one for keys and another for values.
19  * To use this class, insert new pairs KEY<->VALUE with bimap::insert. Then,
20  * you can access the KEY->VALUE map with bimap::direct(), and the VALUE->KEY
21  * map with bimap::inverse(). The consistency of the two internal maps is
22  * assured at any time.
23  *
24  * \note This class can be accessed through iterators to the map KEY->VALUE
25  * only.
26  * \note Both typenames KEY and VALUE must be suitable for being employed as
27  * keys in a std::map, i.e. they must be comparable through a "< operator".
28  * \note Defined in #include <mrpt/containers/bimap.h>
29  * \ingroup mrpt_containers_grp
30  */
31 template <typename KEY, typename VALUE>
32 class bimap
33 {
34  private:
35  std::map<KEY, VALUE> m_k2v;
36  std::map<VALUE, KEY> m_v2k;
37 
38  public:
44 
45  /** Default constructor - does nothing */
46  bimap() {}
47  inline const_iterator begin() const { return m_k2v.begin(); }
48  inline iterator begin() { return m_k2v.begin(); }
49  inline const_iterator end() const { return m_k2v.end(); }
50  inline iterator end() { return m_k2v.end(); }
52  {
53  return m_v2k.begin();
54  }
55  inline iterator_inverse inverse_begin() { return m_v2k.begin(); }
56  inline const_iterator_inverse inverse_end() const { return m_v2k.end(); }
57  inline iterator_inverse inverse_end() { return m_v2k.end(); }
58  inline size_t size() const { return m_k2v.size(); }
59  inline bool empty() const { return m_k2v.empty(); }
60  /** Return a read-only reference to the internal map KEY->VALUES */
61  const std::map<KEY, VALUE>& getDirectMap() const { return m_k2v; }
62  /** Return a read-only reference to the internal map KEY->VALUES */
63  const std::map<VALUE, KEY>& getInverseMap() const { return m_v2k; }
64  /** Clear the contents of the bi-map. */
65  void clear()
66  {
67  m_k2v.clear();
68  m_v2k.clear();
69  }
70 
71  /** Insert a new pair KEY<->VALUE in the bi-map */
72  void insert(const KEY& k, const VALUE& v)
73  {
74  m_k2v[k] = v;
75  m_v2k[v] = k;
76  }
77 
78  /** Get the value associated the given key, KEY->VALUE, returning false if
79  * not present.
80  * \sa inverse, hasKey, hasValue
81  * \return false on key not found.
82  */
83  bool direct(const KEY& k, VALUE& out_v) const
84  {
85  const_iterator i = m_k2v.find(k);
86  if (i == m_k2v.end()) return false;
87  out_v = i->second;
88  return true;
89  }
90 
91  /** Return true if the given key 'k' is in the bi-map \sa hasValue, direct,
92  * inverse */
93  inline bool hasKey(const KEY& k) const
94  {
95  return m_k2v.find(k) != m_k2v.end();
96  }
97  /** Return true if the given value 'v' is in the bi-map \sa hasKey, direct,
98  * inverse */
99  inline bool hasValue(const VALUE& v) const
100  {
101  return m_v2k.find(v) != m_v2k.end();
102  }
103 
104  /** Get the value associated the given key, KEY->VALUE, raising an
105  * exception if not present.
106  * \sa inverse, hasKey, hasValue
107  * \exception std::exception On key not present in the bi-map.
108  */
109  VALUE direct(const KEY& k) const
110  {
111  const_iterator i = m_k2v.find(k);
112  if (i == m_k2v.end()) THROW_EXCEPTION("Key not found.");
113  return i->second;
114  }
115 
116  /** Get the key associated the given value, VALUE->KEY, returning false if
117  * not present.
118  * \sa direct, hasKey, hasValue
119  * \return false on value not found.
120  */
121  bool inverse(const VALUE& v, KEY& out_k) const
122  {
123  const_iterator_inverse i = m_v2k.find(v);
124  if (i == m_v2k.end()) return false;
125  out_k = i->second;
126  return true;
127  }
128 
129  /** Get the key associated the given value, VALUE->KEY, raising an
130  * exception if not present.
131  * \sa direct, hasKey, hasValue
132  * \return false on value not found.
133  */
134  KEY inverse(const VALUE& v) const
135  {
136  const_iterator_inverse i = m_v2k.find(v);
137  if (i == m_v2k.end()) THROW_EXCEPTION("Value not found.");
138  return i->second;
139  }
140 
141  inline const_iterator find_key(const KEY& k) const { return m_k2v.find(k); }
142  inline iterator find_key(const KEY& k) { return m_k2v.find(k); }
143  inline const_iterator_inverse find_value(const VALUE& v) const
144  {
145  return m_v2k.find(v);
146  }
147  inline iterator_inverse find_value(const VALUE& v) { return m_v2k.find(v); }
148 }; // end class bimap
149 
150 } // End of namespace
151 } // End of namespace
mrpt::containers::bimap::clear
void clear()
Clear the contents of the bi-map.
Definition: bimap.h:65
mrpt::containers::bimap::bimap
bimap()
Default constructor - does nothing.
Definition: bimap.h:46
const_iterator
const Scalar * const_iterator
Definition: eigen_plugins.h:27
mrpt::containers::bimap::end
const_iterator end() const
Definition: bimap.h:49
mrpt::containers::bimap::inverse_end
const_iterator_inverse inverse_end() const
Definition: bimap.h:56
mrpt::containers::bimap::begin
iterator begin()
Definition: bimap.h:48
mrpt::containers::bimap::hasKey
bool hasKey(const KEY &k) const
Return true if the given key 'k' is in the bi-map.
Definition: bimap.h:93
mrpt::containers::bimap::insert
void insert(const KEY &k, const VALUE &v)
Insert a new pair KEY<->VALUE in the bi-map.
Definition: bimap.h:72
mrpt::containers::bimap::find_value
const_iterator_inverse find_value(const VALUE &v) const
Definition: bimap.h:143
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: CKalmanFilterCapable.h:30
mrpt::containers::bimap::inverse_begin
const_iterator_inverse inverse_begin() const
Definition: bimap.h:51
mrpt::containers::bimap::empty
bool empty() const
Definition: bimap.h:59
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
mrpt::containers::bimap::end
iterator end()
Definition: bimap.h:50
v
const GLdouble * v
Definition: glext.h:3678
mrpt::containers::bimap::direct
VALUE direct(const KEY &k) const
Get the value associated the given key, KEY->VALUE, raising an exception if not present.
Definition: bimap.h:109
mrpt::containers::bimap::find_value
iterator_inverse find_value(const VALUE &v)
Definition: bimap.h:147
mrpt::containers::bimap::hasValue
bool hasValue(const VALUE &v) const
Return true if the given value 'v' is in the bi-map.
Definition: bimap.h:99
mrpt::containers::bimap::m_v2k
std::map< VALUE, KEY > m_v2k
Definition: bimap.h:36
mrpt::containers::bimap
A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std...
Definition: bimap.h:32
mrpt::containers::bimap::size
size_t size() const
Definition: bimap.h:58
mrpt::containers::bimap::inverse
bool inverse(const VALUE &v, KEY &out_k) const
Get the key associated the given value, VALUE->KEY, returning false if not present.
Definition: bimap.h:121
mrpt::containers::bimap::direct
bool direct(const KEY &k, VALUE &out_v) const
Get the value associated the given key, KEY->VALUE, returning false if not present.
Definition: bimap.h:83
mrpt::containers::bimap::inverse
KEY inverse(const VALUE &v) const
Get the key associated the given value, VALUE->KEY, raising an exception if not present.
Definition: bimap.h:134
mrpt::containers::bimap::find_key
const_iterator find_key(const KEY &k) const
Definition: bimap.h:141
mrpt::containers::bimap::inverse_end
iterator_inverse inverse_end()
Definition: bimap.h:57
mrpt::containers::bimap< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::const_iterator
typename std::map< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::const_iterator const_iterator
Definition: bimap.h:39
mrpt::containers::bimap::find_key
iterator find_key(const KEY &k)
Definition: bimap.h:142
mrpt::containers::bimap::inverse_begin
iterator_inverse inverse_begin()
Definition: bimap.h:55
mrpt::containers::bimap::m_k2v
std::map< KEY, VALUE > m_k2v
Definition: bimap.h:35
mrpt::containers::bimap::begin
const_iterator begin() const
Definition: bimap.h:47
mrpt::containers::bimap< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::const_iterator_inverse
typename std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID >::const_iterator const_iterator_inverse
Definition: bimap.h:42
mrpt::containers::bimap< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::iterator
typename std::map< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::iterator iterator
Definition: bimap.h:40
mrpt::containers::bimap::getInverseMap
const std::map< VALUE, KEY > & getInverseMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:63
iterator
Scalar * iterator
Definition: eigen_plugins.h:26
mrpt::containers::bimap::getDirectMap
const std::map< KEY, VALUE > & getDirectMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:61
mrpt::containers::bimap< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::iterator_inverse
typename std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID >::iterator iterator_inverse
Definition: bimap.h:43



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST