Skip to content

Commit b8b85af

Browse files
authored
chore: consistent use of std::ranges::for_each (acts-project#4319)
Move consistently from `std::for_each` to `std::ranges::for_each`. Will reduce SonarCloud issues,.
1 parent ad8e2a9 commit b8b85af

23 files changed

Lines changed: 145 additions & 130 deletions

Core/include/Acts/Clusterization/Clusterization.ipp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <algorithm>
1414
#include <array>
15+
#include <ranges>
1516
#include <vector>
1617

1718
#include <boost/pending/disjoint_sets.hpp>
@@ -89,7 +90,7 @@ template <std::size_t BufSize>
8990
struct ConnectionsBase {
9091
std::size_t nconn{0};
9192
std::array<Label, BufSize> buf;
92-
ConnectionsBase() { std::fill(buf.begin(), buf.end(), NO_LABEL); }
93+
ConnectionsBase() { std::ranges::fill(buf, NO_LABEL); }
9394
};
9495

9596
template <std::size_t GridDim>

Core/include/Acts/Detector/DetectorVolume.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ struct IndexedSurfacesExtractor {
586586
// The extracted surfaces
587587
std::vector<const Surface*> eSurfaces;
588588
eSurfaces.reserve(indices.size());
589-
std::for_each(indices.begin(), indices.end(),
590-
[&](const auto& i) { eSurfaces.push_back(surfaces[i]); });
589+
std::ranges::for_each(
590+
indices, [&](const auto& i) { eSurfaces.push_back(surfaces[i]); });
591591
return eSurfaces;
592592
}
593593
};
@@ -634,8 +634,8 @@ struct IndexedSubVolumesExtractor {
634634
// The extracted volumes
635635
std::vector<const DetectorVolume*> eVolumes;
636636
eVolumes.reserve(indices.size());
637-
std::for_each(indices.begin(), indices.end(),
638-
[&](const auto& i) { eVolumes.push_back(volumes[i]); });
637+
std::ranges::for_each(
638+
indices, [&](const auto& i) { eVolumes.push_back(volumes[i]); });
639639
return eVolumes;
640640
}
641641
};

Core/include/Acts/Detector/GeometryIdMapper.hpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Acts/Surfaces/Surface.hpp"
1616

1717
#include <map>
18+
#include <ranges>
1819

1920
namespace Acts::Experimental {
2021

@@ -85,16 +86,19 @@ class GeometryIdMapper final : public IGeometryIdGenerator {
8586
}
8687

8788
// Portals
88-
std::for_each(dVolume.portalPtrs().begin(), dVolume.portalPtrs().end(),
89-
[&](auto& portal) { assignGeometryId(cache, *portal); });
89+
std::ranges::for_each(dVolume.portalPtrs(), [&](auto& portal) {
90+
assignGeometryId(cache, *portal);
91+
});
9092

9193
// Surfaces
92-
std::for_each(dVolume.surfacePtrs().begin(), dVolume.surfacePtrs().end(),
93-
[&](auto& surface) { assignGeometryId(cache, *surface); });
94+
std::ranges::for_each(dVolume.surfacePtrs(), [&](auto& surface) {
95+
assignGeometryId(cache, *surface);
96+
});
9497

9598
// Sub volumes
96-
std::for_each(dVolume.volumePtrs().begin(), dVolume.volumePtrs().end(),
97-
[&](auto& volume) { assignGeometryId(cache, *volume); });
99+
std::ranges::for_each(dVolume.volumePtrs(), [&](auto& volume) {
100+
assignGeometryId(cache, *volume);
101+
});
98102
}
99103

100104
/// @brief Method for assigning a geometry id to a portal

Core/include/Acts/Detector/KdtSurfacesProvider.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "Acts/Utilities/KDTree.hpp"
1717

