OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / detail / allocator / default_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/allocator/allocator_traits.h>
19 #include <thrust/detail/type_traits.h>
20 #include <thrust/detail/type_traits/pointer_traits.h>
21 #include <thrust/for_each.h>
22 #include <thrust/uninitialized_fill.h>
23
24 namespace thrust
25 {
26 namespace detail
27 {
28 namespace allocator_traits_detail
29 {
30
31
32 template<typename Allocator>
33   struct construct1_via_allocator
34 {
35   Allocator &a;
36
37   construct1_via_allocator(Allocator &a)
38     : a(a)
39   {}
40
41   template<typename T>
42   inline __host__ __device__
43   void operator()(T &x)
44   {
45     allocator_traits<Allocator>::construct(a, &x);
46   }
47 };
48
49
50 template<typename Allocator, typename T>
51   struct needs_default_construct_via_allocator
52     : has_member_construct1<
53         Allocator,
54         T
55       >
56 {};
57
58
59 // we know that std::allocator::construct's only effect is to call T's 
60 // default constructor, so we needn't use it for default construction
61 template<typename U, typename T>
62   struct needs_default_construct_via_allocator<std::allocator<U>, T>
63     : thrust::detail::false_type
64 {};
65
66
67 template<typename Allocator, typename Pointer, typename Size>
68   typename enable_if<
69     needs_default_construct_via_allocator<
70       Allocator,
71       typename pointer_element<Pointer>::type
72     >::value
73   >::type
74     default_construct_range(Allocator &a, Pointer p, Size n)
75 {
76   thrust::for_each_n(allocator_system<Allocator>::get(a), p, n, construct1_via_allocator<Allocator>(a));
77 }
78
79
80 template<typename Allocator, typename Pointer, typename Size>
81   typename disable_if<
82     needs_default_construct_via_allocator<
83       Allocator,
84       typename pointer_element<Pointer>::type
85     >::value
86   >::type
87     default_construct_range(Allocator &a, Pointer p, Size n)
88 {
89   thrust::uninitialized_fill_n(allocator_system<Allocator>::get(a), p, n, typename pointer_element<Pointer>::type());
90 }
91
92
93 } // end allocator_traits_detail
94
95
96 template<typename Allocator, typename Pointer, typename Size>
97   void default_construct_range(Allocator &a, Pointer p, Size n)
98 {
99   return allocator_traits_detail::default_construct_range(a,p,n);
100 }
101
102
103 } // end detail
104 } // end thrust
105