Skip to content

Commit f54f2d2

Browse files
committed
done porting tests for containers
1 parent b299d5b commit f54f2d2

13 files changed

+176
-112
lines changed

CMakeLists.txt.old

-18
Original file line numberDiff line numberDiff line change
@@ -112,24 +112,6 @@ endif()
112112
# CHECK FOR SYSTEM LIBRARIES, OPTIONS, ETC..
113113
# ----------------------------------------------------------------------------
114114

115-
# Build static or dynamic libs?
116-
# ===================================================
117-
# Default: dynamic libraries (except if building to JavaScript code)
118-
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
119-
set(_def_value OFF)
120-
else()
121-
set(_def_value ON)
122-
endif()
123-
set(BUILD_SHARED_LIBS ${_def_value} CACHE BOOL "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)")
124-
unset(_def_value)
125-
126-
if(BUILD_SHARED_LIBS)
127-
set(CMAKE_MRPT_BUILD_SHARED_LIB "#define MRPT_BUILT_AS_DLL")
128-
set(CMAKE_MRPT_BUILD_SHARED_LIB_ONOFF 1)
129-
else()
130-
set(CMAKE_MRPT_BUILD_SHARED_LIB "/* #define MRPT_BUILT_AS_DLL */")
131-
set(CMAKE_MRPT_BUILD_SHARED_LIB_ONOFF 0)
132-
endif()
133115

134116
# Only for emscripten: build executables as .html demo pages:
135117
# =============================================================

modules/mrpt_common/cmake/mrpt_cmake_functions.cmake

+25-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,6 @@
77
# Released under BSD-3. See LICENSE file
88
# ------------------------------------------------------------------------------
99

10-
include(GNUInstallDirs) # for install dirs in multilib
11-
include(CMakePackageConfigHelpers)
12-
13-
find_package(GTest QUIET)
14-
1510
# This file defines utility CMake functions to ensure uniform settings all
1611
# across MRPT modules, programs, and tests.
1712
#
@@ -20,6 +15,31 @@ find_package(GTest QUIET)
2015
# find_package(mrpt_common REQUIRED) # this includes mrpt_cmake_functions.cmake
2116
#
2217

18+
include(GNUInstallDirs) # for install dirs in multilib
19+
include(CMakePackageConfigHelpers)
20+
21+
find_package(GTest QUIET)
22+
23+
# Build static or dynamic libs?
24+
# ===================================================
25+
# Default: dynamic libraries (except if building to JavaScript code)
26+
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
27+
set(_def_value OFF)
28+
else()
29+
set(_def_value ON)
30+
endif()
31+
set(BUILD_SHARED_LIBS ${_def_value} CACHE BOOL "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)")
32+
unset(_def_value)
33+
34+
if(BUILD_SHARED_LIBS)
35+
set(CMAKE_MRPT_BUILD_SHARED_LIB "#define MRPT_BUILT_AS_DLL")
36+
set(CMAKE_MRPT_BUILD_SHARED_LIB_ONOFF 1)
37+
else()
38+
set(CMAKE_MRPT_BUILD_SHARED_LIB "/* #define MRPT_BUILT_AS_DLL */")
39+
set(CMAKE_MRPT_BUILD_SHARED_LIB_ONOFF 0)
40+
endif()
41+
42+
2343
# ccache:
2444
if(NOT MSVC AND NOT XCODE_VERSION)
2545
option(MRPT_BUILD_WITH_CCACHE "Use ccache compiler cache" ON)

modules/mrpt_containers/include/mrpt/containers/CDynamicGrid.h

+21-12
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,18 @@ class CDynamicGrid
8787
m_resolution = resolution;
8888

8989
// Now the number of cells should be integers:
90-
m_size_x = round((m_x_max - m_x_min) / m_resolution);
91-
m_size_y = round((m_y_max - m_y_min) / m_resolution);
90+
m_size_x = static_cast<std::size_t>(round((m_x_max - m_x_min) / m_resolution));
91+
m_size_y = static_cast<std::size_t>(round((m_y_max - m_y_min) / m_resolution));
9292

9393
// Cells memory:
9494
if (fill_value)
95+
{
9596
m_map.assign(m_size_x * m_size_y, *fill_value);
97+
}
9698
else
99+
{
97100
m_map.resize(m_size_x * m_size_y);
101+
}
98102
}
99103

