GCC Code Coverage Report


Directory: ./
File: tasks/shekhirev_v_hoare_batcher_sort/seq/src/ops_seq.cpp
Date: 2026-06-04 20:25:32
Exec Total Coverage
Lines: 64 65 98.5%
Functions: 8 8 100.0%
Branches: 49 66 74.2%

Line Branch Exec Source
1 #include "shekhirev_v_hoare_batcher_sort/seq/include/ops_seq.hpp"
2
3 #include <algorithm>
4 #include <climits>
5 #include <cstddef>
6 #include <tuple>
7 #include <utility>
8 #include <vector>
9
10 #include "shekhirev_v_hoare_batcher_sort/common/include/common.hpp"
11
12 namespace shekhirev_v_hoare_batcher_sort {
13
14 namespace {
15
16 9312 int HoarePartition(std::vector<int> &arr, int left, int right) {
17 9312 int pivot = arr[left + ((right - left) / 2)];
18 int i = left;
19 int j = right;
20
21
2/2
✓ Branch 0 taken 22304 times.
✓ Branch 1 taken 9312 times.
40928 while (i <= j) {
22
2/2
✓ Branch 0 taken 21944 times.
✓ Branch 1 taken 22304 times.
44248 while (arr[i] < pivot) {
23 21944 i++;
24 }
25
2/2
✓ Branch 0 taken 28136 times.
✓ Branch 1 taken 22304 times.
50440 while (arr[j] > pivot) {
26 28136 j--;
27 }
28
2/2
✓ Branch 0 taken 2440 times.
✓ Branch 1 taken 19864 times.
22304 if (i <= j) {
29 std::swap(arr[i], arr[j]);
30 19864 i++;
31 19864 j--;
32 }
33 }
34 9312 return i;
35 }
36
37 void BatcherComp(std::vector<int> &arr, int left, int right, int r, int m) {
38
2/2
✓ Branch 0 taken 29392 times.
✓ Branch 1 taken 4656 times.
34048 for (int i = left + r; i + r <= right; i += m) {
39
2/2
✓ Branch 0 taken 15888 times.
✓ Branch 1 taken 13504 times.
29392 if (arr[i] > arr[i + r]) {
40 std::swap(arr[i], arr[i + r]);
41 }
42 }
43 }
44
45 } // namespace
46
47
1/2
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
64 ShekhirevHoareBatcherSortSEQ::ShekhirevHoareBatcherSortSEQ(const InType &in) {
48 SetTypeOfTask(GetStaticTypeOfTask());
49
1/2
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
64 GetInput() = in;
50 GetOutput().clear();
51 64 }
52
53 64 bool ShekhirevHoareBatcherSortSEQ::ValidationImpl() {
54 64 return true;
55 }
56
57
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 56 times.
64 bool ShekhirevHoareBatcherSortSEQ::PreProcessingImpl() {
58 const auto &in = GetInput();
59
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 56 times.
64 if (in.empty()) {
60 GetOutput().clear();
61 8 return true;
62 }
63
64 size_t original_size = in.size();
65 size_t p2 = 1;
66
2/2
✓ Branch 0 taken 312 times.
✓ Branch 1 taken 56 times.
368 while (p2 < original_size) {
67 312 p2 *= 2;
68 }
69
70 56 GetOutput() = in;
71 56 GetOutput().resize(p2, INT_MAX);
72 56 return true;
73 }
74
75 96 void ShekhirevHoareBatcherSortSEQ::HoareSort(std::vector<int> &arr, int start_left, int start_right) {
76 96 std::vector<std::pair<int, int>> stk;
77
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 stk.reserve(32);
78
1/2
✓ Branch 1 taken 96 times.
✗ Branch 2 not taken.
96 stk.emplace_back(start_left, start_right);
79
80
2/2
✓ Branch 0 taken 9312 times.
✓ Branch 1 taken 96 times.
9408 while (!stk.empty()) {
81 auto [left, right] = stk.back();
82 stk.pop_back();
83
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9312 times.
9312 if (left >= right) {
85 continue;
86 }
87
88 9312 int partition_index = HoarePartition(arr, left, right);
89
2/2
✓ Branch 0 taken 4520 times.
✓ Branch 1 taken 4792 times.
9312 if (partition_index < right) {
90
1/2
✓ Branch 1 taken 4520 times.
✗ Branch 2 not taken.
4520 stk.emplace_back(partition_index, right);
91 }
92
2/2
✓ Branch 0 taken 4696 times.
✓ Branch 1 taken 4616 times.
9312 if (left < partition_index - 1) {
93
1/4
✓ Branch 1 taken 4696 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4696 stk.emplace_back(left, partition_index - 1);
94 }
95 }
96 96 }
97
98 48 void ShekhirevHoareBatcherSortSEQ::BatcherMerge(std::vector<int> &arr, int start_left, int start_right, int start_r) {
99 48 std::vector<std::tuple<int, int, int, int>> stk;
100
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 stk.reserve(32);
101
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 stk.emplace_back(start_left, start_right, start_r, 0);
102
103
2/2
✓ Branch 0 taken 14016 times.
✓ Branch 1 taken 48 times.
14064 while (!stk.empty()) {
104 auto [left, right, r, stage] = stk.back();
105 stk.pop_back();
106
107 14016 int n = right - left + 1;
108 14016 int m = r * 2;
109
110
2/2
✓ Branch 0 taken 9312 times.
✓ Branch 1 taken 4704 times.
14016 if (m < n) {
111
2/2
✓ Branch 0 taken 4656 times.
✓ Branch 1 taken 4656 times.
9312 if (stage == 0) {
112
1/2
✓ Branch 1 taken 4656 times.
✗ Branch 2 not taken.
4656 stk.emplace_back(left, right, r, 1);
113
1/2
✓ Branch 1 taken 4656 times.
✗ Branch 2 not taken.
4656 stk.emplace_back(left + r, right, m, 0);
114
1/4
✓ Branch 1 taken 4656 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
4656 stk.emplace_back(left, right, m, 0);
115 } else {
116 BatcherComp(arr, left, right, r, m);
117 }
118 } else {
119
1/2
✓ Branch 0 taken 4704 times.
✗ Branch 1 not taken.
4704 if ((left + r) <= right) {
120
2/2
✓ Branch 0 taken 1968 times.
✓ Branch 1 taken 2736 times.
4704 if (arr[left] > arr[left + r]) {
121 std::swap(arr[left], arr[left + r]);
122 }
123 }
124 }
125 }
126 48 }
127
128
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 bool ShekhirevHoareBatcherSortSEQ::RunImpl() {
129 auto &data = GetOutput();
130
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 if (data.size() <= 1) {
131 return true;
132 }
133
134 48 int n = static_cast<int>(data.size());
135 48 int mid = n / 2;
136
137 48 HoareSort(data, 0, mid - 1);
138 48 HoareSort(data, mid, n - 1);
139 48 BatcherMerge(data, 0, n - 1, 1);
140
141 48 return true;
142 }
143
144 64 bool ShekhirevHoareBatcherSortSEQ::PostProcessingImpl() {
145 size_t original_size = GetInput().size();
146 64 GetOutput().resize(original_size);
147 64 return true;
148 }
149
150 } // namespace shekhirev_v_hoare_batcher_sort
151