Created
August 4, 2012 11:42
-
-
Save motonacciu/3256900 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| template <int Begin, int End, int Size, class Enable=void> | |
| struct partition; | |
| template <int BeginIt, int EndIt, int Size> | |
| struct partition<BeginIt, EndIt, Size, typename std::enable_if<(BeginIt<EndIt)>::type> { | |
| template <class T> | |
| inline static void go(T* vec, const T& pivot) { | |
| if (vec[BeginIt] <= pivot) { | |
| return partition<BeginIt+1, EndIt, Size>::go(vec, pivot); | |
| } | |
| if (vec[EndIt-1] > pivot) { | |
| return partition<BeginIt, EndIt-1, Size>::go(vec, pivot); | |
| } | |
| std::swap(vec[BeginIt], vec[EndIt-1]); | |
| partition<BeginIt+1, EndIt-1, Size>::go(vec, pivot); | |
| } | |
| }; | |
| template <int BeginIt, int EndIt, int Size> | |
| struct partition<BeginIt, EndIt, Size, typename std::enable_if<(BeginIt>=EndIt)>::type > { | |
| template <class T> | |
| inline static void go(T* vec, const T& pivot) { | |
| constexpr int pivot_idx = BeginIt-1; | |
| std::swap(vec[0], vec[pivot_idx]); | |
| quicksort(vec, int_<pivot_idx>()); | |
| quicksort(vec+pivot_idx+1, int_<Size-(pivot_idx+1)>()); | |
| } | |
| }; | |
| template <int Val> | |
| class int_{ }; | |
| template <class T, int Size> | |
| inline auto quicksort( T* vec, const int_<Size>& ) -> typename std::enable_if<(Size>=1)>::type { | |
| partition<1,Size,Size>::go(vec, vec[0]); | |
| } | |
| template <class T, int Size> | |
| inline auto quicksort( T* vec, const int_<Size>& ) -> typename std::enable_if<(Size<1)>::type { } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment