From e31056e0711646ab957d4fcdc7dd8241f35a4842 Mon Sep 17 00:00:00 2001 From: morzhovets Date: Thu, 5 Oct 2023 00:30:53 +0400 Subject: [PATCH] Fast functors --- include/momo/HashSorter.h | 3 +- include/momo/RadixSorter.h | 71 ++++++++++++++++++++++++-------------- 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/include/momo/HashSorter.h b/include/momo/HashSorter.h index eb05d3c1..6db76cea 100644 --- a/include/momo/HashSorter.h +++ b/include/momo/HashSorter.h @@ -232,7 +232,8 @@ class HashSorter if (count > 2) pvGroup(begin, count, equalFunc, iterSwapper); }; - internal::RadixSorter<>::Sort(begin, count, iterHashFunc, iterSwapper, itemsGrouper); + internal::RadixSorter<>::Sort(begin, count, iterHashFunc, iterSwapper, + FastCopyableFunctor(itemsGrouper)); } template diff --git a/include/momo/RadixSorter.h b/include/momo/RadixSorter.h index c5cadc95..9907dbe2 100644 --- a/include/momo/RadixSorter.h +++ b/include/momo/RadixSorter.h @@ -56,22 +56,23 @@ namespace internal static const size_t selectionSortMaxCount = size_t{1} << (radixSize / 2 + 1); public: - template Iterator, - typename CodeGetter = RadixSorterCodeGetter> - requires std::unsigned_integral> - static void Sort(Iterator begin, size_t count, const CodeGetter& codeGetter = CodeGetter()) + template Iterator> + static void Sort(Iterator begin, size_t count) { - auto iterSwapper = [] (Iterator iter1, Iterator iter2) { std::iter_swap(iter1, iter2); }; + auto iterSwapper = [] (Iterator iter1, Iterator iter2) + { std::iter_swap(iter1, iter2); }; auto itemsGrouper = [] (Iterator, size_t) {}; - Sort(begin, count, codeGetter, iterSwapper, itemsGrouper); + Sort(begin, count, FastCopyableFunctor(RadixSorterCodeGetter()), //? + FastCopyableFunctor(iterSwapper), FastCopyableFunctor(itemsGrouper)); } - template Iterator, typename CodeGetter, + template Iterator, + internal::conceptConstFunctor CodeGetter, internal::conceptConstFunctor IterSwapper, internal::conceptConstFunctor ItemsGrouper> - requires std::unsigned_integral> - static void Sort(Iterator begin, size_t count, const CodeGetter& codeGetter, - const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper) + static void Sort(Iterator begin, size_t count, FastCopyableFunctor codeGetter, + FastCopyableFunctor iterSwapper, + FastCopyableFunctor itemsGrouper) { typedef decltype(codeGetter(begin)) Code; pvSort(begin, count, codeGetter, iterSwapper, itemsGrouper, @@ -79,10 +80,15 @@ namespace internal } private: - template - static void pvSort(Iterator begin, size_t count, const CodeGetter& codeGetter, - const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper, size_t shift) + template Iterator, + internal::conceptConstFunctor CodeGetter, + internal::conceptConstFunctor IterSwapper, + internal::conceptConstFunctor ItemsGrouper> + static void pvSort(Iterator begin, size_t count, + FastCopyableFunctor codeGetter, + FastCopyableFunctor iterSwapper, + FastCopyableFunctor itemsGrouper, size_t shift) { if (count < 2) return; @@ -98,10 +104,15 @@ namespace internal pvRadixSort(begin, count, codeGetter, iterSwapper, itemsGrouper, shift); } - template - static void pvSelectionSort(Iterator begin, size_t count, const CodeGetter& codeGetter, - const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper) + template Iterator, + internal::conceptConstFunctor CodeGetter, + internal::conceptConstFunctor IterSwapper, + internal::conceptConstFunctor ItemsGrouper> + static void pvSelectionSort(Iterator begin, size_t count, + FastCopyableFunctor codeGetter, + FastCopyableFunctor iterSwapper, + FastCopyableFunctor itemsGrouper) { MOMO_ASSERT(count > 0); std::array codes; //? @@ -129,10 +140,15 @@ namespace internal itemsGrouper(UIntMath<>::Next(begin, prevIndex), count - prevIndex); } - template - static void pvRadixSort(Iterator begin, size_t count, const CodeGetter& codeGetter, - const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper, size_t shift) + template Iterator, + internal::conceptConstFunctor CodeGetter, + internal::conceptConstFunctor IterSwapper, + internal::conceptConstFunctor ItemsGrouper> + static void pvRadixSort(Iterator begin, size_t count, + FastCopyableFunctor codeGetter, + FastCopyableFunctor iterSwapper, + FastCopyableFunctor itemsGrouper, size_t shift) { MOMO_ASSERT(count > 0); std::array endIndexes; @@ -182,9 +198,12 @@ namespace internal } } - template - static void pvRadixSort(Iterator begin, const CodeGetter& codeGetter, - const IterSwapper& iterSwapper, size_t shift, + template Iterator, + internal::conceptConstFunctor CodeGetter, + internal::conceptConstFunctor IterSwapper> + static void pvRadixSort(Iterator begin, FastCopyableFunctor codeGetter, + FastCopyableFunctor iterSwapper, size_t shift, const std::array& endIndexes) { std::array beginIndexes; @@ -208,7 +227,7 @@ namespace internal } } - template + template static size_t pvGetRadix(Code code, size_t shift) noexcept { return static_cast(code >> shift) & ((size_t{1} << radixSize) - 1);