Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:58

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/system.hpp>
0017 #include <boost/compute/context.hpp>
0018 #include <boost/compute/functional.hpp>
0019 #include <boost/compute/command_queue.hpp>
0020 #include <boost/compute/algorithm/copy_if.hpp>
0021 #include <boost/compute/container/vector.hpp>
0022 #include <boost/compute/type_traits/is_device_iterator.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 ///
0028 /// \brief Partitioning algorithm
0029 ///
0030 /// Partitions the elements in the range [\p first, \p last) according to
0031 /// \p predicate. The order of the elements is preserved.
0032 /// \return Iterator pointing to end of true values
0033 ///
0034 /// \param first Iterator pointing to start of range
0035 /// \param last Iterator pointing to end of range
0036 /// \param predicate Unary predicate to be applied on each element
0037 /// \param queue Queue on which to execute
0038 ///
0039 /// Space complexity: \Omega(3n)
0040 ///
0041 /// \see is_partitioned() and partition()
0042 ///
0043 template<class Iterator, class UnaryPredicate>
0044 inline Iterator stable_partition(Iterator first,
0045                                  Iterator last,
0046                                  UnaryPredicate predicate,
0047                                  command_queue &queue = system::default_queue())
0048 {
0049     BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
0050     typedef typename std::iterator_traits<Iterator>::value_type value_type;
0051 
0052     // make temporary copy of the input
0053     ::boost::compute::vector<value_type> tmp(first, last, queue);
0054 
0055     // copy true values
0056     Iterator last_true =
0057         ::boost::compute::copy_if(tmp.begin(),
0058                                   tmp.end(),
0059                                   first,
0060                                   predicate,
0061                                   queue);
0062 
0063     // copy false values
0064     Iterator last_false =
0065         ::boost::compute::copy_if(tmp.begin(),
0066                                   tmp.end(),
0067                                   last_true,
0068                                   not1(predicate),
0069                                   queue);
0070 
0071     // return iterator pointing to the last true value
0072     return last_true;
0073 }
0074 
0075 } // end compute namespace
0076 } // end boost namespace
0077 
0078 #endif // BOOST_COMPUTE_ALGORITHM_STABLE_PARTITION_HPP