| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sosnina_a_radix_simple_merge/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "sosnina_a_radix_simple_merge/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace sosnina_a_radix_simple_merge { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | constexpr int kRadixBits = 8; | ||
| 15 | constexpr int kRadixSize = 1 << kRadixBits; | ||
| 16 | constexpr int kNumPasses = sizeof(int) / sizeof(uint8_t); | ||
| 17 | constexpr uint32_t kSignFlip = 0x80000000U; | ||
| 18 | |||
| 19 | 732 | void RadixSortLSD(std::vector<int> &data, std::vector<int> &buffer) { | |
| 20 | size_t idx = 0; | ||
| 21 |
2/2✓ Branch 0 taken 2618 times.
✓ Branch 1 taken 732 times.
|
3350 | for (int elem : data) { |
| 22 | 2618 | buffer[idx++] = static_cast<int>(static_cast<uint32_t>(elem) ^ kSignFlip); | |
| 23 | } | ||
| 24 | std::swap(data, buffer); | ||
| 25 | |||
| 26 |
2/2✓ Branch 0 taken 2928 times.
✓ Branch 1 taken 732 times.
|
3660 | for (int pass = 0; pass < kNumPasses; ++pass) { |
| 27 | 2928 | std::vector<int> count(kRadixSize + 1, 0); | |
| 28 | |||
| 29 |
2/2✓ Branch 0 taken 10472 times.
✓ Branch 1 taken 2928 times.
|
13400 | for (auto elem : data) { |
| 30 | 10472 | auto digit = static_cast<uint8_t>((static_cast<uint32_t>(elem) >> (pass * kRadixBits)) & 0xFF); | |
| 31 | 10472 | ++count[digit + 1]; | |
| 32 | } | ||
| 33 | |||
| 34 |
2/2✓ Branch 0 taken 749568 times.
✓ Branch 1 taken 2928 times.
|
752496 | for (int i = 1; i <= kRadixSize; ++i) { |
| 35 | 749568 | count[i] += count[i - 1]; | |
| 36 | } | ||
| 37 | |||
| 38 |
2/2✓ Branch 0 taken 10472 times.
✓ Branch 1 taken 2928 times.
|
13400 | for (auto elem : data) { |
| 39 | 10472 | auto digit = static_cast<uint8_t>((static_cast<uint32_t>(elem) >> (pass * kRadixBits)) & 0xFF); | |
| 40 | 10472 | buffer[count[digit]++] = elem; | |
| 41 | } | ||
| 42 | |||
| 43 | std::swap(data, buffer); | ||
| 44 | } | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 2618 times.
✓ Branch 1 taken 732 times.
|
3350 | for (int &elem : data) { |
| 47 | 2618 | elem = static_cast<int>(static_cast<uint32_t>(elem) ^ kSignFlip); | |
| 48 | } | ||
| 49 | 732 | } | |
| 50 | |||
| 51 | 366 | void SimpleMerge(const std::vector<int> &left, const std::vector<int> &right, std::vector<int> &result) { | |
| 52 | size_t i = 0; | ||
| 53 | size_t j = 0; | ||
| 54 | size_t k = 0; | ||
| 55 | |||
| 56 |
4/4✓ Branch 0 taken 1716 times.
✓ Branch 1 taken 238 times.
✓ Branch 2 taken 128 times.
✓ Branch 3 taken 1588 times.
|
1954 | while (i < left.size() && j < right.size()) { |
| 57 |
2/2✓ Branch 0 taken 868 times.
✓ Branch 1 taken 720 times.
|
1588 | if (left[i] <= right[j]) { |
| 58 | 868 | result[k++] = left[i++]; | |
| 59 | } else { | ||
| 60 | 720 | result[k++] = right[j++]; | |
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 |
2/2✓ Branch 0 taken 352 times.
✓ Branch 1 taken 366 times.
|
718 | while (i < left.size()) { |
| 65 | 352 | result[k++] = left[i++]; | |
| 66 | } | ||
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 678 times.
✓ Branch 1 taken 366 times.
|
1044 | while (j < right.size()) { |
| 69 | 678 | result[k++] = right[j++]; | |
| 70 | } | ||
| 71 | 366 | } | |
| 72 | |||
| 73 | } // namespace | ||
| 74 | |||
| 75 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | SosninaATestTaskSEQ::SosninaATestTaskSEQ(const InType &in) { |
| 76 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 77 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | GetInput() = in; |
| 78 |
1/2✓ Branch 1 taken 390 times.
✗ Branch 2 not taken.
|
390 | GetOutput() = in; |
| 79 | 390 | } | |
| 80 | |||
| 81 | 390 | bool SosninaATestTaskSEQ::ValidationImpl() { | |
| 82 | 390 | return !GetInput().empty(); | |
| 83 | } | ||
| 84 | |||
| 85 | 390 | bool SosninaATestTaskSEQ::PreProcessingImpl() { | |
| 86 | 390 | GetOutput() = GetInput(); | |
| 87 | 390 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 |
2/2✓ Branch 0 taken 366 times.
✓ Branch 1 taken 24 times.
|
390 | bool SosninaATestTaskSEQ::RunImpl() { |
| 91 | std::vector<int> &data = GetOutput(); | ||
| 92 |
2/2✓ Branch 0 taken 366 times.
✓ Branch 1 taken 24 times.
|
390 | if (data.size() <= 1) { |
| 93 | return true; | ||
| 94 | } | ||
| 95 | |||
| 96 | 366 | size_t mid = data.size() / 2; | |
| 97 |
1/2✓ Branch 2 taken 366 times.
✗ Branch 3 not taken.
|
366 | std::vector<int> left_part(data.begin(), data.begin() + static_cast<std::ptrdiff_t>(mid)); |
| 98 |
2/6✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 366 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
366 | std::vector<int> right_part(data.begin() + static_cast<std::ptrdiff_t>(mid), data.end()); |
| 99 |
2/6✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 366 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
366 | std::vector<int> left_buffer(left_part.size()); |
| 100 |
1/4✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
366 | std::vector<int> right_buffer(right_part.size()); |
| 101 | |||
| 102 |
1/2✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
|
366 | RadixSortLSD(left_part, left_buffer); |
| 103 |
1/2✓ Branch 1 taken 366 times.
✗ Branch 2 not taken.
|
366 | RadixSortLSD(right_part, right_buffer); |
| 104 | |||
| 105 | 366 | SimpleMerge(left_part, right_part, data); | |
| 106 | |||
| 107 | return std::ranges::is_sorted(data); | ||
| 108 | } | ||
| 109 | |||
| 110 | 390 | bool SosninaATestTaskSEQ::PostProcessingImpl() { | |
| 111 | 390 | return !GetOutput().empty(); | |
| 112 | } | ||
| 113 | |||
| 114 | } // namespace sosnina_a_radix_simple_merge | ||
| 115 |