-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
115 lines (97 loc) · 3 KB
/
utils.h
File metadata and controls
115 lines (97 loc) · 3 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <algorithm> // std::max
#include <cstddef> // std::size_t
#include <iomanip> // std::setw
#include <ostream> // std::ostream
#include <sstream> // std::ostringstream
namespace utils {
// Compute the maximum value of an image
template <typename Img>
typename Img::value_type max(const Img& img)
{
const auto d = img.domain();
auto p_it = typename Img::p_iterator_type(d);
p_it.start();
auto max = img(p_it.value());
for (p_it.next(); p_it.is_valid(); p_it.next()) {
const auto tmp = img(p_it.value());
if (max < tmp) {
max = tmp;
}
}
return max;
}
// Compute the minimum value of an image
template <typename Img>
typename Img::value_type min(const Img& img)
{
const auto d = img.domain();
auto p_it = typename Img::p_iterator_type(d);
p_it.start();
auto min = img(p_it.value());
for (p_it.next(); p_it.is_valid(); p_it.next()) {
const auto tmp = img(p_it.value());
if (min > tmp) {
min = tmp;
}
}
return min;
}
// Compute the width as string of a value
template <typename T>
std::size_t string_width(T val)
{
std::ostringstream oss;
oss << val;
return oss.str().size();
}
template <typename Img>
std::size_t string_max_width(const Img& img)
{
return std::max(string_width(max(img)), string_width(min(img)));
}
template <typename T, typename U>
bool image_equal_to(const T& lhs, const U& rhs)
{
const auto d_lhs = lhs.domain();
if (d_lhs != rhs.domain()) {
return false;
}
auto it = typename T::p_iterator_type(d_lhs);
for (it.start(); it.is_valid(); it.next()) {
if (lhs(it) != rhs(it)) {
return false;
}
}
return true;
}
template <typename T>
std::ostream& print_image(std::ostream& os, const T& rhs)
{
const auto d = rhs.domain();
const auto width = string_max_width(rhs);
for (int i = 0, rows = d.rows(); i < rows; ++i) {
os << "|";
const auto cols = d.cols() - 1;
for (int j = 0; j < cols; ++j) {
const auto p = typename T::point_type{i, j};
if (!d.has(p)) {
// numeral width + 2 spaces for ',' and ' ' which are omitted
os << std::setw(width + 2) << " ";
}
else {
os << std::setw(width) << rhs(p) << ", ";
}
}
const auto p = typename T::point_type{i, cols};
if (!d.has(p)) {
os << std::setw(width) << " "
<< "|\n";
}
else {
os << std::setw(width) << rhs({i, cols}) << "|\n";
}
}
return os;
}
} // namespace utils