2 * Copyright 2008-2013 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/reverse.h>
21 #include <thrust/detail/type_traits.h>
22 #include <thrust/iterator/iterator_traits.h>
24 #include <thrust/system/detail/internal/scalar/stable_merge_sort.h>
25 #include <thrust/system/detail/internal/scalar/stable_radix_sort.h>
42 template<typename RandomAccessIterator,
43 typename StrictWeakOrdering>
44 void stable_sort(RandomAccessIterator first,
45 RandomAccessIterator last,
46 StrictWeakOrdering comp,
47 thrust::detail::true_type)
49 thrust::system::detail::internal::scalar::stable_radix_sort(first, last);
51 // if comp is greater<T> then reverse the keys
52 typedef typename thrust::iterator_traits<RandomAccessIterator>::value_type KeyType;
53 const static bool reverse = thrust::detail::is_same<StrictWeakOrdering, typename thrust::greater<KeyType> >::value;
56 thrust::reverse(first, last);
59 template<typename RandomAccessIterator1,
60 typename RandomAccessIterator2,
61 typename StrictWeakOrdering>
62 void stable_sort_by_key(RandomAccessIterator1 first1,
63 RandomAccessIterator1 last1,
64 RandomAccessIterator2 first2,
65 StrictWeakOrdering comp,
66 thrust::detail::true_type)
68 // if comp is greater<T> then reverse the keys and values
69 typedef typename thrust::iterator_traits<RandomAccessIterator1>::value_type KeyType;
70 const static bool reverse = thrust::detail::is_same<StrictWeakOrdering, typename thrust::greater<KeyType> >::value;
72 // note, we also have to reverse the (unordered) input to preserve stability
75 thrust::reverse(first1, last1);
76 thrust::reverse(first2, first2 + (last1 - first1));
79 thrust::system::detail::internal::scalar::stable_radix_sort_by_key(first1, last1, first2);
83 thrust::reverse(first1, last1);
84 thrust::reverse(first2, first2 + (last1 - first1));
92 template<typename RandomAccessIterator,
93 typename StrictWeakOrdering>
94 void stable_sort(RandomAccessIterator first,
95 RandomAccessIterator last,
96 StrictWeakOrdering comp,
97 thrust::detail::false_type)
99 thrust::system::detail::internal::scalar::stable_merge_sort(first, last, comp);
102 template<typename RandomAccessIterator1,
103 typename RandomAccessIterator2,
104 typename StrictWeakOrdering>
105 void stable_sort_by_key(RandomAccessIterator1 first1,
106 RandomAccessIterator1 last1,
107 RandomAccessIterator2 first2,
108 StrictWeakOrdering comp,
109 thrust::detail::false_type)
111 thrust::system::detail::internal::scalar::stable_merge_sort_by_key(first1, last1, first2, comp);
114 } // end namespace dispatch
115 } // end namespace detail
116 } // end namespace cpp
117 } // end namespace system
118 } // end namespace thrust