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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020