Skip to content

Commit

Permalink
refactor: update to_array (acts-project#3600)
Browse files Browse the repository at this point in the history
"This can be abandoned with C++20 to use the `std::to_array` method"
I think this is not true, since `std::array` doesn't work with dynamically sized vectors.
  • Loading branch information
AJPfleger authored Sep 10, 2024
1 parent c836ca5 commit 415b4e0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
31 changes: 17 additions & 14 deletions Core/include/Acts/Utilities/Helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Acts/Definitions/Algebra.hpp"

#include <algorithm>
#include <array>
#include <iostream>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -71,22 +72,24 @@ std::vector<const T*> unpack_shared_const_vector(
return rawPtrs;
}

/// This can be abandoned with C++20 to use the std::to_array method
/// @brief Converts a vector to a fixed-size array with truncating or padding.
///
/// @note only the first kDIM elements will obviously be filled, if the
/// vector tends to be longer, it is truncated
/// This function copies elements from the input vector into a fixed-size array.
/// If the vector contains more than `kDIM` elements, the array is truncated to
/// fit. If the vector contains fewer elements than `kDIM`, the remaining array
/// elements are value-initialized (default-initialized, i.e., filled with zero
/// or default values).
///
/// @param vecvals the vector of bound values to be converted
/// @return an array with the filled values
template <std::size_t kDIM, typename value_type>
std::array<value_type, kDIM> to_array(const std::vector<value_type>& vecvals) {
std::array<value_type, kDIM> rarray = {};
for (const auto [iv, v] : enumerate(vecvals)) {
if (iv < kDIM) {
rarray[iv] = v;
}
}
return rarray;
/// @tparam kDIM The size of the resulting array.
/// @tparam value_t The type of elements in the vector and the array.
/// @param vecvals The input vector to be converted to an array.
///
/// @return An array containing the first `kDIM` elements of the vector.
template <std::size_t kDIM, typename value_t>
std::array<value_t, kDIM> toArray(const std::vector<value_t>& vecvals) {
std::array<value_t, kDIM> arr = {};
std::copy_n(vecvals.begin(), std::min(vecvals.size(), kDIM), arr.begin());
return arr;
}

/// @brief Dispatch a call based on a runtime value on a function taking the
Expand Down
12 changes: 6 additions & 6 deletions Core/src/Detector/VolumeStructureBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
"object. It needs at least 5 parameters, while " +
std::to_string(boundValues.size()) + " where given");
}
auto bArray = to_array<ConeVolumeBounds::BoundValues::eSize, ActsScalar>(
auto bArray = toArray<ConeVolumeBounds::BoundValues::eSize, ActsScalar>(
boundValues);
volumeBounds = std::make_unique<ConeVolumeBounds>(bArray);
} break;
Expand Down Expand Up @@ -94,7 +94,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
std::to_string(boundValues.size()) + " where given");
}
auto bArray =
to_array<CuboidVolumeBounds::BoundValues::eSize>(boundValues);
toArray<CuboidVolumeBounds::BoundValues::eSize>(boundValues);
volumeBounds = std::make_unique<CuboidVolumeBounds>(bArray);
} break;
case VolumeBounds::BoundsType::eCutoutCylinder: {
Expand All @@ -108,7 +108,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
std::to_string(boundValues.size()) + " where given");
}
auto bArray =
to_array<CutoutCylinderVolumeBounds::BoundValues::eSize>(boundValues);
toArray<CutoutCylinderVolumeBounds::BoundValues::eSize>(boundValues);
volumeBounds = std::make_unique<CutoutCylinderVolumeBounds>(bArray);
} break;
case VolumeBounds::BoundsType::eCylinder: {
Expand Down Expand Up @@ -150,7 +150,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
<< boundValues[2] << ", " << boundValues[3] << ", "
<< boundValues[4]);
auto bArray =
to_array<CylinderVolumeBounds::BoundValues::eSize>(boundValues);
toArray<CylinderVolumeBounds::BoundValues::eSize>(boundValues);
volumeBounds = std::make_unique<CylinderVolumeBounds>(bArray);
} break;
case VolumeBounds::BoundsType::eGenericCuboid: {
Expand All @@ -164,7 +164,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
std::to_string(boundValues.size()) + " where given");
}
auto bArray =
to_array<GenericCuboidVolumeBounds::BoundValues::eSize>(boundValues);
toArray<GenericCuboidVolumeBounds::BoundValues::eSize>(boundValues);
volumeBounds = std::make_unique<GenericCuboidVolumeBounds>(bArray);
} break;
case VolumeBounds::BoundsType::eTrapezoid: {
Expand All @@ -178,7 +178,7 @@ Acts::Experimental::VolumeStructureBuilder::construct(
std::to_string(boundValues.size()) + " where given");
}
auto bArray =
to_array<TrapezoidVolumeBounds::BoundValues::eSize>(boundValues);
toArray<TrapezoidVolumeBounds::BoundValues::eSize>(boundValues);
volumeBounds = std::make_unique<TrapezoidVolumeBounds>(bArray);
} break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ BOOST_AUTO_TEST_CASE(bounding_box_creation) {
BOOST_CHECK_EQUAL(boundValues.size(), 24u);

auto bValueArrray =
to_array<GenericCuboidVolumeBounds::BoundValues::eSize, ActsScalar>(
toArray<GenericCuboidVolumeBounds::BoundValues::eSize, ActsScalar>(
boundValues);
GenericCuboidVolumeBounds gcvbCopy(bValueArrray);
BOOST_CHECK_EQUAL(gcvbCopy.values().size(), 24u);
Expand Down

0 comments on commit 415b4e0

Please sign in to comment.