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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019