From 13a2a4efeab198853d4cac4f599b0fc1fd1bad2a Mon Sep 17 00:00:00 2001 From: Wolfgang Bangerth Date: Wed, 29 Dec 2021 10:23:49 -0700 Subject: [PATCH] Simplify code by using lambda functions. --- include/deal.II/base/parallel.h | 116 +++++++------------------------- 1 file changed, 26 insertions(+), 90 deletions(-) diff --git a/include/deal.II/base/parallel.h b/include/deal.II/base/parallel.h index bef3174dd1..a45bfd8ace 100644 --- a/include/deal.II/base/parallel.h +++ b/include/deal.II/base/parallel.h @@ -71,84 +71,6 @@ namespace parallel #endif - - /** - * Convert a function object of type F into an object that can be applied - * to all elements of a range of synchronous iterators. - */ - template - struct Body - { - /** - * Constructor. Take and package the given function object. - */ - Body(const F &f) - : f(f) - {} - - template - void - operator()(const Range &range) const - { - for (typename Range::const_iterator p = range.begin(); p != range.end(); - ++p) - apply(f, *p); - } - - private: - /** - * The stored function object. - */ - const F f; - - /** - * Apply F to a set of iterators with two elements. - */ - template - static void - apply(const F &f, const std::tuple &p) - { - *std::get<1>(p) = f(*std::get<0>(p)); - } - - /** - * Apply F to a set of iterators with three elements. - */ - template - static void - apply(const F &f, const std::tuple &p) - { - *std::get<2>(p) = f(*std::get<0>(p), *std::get<1>(p)); - } - - /** - * Apply F to a set of iterators with three elements. - */ - template - static void - apply(const F &f, const std::tuple &p) - { - *std::get<3>(p) = f(*std::get<0>(p), *std::get<1>(p), *std::get<2>(p)); - } - }; - - - - /** - * Take a function object and create a Body object from it. We do this in - * this helper function since alternatively we would have to specify the - * actual data type of F -- which for function objects is often - * extraordinarily complicated. - */ - template - Body - make_body(const F &f) - { - return Body(f); - } - - - #ifdef DEAL_II_WITH_TBB /** * Encapsulate tbb::parallel_for. @@ -228,10 +150,13 @@ namespace parallel using SyncIterators = SynchronousIterators; Iterators x_begin(begin_in, out); Iterators x_end(end_in, OutputIterator()); - internal::parallel_for(SyncIterators(x_begin), - SyncIterators(x_end), - internal::make_body(predicate), - grainsize); + internal::parallel_for( + SyncIterators(x_begin), + SyncIterators(x_end), + [predicate](const std::tuple &p) { + *std::get<1>(p) = predicate(*std::get<0>(p)); + }, + grainsize); #endif } @@ -285,10 +210,14 @@ namespace parallel using SyncIterators = SynchronousIterators; Iterators x_begin(begin_in1, in2, out); Iterators x_end(end_in1, InputIterator2(), OutputIterator()); - internal::parallel_for(SyncIterators(x_begin), - SyncIterators(x_end), - internal::make_body(predicate), - grainsize); + internal::parallel_for( + SyncIterators(x_begin), + SyncIterators(x_end), + [predicate]( + const std::tuple &p) { + *std::get<2>(p) = predicate(*std::get<0>(p), *std::get<1>(p)); + }, + grainsize); #endif } @@ -347,10 +276,17 @@ namespace parallel InputIterator2(), InputIterator3(), OutputIterator()); - internal::parallel_for(SyncIterators(x_begin), - SyncIterators(x_end), - internal::make_body(predicate), - grainsize); + internal::parallel_for( + SyncIterators(x_begin), + SyncIterators(x_end), + [predicate]( + const std:: + tuple + &p) { + *std::get<3>(p) = + predicate(*std::get<0>(p), *std::get<1>(p), *std::get<2>(p)); + }, + grainsize); #endif } -- 2.39.5