| 78 | |
| 79 | template<class InputIterator, class OutputIterator, class BinaryPredicate> |
| 80 | inline OutputIterator unique_copy(InputIterator first, |
| 81 | InputIterator last, |
| 82 | OutputIterator result, |
| 83 | BinaryPredicate op, |
| 84 | command_queue &queue) |
| 85 | { |
| 86 | if(first == last){ |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | const context &context = queue.get_context(); |
| 91 | size_t count = detail::iterator_range_size(first, last); |
| 92 | |
| 93 | // flags marking unique elements |
| 94 | vector<uint_> flags(count, context); |
| 95 | |
| 96 | // find each unique element and mark it with a one |
| 97 | transform( |
| 98 | first, last - 1, first + 1, flags.begin() + 1, not2(op), queue |
| 99 | ); |
| 100 | |
| 101 | // first element is always unique |
| 102 | fill_n(flags.begin(), 1, 1, queue); |
| 103 | |
| 104 | // storage for desination indices |
| 105 | vector<uint_> indices(count, context); |
| 106 | |
| 107 | // copy indices for each unique element |
| 108 | vector<uint_>::iterator last_index = detail::copy_index_if( |
| 109 | flags.begin(), flags.end(), indices.begin(), lambda::_1 == 1, queue |
| 110 | ); |
| 111 | |
| 112 | // copy unique values from input to output using the computed indices |
| 113 | gather(indices.begin(), last_index, first, result, queue); |
| 114 | |
| 115 | // return an iterator to the end of the unique output range |
| 116 | return result + std::distance(indices.begin(), last_index); |
| 117 | } |
| 118 | |
| 119 | } // end detail namespace |
| 120 | |