Skip to content

Commit e717f1b

Browse files
authored
Merge pull request #2462 from joto/more-constants
Use more constants
2 parents 8d84b61 + 171e0dc commit e717f1b

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

src/command-line-parser.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,16 @@ void check_options_expire(options_t *options)
228228
{
229229
// Zoom level 31 is the technical limit because we use 32-bit integers for
230230
// the x and y index of a tile ID.
231-
if (options->expire_tiles_zoom_min > 31) {
232-
options->expire_tiles_zoom_min = 31;
231+
constexpr uint32_t ZOOM_MAX = 31;
232+
233+
if (options->expire_tiles_zoom_min > ZOOM_MAX) {
234+
options->expire_tiles_zoom_min = ZOOM_MAX;
233235
log_warn("Minimum zoom level for tile expiry is too "
234236
"large and has been set to 31.");
235237
}
236238

237-
if (options->expire_tiles_zoom > 31) {
238-
options->expire_tiles_zoom = 31;
239+
if (options->expire_tiles_zoom > ZOOM_MAX) {
240+
options->expire_tiles_zoom = ZOOM_MAX;
239241
log_warn("Maximum zoom level for tile expiry is too "
240242
"large and has been set to 31.");
241243
}

src/middle-ram.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,29 @@ void middle_ram_t::stop()
113113
{
114114
assert(m_middle_state == middle_state::done);
115115

116-
auto const mbyte = 1024 * 1024;
116+
constexpr auto MBYTE = 1024 * 1024;
117117

118118
if (m_persistent_cache) {
119119
log_debug("Middle 'ram': Node locations on disk: size={} bytes={}M",
120120
m_persistent_cache->size(),
121-
m_persistent_cache->used_memory() / mbyte);
121+
m_persistent_cache->used_memory() / MBYTE);
122122
} else {
123123
log_debug("Middle 'ram': Node locations in memory: size={} bytes={}M",
124124
m_node_locations.size(),
125-
m_node_locations.used_memory() / mbyte);
125+
m_node_locations.used_memory() / MBYTE);
126126
}
127127

128128
log_debug("Middle 'ram': Way nodes data: size={} capacity={} bytes={}M",
129129
m_way_nodes_data.size(), m_way_nodes_data.capacity(),
130-
m_way_nodes_data.capacity() / mbyte);
130+
m_way_nodes_data.capacity() / MBYTE);
131131

132132
log_debug("Middle 'ram': Way nodes index: size={} capacity={} bytes={}M",
133133
m_way_nodes_index.size(), m_way_nodes_index.capacity(),
134-
m_way_nodes_index.used_memory() / mbyte);
134+
m_way_nodes_index.used_memory() / MBYTE);
135135

136136
log_debug("Middle 'ram': Object data: size={} capacity={} bytes={}M",
137137
m_object_buffer.committed(), m_object_buffer.capacity(),
138-
m_object_buffer.capacity() / mbyte);
138+
m_object_buffer.capacity() / MBYTE);
139139

140140
std::size_t index_size = 0;
141141
std::size_t index_capacity = 0;
@@ -146,13 +146,13 @@ void middle_ram_t::stop()
146146
index_mem += index.used_memory();
147147
}
148148
log_debug("Middle 'ram': Object indexes: size={} capacity={} bytes={}M",
149-
index_size, index_capacity, index_mem / mbyte);
149+
index_size, index_capacity, index_mem / MBYTE);
150150

151151
log_debug("Middle 'ram': Memory used overall: {}MBytes",
152152
(m_node_locations.used_memory() + m_way_nodes_data.capacity() +
153153
m_way_nodes_index.used_memory() + m_object_buffer.capacity() +
154154
index_mem) /
155-
mbyte);
155+
MBYTE);
156156

157157
m_node_locations.clear();
158158

