From 68c7af5ff15801d8560bcc889da3ce7898cde231 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Tue, 8 Sep 2015 10:51:29 +0200 Subject: [PATCH] [BL] Quicksort: Move implementation of array version to the top of the file. Reason for this is the array version have to be visible into the vector version. --- BaseLib/quicksort.h | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/BaseLib/quicksort.h b/BaseLib/quicksort.h index 57a3aaa1b04..a2dc0da963c 100644 --- a/BaseLib/quicksort.h +++ b/BaseLib/quicksort.h @@ -21,29 +21,23 @@ namespace BaseLib { template <typename T1, typename T2 = std::size_t> -void quicksort(std::vector<T1>& array, std::size_t beg, std::size_t end, std::vector<T2>& perm) -{ - quicksort(array.data(), beg, end, perm.data()); -} - -template <typename T1, typename T2 = std::size_t> -void quicksort(std::vector<T1*>& array, std::size_t beg, std::size_t end, std::vector<T2>& perm) +void quicksort(T1* array, std::size_t beg, std::size_t end, T2* perm) { // Zip input arrays. - std::vector<std::pair<T1*, T2>> data; + std::vector<std::pair<T1, T2>> data; data.reserve(end-beg); - std::transform(array.begin()+beg, array.begin()+(end-beg), perm.begin()+beg, + std::transform(array+beg, array+(end-beg), perm+beg, std::back_inserter(data), - [](T1* const& t1, T2 const& t2) + [](T1 const& t1, T2 const& t2) { return std::make_pair(t1, t2); }); // Sort data using first element of the pair. std::sort(data.begin(), data.end(), - [](std::pair<T1*, T2> const& a, std::pair<T1*, T2> const& b) + [](std::pair<T1, T2> const& a, std::pair<T1, T2> const& b) { - return (*a.first < *b.first); + return (a.first < b.first); }); // Unzip sorted data. @@ -55,23 +49,29 @@ void quicksort(std::vector<T1*>& array, std::size_t beg, std::size_t end, std::v } template <typename T1, typename T2 = std::size_t> -void quicksort(T1* array, std::size_t beg, std::size_t end, T2* perm) +void quicksort(std::vector<T1>& array, std::size_t beg, std::size_t end, std::vector<T2>& perm) +{ + quicksort(array.data(), beg, end, perm.data()); +} + +template <typename T1, typename T2 = std::size_t> +void quicksort(std::vector<T1*>& array, std::size_t beg, std::size_t end, std::vector<T2>& perm) { // Zip input arrays. - std::vector<std::pair<T1, T2>> data; + std::vector<std::pair<T1*, T2>> data; data.reserve(end-beg); - std::transform(array+beg, array+end, perm+beg, + std::transform(array.begin()+beg, array.begin()+(end-beg), perm.begin()+beg, std::back_inserter(data), - [](T1 const& t1, T2 const& t2) + [](T1* const& t1, T2 const& t2) { return std::make_pair(t1, t2); }); // Sort data using first element of the pair. std::sort(data.begin(), data.end(), - [](std::pair<T1, T2> const& a, std::pair<T1, T2> const& b) + [](std::pair<T1*, T2> const& a, std::pair<T1*, T2> const& b) { - return (a.first < b.first); + return (*a.first < *b.first); }); // Unzip sorted data. -- GitLab