|
| 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 = T; |
| 30 | + using Container = const_if_t<ReadOnly, SpacePointContainer2>; |
| 31 | + using Column = const_if_t<ReadOnly, std::vector<Value>>; |
| 32 | + |
| 33 | + /// Constructs a space point column proxy for the given container and column. |
| 34 | + /// @param container The container holding the space point. |
| 35 | + /// @param column The column of data to access. |
| 36 | + SpacePointColumnProxy(Container &container, Column &column) |
| 37 | + : m_container{&container}, m_column(&column) {} |
| 38 | + |
| 39 | + /// Copy construct a space point column proxy. |
| 40 | + /// @param other The space point column proxy to copy. |
| 41 | + SpacePointColumnProxy(const SpacePointColumnProxy &other) noexcept = default; |
| 42 | + |
| 43 | + /// Copy construct a mutable space point column proxy. |
| 44 | + /// @param other The mutable space point column proxy to copy. |
| 45 | + explicit SpacePointColumnProxy( |
| 46 | + const SpacePointColumnProxy<T, false> &other) noexcept |
| 47 | + requires ReadOnly |
| 48 | + : m_container(&other.container()), m_column(&other.column()) {} |
| 49 | + |
| 50 | + /// Gets the container holding the space point. |
| 51 | + /// @return A reference to the container holding the space point. |
| 52 | + SpacePointContainer2 &container() noexcept |
| 53 | + requires(!ReadOnly) |
| 54 | + { |
| 55 | + return *m_container; |
| 56 | + } |
| 57 | + /// Gets the container holding the space point. |
| 58 | + /// @return A const reference to the container holding the space point. |
| 59 | + const SpacePointContainer2 &container() const noexcept { |
| 60 | + return *m_container; |
| 61 | + } |
| 62 | + |
| 63 | + /// Returns a const reference to the column container. |
| 64 | + /// @return A const reference to the column container. |
| 65 | + const std::vector<Value> &column() const noexcept { return *m_column; } |
| 66 | + |
| 67 | + /// Returns a mutable span to the column data. |
| 68 | + /// @return A mutable span to the column data. |
| 69 | + std::span<Value> data() noexcept |
| 70 | + requires(!ReadOnly) |
| 71 | + { |
| 72 | + return std::span<Value>(column().data(), column().size()); |
| 73 | + } |
| 74 | + /// Returns a const span to the column data. |
| 75 | + /// @return A const span to the column data. |
| 76 | + std::span<const Value> data() const noexcept { |
| 77 | + return std::span<const Value>(column().data(), column().size()); |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns a mutable reference to the column entry at the given index. |
| 81 | + /// If the index is out of range, an exception is thrown. |
| 82 | + /// @param index The index of the space point to access. |
| 83 | + /// @return A mutable reference to the column entry at the given index. |
| 84 | + /// @throws std::out_of_range if the index is out of range. |
| 85 | + Value &at(Index index) |
| 86 | + requires(!ReadOnly) |
| 87 | + { |
| 88 | + if (index >= column().size()) { |
| 89 | + throw std::out_of_range("Index out of range in SpacePointContainer2: " + |
| 90 | + std::to_string(index) + |
| 91 | + " >= " + std::to_string(size())); |
| 92 | + } |
| 93 | + return data()[index]; |
| 94 | + } |
| 95 | + /// Returns a const reference to the column entry at the given index. |
| 96 | + /// If the index is out of range, an exception is thrown. |
| 97 | + /// @param index The index of the space point to access. |
| 98 | + /// @return A const reference to the column entry at the given index. |
| 99 | + /// @throws std::out_of_range if the index is out of range. |
| 100 | + const Value &at(Index index) const { |
| 101 | + if (index >= column().size()) { |
| 102 | + throw std::out_of_range("Index out of range in SpacePointContainer2: " + |
| 103 | + std::to_string(index) + |
| 104 | + " >= " + std::to_string(size())); |
| 105 | + } |
| 106 | + return data()[index]; |
| 107 | + } |
| 108 | + |
| 109 | + /// Returns a mutable reference to the column entry at the given index. |
| 110 | + /// @param index The index of the space point to access. |
| 111 | + /// @return A mutable reference to the column entry at the given index. |
| 112 | + Value &operator[](Index index) noexcept |
| 113 | + requires(!ReadOnly) |
| 114 | + { |
| 115 | + assert(index < column().size() && "Index out of bounds"); |
| 116 | + return data()[index]; |
| 117 | + } |
| 118 | + /// Returns a const reference to the column entry at the given index. |
| 119 | + /// @param index The index of the space point to access. |
| 120 | + /// @return A const reference to the column entry at the given index. |
| 121 | + const Value &operator[](Index index) const noexcept { |
| 122 | + assert(index < column().size() && "Index out of bounds"); |
| 123 | + return data()[index]; |
| 124 | + } |
| 125 | + |
| 126 | + private: |
| 127 | + Container *m_container{}; |
| 128 | + Column *m_column{}; |
| 129 | + |
| 130 | + std::uint32_t size() const noexcept { return column().size(); } |
| 131 | + |
| 132 | + std::vector<Value> &column() noexcept |
| 133 | + requires(!ReadOnly) |
| 134 | + { |
| 135 | + return *m_column; |
| 136 | + } |
| 137 | + |
| 138 | + friend class SpacePointContainer2; |
| 139 | +}; |
| 140 | + |
| 141 | +template <typename T> |
| 142 | +using ConstSpacePointColumnProxy = SpacePointColumnProxy<T, true>; |
| 143 | +template <typename T> |
| 144 | +using MutableSpacePointColumnProxy = SpacePointColumnProxy<T, false>; |
| 145 | + |
| 146 | +} // namespace Acts::Experimental |
0 commit comments