2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 /*! \file unique_by_key.h
19 * \brief Sequential implementations of unique_by_key algorithms.
24 #include <thrust/detail/config.h>
25 #include <thrust/iterator/iterator_traits.h>
26 #include <thrust/pair.h>
39 template<typename InputIterator1,
40 typename InputIterator2,
41 typename OutputIterator1,
42 typename OutputIterator2,
43 typename BinaryPredicate>
44 thrust::pair<OutputIterator1,OutputIterator2>
45 unique_by_key_copy(InputIterator1 keys_first,
46 InputIterator1 keys_last,
47 InputIterator2 values_first,
48 OutputIterator1 keys_output,
49 OutputIterator2 values_output,
50 BinaryPredicate binary_pred)
52 typedef typename thrust::iterator_traits<InputIterator1>::value_type InputKeyType;
53 typedef typename thrust::iterator_traits<OutputIterator2>::value_type OutputValueType;
55 if(keys_first != keys_last)
57 InputKeyType temp_key = *keys_first;
58 OutputValueType temp_value = *values_first;
60 for(++keys_first, ++values_first;
61 keys_first != keys_last;
62 ++keys_first, ++values_first)
64 InputKeyType key = *keys_first;
65 OutputValueType value = *values_first;
67 if(!binary_pred(temp_key, key))
69 *keys_output = temp_key;
70 *values_output = temp_value;
80 *keys_output = temp_key;
81 *values_output = temp_value;
87 return thrust::make_pair(keys_output, values_output);
88 } // end unique_by_key_copy()
91 template<typename ForwardIterator1,
92 typename ForwardIterator2,
93 typename BinaryPredicate>
94 thrust::pair<ForwardIterator1,ForwardIterator2>
95 unique_by_key(ForwardIterator1 keys_first,
96 ForwardIterator1 keys_last,
97 ForwardIterator2 values_first,
98 BinaryPredicate binary_pred)
100 // unique_by_key_copy() permits in-situ operation
101 return thrust::system::detail::internal::scalar::unique_by_key_copy(keys_first, keys_last, values_first, keys_first, values_first, binary_pred);
102 } // end unique_by_key()
104 } // end namespace scalar
105 } // end namespace internal
106 } // end namespace detail
107 } // end namespace system
108 } // end namespace thrust