@@ -130,12 +130,12 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap ¶
130
130
if ( !noDuplicates )
131
131
{
132
132
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 );
134
134
135
- std::vector< int > indexes ( number );
135
+ std::vector< size_t > indexes ( number );
136
136
std::generate ( indexes.begin (), indexes.end (), bind ( fidsDistribution, mersenneTwister ) );
137
137
QHash< QgsFeatureId, int > idsCount;
138
- for ( int i : indexes )
138
+ for ( size_t i : indexes )
139
139
{
140
140
const QgsFeatureId id = allFeats.at ( i );
141
141
if ( feedback->isCanceled () )
@@ -166,27 +166,29 @@ QVariantMap QgsRandomExtractAlgorithm::processAlgorithm( const QVariantMap ¶
166
166
else
167
167
{
168
168
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;
172
170
173
171
// If the number of features to select is greater than half the total number of features
174
172
// we will instead randomly select features to *exclude* from the output layer
173
+ size_t shuffledFeatureCount = number;
175
174
bool invertSelection = number > count / 2 ;
176
175
if ( invertSelection )
177
- number = count - number;
176
+ shuffledFeatureCount = count - number;
177
+
178
+ size_t nb = count;
178
179
179
180
// Shuffle <number> features at the start of the iterator
180
181
auto cursor = allFeats.begin ();
181
- while ( number-- )
182
+ using difference_type = std::vector<QgsFeatureId>::difference_type;
183
+ while ( shuffledFeatureCount-- )
182
184
{
183
185
if ( feedback->isCanceled () )
184
186
return QVariantMap ();
185
187
186
188
// 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 ) );
188
190
// 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 ) ) ) );
190
192
// Move the cursor to the next feature
191
193
++cursor;
192
194
0 commit comments