Reference documentation for deal.II version 9.1.0-pre
mg_level_object.h
1 // ---------------------------------------------------------------------
2 //
3 // Copyright (C) 2003 - 2018 by the deal.II authors
4 //
5 // This file is part of the deal.II library.
6 //
7 // The deal.II library is free software; you can use it, redistribute
8 // it, and/or modify it under the terms of the GNU Lesser General
9 // Public License as published by the Free Software Foundation; either
10 // version 2.1 of the License, or (at your option) any later version.
11 // The full text of the license can be found in the file LICENSE.md at
12 // the top level directory of deal.II.
13 //
14 // ---------------------------------------------------------------------
15 
16 #ifndef dealii_mg_level_object_h
17 #define dealii_mg_level_object_h
18 
19 #include <deal.II/base/subscriptor.h>
20 
21 #include <memory>
22 #include <vector>
23 
24 DEAL_II_NAMESPACE_OPEN
25 
26 
47 template <class Object>
48 class MGLevelObject : public Subscriptor
49 {
50 public:
70  MGLevelObject(const unsigned int minlevel = 0,
71  const unsigned int maxlevel = 0);
72 
76  Object &operator[](const unsigned int level);
77 
84  const Object &operator[](const unsigned int level) const;
85 
97  void
98  resize(const unsigned int new_minlevel, const unsigned int new_maxlevel);
99 
107  operator=(const double d);
108 
119  DEAL_II_DEPRECATED
120  void
121  clear();
122 
131  void
132  clear_elements();
133 
137  unsigned int
138  min_level() const;
139 
143  unsigned int
144  max_level() const;
145 
156  template <typename ActionFunctionObjectType>
157  void
158  apply(ActionFunctionObjectType action);
159 
163  std::size_t
164  memory_consumption() const;
165 
166 private:
170  unsigned int minlevel;
171 
175  std::vector<std::shared_ptr<Object>> objects;
176 };
177 
178 
179 /* ------------------------------------------------------------------- */
180 
181 
182 template <class Object>
184  const unsigned int max)
185  : minlevel(0)
186 {
187  resize(min, max);
188 }
189 
190 
191 template <class Object>
192 Object &MGLevelObject<Object>::operator[](const unsigned int i)
193 {
194  Assert((i >= minlevel) && (i < minlevel + objects.size()),
195  ExcIndexRange(i, minlevel, minlevel + objects.size()));
196  return *objects[i - minlevel];
197 }
198 
199 
200 template <class Object>
201 const Object &MGLevelObject<Object>::operator[](const unsigned int i) const
202 {
203  Assert((i >= minlevel) && (i < minlevel + objects.size()),
204  ExcIndexRange(i, minlevel, minlevel + objects.size()));
205  return *objects[i - minlevel];
206 }
207 
208 
209 template <class Object>
210 void
211 MGLevelObject<Object>::resize(const unsigned int new_minlevel,
212  const unsigned int new_maxlevel)
213 {
214  Assert(new_minlevel <= new_maxlevel, ExcInternalError());
215  // note that on clear(), the
216  // shared_ptr class takes care of
217  // deleting the object it points to
218  // by itself
219  objects.clear();
220 
221  minlevel = new_minlevel;
222  for (unsigned int i = 0; i < new_maxlevel - new_minlevel + 1; ++i)
223  objects.push_back(std::make_shared<Object>());
224 }
225 
226 
227 template <class Object>
230 {
231  typename std::vector<std::shared_ptr<Object>>::iterator v;
232  for (v = objects.begin(); v != objects.end(); ++v)
233  **v = d;
234  return *this;
235 }
236 
237 
238 template <class Object>
239 void
241 {
242  // Avoid code duplication in deprecated call by calling replacing function
243  clear_elements();
244 }
245 
246 
247 template <class Object>
248 void
250 {
251  typename std::vector<std::shared_ptr<Object>>::iterator v;
252  for (v = objects.begin(); v != objects.end(); ++v)
253  (*v)->clear();
254 }
255 
256 
257 template <class Object>
258 unsigned int
260 {
261  return minlevel;
262 }
263 
264 
265 template <class Object>
266 unsigned int
268 {
269  return minlevel + objects.size() - 1;
270 }
271 
272 template <class Object>
273 template <typename ActionFunctionObjectType>
274 void
275 MGLevelObject<Object>::apply(ActionFunctionObjectType action)
276 {
277  for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl)
278  {
279  action(lvl, (*this)[lvl]);
280  }
281 }
282 
283 
284 template <class Object>
285 std::size_t
287 {
288  std::size_t result = sizeof(*this);
289  using Iter = typename std::vector<std::shared_ptr<Object>>::const_iterator;
290  const Iter end = objects.end();
291  for (Iter o = objects.begin(); o != end; ++o)
292  result += (*o)->memory_consumption();
293 
294  return result;
295 }
296 
297 DEAL_II_NAMESPACE_CLOSE
298 
299 #endif
Object & operator[](const unsigned int level)
MGLevelObject(const unsigned int minlevel=0, const unsigned int maxlevel=0)
std::size_t memory_consumption() const
std::vector< std::shared_ptr< Object > > objects
void apply(ActionFunctionObjectType action)
static::ExceptionBase & ExcIndexRange(int arg1, int arg2, int arg3)
#define Assert(cond, exc)
Definition: exceptions.h:1227
unsigned int max_level() const
unsigned int minlevel
MGLevelObject< Object > & operator=(const double d)
unsigned int min_level() const
void resize(const unsigned int new_minlevel, const unsigned int new_maxlevel)
static::ExceptionBase & ExcInternalError()