libMesh
mapvector.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2025 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 // libMesh Includes -------------------------------
24 #include "libmesh/pool_allocator.h"
25 
26 // C++ Includes -----------------------------------
27 #include <map>
28 
29 namespace libMesh
30 {
31 
41 template <typename Val, typename index_t=unsigned int>
42 class mapvector :
43  public std::map<index_t, Val, std::less<index_t>,
44  FastPoolAllocator<std::pair<const index_t, Val>>>
45 {
46 public:
47  typedef std::map<index_t, Val, std::less<index_t>,
49 
51  {
52  public:
53  veclike_iterator(const typename maptype::iterator & i)
54  : it(i) {}
55 
56  Val & operator*() const { return it->second; }
57 
58  index_t index() const { return it->first; }
59 
60  veclike_iterator & operator++() { ++it; return *this; }
61 
63  {
64  veclike_iterator i = *this;
65  ++(*this);
66  return i;
67  }
68 
69  bool operator==(const veclike_iterator & other) const
70  {
71  return it == other.it;
72  }
73 
74  bool operator!=(const veclike_iterator & other) const
75  {
76  return it != other.it;
77  }
78 
79  private:
80  friend class mapvector;
81 
82  typename maptype::iterator it;
83  };
84 
86  {
87  public:
88  const_veclike_iterator(const typename maptype::const_iterator & i)
89  : it(i) {}
90 
92  : it(i.it) {}
93 
94  const Val & operator*() const { return it->second; }
95 
96  index_t index() const { return it->first; }
97 
98  const_veclike_iterator & operator++() { ++it; return *this; }
99 
101  {
102  veclike_iterator i = *this;
103  ++(*this);
104  return i;
105  }
106 
107  bool operator==(const const_veclike_iterator & other) const
108  {
109  return it == other.it;
110  }
111 
112  bool operator!=(const const_veclike_iterator & other) const
113  {
114  return it != other.it;
115  }
116 
117  private:
118  friend class mapvector;
119 
120  typename maptype::const_iterator it;
121  };
122 
124  {
125  public:
126  const_reverse_veclike_iterator(const typename maptype::const_reverse_iterator & i)
127  : it(i) {}
128 
130  : it(i.it) {}
131 
133  : it(i.it) {}
134 
135  const Val & operator*() const { return it->second; }
136 
138 
140  {
142  ++(*this);
143  return i;
144  }
145 
146  bool operator==(const const_reverse_veclike_iterator & other) const
147  {
148  return it == other.it;
149  }
150 
151  bool operator!=(const const_reverse_veclike_iterator & other) const
152  {
153  return it != other.it;
154  }
155 
156  private:
157  friend class mapvector;
158 
159  typename maptype::const_reverse_iterator it;
160  };
161 
162  veclike_iterator find (const index_t & k)
163  {
164  return veclike_iterator(maptype::find(k));
165  }
166 
167  const_veclike_iterator find (const index_t & k) const
168  {
169  return const_veclike_iterator(maptype::find(k));
170  }
171 
172  Val & operator[] (const index_t & k)
173  {
174  return maptype::operator[](k);
175  }
176 
177  Val operator[] (const index_t & k) const
178  {
179  auto it = this->maptype::find(k);
180  return it == this->end().it? Val() : it->second;
181  }
182 
183  void erase(index_t i)
184  {
185  maptype::erase(i);
186  }
187 
188  veclike_iterator erase(const veclike_iterator & pos)
189  {
190  return veclike_iterator(maptype::erase(pos.it));
191  }
192 
193  veclike_iterator begin()
194  {
195  return veclike_iterator(maptype::begin());
196  }
197 
198  const_veclike_iterator begin() const
199  {
200  return const_veclike_iterator(maptype::begin());
201  }
202 
203  veclike_iterator end()
204  {
205  return veclike_iterator(maptype::end());
206  }
207 
208  const_veclike_iterator end() const
209  {
210  return const_veclike_iterator(maptype::end());
211  }
212 };
213 
214 } // namespace libMesh
215 
216 #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:42
veclike_iterator erase(const veclike_iterator &pos)
Definition: mapvector.h:188
bool operator!=(const const_veclike_iterator &other) const
Definition: mapvector.h:112
bool operator!=(const veclike_iterator &other) const
Definition: mapvector.h:74
An allocator which can be used in standard containers.
veclike_iterator operator++(int)
Definition: mapvector.h:62
const_veclike_iterator end() const
Definition: mapvector.h:208
bool operator==(const const_reverse_veclike_iterator &other) const
Definition: mapvector.h:146
const_veclike_iterator find(const index_t &k) const
Definition: mapvector.h:167
veclike_iterator(const typename maptype::iterator &i)
Definition: mapvector.h:53
The libMesh namespace provides an interface to certain functionality in the library.
const_reverse_veclike_iterator & operator++()
Definition: mapvector.h:137
const_veclike_iterator(const typename maptype::const_iterator &i)
Definition: mapvector.h:88
const_reverse_veclike_iterator operator++(int)
Definition: mapvector.h:139
bool operator==(const veclike_iterator &other) const
Definition: mapvector.h:69
veclike_iterator & operator++()
Definition: mapvector.h:60
const_veclike_iterator(const veclike_iterator &i)
Definition: mapvector.h:91
bool operator!=(const const_reverse_veclike_iterator &other) const
Definition: mapvector.h:151
void erase(index_t i)
Definition: mapvector.h:183
veclike_iterator find(const index_t &k)
Definition: mapvector.h:162
veclike_iterator end()
Definition: mapvector.h:203
const_reverse_veclike_iterator(const veclike_iterator &i)
Definition: mapvector.h:132
std::map< index_t, Val, std::less< index_t >, FastPoolAllocator< std::pair< const index_t, Val > > > maptype
Definition: mapvector.h:48
const_veclike_iterator & operator++()
Definition: mapvector.h:98
veclike_iterator begin()
Definition: mapvector.h:193
const_reverse_veclike_iterator(const const_veclike_iterator &i)
Definition: mapvector.h:129
maptype::const_reverse_iterator it
Definition: mapvector.h:159
const_veclike_iterator begin() const
Definition: mapvector.h:198
bool operator==(const const_veclike_iterator &other) const
Definition: mapvector.h:107
const_reverse_veclike_iterator(const typename maptype::const_reverse_iterator &i)
Definition: mapvector.h:126
Val & operator[](const index_t &k)
Definition: mapvector.h:172