diff --git a/src/core/loops/apply.hh b/src/core/loops/apply.hh index b0daadc..39976ad 100644 --- a/src/core/loops/apply.hh +++ b/src/core/loops/apply.hh @@ -1,142 +1,89 @@ /** * @file * * @author Lucas Frérot * * @section LICENSE * * Copyright (©) 2017 EPFL (Ecole Polytechnique Fédérale de * Lausanne) Laboratory (LSMS - Laboratoire de Simulation en Mécanique des * Solides) * * Tamaas is free software: you can 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 3 of the License, or (at your option) any * later version. * * Tamaas is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with Tamaas. If not, see . * */ /* -------------------------------------------------------------------------- */ #ifndef __APPLY_HH__ #define __APPLY_HH__ /* -------------------------------------------------------------------------- */ #include "tamaas.hh" #include #include #include /* -------------------------------------------------------------------------- */ __BEGIN_TAMAAS__ namespace detail { /// Helper function for application of a functor on a thrust::tuple template -struct Apply; - -template <> -struct Apply<0> { - template - __host__ __device__ static auto apply(Functor&& func, - Tuple&& t[[gnu::unused]]) - -> decltype(func()) { - return func(); - } -}; - -template <> -struct Apply<1> { - template - __host__ __device__ static auto apply(Functor&& func, Tuple&& t) - -> decltype(func(thrust::get<0>(std::forward(t)))) { - return func(thrust::get<0>(std::forward(t))); - } -}; - -template <> -struct Apply<2> { - template - __host__ __device__ static auto apply(Functor&& func, Tuple&& t) - -> decltype(func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)))) { - return func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t))); - } -}; - -template <> -struct Apply<3> { - template - __host__ __device__ static auto apply(Functor&& func, Tuple&& t) - -> decltype(func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t)))) { - return func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t))); +struct Apply { + template + __host__ __device__ static auto apply(Functor&& func, Tuple&& t, + Args&&... args) + -> decltype(Apply::apply(std::forward(func), + std::forward(t), + thrust::get(t), + std::forward(args)...)) { + return Apply::apply( + std::forward(func), std::forward(t), + thrust::get(t), std::forward(args)...); } }; template <> -struct Apply<4> { - template - __host__ __device__ static auto apply(Functor&& func, Tuple&& t) - -> decltype(func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t)), - thrust::get<3>(std::forward(t)))) { - return func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t)), - thrust::get<3>(std::forward(t))); - } -}; - -template <> -struct Apply<5> { - template - __host__ __device__ static auto apply(Functor&& func, Tuple&& t) - -> decltype(func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t)), - thrust::get<3>(std::forward(t)), - thrust::get<4>(std::forward(t)))) { - return func(thrust::get<0>(std::forward(t)), - thrust::get<1>(std::forward(t)), - thrust::get<2>(std::forward(t)), - thrust::get<3>(std::forward(t)), - thrust::get<4>(std::forward(t))); +struct Apply<0> { + template + __host__ __device__ static auto + apply(Functor&& func, Tuple&& t[[gnu::unused]], Args&&... args) + -> decltype(func(std::forward(args)...)) { + return func(std::forward(args)...); } }; /// Helper class for functor application in thrust template class ApplyFunctor { public: __host__ __device__ ApplyFunctor(const Functor& functor) : functor(functor) {} __host__ __device__ ApplyFunctor(const ApplyFunctor& o) : functor(o.functor) {} template __host__ __device__ ret_type operator()(Tuple&& t) const { return Apply::type>::value>::apply(functor, std::forward(t)); } private: const Functor& functor; }; } // namespace detail __END_TAMAAS__ #endif