Skip to content

Commit 6cf3d72

Browse files
YoannQDQnyalldawson
authored andcommittedMar 30, 2023
Fix clang-tidy warnings
1 parent 3750332 commit 6cf3d72

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed
 

‎src/analysis/processing/qgsalgorithmrandomextract.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &para
130130
if ( !noDuplicates )
131131
{
132132
feedback->pushInfo( QObject::tr( "Randomly select %1 features" ).arg( number ) );
133-
const std::uniform_int_distribution<int> fidsDistribution( 0, allFeats.size() - 1 );
133+
const std::uniform_int_distribution<size_t> fidsDistribution( 0, allFeats.size() - 1 );
134134

135-
std::vector< int > indexes( number );
135+
std::vector< size_t > indexes( number );
136136
std::generate( indexes.begin(), indexes.end(), bind( fidsDistribution, mersenneTwister ) );
137137
QHash< QgsFeatureId, int > idsCount;
138-
for ( int i : indexes )
138+
for ( size_t i : indexes )
139139
{
140140
const QgsFeatureId id = allFeats.at( i );
141141
if ( feedback->isCanceled() )
@@ -166,27 +166,29 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap &para
166166
else
167167
{
168168
feedback->pushInfo( QObject::tr( "Randomly select %1 features" ).arg( number ) );
169-
std::uniform_int_distribution<int> fidsDistribution;
170-
171-
int nb = count;
169+
std::uniform_int_distribution<size_t> fidsDistribution;
172170

173171
// If the number of features to select is greater than half the total number of features
174172
// we will instead randomly select features to *exclude* from the output layer
173+
size_t shuffledFeatureCount = number;
175174
bool invertSelection = number > count / 2;
176175
if ( invertSelection )
177-
number = count - number;
176+
shuffledFeatureCount = count - number;
177+
178+
size_t nb = count;
178179

179180
// Shuffle <number> features at the start of the iterator
180181
auto cursor = allFeats.begin();
181-
while ( number-- )
182+
using difference_type = std::vector<QgsFeatureId>::difference_type;
183+
while ( shuffledFeatureCount-- )
182184
{
183185
if ( feedback->isCanceled() )
184186
return QVariantMap();
185187

186188
// Update the distribution to match the number of unshuffled features
187-
fidsDistribution.param( std::uniform_int_distribution<int>::param_type( 0, nb - 1 ) );
189+
fidsDistribution.param( std::uniform_int_distribution<size_t>::param_type( 0, nb - 1 ) );
188190
// Swap the current feature with a random one
189-
std::swap( *cursor, *( cursor + fidsDistribution( mersenneTwister ) ) );
191+
std::swap( *cursor, *( cursor + static_cast<difference_type>( fidsDistribution( mersenneTwister ) ) ) );
190192
// Move the cursor to the next feature
191193
++cursor;
192194

0 commit comments

Comments
 (0)
Please sign in to comment.