ATLAS Offline Software
PackedLinkConversions.icc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
3  */
4 /**
5  * @file AthContainers/tools/PackedLinkConversions.icc
6  * @author scott snyder <snyder@bnl.gov>
7  * @date Jun, 2024
8  * @brief Conversions between PackedLink and ElementLink.
9  */
10 
11 
12 namespace SG { namespace detail {
13 
14 
15 /**
16  * @brief Constructor.
17  * @param dlinks Span over DataLinks.
18  */
19 template <class CONT>
20 inline
21 PackedLinkConstConverter<CONT>::PackedLinkConstConverter
22  (const const_DataLink_span& dlinks)
23  : m_dlinks (dlinks)
24 {
25 }
26 
27 
28 /**
29  * @brief Transform a PackedLink to an ElementLink.
30  * @param plink The link to transform.
31  */
32 template <class CONT>
33 inline
34 auto PackedLinkConstConverter<CONT>::operator()
35  (const PackedLinkBase& plink) const -> const value_type
36 {
37  unsigned int coll = plink.collection();
38  // We need to special-case this: if there were no non-null links,
39  // then the DataLink collection may not have been filled.
40  if (coll == 0) {
41  return value_type();
42  }
43 #ifdef XAOD_STANDALONE
44  return value_type (m_dlinks.at(coll).key(), plink.index());
45 #else
46  return value_type (m_dlinks.at(coll), plink.index());
47 #endif
48 }
49 
50 
51 //****************************************************************************
52 
53 
54 /**
55  * @brief Constructor.
56  * @param dlinks Span over DataLinks.
57  */
58 template <class CONT>
59 inline
60 PackedLinkVectorConstConverter<CONT>::PackedLinkVectorConstConverter
61  (const const_DataLink_span& dlinks)
62  : m_dlinks (dlinks)
63 {
64 }
65 
66 
67 /**
68  * @brief Transform a vector of PackedLinks to a span over ElementLinks.
69  * @param velt The vector to transform.
70  */
71 template <class CONT>
72 template <class VALLOC>
73 inline
74 auto PackedLinkVectorConstConverter<CONT>::operator()
75  (const std::vector<PackedLink<CONT>, VALLOC>& velt) const -> value_type
76 {
77  return value_type (CxxUtils::make_span (velt), m_dlinks);
78 }
79 
80 
81 //****************************************************************************
82 
83 
84 /**
85  * @brief Constructor.
86  * @param container Container holding the variables.
87  * @param auxid The ID of the PackedLink variable.
88  * @param linked_auxid The ID of the linked variable of DataLinks.
89  */
90 template <class CONT>
91 inline
92 PackedLinkConverter<CONT>::PackedLinkConverter (AuxVectorData& container,
93  SG::auxid_t auxid,
94  SG::auxid_t linked_auxid)
95  : m_container (container),
96  m_linkedVec (container, auxid),
97  m_dlinks (*container.getDataSpan (linked_auxid))
98 {
99  // cppcheck-suppress missingReturn; false positive
100 }
101 
102 
103 /**
104  * @brief Convert a PackedLink to an ElementLink.
105  * @param plink The link to transform.
106  */
107 template <class CONT>
108 auto PackedLinkConverter<CONT>::operator() (const PackedLinkBase& plink) const
109  -> const value_type
110 {
111  unsigned int coll = plink.collection();
112  // We need to special-case this: if there were no non-null links,
113  // then the DataLink collection may not have been filled.
114  if (coll == 0) {
115  return value_type();
116  }
117 #ifdef XAOD_STANDALONE
118  return value_type (m_dlinks.at(coll).key(), plink.index());
119 #else
120  return value_type (static_cast<const DLink_t&>(m_dlinks.at(coll)), plink.index());
121 #endif
122 }
123 
124 
125 /**
126  * @brief Convert an ElementLink to a PackedLink.
127  * @param pl The destination PackedLink.
128  * @param link The link to transform.
129  */
130 template <class CONT>
131 void PackedLinkConverter<CONT>::set (PackedLinkBase& pl, const value_type& link)
132 {
133  pl.setIndex (link.isDefault() ? 0 : link.index());
134  auto [dlindex, cacheValid] = PLVH::findCollection (m_linkedVec,
135  link.key(),
136  m_dlinks,
137  link.source());
138  pl.setCollection (dlindex);
139  if (!cacheValid) m_container.clearCache (m_linkedVec->auxid());
140 }
141 
142 
143 /**
144  * @brief Convert a range of ElementLinks to a vector of PackedLinks.
145  * @param plv The destination vector of PackedLinks.
146  * @param r The range of ElementLinks.
147  */
148 template <class CONT>
149 template <class VALLOC, ElementLinkRange<CONT> RANGE>
150 void PackedLinkConverter<CONT>::set (std::vector<PLink_t, VALLOC>& plv,
151  const RANGE& r)
152 {
153  if (!PLVH::template assignVElt (plv, *m_linkedVec, m_dlinks, r)) {
154  m_container.clearCache (m_linkedVec->auxid());
155  }
156 }
157 
158 
159 /**
160  * @brief Insert a range of ElementLinks into a vector of PackedLinks.
161  * @param plv The destination vector of PackedLinks.
162  * @param pos Position in the container at which to insert the range.
163  * @param r The range of ElementLinks.
164  */
165 template <class CONT>
166 template <class VALLOC, ElementLinkRange<CONT> RANGE>
167 void PackedLinkConverter<CONT>::insert (std::vector<PLink_t, VALLOC>& plv,
168  size_t pos,
169  const RANGE& r)
170 {
171  if (!PLVH::template insertVElt (plv, pos, *m_linkedVec, m_dlinks, r)) {
172  m_container.clearCache (m_linkedVec->auxid());
173  }
174 }
175 
176 
177 } } // namespace SG::detail