2 * Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration.
5 * @file AthContainers/tools/PackedLinkVectorHelper.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Helper functions for managing @c PackedLink variables.
12#ifndef XAOD_STANDALONE
13#include "AthenaKernel/proxyDictFromEventContext.h"
21 * @brief Constructor from an @c IAuxTypeVector directly.
22 * @param linkedVector The @c IAuxTypeVector for the aux variable.
25PackedLinkVectorHelperBase::LinkedVector::LinkedVector (IAuxTypeVector& linkedVector)
26 : m_data (&linkedVector)
32 * @brief Constructor from a container and aux ID.
33 * @param container Container holding the variables.
34 * @param auxid The ID of the PackedLink variable.
37PackedLinkVectorHelperBase::LinkedVector::LinkedVector (AuxVectorData& container, SG::auxid_t auxid)
38 : m_data (std::make_pair (&container, auxid))
44 * @brief Return the @c IAuxTypeVector.
47IAuxTypeVector* PackedLinkVectorHelperBase::LinkedVector::get()
49 // If we already have it, return it.
50 if (m_data.index() == 0)
51 return std::get<0>(m_data);
53 // Look it up from the container.
54 auto [container, auxid] = std::get<1>(m_data);
55 IAuxTypeVector* lv = container->getStore()->linkedVector (auxid);
57 // Remember it to possibly use again.
64 * @brief Return the @c IAuxTypeVector.
68PackedLinkVectorHelperBase::LinkedVector::operator*()
75 * @brief Return the @c IAuxTypeVector.
78IAuxTypeVector* PackedLinkVectorHelperBase::LinkedVector::operator->()
85 * @brief Return the current store from the first valid link in the span.
86 * If there are no valid links, then return the global default.
87 * @param links The span of links.
90auto PackedLinkVectorHelperBase::storeFromSpan ([[maybe_unused]] DataLinkBase_span& links)
94 return xAOD::TActiveEvent::event();
96 if (links.size() > 1) {
97 return links[1].source();
99 return Atlas::proxyDictFromEventContext();
105 * @brief Return a span over all the linked @c DataLinkBase's.
106 * from the linked vector.
107 * @param linkedVec Interface for the linked vector of @c DataLinkBase's.
110auto PackedLinkVectorHelperBase::getLinkBaseSpan (IAuxTypeVector& linkedVec)
113 return DataLinkBase_span (linkedVec.getDataSpan());
118 * @brief Return a span over all the linked @c DataLinkBase's.
119 * from the linked vector.
120 * @param linkedVec Interface for the linked vector of @c DataLinkBase's.
123auto PackedLinkVectorHelperBase::getLinkBaseSpan (const IAuxTypeVector& linkedVec)
124 -> const_DataLinkBase_span
126 return const_DataLinkBase_span (linkedVec.getDataSpan());
131 * @brief Find the collection index in the linked DataLinks for a sgkey.
132 * @param linkedVec How to find the linked vector.
133 * @param sgkey Hashed key for which to search.
134 * @param links Span over the vector of @c DataLinks, as @c DataLinkBase.
135 * May be modified if the vector grows.
136 * @param sg The @c IProxyDict of the current store.
137 * May be null to use the global, thread-local default.
139 * Searches for a @c DataLink matching @c sgkey in the linked vector.
140 * If not found, then a new entry is added.
141 * Returns a pair (INDEX, CACHEVALID). INDEX is the index in the vector
142 * of the sgkey (and thus the collection index to store in the @c PackedLink).
143 * CACHEVALID is true if it is known that the payload of the vector
144 * has not moved. (If this is false, any caches/iterators must be assumed
149std::pair<size_t, bool> PackedLinkVectorHelper<CONT>::findCollection
150 (LinkedVector& linkedVec,
151 sgkey_t sgkey, DataLinkBase_span& links, IProxyDict* sg)
153 return findCollectionBase (linkedVec, sgkey, links, sg, initLink);
158 * @brief Update collection index of a collection of @c PackedLink.
159 * @param linkedVec Interface for the linked vector of DataLinks.
160 * @param ptr Pointer to the start of the PackedLink collection.
161 * @param n Length of the PackedLink collection.
162 * @param srcDLinks Span over the vector of DataLinks
163 * for the source container of the links.
164 * @param sg The @c IProxyDict of the current store.
165 * If null, take it from the links in @c srcDlinks,
166 * or use the global, thread-local default.
168 * To be used after links have been copied/moved from one container
169 * to another. The collection indices are updated to be appropriate
170 * for the destination container.
171 * Returns true if it is known that the payload of the linked vector
172 * has not moved. (If this is false, any caches/iterators must be assumed
177bool PackedLinkVectorHelper<CONT>::updateLinks (IAuxTypeVector& linkedVec,
178 SG::PackedLink<CONT>* ptr,
180 const const_DataLinkBase_span& srcDLinks,
183 PackedLinkBase_span span (static_cast<PackedLinkBase*>(ptr), n);
184 return updateLinksBase (linkedVec, span, srcDLinks, sg, initLink);
189 * @brief Update collection index of a collection of @c PackedLink.
190 * @param linkedVec Interface for the linked vector of DataLinks.
191 * @param ptr Pointer to the start of the PackedLink collection.
192 * @param n Length of the PackedLink collection.
193 * @param srclv Interface for the linked vector
194 * for the source container of the links.
195 * @param sg The @c IProxyDict of the current store.
196 * If null, take it from the links in @c srcDlinks,
197 * or use the global, thread-local default.
199 * To be used after links have been copied/moved from one container
200 * to another. The collection indices are updated to be appropriate
201 * for the destination container.
202 * Returns true if it is known that the payload of the linked vector
203 * has not moved. (If this is false, any caches/iterators must be assumed
208bool PackedLinkVectorHelper<CONT>::updateLinks (IAuxTypeVector& linkedVec,
209 SG::PackedLink<CONT>* ptr,
211 const IAuxTypeVector& srclv,
214 const_DataLinkBase_span srcDLinks = getLinkBaseSpan (srclv);
215 PackedLinkBase_span span (static_cast<PackedLinkBase*>(ptr), n);
216 return updateLinksBase (linkedVec, span, srcDLinks, sg, initLink);
221 * @brief Update collection index of a collection of @c PackedLink vectors.
222 * @param linkedVec Interface for the linked vector of DataLinks.
223 * @param ptr Pointer to the start of the PackedLink vector collection.
224 * @param n Length of the PackedLink vector collection.
225 * @param srclv Interface for the linked vector
226 * for the source container of the links.
227 * @param sg The @c IProxyDict of the current store.
228 * If null, take it from the links in @c srcDlinks,
229 * or use the global, thread-local default.
231 * To be used after links have been copied/moved from one container
232 * to another. The collection indices are updated to be appropriate
233 * for the destination container.
234 * Returns true if it is known that the payload of the linked vector
235 * has not moved. (If this is false, any caches/iterators must be assumed
239template <class VALLOC>
240bool PackedLinkVectorHelper<CONT>::updateLinks (IAuxTypeVector& linkedVec,
241 std::vector<SG::PackedLink<CONT>, VALLOC>* ptr,
243 const IAuxTypeVector& srclv,
246 const_DataLinkBase_span srcDLinks = getLinkBaseSpan (srclv);
248 DataLinkBase_span DLinks (getLinkBaseSpan (linkedVec));
249 sg = storeFromSpan (DLinks);
251 bool cacheValid = true;
252 for (size_t i = 0; i < n; i++) {
253 PackedLinkBase_span span (static_cast<PackedLinkBase*>(ptr[i].data()),
255 cacheValid &= updateLinksBase (linkedVec, span, srcDLinks, sg, initLink);
262 * @brief Assign a range of @c ElementLink to a vector of @c PackedLink.
263 * @param vect The vector of @c PackedLink to which to assign.
264 * @param linkedVec Interface for the linked vector of DataLinks.
265 * @param dlinks Span over the link vector, as @c DataLinkBase.
266 * @param x The range to assign.
268 * Returns true if it is known that the payload of the linked vector
269 * has not moved. (If this is false, any caches/iterators must be assumed
273template <class VALLOC, ElementLinkRange<CONT> RANGE>
275PackedLinkVectorHelper<CONT>::assignVElt (std::vector<PLink_t, VALLOC>& velt,
276 IAuxTypeVector& linkedVec,
277 DataLinkBase_span& dlinks,
280 // Define a ElementLink->PackedLink transform.
281 bool cacheValid = true;
282 LinkedVector lv (linkedVec);
283 auto xform = [&] (const Link_t& el) {
284 auto [dlindex, flag] = findCollection (lv,
289 return PackedLink<CONT> (dlindex, el.isDefault() ? 0 : el.index());
292 // Make a transformed range and assign it to the vector.
293#ifdef __cpp_lib_containers_ranges // c++23
294 velt.assign_range (x | std::views::transform (xform));
296 const auto r = x | std::views::transform (xform);
297 velt.assign (r.begin(), r.end());
304 * @brief Insert a range of @c ElementLink into a vector of @c PackedLink.
305 * @param vect The vector of @c PackedLink to which to assign.
306 * @param pos The position at which to do the insertion.
307 * @param linkedVec Interface for the linked vector of DataLinks.
308 * @param dlinks Span over the link vector, as @c DataLinkBase.
309 * @param x The range to assign.
311 * Returns true if it is known that the payload of the linked vector
312 * has not moved. (If this is false, any caches/iterators must be assumed
316template <class VALLOC, ElementLinkRange<CONT> RANGE>
318PackedLinkVectorHelper<CONT>::insertVElt (std::vector<PLink_t, VALLOC>& velt,
320 IAuxTypeVector& linkedVec,
321 DataLinkBase_span& dlinks,
324 // Define a ElementLink->PackedLink transform.
325 bool cacheValid = true;
326 LinkedVector lv (linkedVec);
327 auto xform = [&] (const Link_t& el) {
328 auto [dlindex, flag] = findCollection (lv,
333 return PackedLink<CONT> (dlindex, el.isDefault() ? 0 : el.index());
336 // Make a transformed range and assign it to the vector.
337#ifdef __cpp_lib_containers_ranges // c++23
338 velt.insert_range (velt.begin()+pos, x | std::views::transform (xform));
340 const auto r = x | std::views::transform (xform);
341 velt.insert (velt.begin()+pos, r.begin(), r.end());
347#ifndef XAOD_STANDALONE
349 * @brief Apply thinning to packed links, to prepare them for output.
350 * @param linkedVec Interface for the linked vector of @c DataLinks.
351 * @param ptr Pointer to the start of the PackedLink collection.
352 * @param n Length of the PackedLink collection.
353 * @param dlinks Span over the source link vector, as @c DataLinkBase.
354 * @param tc The @c ThinningCache for this object, if it exists.
355 * @param sg The @c IProxyDict of the current store.
356 * If null, take it from the links in @c srcDlinks,
357 * or use the global, thread-local default.
359 * Returns true if it is known that the payload of the linked vector
360 * has not moved. (If this is false, any caches/iterators must be assumed
366PackedLinkVectorHelper<CONT>::applyThinning (IAuxTypeVector& linkedVec,
367 PackedLink<CONT>* ptr,
369 DataLinkBase_span& dlinks,
370 const SG::ThinningCache* tc,
373 PackedLinkBase_span lspan (static_cast<PackedLinkBase*>(ptr), n);
374 return applyThinningBase (linkedVec, lspan, dlinks, tc, sg, initLink);
380 * @brief Initialize a @c DataLink.
381 * @param dl The link to initialize. Really of type @c DLink_t.
382 * @param sgkey Hashed key to which to initialize the link.
383 * @param sg The @c IProxyDict of the current store.
384 * May be null to use the global, thread-local default.
388void PackedLinkVectorHelper<CONT>::initLink (DataLinkBase& dl,
390 [[maybe_unused]] IProxyDict* sg)
392#ifdef XAOD_STANDALONE
393 dl.setPersKey (sgkey);
395 static_cast<DLink_t&>(dl).toIdentifiedObject (sgkey, sg);
400} // namespace SG::detail