Skip to content

Commit

Permalink
Fast functors
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Oct 4, 2023
1 parent 3954d50 commit e31056e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 27 deletions.
3 changes: 2 additions & 1 deletion include/momo/HashSorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Iterator, typename EqualFunc, typename IterSwapper>
Expand Down
71 changes: 45 additions & 26 deletions include/momo/RadixSorter.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,33 +56,39 @@ namespace internal
static const size_t selectionSortMaxCount = size_t{1} << (radixSize / 2 + 1);

public:
template<conceptIterator<std::random_access_iterator_tag> Iterator,
typename CodeGetter = RadixSorterCodeGetter<Iterator>>
requires std::unsigned_integral<std::invoke_result_t<const CodeGetter&, Iterator>>
static void Sort(Iterator begin, size_t count, const CodeGetter& codeGetter = CodeGetter())
template<conceptIterator<std::random_access_iterator_tag> 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<Iterator>()), //?
FastCopyableFunctor(iterSwapper), FastCopyableFunctor(itemsGrouper));
}

template<conceptIterator<std::random_access_iterator_tag> Iterator, typename CodeGetter,
template<conceptIterator<std::random_access_iterator_tag> Iterator,
internal::conceptConstFunctor<size_t, Iterator> CodeGetter,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper,
internal::conceptConstFunctor<void, Iterator, size_t> ItemsGrouper>
requires std::unsigned_integral<std::invoke_result_t<const CodeGetter&, Iterator>>
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> codeGetter,
FastCopyableFunctor<IterSwapper> iterSwapper,
FastCopyableFunctor<ItemsGrouper> itemsGrouper)
{
typedef decltype(codeGetter(begin)) Code;
pvSort<Code>(begin, count, codeGetter, iterSwapper, itemsGrouper,
8 * sizeof(Code) - radixSize);
}

private:
template<typename Code, typename Iterator, typename CodeGetter,
typename IterSwapper, typename ItemsGrouper>
static void pvSort(Iterator begin, size_t count, const CodeGetter& codeGetter,
const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper, size_t shift)
template<std::unsigned_integral Code,
conceptIterator<std::random_access_iterator_tag> Iterator,
internal::conceptConstFunctor<size_t, Iterator> CodeGetter,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper,
internal::conceptConstFunctor<void, Iterator, size_t> ItemsGrouper>
static void pvSort(Iterator begin, size_t count,
FastCopyableFunctor<CodeGetter> codeGetter,
FastCopyableFunctor<IterSwapper> iterSwapper,
FastCopyableFunctor<ItemsGrouper> itemsGrouper, size_t shift)
{
if (count < 2)
return;
Expand All @@ -98,10 +104,15 @@ namespace internal
pvRadixSort<Code>(begin, count, codeGetter, iterSwapper, itemsGrouper, shift);
}

template<typename Code, typename Iterator, typename CodeGetter,
typename IterSwapper, typename ItemsGrouper>
static void pvSelectionSort(Iterator begin, size_t count, const CodeGetter& codeGetter,
const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper)
template<std::unsigned_integral Code,
conceptIterator<std::random_access_iterator_tag> Iterator,
internal::conceptConstFunctor<size_t, Iterator> CodeGetter,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper,
internal::conceptConstFunctor<void, Iterator, size_t> ItemsGrouper>
static void pvSelectionSort(Iterator begin, size_t count,
FastCopyableFunctor<CodeGetter> codeGetter,
FastCopyableFunctor<IterSwapper> iterSwapper,
FastCopyableFunctor<ItemsGrouper> itemsGrouper)
{
MOMO_ASSERT(count > 0);
std::array<Code, selectionSortMaxCount> codes; //?
Expand Down Expand Up @@ -129,10 +140,15 @@ namespace internal
itemsGrouper(UIntMath<>::Next(begin, prevIndex), count - prevIndex);
}

template<typename Code, typename Iterator, typename CodeGetter,
typename IterSwapper, typename ItemsGrouper>
static void pvRadixSort(Iterator begin, size_t count, const CodeGetter& codeGetter,
const IterSwapper& iterSwapper, const ItemsGrouper& itemsGrouper, size_t shift)
template<std::unsigned_integral Code,
conceptIterator<std::random_access_iterator_tag> Iterator,
internal::conceptConstFunctor<size_t, Iterator> CodeGetter,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper,
internal::conceptConstFunctor<void, Iterator, size_t> ItemsGrouper>
static void pvRadixSort(Iterator begin, size_t count,
FastCopyableFunctor<CodeGetter> codeGetter,
FastCopyableFunctor<IterSwapper> iterSwapper,
FastCopyableFunctor<ItemsGrouper> itemsGrouper, size_t shift)
{
MOMO_ASSERT(count > 0);
std::array<size_t, radixCount> endIndexes;
Expand Down Expand Up @@ -182,9 +198,12 @@ namespace internal
}
}

template<typename Code, typename Iterator, typename CodeGetter, typename IterSwapper>
static void pvRadixSort(Iterator begin, const CodeGetter& codeGetter,
const IterSwapper& iterSwapper, size_t shift,
template<std::unsigned_integral Code,
conceptIterator<std::random_access_iterator_tag> Iterator,
internal::conceptConstFunctor<size_t, Iterator> CodeGetter,
internal::conceptConstFunctor<void, Iterator, Iterator> IterSwapper>
static void pvRadixSort(Iterator begin, FastCopyableFunctor<CodeGetter> codeGetter,
FastCopyableFunctor<IterSwapper> iterSwapper, size_t shift,
const std::array<size_t, radixCount>& endIndexes)
{
std::array<size_t, radixCount> beginIndexes;
Expand All @@ -208,7 +227,7 @@ namespace internal
}
}

template<typename Code>
template<std::unsigned_integral Code>
static size_t pvGetRadix(Code code, size_t shift) noexcept
{
return static_cast<size_t>(code >> shift) & ((size_t{1} << radixSize) - 1);
Expand Down

0 comments on commit e31056e

Please sign in to comment.