Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 07:47:59

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/ContainerSubset.hpp"
0013 #include "Acts/Utilities/TypeTraits.hpp"
0014 
0015 #include <cassert>
0016 #include <span>
0017 #include <vector>
0018 
0019 namespace Acts::Experimental {
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   constexpr static bool ReadOnly = read_only;
0029   using Index = SpacePointIndex2;
0030   using IndexRange = SpacePointIndexRange2;
0031   using IndexSubset = SpacePointIndexSubset2;
0032   using Value = T;
0033   using Container = const_if_t<ReadOnly, SpacePointContainer2>;
0034   using Column = const_if_t<ReadOnly, std::vector<Value>>;
0035 
0036   /// Constructs a space point column proxy for the given container and column.
0037   /// @param container The container holding the space point.
0038   /// @param column The column of data to access.
0039   SpacePointColumnProxy(Container &container, Column &column)
0040       : m_container{&container}, m_column(&column) {}
0041 
0042   /// Copy construct a space point column proxy.
0043   /// @param other The space point column proxy to copy.
0044   SpacePointColumnProxy(const SpacePointColumnProxy &other) noexcept = default;
0045 
0046   /// Copy construct a mutable space point column proxy.
0047   /// @param other The mutable space point column proxy to copy.
0048   explicit SpacePointColumnProxy(
0049       const SpacePointColumnProxy<T, false> &other) noexcept
0050     requires ReadOnly
0051       : m_container(&other.container()), m_column(&other.column()) {}
0052 
0053   /// Returns a const proxy of the space point column.
0054   /// @return A const proxy of the space point column.
0055   SpacePointColumnProxy<T, true> asConst() const noexcept
0056     requires(!ReadOnly)
0057   {
0058     return {*m_container, *m_column};
0059   }
0060 
0061   /// Gets the container holding the space point.
0062   /// @return A reference to the container holding the space point.
0063   SpacePointContainer2 &container() noexcept
0064     requires(!ReadOnly)
0065   {
0066     return *m_container;
0067   }
0068   /// Gets the container holding the space point.
0069   /// @return A const reference to the container holding the space point.
0070   const SpacePointContainer2 &container() const noexcept {
0071     return *m_container;
0072   }
0073 
0074   /// Returns a const reference to the column container.
0075   /// @return A const reference to the column container.
0076   const std::vector<Value> &column() const noexcept { return *m_column; }
0077 
0078   /// Returns a mutable span to the column data.
0079   /// @return A mutable span to the column data.
0080   std::span<Value> data() noexcept
0081     requires(!ReadOnly)
0082   {
0083     return std::span<Value>(column().data(), column().size());
0084   }
0085   /// Returns a const span to the column data.
0086   /// @return A const span to the column data.
0087   std::span<const Value> data() const noexcept {
0088     return std::span<const Value>(column().data(), column().size());
0089   }
0090 
0091   /// Returns a mutable reference to the column entry at the given index.
0092   /// If the index is out of range, an exception is thrown.
0093   /// @param index The index of the space point to access.
0094   /// @return A mutable reference to the column entry at the given index.
0095   /// @throws std::out_of_range if the index is out of range.
0096   Value &at(Index index)
0097     requires(!ReadOnly)
0098   {
0099     if (index >= column().size()) {
0100       throw std::out_of_range("Index out of range in SpacePointContainer2: " +
0101                               std::to_string(index) +
0102                               " >= " + std::to_string(size()));
0103     }
0104     return data()[index];
0105   }
0106   /// Returns a const reference to the column entry at the given index.
0107   /// If the index is out of range, an exception is thrown.
0108   /// @param index The index of the space point to access.
0109   /// @return A const reference to the column entry at the given index.
0110   /// @throws std::out_of_range if the index is out of range.
0111   const Value &at(Index index) const {
0112     if (index >= column().size()) {
0113       throw std::out_of_range("Index out of range in SpacePointContainer2: " +
0114                               std::to_string(index) +
0115                               " >= " + std::to_string(size()));
0116     }
0117     return data()[index];
0118   }
0119 
0120   /// Returns a mutable reference to the column entry at the given index.
0121   /// @param index The index of the space point to access.
0122   /// @return A mutable reference to the column entry at the given index.
0123   Value &operator[](Index index) noexcept
0124     requires(!ReadOnly)
0125   {
0126     assert(index < column().size() && "Index out of bounds");
0127     return data()[index];
0128   }
0129   /// Returns a const reference to the column entry at the given index.
0130   /// @param index The index of the space point to access.
0131   /// @return A const reference to the column entry at the given index.
0132   const Value &operator[](Index index) const noexcept {
0133     assert(index < column().size() && "Index out of bounds");
0134     return data()[index];
0135   }
0136 
0137   using Subset = ContainerSubset<Column, Value, Index, ReadOnly>;
0138 
0139   Subset subset(const IndexSubset &subset) const noexcept {
0140     return Subset(*m_column, subset);
0141   }
0142 
0143  private:
0144   Container *m_container{};
0145   Column *m_column{};
0146 
0147   std::uint32_t size() const noexcept { return column().size(); }
0148 
0149   std::vector<Value> &column() noexcept
0150     requires(!ReadOnly)
0151   {
0152     return *m_column;
0153   }
0154 
0155   friend class SpacePointContainer2;
0156 };
0157 
0158 template <typename T>
0159 using ConstSpacePointColumnProxy = SpacePointColumnProxy<T, true>;
0160 template <typename T>
0161 using MutableSpacePointColumnProxy = SpacePointColumnProxy<T, false>;
0162 
0163 }  // namespace Acts::Experimental