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



Page generated by Doxygen 1.8.9.1 for MRPT 1.3.0 SVN: at Sun Sep 13 03:55:12 UTC 2015