From 16fdb15395bade4d3aaa0768aec5ffcf7ab3ce48 Mon Sep 17 00:00:00 2001 From: Kyle Lutz Date: Fri, 28 Nov 2014 14:08:59 -0800 Subject: [PATCH] Add identity() function --- include/boost/compute/detail/meta_kernel.hpp | 7 ++ include/boost/compute/functional.hpp | 1 + include/boost/compute/functional/identity.hpp | 64 +++++++++++++++++++ test/CMakeLists.txt | 1 + test/test_functional_identity.cpp | 39 +++++++++++ 5 files changed, 112 insertions(+) create mode 100644 include/boost/compute/functional/identity.hpp create mode 100644 test/test_functional_identity.cpp diff --git a/include/boost/compute/detail/meta_kernel.hpp b/include/boost/compute/detail/meta_kernel.hpp index 544051c7..018f1000 100644 --- a/include/boost/compute/detail/meta_kernel.hpp +++ b/include/boost/compute/detail/meta_kernel.hpp @@ -1027,6 +1027,13 @@ inline meta_kernel& operator<<(meta_kernel &k, return k << "convert_" << type_name() << "(" << expr.m_arg << ")"; } +template +inline meta_kernel& operator<<(meta_kernel &k, + const invoked_identity &expr) +{ + return k << expr.m_arg; +} + template<> struct inject_type_impl { diff --git a/include/boost/compute/functional.hpp b/include/boost/compute/functional.hpp index 3b681f52..ea2606ae 100644 --- a/include/boost/compute/functional.hpp +++ b/include/boost/compute/functional.hpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/include/boost/compute/functional/identity.hpp b/include/boost/compute/functional/identity.hpp new file mode 100644 index 00000000..95e6da80 --- /dev/null +++ b/include/boost/compute/functional/identity.hpp @@ -0,0 +1,64 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2013-2014 Kyle Lutz +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// See http://kylelutz.github.com/compute for more information. +//---------------------------------------------------------------------------// + +#ifndef BOOST_COMPUTE_FUNCTIONAL_IDENTITY_HPP +#define BOOST_COMPUTE_FUNCTIONAL_IDENTITY_HPP + +namespace boost { +namespace compute { +namespace detail { + +template +struct invoked_identity +{ + typedef T result_type; + + invoked_identity(const Arg &arg) + : m_arg(arg) + { + } + + Arg m_arg; +}; + +} // end detail namespace + +/// Identity function which simply returns its input. +/// +/// For example, to directly copy values using the transform() algorithm: +/// \code +/// transform(input.begin(), input.end(), output.begin(), identity(), queue); +/// \endcode +/// +/// \see as(), convert() +template +class identity +{ +public: + /// Identity function result type. + typedef T result_type; + + /// Creates a new identity function. + identity() + { + } + + /// \internal_ + template + detail::invoked_identity operator()(const Arg &arg) const + { + return detail::invoked_identity(arg); + } +}; + +} // end compute namespace +} // end boost namespace + +#endif // BOOST_COMPUTE_FUNCTIONAL_IDENTITY_HPP diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 2a391eab..33471cff 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -138,6 +138,7 @@ add_compute_test("functional.bind" test_functional_bind.cpp) add_compute_test("functional.convert" test_functional_convert.cpp) add_compute_test("functional.get" test_functional_get.cpp) add_compute_test("functional.hash" test_functional_hash.cpp) +add_compute_test("functional.identity" test_functional_identity.cpp) add_compute_test("functional.popcount" test_functional_popcount.cpp) add_compute_test("functional.unpack" test_functional_unpack.cpp) diff --git a/test/test_functional_identity.cpp b/test/test_functional_identity.cpp new file mode 100644 index 00000000..84cbde53 --- /dev/null +++ b/test/test_functional_identity.cpp @@ -0,0 +1,39 @@ +//---------------------------------------------------------------------------// +// Copyright (c) 2013-2014 Kyle Lutz +// +// Distributed under the Boost Software License, Version 1.0 +// See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt +// +// See http://kylelutz.github.com/compute for more information. +//---------------------------------------------------------------------------// + +#define BOOST_TEST_MODULE TestFunctionalIdentity +#include + +#include +#include +#include +#include + +#include "check_macros.hpp" +#include "context_setup.hpp" + +namespace compute = boost::compute; + +BOOST_AUTO_TEST_CASE(copy_with_identity_transform) +{ + int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 }; + compute::vector input(data, data + 8, queue); + compute::vector output(8, context); + + compute::transform( + input.begin(), input.end(), output.begin(), compute::identity(), queue + ); + + CHECK_RANGE_EQUAL( + int, 8, output, (1, 2, 3, 4, 5, 6, 7, 8) + ); +} + +BOOST_AUTO_TEST_SUITE_END()