1818
#include <array>
19+
#include <ranges>
1920
#include <stdexcept>
2021
#include <tuple>
2122
#include <vector>
@@ -90,8 +91,8 @@ class KdtSurfaces {
9091
// Strip the surfaces
9192
std::vector<std::shared_ptr<Surface>> surfacePtrs;
9293
auto surfaceQuery = m_kdt->rangeSearchWithKey(range);
93-
std::for_each(surfaceQuery.begin(), surfaceQuery.end(),
94-
[&](auto& surf) { surfacePtrs.push_back(surf.second); });
94+
std::ranges::for_each(
95+
surfaceQuery, [&](auto& surf) { surfacePtrs.push_back(surf.second); });
9596
return surfacePtrs;
9697
}
9798

@@ -150,7 +151,7 @@ class KdtSurfaces {
150151
std::transform(c.begin(), c.end(), q.begin(), c.begin(),
151152
std::plus<double>());
152153
}
153-
std::for_each(c.begin(), c.end(), [&](auto& v) { v *= weight; });
154+
std::ranges::for_each(c, [&](auto& v) { v *= weight; });
154155
return c;
155156
}
156157
};

Core/include/Acts/Detector/detail/ReferenceGenerators.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Acts/Surfaces/Surface.hpp"
1515
#include "Acts/Utilities/BinningData.hpp"
1616

17+
#include <ranges>
1718
#include <vector>
1819

