Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:11:11

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/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 Comparison (<=>) operator
0077   /// @param [in] other The other GridGlobalIterator to be compared against this one
0078   /// @return The result of the comparison
0079   auto operator<=>(const GridGlobalIterator<T, Axes...>& other) const;
0080 
0081   /// @brief Increment this iterator with an offset
0082   /// @param [in] offset The increment value
0083   /// @return The incremented iterator
0084   GridGlobalIterator<T, Axes...>& operator+=(const std::size_t offset);
0085   /// @brief Decrement this iterator with an offset
0086   /// @param [in] offset The decrement value
0087   /// @return The decremented iterator
0088   GridGlobalIterator<T, Axes...>& operator-=(const std::size_t offset);
0089   /// @brief Create incremented iterator
0090   /// @param [in] offset The increment value
0091   /// @return The incremented iterator
0092   GridGlobalIterator<T, Axes...> operator+(const std::size_t offset) const;
0093   /// @brief Create decremented iterator
0094   /// @param [in] offset The decrement value
0095   /// @return The decremented iterator
0096   GridGlobalIterator<T, Axes...> operator-(const std::size_t offset) const;
0097 
0098   /// @brief Distance between two GridGlobalIterators
0099   /// @param [in] other The other GridGlobalIterator
0100   /// @return The distance between the two iterators
0101   ///
0102   /// This will compute the distance by comparing the global positions in the
0103   /// two iterators
0104   ///
0105   /// @pre The two iterators must have the same grid
0106   difference_type operator-(const GridGlobalIterator<T, Axes...>& other) const;
0107   /// @brief Return stored value at given global position
0108   /// @return The stored value in the grid from that given global position
0109   const value_type& operator*() const;
0110 
0111   /// @brief Increment operator (pre)
0112   /// @return The global iterator after the increment
0113   ///
0114   /// This will increase the global position by one
0115   GridGlobalIterator<T, Axes...>& operator++();
0116   /// @brief Increment operator (post)
0117   /// @return The global iterator before the increment
0118   ///
0119   /// This will increase the global position by one
0120   GridGlobalIterator<T, Axes...> operator++(int);
0121 
0122   /// @brief Retrieve the global bin index
0123   /// @return The current global bin index in the grid
0124   std::size_t globalBinIndex() const;
0125   /// @brief Retrieve the local bins indices
0126   /// @return The current local bins indexed in the grid
0127   std::array<std::size_t, DIM> localBinsIndices() const;
0128 
0129  private:
0130   /// @brief The grid on which we are iterating
0131   ///
0132   /// The iterator never takes ownership of the grid. If the grid gets
0133   /// invalidated (e.g. in a move operation) we can get undefined behaviours
0134   /// if the iterator gets used after being invalidated
0135   Acts::detail::RefHolder<const Acts::Grid<T, Axes...>> m_grid{nullptr};
0136   /// @brief The iteration index, corresponding to the global bin in the grid
0137   std::size_t m_idx{0ul};
0138 };
0139 
0140 /// @class GridLocalIterator
0141 /// Grid iterator using the local position. This iterates on all
0142 /// local bins in the grid, and can exclude under- and over-flows
0143 /// Can also allow for custom navigation pattern along axes
0144 /// @tparam T The type stored in the grid bins
0145 /// @tparam Axes ... The types of the axes in the grid
0146 template <typename T, class... Axes>
0147 class GridLocalIterator {
0148  public:
0149   static constexpr std::size_t DIM = sizeof...(Axes);
0150 
0151   using iterator_category = std::bidirectional_iterator_tag;
0152   using value_type = T;
0153   using difference_type = std::ptrdiff_t;
0154   using pointer = value_type*;
0155   using reference = value_type&;
0156 
0157   /// @brief Default constructor
0158   GridLocalIterator() = default;
0159   /// @brief Constructor taking ownership of the grid is not allowed
0160   /// @param [in] grid The grid
0161   /// @param [in] indices The local position
0162   GridLocalIterator(Acts::Grid<T, Axes...>&& grid,
0163                     const std::array<std::size_t, DIM>& indices) = delete;
0164   /// @brief Constructor taking ownership of the grid is not allowed
0165   /// @param [in] grid The grid
0166   /// @param [in] indices The local position
0167   /// @param [in] navigation The custom navigation pattern for each axis
0168   ///
0169   /// @pre None of the navigation vectors is allowed to be an empty vector
0170   GridLocalIterator(Acts::Grid<T, Axes...>&& grid,
0171                     const std::array<std::size_t, DIM>& indices,
0172                     std::array<std::vector<std::size_t>, DIM> navigation) =
0173       delete;
0174   /// @brief Constructor
0175   /// @param [in] grid The grid
0176   /// @param [in] indices The local position
0177   ///
0178   /// @pre The local bins must be a valid local position in the grid
0179   GridLocalIterator(const Acts::Grid<T, Axes...>& grid,
0180                     const std::array<std::size_t, DIM>& indices);
0181   /// @brief Constructor with custom navigation pattern
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 The local bins must be a valid local position in the grid.
0187   /// The navigation pattern must be consistent with the local bins (i.e. size
0188   /// <= num bins in the axis) in the grid and have no repetitions.
0189   ///
0190   /// @pre None of the navigation vectors is allowed to be an empty vector
0191   GridLocalIterator(const Acts::Grid<T, Axes...>& grid,
0192                     const std::array<std::size_t, DIM>& indices,
0193                     std::array<std::vector<std::size_t>, DIM> navigation);
0194 
0195   /// @brief Copy constructor
0196   /// @param [in] other The GridLocalIterator to be copied
0197   GridLocalIterator(const GridLocalIterator<T, Axes...>& other) = default;
0198   /// @brief Copy assignment operator
0199   /// @param [in] other The GridLocalIterator to be copied
0200   /// @return The copied GridLocalIterator
0201   GridLocalIterator<T, Axes...>& operator=(
0202       const GridLocalIterator<T, Axes...>& other) = default;
0203 
0204   /// @brief Move constructor
0205   /// @param [in] other The GridLocalIterator to be moved
0206   ///
0207   /// This will invalidate the other GridLocalIterator
0208   GridLocalIterator(GridLocalIterator<T, Axes...>&& other) noexcept;
0209   /// @brief Move assignment operator
0210   /// @param [in] other The GridLocalIterator to be moved
0211   /// @return The moved GridLocalIterator
0212   ///
0213   /// This will invalidate the other GridLocalIterator
0214   GridLocalIterator<T, Axes...>& operator=(
0215       GridLocalIterator<T, Axes...>&& other) noexcept;
0216 
0217   /// @brief Default destructor
0218   ~GridLocalIterator() = default;
0219 
0220   /// @brief Equality operator
0221   /// @param [in] other The other GridLocalIterator to be compared against this one
0222   /// @return The result of the comparison
0223   bool operator==(const Acts::GridLocalIterator<T, Axes...>& other) const;
0224 
0225   /// @brief Return stored value at given local position
0226   /// @return The stored value in the grid from that given local position
0227   const value_type& operator*() const;
0228 
0229   /// @brief Increment operator (pre)
0230   /// @return The local iterator after the increment
0231   ///
0232   /// This will increase the local position by one
0233   GridLocalIterator<T, Axes...>& operator++();
0234   /// @brief Increment operator (post)
0235   /// @return The local iterator before the increment
0236   ///
0237   /// This will increase the local position by one
0238   GridLocalIterator<T, Axes...> operator++(int);
0239 
0240   /// @brief Retrieve the global bin index
0241   /// @return The current global bin index in the grid
0242   std::size_t globalBinIndex() const;
0243   /// @brief Retrieve the local position
0244   /// @return The current local position in the grid
0245   std::array<std::size_t, DIM> localBinsIndices() const;
0246 
0247  private:
0248   /// @brief Increment the local position
0249   /// @tparam N Current dimension
0250   template <std::size_t N>
0251   void increment();
0252 
0253  private:
0254   /// @brief The grid on which we are iterating
0255   ///
0256   /// The iterator never takes ownership of the grid. If the grid gets
0257   /// invalidated (e.g. in a move operation) we can get undefined behaviours
0258   /// if the iterator gets used after being invalidated
0259   Acts::detail::RefHolder<const Acts::Grid<T, Axes...>> m_grid{nullptr};
0260   /// @brief The maximum number of local bins in the grid. This does not include
0261   /// under- and over-flow bins
0262   std::array<std::size_t, DIM> m_numLocalBins{};
0263   /// @brief The current iteration position.
0264   ///
0265   /// This represent the position in the navigation pattern.
0266   /// For each axis, the current index goes from 0ul to the size of the
0267   /// corresponding navigation
0268   ///
0269   /// The local position in the gris is then obtained, for each axis i,
0270   /// via m_navigationIndex[m_currentIndex[i]]
0271   std::array<std::size_t, DIM> m_currentIndex{};
0272   /// @brief The custom navigation pattern in the grid
0273   ///
0274   /// This allows users to define any custom iteration sequence in all the
0275   /// different axes of the grid. If nothing is defined by the user, then
0276   /// a std::iota is used as the default starting with the 1ul bin (0ul) is
0277   /// the under-flow in the axis
0278   std::array<std::vector<std::size_t>, DIM> m_navigationIndex{};
0279 };
0280 
0281 template <typename T, class... Axes>
0282 GridGlobalIterator(const Acts::Grid<T, Axes...>& grid,
0283                    std::size_t idx) -> GridGlobalIterator<T, Axes...>;
0284 
0285 }  // namespace Acts
0286 
0287 #include "Acts/Utilities/GridIterator.ipp"