$extrastylesheet
mapvector.h
Go to the documentation of this file.
00001 // The libMesh Finite Element Library.
00002 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
00003 
00004 // This library is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public
00006 // License as published by the Free Software Foundation; either
00007 // version 2.1 of the License, or (at your option) any later version.
00008 
00009 // This library is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // Lesser General Public License for more details.
00013 
00014 // You should have received a copy of the GNU Lesser General Public
00015 // License along with this library; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 
00018 
00019 
00020 #ifndef LIBMESH_MAPVECTOR_H
00021 #define LIBMESH_MAPVECTOR_H
00022 
00023 // C++ Includes   -----------------------------------
00024 #include <map>
00025 
00026 namespace libMesh
00027 {
00028 
00037 template <typename Val, typename index_t=unsigned int>
00038 class mapvector : public std::map<index_t, Val>
00039 {
00040 public:
00041   typedef std::map<index_t, Val> maptype;
00042 
00043   Val& operator[] (const index_t &k)
00044   {
00045     return maptype::operator[](k);
00046   }
00047   Val operator[] (const index_t &k) const
00048   {
00049     typename maptype::const_iterator it = this->find(k);
00050     return it == this->end().it? Val() : it->second;
00051   }
00052 
00053   class veclike_iterator
00054   {
00055   public:
00056     veclike_iterator(const typename maptype::iterator &i)
00057       : it(i) {}
00058 
00059     veclike_iterator(const veclike_iterator &i)
00060       : it(i.it) {}
00061 
00062     Val& operator*() const { return it->second; }
00063 
00064     veclike_iterator& operator++() { ++it; return *this; }
00065 
00066     veclike_iterator operator++(int) {
00067       veclike_iterator i = *this;
00068       ++(*this);
00069       return i;
00070     }
00071 
00072     bool operator==(const veclike_iterator &other) const {
00073       return it == other.it;
00074     }
00075 
00076     bool operator!=(const veclike_iterator &other) const {
00077       return it != other.it;
00078     }
00079 
00080     typename maptype::iterator it;
00081   };
00082 
00083   class const_veclike_iterator
00084   {
00085   public:
00086     const_veclike_iterator(const typename maptype::const_iterator &i)
00087       : it(i) {}
00088 
00089     const_veclike_iterator(const const_veclike_iterator &i)
00090       : it(i.it) {}
00091 
00092     const_veclike_iterator(const veclike_iterator &i)
00093       : it(i.it) {}
00094 
00095     const Val& operator*() const { return it->second; }
00096 
00097     const_veclike_iterator& operator++() { ++it; return *this; }
00098 
00099     const_veclike_iterator operator++(int) {
00100       veclike_iterator i = *this;
00101       ++(*this);
00102       return i;
00103     }
00104 
00105     bool operator==(const const_veclike_iterator &other) const {
00106       return it == other.it;
00107     }
00108 
00109     bool operator!=(const const_veclike_iterator &other) const {
00110       return it != other.it;
00111     }
00112 
00113     typename maptype::const_iterator it;
00114   };
00115 
00116   void erase(index_t i) {
00117     maptype::erase(i);
00118   }
00119 
00120   void erase(const veclike_iterator &pos) {
00121     maptype::erase(pos.it);
00122   }
00123 
00124   veclike_iterator begin() {
00125     return veclike_iterator(maptype::begin());
00126   }
00127 
00128   const_veclike_iterator begin() const {
00129     return const_veclike_iterator(maptype::begin());
00130   }
00131 
00132   veclike_iterator end() {
00133     return veclike_iterator(maptype::end());
00134   }
00135 
00136   const_veclike_iterator end() const {
00137     return const_veclike_iterator(maptype::end());
00138   }
00139 };
00140 
00141 } // namespace libMesh
00142 
00143 #endif // LIBMESH_MAPVECTOR_H