Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-06 08:10:17

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