100104
/** Erase the contents of all the cells. */
@@ -125,7 +129,9 @@ class CDynamicGrid
125129
// Is resize really necessary?
126130
if (new_x_min >= m_x_min && new_y_min >= m_y_min && new_x_max <= m_x_max &&
127131
new_y_max <= m_y_max)
132+
{
128133
return;
134+
}
129135

130136
if (new_x_min > m_x_min) new_x_min = m_x_min;
131137
if (new_x_max < m_x_max) new_x_max = m_x_max;
@@ -153,23 +159,26 @@ class CDynamicGrid
153159
new_y_max = m_resolution * round(new_y_max / m_resolution);
154160

155161
// Change the map size: Extensions at each side:
156-
unsigned int extra_x_izq = round((m_x_min - new_x_min) / m_resolution);
157-
unsigned int extra_y_arr = round((m_y_min - new_y_min) / m_resolution);
162+
const auto extra_x_left = static_cast<std::size_t>(round((m_x_min - new_x_min) / m_resolution));
163+
const auto extra_y_top = static_cast<std::size_t>(round((m_y_min - new_y_min) / m_resolution));
158164

159-
unsigned int new_size_x = round((new_x_max - new_x_min) / m_resolution);
160-
unsigned int new_size_y = round((new_y_max - new_y_min) / m_resolution);
165+
const auto new_size_x = static_cast<std::size_t>(round((new_x_max - new_x_min) / m_resolution));
166+
const auto new_size_y = static_cast<std::size_t>(round((new_y_max - new_y_min) / m_resolution));
161167

162168
// Reserve new memory:
163169
grid_data_t new_map;
164170
new_map.resize(new_size_x * new_size_y, defaultValueNewCells);
165171

