Skip to content

Commit 60c41dc

Browse files
committed
fix all warnings in mrpt_random
1 parent 5762527 commit 60c41dc

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

modules/mrpt_random/include/mrpt/random/random_shuffle.h

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@ namespace mrpt::random
2424
template <class RandomIt, class URBG>
2525
void shuffle(RandomIt first, RandomIt last, URBG&& g)
2626
{
27-
const uint64_t n = last - first;
27+
const auto n = static_cast<int64_t>(last - first);
28+
if (n == 0)
29+
{
30+
return;
31+
}
2832
for (int64_t i = static_cast<int64_t>(n) - 1; i > 0; --i)
29-
std::swap(first[i], first[portable_uniform_distribution(g, 0, i)]);
33+
{
34+
const auto idx = portable_uniform_distribution(g, 0, static_cast<uint64_t>(i));
35+
std::swap(first[i], first[static_cast<typename RandomIt::difference_type>(idx)]);
36+
}
3037
}
3138

3239
/** Uniform shuffle a sequence.
@@ -49,10 +56,19 @@ void shuffle(RandomIt first, RandomIt last)
4956
template <class RandomIt, class URBG>
5057
void partial_shuffle(RandomIt first, RandomIt last, URBG&& g, size_t N)
5158
{
52-
const int64_t n = static_cast<int64_t>(last - first);
53-
const int64_t n_1 = n - 1;
54-
for (int64_t i = 0; i < n && i < static_cast<int64_t>(N); ++i)
55-
std::swap(first[i], first[portable_uniform_distribution(g, i, n_1)]);
59+
const uint64_t n = static_cast<uint64_t>(last - first);
60+
if (n == 0 || N == 0)
61+
{
62+
return;
63+
}
64+
const uint64_t n_1 = n - 1;
65+
for (uint64_t i = 0; i < n && i < N; ++i)
66+
{
67+
const auto idx = portable_uniform_distribution(g, i, n_1);
68+
std::swap(
69+
first[static_cast<typename RandomIt::difference_type>(i)],
70+
first[static_cast<typename RandomIt::difference_type>(idx)]);
71+
}
5672
}
5773

5874
} // namespace mrpt::random

modules/mrpt_random/src/random_unittest.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
+------------------------------------------------------------------------+ */
99

1010
#include <gtest/gtest.h>
11-
#include <mrpt/math/CMatrixFixed.h>
1211
#include <mrpt/random/RandomGenerators.h>
1312
#include <mrpt/random/random_shuffle.h>
1413

@@ -83,6 +82,8 @@ TEST(Random, KnownSequence)
8382
}
8483
}
8584

85+
// TODO(jlbc): Move this test to mrpt_math
86+
#if 0
8687
TEST(Random, drawGaussianMultivariateMany)
8788
{
8889
using namespace mrpt::random;
@@ -100,6 +101,7 @@ TEST(Random, drawGaussianMultivariateMany)
100101
EXPECT_EQ(samples.size(), nSamples);
101102
EXPECT_EQ(samples.at(0).size(), static_cast<size_t>(cov.rows()));
102103
}
104+
#endif
103105

104106
TEST(Random, portable_uniform_distribution)
105107
{
@@ -130,10 +132,10 @@ TEST(Random, portable_uniform_distribution)
130132
},
131133
};
132134

133-
for (int seed = 0; seed < 5; seed++)
135+
for (uint64_t seed = 0; seed < 5; seed++)
134136
{
135137
rnd.seed(seeds[seed]);
136-
for (int i = 0; i < 30; i++)
138+
for (uint64_t i = 0; i < 30; i++)
137139
{
138140
const auto v = mrpt::random::portable_uniform_distribution(rnd, seed * 5, 100 - 20 * seed);
139141
EXPECT_EQ(v, psr_seq[seed][i]) << "seed=" << seed << " i=" << i << "\n";

0 commit comments

Comments
 (0)