OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / detail / allocator / fill_construct_range.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/detail/type_traits.h>
19 #include <thrust/detail/allocator/allocator_traits.h>
20 #include <thrust/detail/type_traits/pointer_traits.h>
21 #include <thrust/for_each.h>
22 #include <thrust/uninitialized_fill.h>
23 #include <memory>
24
25 namespace thrust
26 {
27 namespace detail
28 {
29 namespace allocator_traits_detail
30 {
31
32 // fill_construct_range has 2 cases:
33 // if Allocator has an effectful member function construct:
34 //   1. construct via the allocator
35 // else
36 //   2. construct via uninitialized_fill
37
38 template<typename Allocator, typename T, typename Arg1>
39   struct has_effectful_member_construct2
40     : has_member_construct2<Allocator,T,Arg1>
41 {};
42
43 // std::allocator::construct's only effect is to invoke placement new
44 template<typename U, typename T, typename Arg1>
45   struct has_effectful_member_construct2<std::allocator<U>,T,Arg1>
46     : thrust::detail::false_type
47 {};
48
49
50 template<typename Allocator, typename Arg1>
51   struct construct2_via_allocator
52 {
53   Allocator &a;
54   Arg1 arg;
55
56   construct2_via_allocator(Allocator &a, const Arg1 &arg)
57     : a(a), arg(arg)
58   {}
59
60   template<typename T>
61   inline __host__ __device__
62   void operator()(T &x)
63   {
64     allocator_traits<Allocator>::construct(a, &x, arg);
65   }
66 };
67
68
69 template<typename Allocator, typename Pointer, typename Size, typename T>
70   typename enable_if<
71     has_effectful_member_construct2<
72       Allocator,
73       typename pointer_element<Pointer>::type,
74       T
75     >::value
76   >::type
77     fill_construct_range(Allocator &a, Pointer p, Size n, const T &value)
78 {
79   thrust::for_each_n(allocator_system<Allocator>::get(a), p, n, construct2_via_allocator<Allocator,T>(a, value));
80 }
81
82
83 template<typename Allocator, typename Pointer, typename Size, typename T>
84   typename disable_if<
85     has_effectful_member_construct2<
86       Allocator,
87       typename pointer_element<Pointer>::type,
88       T
89     >::value
90   >::type
91     fill_construct_range(Allocator &a, Pointer p, Size n, const T &value)
92 {
93   thrust::uninitialized_fill_n(allocator_system<Allocator>::get(a), p, n, value);
94 }
95
96
97 } // end allocator_traits_detail
98
99
100 template<typename Alloc, typename Pointer, typename Size, typename T>
101   void fill_construct_range(Alloc &a, Pointer p, Size n, const T &value)
102 {
103   return allocator_traits_detail::fill_construct_range(a,p,n,value);
104 }
105
106
107 } // end detail
108 } // end thrust
109