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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020