166172
// Copy previous rows:
167-
unsigned int x, y;
168173
iterator itSrc, itDst;
169-
for (y = 0; y < m_size_y; y++)
174+
for (std::size_t y = 0; y < m_size_y; y++)
170175
{
171-
for (x = 0, itSrc = (m_map.begin() + y * m_size_x),
172-
itDst = (new_map.begin() + extra_x_izq + (y + extra_y_arr) * new_size_x);
176+
const auto idxSrc = static_cast<typename std::vector<T>::difference_type>(y * m_size_x);
177+
const auto idxDst = static_cast<typename std::vector<T>::difference_type>(
178+
extra_x_left + (y + extra_y_top) * new_size_x);
179+
180+
std::size_t x;
181+
for (x = 0, itSrc = (m_map.begin() + idxSrc), itDst = (new_map.begin() + idxDst);
173182
x < m_size_x; ++x, ++itSrc, ++itDst)
174183
{
175184
*itDst = *itSrc;
@@ -198,7 +207,7 @@ class CDynamicGrid
198207
const int cy = y2idx(y);
199208
if (cx < 0 || cx >= static_cast<int>(m_size_x)) return nullptr;
200209
if (cy < 0 || cy >= static_cast<int>(m_size_y)) return nullptr;
201-
return &m_map[cx + cy * m_size_x];
210+
return &m_map[static_cast<size_t>(cx) + static_cast<size_t>(cy) * m_size_x];
202211
}
203212
/** \overload */
204213
inline const T* cellByPos(double x, double y) const
@@ -207,7 +216,7 @@ class CDynamicGrid
207216
const int cy = y2idx(y);
208217
if (cx < 0 || cx >= static_cast<int>(m_size_x)) return nullptr;
209218
if (cy < 0 || cy >= static_cast<int>(m_size_y)) return nullptr;
210-
return &m_map[cx + cy * m_size_x];
219+
return &m_map[static_cast<size_t>(cx) + static_cast<size_t>(cy) * m_size_x];
211220
}
212221

213222
/** Returns a pointer to the contents of a cell given by its cell indexes,

modules/mrpt_containers/include/mrpt/containers/find_closest.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,18 @@ namespace mrpt::containers
3232
*/
3333
template <typename Container>
3434
std::optional<std::pair<typename Container::key_type, typename Container::mapped_type>>
35-
find_closest_with_tolerance(const Container& data, const double x, double tolerance)
35+
find_closest_with_tolerance(
36+
const Container& data,
37+
const typename Container::key_type x,
38+
const typename Container::key_type tolerance)
3639
{
3740
const auto t_min = x - tolerance;
3841
const auto t_max = x + tolerance;
3942

4043
auto it_lo = data.lower_bound(t_min);
4144
auto it_hi = data.upper_bound(t_max);
4245

43-
double min_distance = std::numeric_limits<double>::max();
46+
auto min_distance = std::numeric_limits<typename Container::key_type>::max();
4447
std::optional<std::pair<typename Container::key_type, typename Container::mapped_type>> best;
4548

4649
for (auto it = it_lo; it != it_hi; ++it)
@@ -71,13 +74,16 @@ find_closest_with_tolerance(const Container& data, const double x, double tolera
7174
*/
7275
template <typename Container>
7376
std::optional<std::pair<typename Container::key_type, typename Container::mapped_type>>
74-
find_closest(const Container& data, const double x)
77+
find_closest(const Container& data, const typename Container::key_type x)
7578
{
7679
typename Container::const_iterator its[2] = {data.lower_bound(x), data.upper_bound(x)};
7780

78-
if (!data.empty() && its[0] != data.begin()) --its[0];
81+
if (!data.empty() && its[0] != data.begin())
82+
{
83+
--its[0];
84+
}
7985

80-
double min_distance = std::numeric_limits<double>::max();
86+
auto min_distance = std::numeric_limits<typename Container::key_type>::max();
8187
std::optional<std::pair<typename Container::key_type, typename Container::mapped_type>> best;
8288

8389
for (const auto& it : its)

modules/mrpt_containers/include/mrpt/containers/ts_hash_map.h

+22-9
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,15 @@ class ts_hash_map
114114
return m_vec == o.m_vec && m_idx_outer == o.m_idx_outer && m_idx_inner == o.m_idx_inner;
115115
}
116116
bool operator!=(const const_iterator& o) const { return !(*this == o); }
117-
const value_type& operator*() const { return (*m_vec)[m_idx_outer][m_idx_inner]; }
118-
const value_type* operator->() const { return &(*m_vec)[m_idx_outer][m_idx_inner]; }
117+
const value_type& operator*() const
118+
{
119+
return (*m_vec)[static_cast<std::size_t>(m_idx_outer)][static_cast<std::size_t>(m_idx_inner)];
120+
}
121+
const value_type* operator->() const
122+
{
123+
return &(
124+
*m_vec)[static_cast<std::size_t>(m_idx_outer)][static_cast<std::size_t>(m_idx_inner)];
125+
}
119126
inline const_iterator operator++(int)
120127
{ /* Post: it++ */
121128
const_iterator aux = *this;
@@ -143,8 +150,10 @@ class ts_hash_map
143150
m_idx_inner = 0;
144151
m_idx_outer++;
145152
}
146-
} while (m_idx_outer < static_cast<int>(m_parent->m_vec.size()) &&
147-
!(*m_vec)[m_idx_outer][m_idx_inner].used);
153+
} while (
154+
m_idx_outer < static_cast<int>(m_parent->m_vec.size()) &&
155+
!(*m_vec)[static_cast<std::size_t>(m_idx_outer)][static_cast<std::size_t>(m_idx_inner)]
156+
.used);
148157
}
149158
};
150159

