Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-07-26 08:21:57

0001 /** TRACCC library, part of the ACTS project (R&D line)
0002  *
0003  * (c) 2021-2022 CERN for the benefit of the ACTS project
0004  *
0005  * Mozilla Public License Version 2.0
0006  */
0007 
0008 #pragma once
0009 
0010 // Project include(s).
0011 #include "traccc/definitions/qualifiers.hpp"
0012 #include "traccc/edm/details/container_element.hpp"
0013 #include "traccc/utils/pair.hpp"
0014 
0015 // VecMem include(s).
0016 #include <vecmem/memory/memory_resource.hpp>
0017 
0018 // System include(s).
0019 #include <cassert>
0020 #include <stdexcept>
0021 #include <type_traits>
0022 
0023 namespace traccc {
0024 
0025 /// Container base class for describing objects in a given event
0026 ///
0027 /// This is the generic container of the code, holding all relevant
0028 /// information about objcts in a given event.
0029 ///
0030 /// It can be instantiated with different vector types, to be able to use
0031 /// the same container type in both host and device code.
0032 ///
0033 /// It also can be instantiated with different edm types represented by
0034 /// header and item type.
0035 ///
0036 template <typename header_t, typename item_t,
0037           template <typename> class vector_t,
0038           template <typename> class jagged_vector_t,
0039           template <typename, typename> class pair_t = traccc::pair>
0040 class container_base {
0041  public:
0042   /// @name Type definitions
0043   /// @{
0044 
0045   /// Header type
0046   using header_type = header_t;
0047 
0048   /// Item type
0049   using item_type = item_t;
0050 
0051   /// Vector type used by the container
0052   template <typename T>
0053   using vector_type = vector_t<T>;
0054 
0055   /// Jagged vector type used by the container
0056   template <typename T>
0057   using jagged_vector_type = jagged_vector_t<T>;
0058 
0059   /// The header vector type
0060   using header_vector = vector_type<header_type>;
0061 
0062   /// The item vector type
0063   using item_vector = jagged_vector_type<item_type>;
0064 
0065   /**
0066    * @brief The size type of this container, which is the type by which
0067    * its elements are indexed.
0068    */
0069   using size_type = typename header_vector::size_type;
0070 
0071   /// The element link type
0072   using link_type = pair_t<typename header_vector::size_type,
0073                            typename item_vector::size_type>;
0074 
0075   /// @}
0076 
0077   /**
0078    * @brief The type name of the element view which is returned by various
0079    * methods in this class.
0080    */
0081   using element_view = container_element<typename header_vector::reference,
0082                                          typename item_vector::reference>;
0083 
0084   /**
0085    * @brief The type name of the constant element view which is returned
0086    * by various methods in this class.
0087    */
0088   using const_element_view =
0089       container_element<typename header_vector::const_reference,
0090                         typename item_vector::const_reference>;
0091 
0092   /**
0093    * We need to assert that the header vector and the outer layer of the
0094    * jagged vector have the same size type, so they can be indexed using
0095    * the same type.
0096    */
0097   static_assert(
0098       std::is_convertible<typename header_vector::size_type,
0099                           typename item_vector::size_type>::value,
0100       "Size type for container header and item vectors must be the same.");
0101 
0102   /// Default copy-constructor
0103   container_base(const container_base&) = default;
0104   /// Default move-constructor
0105   container_base(container_base&&) noexcept = default;
0106 
0107   /**
0108    * @brief (Copy) Constructor from a header and item vector
0109    *
0110    * To enforce the invariant that both vectors must be the same size, we
0111    * check this in the constructor. This is also checked in release
0112    * builds.
0113    */
0114   TRACCC_HOST
0115   container_base(const header_vector& hv, const item_vector& iv)
0116       : m_headers(hv), m_items(iv) {
0117     if (m_headers.size() != m_items.size()) {
0118       throw std::logic_error("Header and item length not equal.");
0119     }
0120   }
0121 
0122   /**
0123    * @brief (Move) Constructor from a header and item vector
0124    *
0125    * To enforce the invariant that both vectors must be the same size, we
0126    * check this in the constructor. This is also checked in release
0127    * builds.
0128    */
0129   TRACCC_HOST
0130   container_base(header_vector&& hv, item_vector&& iv)
0131       : m_headers(hv), m_items(iv) {
0132     if (m_headers.size() != m_items.size()) {
0133       throw std::logic_error("Header and item length not equal.");
0134     }
0135   }
0136 
0137   /**
0138    * @brief Constructor from a pair of "view type" objects
0139    */
0140   template <typename header_vector_tp, typename item_vector_tp>
0141     requires(std::constructible_from<header_vector, const header_vector_tp&> &&
0142              std::constructible_from<item_vector, const item_vector_tp&>)
0143   TRACCC_HOST_DEVICE container_base(const header_vector_tp& hv,
0144                                     const item_vector_tp& iv)
0145       : m_headers(hv), m_items(iv) {
0146     assert(m_headers.size() == m_items.size());
0147   }
0148 
0149   /**
0150    * @brief Constructor with vector size and memory resource .
0151    */
0152   template <typename size_type>
0153   TRACCC_HOST explicit container_base(size_type size,
0154                                       vecmem::memory_resource* mr)
0155       : m_headers(size, mr), m_items(size, mr) {}
0156 
0157   /**
0158    * @brief Constructor with memory resource .
0159    */
0160 
0161   TRACCC_HOST explicit container_base(vecmem::memory_resource* mr)
0162       : m_headers(mr), m_items(mr) {}
0163 
0164   /**
0165    * @brief Default Constructor
0166    */
0167   container_base() = default;
0168 
0169   /// Default (copy) assignment operator
0170   container_base& operator=(const container_base&) = default;
0171   /// Default (move) assignment operator
0172   container_base& operator=(container_base&&) noexcept = default;
0173 
0174   /**
0175    * @brief Accessor method for the internal header vector.
0176    */
0177   TRACCC_HOST_DEVICE
0178   const header_vector& get_headers() const { return m_headers; }
0179 
0180   /**
0181    * @brief Non-const accessor method for the internal header vector.
0182    *
0183    * @warning Do not use this function! It is dangerous, and risks breaking
0184    * invariants!
0185    */
0186   TRACCC_HOST_DEVICE
0187   header_vector& get_headers() { return m_headers; }
0188 
0189   /**
0190    * @brief Accessor method for the internal item vector-of-vectors.
0191    */
0192   TRACCC_HOST_DEVICE
0193   const item_vector& get_items() const { return m_items; }
0194 
0195   /**
0196    * @brief Non-const accessor method for the internal item vector-of-vectors.
0197    *
0198    * @warning Do not use this function! It is dangerous, and risks breaking
0199    * invariants!
0200    */
0201   TRACCC_HOST_DEVICE
0202   item_vector& get_items() { return m_items; }
0203 
0204  protected:
0205   /// Headers information related to the objects in the event
0206   header_vector m_headers;
0207 
0208   /// All objects in the event
0209   item_vector m_items;
0210 
0211 };  // class container_base
0212 
0213 }  // namespace traccc