Main MRPT website > C++ reference
MRPT logo
bimap.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef mrpt_bimap_H
29 #define mrpt_bimap_H
30 
31 // Note: This file is included from "stl_extensions.h"
32 
33 #include <mrpt/utils/utils_defs.h>
34 #include <map>
35 
36 namespace mrpt
37 {
38  namespace utils
39  {
40  /** 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.
41  * 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.
42  *
43  * \note This class can be accessed through iterators to the map KEY->VALUE only.
44  * \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".
45  * \ingroup stlext_grp
46  */
47  template <typename KEY,typename VALUE>
48  class bimap
49  {
50  private:
51  std::map<KEY,VALUE> m_k2v;
52  std::map<VALUE,KEY> m_v2k;
53 
54  public:
57 
60 
61  /** Default constructor - does nothing */
62  bimap() { }
63 
64  inline const_iterator begin() const { return m_k2v.begin(); }
65  inline iterator begin() { return m_k2v.begin(); }
66  inline const_iterator end() const { return m_k2v.end(); }
67  inline iterator end() { return m_k2v.end(); }
68 
69  inline const_iterator_inverse inverse_begin() const { return m_v2k.begin(); }
70  inline iterator_inverse inverse_begin() { return m_v2k.begin(); }
71  inline const_iterator_inverse inverse_end() const { return m_v2k.end(); }
72  inline iterator_inverse inverse_end() { return m_v2k.end(); }
73 
74  inline size_t size() const { return m_k2v.size(); }
75  inline bool empty() const { return m_k2v.empty(); }
76 
77  /** Return a read-only reference to the internal map KEY->VALUES */
78  const std::map<KEY,VALUE> &getDirectMap() const { return m_k2v; }
79  /** Return a read-only reference to the internal map KEY->VALUES */
80  const std::map<VALUE,KEY> &getInverseMap() const { return m_v2k; }
81 
82  void clear() //!< Clear the contents of the bi-map.
83  {
84  m_k2v.clear();
85  m_v2k.clear();
86  }
87 
88  /** Insert a new pair KEY<->VALUE in the bi-map */
89  void insert(const KEY &k,const VALUE &v)
90  {
91  m_k2v[k]=v;
92  m_v2k[v]=k;
93  }
94 
95  /** Get the value associated the given key, KEY->VALUE, returning false if not present.
96  * \sa inverse, hasKey, hasValue
97  * \return false on key not found.
98  */
99  bool direct(const KEY &k, VALUE &out_v) const
100  {
101  const_iterator i=m_k2v.find(k);
102  if (i==m_k2v.end()) return false;
103  out_v = i->second;
104  return true;
105  }
106 
107  /** Return true if the given key 'k' is in the bi-map \sa hasValue, direct, inverse */
108  inline bool hasKey(const KEY& k) const {
109  return m_k2v.find(k)!=m_k2v.end();
110  }
111  /** Return true if the given value 'v' is in the bi-map \sa hasKey, direct, inverse */
112  inline bool hasValue(const VALUE& v) const {
113  return m_v2k.find(v)!=m_v2k.end();
114  }
115 
116  /** Get the value associated the given key, KEY->VALUE, raising an exception if not present.
117  * \sa inverse, hasKey, hasValue
118  * \exception std::exception On key not present in the bi-map.
119  */
120  VALUE direct(const KEY &k) const
121  {
122  const_iterator i=m_k2v.find(k);
123  if (i==m_k2v.end()) THROW_EXCEPTION("Key not found.");
124  return i->second;
125  }
126 
127  /** Get the key associated the given value, VALUE->KEY, returning false if not present.
128  * \sa direct, hasKey, hasValue
129  * \return false on value not found.
130  */
131  bool inverse(const VALUE &v, KEY &out_k) const
132  {
133  const_iterator_inverse i=m_v2k.find(v);
134  if (i==m_v2k.end()) return false;
135  out_k = i->second;
136  return true;
137  }
138 
139  /** Get the key associated the given value, VALUE->KEY, raising an exception if not present.
140  * \sa direct, hasKey, hasValue
141  * \return false on value not found.
142  */
143  KEY inverse(const VALUE &v) const
144  {
145  const_iterator_inverse i=m_v2k.find(v);
146  if (i==m_v2k.end()) THROW_EXCEPTION("Value not found.");
147  return i->second;
148  }
149 
150 
151  inline const_iterator find_key(const KEY& k) const { return m_k2v.find(k); }
152  inline iterator find_key(const KEY& k) { return m_k2v.find(k); }
153 
154  inline const_iterator_inverse find_value(const VALUE& v) const { return m_v2k.find(v); }
155  inline iterator_inverse find_value(const VALUE& v) { return m_v2k.find(v); }
156 
157 
158  }; // end class bimap
159 
160  } // End of namespace
161 } // End of namespace
162 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013