Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-27 07:24:05

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 // Project include(s)
0012 #include "detray/definitions/detail/qualifiers.hpp"
0013 #include "detray/utils/ranges/ranges.hpp"
0014 
0015 namespace detray::ranges {
0016 
0017 /// @brief Struct that implements a view on a single element.
0018 ///
0019 /// @see https://en.cppreference.com/w/cpp/ranges/single_view
0020 ///
0021 /// @tparam value_t type of the single element (outside of a container)
0022 ///
0023 /// @note Does not take ownership of the value it operates on. Its lifetime
0024 /// needs to be guaranteed throughout iteration or between iterations with the
0025 /// same view instance.
0026 /// @note Is not fit for lazy evaluation.
0027 template <typename value_t>
0028 class single_view
0029     : public detray::ranges::view_interface<single_view<value_t>> {
0030  public:
0031   using iterator_t = value_t*;
0032 
0033   /// Default constructor
0034   constexpr single_view() = default;
0035 
0036   /// Construct iterator from the single @param value - copy
0037   DETRAY_HOST_DEVICE constexpr explicit single_view(const value_t& value)
0038       : m_value{value} {}
0039 
0040   /// Construct iterator from the single @param value - move
0041   DETRAY_HOST_DEVICE constexpr explicit single_view(value_t&& value)
0042       : m_value{std::move(value)} {}
0043 
0044   /// Construct value in place from @param args
0045   template <class... Args>
0046   DETRAY_HOST_DEVICE constexpr explicit single_view(std::in_place_t /*tag*/,
0047                                                     Args&&... args)
0048       : m_value{std::in_place, std::forward<Args>(args)...} {}
0049 
0050   /// @return the single value
0051   DETRAY_HOST_DEVICE
0052   constexpr auto value() const -> value_t { return m_value; }
0053 
0054   /// @return the single value
0055   DETRAY_HOST_DEVICE
0056   constexpr auto ref() const -> const value_t& { return m_value; }
0057   DETRAY_HOST_DEVICE
0058   constexpr auto ref() -> value_t& { return m_value; }
0059 
0060   /// @returns value pointer.
0061   DETRAY_HOST_DEVICE
0062   constexpr auto begin() noexcept -> value_t* { return &m_value; }
0063 
0064   /// @returns value pointer - const
0065   DETRAY_HOST_DEVICE
0066   constexpr auto begin() const noexcept -> const value_t* { return &m_value; }
0067 
0068   /// @returns sentinel position.
0069   DETRAY_HOST_DEVICE
0070   constexpr auto end() noexcept -> value_t* { return &m_value + 1; }
0071 
0072   /// @returns sentinel position - const
0073   DETRAY_HOST_DEVICE
0074   constexpr auto end() const noexcept -> const value_t* { return &m_value + 1; }
0075 
0076   /// @returns a pointer to the beginning of the data
0077   DETRAY_HOST_DEVICE
0078   constexpr auto data() noexcept -> value_t* { return &m_value; }
0079 
0080   /// @returns a pointer to the beginning of the data - const
0081   DETRAY_HOST_DEVICE
0082   constexpr auto data() const noexcept -> const value_t* { return &m_value; }
0083 
0084   /// @returns the size of the single view, which is always 'one'.
0085   DETRAY_HOST_DEVICE
0086   static constexpr auto size() noexcept -> std::size_t { return 1; }
0087 
0088   /// @returns the value directly
0089   DETRAY_HOST_DEVICE
0090   constexpr auto front() noexcept -> value_t { return m_value; }
0091 
0092   /// @returns the value directly
0093   DETRAY_HOST_DEVICE
0094   constexpr auto back() noexcept -> value_t { return m_value; }
0095 
0096   /// @returns the value directly
0097   DETRAY_HOST_DEVICE constexpr auto operator[](const dindex /*unused*/) const
0098       -> value_t {
0099     return m_value;
0100   }
0101 
0102  private:
0103   value_t m_value{};
0104 };
0105 
0106 namespace views {
0107 
0108 /// @brief interface type to construct a @c single_view with CTAD
0109 template <typename value_t>
0110 struct single : public detray::ranges::single_view<value_t> {
0111   using base_type = detray::ranges::single_view<value_t>;
0112 
0113   constexpr single() = default;
0114 
0115   template <typename deduced_value_t>
0116     requires(!std::same_as<deduced_value_t, single>)
0117   DETRAY_HOST_DEVICE constexpr explicit single(const deduced_value_t& value)
0118       : base_type(value) {}
0119 
0120   template <typename deduced_value_t>
0121     requires(!std::same_as<deduced_value_t, single>)
0122   DETRAY_HOST_DEVICE constexpr explicit single(deduced_value_t&& value)
0123       : base_type(std::forward<deduced_value_t>(value)) {}
0124 
0125   template <class... Args>
0126   DETRAY_HOST_DEVICE constexpr explicit single(std::in_place_t /*unused*/,
0127                                                Args&&... args)
0128       : base_type(std::in_place /*tag*/, std::forward<Args>(args)... /*args*/) {
0129   }
0130 };
0131 
0132 // deduction guides
0133 
0134 template <typename deduced_value_t>
0135 DETRAY_HOST_DEVICE single(deduced_value_t) -> single<deduced_value_t>;
0136 
0137 }  // namespace views
0138 
0139 }  // namespace detray::ranges