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