-
-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathvalidate.cpp
More file actions
79 lines (65 loc) · 2.13 KB
/
Copy pathvalidate.cpp
File metadata and controls
79 lines (65 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "solution.h"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <memory>
static void reference_solution(const InputVector &input, uint8_t radius,
OutputVector &output) {
int pos = 0;
int currentSum = 0;
int size = (int)input.size();
// 1. left border - time spend in this loop can be ignored, no need to
// optimize it
for (int i = 0; i < std::min<int>(size, radius); ++i) {
currentSum += input[i];
}
int limit = std::min(radius + 1, size - radius);
for (pos = 0; pos < limit; ++pos) {
currentSum += input[pos + radius];
output[pos] = currentSum;
}
// 2. main loop. During optimization, focus mainly on this part
limit = size - radius;
for (; pos < limit; ++pos) {
currentSum -= input[pos - radius - 1];
currentSum += input[pos + radius];
output[pos] = currentSum;
}
// 3. special case, executed only if size <= 2*radius + 1
limit = std::min(radius + 1, size);
for (; pos < limit; pos++) {
output[pos] = currentSum;
}
// 4. right border - time spend in this loop can be ignored, no need to
// optimize it
for (; pos < size; ++pos) {
currentSum -= input[pos - radius - 1];
output[pos] = currentSum;
}
}
int main() {
InputVector inA;
init(inA);
OutputVector expected, received;
zero(expected, (int)inA.size());
zero(received, (int)inA.size());
reference_solution(inA, radius, expected);
imageSmoothing(inA, radius, received);
if (expected.size() != received.size()) {
std::cerr << "Result has invalid size. Expected size: " << expected.size()
<< " received: " << received.size() << std::endl;
return 1;
}
auto cmp_result =
std::mismatch(expected.begin(), expected.end(), received.begin());
if (cmp_result.first != expected.end()) {
std::cerr << "Validation Failed at position: "
<< std::distance(expected.begin(), cmp_result.first)
<< ". Expected: " << *cmp_result.first
<< " received: " << *cmp_result.second << "." << std::endl;
return 1;
}
std::cout << "Validation Successful" << std::endl;
return 0;
}