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



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020