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://www.mrpt.org/                                |
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                   * \ingroup stlext_grp
00046                   */
00047                 template <typename KEY,typename VALUE>
00048                 class bimap
00049                 {
00050                 private:
00051                         std::map<KEY,VALUE>     m_k2v;
00052                         std::map<VALUE,KEY>     m_v2k;
00053 
00054                 public:
00055                         typedef typename std::map<KEY,VALUE>::const_iterator const_iterator;
00056                         typedef typename std::map<KEY,VALUE>::iterator iterator;
00057 
00058                         typedef typename std::map<VALUE,KEY>::const_iterator const_iterator_inverse;
00059                         typedef typename std::map<VALUE,KEY>::iterator iterator_inverse;
00060 
00061                         /** Default constructor - does nothing */
00062                         bimap() { }
00063 
00064                         inline const_iterator begin() const { return m_k2v.begin(); }
00065                         inline iterator       begin() { return m_k2v.begin(); }
00066                         inline const_iterator end() const { return m_k2v.end(); }
00067                         inline iterator       end() { return m_k2v.end(); }
00068 
00069                         inline const_iterator_inverse inverse_begin() const { return m_v2k.begin(); }
00070                         inline iterator_inverse       inverse_begin() { return m_v2k.begin(); }
00071                         inline const_iterator_inverse inverse_end() const { return m_v2k.end(); }
00072                         inline iterator_inverse       inverse_end() { return m_v2k.end(); }
00073 
00074                         inline size_t size() const { return m_k2v.size(); }
00075                         inline bool empty() const { return m_k2v.empty(); }
00076 
00077                         /** Return a read-only reference to the internal map KEY->VALUES */
00078                         const std::map<KEY,VALUE> &getDirectMap() const { return m_k2v; }
00079                         /** Return a read-only reference to the internal map KEY->VALUES */
00080                         const std::map<VALUE,KEY> &getInverseMap() const { return m_v2k; }
00081 
00082                         void clear() //!< Clear the contents of the bi-map.
00083                         {
00084                                 m_k2v.clear();
00085                                 m_v2k.clear();
00086                         }
00087 
00088                         /** Insert a new pair KEY<->VALUE in the bi-map */
00089                         void insert(const KEY &k,const VALUE &v)
00090                         {
00091                                 m_k2v[k]=v;
00092                                 m_v2k[v]=k;
00093                         }
00094 
00095                         /**  Get the value associated the given key, KEY->VALUE, returning false if not present.
00096                           *  \sa inverse, hasKey, hasValue
00097                           * \return false on key not found.
00098                           */
00099                         bool direct(const KEY &k, VALUE &out_v) const
00100                         {
00101                                 const_iterator i=m_k2v.find(k);
00102                                 if (i==m_k2v.end()) return false;
00103                                 out_v = i->second;
00104                                 return true;
00105                         }
00106 
00107                         /** Return true if the given key 'k' is in the bi-map  \sa hasValue, direct, inverse */
00108                         inline bool hasKey(const KEY& k) const {
00109                                 return m_k2v.find(k)!=m_k2v.end();
00110                         }
00111                         /** Return true if the given value 'v' is in the bi-map \sa hasKey, direct, inverse */
00112                         inline bool hasValue(const VALUE& v) const {
00113                                 return m_v2k.find(v)!=m_v2k.end();
00114                         }
00115 
00116                         /**  Get the value associated the given key, KEY->VALUE, raising an exception if not present.
00117                           *  \sa inverse, hasKey, hasValue
00118                           * \exception std::exception On key not present in the bi-map.
00119                           */
00120                         VALUE direct(const KEY &k) const
00121                         {
00122                                 const_iterator i=m_k2v.find(k);
00123                                 if (i==m_k2v.end()) THROW_EXCEPTION("Key not found.");
00124                                 return i->second;
00125                         }
00126 
00127                         /**  Get the key associated the given value, VALUE->KEY, returning false if not present.
00128                           *  \sa direct, hasKey, hasValue
00129                           * \return false on value not found.
00130                           */
00131                         bool inverse(const VALUE &v, KEY &out_k) const
00132                         {
00133                                 const_iterator_inverse i=m_v2k.find(v);
00134                                 if (i==m_v2k.end()) return false;
00135                                 out_k = i->second;
00136                                 return true;
00137                         }
00138 
00139                         /**  Get the key associated the given value, VALUE->KEY, raising an exception if not present.
00140                           *  \sa direct, hasKey, hasValue
00141                           * \return false on value not found.
00142                           */
00143                         KEY inverse(const VALUE &v) const
00144                         {
00145                                 const_iterator_inverse i=m_v2k.find(v);
00146                                 if (i==m_v2k.end()) THROW_EXCEPTION("Value not found.");
00147                                 return i->second;
00148                         }
00149 
00150 
00151                         inline const_iterator find_key(const KEY& k) const  { return m_k2v.find(k); }
00152                         inline iterator       find_key(const KEY& k)        { return m_k2v.find(k); }
00153 
00154                         inline const_iterator_inverse find_value(const VALUE& v) const  { return m_v2k.find(v); }
00155                         inline iterator_inverse       find_value(const VALUE& v)        { return m_v2k.find(v); }
00156 
00157 
00158                 };  // end class bimap
00159 
00160         } // End of namespace
00161 } // End of namespace
00162 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011