| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "rastvorov_k_radix_sort_double_merge/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <array> | ||
| 4 | #include <bit> | ||
| 5 | #include <cmath> | ||
| 6 | #include <cstddef> | ||
| 7 | #include <cstdint> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "rastvorov_k_radix_sort_double_merge/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace rastvorov_k_radix_sort_double_merge { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | inline uint64_t DoubleToOrderedKey(double x) { | ||
| 17 | 256 | if (std::isnan(x)) { | |
| 18 | return UINT64_MAX; | ||
| 19 | } | ||
| 20 | |||
| 21 | const auto bits = std::bit_cast<uint64_t>(x); | ||
| 22 | |||
| 23 | 256 | const uint64_t sign = bits >> 63U; | |
| 24 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 208 times.
|
256 | if (sign != 0U) { |
| 25 | 48 | return ~bits; | |
| 26 | } | ||
| 27 | 208 | return bits ^ (1ULL << 63U); | |
| 28 | } | ||
| 29 | |||
| 30 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 48 times.
|
64 | void RadixSortDouble(std::vector<double> *vec) { |
| 31 | auto &a = *vec; | ||
| 32 | const std::size_t n = a.size(); | ||
| 33 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 48 times.
|
64 | if (n <= 1) { |
| 34 | 16 | return; | |
| 35 | } | ||
| 36 | |||
| 37 | 48 | std::vector<double> out(n); | |
| 38 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<uint64_t> keys(n); |
| 39 |
1/4✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
48 | std::vector<uint64_t> out_keys(n); |
| 40 | |||
| 41 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 48 times.
|
304 | for (std::size_t i = 0; i < n; ++i) { |
| 42 |
1/2✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
|
512 | keys[i] = DoubleToOrderedKey(a[i]); |
| 43 | } | ||
| 44 | |||
| 45 |
2/2✓ Branch 0 taken 384 times.
✓ Branch 1 taken 48 times.
|
432 | for (std::size_t pass = 0; pass < 8; ++pass) { |
| 46 | 384 | std::array<std::size_t, 256> count{}; | |
| 47 | 384 | const std::size_t shift = pass * 8; | |
| 48 | |||
| 49 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 384 times.
|
2432 | for (std::size_t i = 0; i < n; ++i) { |
| 50 | 2048 | const auto byte = static_cast<unsigned>((keys[i] >> shift) & 0xFFULL); | |
| 51 | 2048 | count.at(static_cast<std::size_t>(byte))++; | |
| 52 | } | ||
| 53 | |||
| 54 | 384 | std::array<std::size_t, 256> pos{}; | |
| 55 | pos.at(0) = 0; | ||
| 56 |
2/2✓ Branch 0 taken 97920 times.
✓ Branch 1 taken 384 times.
|
98304 | for (std::size_t byte_idx = 1; byte_idx < pos.size(); ++byte_idx) { |
| 57 | 97920 | pos.at(byte_idx) = pos.at(byte_idx - 1) + count.at(byte_idx - 1); | |
| 58 | } | ||
| 59 | |||
| 60 |
2/2✓ Branch 0 taken 2048 times.
✓ Branch 1 taken 384 times.
|
2432 | for (std::size_t i = 0; i < n; ++i) { |
| 61 | 2048 | const auto byte = static_cast<unsigned>((keys[i] >> shift) & 0xFFULL); | |
| 62 | 2048 | const std::size_t p = pos.at(static_cast<std::size_t>(byte))++; | |
| 63 | 2048 | out[p] = a[i]; | |
| 64 | 2048 | out_keys[p] = keys[i]; | |
| 65 | } | ||
| 66 | |||
| 67 | a.swap(out); | ||
| 68 | keys.swap(out_keys); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | } // namespace | ||
| 73 | |||
| 74 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | RastvorovKRadixSortDoubleMergeSEQ::RastvorovKRadixSortDoubleMergeSEQ(const InType &in) { |
| 75 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 76 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | GetInput() = in; |
| 77 | GetOutput() = {}; | ||
| 78 | 64 | } | |
| 79 | |||
| 80 | 64 | bool RastvorovKRadixSortDoubleMergeSEQ::ValidationImpl() { | |
| 81 | 64 | return true; | |
| 82 | } | ||
| 83 | |||
| 84 | 64 | bool RastvorovKRadixSortDoubleMergeSEQ::PreProcessingImpl() { | |
| 85 | 64 | data_ = GetInput(); | |
| 86 | GetOutput().clear(); | ||
| 87 | 64 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | 64 | bool RastvorovKRadixSortDoubleMergeSEQ::RunImpl() { | |
| 91 | 64 | RadixSortDouble(&data_); | |
| 92 | 64 | return true; | |
| 93 | } | ||
| 94 | |||
| 95 | 64 | bool RastvorovKRadixSortDoubleMergeSEQ::PostProcessingImpl() { | |
| 96 | 64 | GetOutput() = data_; | |
| 97 | 64 | return true; | |
| 98 | } | ||
| 99 | |||
| 100 | } // namespace rastvorov_k_radix_sort_double_merge | ||
| 101 |