32 #ifndef _GLIBCXX_PARALLEL_QUICKSORT_H
33 #define _GLIBCXX_PARALLEL_QUICKSORT_H 1
38 namespace __gnu_parallel
49 template<
typename _RAIter,
typename _Compare>
50 typename std::iterator_traits<_RAIter>::difference_type
52 _Compare __comp,
typename std::iterator_traits
53 <_RAIter>::difference_type __pivot_rank,
54 typename std::iterator_traits
55 <_RAIter>::difference_type
58 typedef std::iterator_traits<_RAIter> _TraitsType;
59 typedef typename _TraitsType::value_type _ValueType;
60 typedef typename _TraitsType::difference_type _DifferenceType;
62 _DifferenceType __n = __end - __begin;
63 __num_samples =
std::min(__num_samples, __n);
66 _ValueType* __samples =
static_cast<_ValueType*
>
67 (::operator
new(__num_samples *
sizeof(_ValueType)));
69 for (_DifferenceType __s = 0; __s < __num_samples; ++__s)
71 const unsigned long long __index =
static_cast<unsigned long long>
72 (__s) * __n / __num_samples;
73 ::new(&(__samples[__s])) _ValueType(__begin[__index]);
76 __gnu_sequential::sort(__samples, __samples + __num_samples, __comp);
78 _ValueType& __pivot = __samples[__pivot_rank * __num_samples / __n];
81 __pred(__comp, __pivot);
83 __pred, __num_threads);
85 for (_DifferenceType __s = 0; __s < __num_samples; ++__s)
86 __samples[__s].~_ValueType();
87 ::operator
delete(__samples);
99 template<
typename _RAIter,
typename _Compare>
105 typedef std::iterator_traits<_RAIter> _TraitsType;
106 typedef typename _TraitsType::value_type _ValueType;
107 typedef typename _TraitsType::difference_type _DifferenceType;
109 if (__num_threads <= 1)
111 __gnu_sequential::sort(__begin, __end, __comp);
115 _DifferenceType __n = __end - __begin, __pivot_rank;
122 if ((__num_threads % 2) == 1)
123 __num_threads_left = __num_threads / 2 + 1;
125 __num_threads_left = __num_threads / 2;
127 __pivot_rank = __n * __num_threads_left / __num_threads;
130 (__begin, __end, __comp, __pivot_rank,
133 #pragma omp parallel sections num_threads(2)
137 __comp, __num_threads_left);
140 __comp, __num_threads - __num_threads_left);
152 template<
typename _RAIter,
typename _Compare>
160 typedef std::iterator_traits<_RAIter> _TraitsType;
161 typedef typename _TraitsType::value_type _ValueType;
162 typedef typename _TraitsType::difference_type _DifferenceType;
164 _DifferenceType __n = __end - __begin;
167 if (__num_threads > __n)
171 __begin, __begin + __n, __comp, __num_threads);