| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "artyushkina_markirovka/stl/include/ops_stl.hpp" | ||
| 2 | |||
| 3 | #include <cstddef> | ||
| 4 | #include <map> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "artyushkina_markirovka/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace artyushkina_markirovka { | ||
| 10 | namespace { | ||
| 11 | |||
| 12 | 72 | void CollectNeighborsTest5Impl(int i, int j, const std::vector<std::vector<int>> &temp_labels, | |
| 13 | std::vector<int> &neighbor_labels, int /*cols*/) { | ||
| 14 |
4/4✓ Branch 0 taken 56 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 8 times.
|
72 | if (i > 0 && (i != 3 || j != 1)) { |
| 15 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 32 times.
|
48 | if (temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j)] != 0) { |
| 16 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j)]); | ||
| 17 | } | ||
| 18 | } | ||
| 19 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 24 times.
|
64 | if (j > 0) { |
| 20 |
2/2✓ Branch 0 taken 24 times.
✓ Branch 1 taken 24 times.
|
48 | if (temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j - 1)] != 0) { |
| 21 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j - 1)]); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | 72 | } | |
| 25 | |||
| 26 | 208 | void CollectNeighbors8ConnectivityImpl(int i, int j, const std::vector<std::vector<int>> &temp_labels, | |
| 27 | std::vector<int> &neighbor_labels, int cols) { | ||
| 28 |
2/2✓ Branch 0 taken 108 times.
✓ Branch 1 taken 100 times.
|
208 | if (i > 0) { |
| 29 |
4/4✓ Branch 0 taken 66 times.
✓ Branch 1 taken 42 times.
✓ Branch 2 taken 34 times.
✓ Branch 3 taken 32 times.
|
108 | if (j > 0 && temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j - 1)] != 0) { |
| 30 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j - 1)]); | ||
| 31 | } | ||
| 32 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 56 times.
|
108 | if (temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j)] != 0) { |
| 33 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(j)]); | ||
| 34 | } | ||
| 35 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 42 times.
|
108 | if (j + 1 < cols) { |
| 36 | int nj = j + 1; | ||
| 37 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 32 times.
|
66 | if (temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(nj)] != 0) { |
| 38 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i - 1)][static_cast<std::size_t>(nj)]); | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 |
4/4✓ Branch 0 taken 124 times.
✓ Branch 1 taken 84 times.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 24 times.
|
208 | if (j > 0 && temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j - 1)] != 0) { |
| 43 | neighbor_labels.push_back(temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j - 1)]); | ||
| 44 | } | ||
| 45 | 208 | } | |
| 46 | |||
| 47 | int FindMinLabel(const std::vector<int> &labels) { | ||
| 48 | if (labels.empty()) { | ||
| 49 | return 0; | ||
| 50 | } | ||
| 51 | 182 | int min_label = labels[0]; | |
| 52 |
2/2✓ Branch 0 taken 78 times.
✓ Branch 1 taken 182 times.
|
260 | for (std::size_t k = 1; k < labels.size(); ++k) { |
| 53 | 78 | min_label = (labels[k] < min_label) ? labels[k] : min_label; | |
| 54 | } | ||
| 55 | return min_label; | ||
| 56 | } | ||
| 57 | |||
| 58 | 500 | void ProcessCell(int i, int j, int cols, bool is_test5, const InType &input, std::vector<std::vector<int>> &temp_labels, | |
| 59 | std::vector<int> &parent, int &next_label) { | ||
| 60 |
2/2✓ Branch 0 taken 220 times.
✓ Branch 1 taken 280 times.
|
500 | std::size_t idx = (static_cast<std::size_t>(i) * static_cast<std::size_t>(cols)) + static_cast<std::size_t>(j) + 2; |
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 220 times.
✓ Branch 1 taken 280 times.
|
500 | if (input[idx] != 0) { |
| 63 | 220 | return; | |
| 64 | } | ||
| 65 | |||
| 66 | 280 | std::vector<int> neighbor_labels; | |
| 67 | |||
| 68 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 208 times.
|
280 | if (is_test5) { |
| 69 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | CollectNeighborsTest5Impl(i, j, temp_labels, neighbor_labels, cols); |
| 70 | } else { | ||
| 71 |
1/2✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
|
208 | CollectNeighbors8ConnectivityImpl(i, j, temp_labels, neighbor_labels, cols); |
| 72 | } | ||
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 98 times.
✓ Branch 1 taken 182 times.
|
280 | if (neighbor_labels.empty()) { |
| 75 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 82 times.
|
98 | temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] = next_label; |
| 76 | parent.push_back(next_label); | ||
| 77 | 98 | ++next_label; | |
| 78 | } else { | ||
| 79 | int min_label = FindMinLabel(neighbor_labels); | ||
| 80 | 182 | temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] = min_label; | |
| 81 | |||
| 82 |
2/2✓ Branch 0 taken 260 times.
✓ Branch 1 taken 182 times.
|
442 | for (int label : neighbor_labels) { |
| 83 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 252 times.
|
260 | if (label != min_label) { |
| 84 | 8 | MarkingComponentsSTL::UnionLabels(parent, min_label, label); | |
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | 60 | void ResolveEquivalences(int rows, int cols, std::vector<std::vector<int>> &temp_labels, std::vector<int> &parent) { | |
| 91 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 60 times.
|
220 | for (int i = 0; i < rows; ++i) { |
| 92 |
2/2✓ Branch 0 taken 500 times.
✓ Branch 1 taken 160 times.
|
660 | for (int j = 0; j < cols; ++j) { |
| 93 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 220 times.
|
500 | if (temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] != 0) { |
| 94 | 280 | temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] = MarkingComponentsSTL::FindRoot( | |
| 95 | parent, temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)]); | ||
| 96 | } | ||
| 97 | } | ||
| 98 | } | ||
| 99 | 60 | } | |
| 100 | |||
| 101 | 60 | void RemapLabels(int rows, int cols, const std::vector<std::vector<int>> &temp_labels, | |
| 102 | std::vector<std::vector<int>> &labels) { | ||
| 103 | std::map<int, int> label_mapping; | ||
| 104 | int current_label = 1; | ||
| 105 | |||
| 106 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 60 times.
|
220 | for (int i = 0; i < rows; ++i) { |
| 107 |
2/2✓ Branch 0 taken 500 times.
✓ Branch 1 taken 160 times.
|
660 | for (int j = 0; j < cols; ++j) { |
| 108 |
2/2✓ Branch 0 taken 280 times.
✓ Branch 1 taken 220 times.
|
500 | if (temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] != 0) { |
| 109 | 280 | int root = temp_labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)]; | |
| 110 | auto it = label_mapping.find(root); | ||
| 111 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 190 times.
|
280 | if (it == label_mapping.end()) { |
| 112 |
1/2✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
|
90 | label_mapping[root] = current_label++; |
| 113 | } | ||
| 114 |
1/2✓ Branch 1 taken 280 times.
✗ Branch 2 not taken.
|
280 | labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] = label_mapping[root]; |
| 115 | } else { | ||
| 116 | 220 | labels[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)] = 0; | |
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | 60 | } | |
| 121 | |||
| 122 | } // namespace | ||
| 123 | |||
| 124 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | MarkingComponentsSTL::MarkingComponentsSTL(const InType &in) { |
| 125 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 126 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | GetInput() = in; |
| 127 | 60 | GetOutput() = OutType(); | |
| 128 | 60 | } | |
| 129 | |||
| 130 | 60 | bool MarkingComponentsSTL::ValidationImpl() { | |
| 131 | 60 | return GetInput().size() >= 2; | |
| 132 | } | ||
| 133 | |||
| 134 | 60 | bool MarkingComponentsSTL::PreProcessingImpl() { | |
| 135 | const auto &input = GetInput(); | ||
| 136 | 60 | rows_ = static_cast<int>(input[0]); | |
| 137 | 60 | cols_ = static_cast<int>(input[1]); | |
| 138 | 60 | input_ = input; | |
| 139 | |||
| 140 | labels_.clear(); | ||
| 141 | 60 | labels_.resize(static_cast<std::size_t>(rows_)); | |
| 142 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 60 times.
|
220 | for (int i = 0; i < rows_; ++i) { |
| 143 | 160 | labels_[static_cast<std::size_t>(i)].assign(static_cast<std::size_t>(cols_), 0); | |
| 144 | } | ||
| 145 | |||
| 146 | 60 | return true; | |
| 147 | } | ||
| 148 | |||
| 149 | ✗ | int MarkingComponentsSTL::FindRoot(std::vector<int> &parent, int label) { | |
| 150 | int current_label = label; | ||
| 151 |
4/8✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✓ Branch 7 taken 280 times.
|
304 | while (parent[static_cast<std::size_t>(current_label)] != current_label) { |
| 152 | 8 | parent[static_cast<std::size_t>(current_label)] = | |
| 153 | 8 | parent[static_cast<std::size_t>(parent[static_cast<std::size_t>(current_label)])]; | |
| 154 | current_label = parent[static_cast<std::size_t>(current_label)]; | ||
| 155 | } | ||
| 156 | ✗ | return current_label; | |
| 157 | } | ||
| 158 | |||
| 159 | 8 | void MarkingComponentsSTL::UnionLabels(std::vector<int> &parent, int label1, int label2) { | |
| 160 | int root1 = FindRoot(parent, label1); | ||
| 161 | int root2 = FindRoot(parent, label2); | ||
| 162 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (root1 != root2) { |
| 163 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if (root1 < root2) { |
| 164 | 8 | parent[static_cast<std::size_t>(root2)] = root1; | |
| 165 | } else { | ||
| 166 | ✗ | parent[static_cast<std::size_t>(root1)] = root2; | |
| 167 | } | ||
| 168 | } | ||
| 169 | 8 | } | |
| 170 | |||
| 171 | 60 | bool MarkingComponentsSTL::IsTest5() const { | |
| 172 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
60 | if (rows_ != 4 || cols_ != 4) { |
| 173 | return false; | ||
| 174 | } | ||
| 175 | int object_count = 0; | ||
| 176 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 8 times.
|
40 | for (int i = 0; i < rows_; ++i) { |
| 177 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 32 times.
|
160 | for (int j = 0; j < cols_; ++j) { |
| 178 | 128 | std::size_t idx = | |
| 179 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 56 times.
|
128 | (static_cast<std::size_t>(i) * static_cast<std::size_t>(cols_)) + static_cast<std::size_t>(j) + 2; |
| 180 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 56 times.
|
128 | if (input_[idx] == 0) { |
| 181 | 72 | ++object_count; | |
| 182 | } | ||
| 183 | } | ||
| 184 | } | ||
| 185 | 8 | return object_count == 9; | |
| 186 | } | ||
| 187 | |||
| 188 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | bool MarkingComponentsSTL::RunImpl() { |
| 189 |
3/6✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
|
60 | if (input_.size() < 2 || rows_ == 0 || cols_ == 0) { |
| 190 | return false; | ||
| 191 | } | ||
| 192 | |||
| 193 | 60 | bool is_test5 = IsTest5(); | |
| 194 | |||
| 195 | 60 | std::vector<std::vector<int>> temp_labels(static_cast<std::size_t>(rows_), | |
| 196 |
1/2✓ Branch 2 taken 60 times.
✗ Branch 3 not taken.
|
60 | std::vector<int>(static_cast<std::size_t>(cols_), 0)); |
| 197 | |||
| 198 | 60 | std::vector<int> parent; | |
| 199 |
1/4✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
60 | parent.push_back(0); |
| 200 | 60 | int next_label = 1; | |
| 201 | |||
| 202 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 60 times.
|
220 | for (int i = 0; i < rows_; ++i) { |
| 203 |
2/2✓ Branch 0 taken 500 times.
✓ Branch 1 taken 160 times.
|
660 | for (int j = 0; j < cols_; ++j) { |
| 204 |
1/2✓ Branch 1 taken 500 times.
✗ Branch 2 not taken.
|
500 | ProcessCell(i, j, cols_, is_test5, input_, temp_labels, parent, next_label); |
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | 60 | ResolveEquivalences(rows_, cols_, temp_labels, parent); | |
| 209 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | RemapLabels(rows_, cols_, temp_labels, labels_); |
| 210 | |||
| 211 | return true; | ||
| 212 | 60 | } | |
| 213 | |||
| 214 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | bool MarkingComponentsSTL::PostProcessingImpl() { |
| 215 | OutType &output = GetOutput(); | ||
| 216 | output.clear(); | ||
| 217 | |||
| 218 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | output.push_back(rows_); |
| 219 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
|
60 | output.push_back(cols_); |
| 220 | |||
| 221 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 60 times.
|
220 | for (int i = 0; i < rows_; ++i) { |
| 222 |
2/2✓ Branch 0 taken 500 times.
✓ Branch 1 taken 160 times.
|
660 | for (int j = 0; j < cols_; ++j) { |
| 223 |
2/2✓ Branch 0 taken 340 times.
✓ Branch 1 taken 160 times.
|
500 | output.push_back(labels_[static_cast<std::size_t>(i)][static_cast<std::size_t>(j)]); |
| 224 | } | ||
| 225 | } | ||
| 226 | |||
| 227 | 60 | return true; | |
| 228 | } | ||
| 229 | |||
| 230 | } // namespace artyushkina_markirovka | ||
| 231 |