1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_PREV_PERMUTATION_HPP
12 #define BOOST_COMPUTE_ALGORITHM_PREV_PERMUTATION_HPP
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/container/detail/scalar.hpp>
19 #include <boost/compute/algorithm/reverse.hpp>
26 /// \brief Helper function for prev_permutation
28 /// To find rightmost element which is greater
29 /// than its next element
31 template<class InputIterator>
32 inline InputIterator prev_permutation_helper(InputIterator first,
36 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
38 size_t count = detail::iterator_range_size(first, last);
39 if(count == 0 || count == 1){
43 const context &context = queue.get_context();
45 detail::meta_kernel k("prev_permutation");
46 size_t index_arg = k.add_arg<int *>(memory_object::global_memory, "index");
47 atomic_max<int_> atomic_max_int;
49 k << k.decl<const int_>("i") << " = get_global_id(0);\n"
50 << k.decl<const value_type>("cur_value") << "="
51 << first[k.var<const int_>("i")] << ";\n"
52 << k.decl<const value_type>("next_value") << "="
53 << first[k.expr<const int_>("i+1")] << ";\n"
54 << "if(cur_value > next_value){\n"
55 << " " << atomic_max_int(k.var<int_ *>("index"), k.var<int_>("i")) << ";\n"
58 kernel kernel = k.compile(context);
60 scalar<int_> index(context);
61 kernel.set_arg(index_arg, index.get_buffer());
63 index.write(static_cast<int_>(-1), queue);
65 queue.enqueue_1d_range_kernel(kernel, 0, count, 0);
67 int result = static_cast<int>(index.read(queue));
68 if(result == -1) return last;
69 else return first + result;
73 /// \brief Helper function for prev_permutation
75 /// To find the largest element to the right of the element found above
76 /// that is smaller than it
78 template<class InputIterator, class ValueType>
79 inline InputIterator pp_floor(InputIterator first,
84 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
86 size_t count = detail::iterator_range_size(first, last);
90 const context &context = queue.get_context();
92 detail::meta_kernel k("pp_floor");
93 size_t index_arg = k.add_arg<int *>(memory_object::global_memory, "index");
94 size_t value_arg = k.add_arg<value_type>(memory_object::private_memory, "value");
95 atomic_max<int_> atomic_max_int;
97 k << k.decl<const int_>("i") << " = get_global_id(0);\n"
98 << k.decl<const value_type>("cur_value") << "="
99 << first[k.var<const int_>("i")] << ";\n"
100 << "if(cur_value >= " << first[k.expr<int_>("*index")]
101 << " && cur_value < value){\n"
102 << " " << atomic_max_int(k.var<int_ *>("index"), k.var<int_>("i")) << ";\n"
105 kernel kernel = k.compile(context);
107 scalar<int_> index(context);
108 kernel.set_arg(index_arg, index.get_buffer());
110 index.write(static_cast<int_>(0), queue);
112 kernel.set_arg(value_arg, value);
114 queue.enqueue_1d_range_kernel(kernel, 0, count, 0);
116 int result = static_cast<int>(index.read(queue));
117 return first + result;
120 } // end detail namespace
123 /// \brief Permutation generating algorithm
125 /// Transforms the range [first, last) into the previous permutation from
126 /// the set of all permutations arranged in lexicographic order
127 /// \return Boolean value signifying if the first permutation was crossed
128 /// and the range was reset
130 /// \param first Iterator pointing to start of range
131 /// \param last Iterator pointing to end of range
132 /// \param queue Queue on which to execute
134 template<class InputIterator>
135 inline bool prev_permutation(InputIterator first,
137 command_queue &queue = system::default_queue())
139 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
141 if(first == last) return false;
143 InputIterator first_element =
144 detail::prev_permutation_helper(first, last, queue);
146 if(first_element == last)
148 reverse(first, last, queue);
152 value_type first_value = first_element.read(queue);
154 InputIterator ceiling_element =
155 detail::pp_floor(first_element + 1, last, first_value, queue);
157 value_type ceiling_value = ceiling_element.read(queue);
159 first_element.write(ceiling_value, queue);
160 ceiling_element.write(first_value, queue);
162 reverse(first_element + 1, last, queue);
167 } // end compute namespace
168 } // end boost namespace
170 #endif // BOOST_COMPUTE_ALGORITHM_PREV_PERMUTATION_HPP