mapvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2014 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #ifndef LIBMESH_MAPVECTOR_H
21 #define LIBMESH_MAPVECTOR_H
22 
23 // C++ Includes -----------------------------------
24 #include <map>
25 
26 namespace libMesh
27 {
28 
37 template <typename Val, typename index_t=unsigned int>
38 class mapvector : public std::map<index_t, Val>
39 {
40 public:
41  typedef std::map<index_t, Val> maptype;
42 
43  Val& operator[] (const index_t &k)
44  {
45  return maptype::operator[](k);
46  }
47  Val operator[] (const index_t &k) const
48  {
49  typename maptype::const_iterator it = this->find(k);
50  return it == this->end().it? Val() : it->second;
51  }
52 
54  {
55  public:
56  veclike_iterator(const typename maptype::iterator &i)
57  : it(i) {}
58 
60  : it(i.it) {}
61 
62  Val& operator*() const { return it->second; }
63 
64  veclike_iterator& operator++() { ++it; return *this; }
65 
67  veclike_iterator i = *this;
68  ++(*this);
69  return i;
70  }
71 
72  bool operator==(const veclike_iterator &other) const {
73  return it == other.it;
74  }
75 
76  bool operator!=(const veclike_iterator &other) const {
77  return it != other.it;
78  }
79 
80  typename maptype::iterator it;
81  };
82 
84  {
85  public:
86  const_veclike_iterator(const typename maptype::const_iterator &i)
87  : it(i) {}
88 
90  : it(i.it) {}
91 
93  : it(i.it) {}
94 
95  const Val& operator*() const { return it->second; }
96 
97  const_veclike_iterator& operator++() { ++it; return *this; }
98 
100  veclike_iterator i = *this;
101  ++(*this);
102  return i;
103  }
104 
105  bool operator==(const const_veclike_iterator &other) const {
106  return it == other.it;
107  }
108 
109  bool operator!=(const const_veclike_iterator &other) const {
110  return it != other.it;
111  }
112 
113  typename maptype::const_iterator it;
114  };
115 
116  void erase(index_t i) {
117  maptype::erase(i);
118  }
119 
120  void erase(const veclike_iterator &pos) {
121  maptype::erase(pos.it);
122  }
123 
124  veclike_iterator begin() {
125  return veclike_iterator(maptype::begin());
126  }
127 
128  const_veclike_iterator begin() const {
129  return const_veclike_iterator(maptype::begin());
130  }
131 
132  veclike_iterator end() {
133  return veclike_iterator(maptype::end());
134  }
135 
136  const_veclike_iterator end() const {
137  return const_veclike_iterator(maptype::end());
138  }
139 };
140 
141 } // namespace libMesh
142 
143 #endif // LIBMESH_MAPVECTOR_H

Site Created By: libMesh Developers
Last modified: February 07 2014 16:57:06 UTC

Hosted By:
SourceForge.net Logo