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