Main MRPT website > C++ reference
MRPT logo

bimap.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef  mrpt_bimap_H
00029 #define  mrpt_bimap_H
00030 
00031 // Note: This file is included from "stl_extensions.h"
00032 
00033 #include <mrpt/utils/utils_defs.h>
00034 #include <map>
00035 
00036 namespace mrpt
00037 {
00038         namespace utils
00039         {
00040                 /** 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.
00041                   * 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.
00042                   *
00043                   * \note This class can be accessed through iterators to the map KEY->VALUE only.
00044                   * \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".
00045                   */
00046                 template <typename KEY,typename VALUE>
00047                 class bimap
00048                 {
00049                 private:
00050                         std::map<KEY,VALUE>     m_k2v;
00051                         std::map<VALUE,KEY>     m_v2k;
00052 
00053                 public:
00054                         typedef typename std::map<KEY,VALUE>::const_iterator const_iterator;
00055                         typedef typename std::map<KEY,VALUE>::iterator iterator;
00056 
00057                         typedef typename std::map<VALUE,KEY>::const_iterator const_iterator_inverse;
00058                         typedef typename std::map<VALUE,KEY>::iterator iterator_inverse;
00059 
00060                         /** Default constructor - does nothing */
00061                         bimap() { }
00062 
00063                         inline const_iterator begin() const { return m_k2v.begin(); }
00064                         inline iterator       begin() { return m_k2v.begin(); }
00065                         inline const_iterator end() const { return m_k2v.end(); }
00066                         inline iterator       end() { return m_k2v.end(); }
00067 
00068                         inline const_iterator_inverse inverse_begin() const { return m_v2k.begin(); }
00069                         inline iterator_inverse       inverse_begin() { return m_v2k.begin(); }
00070                         inline const_iterator_inverse inverse_end() const { return m_v2k.end(); }
00071                         inline iterator_inverse       inverse_end() { return m_v2k.end(); }
00072 
00073                         inline size_t size() const { return m_k2v.size(); }
00074                         inline bool empty() const { return m_k2v.empty(); }
00075 
00076                         /** Return a read-only reference to the internal map KEY->VALUES */
00077                         const std::map<KEY,VALUE> &getDirectMap() const { return m_k2v; }
00078                         /** Return a read-only reference to the internal map KEY->VALUES */
00079                         const std::map<VALUE,KEY> &getInverseMap() const { return m_v2k; }
00080 
00081                         void clear() //!< Clear the contents of the bi-map.
00082                         {
00083                                 m_k2v.clear();
00084                                 m_v2k.clear();
00085                         }
00086 
00087                         /** Insert a new pair KEY<->VALUE in the bi-map */
00088                         void insert(const KEY &k,const VALUE &v)
00089                         {
00090                                 m_k2v[k]=v;
00091                                 m_v2k[v]=k;
00092                         }
00093 
00094                         /**  Get the value associated the given key, KEY->VALUE, returning false if not present.
00095                           *  \sa inverse, hasKey, hasValue
00096                           * \return false on key not found.
00097                           */
00098                         bool direct(const KEY &k, VALUE &out_v) const
00099                         {
00100                                 const_iterator i=m_k2v.find(k);
00101                                 if (i==m_k2v.end()) return false;
00102                                 out_v = i->second;
00103                                 return true;
00104                         }
00105 
00106                         /** Return true if the given key 'k' is in the bi-map  \sa hasValue, direct, inverse */
00107                         inline bool hasKey(const KEY& k) const {
00108                                 return m_k2v.find(k)!=m_k2v.end();
00109                         }
00110                         /** Return true if the given value 'v' is in the bi-map \sa hasKey, direct, inverse */
00111                         inline bool hasValue(const VALUE& v) const {
00112                                 return m_v2k.find(v)!=m_v2k.end();
00113                         }
00114 
00115                         /**  Get the value associated the given key, KEY->VALUE, raising an exception if not present.
00116                           *  \sa inverse, hasKey, hasValue
00117                           * \exception std::exception On key not present in the bi-map.
00118                           */
00119                         VALUE direct(const KEY &k) const
00120                         {
00121                                 const_iterator i=m_k2v.find(k);
00122                                 if (i==m_k2v.end()) THROW_EXCEPTION("Key not found.");
00123                                 return i->second;
00124                         }
00125 
00126                         /**  Get the key associated the given value, VALUE->KEY, returning false if not present.
00127                           *  \sa direct, hasKey, hasValue
00128                           * \return false on value not found.
00129                           */
00130                         bool inverse(const VALUE &v, KEY &out_k) const
00131                         {
00132                                 const_iterator_inverse i=m_v2k.find(v);
00133                                 if (i==m_v2k.end()) return false;
00134                                 out_k = i->second;
00135                                 return true;
00136                         }
00137 
00138                         /**  Get the key associated the given value, VALUE->KEY, raising an exception if not present.
00139                           *  \sa direct, hasKey, hasValue
00140                           * \return false on value not found.
00141                           */
00142                         KEY inverse(const VALUE &v) const
00143                         {
00144                                 const_iterator_inverse i=m_v2k.find(v);
00145                                 if (i==m_v2k.end()) THROW_EXCEPTION("Value not found.");
00146                                 return i->second;
00147                         }
00148 
00149 
00150                         inline const_iterator find_key(const KEY& k) const  { return m_k2v.find(k); }
00151                         inline iterator       find_key(const KEY& k)        { return m_k2v.find(k); }
00152 
00153                         inline const_iterator_inverse find_value(const VALUE& v) const  { return m_v2k.find(v); }
00154                         inline iterator_inverse       find_value(const VALUE& v)        { return m_v2k.find(v); }
00155 
00156 
00157                 };  // end class bimap
00158 
00159         } // End of namespace
00160 } // End of namespace
00161 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011