| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "yurkin_g_shellbetcher/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <cstdint> | ||
| 6 | #include <random> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include "yurkin_g_shellbetcher/common/include/common.hpp" | ||
| 10 | |||
| 11 | namespace yurkin_g_shellbetcher { | ||
| 12 | namespace { | ||
| 13 | |||
| 14 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | void ShellSort(std::vector<int> &data) { |
| 15 | const std::size_t n = data.size(); | ||
| 16 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (n < 2) { |
| 17 | return; | ||
| 18 | } | ||
| 19 | |||
| 20 | std::size_t gap = 1; | ||
| 21 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 24 times.
|
32 | while (gap < n / 3) { |
| 22 | 8 | gap = (gap * 3) + 1; | |
| 23 | } | ||
| 24 | |||
| 25 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 24 times.
|
56 | while (gap > 0) { |
| 26 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 32 times.
|
152 | for (std::size_t i = gap; i < n; ++i) { |
| 27 | 120 | int tmp = data[i]; | |
| 28 | std::size_t j = i; | ||
| 29 |
4/4✓ Branch 0 taken 176 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 80 times.
✓ Branch 3 taken 96 times.
|
200 | while (j >= gap && data[j - gap] > tmp) { |
| 30 | 80 | data[j] = data[j - gap]; | |
| 31 | j -= gap; | ||
| 32 | } | ||
| 33 | 120 | data[j] = tmp; | |
| 34 | } | ||
| 35 | 32 | gap = (gap - 1) / 3; | |
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | 24 | void OddEvenBatcherMerge(const std::vector<int> &a, const std::vector<int> &b, std::vector<int> &out) { | |
| 40 | 24 | out.resize(a.size() + b.size()); | |
| 41 | |||
| 42 | 24 | std::ranges::merge(a, b, out.begin()); | |
| 43 | |||
| 44 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (int phase = 0; phase < 2; ++phase) { |
| 45 | 48 | auto start = static_cast<std::size_t>(phase); | |
| 46 |
2/2✓ Branch 0 taken 96 times.
✓ Branch 1 taken 48 times.
|
144 | for (std::size_t i = start; i + 1 < out.size(); i += 2) { |
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
|
96 | if (out[i] > out[i + 1]) { |
| 48 | std::swap(out[i], out[i + 1]); | ||
| 49 | } | ||
| 50 | } | ||
| 51 | } | ||
| 52 | 24 | } | |
| 53 | |||
| 54 | } // namespace | ||
| 55 | |||
| 56 | 24 | YurkinGShellBetcherSEQ::YurkinGShellBetcherSEQ(const InType &in) { | |
| 57 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 58 | 24 | GetInput() = in; | |
| 59 | GetOutput() = 0; | ||
| 60 | 24 | } | |
| 61 | |||
| 62 | 24 | bool YurkinGShellBetcherSEQ::ValidationImpl() { | |
| 63 | 24 | return GetInput() > 0; | |
| 64 | } | ||
| 65 | |||
| 66 | 24 | bool YurkinGShellBetcherSEQ::PreProcessingImpl() { | |
| 67 | 24 | return true; | |
| 68 | } | ||
| 69 | |||
| 70 | 24 | bool YurkinGShellBetcherSEQ::RunImpl() { | |
| 71 | 24 | const InType n = GetInput(); | |
| 72 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (n <= 0) { |
| 73 | return false; | ||
| 74 | } | ||
| 75 | |||
| 76 | 24 | std::vector<int> data(static_cast<std::size_t>(n)); | |
| 77 | |||
| 78 | 24 | std::mt19937 gen(static_cast<unsigned int>(n)); | |
| 79 | std::uniform_int_distribution<int> dist(0, 1'000'000); | ||
| 80 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
|
144 | for (int &v : data) { |
| 81 | 120 | v = dist(gen); | |
| 82 | } | ||
| 83 | |||
| 84 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<int> expected = data; |
| 85 | std::ranges::sort(expected); | ||
| 86 | |||
| 87 | 24 | ShellSort(data); | |
| 88 | |||
| 89 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | std::vector<int> merged; |
| 90 |
2/6✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
24 | OddEvenBatcherMerge(data, {}, merged); |
| 91 | data.swap(merged); | ||
| 92 | |||
| 93 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (!std::ranges::is_sorted(data)) { |
| 94 | return false; | ||
| 95 | } | ||
| 96 | |||
| 97 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (data != expected) { |
| 98 | return false; | ||
| 99 | } | ||
| 100 | |||
| 101 | std::int64_t checksum = 0; | ||
| 102 |
2/2✓ Branch 0 taken 120 times.
✓ Branch 1 taken 24 times.
|
144 | for (int v : data) { |
| 103 | 120 | checksum += static_cast<std::int64_t>(v); | |
| 104 | } | ||
| 105 | |||
| 106 | 24 | GetOutput() = static_cast<OutType>(checksum & 0x7FFFFFFF); | |
| 107 | 24 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | 24 | bool YurkinGShellBetcherSEQ::PostProcessingImpl() { | |
| 111 | 24 | return true; | |
| 112 | } | ||
| 113 | |||
| 114 | } // namespace yurkin_g_shellbetcher | ||
| 115 |