Skip to content

Commit

Permalink
refactor: use contains for maps and sets (#3670)
Browse files Browse the repository at this point in the history
I introduced also for `Core/include/Acts/Geometry/GeometryHierarchyMap.hpp` a contains method, as it would be expected from a map.
  • Loading branch information
AJPfleger authored Sep 30, 2024
1 parent 24063e9 commit f0dfbcd
Show file tree
Hide file tree
Showing 46 changed files with 108 additions and 113 deletions.
4 changes: 2 additions & 2 deletions Core/include/Acts/EventData/VectorMultiTrajectory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class VectorMultiTrajectoryBase {
case "typeFlags"_hash:
return true;
default:
return instance.m_dynamic.find(key) != instance.m_dynamic.end();
return instance.m_dynamic.contains(key);
}
}

Expand Down Expand Up @@ -287,7 +287,7 @@ class VectorMultiTrajectoryBase {
case "typeFlags"_hash:
return true;
default:
return instance.m_dynamic.find(key) != instance.m_dynamic.end();
return instance.m_dynamic.contains(key);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Core/include/Acts/EventData/VectorTrackContainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class VectorTrackContainerBase {
using namespace Acts::HashedStringLiteral;
switch (key) {
default:
return m_dynamic.find(key) != m_dynamic.end();
return m_dynamic.contains(key);
}
}

Expand Down
17 changes: 17 additions & 0 deletions Core/include/Acts/Geometry/GeometryHierarchyMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ class GeometryHierarchyMap {
/// @retval `.end()` iterator if no matching element exists
Iterator find(const GeometryIdentifier& id) const;

/// Check if the most specific value exists for a given geometry identifier.
///
/// This function checks if there is an element matching exactly the given
/// geometry id, or from the element for the next available higher level
/// within the geometry hierarchy.
///
/// @param id geometry identifier for which existence is being checked
/// @retval `true` if a matching element exists
/// @retval `false` if no matching element exists
bool contains(const GeometryIdentifier& id) const;

private:
// NOTE this class assumes that it knows the ordering of the levels within
// the geometry id. if the geometry id changes, this code has to be
Expand Down Expand Up @@ -303,4 +314,10 @@ inline auto GeometryHierarchyMap<value_t>::find(
return end();
}

template <typename value_t>
inline auto GeometryHierarchyMap<value_t>::contains(
const GeometryIdentifier& id) const -> bool {
return this->find(id) != this->end();
}

} // namespace Acts
8 changes: 4 additions & 4 deletions Core/include/Acts/Visualization/detail/ObjVisualization3D.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void ObjVisualization3D<T>::write(std::ostream& os, std::ostream& mos) const {
materialName += std::to_string(color[1]) + std::string("_");
materialName += std::to_string(color[2]);

if (materials.find(materialName) == materials.end()) {
if (!materials.contains(materialName)) {
mos << "newmtl " << materialName << "\n";
std::vector<std::string> shadings = {"Ka", "Kd", "Ks"};
for (const auto& shd : shadings) {
Expand All @@ -123,7 +123,7 @@ void ObjVisualization3D<T>::write(std::ostream& os, std::ostream& mos) const {
std::size_t iv = 0;
Color lastVertexColor = {0, 0, 0};
for (const VertexType& vtx : m_vertices) {
if (m_vertexColors.find(iv) != m_vertexColors.end()) {
if (m_vertexColors.contains(iv)) {
auto color = m_vertexColors.find(iv)->second;
if (color != lastVertexColor) {
os << mixColor(color) << "\n";
Expand All @@ -139,7 +139,7 @@ void ObjVisualization3D<T>::write(std::ostream& os, std::ostream& mos) const {
std::size_t il = 0;
Color lastLineColor = {0, 0, 0};
for (const LineType& ln : m_lines) {
if (m_lineColors.find(il) != m_lineColors.end()) {
if (m_lineColors.contains(il)) {
auto color = m_lineColors.find(il)->second;
if (color != lastLineColor) {
os << mixColor(color) << "\n";
Expand All @@ -152,7 +152,7 @@ void ObjVisualization3D<T>::write(std::ostream& os, std::ostream& mos) const {
std::size_t is = 0;
Color lastFaceColor = {0, 0, 0};
for (const FaceType& fc : m_faces) {
if (m_faceColors.find(is) != m_faceColors.end()) {
if (m_faceColors.contains(is)) {
auto color = m_faceColors.find(is)->second;
if (color != lastFaceColor) {
os << mixColor(color) << "\n";
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Detector/CylindricalContainerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Acts::Experimental::CylindricalContainerBuilder::construct(
// Assign the proto material
// Material assignment from configuration
for (const auto& [ip, bDescription] : m_cfg.portalMaterialBinning) {
if (portalContainer.find(ip) != portalContainer.end()) {
if (portalContainer.contains(ip)) {
auto bd = detail::ProtoMaterialHelper::attachProtoMaterial(
gctx, portalContainer[ip]->surface(), bDescription);
ACTS_VERBOSE("-> Assigning proto material to portal " << ip << " with "
Expand Down
6 changes: 3 additions & 3 deletions Core/src/Detector/Detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Acts::Experimental::Detector::Detector(
v->closePortals();
// Store the name
const std::string vName = v->name();
if (m_volumeNameIndex.find(vName) != m_volumeNameIndex.end()) {
if (m_volumeNameIndex.contains(vName)) {
throw std::invalid_argument("Detector: duplicate volume name " + vName +
" detected.");
}
Expand All @@ -79,7 +79,7 @@ Acts::Experimental::Detector::Detector(
"' with undefined geometry id detected" +
". Make sure a GeometryIdGenerator is used.");
}
if (volumeGeoIdMap.find(vgeoID) != volumeGeoIdMap.end()) {
if (volumeGeoIdMap.contains(vgeoID)) {
std::stringstream ss;
ss << vgeoID;
throw std::invalid_argument("Detector: duplicate volume geometry id '" +
Expand All @@ -104,7 +104,7 @@ Acts::Experimental::Detector::Detector(
}
// ---------------------------------------------------------------

if (surfaceGeoIdMap.find(sgeoID) != surfaceGeoIdMap.end()) {
if (surfaceGeoIdMap.contains(sgeoID)) {
std::stringstream ss;
ss << sgeoID;
throw std::invalid_argument(
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Detector/DetectorVolumeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Acts::Experimental::DetectorVolumeBuilder::construct(

// Assign the proto material if configured to do so
for (const auto& [ip, bDescription] : m_cfg.portalMaterialBinning) {
if (portalContainer.find(ip) != portalContainer.end()) {
if (portalContainer.contains(ip)) {
auto bd = detail::ProtoMaterialHelper::attachProtoMaterial(
gctx, portalContainer[ip]->surface(), bDescription);
ACTS_VERBOSE("-> Assigning proto material to portal " << ip << " with "
Expand Down
11 changes: 4 additions & 7 deletions Core/src/Detector/detail/CuboidalDetectorHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,12 @@ Acts::Experimental::detail::CuboidalDetectorHelper::connect(
auto& formerContainer = containers[ic - 1];
auto& currentContainer = containers[ic];
// Check and throw exception
if (formerContainer.find(startIndex) == formerContainer.end()) {
if (!formerContainer.contains(startIndex)) {
throw std::invalid_argument(
"CuboidalDetectorHelper: proto container has no fuse portal at index "
"of former container.");
}
if (currentContainer.find(endIndex) == currentContainer.end()) {
if (!currentContainer.contains(endIndex)) {
throw std::invalid_argument(
"CuboidalDetectorHelper: proto container has no fuse portal at index "
"of current container.");
Expand Down Expand Up @@ -343,11 +343,8 @@ Acts::Experimental::detail::CuboidalDetectorHelper::xyzBoundaries(
auto fillMap = [&](std::map<ActsScalar, std::size_t>& map,
const std::array<ActsScalar, 2u>& values) {
for (auto v : values) {
if (map.find(v) != map.end()) {
++map[v];
} else {
map[v] = 1u;
}
// This will insert v with a value of 0 if it doesn't exist
++map[v];
}
};

Expand Down
22 changes: 10 additions & 12 deletions Core/src/Detector/detail/CylindricalDetectorHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -860,16 +860,14 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInR(
auto& formerContainer = containers[ic - 1];
auto& currentContainer = containers[ic];
// Check and throw exception
if (formerContainer.find(2u) == formerContainer.end()) {
if (!formerContainer.contains(2u)) {
throw std::invalid_argument(
"CylindricalDetectorHelper: proto container has no outer cover, "
"can "
"CylindricalDetectorHelper: proto container has no outer cover, can "
"not be connected in R");
}
if (currentContainer.find(3u) == currentContainer.end()) {
if (!currentContainer.contains(3u)) {
throw std::invalid_argument(
"CylindricalDetectorHelper: proto container has no inner cover, "
"can "
"CylindricalDetectorHelper: proto container has no inner cover, can "
"not be connected in R");
}

Expand Down Expand Up @@ -897,7 +895,7 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInR(
}

// Proto container refurbishment
if (containers[0u].find(3u) != containers[0u].end()) {
if (containers[0u].contains(3u)) {
dShell[3u] = containers[0u].find(3u)->second;
}
dShell[2u] = containers[containers.size() - 1u].find(2u)->second;
Expand All @@ -907,7 +905,7 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInR(

for (auto [s, volumes] : sideVolumes) {
auto pR = connectInR(gctx, volumes, {s});
if (pR.find(s) != pR.end()) {
if (pR.contains(s)) {
dShell[s] = pR.find(s)->second;
}
}
Expand All @@ -934,12 +932,12 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInZ(
auto& formerContainer = containers[ic - 1];
auto& currentContainer = containers[ic];
// Check and throw exception
if (formerContainer.find(1u) == formerContainer.end()) {
if (!formerContainer.contains(1u)) {
throw std::invalid_argument(
"CylindricalDetectorHelper: proto container has no negative disc, "
"can not be connected in Z");
}
if (currentContainer.find(0u) == currentContainer.end()) {
if (!currentContainer.contains(0u)) {
throw std::invalid_argument(
"CylindricalDetectorHelper: proto container has no positive disc, "
"can not be connected in Z");
Expand Down Expand Up @@ -972,7 +970,7 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInZ(

// Check if this is a tube or a cylinder container (check done on 1st)
std::vector<unsigned int> nominalSides = {2u, 4u, 5u};
if (containers[0u].find(3u) != containers[0u].end()) {
if (containers[0u].contains(3u)) {
nominalSides.push_back(3u);
}

Expand All @@ -985,7 +983,7 @@ Acts::Experimental::detail::CylindricalDetectorHelper::connectInZ(
for (auto [s, volumes] : sideVolumes) {
ACTS_VERBOSE(" - connect " << volumes.size() << " at selected side " << s);
auto pR = connectInZ(gctx, volumes, {s}, logLevel);
if (pR.find(s) != pR.end()) {
if (pR.contains(s)) {
dShell[s] = pR.find(s)->second;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Core/src/EventData/VectorTrackContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void VectorTrackContainer::copyDynamicFrom_impl(IndexType dstIdx,
void VectorTrackContainer::ensureDynamicColumns_impl(
const detail_vtc::VectorTrackContainerBase& other) {
for (auto& [key, value] : other.m_dynamic) {
if (m_dynamic.find(key) == m_dynamic.end()) {
if (!m_dynamic.contains(key)) {
m_dynamic[key] = value->clone(true);
}
}
Expand Down
8 changes: 3 additions & 5 deletions Core/src/Material/MaterialInteractionAssignment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ Acts::MaterialInteractionAssignment::assign(

// A local veta veto kicked in
GeometryIdentifier intersectionID = surface->geometryId();
if (options.localVetos.find(intersectionID) != options.localVetos.end()) {
if (options.localVetos.contains(intersectionID)) {
const auto& localVeto = *options.localVetos.find(intersectionID);
if (localVeto(materialInteraction, intersectedSurfaces[is])) {
unassignedMaterialInteractions.push_back(materialInteraction);
Expand All @@ -91,8 +91,7 @@ Acts::MaterialInteractionAssignment::assign(
assignedMaterialInteraction.intersectionID = intersectionID;
// Check for possible reassignment
if (is + 1u < intersectedSurfaces.size() &&
options.reAssignments.find(intersectionID) !=
options.reAssignments.end()) {
options.reAssignments.contains(intersectionID)) {
auto reAssignment = (*options.reAssignments.find(intersectionID));
reAssignment(assignedMaterialInteraction, intersectedSurfaces[is],
intersectedSurfaces[is + 1]);
Expand All @@ -110,8 +109,7 @@ Acts::MaterialInteractionAssignment::assign(
// (empty bin correction can use this information)
std::vector<IAssignmentFinder::SurfaceAssignment> surfacesWithoutAssignments;
for (const auto& intersectedSurface : intersectedSurfaces) {
if (assignedSurfaces.find(intersectedSurface.surface) ==
assignedSurfaces.end()) {
if (!assignedSurfaces.contains(intersectedSurface.surface)) {
surfacesWithoutAssignments.push_back(intersectedSurface);
}
}
Expand Down
6 changes: 2 additions & 4 deletions Core/src/Material/SurfaceMaterialMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,7 @@ void Acts::SurfaceMaterialMapper::mapInteraction(
// Now assign the material for the accumulation process
auto tBin = currentAccMaterial->second.accumulate(
currentPos, rmIter->materialSlab, currentPathCorrection);
if (touchedMapBins.find(&(currentAccMaterial->second)) ==
touchedMapBins.end()) {
if (!touchedMapBins.contains(&(currentAccMaterial->second))) {
touchedMapBins.insert(MapBin(&(currentAccMaterial->second), tBin));
}
if (m_cfg.computeVariance) {
Expand Down Expand Up @@ -483,8 +482,7 @@ void Acts::SurfaceMaterialMapper::mapSurfaceInteraction(
// Now assign the material for the accumulation process
auto tBin = currentAccMaterial->second.accumulate(
currentPos, rmIter->materialSlab, rmIter->pathCorrection);
if (touchedMapBins.find(&(currentAccMaterial->second)) ==
touchedMapBins.end()) {
if (!touchedMapBins.contains(&(currentAccMaterial->second))) {
touchedMapBins.insert(MapBin(&(currentAccMaterial->second), tBin));
}
if (m_cfg.computeVariance) {
Expand Down
3 changes: 1 addition & 2 deletions Core/src/TrackFinding/GbtsConnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,7 @@ GbtsConnector::GbtsConnector(std::ifstream &inFile) {
std::list<const GbtsConnection *>::iterator cIt = lConns.begin();

while (cIt != lConns.end()) {
if (zeroLayers.find((*cIt)->m_dst) !=
zeroLayers.end()) { // check if contains
if (zeroLayers.contains((*cIt)->m_dst)) {
theStage.push_back(*cIt);
cIt = lConns.erase(cIt);
continue;
Expand Down
4 changes: 2 additions & 2 deletions Core/src/Vertexing/AdaptiveGridTrackDensity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Result<double> AdaptiveGridTrackDensity::estimateSeedWidth(
bool binFilled = true;
while (gridValue > maxValue / 2) {
// Check if we are still operating on continuous z values
if (densityMap.count({rhmBin + 1, tMaxBin}) == 0) {
if (!densityMap.contains({rhmBin + 1, tMaxBin})) {
binFilled = false;
break;
}
Expand All @@ -308,7 +308,7 @@ Result<double> AdaptiveGridTrackDensity::estimateSeedWidth(
binFilled = true;
while (gridValue > maxValue / 2) {
// Check if we are still operating on continuous z values
if (densityMap.count({lhmBin - 1, tMaxBin}) == 0) {
if (!densityMap.contains({lhmBin - 1, tMaxBin})) {
binFilled = false;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Core/src/Vertexing/AdaptiveMultiVertexFitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Acts::Result<void> Acts::AdaptiveMultiVertexFitter::setAllVertexCompatibilities(
auto& trkAtVtx = state.tracksAtVerticesMap.at(std::make_pair(trk, vtx));
// Recover from cases where linearization point != 0 but
// more tracks were added later on
if (vtxInfo.impactParams3D.find(trk) == vtxInfo.impactParams3D.end()) {
if (!vtxInfo.impactParams3D.contains(trk)) {
auto res = m_cfg.ipEst.estimate3DImpactParameters(
vertexingOptions.geoContext, vertexingOptions.magFieldContext,
m_cfg.extractParameters(trk),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::vector<Acts::ActsScalar> ActsExamples::GeometricConfig::variances(
std::vector<Acts::ActsScalar> rVariances;
for (const auto& bIndex : indices) {
Acts::ActsScalar var = 0.;
if (varianceMap.find(bIndex) != varianceMap.end()) {
if (varianceMap.contains(bIndex)) {
// Try to find the variance for this cluster size
std::size_t lsize =
std::min(csizes[bIndex], varianceMap.at(bIndex).size());
Expand Down
2 changes: 1 addition & 1 deletion Examples/Algorithms/Geant4/src/Geant4Manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ std::shared_ptr<Geant4Handle> Geant4Manager::createHandle(

void Geant4Manager::registerPhysicsListFactory(
std::string name, std::shared_ptr<PhysicsListFactory> physicsListFactory) {
if (m_physicsListFactories.find(name) != m_physicsListFactories.end()) {
if (m_physicsListFactories.contains(name)) {
throw std::invalid_argument("name already mapped");
}
m_physicsListFactories.emplace(std::move(name),
Expand Down
2 changes: 1 addition & 1 deletion Examples/Algorithms/Geant4/src/MaterialSteppingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void ActsExamples::MaterialSteppingAction::UserSteppingAction(
G4Track* g4Track = step->GetTrack();
std::size_t trackID = g4Track->GetTrackID();
auto& materialTracks = eventStore().materialTracks;
if (materialTracks.find(trackID - 1) == materialTracks.end()) {
if (!materialTracks.contains(trackID - 1)) {
Acts::RecordedMaterialTrack rmTrack;
const auto& g4Vertex = g4Track->GetVertexPosition();
Acts::Vector3 vertex(g4Vertex[0], g4Vertex[1], g4Vertex[2]);
Expand Down
12 changes: 4 additions & 8 deletions Examples/Algorithms/Geant4/src/ParticleTrackingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,15 @@ void ActsExamples::ParticleTrackingAction::PostUserTrackingAction(
const G4Track* aTrack) {
// The initial particle maybe was not registered because a particle ID
// collision
if (eventStore().trackIdMapping.find(aTrack->GetTrackID()) ==
eventStore().trackIdMapping.end()) {
if (!eventStore().trackIdMapping.contains(aTrack->GetTrackID())) {
ACTS_WARNING("Particle ID for track ID " << aTrack->GetTrackID()
<< " not registered. Skip");
return;
}

const auto barcode = eventStore().trackIdMapping.at(aTrack->GetTrackID());

auto hasHits = eventStore().particleHitCount.find(barcode) !=
eventStore().particleHitCount.end() &&
auto hasHits = eventStore().particleHitCount.contains(barcode) &&
eventStore().particleHitCount.at(barcode) > 0;

if (!m_cfg.keepParticlesWithoutHits && !hasHits) {
Expand Down Expand Up @@ -146,13 +144,11 @@ ActsExamples::ParticleTrackingAction::makeParticleId(G4int trackId,
G4int parentId) const {
// We already have this particle registered (it is one of the input particles
// or we are making a final particle state)
if (eventStore().trackIdMapping.find(trackId) !=
eventStore().trackIdMapping.end()) {
if (eventStore().trackIdMapping.contains(trackId)) {
return std::nullopt;
}

if (eventStore().trackIdMapping.find(parentId) ==
eventStore().trackIdMapping.end()) {
if (!eventStore().trackIdMapping.contains(parentId)) {
ACTS_DEBUG("Parent particle " << parentId
<< " not registered, cannot build barcode");
eventStore().parentIdNotFound++;
Expand Down
Loading

0 comments on commit f0dfbcd

Please sign in to comment.