Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /acts/Core/include/Acts/EventData/SeedProxy.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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/EventData/SeedColumns.hpp"
0012 #include "Acts/EventData/SpacePointContainer.hpp"
0013 #include "Acts/EventData/Types.hpp"
0014 #include "Acts/Utilities/TypeTraits.hpp"
0015 
0016 #include <cassert>
0017 
0018 namespace Acts {
0019 
0020 class SeedContainer;
0021 
0022 /// A proxy class for accessing individual seeds.
0023 template <bool read_only>
0024 class SeedProxy {
0025  public:
0026   /// Indicates whether this seed proxy is read-only or if it can be
0027   /// modified
0028   static constexpr bool ReadOnly = read_only;
0029 
0030   /// Type alias for seed index type
0031   using IndexType = SeedIndex;
0032 
0033   /// Type alias for container type (const if read-only)
0034   using ContainerType = const_if_t<ReadOnly, SeedContainer>;
0035 
0036   /// Constructs a seed proxy for the given container and index.
0037   /// @param container The container holding the seed.
0038   /// @param index The index of the seed in the container.
0039   SeedProxy(ContainerType &container, IndexType index) noexcept
0040       : m_container{&container}, m_index{index} {}
0041 
0042   /// Copy construct a seed proxy.
0043   /// @param other The seed proxy to copy.
0044   SeedProxy(const SeedProxy &other) noexcept = default;
0045 
0046   /// Copy construct a mutable seed proxy.
0047   /// @param other The mutable seed proxy to copy.
0048   explicit SeedProxy(const SeedProxy<false> &other) noexcept
0049     requires ReadOnly
0050       : m_container(&other.container()), m_index(other.index()) {}
0051 
0052   /// Copy assign a seed proxy.
0053   /// @param other The seed proxy to copy.
0054   /// @return Reference to this seed proxy after assignment.
0055   SeedProxy &operator=(const SeedProxy &other) noexcept = default;
0056 
0057   /// Copy assign a mutable seed proxy.
0058   /// @param other The mutable seed proxy to copy.
0059   /// @return Reference to this seed proxy after assignment.
0060   SeedProxy &operator=(const SeedProxy<false> &other) noexcept
0061     requires ReadOnly
0062   {
0063     m_container = &other.container();
0064     m_index = other.index();
0065     return *this;
0066   }
0067 
0068   /// Move assign a seed proxy.
0069   /// @param other The seed proxy to move.
0070   /// @return Reference to this seed proxy after assignment.
0071   SeedProxy &operator=(SeedProxy &&other) noexcept = default;
0072 
0073   /// Move assign a mutable seed proxy.
0074   /// @param other The mutable seed proxy to move.
0075   /// @return Reference to this seed proxy after assignment.
0076   SeedProxy &operator=(SeedProxy<false> &&other) noexcept
0077     requires ReadOnly
0078   {
0079     m_container = &other.container();
0080     m_index = other.index();
0081     return *this;
0082   }
0083 
0084   /// Returns a const proxy of the seed.
0085   /// @return A const proxy of the seed.
0086   SeedProxy<true> asConst() const noexcept
0087     requires(!ReadOnly)
0088   {
0089     return {*m_container, m_index};
0090   }
0091 
0092   /// Gets the container holding the seed.
0093   /// @return A reference to the container holding the seed.
0094   SeedContainer &container() noexcept
0095     requires(!ReadOnly)
0096   {
0097     return *m_container;
0098   }
0099 
0100   /// Gets the container holding the seed.
0101   /// @return A const reference to the container holding the seed.
0102   const SeedContainer &container() const noexcept { return *m_container; }
0103 
0104   /// Gets the index of the seed in the container.
0105   /// @return The index of the seed in the container.
0106   IndexType index() const noexcept { return m_index; }
0107 
0108   /// Assigns space point indices to the seed at the given index.
0109   /// @param spacePointIndices A span of space point indices to assign to the seed.
0110   /// @throws std::out_of_range if the index is out of range.
0111   /// @throws std::logic_error if space point indices are already assigned to the seed.
0112   void assignSpacePointIndices(
0113       std::span<const SpacePointIndex> spacePointIndices)
0114     requires(!ReadOnly)
0115   {
0116     if (m_index >= m_container->size()) {
0117       throw std::out_of_range("Index out of range in SeedContainer");
0118     }
0119     if (m_container->m_spacePointCounts[m_index] != 0) {
0120       throw std::logic_error("Space points already assigned to the seed");
0121     }
0122 
0123     m_container->m_spacePointOffsets[m_index] =
0124         static_cast<std::uint32_t>(m_container->m_spacePoints.size());
0125     m_container->m_spacePointCounts[m_index] =
0126         static_cast<std::uint8_t>(spacePointIndices.size());
0127     m_container->m_spacePoints.insert(m_container->m_spacePoints.end(),
0128                                       spacePointIndices.begin(),
0129                                       spacePointIndices.end());
0130   }
0131 
0132   /// Returns the size of the seed, i.e., the number of space points
0133   /// associated with it.
0134   /// @return The number of space points in the seed.
0135   [[nodiscard]] std::size_t size() const noexcept {
0136     return spacePointIndices().size();
0137   }
0138 
0139   /// Checks if the seed is empty, i.e., has no space points associated with it.
0140   /// @return True if the seed is empty, false otherwise.
0141   [[nodiscard]]
0142   bool empty() const noexcept {
0143     return size() == 0;
0144   }
0145 
0146   /// Mutable access to the space point indices of the seed.
0147   /// @return A mutable span of space point indices associated with the seed.
0148   std::span<SpacePointIndex> spacePointIndices() noexcept
0149     requires(!ReadOnly)
0150   {
0151     const std::size_t offset = accessImpl(m_container->m_spacePointOffsets);
0152     const std::size_t count = accessImpl(m_container->m_spacePointCounts);
0153     return std::span<SpacePointIndex>(
0154         m_container->m_spacePoints.data() + offset, count);
0155   }
0156 
0157   /// Mutable access to the quality of the seed.
0158   /// @return A mutable reference to the quality of the seed.
0159   float &quality() noexcept
0160     requires(!ReadOnly)
0161   {
0162     return accessImpl(m_container->m_qualities);
0163   }
0164 
0165   /// Mutable access to the vertex Z coordinate of the seed.
0166   /// @return A mutable reference to the vertex Z coordinate of the seed.
0167   float &vertexZ() noexcept
0168     requires(!ReadOnly)
0169   {
0170     return accessImpl(m_container->m_vertexZs);
0171   }
0172 
0173   /// Const access to the space point indices of the seed.
0174   /// @return A span of space point indices associated with the seed.
0175   ///         This span is read-only and cannot be modified.
0176   std::span<const SpacePointIndex> spacePointIndices() const noexcept {
0177     const std::size_t offset = accessImpl(m_container->m_spacePointOffsets);
0178     const std::size_t count = accessImpl(m_container->m_spacePointCounts);
0179     return std::span<const SpacePointIndex>(
0180         m_container->m_spacePoints.data() + offset, count);
0181   }
0182 
0183   /// Const access to the quality of the seed.
0184   /// @return The quality of the seed.
0185   float quality() const noexcept {
0186     return accessImpl(m_container->m_qualities);
0187   }
0188 
0189   /// Const access to the vertex Z coordinate of the seed.
0190   /// @return The vertex Z coordinate of the seed.
0191   float vertexZ() const noexcept { return accessImpl(m_container->m_vertexZs); }
0192 
0193   /// Iterator over space points referenced by the seed.
0194   class SpacePointIterator {
0195    public:
0196     /// Iterator value type
0197     using value_type = ConstSpacePointProxy;
0198     /// Iterator difference type
0199     using difference_type = std::ptrdiff_t;
0200     /// Iterator pointer type
0201     using pointer = void;
0202     /// Iterator reference type
0203     using reference = void;
0204 
0205     /// Iterator category
0206     using iterator_category = std::random_access_iterator_tag;
0207     /// Iterator concept
0208     using iterator_concept = std::random_access_iterator_tag;
0209 
0210     SpacePointIterator() = default;
0211     /// Constructor from space point container and index pointer
0212     /// @param spacePointContainer Container of space points
0213     /// @param indexPointer Pointer to space point index
0214     SpacePointIterator(const SpacePointContainer &spacePointContainer,
0215                        const SpacePointIndex *indexPointer) noexcept
0216         : m_spacePointContainer{&spacePointContainer},
0217           m_indexPointer{indexPointer} {}
0218 
0219     /// Dereference operator
0220     /// @return Proxy to the space point at the current position
0221     value_type operator*() const noexcept {
0222       return (*m_spacePointContainer)[*m_indexPointer];
0223     }
0224     /// Subscript operator
0225     /// @param n Offset from the current position
0226     /// @return Proxy to the space point at offset n
0227     value_type operator[](difference_type n) const noexcept {
0228       return (*m_spacePointContainer)[m_indexPointer[n]];
0229     }
0230 
0231     /// Pre-increment operator
0232     /// @return Reference to the incremented iterator
0233     constexpr SpacePointIterator &operator++() noexcept {
0234       ++m_indexPointer;
0235       return *this;
0236     }
0237     /// Post-increment operator
0238     /// @return Copy of the iterator before increment
0239     constexpr SpacePointIterator operator++(int) noexcept {
0240       auto tmp = *this;
0241       ++(*this);
0242       return tmp;
0243     }
0244     /// Pre-decrement operator
0245     /// @return Reference to the decremented iterator
0246     constexpr SpacePointIterator &operator--() noexcept {
0247       --m_indexPointer;
0248       return *this;
0249     }
0250     /// Post-decrement operator
0251     /// @return Copy of the iterator before decrement
0252     constexpr SpacePointIterator operator--(int) noexcept {
0253       auto tmp = *this;
0254       --(*this);
0255       return tmp;
0256     }
0257 
0258     /// Compound addition assignment operator
0259     /// @param n Number of positions to advance
0260     /// @return Reference to the advanced iterator
0261     constexpr SpacePointIterator &operator+=(difference_type n) noexcept {
0262       m_indexPointer += n;
0263       return *this;
0264     }
0265     /// Compound subtraction assignment operator
0266     /// @param n Number of positions to move back
0267     /// @return Reference to the moved iterator
0268     constexpr SpacePointIterator &operator-=(difference_type n) noexcept {
0269       m_indexPointer -= n;
0270       return *this;
0271     }
0272 
0273    private:
0274     const SpacePointContainer *m_spacePointContainer{nullptr};
0275     const SpacePointIndex *m_indexPointer{nullptr};
0276 
0277     friend constexpr SpacePointIterator operator+(SpacePointIterator it,
0278                                                   difference_type n) noexcept {
0279       return it += n;
0280     }
0281 
0282     friend constexpr SpacePointIterator operator+(
0283         difference_type n, SpacePointIterator it) noexcept {
0284       return it += n;
0285     }
0286 
0287     friend constexpr SpacePointIterator operator-(SpacePointIterator it,
0288                                                   difference_type n) noexcept {
0289       return it -= n;
0290     }
0291 
0292     friend constexpr difference_type operator-(
0293         const SpacePointIterator &lhs, const SpacePointIterator &rhs) noexcept {
0294       return lhs.m_indexPointer - rhs.m_indexPointer;
0295     }
0296 
0297     friend constexpr auto operator<=>(const SpacePointIterator &a,
0298                                       const SpacePointIterator &b) noexcept {
0299       return a.m_indexPointer <=> b.m_indexPointer;
0300     }
0301 
0302     friend constexpr bool operator==(const SpacePointIterator &a,
0303                                      const SpacePointIterator &b) noexcept {
0304       return a.m_indexPointer == b.m_indexPointer;
0305     }
0306   };
0307 
0308   /// Range facade for the seed space points.
0309   class SpacePointRange {
0310    public:
0311     /// Size type for the range
0312     using size_type = std::size_t;
0313     /// Value type for the range
0314     using value_type = ConstSpacePointProxy;
0315 
0316     /// Constructor
0317     /// @param spacePointContainer The space point container
0318     /// @param spacePointIndices The space point indices
0319     SpacePointRange(const SpacePointContainer &spacePointContainer,
0320                     std::span<const SpacePointIndex> spacePointIndices) noexcept
0321         : m_spacePointContainer{&spacePointContainer},
0322           m_spacePointIndices{spacePointIndices} {}
0323 
0324     /// Get the number of space points in the range
0325     /// @return Number of space points
0326     std::size_t size() const noexcept { return m_spacePointIndices.size(); }
0327     /// Check if the range is empty
0328     /// @return True if the range is empty
0329     bool empty() const noexcept { return size() == 0; }
0330 
0331     /// Subscript operator
0332     /// @param index Index of the space point
0333     /// @return Proxy to the space point at the given index
0334     ConstSpacePointProxy operator[](std::size_t index) const noexcept {
0335       return (*m_spacePointContainer)[m_spacePointIndices[index]];
0336     }
0337 
0338     /// Get iterator to the beginning
0339     /// @return Iterator to the first space point
0340     SpacePointIterator begin() const noexcept {
0341       return {*m_spacePointContainer, m_spacePointIndices.data()};
0342     }
0343     /// Get iterator to the end
0344     /// @return Iterator past the last space point
0345     SpacePointIterator end() const noexcept {
0346       return {*m_spacePointContainer,
0347               m_spacePointIndices.data() + m_spacePointIndices.size()};
0348     }
0349 
0350    private:
0351     const SpacePointContainer *m_spacePointContainer{nullptr};
0352     std::span<const SpacePointIndex> m_spacePointIndices;
0353   };
0354 
0355   /// Get the space points associated with this seed. The space point container
0356   /// is taken from the seed container.
0357   /// @return Range of space points for this seed
0358   SpacePointRange spacePoints() const {
0359     return {m_container->spacePointContainer(), spacePointIndices()};
0360   }
0361 
0362   /// Get the space points associated with this seed using an external space
0363   /// point container.
0364   /// @param spacePointContainer External container holding all space points
0365   /// @return Range of space points for this seed
0366   SpacePointRange spacePoints(
0367       const SpacePointContainer &spacePointContainer) const noexcept {
0368     return {spacePointContainer, spacePointIndices()};
0369   }
0370 
0371   /// Copies the specified columns from another seed to this seed.
0372   /// @param other The seed proxy to copy from.
0373   /// @param columnsToCopy The columns to copy from the other seed.
0374   template <bool other_read_only>
0375   void copyFrom(const SeedProxy<other_read_only> &other,
0376                 SeedColumns columnsToCopy) const
0377     requires(!ReadOnly)
0378   {
0379     m_container->copyFrom(m_index, other.container(), other.index(),
0380                           columnsToCopy);
0381   }
0382 
0383  private:
0384   ContainerType *m_container{nullptr};
0385   IndexType m_index{0};
0386 
0387   template <typename Column>
0388   auto &accessImpl(Column &&column) const {
0389     assert(m_index < column.size() && "Index out of bounds");
0390     return column[m_index];
0391   }
0392 };
0393 
0394 }  // namespace Acts