Wiselib
wiselib.testing/util/pstl/map_static_vector.h
Go to the documentation of this file.
00001 /***************************************************************************
00002  ** This file is part of the generic algorithm library Wiselib.           **
00003  ** Copyright (C) 2008,2009 by the Wisebed (www.wisebed.eu) project.      **
00004  **                                                                       **
00005  ** The Wiselib is free software: you can redistribute it and/or modify   **
00006  ** it under the terms of the GNU Lesser General Public License as        **
00007  ** published by the Free Software Foundation, either version 3 of the    **
00008  ** License, or (at your option) any later version.                       **
00009  **                                                                       **
00010  ** The Wiselib is distributed in the hope that it will be useful,        **
00011  ** but WITHOUT ANY WARRANTY; without even the implied warranty of        **
00012  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         **
00013  ** GNU Lesser General Public License for more details.                   **
00014  **                                                                       **
00015  ** You should have received a copy of the GNU Lesser General Public      **
00016  ** License along with the Wiselib.                                       **
00017  ** If not, see <http://www.gnu.org/licenses/>.                           **
00018  ***************************************************************************/
00019 #ifndef __UTIL_PSTL_MAP_STATIC_VECTOR__
00020 #define __UTIL_PSTL_MAP_STATIC_VECTOR__
00021 
00022 #include "util/pstl/iterator.h"
00023 #include "util/pstl/vector_static.h"
00024 #include "util/pstl/pair.h"
00025 #include "util/serialization/pstl_pair.h"
00026 
00027 namespace wiselib
00028 {
00029 
00030    template<typename OsModel_P,
00031             typename Key_P,
00032             typename Value_P,
00033             unsigned int TABLE_SIZE>
00034    class MapStaticVector
00035       : public vector_static<OsModel_P, pair<Key_P, Value_P>, TABLE_SIZE>
00036    {
00037    public:
00038       typedef OsModel_P OsModel;
00039 
00040       typedef MapStaticVector<OsModel, Key_P, Value_P, TABLE_SIZE> map_type;
00041       typedef typename map_type::vector_type vector_type;
00042 
00043       typedef typename map_type::iterator iterator;
00044       typedef typename map_type::size_type size_type;
00045 
00046       typedef typename map_type::value_type value_type;
00047       typedef Key_P key_type;
00048       typedef Value_P mapped_type;
00049       typedef typename map_type::pointer pointer;
00050       typedef typename map_type::reference reference;
00051       // --------------------------------------------------------------------
00052       MapStaticVector()
00053          : vector_type()
00054       {}
00055       // --------------------------------------------------------------------
00056       MapStaticVector( const MapStaticVector& map )
00057          : vector_type( map )
00058       {}
00059       // --------------------------------------------------------------------
00060       ~MapStaticVector()
00061       {}
00062       // --------------------------------------------------------------------
00063       MapStaticVector& operator=( MapStaticVector& map )
00064       {
00065          vector_type::clear();
00066          for ( iterator it = map.begin(); it != map.end(); ++it )
00067             vector_type::insert( this->end(), *it );
00068 
00069          return *this;
00070       }
00071       // --------------------------------------------------------------------
00072       template <class InputIterator>
00073       MapStaticVector( InputIterator f, InputIterator l )
00074       {
00075          for ( InputIterator it = f; it != l; ++it )
00076             insert( *it );
00077       }
00078       // --------------------------------------------------------------------
00079       void swap( map_type& m )
00080       {
00081          map_type tmp = *this;
00082          *this = m;
00083          m = tmp;
00084       }
00085       // --------------------------------------------------------------------
00088       pair<iterator, bool> insert( const value_type& x )
00089       {
00090          pair<iterator, bool> ret;
00091          ret.first = find(x.first);
00092 
00093          if ( ret.first != this->end() )
00094          {
00095             ret.second = false;
00096             return ret;
00097          }
00098 
00099          ret.first = vector_type::insert( this->end(), x );
00100          ret.second = true;
00101          return ret;
00102       }
00103       // --------------------------------------------------------------------
00104       template <class InputIterator>
00105       void insert ( InputIterator first, InputIterator last )
00106       {
00107          for ( InputIterator it = first; it != last; ++it )
00108             insert( *it );
00109       }
00110       // --------------------------------------------------------------------
00111       size_type erase( const key_type& k )
00112       {
00113          size_type count = 0;
00114          iterator it;
00115          while ( (it = find(k)) != this->end() )
00116          {
00117             vector_type::erase( it );
00118             count++;
00119          }
00120          return count;
00121       }
00123       // --------------------------------------------------------------------
00126       iterator find( const key_type& k )
00127       {
00128          for ( iterator it = this->begin(); it != this->end(); ++it )
00129          {
00130             if ( it->first == k )
00131                return it;
00132          }
00133          return this->end();
00134       }
00135       // --------------------------------------------------------------------
00136       size_type count(const key_type& k)
00137       {
00138          size_type count = 0;
00139 
00140          for ( iterator it = this->begin(); it != this->end(); ++it )
00141          {
00142             if ( it->first == k )
00143                count++;
00144          }
00145 
00146          return count;
00147       }
00149       // --------------------------------------------------------------------
00152       mapped_type& operator[]( const key_type& k )
00153       {
00154          iterator it = find(k);
00155          if ( it != this->end() )
00156             return it->second;
00157 
00158          value_type val;
00159          val.first = k;
00160          this->push_back( val );
00161 
00162          it = find(k);
00163          if ( it != this->end() )
00164             return it->second;
00165 
00166          // return dummy value that can be written to; this dummy value is
00167          // *only* returned if the static vector is full and can not hold
00168          // new components
00169          // TODO: Print Error message since this case should not happen!
00170          return dummy_;
00171       }
00173 
00174    private:
00175       mapped_type dummy_;
00176    };
00177 
00178 }
00179 
00180 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines