2
* Copyright 2008-2012 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.
19
#include <thrust/detail/config.h>
20
#include <thrust/system/detail/internal/scalar/stable_primitive_sort.h>
21
#include <thrust/system/detail/internal/scalar/stable_radix_sort.h>
22
#include <thrust/functional.h>
23
#include <thrust/system/detail/internal/scalar/partition.h>
24
#include <thrust/iterator/zip_iterator.h>
25
#include <thrust/detail/type_traits.h>
37
namespace stable_primitive_sort_detail
41
template<typename Iterator>
42
struct enable_if_bool_sort
43
: thrust::detail::enable_if<
44
thrust::detail::is_same<
46
typename thrust::iterator_value<Iterator>::type
52
template<typename Iterator>
53
struct disable_if_bool_sort
54
: thrust::detail::disable_if<
55
thrust::detail::is_same<
57
typename thrust::iterator_value<Iterator>::type
64
template<typename RandomAccessIterator>
65
typename enable_if_bool_sort<RandomAccessIterator>::type
66
stable_primitive_sort(RandomAccessIterator first, RandomAccessIterator last)
68
// use stable_partition if we're sorting bool
69
// stable_partition puts true values first, so we need to logical_not
70
scalar::stable_partition(first, last, thrust::logical_not<bool>());
74
template<typename RandomAccessIterator>
75
typename disable_if_bool_sort<RandomAccessIterator>::type
76
stable_primitive_sort(RandomAccessIterator first, RandomAccessIterator last)
78
// call stable_radix_sort
79
scalar::stable_radix_sort(first,last);
83
struct logical_not_first
85
template<typename Tuple>
87
bool operator()(Tuple t)
89
return !thrust::get<0>(t);
94
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
95
typename enable_if_bool_sort<RandomAccessIterator1>::type
96
stable_primitive_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last,
97
RandomAccessIterator2 values_first)
99
// use stable_partition if we're sorting bool
100
// stable_partition puts true values first, so we need to logical_not
101
scalar::stable_partition(thrust::make_zip_iterator(thrust::make_tuple(keys_first, values_first)),
102
thrust::make_zip_iterator(thrust::make_tuple(keys_last, values_first)),
103
logical_not_first());
107
template<typename RandomAccessIterator1, typename RandomAccessIterator2>
108
typename disable_if_bool_sort<RandomAccessIterator1>::type
109
stable_primitive_sort_by_key(RandomAccessIterator1 keys_first, RandomAccessIterator1 keys_last,
110
RandomAccessIterator2 values_first)
112
// call stable_radix_sort_by_key
113
scalar::stable_radix_sort_by_key(keys_first, keys_last, values_first);
119
template<typename RandomAccessIterator>
120
void stable_primitive_sort(RandomAccessIterator first,
121
RandomAccessIterator last)
123
scalar::stable_primitive_sort_detail::stable_primitive_sort(first,last);
126
template<typename RandomAccessIterator1,
127
typename RandomAccessIterator2>
128
void stable_primitive_sort_by_key(RandomAccessIterator1 keys_first,
129
RandomAccessIterator1 keys_last,
130
RandomAccessIterator2 values_first)
132
scalar::stable_primitive_sort_detail::stable_primitive_sort_by_key(keys_first, keys_last, values_first);
135
} // end namespace scalar
136
} // end namespace internal
137
} // end namespace detail
138
} // end namespace system
139
} // end namespace thrust
141
#include <thrust/system/detail/internal/scalar/stable_primitive_sort.inl>