Skip to content

Commit

Permalink
chore: Replace legacy limits with std::numeric_limits or similar for …
Browse files Browse the repository at this point in the history
…consistency (acts-project#3298)
  • Loading branch information
AJPfleger authored and Matthew Harris committed Jun 18, 2024
1 parent f4d41f3 commit ac6f02e
Show file tree
Hide file tree
Showing 24 changed files with 126 additions and 80 deletions.
12 changes: 7 additions & 5 deletions Core/include/Acts/Material/MaterialComposition.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ class ElementFraction {
/// @param e is the atomic number of the element
/// @param f is the relative fraction and must be a value in [0,1]
constexpr ElementFraction(unsigned int e, float f)
: m_element(static_cast<uint8_t>(e)),
m_fraction(static_cast<uint8_t>(f * UINT8_MAX)) {
: m_element(static_cast<std::uint8_t>(e)),
m_fraction(static_cast<std::uint8_t>(
f * std::numeric_limits<std::uint8_t>::max())) {
assert((0u < e) && ("The atomic number must be positive"));
assert((0.0f <= f) && (f <= 1.0f) && "Relative fraction must be in [0,1]");
}
Expand All @@ -48,8 +49,8 @@ class ElementFraction {
/// @param e is the atomic number of the element
/// @param w is the integer weight and must be a value in [0,256)
constexpr explicit ElementFraction(unsigned int e, unsigned int w)
: m_element(static_cast<uint8_t>(e)),
m_fraction(static_cast<uint8_t>(w)) {
: m_element(static_cast<std::uint8_t>(e)),
m_fraction(static_cast<std::uint8_t>(w)) {
assert((0u < e) && ("The atomic number must be positive"));
assert((w < 256u) && "Integer weight must be in [0,256)");
}
Expand All @@ -66,7 +67,8 @@ class ElementFraction {
constexpr uint8_t element() const { return m_element; }
/// The relative fraction of this element.
constexpr float fraction() const {
return static_cast<float>(m_fraction) / UINT8_MAX;
return static_cast<float>(m_fraction) /
std::numeric_limits<std::uint8_t>::max();
}

private:
Expand Down
5 changes: 3 additions & 2 deletions Core/include/Acts/Surfaces/BoundaryCheck.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ inline Acts::BoundaryCheck::BoundaryCheck(bool check)
inline Acts::BoundaryCheck::BoundaryCheck(bool checkLocal0, bool checkLocal1,
double tolerance0, double tolerance1)
: m_weight(SquareMatrix2::Identity()),
m_tolerance(checkLocal0 ? tolerance0 : DBL_MAX,
checkLocal1 ? tolerance1 : DBL_MAX),
m_tolerance(
checkLocal0 ? tolerance0 : std::numeric_limits<double>::max(),
checkLocal1 ? tolerance1 : std::numeric_limits<double>::max()),
m_type(Type::eAbsolute) {}

inline Acts::BoundaryCheck::BoundaryCheck(const SquareMatrix2& localCovariance,
Expand Down
4 changes: 2 additions & 2 deletions Core/include/Acts/TrackFitting/GlobalChiSquareFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,13 @@ struct Gx2FitterResult {
// This is the index of the 'tip' of the track stored in multitrajectory.
// This corresponds to the last measurement state in the multitrajectory.
// Since this KF only stores one trajectory, it is unambiguous.
// SIZE_MAX is the start of a trajectory.
// Acts::MultiTrajectoryTraits::kInvalid is the start of a trajectory.
std::size_t lastMeasurementIndex = Acts::MultiTrajectoryTraits::kInvalid;

// This is the index of the 'tip' of the states stored in multitrajectory.
// This corresponds to the last state in the multitrajectory.
// Since this KF only stores one trajectory, it is unambiguous.
// SIZE_MAX is the start of a trajectory.
// Acts::MultiTrajectoryTraits::kInvalid is the start of a trajectory.
std::size_t lastTrackIndex = Acts::MultiTrajectoryTraits::kInvalid;

// The optional Parameters at the provided surface
Expand Down
11 changes: 6 additions & 5 deletions Core/include/Acts/TrackFitting/KalmanFitter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,14 @@ struct KalmanFitterResult {
/// This is the index of the 'tip' of the track stored in multitrajectory.
/// This corresponds to the last measurement state in the multitrajectory.
/// Since this KF only stores one trajectory, it is unambiguous.
/// SIZE_MAX is the start of a trajectory.
std::size_t lastMeasurementIndex = SIZE_MAX;
/// Acts::MultiTrajectoryTraits::kInvalid is the start of a trajectory.
std::size_t lastMeasurementIndex = Acts::MultiTrajectoryTraits::kInvalid;

/// This is the index of the 'tip' of the states stored in multitrajectory.
/// This corresponds to the last state in the multitrajectory.
/// Since this KF only stores one trajectory, it is unambiguous.
/// SIZE_MAX is the start of a trajectory.
std::size_t lastTrackIndex = SIZE_MAX;
/// Acts::MultiTrajectoryTraits::kInvalid is the start of a trajectory.
std::size_t lastTrackIndex = Acts::MultiTrajectoryTraits::kInvalid;

/// The optional Parameters at the provided surface
std::optional<BoundTrackParameters> fittedParameters;
Expand Down Expand Up @@ -528,7 +528,8 @@ class KalmanFitter {
const navigator_t& navigator,
result_type& result) const {
// Check if there is a measurement on track
if (result.lastMeasurementIndex == SIZE_MAX) {
if (result.lastMeasurementIndex ==
Acts::MultiTrajectoryTraits::kInvalid) {
ACTS_ERROR("No point to reverse for a track without measurements.");
return KalmanFitterError::ReverseNavigationFailed;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ globalTrackParametersCovariance(const traj_t& multiTraj,
using GainMatrix = CovMatrix;

// The last smoothed state index
std::size_t lastSmoothedIndex = SIZE_MAX;
std::size_t lastSmoothedIndex = Acts::MultiTrajectoryTraits::kInvalid;
// The total number of smoothed states
std::size_t nSmoothedStates = 0;
// Visit all the states
multiTraj.visitBackwards(entryIndex, [&](const auto& ts) {
if (ts.hasSmoothed()) {
if (lastSmoothedIndex == SIZE_MAX) {
if (lastSmoothedIndex == Acts::MultiTrajectoryTraits::kInvalid) {
lastSmoothedIndex = ts.index();
}
nSmoothedStates++;
Expand Down
3 changes: 2 additions & 1 deletion Core/include/Acts/Utilities/detail/Subspace.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ namespace Acts::detail {
/// @tparam kSize Size of the subspace
template <std::size_t kFullSize, std::size_t kSize>
class FixedSizeSubspace {
static_assert(kFullSize <= static_cast<std::size_t>(UINT8_MAX),
static_assert(kFullSize <= static_cast<std::size_t>(
std::numeric_limits<std::uint8_t>::max()),
"Full vector space size is larger than the supported range");
static_assert(1u <= kSize, "Subspace size must be at least 1");
static_assert(kSize <= kFullSize,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <array>
#include <cmath>
#include <cstdint>
#include <limits>
#include <ostream>
#include <set>
#include <stdexcept>
Expand Down Expand Up @@ -308,9 +309,9 @@ ActsExamples::DigitizationAlgorithm::localParameters(

Acts::ActsScalar totalWeight = 0.;
Acts::Vector2 m(0., 0.);
std::size_t b0min = SIZE_MAX;
std::size_t b0min = std::numeric_limits<std::size_t>::max();
std::size_t b0max = 0;
std::size_t b1min = SIZE_MAX;
std::size_t b1min = std::numeric_limits<std::size_t>::max();
std::size_t b1max = 0;
// Combine the channels
for (const auto& ch : channels) {
Expand Down
5 changes: 3 additions & 2 deletions Examples/Algorithms/Digitization/src/ModuleClusters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <memory>
#include <stdexcept>
#include <type_traits>
Expand Down Expand Up @@ -270,9 +271,9 @@ ModuleValue ModuleClusters::squash(std::vector<ModuleValue>& values) {
Acts::Vector2 pos(0., 0.);
Acts::Vector2 var(0., 0.);

std::size_t b0min = SIZE_MAX;
std::size_t b0min = std::numeric_limits<std::size_t>::max();
std::size_t b0max = 0;
std::size_t b1min = SIZE_MAX;
std::size_t b1min = std::numeric_limits<std::size_t>::max();
std::size_t b1max = 0;

for (std::size_t i = 0; i < values.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "ActsFatras/EventData/Barcode.hpp"
#include "ActsFatras/EventData/Particle.hpp"

#include <cstdint>
#include <limits>
#include <ostream>
#include <stdexcept>

Expand Down Expand Up @@ -43,7 +43,7 @@ std::string ActsExamples::EventGenerator::name() const {

std::pair<std::size_t, std::size_t>
ActsExamples::EventGenerator::availableEvents() const {
return {0u, SIZE_MAX};
return {0u, std::numeric_limits<std::size_t>::max()};
}

ActsExamples::ProcessCode ActsExamples::EventGenerator::read(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ class EventGenerator final : public ActsExamples::IReader {

/// Name of the reader.
std::string name() const final;
/// Available events range. Always return [0,SIZE_MAX) since we generate them.
/// Available events range. Always return
/// [0,std::numeric_limits<std::size_t>::max()) since we generate them.
std::pair<std::size_t, std::size_t> availableEvents() const final;
/// Generate an event.
ProcessCode read(const AlgorithmContext& ctx) final;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ namespace ActsExamples {
/// calls.
class IReader : public SequenceElement {
public:
/// Provide range of available events or [0, SIZE_MAX) if undefined.
/// Provide range of available events or [0,
/// std::numeric_limits<std::size_t>::max()) if undefined.
///
/// The upper limit is exclusive, i.e. [0,3) means events 0, 1, and 2.
virtual std::pair<std::size_t, std::size_t> availableEvents() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ class Sequencer {
struct Config {
/// number of events to skip at the beginning
std::size_t skip = 0;
/// number of events to process, SIZE_MAX to process all available events
/// number of events to process, std::numeric_limits<std::size_t>::max() to
/// process all available events
std::optional<std::size_t> events = std::nullopt;
/// logging level
Acts::Logging::Level logLevel = Acts::Logging::INFO;
Expand Down Expand Up @@ -158,7 +159,9 @@ class Sequencer {
private:
/// List of all configured algorithm names.
std::vector<std::string> listAlgorithmNames() const;
/// Determine range of (requested) events; [SIZE_MAX, SIZE_MAX) for error.
/// Determine range of (requested) events;
/// [std::numeric_limits<std::size_t>::max(),
/// std::numeric_limits<std::size_t>::max()) for error.
std::pair<std::size_t, std::size_t> determineEventsRange() const;

std::pair<std::string, std::size_t> fpeMaskCount(
Expand Down
15 changes: 10 additions & 5 deletions Examples/Framework/src/Framework/Sequencer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ std::string_view getAlgorithmType(const SequenceElement& element) {
return "Algorithm";
}

// Saturated addition that does not overflow and exceed SIZE_MAX.
// Saturated addition that does not overflow and exceed
// std::numeric_limits<std::size_t>::max().
//
// From http://locklessinc.com/articles/sat_arithmetic/
std::size_t saturatedAdd(std::size_t a, std::size_t b) {
Expand Down Expand Up @@ -290,7 +291,9 @@ std::vector<std::string> Sequencer::listAlgorithmNames() const {
}

std::pair<std::size_t, std::size_t> Sequencer::determineEventsRange() const {
constexpr auto kInvalidEventsRange = std::make_pair(SIZE_MAX, SIZE_MAX);
constexpr auto kInvalidEventsRange =
std::make_pair(std::numeric_limits<std::size_t>::max(),
std::numeric_limits<std::size_t>::max());

// Note on skipping events:
//
Expand All @@ -303,7 +306,7 @@ std::pair<std::size_t, std::size_t> Sequencer::determineEventsRange() const {

// determine intersection of event ranges available from readers
std::size_t beg = 0u;
std::size_t end = SIZE_MAX;
std::size_t end = std::numeric_limits<std::size_t>::max();
for (const auto& reader : m_readers) {
auto available = reader->availableEvents();
beg = std::max(beg, available.first);
Expand All @@ -329,7 +332,8 @@ std::pair<std::size_t, std::size_t> Sequencer::determineEventsRange() const {
return kInvalidEventsRange;
}
// events range was not defined by either the readers or user command line.
if ((beg == 0u) && (end == SIZE_MAX) && (!m_cfg.events.has_value())) {
if ((beg == 0u) && (end == std::numeric_limits<std::size_t>::max()) &&
(!m_cfg.events.has_value())) {
ACTS_ERROR("Could not determine number of events");
return kInvalidEventsRange;
}
Expand Down Expand Up @@ -421,7 +425,8 @@ int Sequencer::run() {
// processing only works w/ a well-known number of events
// error message is already handled by the helper function
std::pair<std::size_t, std::size_t> eventsRange = determineEventsRange();
if ((eventsRange.first == SIZE_MAX) && (eventsRange.second == SIZE_MAX)) {
if ((eventsRange.first == std::numeric_limits<std::size_t>::max()) &&
(eventsRange.second == std::numeric_limits<std::size_t>::max())) {
return EXIT_FAILURE;
}

Expand Down
2 changes: 1 addition & 1 deletion Examples/Framework/src/Utilities/Paths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ std::pair<std::size_t, std::size_t> ActsExamples::determineEventFilesRange(
}

// invalid default range that allows simple restriction later on
std::size_t eventMin = SIZE_MAX;
std::size_t eventMin = std::numeric_limits<std::size_t>::max();
std::size_t eventMax = 0;

// filter matching event files from the directory listing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <algorithm>
#include <cstdint>
#include <limits>
#include <tuple>
#include <unordered_map>
#include <utility>
Expand Down Expand Up @@ -53,7 +54,8 @@ struct EventFraction {
float initialMomentum = 0.;
};

static constexpr uint32_t s_MaxValue = UINT32_MAX;
static constexpr std::uint32_t s_MaxValue =
std::numeric_limits<std::uint32_t>::max();
using EventCollection = std::vector<EventFraction>;
using EventProperties = std::vector<std::vector<float>>;
using ProbabilityDistributions = std::vector<TH1F*>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <algorithm>
#include <cstdint>
#include <iterator>
#include <limits>
#include <memory>
#include <stdexcept>
#include <tuple>
Expand Down Expand Up @@ -175,8 +176,9 @@ std::pair<std::vector<float>, std::vector<uint32_t>> buildMap(
std::vector<uint32_t> normalisedHistoContents(nBins);
const double invIntegral = 1. / std::get<2>(map);
for (int iBin = 0; iBin < nBins; ++iBin) {
normalisedHistoContents[iBin] = static_cast<unsigned int>(
UINT32_MAX * (histoContents[iBin] * invIntegral));
normalisedHistoContents[iBin] =
static_cast<unsigned int>(std::numeric_limits<std::uint32_t>::max() *
(histoContents[iBin] * invIntegral));
}

auto histoBorders = std::get<0>(map);
Expand Down Expand Up @@ -213,8 +215,9 @@ std::pair<std::vector<float>, std::vector<uint32_t>> buildMap(TH1F const* hist,
std::vector<uint32_t> normalisedHistoContents(nBins);
const double invIntegral = 1. / std::max(integral, std::get<2>(map));
for (int iBin = 0; iBin < nBins; ++iBin) {
normalisedHistoContents[iBin] = static_cast<unsigned int>(
UINT32_MAX * (histoContents[iBin] * invIntegral));
normalisedHistoContents[iBin] =
static_cast<unsigned int>(std::numeric_limits<std::uint32_t>::max() *
(histoContents[iBin] * invIntegral));
}

std::vector<float> histoBorders = std::get<0>(map);
Expand Down
4 changes: 2 additions & 2 deletions Fatras/include/ActsFatras/Kernel/InteractionList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ class InteractionList {
std::numeric_limits<Particle::Scalar>::infinity();
Particle::Scalar l0Limit =
std::numeric_limits<Particle::Scalar>::infinity();
std::size_t x0Process = SIZE_MAX;
std::size_t l0Process = SIZE_MAX;
std::size_t x0Process = std::numeric_limits<std::size_t>::max();
std::size_t l0Process = std::numeric_limits<std::size_t>::max();
};

/// Disable a specific process identified by index.
Expand Down
4 changes: 2 additions & 2 deletions Fatras/include/ActsFatras/Kernel/SimulationResult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ struct SimulationResult {
Particle::Scalar x0Limit = std::numeric_limits<Particle::Scalar>::quiet_NaN();
Particle::Scalar l0Limit = std::numeric_limits<Particle::Scalar>::quiet_NaN();
// Process selection for the next interaction.
std::size_t x0Process = SIZE_MAX;
std::size_t l0Process = SIZE_MAX;
std::size_t x0Process = std::numeric_limits<std::size_t>::max();
std::size_t l0Process = std::numeric_limits<std::size_t>::max();
};

} // namespace ActsFatras
6 changes: 4 additions & 2 deletions Fatras/src/Physics/NuclearInteraction/NuclearInteraction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ unsigned int NuclearInteraction::sampleDiscreteValues(
}

// Find the bin
const uint32_t int_rnd = static_cast<uint32_t>(UINT32_MAX * rnd);
const std::uint32_t int_rnd = static_cast<std::uint32_t>(
std::numeric_limits<std::uint32_t>::max() * rnd);
const auto it = std::upper_bound(distribution.second.begin(),
distribution.second.end(), int_rnd);
std::size_t iBin = std::min(
Expand All @@ -77,7 +78,8 @@ Particle::Scalar NuclearInteraction::sampleContinuousValues(
}

// Find the bin
const uint32_t int_rnd = static_cast<uint32_t>(UINT32_MAX * rnd);
const std::uint32_t int_rnd = static_cast<std::uint32_t>(
std::numeric_limits<std::uint32_t>::max() * rnd);
// Fast exit for non-normalised CDFs like interaction probability
if (int_rnd > distribution.second.back()) {
return std::numeric_limits<Scalar>::infinity();
Expand Down
6 changes: 4 additions & 2 deletions Tests/UnitTests/Core/Utilities/AlgebraHelpersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ BOOST_AUTO_TEST_CASE(safeInverseBadLargeMatrix) {
}

BOOST_AUTO_TEST_CASE(SafeInverseFPESmallMatrix) {
Eigen::Matrix<double, 4, 4> m = Eigen::MatrixXd::Identity(4, 4) * SIZE_MAX;
Eigen::Matrix<double, 4, 4> m =
Eigen::MatrixXd::Identity(4, 4) * std::numeric_limits<std::size_t>::max();
m(1, 1) = 1;

auto mInv = Acts::safeInverse(m);
Expand All @@ -88,7 +89,8 @@ BOOST_AUTO_TEST_CASE(SafeInverseFPESmallMatrix) {
}

BOOST_AUTO_TEST_CASE(SafeInverseFPELargeMatrix) {
Eigen::Matrix<double, 5, 5> m = Eigen::MatrixXd::Identity(5, 5) * SIZE_MAX;
Eigen::Matrix<double, 5, 5> m =
Eigen::MatrixXd::Identity(5, 5) * std::numeric_limits<std::size_t>::max();
m(1, 1) = 1;

auto mInv = Acts::safeInverse(m);
Expand Down
Loading

0 comments on commit ac6f02e

Please sign in to comment.