From: Timo Heister Date: Tue, 8 Aug 2017 18:25:24 +0000 (-0600) Subject: add MGLevelObject::apply() X-Git-Tag: v9.0.0-rc1~1337^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F4728%2Fhead;p=dealii.git add MGLevelObject::apply() --- diff --git a/include/deal.II/base/mg_level_object.h b/include/deal.II/base/mg_level_object.h index cf632d0a58..5d848658cb 100644 --- a/include/deal.II/base/mg_level_object.h +++ b/include/deal.II/base/mg_level_object.h @@ -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 + * + * action(const unsigned int level, Object &object); + * + * This means this function can accept a lambda, a std::function, or a plain + * function pointer. + */ + template + void apply (ActionFunctionObjectType action); + /** * Memory used by this object. */ @@ -250,6 +263,17 @@ MGLevelObject::max_level () const return minlevel + objects.size() - 1; } +template +template +void +MGLevelObject::apply (ActionFunctionObjectType action) +{ + for (unsigned int lvl = min_level(); lvl <= max_level(); ++lvl) + { + action (lvl, (*this)[lvl]); + } +} + template 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 index 0000000000..89e5cb4204 --- /dev/null +++ b/tests/multigrid/mg_level_obj_01.cc @@ -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 + +#include +#include +#include +#include + +using namespace std; + + +template +void check(MGLevelObject &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 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 index 0000000000..acd41a76c2 --- /dev/null +++ b/tests/multigrid/mg_level_obj_01.output @@ -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