sprint 1-alpha
sprint/container/unordered_map.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 *   sprint::unordered_map
00003 *
00004 *   Copyright (C) 2005-2011  Paolo Medici <www.pmx.it>
00005 *
00006 *  This library is free software; you can redistribute it and/or
00007 *  modify it under the terms of the GNU Lesser General Public
00008 *  License as published by the Free Software Foundation; either
00009 *  version 2.1 of the License, or (at your option) any later version.
00010 *
00011 *  This library is distributed in the hope that it will be useful,
00012 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 *  Lesser General Public License for more details.
00015 *
00016 *  You should have received a copy of the GNU Lesser General Public
00017 *  License along with this library; if not, write to the Free Software
00018 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 *
00020 *******************************************************************************/
00021 
00028 #ifndef _SPRINT_UNORDERED_MAP_H
00029 #define _SPRINT_UNORDERED_MAP_H
00030 
00031 #include <vector>
00032 #include <utility> // std::pair
00033 
00034 namespace sprint {
00035 
00038 template <typename _Key, typename _Tp>
00039 class unordered_map: public std::vector< std::pair<_Key,_Tp> > {
00040         public:
00041                 typedef _Key                                                            key_type;
00042     typedef _Tp                                                                 mapped_type;
00043     typedef std::pair<_Key, _Tp>                 value_type;
00044 
00045                 typedef typename std::vector< value_type >      parent;
00046         public:
00047                 typedef typename parent::iterator               iterator;
00048     typedef typename parent::const_iterator         const_iterator;    
00049   public: // temporary interface
00050                 iterator
00051                 force_insert(const key_type & __x, const mapped_type & type)
00052                 {
00053                         parent::push_back(value_type(__x, mapped_type() ));
00054                         return parent::end() - 1;
00055                 }       
00056         public:
00057     
00059                 iterator find(const key_type& __x)      {
00060                         for(iterator i = parent::begin(); i!= parent::end(); i++)
00061                                 if(i->first == __x)
00062                                                 return i;
00063                         return parent::end();
00064                         }
00066                 const_iterator find(const key_type& __x)        const {
00067                         for(const_iterator i = parent::begin(); i!= parent::end(); i++)
00068                                 if(i->first == __x)
00069                                                 return i;
00070                         return parent::end();
00071                 }       
00072                 
00074                 mapped_type &
00075                 operator[](const key_type & __x)
00076                 {
00077                         iterator i = find(__x);
00078                         if(i==parent::end())
00079                                 i = force_insert(__x, mapped_type());
00080                 return i->second;
00081                 }
00082                 
00085                 void insert(const value_type & __x)
00086                  {
00087              parent::push_back(__x);
00088          }
00089 
00090 };
00091 
00092 }
00093 
00094 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines