Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-10-31 08:16:26

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include "Acts/Utilities/Holders.hpp"
0012 
0013 #include <array>
0014 #include <vector>
0015 
0016 namespace Acts {
0017 
0018 template <typename T, class... Axes>
0019   requires(std::is_default_constructible_v<T> && !std::is_same_v<T, bool>)
0020 class Grid;
0021 
0022 /// @class GridGlobalIterator
0023 /// Grid iterator using the global position. This iterates on all
0024 /// the bins in the grid, including under- and over-flows
0025 /// @tparam T The type stored in the grid bins
0026 /// @tparam Axes ... The types of the axes in the grid
0027 template <typename T, class... Axes>
0028 class GridGlobalIterator {
0029  public:
0030   /// Dimensionality of the grid (number of axes)
0031   static constexpr std::size_t DIM = sizeof...(Axes);
0032 
0033   /// Iterator category for standard algorithms
0034   using iterator_category = std::random_access_iterator_tag;
0035   /// Type of values stored in grid bins
0036   using value_type = T;
0037   /// Type for iterator arithmetic
0038   using difference_type = std::ptrdiff_t;
0039   /// Pointer type to grid values
0040   using pointer = value_type*;
0041   /// Reference type to grid values
0042   using reference = value_type&;
0043 
0044   /// @brief Default constructor
0045   GridGlobalIterator() = default;
0046   /// @brief Constructor taking ownership of the grid is not allowed
0047   /// @param [in] grid The grid
0048   /// @param [in] idx The global bin
0049   GridGlobalIterator(Grid<T, Axes...>&& grid, std::size_t idx) = delete;
0050   /// @brief Constructor not taking ownership of the grid
0051   /// @param [in] grid The grid
0052   /// @param [in] idx The global bin
0053   ///
0054   /// @pre Global bin index must be a valid index for the grid
0055   explicit GridGlobalIterator(const Grid<T, Axes...>& grid,
0056                               std::size_t idx = 0ul);
0057 
0058   /// @brief Copy constructor
0059   /// @param [in] other The GlobalBinIterator to be copied
0060   GridGlobalIterator(const GridGlobalIterator<T, Axes...>& other) = default;
0061   /// @brief Copy assignment
0062   /// @param [in] other The GlobalBinIterator to be copied
0063   /// @return The new global bin iterator
0064   GridGlobalIterator<T, Axes...>& operator=(
0065       const GridGlobalIterator<T, Axes...>& other) = default;
0066 
0067   /// @brief Move constructor
0068   /// @param [in] other The GlobalBinIterator to be moved
0069   ///
0070   /// This will invalidate the other GlobalBinIterator
0071   GridGlobalIterator(GridGlobalIterator<T, Axes...>&& other) noexcept;
0072   /// @brief Move assignment
0073   /// @param [in] other The GlobalBinIterator to be moved
0074   /// @return The new global bin iterator
0075   ///
0076   /// This will invalidate the other GlobalBinIterator
0077   GridGlobalIterator<T, Axes...>& operator=(
0078       GridGlobalIterator<T, Axes...>&& other) noexcept;
0079 
0080   /// @brief Default destructor
0081   ~GridGlobalIterator() = default;
0082 
0083   /// @brief Equality operator
0084   /// @param [in] other The other GridGlobalIterator to be compared against this one
0085   /// @return The result of the comparison
0086   bool operator==(const GridGlobalIterator<T, Axes...>& other) const;
0087   /// @brief Comparison (<=>) operator
0088   /// @param [in] other The other GridGlobalIterator to be compared against this one
0089   /// @return The result of the comparison
0090   auto operator<=>(const GridGlobalIterator<T, Axes...>& other) const;
0091 
0092   /// @brief Increment this iterator with an offset
0093   /// @param [in] offset The increment value
0094   /// @return The incremented iterator
0095   GridGlobalIterator<T, Axes...>& operator+=(const std::size_t offset);
0096   /// @brief Decrement this iterator with an offset
0097   /// @param [in] offset The decrement value
0098   /// @return The decremented iterator
0099   GridGlobalIterator<T, Axes...>& operator-=(const std::size_t offset);
0100   /// @brief Create incremented iterator
0101   /// @param [in] offset The increment value
0102   /// @return The incremented iterator
0103   GridGlobalIterator<T, Axes...> operator+(const std::size_t offset) const;
0104   /// @brief Create decremented iterator
0105   /// @param [in] offset The decrement value
0106   /// @return The decremented iterator
0107   GridGlobalIterator<T, Axes...> operator-(const std::size_t offset) const;
0108 
0109   /// @brief Distance between two GridGlobalIterators
0110   /// @param [in] other The other GridGlobalIterator
0111   /// @return The distance between the two iterators
0112   ///
0113   /// This will compute the distance by comparing the global positions in the
0114   /// two iterators
0115   ///
0116   /// @pre The two iterators must have the same grid
0117   difference_type operator-(const GridGlobalIterator<T, Axes...>& other) const;
0118   /// @brief Return stored value at given global position
0119   /// @return The stored value in the grid from that given global position
0120   const value_type& operator*() const;
0121 
0122   /// @brief Increment operator (pre)
0123   /// @return The global iterator after the increment
0124   ///
0125   /// This will increase the global position by one
0126   GridGlobalIterator<T, Axes...>& operator++();
0127   /// @brief Increment operator (post)
0128   /// @return The global iterator before the increment
0129   ///
0130   /// This will increase the global position by one
0131   GridGlobalIterator<T, Axes...> operator++(int);
0132 
0133   /// @brief Retrieve the global bin index
0134   /// @return The current global bin index in the grid
0135   std::size_t globalBinIndex() const;
0136   /// @brief Retrieve the local bins indices
0137   /// @return The current local bins indexed in the grid
0138   std::array<std::size_t, DIM> localBinsIndices() const;
0139 
0140  private:
0141   /// @brief The grid on which we are iterating
0142   ///
0143   /// The iterator never takes ownership of the grid. If the grid gets
0144   /// invalidated (e.g. in a move operation) we can get undefined behaviours
0145   /// if the iterator gets used after being invalidated
0146   detail::RefHolder<const Grid<T, Axes...>> m_grid{nullptr};
0147   /// @brief The iteration index, corresponding to the global bin in the grid
0148   std::size_t m_idx{0ul};
0149 };
0150 
0151 /// @class GridLocalIterator
0152 /// Grid iterator using the local position. This iterates on all
0153 /// local bins in the grid, and can exclude under- and over-flows
0154 /// Can also allow for custom navigation pattern along axes
0155 /// @tparam T The type stored in the grid bins
0156 /// @tparam Axes ... The types of the axes in the grid
0157 template <typename T, class... Axes>
0158 class GridLocalIterator {
0159  public:
0160   /// Dimensionality of the grid (number of axes)
0161   static constexpr std::size_t DIM = sizeof...(Axes);
0162 
0163   /// Iterator category for standard algorithms
0164   using iterator_category = std::bidirectional_iterator_tag;
0165   /// Type of values stored in grid bins
0166   using value_type = T;
0167   /// Type for iterator arithmetic
0168   using difference_type = std::ptrdiff_t;
0169   /// Pointer type to grid values
0170   using pointer = value_type*;
0171   /// Reference type to grid values
0172   using reference = value_type&;
0173 
0174   /// @brief Default constructor
0175   GridLocalIterator() = default;
0176   /// @brief Constructor taking ownership of the grid is not allowed
0177   /// @param [in] grid The grid
0178   /// @param [in] indices The local position
0179   GridLocalIterator(Grid<T, Axes...>&& grid,
0180                     const std::array<std::size_t, DIM>& indices) = delete;
0181   /// @brief Constructor taking ownership of the grid is not allowed
0182   /// @param [in] grid The grid
0183   /// @param [in] indices The local position
0184   /// @param [in] navigation The custom navigation pattern for each axis
0185   ///
0186   /// @pre None of the navigation vectors is allowed to be an empty vector
0187   GridLocalIterator(
0188       Grid<T, Axes...>&& grid, const std::array<std::size_t, DIM>& indices,
0189       std::array<std::vector<std::size_t>, DIM> navigation) = delete;
0190   /// @brief Constructor
0191   /// @param [in] grid The grid
0192   /// @param [in] indices The local position
0193   ///
0194   /// @pre The local bins must be a valid local position in the grid
0195   GridLocalIterator(const Grid<T, Axes...>& grid,
0196                     const std::array<std::size_t, DIM>& indices);
0197   /// @brief Constructor with custom navigation pattern
0198   /// @param [in] grid The grid
0199   /// @param [in] indices The local position
0200   /// @param [in] navigation The custom navigation pattern for each axis
0201   ///
0202   /// @pre The local bins must be a valid local position in the grid.
0203   /// The navigation pattern must be consistent with the local bins (i.e. size
0204   /// <= num bins in the axis) in the grid and have no repetitions.
0205   ///
0206   /// @pre None of the navigation vectors is allowed to be an empty vector
0207   GridLocalIterator(const Grid<T, Axes...>& grid,
0208                     const std::array<std::size_t, DIM>& indices,
0209                     std::array<std::vector<std::size_t>, DIM> navigation);
0210 
0211   /// @brief Copy constructor
0212   /// @param [in] other The GridLocalIterator to be copied
0213   GridLocalIterator(const GridLocalIterator<T, Axes...>& other) = default;
0214   /// @brief Copy assignment operator
0215   /// @param [in] other The GridLocalIterator to be copied
0216   /// @return The copied GridLocalIterator
0217   GridLocalIterator<T, Axes...>& operator=(
0218       const GridLocalIterator<T, Axes...>& other) = default;
0219 
0220   /// @brief Move constructor
0221   /// @param [in] other The GridLocalIterator to be moved
0222   ///
0223   /// This will invalidate the other GridLocalIterator
0224   GridLocalIterator(GridLocalIterator<T, Axes...>&& other) noexcept;
0225   /// @brief Move assignment operator
0226   /// @param [in] other The GridLocalIterator to be moved
0227   /// @return The moved GridLocalIterator
0228   ///
0229   /// This will invalidate the other GridLocalIterator
0230   GridLocalIterator<T, Axes...>& operator=(
0231       GridLocalIterator<T, Axes...>&& other) noexcept;
0232 
0233   /// @brief Default destructor
0234   ~GridLocalIterator() = default;
0235 
0236   /// @brief Equality operator
0237   /// @param [in] other The other GridLocalIterator to be compared against this one
0238   /// @return The result of the comparison
0239   bool operator==(const GridLocalIterator<T, Axes...>& other) const;
0240 
0241   /// @brief Return stored value at given local position
0242   /// @return The stored value in the grid from that given local position
0243   const value_type& operator*() const;
0244 
0245   /// @brief Increment operator (pre)
0246   /// @return The local iterator after the increment
0247   ///
0248   /// This will increase the local position by one
0249   GridLocalIterator<T, Axes...>& operator++();
0250   /// @brief Increment operator (post)
0251   /// @return The local iterator before the increment
0252   ///
0253   /// This will increase the local position by one
0254   GridLocalIterator<T, Axes...> operator++(int);
0255 
0256   /// @brief Retrieve the global bin index
0257   /// @return The current global bin index in the grid
0258   std::size_t globalBinIndex() const;
0259   /// @brief Retrieve the local position
0260   /// @return The current local position in the grid
0261   std::array<std::size_t, DIM> localBinsIndices() const;
0262 
0263  private:
0264   /// @brief Increment the local position
0265   /// @tparam N Current dimension
0266   template <std::size_t N>
0267   void increment();
0268 
0269  private:
0270   /// @brief The grid on which we are iterating
0271   ///
0272   /// The iterator never takes ownership of the grid. If the grid gets
0273   /// invalidated (e.g. in a move operation) we can get undefined behaviours
0274   /// if the iterator gets used after being invalidated
0275   detail::RefHolder<const Grid<T, Axes...>> m_grid{nullptr};
0276   /// @brief The maximum number of local bins in the grid. This does not include
0277   /// under- and over-flow bins
0278   std::array<std::size_t, DIM> m_numLocalBins{};
0279   /// @brief The current iteration position.
0280   ///
0281   /// This represent the position in the navigation pattern.
0282   /// For each axis, the current index goes from 0ul to the size of the
0283   /// corresponding navigation
0284   ///
0285   /// The local position in the gris is then obtained, for each axis i,
0286   /// via m_navigationIndex[m_currentIndex[i]]
0287   std::array<std::size_t, DIM> m_currentIndex{};
0288   /// @brief The custom navigation pattern in the grid
0289   ///
0290   /// This allows users to define any custom iteration sequence in all the
0291   /// different axes of the grid. If nothing is defined by the user, then
0292   /// a std::iota is used as the default starting with the 1ul bin (0ul) is
0293   /// the under-flow in the axis
0294   std::array<std::vector<std::size_t>, DIM> m_navigationIndex{};
0295 };
0296 
0297 /// Deduction guide for GridGlobalIterator
0298 /// @param grid Grid to iterate over
0299 /// @param idx Starting index
0300 template <typename T, class... Axes>
0301 GridGlobalIterator(const Grid<T, Axes...>& grid,
0302                    std::size_t idx) -> GridGlobalIterator<T, Axes...>;
0303 
0304 }  // namespace Acts
0305 
0306 #include "Acts/Utilities/GridIterator.ipp"