libMesh
mapvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 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 
60  veclike_iterator(const veclike_iterator & i) = default;
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  veclike_iterator erase(const veclike_iterator & pos) {
121  return veclike_iterator(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
libMesh::mapvector::veclike_iterator
Definition: mapvector.h:54
libMesh::mapvector::const_veclike_iterator::const_veclike_iterator
const_veclike_iterator(const veclike_iterator &i)
Definition: mapvector.h:92
libMesh::mapvector::veclike_iterator::operator++
veclike_iterator & operator++()
Definition: mapvector.h:64
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::mapvector::const_veclike_iterator::const_veclike_iterator
const_veclike_iterator(const const_veclike_iterator &i)
Definition: mapvector.h:89
end
IterBase * end
Also have a polymorphic pointer to the end object, this prevents iterating past the end.
Definition: variant_filter_iterator.h:343
libMesh::mapvector::const_veclike_iterator::operator==
bool operator==(const const_veclike_iterator &other) const
Definition: mapvector.h:105
libMesh::mapvector::maptype
std::map< index_t, Val > maptype
Definition: mapvector.h:42
libMesh::mapvector::erase
void erase(index_t i)
Definition: mapvector.h:116
libMesh::mapvector::const_veclike_iterator::operator!=
bool operator!=(const const_veclike_iterator &other) const
Definition: mapvector.h:109
libMesh::mapvector::const_veclike_iterator::operator++
const_veclike_iterator & operator++()
Definition: mapvector.h:97
libMesh::mapvector::const_veclike_iterator::it
maptype::const_iterator it
Definition: mapvector.h:113
libMesh::mapvector::erase
veclike_iterator erase(const veclike_iterator &pos)
Definition: mapvector.h:120
libMesh::mapvector::const_veclike_iterator::const_veclike_iterator
const_veclike_iterator(const typename maptype::const_iterator &i)
Definition: mapvector.h:86
libMesh::mapvector::veclike_iterator::operator++
veclike_iterator operator++(int)
Definition: mapvector.h:66
libMesh::mapvector
This mapvector templated class is intended to provide the performance characteristics of a std::map w...
Definition: mapvector.h:39
libMesh::mapvector::begin
const_veclike_iterator begin() const
Definition: mapvector.h:128
libMesh::mapvector::operator[]
Val & operator[](const index_t &k)
Definition: mapvector.h:44
libMesh::mapvector::veclike_iterator::it
maptype::iterator it
Definition: mapvector.h:80
libMesh::mapvector::const_veclike_iterator::operator*
const Val & operator*() const
Definition: mapvector.h:95
libMesh::mapvector::const_veclike_iterator::operator++
const_veclike_iterator operator++(int)
Definition: mapvector.h:99
libMesh::mapvector::end
veclike_iterator end()
Definition: mapvector.h:132
libMesh::mapvector::begin
veclike_iterator begin()
Definition: mapvector.h:124
libMesh::mapvector::veclike_iterator::operator!=
bool operator!=(const veclike_iterator &other) const
Definition: mapvector.h:76
libMesh::mapvector::veclike_iterator::operator*
Val & operator*() const
Definition: mapvector.h:62
libMesh::mapvector::const_veclike_iterator
Definition: mapvector.h:83
libMesh::mapvector::veclike_iterator::operator==
bool operator==(const veclike_iterator &other) const
Definition: mapvector.h:72
libMesh::mapvector::veclike_iterator::veclike_iterator
veclike_iterator(const typename maptype::iterator &i)
Definition: mapvector.h:57
libMesh::mapvector::end
const_veclike_iterator end() const
Definition: mapvector.h:136