|
| 1 | +// This file is part of the ACTS project. |
| 2 | +// |
| 3 | +// Copyright (C) 2016 CERN for the benefit of the ACTS project |
| 4 | +// |
| 5 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 8 | + |
| 9 | +#pragma once |
| 10 | + |
| 11 | +#include "Acts/EventData/Types.hpp" |
| 12 | +#include "Acts/Utilities/TypeTraits.hpp" |
| 13 | + |
| 14 | +#include <cassert> |
| 15 | +#include <span> |
| 16 | +#include <vector> |
| 17 | + |
| 18 | +namespace Acts::Experimental { |
| 19 | + |
| 20 | +class SpacePointContainer2; |
| 21 | + |
| 22 | +/// Additional column of data that can be added to the space point container. |
| 23 | +/// The column is indexed by the space point index. |
| 24 | +template <typename T, bool read_only> |
| 25 | +class SpacePointColumnProxy { |
| 26 | + public: |
| 27 | + constexpr static bool ReadOnly = read_only; |
| 28 | + using Index = SpacePointIndex2; |
| 29 | + using Value = const_if_t<ReadOnly, T>; |
| 30 | + using Container = const_if_t<ReadOnly, SpacePointContainer2>; |
| 31 | + using Column = const_if_t<ReadOnly, std::vector<T>>; |
| 32 | + using Span = std::span<Value>; |
| 33 | + using Reference = Value &; |
| 34 | + |
| 35 | + SpacePointColumnProxy(Container &container, Column &column) |
| 36 | + : m_container{&container}, m_column(&column) {} |
| 37 | + |
| 38 | + /// Gets the container holding the space point. |
| 39 | + /// @return A reference to the container holding the space point. |
| 40 | + SpacePointContainer2 &container() const noexcept |
| 41 | + requires(!ReadOnly) |
| 42 | + { |
| 43 | + return *m_container; |
| 44 | + } |
| 45 | + /// Gets the container holding the space point. |
| 46 | + /// @return A const reference to the container holding the space point. |
| 47 | + const SpacePointContainer2 &container() const noexcept { |
| 48 | + return *m_container; |
| 49 | + } |
| 50 | + |
| 51 | + /// Returns a const reference to the column container. |
| 52 | + /// @return A const reference to the column container. |
| 53 | + const Column &column() const noexcept { return *m_column; } |
| 54 | + |
| 55 | + /// Returns a span to the column data. |
| 56 | + /// @return A span to the column data. |
| 57 | + Span data() const noexcept { return Span(column().data(), column().size()); } |
| 58 | + |
| 59 | + /// Returns a reference to the column entry at the given index. |
| 60 | + /// If the index is out of range, an exception is thrown. |
| 61 | + /// @param index The index of the space point to access. |
| 62 | + /// @return A reference to the column entry at the given index. |
| 63 | + /// @throws std::out_of_range if the index is out of range. |
| 64 | + Reference at(Index index) const { |
| 65 | + if (index >= column().size()) { |
| 66 | + throw std::out_of_range("Index out of range"); |
| 67 | + } |
| 68 | + return data()[index]; |
| 69 | + } |
| 70 | + |
| 71 | + /// Returns a reference to the column entry at the given index. |
| 72 | + /// @param index The index of the space point to access. |
| 73 | + /// @return A reference to the column entry at the given index. |
| 74 | + Reference operator[](Index index) const noexcept { |
| 75 | + assert(index < column().size() && "Index out of bounds"); |
| 76 | + return data()[index]; |
| 77 | + } |
| 78 | + |
| 79 | + private: |
| 80 | + Container *m_container{}; |
| 81 | + Column *m_column{}; |
| 82 | + |
| 83 | + Column &column() const noexcept |
| 84 | + requires(!ReadOnly) |
| 85 | + { |
| 86 | + return *m_column; |
| 87 | + } |
| 88 | + |
| 89 | + friend class SpacePointContainer2; |
| 90 | +}; |
| 91 | + |
| 92 | +template <typename T> |
| 93 | +using ConstSpacePointColumnProxy = SpacePointColumnProxy<T, true>; |
| 94 | +template <typename T> |
| 95 | +using MutableSpacePointColumnProxy = SpacePointColumnProxy<T, false>; |
| 96 | + |
| 97 | +} // namespace Acts::Experimental |
0 commit comments