src/tile.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,17 @@ geom::point_t tile_t::to_world_coords(geom::point_t p,
3636

3737
geom::point_t tile_t::center() const noexcept
3838
{
39-
return to_world_coords({0.5, 0.5}, 1);
39+
constexpr double MIDDLE = 0.5;
40+
return to_world_coords({MIDDLE, MIDDLE}, 1);
4041
}
4142

4243
namespace {
4344

4445
// Quadkey implementation uses bit interleaving code from
4546
// https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2018/01/08/interleave.c
4647

48+
// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
49+
4750
uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept
4851
{
4952
uint64_t word = input;
@@ -66,6 +69,8 @@ uint32_t deinterleave_lowuint32(uint64_t word) noexcept
6669
return static_cast<uint32_t>(word);
6770
}
6871

72+
// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
73+
6974
uint32_t parse_num_with_max(std::string const &str, uint32_t max)
7075
{
7176
std::size_t pos = 0;

src/util.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,22 @@ namespace util {
2222

2323
std::string human_readable_duration(uint64_t seconds)
2424
{
25-
if (seconds < 60UL) {
25+
constexpr uint64_t SECONDS_IN_MINUTE = 60UL;
26+
constexpr uint64_t MINUTES_IN_HOUR = 60UL;
27+
28+
if (seconds < SECONDS_IN_MINUTE) {
2629
return fmt::format("{}s", seconds);
2730
}
2831

29-
if (seconds < (60UL * 60UL)) {
30-
return fmt::format("{}s ({}m {}s)", seconds, seconds / 60,
31-
seconds % 60);
32+
uint64_t const mins = seconds / SECONDS_IN_MINUTE;
33+
uint64_t const secs = seconds % SECONDS_IN_MINUTE;
34+
35+
if (seconds < (SECONDS_IN_MINUTE * MINUTES_IN_HOUR)) {
36+
return fmt::format("{}s ({}m {}s)", seconds, mins, secs);
3237
}
3338

34-
auto const secs = seconds % 60;
35-
auto const mins = seconds / 60;
36-
return fmt::format("{}s ({}h {}m {}s)", seconds, mins / 60, mins % 60,
37-
secs);
39+
return fmt::format("{}s ({}h {}m {}s)", seconds, mins / MINUTES_IN_HOUR,
40+
mins % MINUTES_IN_HOUR, secs);
3841
}
3942

4043
std::string human_readable_duration(std::chrono::microseconds duration)

src/wkb.cpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,37 +208,48 @@ class make_ewkb_visitor_t
208208

209209
std::string operator()(geom::point_t const &geom) const
210210
{
211+
constexpr std::size_t SIZE_POINT_HEADER_WITH_SRID = 1UL + 4UL + 4UL;
212+
constexpr std::size_t SIZE_POINT =
213+
SIZE_POINT_HEADER_WITH_SRID + SIZE_COORDINATE_PAIR;
214+
211215
std::string data;
212216

213217
if (m_ensure_multi) {
214218
write_header(&data, wkb_multi_point, m_srid);
215219
write_length(&data, 1);
216220
write_point(&data, geom);
217221
} else {
218-
// 9 byte header plus one set of coordinates
219-
constexpr std::size_t SIZE = 9 + 2 * 8;
220-
data.reserve(SIZE);
222+
data.reserve(SIZE_POINT);
221223
write_point(&data, geom, m_srid);
222-
assert(data.size() == SIZE);
224+
assert(data.size() == SIZE_POINT);
223225
}
224226

225227
return data;
226228
}
227229

228230
std::string operator()(geom::linestring_t const &geom) const
229231
{
232+
constexpr std::size_t SIZE_HEADER_WITH_COUNT = 1UL + 4UL + 4UL;
233+
constexpr std::size_t SIZE_HEADER_WITH_COUNT_AND_SRID =
234+
SIZE_HEADER_WITH_COUNT + 4UL;
235+
230236
std::string data;
231237

238+
std::size_t const coords_size = geom.size() * SIZE_COORDINATE_PAIR;
239+
232240
if (m_ensure_multi) {
233-
// Two 13 bytes headers plus n sets of coordinates
234-
data.reserve(2UL * 13UL + geom.size() * (2UL * 8UL));
241+
data.reserve(SIZE_HEADER_WITH_COUNT_AND_SRID +
242+
SIZE_HEADER_WITH_COUNT + coords_size);
235243
write_header(&data, wkb_multi_line, m_srid);
236244
write_length(&data, 1);
237245
write_linestring(&data, geom);
246+
assert(data.size() == SIZE_HEADER_WITH_COUNT_AND_SRID +
247+
SIZE_HEADER_WITH_COUNT + coords_size);
238248
} else {
239-
// 13 byte header plus n sets of coordinates
240-
data.reserve(13UL + geom.size() * (2UL * 8UL));
249+
data.reserve(SIZE_HEADER_WITH_COUNT_AND_SRID + coords_size);
241250
write_linestring(&data, geom, m_srid);
251+
assert(data.size() ==
252+
SIZE_HEADER_WITH_COUNT_AND_SRID + coords_size);
242253
}
243254

244255
return data;
@@ -288,6 +299,8 @@ class make_ewkb_visitor_t
288299
}
289300

290301
private:
302+
static constexpr std::size_t SIZE_COORDINATE_PAIR = 2UL * sizeof(double);
303+
291304
uint32_t m_srid;
292305
bool m_ensure_multi;
293306

0 commit comments

Comments
 (0)