Go to the documentation of this file.
9 #ifndef EAGINE_NETWORK_SORTER_HPP
10 #define EAGINE_NETWORK_SORTER_HPP
25 typename Compare = std::less<T>,
26 typename Network = sorting_network<N>>
27 class basic_network_sorter {
32 auto _min(
const T& a,
const T& b)
const ->
const T& {
33 return _before(a, b) ? a : b;
36 auto _max(
const T& a,
const T& b)
const ->
const T& {
37 return _before(a, b) ? b : a;
40 auto _min_max_cpy(
const T& a,
const T& b,
bool min,
bool max)
const
42 return min ? _min(a, b) : max ? _max(a, b) : a;
46 void single_sort_step(
47 std::array<T, N>& src,
48 std::array<T, N>& dst,
71 typename Compare = std::less<T>,
72 typename Network = sorting_network<N>>
73 class network_sorter : basic_network_sorter<T, N, Compare, Network> {
76 std::array<std::array<T, N>, 2> _a;
79 constexpr network_sorter(std::array<T, N> a)
82 using basic_network_sorter<T, N, Compare, Network>::rounds;
84 auto done() const noexcept ->
bool {
85 return _round >= rounds();
88 auto next_round() noexcept ->
bool {
89 return !done() && (++_round < rounds());
100 return sort_single(_round, i);
103 auto sort_round() ->
auto& {
104 EAGINE_ASSERT(!done());
111 auto sort() ->
auto& {
112 while(sort_round().next_round()) {
117 auto result() const noexcept -> const std::array<T, N>& {
118 return _a[rounds() % 2];
121 auto operator()() ->
const std::array<T, N>& {
122 return sort().result();
126 template <std::
size_t N,
typename Cmp,
typename T,
typename P,
typename S>
127 auto network_sort(memory::basic_span<T, P, S> spn)
128 -> memory::basic_span<T, P, S> {
131 std::array<T, N> init{};
132 copy(spn,
cover(init));
133 network_sorter<T, N, Cmp> sorter(std::move(init));
134 return copy(
view(sorter.sort().result()), spn);
139 #endif // EAGINE_NETWORK_SORTER_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
static constexpr auto span_size(T v) noexcept
Converts argument to span size type.
Definition: types.hpp:59
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
static auto sort(basic_span< T, P, S > spn) -> basic_span< T, P, S >
Sorts the elements of a span.
Definition: span_algo.hpp:609
integer_range(B, E) -> integer_range< std::common_type_t< B, E >>
Deduction guide for integer_range.
static auto copy(const_block source, block dest) -> block
Copies the content of source block to destination block.
Definition: copy.hpp:23