1920
namespace Acts::Experimental::detail {
@@ -83,8 +84,7 @@ struct PolyhedronReferenceGenerator {
8384
// Add the barycenter if configured
8485
if constexpr (aBARY) {
8586
Vector3 bc(0., 0., 0.);
86-
std::for_each(rPositions.begin(), rPositions.end(),
87-
[&](const auto& p) { bc += p; });
87+
std::ranges::for_each(rPositions, [&](const auto& p) { bc += p; });
8888
bc *= 1. / rPositions.size();
8989
rPositions.push_back(bc);
9090
}

Core/include/Acts/Navigation/NavigationStateFillers.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Acts/Surfaces/BoundaryTolerance.hpp"
1515
#include "Acts/Utilities/Intersection.hpp"
1616

17+
#include <ranges>
1718
#include <vector>
1819

1920
namespace Acts {
@@ -49,7 +50,7 @@ struct SurfacesFiller {
4950
/// @param surfaces the surfaces that are filled in
5051
inline static void fill(NavigationState& nState,
5152
const std::vector<const Surface*>& surfaces) {
52-
std::for_each(surfaces.begin(), surfaces.end(), [&](const auto& s) {
53+
std::ranges::for_each(surfaces, [&](const auto& s) {
5354
nState.surfaceCandidates.push_back(NavigationState::SurfaceCandidate{
5455
ObjectIntersection<Surface>::invalid(), s, nullptr,
5556
nState.surfaceBoundaryTolerance});
@@ -66,7 +67,7 @@ struct PortalsFiller {
6667
/// @param portals the portals that are filled in
6768
inline static void fill(NavigationState& nState,
6869
const std::vector<const Portal*>& portals) {
69-
std::for_each(portals.begin(), portals.end(), [&](const auto& p) {
70+
std::ranges::for_each(portals, [&](const auto& p) {
7071
nState.surfaceCandidates.push_back(NavigationState::SurfaceCandidate{
7172
ObjectIntersection<Surface>::invalid(), nullptr, p,
7273
BoundaryTolerance::None()});

Core/src/Detector/CuboidalContainerBuilder.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include <algorithm>
2020
#include <ostream>
21+
#include <ranges>
2122
#include <stdexcept>
2223
#include <utility>
2324

@@ -128,17 +129,16 @@ Acts::Experimental::CuboidalContainerBuilder::construct(
128129
std::vector<DetectorComponent::PortalContainer> containers;
129130
std::vector<std::shared_ptr<DetectorVolume>> rootVolumes;
130131
// Run through the builders
131-
std::for_each(
132-
m_cfg.builders.begin(), m_cfg.builders.end(), [&](const auto& builder) {
133-
auto [cVolumes, cContainer, cRoots] = builder->construct(gctx);
134-
atNavigationLevel = (atNavigationLevel && cVolumes.size() == 1u);
135-
ACTS_VERBOSE("Number of volumes: " << cVolumes.size());
136-
// Collect individual components, volumes, containers, roots
137-
volumes.insert(volumes.end(), cVolumes.begin(), cVolumes.end());
138-
containers.push_back(cContainer);
139-
rootVolumes.insert(rootVolumes.end(), cRoots.volumes.begin(),
140-
cRoots.volumes.end());
141-
});
132+
std::ranges::for_each(m_cfg.builders, [&](const auto& builder) {
133+
auto [cVolumes, cContainer, cRoots] = builder->construct(gctx);
134+
atNavigationLevel = (atNavigationLevel && cVolumes.size() == 1u);
135+
ACTS_VERBOSE("Number of volumes: " << cVolumes.size());
136+
// Collect individual components, volumes, containers, roots
137+
volumes.insert(volumes.end(), cVolumes.begin(), cVolumes.end());
138+
containers.push_back(cContainer);
139+
rootVolumes.insert(rootVolumes.end(), cRoots.volumes.begin(),
140+
cRoots.volumes.end());
141+
});
142142
// Navigation level detected, connect volumes (cleaner and faster than
143143
// connect containers)
144144
if (atNavigationLevel) {
@@ -161,12 +161,12 @@ Acts::Experimental::CuboidalContainerBuilder::construct(
161161
ACTS_DEBUG("Assigning geometry ids to the detector");
162162
auto cache = m_cfg.geoIdGenerator->generateCache();
163163
if (m_cfg.geoIdReverseGen) {
164-
std::for_each(rootVolumes.rbegin(), rootVolumes.rend(), [&](auto& v) {
164+
std::ranges::for_each(rootVolumes, [&](auto& v) {
165165
m_cfg.geoIdGenerator->assignGeometryId(cache, *v);
166166
ACTS_VERBOSE("-> Assigning geometry id to volume " << v->name());
167167
});
168168
} else {
169-
std::for_each(rootVolumes.begin(), rootVolumes.end(), [&](auto& v) {
169+
std::ranges::for_each(rootVolumes, [&](auto& v) {
170170
m_cfg.geoIdGenerator->assignGeometryId(cache, *v);
171171
ACTS_VERBOSE("-> Assigning geometry id to volume " << v->name());
172172
});

Core/src/Detector/CylindricalContainerBuilder.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <algorithm>
2222
#include <ostream>
23+
#include <ranges>
2324
#include <stdexcept>
2425
#include <utility>
2526

@@ -215,16 +216,15 @@ Acts::Experimental::CylindricalContainerBuilder::construct(
215216
std::vector<DetectorComponent::PortalContainer> containers;
216217
std::vector<std::shared_ptr<DetectorVolume>> rootVolumes;
217218
// Run through the builders
218-
std::for_each(
219-
m_cfg.builders.begin(), m_cfg.builders.end(), [&](const auto& builder) {
220-
auto [cVolumes, cContainer, cRoots] = builder->construct(gctx);
221-
atNavigationLevel = (atNavigationLevel && cVolumes.size() == 1u);
222-
// Collect individual components, volumes, containers, roots
223-
volumes.insert(volumes.end(), cVolumes.begin(), cVolumes.end());
224-
containers.push_back(cContainer);
225-
rootVolumes.insert(rootVolumes.end(), cRoots.volumes.begin(),
226-
cRoots.volumes.end());
227-
});
219+
std::ranges::for_each(m_cfg.builders, [&](const auto& builder) {
220+
auto [cVolumes, cContainer, cRoots] = builder->construct(gctx);
221+
atNavigationLevel = (atNavigationLevel && cVolumes.size() == 1u);
222+
// Collect individual components, volumes, containers, roots
223+
volumes.insert(volumes.end(), cVolumes.begin(), cVolumes.end());
224+
containers.push_back(cContainer);
225+
rootVolumes.insert(rootVolumes.end(), cRoots.volumes.begin(),
226+
cRoots.volumes.end());
227+
});
228228
// Navigation level detected, connect volumes (cleaner and faster than
229229
// connect containers)
230230
if (atNavigationLevel) {
@@ -250,7 +250,7 @@ Acts::Experimental::CylindricalContainerBuilder::construct(
250250
ACTS_VERBOSE("-> Assigning geometry id to volume " << v->name());
251251
});
252252
} else {
253-
std::for_each(rootVolumes.begin(), rootVolumes.end(), [&](auto& v) {
253+
std::ranges::for_each(rootVolumes, [&](auto& v) {
254254
m_cfg.geoIdGenerator->assignGeometryId(cache, *v);
255255
ACTS_VERBOSE("-> Assigning geometry id to volume " << v->name());
256256
});

Core/src/Detector/DetectorBuilder.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "Acts/Material/IMaterialDecorator.hpp"
1515
#include "Acts/Navigation/DetectorVolumeFinders.hpp"
1616

17+
#include <algorithm>
1718
#include <stdexcept>
1819

1920
Acts::Experimental::DetectorBuilder::DetectorBuilder(
@@ -41,7 +42,7 @@ Acts::Experimental::DetectorBuilder::construct(
4142
if (m_cfg.geoIdGenerator != nullptr) {
4243
ACTS_DEBUG("Assigning geometry ids to the detector");
4344
auto cache = m_cfg.geoIdGenerator->generateCache();
44-
std::for_each(roots.volumes.begin(), roots.volumes.end(), [&](auto& v) {
45+
std::ranges::for_each(roots.volumes, [&](auto& v) {
4546
ACTS_VERBOSE("-> Assigning geometry id to volume " << v->name());
4647
m_cfg.geoIdGenerator->assignGeometryId(cache, *v);
4748
});
@@ -50,7 +51,7 @@ Acts::Experimental::DetectorBuilder::construct(
5051
// Decorate the volumes with material - Surface material only at this moment
5152
if (m_cfg.materialDecorator != nullptr) {
5253
ACTS_DEBUG("Decorating the detector with material");
53-
std::for_each(volumes.begin(), volumes.end(), [&](auto& v) {
54+
std::ranges::for_each(volumes, [&](auto& v) {
5455
// Assign to surfaces
5556
for (auto& sf : v->surfacePtrs()) {
5657
m_cfg.materialDecorator->decorate(*sf);

Core/src/Detector/GeometryIdGenerator.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ void Acts::Experimental::GeometryIdGenerator::assignGeometryId(
3131
}
3232

3333
// Portals
34-
std::for_each(dVolume.portalPtrs().begin(), dVolume.portalPtrs().end(),
35-
[&](auto& portal) { assignGeometryId(cache, *portal); });
34+
std::ranges::for_each(dVolume.portalPtrs(), [&](auto& portal) {
35+
assignGeometryId(cache, *portal);
36+
});
3637

3738
// Surfaces
38-
std::for_each(dVolume.surfacePtrs().begin(), dVolume.surfacePtrs().end(),
39-
[&](auto& surface) { assignGeometryId(cache, *surface); });
39+
std::ranges::for_each(dVolume.surfacePtrs(), [&](auto& surface) {
40+
assignGeometryId(cache, *surface);
41+
});
4042

4143
if (m_cfg.resetSubCounters) {
4244
ccache.portalCount = 0u;
@@ -45,8 +47,9 @@ void Acts::Experimental::GeometryIdGenerator::assignGeometryId(
4547
}
4648

4749
// Sub volumes
48-
std::for_each(dVolume.volumePtrs().begin(), dVolume.volumePtrs().end(),
49-
[&](auto& volume) { assignGeometryId(cache, *volume); });
50+
std::ranges::for_each(dVolume.volumePtrs(), [&](auto& volume) {
51+
assignGeometryId(cache, *volume);
52+
});
5053
}
5154

5255
void Acts::Experimental::GeometryIdGenerator::assignGeometryId(

0 commit comments

Comments
 (0)