@@ -158,11 +167,13 @@ class ts_hash_map
158167
}
159168
value_type& operator*()
160169
{
161-
return (*const_iterator::m_vec)[const_iterator::m_idx_outer][const_iterator::m_idx_inner];
170+
return (*const_iterator::m_vec)[static_cast<std::size_t>(const_iterator::m_idx_outer)]
171+
[static_cast<std::size_t>(const_iterator::m_idx_inner)];
162172
}
163173
value_type* operator->()
164174
{
165-
return &(*const_iterator::m_vec)[const_iterator::m_idx_outer][const_iterator::m_idx_inner];
175+
return &(*const_iterator::m_vec)[static_cast<std::size_t>(const_iterator::m_idx_outer)]
176+
[static_cast<std::size_t>(const_iterator::m_idx_inner)];
166177
}
167178
inline iterator operator++(int)
168179
{ /* Post: it++ */
@@ -269,7 +280,9 @@ class ts_hash_map
269280
for (unsigned int i = 0; i < NUM_HAS_TABLE_COLLISIONS_ALLOWED; i++)
270281
{
271282
if (match_arr[i].used && match_arr[i].first == key)
272-
return const_iterator(m_vec, *this, hash, i);
283+
{
284+
return const_iterator(m_vec, *this, hash, static_cast<int>(i));
285+
}
273286
}
274287
return this->end();
275288
}
@@ -285,7 +298,7 @@ class ts_hash_map
285298
const_iterator end() const
286299
{
287300
auto lck = mrpt::lockHelper(m_mtx);
288-
return const_iterator(m_vec, *this, m_vec.size(), 0);
301+
return const_iterator(m_vec, *this, static_cast<int>(m_vec.size()), 0);
289302
}
290303
iterator begin()
291304
{
@@ -297,7 +310,7 @@ class ts_hash_map
297310
iterator end()
298311
{
299312
auto lck = mrpt::lockHelper(m_mtx);
300-
return iterator(m_vec, *this, m_vec.size(), 0);
313+
return iterator(m_vec, *this, static_cast<int>(m_vec.size()), 0);
301314
}
302315
/** @} */
303316

modules/mrpt_containers/include/mrpt/containers/vector_with_small_size_optimization.h

+13-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ class vector_with_small_size_optimization
6464

6565
vector_with_small_size_optimization(size_t n) : m_is_small(n <= small_size), m_size(n)
6666
{
67-
if (!m_is_small) m_v.resize(n);
67+
if (!m_is_small)
68+
{
69+
m_v.resize(n);
70+
}
6871
}
6972

7073
vector_with_small_size_optimization(const vector_with_small_size_optimization& o) { *this = o; }
@@ -74,19 +77,27 @@ class vector_with_small_size_optimization
7477
m_size = o.m_size;
7578
m_is_small = o.m_is_small;
7679
if (m_size > small_size)
80+
{
7781
m_v = o.m_v;
82+
}
7883
else if (m_size > 0)
84+
{
7985
m_a = o.m_a;
86+
}
8087
return *this;
8188
}
8289
vector_with_small_size_optimization& operator=(vector_with_small_size_optimization&& o)
8390
{
8491
m_size = o.m_size;
8592
m_is_small = o.m_is_small;
8693
if (m_size > small_size)
94+
{
8795
m_v = std::move(o.m_v);
96+
}
8897
else if (m_size > 0)
98+
{
8999
m_a = std::move(o.m_a);
100+
}
90101
return *this;
91102
}
92103

@@ -179,7 +190,7 @@ class vector_with_small_size_optimization
179190
}
180191
else if (!m_is_small && n <= small_size)
181192
{
182-
std::copy(m_v.begin(), m_v.begin() + n, m_a.begin());
193+
std::copy(m_v.begin(), m_v.begin() + static_cast<difference_type>(n), m_a.begin());
183194
}
184195
}
185196
m_size = n;

modules/mrpt_containers/include/mrpt/containers/yaml.h

+6-7
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class yaml
226226
bool hasComment(CommentPosition pos) const
227227
{
228228
MRPT_START
229-
unsigned int posIndex = static_cast<int>(pos);
230-
ASSERT_LT_(posIndex, static_cast<int>(CommentPosition::MAX));
229+
const auto posIndex = static_cast<unsigned int>(pos);
230+
ASSERT_LT_(posIndex, static_cast<unsigned int>(CommentPosition::MAX));
231231
return comments[posIndex].has_value();
232232
MRPT_END
233233
}
@@ -247,8 +247,8 @@ class yaml
247247
const std::string& comment(CommentPosition pos) const
248248
{
249249
MRPT_START
250-
unsigned int posIndex = static_cast<int>(pos);
251-
ASSERT_LT_(posIndex, static_cast<int>(CommentPosition::MAX));
250+
const auto posIndex = static_cast<unsigned int>(pos);
251+
ASSERT_LT_(posIndex, static_cast<unsigned int>(CommentPosition::MAX));
252252
ASSERTMSG_(
253253
comments[posIndex].has_value(), "Trying to access comment but this node has none.");
254254
return comments[posIndex].value();
@@ -654,9 +654,8 @@ class yaml
654654
"on the left hand.");
655655
operator[](vc.keyname) = vc.value;
656656
// keyComment:
657-
int posIndex = static_cast<int>(vc.position);
658-
ASSERT_GE_(posIndex, 0);
659-
ASSERT_LT_(posIndex, static_cast<int>(CommentPosition::MAX));
657+
unsigned int posIndex = static_cast<unsigned int>(vc.position);
658+
ASSERT_LT_(posIndex, static_cast<unsigned int>(CommentPosition::MAX));
660659
auto& n = keyNode(vc.keyname);
661660
n.comments[posIndex].emplace(vc.comment);
662661
return *this;

0 commit comments

Comments
 (0)