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
const_iterator end() const
Definition: bimap.h:49
Scalar * iterator
Definition: eigen_plugins.h:26
iterator_inverse inverse_begin()
Definition: bimap.h:55
const_iterator_inverse find_value(const VALUE &v) const
Definition: bimap.h:143
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
bool empty() const
Definition: bimap.h:59
std::map< VALUE, KEY > m_v2k
Definition: bimap.h:36
const_iterator_inverse inverse_end() const
Definition: bimap.h:56
const_iterator find_key(const KEY &k) const
Definition: bimap.h:141
bool hasKey(const KEY &k) const
Return true if the given key &#39;k&#39; is in the bi-map.
Definition: bimap.h:93
void insert(const KEY &k, const VALUE &v)
Insert a new pair KEY<->VALUE in the bi-map.
Definition: bimap.h:72
iterator begin()
Definition: bimap.h:48
const_iterator_inverse inverse_begin() const
Definition: bimap.h:51
void clear()
Clear the contents of the bi-map.
Definition: bimap.h:65
size_t size() const
Definition: bimap.h:58
iterator_inverse find_value(const VALUE &v)
Definition: bimap.h:147
const std::map< VALUE, KEY > & getInverseMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:63
A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std...
Definition: bimap.h:32
iterator_inverse inverse_end()
Definition: bimap.h:57
typename std::map< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::const_iterator const_iterator
Definition: bimap.h:39
iterator end()
Definition: bimap.h:50
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
typename std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID >::const_iterator const_iterator_inverse
Definition: bimap.h:42
const GLdouble * v
Definition: glext.h:3678
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
typename std::map< mrpt::maps::CLandmark::TLandmarkID, unsigned int >::iterator iterator
Definition: bimap.h:40
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
typename std::map< unsigned int, mrpt::maps::CLandmark::TLandmarkID >::iterator iterator_inverse
Definition: bimap.h:43
const std::map< KEY, VALUE > & getDirectMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:61
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
bool hasValue(const VALUE &v) const
Return true if the given value &#39;v&#39; is in the bi-map.
Definition: bimap.h:99
iterator find_key(const KEY &k)
Definition: bimap.h:142
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
const_iterator begin() const
Definition: bimap.h:47
const Scalar * const_iterator
Definition: eigen_plugins.h:27
std::map< KEY, VALUE > m_k2v
Definition: bimap.h:35
bimap()
Default constructor - does nothing.
Definition: bimap.h:46



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019