Skip to content

Commit 8b10497

Browse files
committed
Make chunked_insertion_sort a function object
1 parent 18fbd31 commit 8b10497

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

source/containers/algorithms/sort/chunked_insertion_sort.cpp

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,35 @@ constexpr auto runtime_sort_exactly_n_relocate(Iterator it, auto const size, aut
6060
}
6161
}
6262

63-
export template<typename Compare = std::less<>>
64-
constexpr auto chunked_insertion_sort(range auto && r, Compare const compare = Compare()) -> void {
65-
constexpr auto chunk_size = 4_bi;
66-
auto it = containers::begin(r);
67-
auto const last = containers::end(r);
68-
#if 1
69-
auto const initial_sort_size = bounded::min(chunk_size, ::containers::size(r));
70-
::containers::small_size_optimized_sort(
71-
subrange(it, initial_sort_size),
72-
compare,
73-
[](auto &&, auto) { std::unreachable(); }
74-
);
75-
it += initial_sort_size;
76-
#endif
77-
auto buffer = containers::uninitialized_array<std::remove_reference_t<decltype(*it)>, chunk_size>();
78-
while (it != last) {
79-
auto const count = bounded::min(chunk_size, last - it);
80-
auto next_it = ::containers::runtime_sort_exactly_n_relocate(it, count, buffer.data(), compare);
81-
merge_relocate_second_range(
82-
subrange(containers::begin(r), it),
83-
subrange(buffer.data(), count),
84-
next_it,
85-
compare
63+
struct chunked_insertion_sort_t {
64+
template<typename Compare = std::less<>>
65+
static constexpr auto operator()(range auto && r, Compare const compare = Compare()) -> void {
66+
constexpr auto chunk_size = 4_bi;
67+
auto it = containers::begin(r);
68+
auto const last = containers::end(r);
69+
#if 1
70+
auto const initial_sort_size = bounded::min(chunk_size, ::containers::size(r));
71+
::containers::small_size_optimized_sort(
72+
subrange(it, initial_sort_size),
73+
compare,
74+
[](auto &&, auto) { std::unreachable(); }
8675
);
87-
it = next_it;
76+
it += initial_sort_size;
77+
#endif
78+
auto buffer = containers::uninitialized_array<std::remove_reference_t<decltype(*it)>, chunk_size>();
79+
while (it != last) {
80+
auto const count = bounded::min(chunk_size, last - it);
81+
auto next_it = ::containers::runtime_sort_exactly_n_relocate(it, count, buffer.data(), compare);
82+
merge_relocate_second_range(
83+
subrange(containers::begin(r), it),
84+
subrange(buffer.data(), count),
85+
next_it,
86+
compare
87+
);
88+
it = next_it;
89+
}
8890
}
8991
};
92+
export constexpr auto chunked_insertion_sort = chunked_insertion_sort_t();
9093

9194
} // namespace containers

0 commit comments

Comments
 (0)