move quicksort partitioner to detail namespace

This commit is contained in:
rexy712 2020-05-07 11:57:06 -07:00
parent 31a177fc86
commit ef5cccee73

View File

@ -25,34 +25,36 @@
namespace rexy::cx{
template<class Iter, class Compare>
constexpr Iter qs_partition(Iter left, Iter right, const Compare& cmp)
noexcept(std::is_nothrow_invocable<Compare,decltype(*left),decltype(*right)>::value &&
noexcept(cx::swap(*left,*right)))
{
auto range = right - left;
auto pivot = left + (range / 2);
auto value = *pivot;
namespace detail{
template<class Iter, class Compare>
constexpr Iter qs_partition(Iter left, Iter right, const Compare& cmp)
noexcept(std::is_nothrow_invocable<Compare,decltype(*left),decltype(*right)>::value &&
noexcept(cx::swap(*left,*right)))
{
auto range = right - left;
auto pivot = left + (range / 2);
auto value = *pivot;
//move pivot value all the way to the right side to preserve it
cx::swap(*pivot, *right);
for(auto it = left;it != right;++it){
if(cmp(*it, value)){
cx::swap(*left, *it);
++left;
//move pivot value all the way to the right side to preserve it
cx::swap(*pivot, *right);
for(auto it = left;it != right;++it){
if(cmp(*it, value)){
cx::swap(*left, *it);
++left;
}
}
//move pivot value back to proper position
cx::swap(*left, *right);
return left;
}
//move pivot value back to proper position
cx::swap(*left, *right);
return left;
}
template<class Iter, class Compare>
constexpr void quicksort(Iter left, Iter right, const Compare& cmp)
noexcept(noexcept(cx::qs_partition(left, right, cmp)))
noexcept(noexcept(cx::detail::qs_partition(left, right, cmp)))
{
while(left < right){
auto real_right = right-1;
auto pivot = qs_partition(left, real_right, cmp);
auto pivot = detail::qs_partition(left, real_right, cmp);
quicksort(left, pivot, cmp);
left = ++pivot;
}