Main MRPT website > C++ reference
MRPT logo
bimap.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (c) 2005-2013, Individual contributors, see AUTHORS file |
7  | Copyright (c) 2005-2013, MAPIR group, University of Malaga |
8  | Copyright (c) 2012-2013, University of Almeria |
9  | All rights reserved. |
10  | |
11  | Redistribution and use in source and binary forms, with or without |
12  | modification, are permitted provided that the following conditions are |
13  | met: |
14  | * Redistributions of source code must retain the above copyright |
15  | notice, this list of conditions and the following disclaimer. |
16  | * Redistributions in binary form must reproduce the above copyright |
17  | notice, this list of conditions and the following disclaimer in the |
18  | documentation and/or other materials provided with the distribution. |
19  | * Neither the name of the copyright holders nor the |
20  | names of its contributors may be used to endorse or promote products |
21  | derived from this software without specific prior written permission.|
22  | |
23  | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24  | 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
25  | TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR|
26  | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE |
27  | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL|
28  | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR|
29  | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30  | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
31  | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
32  | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
33  | POSSIBILITY OF SUCH DAMAGE. |
34  +---------------------------------------------------------------------------+ */
35 #ifndef mrpt_bimap_H
36 #define mrpt_bimap_H
37 
38 // Note: This file is included from "stl_extensions.h"
39 
40 #include <mrpt/utils/utils_defs.h>
41 #include <map>
42 
43 namespace mrpt
44 {
45  namespace utils
46  {
47  /** 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.
48  * 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.
49  *
50  * \note This class can be accessed through iterators to the map KEY->VALUE only.
51  * \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".
52  * \ingroup stlext_grp
53  */
54  template <typename KEY,typename VALUE>
55  class bimap
56  {
57  private:
58  std::map<KEY,VALUE> m_k2v;
59  std::map<VALUE,KEY> m_v2k;
60 
61  public:
64 
67 
68  /** Default constructor - does nothing */
69  bimap() { }
70 
71  inline const_iterator begin() const { return m_k2v.begin(); }
72  inline iterator begin() { return m_k2v.begin(); }
73  inline const_iterator end() const { return m_k2v.end(); }
74  inline iterator end() { return m_k2v.end(); }
75 
76  inline const_iterator_inverse inverse_begin() const { return m_v2k.begin(); }
77  inline iterator_inverse inverse_begin() { return m_v2k.begin(); }
78  inline const_iterator_inverse inverse_end() const { return m_v2k.end(); }
79  inline iterator_inverse inverse_end() { return m_v2k.end(); }
80 
81  inline size_t size() const { return m_k2v.size(); }
82  inline bool empty() const { return m_k2v.empty(); }
83 
84  /** Return a read-only reference to the internal map KEY->VALUES */
85  const std::map<KEY,VALUE> &getDirectMap() const { return m_k2v; }
86  /** Return a read-only reference to the internal map KEY->VALUES */
87  const std::map<VALUE,KEY> &getInverseMap() const { return m_v2k; }
88 
89  void clear() //!< Clear the contents of the bi-map.
90  {
91  m_k2v.clear();
92  m_v2k.clear();
93  }
94 
95  /** Insert a new pair KEY<->VALUE in the bi-map */
96  void insert(const KEY &k,const VALUE &v)
97  {
98  m_k2v[k]=v;
99  m_v2k[v]=k;
100  }
101 
102  /** Get the value associated the given key, KEY->VALUE, returning false if not present.
103  * \sa inverse, hasKey, hasValue
104  * \return false on key not found.
105  */
106  bool direct(const KEY &k, VALUE &out_v) const
107  {
108  const_iterator i=m_k2v.find(k);
109  if (i==m_k2v.end()) return false;
110  out_v = i->second;
111  return true;
112  }
113 
114  /** Return true if the given key 'k' is in the bi-map \sa hasValue, direct, inverse */
115  inline bool hasKey(const KEY& k) const {
116  return m_k2v.find(k)!=m_k2v.end();
117  }
118  /** Return true if the given value 'v' is in the bi-map \sa hasKey, direct, inverse */
119  inline bool hasValue(const VALUE& v) const {
120  return m_v2k.find(v)!=m_v2k.end();
121  }
122 
123  /** Get the value associated the given key, KEY->VALUE, raising an exception if not present.
124  * \sa inverse, hasKey, hasValue
125  * \exception std::exception On key not present in the bi-map.
126  */
127  VALUE direct(const KEY &k) const
128  {
129  const_iterator i=m_k2v.find(k);
130  if (i==m_k2v.end()) THROW_EXCEPTION("Key not found.");
131  return i->second;
132  }
133 
134  /** Get the key associated the given value, VALUE->KEY, returning false if not present.
135  * \sa direct, hasKey, hasValue
136  * \return false on value not found.
137  */
138  bool inverse(const VALUE &v, KEY &out_k) const
139  {
140  const_iterator_inverse i=m_v2k.find(v);
141  if (i==m_v2k.end()) return false;
142  out_k = i->second;
143  return true;
144  }
145 
146  /** Get the key associated the given value, VALUE->KEY, raising an exception if not present.
147  * \sa direct, hasKey, hasValue
148  * \return false on value not found.
149  */
150  KEY inverse(const VALUE &v) const
151  {
152  const_iterator_inverse i=m_v2k.find(v);
153  if (i==m_v2k.end()) THROW_EXCEPTION("Value not found.");
154  return i->second;
155  }
156 
157 
158  inline const_iterator find_key(const KEY& k) const { return m_k2v.find(k); }
159  inline iterator find_key(const KEY& k) { return m_k2v.find(k); }
160 
161  inline const_iterator_inverse find_value(const VALUE& v) const { return m_v2k.find(v); }
162  inline iterator_inverse find_value(const VALUE& v) { return m_v2k.find(v); }
163 
164 
165  }; // end class bimap
166 
167  } // End of namespace
168 } // End of namespace
169 #endif
iterator find_key(const KEY &k)
Definition: bimap.h:159
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:106
bool empty() const
Definition: bimap.h:82
bimap()
Default constructor - does nothing.
Definition: bimap.h:69
const_iterator end() const
Definition: bimap.h:73
std::map< KEY, VALUE >::const_iterator const_iterator
Definition: bimap.h:62
#define THROW_EXCEPTION(msg)
iterator begin()
Definition: bimap.h:72
Scalar * iterator
Definition: eigen_plugins.h:49
std::map< VALUE, KEY >::const_iterator const_iterator_inverse
Definition: bimap.h:65
iterator_inverse inverse_end()
Definition: bimap.h:79
const Scalar * const_iterator
Definition: eigen_plugins.h:50
void clear()
Definition: bimap.h:89
bool hasKey(const KEY &k) const
Return true if the given key &#39;k&#39; is in the bi-map.
Definition: bimap.h:115
A bidirectional version of std::map, declared as bimap<KEY,VALUE> and which actually contains two std...
Definition: bimap.h:55
const_iterator find_key(const KEY &k) const
Definition: bimap.h:158
const_iterator_inverse inverse_end() const
Definition: bimap.h:78
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:138
iterator_inverse find_value(const VALUE &v)
Definition: bimap.h:162
bool hasValue(const VALUE &v) const
Return true if the given value &#39;v&#39; is in the bi-map.
Definition: bimap.h:119
std::map< KEY, VALUE >::iterator iterator
Definition: bimap.h:63
const_iterator_inverse find_value(const VALUE &v) const
Definition: bimap.h:161
std::map< VALUE, KEY >::iterator iterator_inverse
Definition: bimap.h:66
VALUE direct(const KEY &k) const
Get the value associated the given key, KEY->VALUE, raising an exception if not present.
Definition: bimap.h:127
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
const_iterator_inverse inverse_begin() const
Definition: bimap.h:76
const_iterator begin() const
Definition: bimap.h:71
std::map< VALUE, KEY > m_v2k
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:150
std::map< KEY, VALUE > m_k2v
Definition: bimap.h:58
void insert(const KEY &k, const VALUE &v)
Insert a new pair KEY<->VALUE in the bi-map.
Definition: bimap.h:96
iterator_inverse inverse_begin()
Definition: bimap.h:77
const std::map< VALUE, KEY > & getInverseMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:87
iterator end()
Definition: bimap.h:74
const std::map< KEY, VALUE > & getDirectMap() const
Return a read-only reference to the internal map KEY->VALUES.
Definition: bimap.h:85
size_t size() const
Definition: bimap.h:81



Page generated by Doxygen 1.8.14 for MRPT 1.0.2 SVN: at lun oct 28 00:52:41 CET 2019 Hosted on:
SourceForge.net Logo