| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "morozova_s_connected_components/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <array> | ||
| 5 | #include <cstddef> | ||
| 6 | #include <queue> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "morozova_s_connected_components/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace morozova_s_connected_components { | ||
| 13 | |||
| 14 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | MorozovaSConnectedComponentsSEQ::MorozovaSConnectedComponentsSEQ(const InType &in) : BaseTask() { |
| 15 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 16 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GetInput() = in; |
| 17 | 32 | } | |
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | constexpr std::array<std::pair<int, int>, 8> kShifts = { | ||
| 22 | {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}}}; | ||
| 23 | } // namespace | ||
| 24 | |||
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | bool MorozovaSConnectedComponentsSEQ::ValidationImpl() { |
| 26 | const auto &input = GetInput(); | ||
| 27 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (input.empty()) { |
| 28 | return true; | ||
| 29 | } | ||
| 30 | const size_t cols = input.front().size(); | ||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (cols == 0) { |
| 32 | return false; | ||
| 33 | } | ||
| 34 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 288 times.
|
320 | for (const auto &row : input) { |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 288 times.
|
288 | if (row.size() != cols) { |
| 36 | return false; | ||
| 37 | } | ||
| 38 |
2/2✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 288 times.
|
3040 | for (int val : row) { |
| 39 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2752 times.
|
2752 | if (val != 0 && val != 1) { |
| 40 | return false; | ||
| 41 | } | ||
| 42 | } | ||
| 43 | } | ||
| 44 | return true; | ||
| 45 | } | ||
| 46 | |||
| 47 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | bool MorozovaSConnectedComponentsSEQ::PreProcessingImpl() { |
| 48 | const auto &input = GetInput(); | ||
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | if (input.empty()) { |
| 50 | GetOutput().clear(); | ||
| 51 | ✗ | return true; | |
| 52 | } | ||
| 53 | 32 | rows_ = static_cast<int>(input.size()); | |
| 54 | 32 | cols_ = static_cast<int>(input.front().size()); | |
| 55 | auto &output = GetOutput(); | ||
| 56 | 32 | output.resize(rows_); | |
| 57 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 32 times.
|
320 | for (int i = 0; i < rows_; ++i) { |
| 58 | 288 | output[i].resize(cols_, 0); | |
| 59 | } | ||
| 60 | return true; | ||
| 61 | } | ||
| 62 | |||
| 63 | 312 | void MorozovaSConnectedComponentsSEQ::ProcessComponent(int start_i, int start_j, int current_label) { | |
| 64 | const auto &input = GetInput(); | ||
| 65 | auto &output = GetOutput(); | ||
| 66 | std::queue<std::pair<int, int>> q; | ||
| 67 | q.emplace(start_i, start_j); | ||
| 68 | 312 | output[start_i][start_j] = current_label; | |
| 69 |
2/2✓ Branch 0 taken 768 times.
✓ Branch 1 taken 312 times.
|
1080 | while (!q.empty()) { |
| 70 | const auto [x, y] = q.front(); | ||
| 71 | q.pop(); | ||
| 72 |
2/2✓ Branch 0 taken 6144 times.
✓ Branch 1 taken 768 times.
|
6912 | for (const auto &[dx, dy] : kShifts) { |
| 73 | 6144 | const int nx = x + dx; | |
| 74 | 6144 | const int ny = y + dy; | |
| 75 |
12/12✓ Branch 0 taken 5976 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 5664 times.
✓ Branch 3 taken 312 times.
✓ Branch 4 taken 5512 times.
✓ Branch 5 taken 152 times.
✓ Branch 6 taken 5224 times.
✓ Branch 7 taken 288 times.
✓ Branch 8 taken 1824 times.
✓ Branch 9 taken 3400 times.
✓ Branch 10 taken 456 times.
✓ Branch 11 taken 1368 times.
|
6144 | if (nx >= 0 && nx < rows_ && ny >= 0 && ny < cols_ && input[nx][ny] == 1 && output[nx][ny] == 0) { |
| 76 |
1/2✓ Branch 1 taken 456 times.
✗ Branch 2 not taken.
|
456 | output[nx][ny] = current_label; |
| 77 | q.emplace(nx, ny); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | 312 | } | |
| 82 | |||
| 83 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | bool MorozovaSConnectedComponentsSEQ::RunImpl() { |
| 84 | const auto &input = GetInput(); | ||
| 85 | auto &output = GetOutput(); | ||
| 86 |
3/6✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 32 times.
✗ Branch 5 not taken.
|
32 | if (input.empty() || rows_ == 0 || cols_ == 0) { |
| 87 | return true; | ||
| 88 | } | ||
| 89 | int current_label = 1; | ||
| 90 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 32 times.
|
320 | for (int i = 0; i < rows_; ++i) { |
| 91 |
2/2✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 288 times.
|
3040 | for (int j = 0; j < cols_; ++j) { |
| 92 |
4/4✓ Branch 0 taken 768 times.
✓ Branch 1 taken 1984 times.
✓ Branch 2 taken 312 times.
✓ Branch 3 taken 456 times.
|
2752 | if (input[i][j] == 1 && output[i][j] == 0) { |
| 93 | 312 | ProcessComponent(i, j, current_label); | |
| 94 | 312 | ++current_label; | |
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return true; | ||
| 100 | } | ||
| 101 | |||
| 102 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | bool MorozovaSConnectedComponentsSEQ::PostProcessingImpl() { |
| 103 | auto &output = GetOutput(); | ||
| 104 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | if (output.empty()) { |
| 105 | return true; | ||
| 106 | } | ||
| 107 | 32 | int max_label = 0; | |
| 108 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 32 times.
|
320 | for (const auto &row : output) { |
| 109 |
2/2✓ Branch 0 taken 2752 times.
✓ Branch 1 taken 288 times.
|
3040 | for (const int v : row) { |
| 110 | 2752 | max_label = std::max(max_label, v); | |
| 111 | } | ||
| 112 | } | ||
| 113 | 32 | output.push_back({max_label}); | |
| 114 | 32 | return true; | |
| 115 | } | ||
| 116 | |||
| 117 | } // namespace morozova_s_connected_components | ||
| 118 |