libMesh
mapvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2017 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 
38 template <typename Val, typename index_t=unsigned int>
39 class mapvector : public std::map<index_t, Val>
40 {
41 public:
42  typedef std::map<index_t, Val> maptype;
43 
44  Val & operator[] (const index_t & k)
45  {
46  return maptype::operator[](k);
47  }
48  Val operator[] (const index_t & k) const
49  {
50  typename maptype::const_iterator it = this->find(k);
51  return it == this->end().it? Val() : it->second;
52  }
53 
55  {
56  public:
57  veclike_iterator(const typename maptype::iterator & i)
58  : it(i) {}
59 
61  : it(i.it) {}
62 
63  Val & operator*() const { return it->second; }
64 
65  veclike_iterator & operator++() { ++it; return *this; }
66 
68  veclike_iterator i = *this;
69  ++(*this);
70  return i;
71  }
72 
73  bool operator==(const veclike_iterator & other) const {
74  return it == other.it;
75  }
76 
77  bool operator!=(const veclike_iterator & other) const {
78  return it != other.it;
79  }
80 
81  typename maptype::iterator it;
82  };
83 
85  {
86  public:
87  const_veclike_iterator(const typename maptype::const_iterator & i)
88  : it(i) {}
89 
91  : it(i.it) {}
92 
94  : it(i.it) {}
95 
96  const Val & operator*() const { return it->second; }
97 
98  const_veclike_iterator & operator++() { ++it; return *this; }
99 
101  veclike_iterator i = *this;
102  ++(*this);
103  return i;
104  }
105 
106  bool operator==(const const_veclike_iterator & other) const {
107  return it == other.it;
108  }
109 
110  bool operator!=(const const_veclike_iterator & other) const {
111  return it != other.it;
112  }
113 
114  typename maptype::const_iterator it;
115  };
116 
117  void erase(index_t i) {
118  maptype::erase(i);
119  }
120 
121  void erase(const veclike_iterator & pos) {
122  maptype::erase(pos.it);
123  }
124 
126  return veclike_iterator(maptype::begin());
127  }
128 
129  const_veclike_iterator begin() const {
130  return const_veclike_iterator(maptype::begin());
131  }
132 
134  return veclike_iterator(maptype::end());
135  }
136 
137  const_veclike_iterator end() const {
138  return const_veclike_iterator(maptype::end());
139  }
140 };
141 
142 } // namespace libMesh
143 
144 #endif // LIBMESH_MAPVECTOR_H
const_veclike_iterator operator++(int)
Definition: mapvector.h:100
This mapvector templated class is intended to provide the performance characteristics of a std::map w...
Definition: mapvector.h:39
veclike_iterator(const veclike_iterator &i)
Definition: mapvector.h:60
void erase(const veclike_iterator &pos)
Definition: mapvector.h:121
std::map< index_t, Val > maptype
Definition: mapvector.h:42
veclike_iterator operator++(int)
Definition: mapvector.h:67
veclike_iterator(const typename maptype::iterator &i)
Definition: mapvector.h:57
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end...
The libMesh namespace provides an interface to certain functionality in the library.
const_veclike_iterator(const typename maptype::const_iterator &i)
Definition: mapvector.h:87
bool operator==(const veclike_iterator &other) const
Definition: mapvector.h:73
veclike_iterator & operator++()
Definition: mapvector.h:65
const_veclike_iterator(const const_veclike_iterator &i)
Definition: mapvector.h:90
const_veclike_iterator(const veclike_iterator &i)
Definition: mapvector.h:93
void erase(index_t i)
Definition: mapvector.h:117
bool operator!=(const const_veclike_iterator &other) const
Definition: mapvector.h:110
veclike_iterator end()
Definition: mapvector.h:133
bool operator==(const const_veclike_iterator &other) const
Definition: mapvector.h:106
const_veclike_iterator & operator++()
Definition: mapvector.h:98
bool operator!=(const veclike_iterator &other) const
Definition: mapvector.h:77
veclike_iterator begin()
Definition: mapvector.h:125
const_veclike_iterator begin() const
Definition: mapvector.h:129
const_veclike_iterator end() const
Definition: mapvector.h:137
Val & operator[](const index_t &k)
Definition: mapvector.h:44