Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-13 09:38:48

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/Types.hpp"
0012 #include "Acts/Utilities/TypeTraits.hpp"
0013 #include "Acts/Utilities/detail/ContainerSubset.hpp"
0014 
0015 #include <cassert>
0016 #include <span>
0017 #include <vector>
0018 
0019 namespace Acts {
0020 
0021 class SpacePointContainer2;
0022 
0023 /// Additional column of data that can be added to the space point container.
0024 /// The column is indexed by the space point index.
0025 template <typename T, bool read_only>
0026 class SpacePointColumnProxy {
0027  public:
0028   /// Flag indicating whether this space point column proxy is read-only
0029   constexpr static bool ReadOnly = read_only;
0030   /// Type alias for space point index type
0031   using Index = SpacePointIndex2;
0032   /// Type alias for space point index range type
0033   using IndexRange = SpacePointIndexRange2;
0034   /// Type alias for space point index subset type
0035   using IndexSubset = SpacePointIndexSubset2;
0036   /// Type alias for column value type
0037   using Value = T;
0038   /// Type alias for container type (const if read-only)
0039   using Container = const_if_t<ReadOnly, SpacePointContainer2>;
0040   /// Type alias for column container type (const if read-only)
0041   using Column = const_if_t<ReadOnly, std::vector<Value>>;
0042 
0043   /// Constructs a space point column proxy for the given container and column.
0044   /// @param container The container holding the space point.
0045   /// @param column The column of data to access.
0046   SpacePointColumnProxy(Container &container, Column &column)
0047       : m_container{&container}, m_column(&column) {}
0048 
0049   /// Copy construct a space point column proxy.
0050   /// @param other The space point column proxy to copy.
0051   SpacePointColumnProxy(const SpacePointColumnProxy &other) noexcept = default;
0052 
0053   /// Copy construct a mutable space point column proxy.
0054   /// @param other The mutable space point column proxy to copy.
0055   explicit SpacePointColumnProxy(
0056       const SpacePointColumnProxy<T, false> &other) noexcept
0057     requires ReadOnly
0058       : m_container(&other.container()), m_column(&other.column()) {}
0059 
0060   /// Returns a const proxy of the space point column.
0061   /// @return A const proxy of the space point column.
0062   SpacePointColumnProxy<T, true> asConst() const noexcept
0063     requires(!ReadOnly)
0064   {
0065     return {*m_container, *m_column};
0066   }
0067 
0068   /// Gets the container holding the space point.
0069   /// @return A reference to the container holding the space point.
0070   SpacePointContainer2 &container() noexcept
0071     requires(!ReadOnly)
0072   {
0073     return *m_container;
0074   }
0075   /// Gets the container holding the space point.
0076   /// @return A const reference to the container holding the space point.
0077   const SpacePointContainer2 &container() const noexcept {
0078     return *m_container;
0079   }
0080 
0081   /// Returns a const reference to the column container.
0082   /// @return A const reference to the column container.
0083   const std::vector<Value> &column() const noexcept { return *m_column; }
0084 
0085   /// Returns a mutable span to the column data.
0086   /// @return A mutable span to the column data.
0087   std::span<Value> data() noexcept
0088     requires(!ReadOnly)
0089   {
0090     return std::span<Value>(column().data(), column().size());
0091   }
0092   /// Returns a const span to the column data.
0093   /// @return A const span to the column data.
0094   std::span<const Value> data() const noexcept {
0095     return std::span<const Value>(column().data(), column().size());
0096   }
0097 
0098   /// Returns a mutable reference to the column entry at the given index.
0099   /// If the index is out of range, an exception is thrown.
0100   /// @param index The index of the space point to access.
0101   /// @return A mutable reference to the column entry at the given index.
0102   /// @throws std::out_of_range if the index is out of range.
0103   Value &at(Index index)
0104     requires(!ReadOnly)
0105   {
0106     if (index >= column().size()) {
0107       throw std::out_of_range("Index out of range in SpacePointContainer2: " +
0108                               std::to_string(index) +
0109                               " >= " + std::to_string(size()));
0110     }
0111     return data()[index];
0112   }
0113   /// Returns a const reference to the column entry at the given index.
0114   /// If the index is out of range, an exception is thrown.
0115   /// @param index The index of the space point to access.
0116   /// @return A const reference to the column entry at the given index.
0117   /// @throws std::out_of_range if the index is out of range.
0118   const Value &at(Index index) const {
0119     if (index >= column().size()) {
0120       throw std::out_of_range("Index out of range in SpacePointContainer2: " +
0121                               std::to_string(index) +
0122                               " >= " + std::to_string(size()));
0123     }
0124     return data()[index];
0125   }
0126 
0127   /// Returns a mutable reference to the column entry at the given index.
0128   /// @param index The index of the space point to access.
0129   /// @return A mutable reference to the column entry at the given index.
0130   Value &operator[](Index index) noexcept
0131     requires(!ReadOnly)
0132   {
0133     assert(index < column().size() && "Index out of bounds");
0134     return data()[index];
0135   }
0136   /// Returns a const reference to the column entry at the given index.
0137   /// @param index The index of the space point to access.
0138   /// @return A const reference to the column entry at the given index.
0139   const Value &operator[](Index index) const noexcept {
0140     assert(index < column().size() && "Index out of bounds");
0141     return data()[index];
0142   }
0143 
0144   class Subset : public detail::ContainerSubset<Subset, Subset, Column, Value,
0145                                                 Index, ReadOnly> {
0146    public:
0147     using Base =
0148         detail::ContainerSubset<Subset, Subset, Column, Value, Index, ReadOnly>;
0149 
0150     using Base::Base;
0151   };
0152 
0153   /// Creates a subset view of this space point column based on provided
0154   /// indices.
0155   ///
0156   /// This method creates a subset proxy that provides access to only the space
0157   /// points at the indices specified in the IndexSubset. The subset maintains a
0158   /// reference to the original column data without copying, enabling efficient
0159   /// access to selected space points for filtering, clustering, or other
0160   /// operations.
0161   ///
0162   /// @param subset The index subset specifying which space points to include
0163   /// @return A subset proxy providing access to the selected space points
0164   ///
0165   /// @note The returned subset shares data with the original column
0166   /// @note The subset remains valid only as long as the original column exists
0167   /// @note This operation does not copy data, providing efficient subset access
0168   Subset subset(const IndexSubset &subset) const noexcept {
0169     return Subset(*m_column, subset);
0170   }
0171 
0172  private:
0173   Container *m_container{};
0174   Column *m_column{};
0175 
0176   std::uint32_t size() const noexcept { return column().size(); }
0177 
0178   std::vector<Value> &column() noexcept
0179     requires(!ReadOnly)
0180   {
0181     return *m_column;
0182   }
0183 
0184   friend class SpacePointContainer2;
0185 };
0186 
0187 template <typename T>
0188 using ConstSpacePointColumnProxy = SpacePointColumnProxy<T, true>;
0189 template <typename T>
0190 using MutableSpacePointColumnProxy = SpacePointColumnProxy<T, false>;
0191 
0192 }  // namespace Acts