]> https://gitweb.dealii.org/ - dealii.git/commitdiff
add MGLevelObject::apply() 4728/head
authorTimo Heister <timo.heister@gmail.com>
Tue, 8 Aug 2017 18:25:24 +0000 (12:25 -0600)
committerTimo Heister <timo.heister@gmail.com>
Tue, 8 Aug 2017 18:28:03 +0000 (12:28 -0600)
include/deal.II/base/mg_level_object.h
tests/multigrid/mg_level_obj_01.cc [new file with mode: 0644]
tests/multigrid/mg_level_obj_01.output [new file with mode: 0644]

index cf632d0a583d6a4dd72cbaf238e2da75183ababc..5d848658cbc7aa2d813d26c1b35d904f7a1c713a 100644 (file)
@@ -136,6 +136,19 @@ public:
    */
   unsigned int max_level () const;
 
+  /**
+   * Apply the action @p action to every object stored in here. The
+   * parameter @p action is expected to be a function object that accepts
+   * the syntax
+   * <code>
+   *   action(const unsigned int level, Object &object);
+   * </code>
+   * This means this function can accept a lambda, a std::function, or a plain
+   * function pointer.
+   */
+  template <typename ActionFunctionObjectType>
+  void apply (ActionFunctionObjectType action);
+
   /**
    * Memory used by this object.
    */
@@ -250,6 +263,17 @@ MGLevelObject<Object>::max_level () const
   return minlevel + objects.size() - 1;
 }
 
+template<class Object>
+template <typename ActionFunctionObjectType>
+void
+MGLevelObject<Object>::apply (ActionFunctionObjectType action)
+{
+  for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl)
+    {
+      action (lvl, (*this)[lvl]);
+    }
+}
+
 
 template <class Object>
 std::size_t
diff --git a/tests/multigrid/mg_level_obj_01.cc b/tests/multigrid/mg_level_obj_01.cc
new file mode 100644 (file)
index 0000000..89e5cb4
--- /dev/null
@@ -0,0 +1,52 @@
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2017 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+
+// check MGLevelObject::apply()
+
+#include "../tests.h"
+#include <deal.II/base/mg_level_object.h>
+
+#include <fstream>
+#include <iomanip>
+#include <iomanip>
+#include <algorithm>
+
+using namespace std;
+
+
+template <class T>
+void check(MGLevelObject<T> &obj)
+{
+  obj.apply([&] (const unsigned int lvl, T & value)
+  {
+    value = (T)lvl;
+  });
+
+  obj.apply([&] (const unsigned int lvl, T & value)
+  {
+    deallog << "lvl: " << lvl << " value: " << value << std::endl;
+  });
+}
+
+int main()
+{
+  initlog();
+
+  MGLevelObject<double> o(2,4);
+  check(o);
+  o.resize(0,1);
+  check(o);
+}
diff --git a/tests/multigrid/mg_level_obj_01.output b/tests/multigrid/mg_level_obj_01.output
new file mode 100644 (file)
index 0000000..acd41a7
--- /dev/null
@@ -0,0 +1,6 @@
+
+DEAL::lvl: 2 value: 2.00000
+DEAL::lvl: 3 value: 3.00000
+DEAL::lvl: 4 value: 4.00000
+DEAL::lvl: 0 value: 0.00000
+DEAL::lvl: 1 value: 1.00000

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.