OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / system / detail / generic / uninitialized_copy.inl
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 #include <thrust/detail/config.h>
18 #include <thrust/system/detail/generic/uninitialized_copy.h>
19 #include <thrust/copy.h>
20 #include <thrust/for_each.h>
21 #include <thrust/detail/internal_functional.h>
22 #include <thrust/detail/type_traits.h>
23 #include <thrust/iterator/iterator_traits.h>
24
25 namespace thrust
26 {
27 namespace system
28 {
29 namespace detail
30 {
31 namespace generic
32 {
33 namespace detail
34 {
35
36 template<typename InputType,
37          typename OutputType>
38   struct uninitialized_copy_functor
39 {
40   template<typename Tuple>
41   __host__ __device__
42   void operator()(Tuple t)
43   {
44     const InputType &in = thrust::get<0>(t);
45     OutputType &out = thrust::get<1>(t);
46
47     ::new(static_cast<void*>(&out)) OutputType(in);
48   } // end operator()()
49 }; // end uninitialized_copy_functor
50
51
52 // non-trivial copy constructor path
53 template<typename ExecutionPolicy,
54          typename InputIterator,
55          typename ForwardIterator>
56   ForwardIterator uninitialized_copy(thrust::execution_policy<ExecutionPolicy> &exec,
57                                      InputIterator first,
58                                      InputIterator last,
59                                      ForwardIterator result,
60                                      thrust::detail::false_type) // has_trivial_copy_constructor
61 {
62   // zip up the iterators
63   typedef thrust::tuple<InputIterator,ForwardIterator> IteratorTuple;
64   typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
65
66   ZipIterator begin = thrust::make_zip_iterator(thrust::make_tuple(first,result));
67   ZipIterator end = begin;
68
69   // get a zip_iterator pointing to the end
70   const typename thrust::iterator_difference<InputIterator>::type n = thrust::distance(first,last);
71   thrust::advance(end, n);
72
73   // create a functor
74   typedef typename iterator_traits<InputIterator>::value_type InputType;
75   typedef typename iterator_traits<ForwardIterator>::value_type OutputType;
76
77   detail::uninitialized_copy_functor<InputType, OutputType> f;
78
79   // do the for_each
80   thrust::for_each(exec, begin, end, f);
81
82   // return the end of the output range
83   return thrust::get<1>(end.get_iterator_tuple());
84 } // end uninitialized_copy()
85
86
87 // trivial copy constructor path
88 template<typename ExecutionPolicy,
89          typename InputIterator,
90          typename ForwardIterator>
91   ForwardIterator uninitialized_copy(thrust::execution_policy<ExecutionPolicy> &exec,
92                                      InputIterator first,
93                                      InputIterator last,
94                                      ForwardIterator result,
95                                      thrust::detail::true_type) // has_trivial_copy_constructor
96 {
97   return thrust::copy(exec, first, last, result);
98 } // end uninitialized_copy()
99
100
101 // non-trivial copy constructor path
102 template<typename ExecutionPolicy,
103          typename InputIterator,
104          typename Size,
105          typename ForwardIterator>
106   ForwardIterator uninitialized_copy_n(thrust::execution_policy<ExecutionPolicy> &exec,
107                                        InputIterator first,
108                                        Size n,
109                                        ForwardIterator result,
110                                        thrust::detail::false_type) // has_trivial_copy_constructor
111 {
112   // zip up the iterators
113   typedef thrust::tuple<InputIterator,ForwardIterator> IteratorTuple;
114   typedef thrust::zip_iterator<IteratorTuple> ZipIterator;
115
116   ZipIterator zipped_first = thrust::make_zip_iterator(thrust::make_tuple(first,result));
117
118   // create a functor
119   typedef typename iterator_traits<InputIterator>::value_type   InputType;
120   typedef typename iterator_traits<ForwardIterator>::value_type OutputType;
121
122   detail::uninitialized_copy_functor<InputType, OutputType> f;
123
124   // do the for_each_n
125   ZipIterator zipped_last = thrust::for_each_n(exec, zipped_first, n, f);
126
127   // return the end of the output range
128   return thrust::get<1>(zipped_last.get_iterator_tuple());
129 } // end uninitialized_copy_n()
130
131
132 // trivial copy constructor path
133 template<typename ExecutionPolicy,
134          typename InputIterator,
135          typename Size,
136          typename ForwardIterator>
137   ForwardIterator uninitialized_copy_n(thrust::execution_policy<ExecutionPolicy> &exec,
138                                        InputIterator first,
139                                        Size n,
140                                        ForwardIterator result,
141                                        thrust::detail::true_type) // has_trivial_copy_constructor
142 {
143   return thrust::copy_n(exec, first, n, result);
144 } // end uninitialized_copy_n()
145
146
147 } // end detail
148
149
150 template<typename ExecutionPolicy,
151          typename InputIterator,
152          typename ForwardIterator>
153   ForwardIterator uninitialized_copy(thrust::execution_policy<ExecutionPolicy> &exec,
154                                      InputIterator first,
155                                      InputIterator last,
156                                      ForwardIterator result)
157 {
158   typedef typename iterator_traits<ForwardIterator>::value_type ResultType;
159
160   typedef typename thrust::detail::has_trivial_copy_constructor<ResultType>::type ResultTypeHasTrivialCopyConstructor;
161
162   return thrust::system::detail::generic::detail::uninitialized_copy(exec, first, last, result, ResultTypeHasTrivialCopyConstructor());
163 } // end uninitialized_copy()
164
165
166 template<typename ExecutionPolicy,
167          typename InputIterator,
168          typename Size,
169          typename ForwardIterator>
170   ForwardIterator uninitialized_copy_n(thrust::execution_policy<ExecutionPolicy> &exec,
171                                        InputIterator first,
172                                        Size n,
173                                        ForwardIterator result)
174 {
175   typedef typename iterator_traits<ForwardIterator>::value_type ResultType;
176
177   typedef typename thrust::detail::has_trivial_copy_constructor<ResultType>::type ResultTypeHasTrivialCopyConstructor;
178
179   return thrust::system::detail::generic::detail::uninitialized_copy_n(exec, first, n, result, ResultTypeHasTrivialCopyConstructor());
180 } // end uninitialized_copy_n()
181
182
183 } // end namespace generic
184 } // end namespace detail
185 } // end namespace system
186 } // end namespace thrust
187