replace std::sort with std::ranges::sort where appropriate

the c++-20 constrained algorithm is safer and code is more readable
This commit is contained in:
Arne Morten Kvarving committed 2026-02-20 14:21:53 +01:00
1 parent f29e84d06b
commit 9ff0392e6b
17 files changed
+29 -34

No files matched your search

@@ -867,7 +867,7 @@ CollectDataOnIORank(const Grid& grid, const EquilGrid* equilGrid,
sortedCartesianIdx_.push_back(cartMapper.cartesianIndex(idx));
}
std::sort(sortedCartesianIdx_.begin(), sortedCartesianIdx_.end());
std::ranges::sort(sortedCartesianIdx_);
localIdxToGlobalIdx_.resize(localGridView.size(0), -1);
// the I/O rank receives from all other ranks
@@ -47,7 +47,7 @@ namespace {
void reportUnsupportedOptionValuesAndThrow(std::vector<std::string> unsupp,
std::string_view optionName)
{
std::sort(unsupp.begin(), unsupp.end());
std::ranges::sort(unsupp);
auto u = std::unique(unsupp.begin(), unsupp.end());
const auto pl = (std::distance(unsupp.begin(), u) != 1) ? "s" : "";
+1 -1
View File
@@ -365,7 +365,7 @@ void FlowGenericVanguard::init()
{
parallelWells_.emplace_back(well.name(), true);
}
std::sort(parallelWells_.begin(), parallelWells_.end());
std::ranges::sort(parallelWells_);
// Check whether allowing distribute wells makes sense
if (enableDistributedWells() )
+2 -2
View File
@@ -139,7 +139,7 @@ FlowsContainer(const Schedule& schedule,
}
}
}
std::sort(blockVelocityAllIds_.begin(), blockVelocityAllIds_.end());
std::ranges::sort(blockVelocityAllIds_);
auto last = std::unique(blockVelocityAllIds_.begin(), blockVelocityAllIds_.end());
blockVelocityAllIds_.erase(last, blockVelocityAllIds_.end());
}
@@ -162,7 +162,7 @@ FlowsContainer(const Schedule& schedule,
}
}
}
std::sort(blockFlowsAllIds_.begin(), blockFlowsAllIds_.end());
std::ranges::sort(blockFlowsAllIds_);
auto last = std::unique(blockFlowsAllIds_.begin(), blockFlowsAllIds_.end());
blockFlowsAllIds_.erase(last, blockFlowsAllIds_.end());
}
@@ -272,7 +272,7 @@ doLoadBalance_(const Dune::EdgeWeightMethod edgeWeightsMethod,
}
}
}
std::sort(parallelWells.begin(), parallelWells.end());
std::ranges::sort(parallelWells);
// Calling Schedule::filterConnections would remove any perforated
// cells that exist only on other ranks even in the case of
+1 -1
View File
@@ -65,7 +65,7 @@ void logUniqueFailedCells(const std::string& messageTag,
}
std::vector<int> sorted(cells);
std::sort(sorted.begin(), sorted.end());
std::ranges::sort(sorted);
auto u = std::unique(sorted.begin(), sorted.end());
const auto numFailed = static_cast<std::size_t>
+1 -1
View File
@@ -37,7 +37,7 @@ namespace {
{
auto regs = regionNames;
std::sort(regs.begin(), regs.end());
std::ranges::sort(regs);
return { regs.begin(), std::unique(regs.begin(), regs.end()) };
}
+2 -2
View File
@@ -213,8 +213,8 @@ void SimulatorSerializer::checkSerializedCmdLine(const std::string& current,
auto curr_strings = split_string(current, '\n');
auto stored_strings = split_string(stored, '\n');
std::sort(curr_strings.begin(), curr_strings.end());
std::sort(stored_strings.begin(), stored_strings.end());
std::ranges::sort(curr_strings);
std::ranges::sort(stored_strings);
curr_strings = filter_strings(curr_strings);
stored_strings = filter_strings(stored_strings);
@@ -2423,7 +2423,7 @@ equilibrateTiltedFaultBlock(const CellRange& cells,
return std::atan2(a[1] - cy, a[0] - cx) < std::atan2(b[1] - cy, b[0] - cx);
};
std::sort(intersectionPoints.begin(), intersectionPoints.end(), angleCompare);
std::ranges::sort(intersectionPoints, angleCompare);
return polygonArea(intersectionPoints);
+1 -1
View File
@@ -262,7 +262,7 @@ void ZoltanPartitioner::connectNeighbors(std::vector<int>& cells,
// Sort final result for faster processing in later steps
cells.assign(visited.begin(), visited.end());
std::sort(cells.begin(), cells.end());
std::ranges::sort(cells);
}
template <class GridView, class Element>
@@ -106,13 +106,9 @@ createMasterGroupNameOrder_()
}
}
// Sort the vector based on the slave group names in lexicographical order
std::sort(
slave_group_names.begin(),
slave_group_names.end(),
[](const auto &lhs, const auto &rhs) {
return lhs.second < rhs.second;
}
);
std::ranges::sort(slave_group_names,
[](const auto &lhs, const auto &rhs)
{ return lhs.second < rhs.second; });
// Create a map from the master group name to the index in the vector
// This is used to determine the order in which the slaves will send the potentials
std::map<std::string, std::size_t> master_group_map;
+1 -1
View File
@@ -62,7 +62,7 @@ std::vector<int> HDF5Serializer::reportSteps() const
std::ranges::transform(entries, result.begin(),
[](const std::string& input)
{ return std::atoi(input.c_str()); });
std::sort(result.begin(), result.end());
std::ranges::sort(result);
return result;
}
@@ -589,12 +589,10 @@ sortedPointIndices(const ViolationSample& violation,
std::iota(sortedIdxs.begin(), sortedIdxs.end(), std::size_t{0});
std::sort(sortedIdxs.begin(), sortedIdxs.end(),
[pointIDs = violation.pointID.data() + (checkIx * this->numSamplePoints_)]
(const std::size_t i1, const std::size_t i2)
{
return pointIDs[i1] < pointIDs[i2];
});
std::ranges::sort(sortedIdxs,
[pointIDs = violation.pointID.data() + (checkIx * this->numSamplePoints_)]
(const std::size_t i1, const std::size_t i2)
{ return pointIDs[i1] < pointIDs[i2]; });
return sortedIdxs;
}
@@ -1784,8 +1784,7 @@ getMaxWellConnections() const
}
// also include wells with no perforations in case
std::sort(compressed_well_perforations.begin(),
compressed_well_perforations.end());
std::ranges::sort(compressed_well_perforations);
}
return wellConnections;
+1 -1
View File
@@ -768,7 +768,7 @@ sortGradients_(std::vector<GradPair>& grads)
auto cmp = [](GradPair a, GradPair b) {
return a.second < b.second;
};
std::sort(grads.begin(), grads.end(), cmp);
std::ranges::sort(grads, cmp);
}
template<typename Scalar, typename IndexTraits>
@@ -348,7 +348,7 @@ defineReservoirCommunication()
std::tie(sourceCells, std::ignore) =
allGatherv(this->calculators_.allWBPCells(), this->reservoirSrc_.comm());
std::sort(sourceCells.begin(), sourceCells.end());
std::ranges::sort(sourceCells);
auto u = std::unique(sourceCells.begin(), sourceCells.end());
this->reservoirSrc_.buildStructure({sourceCells.begin(), u});
+6 -4
View File
@@ -109,8 +109,9 @@ GlobalPerfContainerFactory(const IndexSet& local_indices,
int count = 0;
std::for_each(global_pairs.begin(), global_pairs.end(), [&count](Pair& pair){ pair.second = count++;});
// sort the complete range to get the correct ordering
std::sort(global_pairs.begin(), global_pairs.end(),
[](const Pair& p1, const Pair& p2){ return p1.first < p2.first; } );
std::ranges::sort(global_pairs,
[](const Pair& p1, const Pair& p2)
{ return p1.first < p2.first; });
map_received_.resize(global_pairs.size());
std::ranges::transform(global_pairs, map_received_.begin(),
[](const Pair& pair){ return pair.second; });
@@ -348,8 +349,9 @@ void CommunicateAboveBelow<Scalar>::partialSumPerfValues(RAIterator begin, RAIte
std::vector<Pair> global_pairs(displ.back());
comm_.allgatherv(my_pairs.data(), my_pairs.size(), global_pairs.data(), sizes.data(), displ.data());
// sort the complete range to get the correct ordering
std::sort(global_pairs.begin(), global_pairs.end(),
[](const Pair& p1, const Pair& p2){ return p1.first < p2.first; } );
std::ranges::sort(global_pairs,
[](const Pair& p1, const Pair& p2)
{ return p1.first < p2.first; });
std::vector<Value> sums(global_pairs.size());
std::ranges::transform(global_pairs, sums.begin(),
[](const Pair& p) { return p.second; });