OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / system / detail / internal / scalar / unique_by_key.h
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 /*! \file unique_by_key.h
19  *  \brief Sequential implementations of unique_by_key algorithms.
20  */
21
22 #pragma once
23
24 #include <thrust/detail/config.h>
25 #include <thrust/iterator/iterator_traits.h>
26 #include <thrust/pair.h>
27
28 namespace thrust
29 {
30 namespace system
31 {
32 namespace detail
33 {
34 namespace internal
35 {
36 namespace scalar
37 {
38
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)
51 {
52   typedef typename thrust::iterator_traits<InputIterator1>::value_type  InputKeyType;
53   typedef typename thrust::iterator_traits<OutputIterator2>::value_type OutputValueType;
54
55   if(keys_first != keys_last)
56   {
57     InputKeyType    temp_key   = *keys_first;
58     OutputValueType temp_value = *values_first;
59
60     for(++keys_first, ++values_first;
61         keys_first != keys_last;
62         ++keys_first, ++values_first)
63     {
64       InputKeyType    key   = *keys_first;
65       OutputValueType value = *values_first;
66
67       if(!binary_pred(temp_key, key))
68       {
69         *keys_output   = temp_key;
70         *values_output = temp_value;
71
72         ++keys_output;
73         ++values_output;
74
75         temp_key   = key;
76         temp_value = value;
77       }
78     }
79
80     *keys_output   = temp_key;
81     *values_output = temp_value;
82
83     ++keys_output;
84     ++values_output;
85   }
86
87   return thrust::make_pair(keys_output, values_output);
88 } // end unique_by_key_copy()
89
90
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)
99 {
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()
103
104 } // end namespace scalar
105 } // end namespace internal
106 } // end namespace detail
107 } // end namespace system
108 } // end namespace thrust
109