Wiselib
wiselib.testing/util/pstl/map_list.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_LIST__
00020 #define __UTIL_PSTL_MAP_LIST__
00021 
00022 #include "util/pstl/pair.h"
00023 
00024 namespace wiselib {
00025    
00036    template<
00037       typename OsModel_P,
00038       typename List_P
00039    >
00040    class MapList {
00041       public:
00042          typedef OsModel_P OsModel;
00043          typedef typename OsModel::size_t size_t;
00044          typedef typename OsModel::size_t size_type;
00045          typedef List_P list_type;
00046          typedef typename list_type::value_type::first_type key_type;
00047          typedef typename list_type::value_type::second_type mapped_type;
00048          typedef typename list_type::value_type value_type;
00049          
00050          typedef MapList<OsModel_P, List_P> self_type;
00051          
00052          typedef typename list_type::value_type pair_type;
00053          typedef typename list_type::value_type list_value_type;
00054          
00055          typedef typename list_type::iterator iterator;
00056          typedef typename list_type::const_iterator const_iterator;
00057          
00058          MapList() : list_() { }
00059          ~MapList() { clear(); }
00060          
00061          list_type& list() { return list_; }
00062          
00063          iterator begin() { return list_.begin(); }
00064          iterator end() { return list_.end(); }
00065          const_iterator begin() const { return list_.begin(); }
00066          const_iterator end() const { return list_.end(); }
00067          
00068          bool empty() const { return list_.empty(); }
00069          size_t size() const { return list_.size(); }
00070          
00071          mapped_type& operator[](const key_type& x) {
00072             pair<key_type, mapped_type> p = make_pair(x, mapped_type());
00073             iterator it = insert(p).first;
00074             return it->second;
00075          }
00076          
00077          pair<iterator, bool> insert(const value_type& v) {
00078             pair<iterator, bool> r;
00079             r.first = find(v.first);
00080             if(r.first != end()) {
00081                r.second = false;
00082             }
00083             else {
00084                iterator i = list_.push_back(v);
00085                r.first = i;
00086                r.second = true;
00087             }
00088             return r;
00089          }
00090          
00091          iterator insert(iterator pos, const value_type& v) {
00092             return insert(v).first;
00093          }
00094          
00095          void insert(iterator first, iterator last) {
00096             for(iterator i = first; i != last; ++i) {
00097                insert(*i);
00098             }
00099          }
00100          
00101          void erase(iterator iter) {
00102             list_.erase(iter);
00103          }
00104          
00105          void erase(iterator first, iterator last) {
00106             iterator rm = first;
00107             while(rm != last && rm != end()) {
00108                rm = erase(rm);
00109             }
00110          }
00111          
00112          size_t erase(const key_type& k) {
00113             size_t count = 0;
00114             iterator it = find(k);
00115             while(it != end()) {
00116                list_.erase(it);
00117                count++;
00118                it = find(k);
00119             };
00120             return count;
00121          }
00122          
00123          void swap(self_type& other) { list_.swap(other.list_); }
00124          void clear() { list_.clear(); }
00125          
00126          iterator find(const key_type& k) {
00127             iterator it = begin();
00128             while(it != end() && it->first != k) {
00129                ++it;
00130             }
00131             return it;
00132          }
00133          
00134          const_iterator find(const key_type& k) const {
00135             const_iterator it = begin();
00136             while(it != end() && it->first != k) {
00137                ++it;
00138             }
00139             return it;
00140          }
00141          
00142          size_t count(const key_type& k) const {
00143             return find(k) == end() ? 0 : 1;
00144          }
00145       
00146       private:
00147          list_type list_;
00148          
00149    }; // class MapList
00150 
00151 } // namespace
00152 
00153 
00154 #endif // __UTIL_PSTL_MAP_LIST__
00155 
00156 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines