ATLAS Offline Software
PackedLinkVector.icc
Go to the documentation of this file.
1 /**
2  * @file AthContainers/tools/PackedLinkVector.icc
3  * @author scott snyder <snyder@bnl.gov>
4  * @date Sep, 2023
5  * @brief Implementation of @c IAuxTypeVector for @c PackedLink types.
6  */
7 
8 
9 #include "AthContainers/AuxTypeRegistry.h"
10 
11 
12 namespace SG {
13 
14 
15 /**
16  * @brief Constructor.
17  * @param auxid The auxid of the variable this vector represents.
18  * @param vecPtr Pointer to the object.
19  * @param linkedVec Interface for the linked vector of DataLinks.
20  * @param ownFlag If true, take ownership of the object.
21  */
22 template <class CONT, class ALLOC>
23 PackedLinkVectorHolder<CONT, ALLOC>::PackedLinkVectorHolder
24  (auxid_t auxid,
25  vector_type* vecPtr,
26  IAuxTypeVector* linkedVec,
27  bool ownFlag)
28  : Base (auxid, vecPtr, ownFlag, false),
29  m_linkedVec (linkedVec)
30 {
31 }
32 
33 
34 /**
35  * @brief Insert elements into the vector via move semantics.
36  * @param pos The starting index of the insertion.
37  * @param src Start of the vector containing the range of elements to insert.
38  * @param src_pos Position of the first element to insert.
39  * @param src_n Number of elements to insert.
40  * @param srcStore The source store.
41  *
42  * @c beg and @c end define a range of container elements, with length
43  * @c len defined by the difference of the pointers divided by the
44  * element size.
45  *
46  * The size of the container will be increased by @c len, with the elements
47  * starting at @c pos copied to @c pos+len.
48  *
49  * The contents of the source range will then be moved to our vector
50  * starting at @c pos. This will be done via move semantics if possible;
51  * otherwise, it will be done with a copy.
52  *
53  * Returns true if it is known that the vector's memory did not move,
54  * false otherwise.
55  */
56 template <class CONT, class ALLOC>
57 bool PackedLinkVectorHolder<CONT, ALLOC>::insertMove
58  (size_t pos, void* src, size_t src_pos, size_t src_n,
59  IAuxStore& srcStore)
60 {
61  bool ret = Base::insertMove (pos, src, src_pos, src_n, srcStore);
62  auto srcp = reinterpret_cast<element_type*> (src);
63  auto begp = srcp + src_pos;
64  auto endp = begp + src_n;
65  size_t n = std::distance (begp, endp);
66  vector_type& vec = this->vec();
67 
68  const IAuxTypeVector* srcLv = srcStore.linkedVector (this->auxid());
69  typename Helper::const_DataLinkBase_span srcDLinks = Helper::getLinkBaseSpan (*srcLv);
70  ret &= Helper::updateLinks (*m_linkedVec, vec.data()+pos, n,
71  srcDLinks, nullptr);
72  return ret;
73 }
74 
75 
76 //*****************************************************************************
77 
78 
79 /**
80  * @brief Constructor.
81  * @param auxid The auxid of the variable this vector represents.
82  * @param vecPtr Pointer to the object (of type @c CONT).
83  * @param linkedVec Interface for the linked vector of DataLinks.
84  * @param ownFlag If true, take ownership of the object.
85  */
86 template <class CONT, class VALLOC, class VELT, class ALLOC>
87 PackedLinkVVectorHolder<CONT, VALLOC, VELT, ALLOC>::PackedLinkVVectorHolder
88  (auxid_t auxid,
89  vector_type* vecPtr,
90  IAuxTypeVector* linkedVec,
91  bool ownFlag)
92  : Base (auxid, vecPtr, ownFlag, false),
93  m_linkedVec (linkedVec)
94 {
95 }
96 
97 
98 /**
99  * @brief Insert elements into the vector via move semantics.
100  * @param pos The starting index of the insertion.
101  * @param src Start of the vector containing the range of elements to insert.
102  * @param src_pos Position of the first element to insert.
103  * @param src_n Number of elements to insert.
104  * @param srcStore The source store.
105  *
106  * @c beg and @c end define a range of container elements, with length
107  * @c len defined by the difference of the pointers divided by the
108  * element size.
109  *
110  * The size of the container will be increased by @c len, with the elements
111  * starting at @c pos copied to @c pos+len.
112  *
113  * The contents of the source range will then be moved to our vector
114  * starting at @c pos. This will be done via move semantics if possible;
115  * otherwise, it will be done with a copy.
116  *
117  * Returns true if it is known that the vector's memory did not move,
118  * false otherwise.
119  */
120 template <class CONT, class VALLOC, class VELT, class ALLOC>
121 bool PackedLinkVVectorHolder<CONT, VALLOC, VELT, ALLOC>::insertMove
122  (size_t pos, void* src, size_t src_pos, size_t src_n,
123  IAuxStore& srcStore)
124 {
125  bool ret = Base::insertMove (pos, src, src_pos, src_n, srcStore);
126  auto srcp = reinterpret_cast<element_type*> (src);
127  auto begp = srcp + src_pos;
128  auto endp = begp + src_n;
129  size_t n = std::distance (begp, endp);
130  vector_type& vec = this->vec();
131  const IAuxTypeVector* srcLv = srcStore.linkedVector (this->auxid());
132  typename Helper::const_DataLinkBase_span srcDLinks = Helper::getLinkBaseSpan (*srcLv);
133 
134  for (size_t i = pos; i < pos+n; ++i) {
135  ret &= Helper::updateLinks (*m_linkedVec, vec[i].data(), vec[i].size(),
136  srcDLinks, nullptr);
137  }
138  return ret;
139 }
140 
141 
142 //*****************************************************************************
143 
144 
145 /**
146  * @brief Constructor. Makes a new vector.
147  * @param auxid The auxid of the variable this vector represents.
148  * @param size Initial size of the new vector.
149  * @param capacity Initial capacity of the new vector.
150  * @param linkedVec Ownership of the linked vector.
151  */
152 template <class HOLDER>
153 PackedLinkVectorT<HOLDER>::PackedLinkVectorT (auxid_t auxid,
154  size_t size,
155  size_t capacity,
156  std::unique_ptr<IAuxTypeVector> linkedVec)
157  : Base (auxid, &m_vec, linkedVec.get(), false),
158  m_linkedVecHolder (std::move (linkedVec))
159 {
160  m_vec.reserve (capacity);
161  m_vec.resize (size);
162 }
163 
164 
165 /**
166  * @brief Copy constructor.
167  */
168 template <class HOLDER>
169 PackedLinkVectorT<HOLDER>::PackedLinkVectorT (const PackedLinkVectorT& other)
170  : Base (other.auxid(), &m_vec, false, false),
171  m_vec (other.m_vec),
172  m_linkedVecHolder (other.m_linkedVec->clone())
173 {
174  Base::m_linkedVec = m_linkedVecHolder.get();
175 }
176 
177 
178 /**
179  * @brief Move constructor.
180  */
181 template <class HOLDER>
182 PackedLinkVectorT<HOLDER>::PackedLinkVectorT (PackedLinkVectorT&& other)
183  : Base (other.auxid(), &m_vec, false, other.m_linkedVec),
184  m_vec (std::move (other.m_vec)),
185  m_linkedVecHolder (std::move (other.m_linkedVecHolder))
186 {
187 }
188 
189 
190 /**
191  * @brief Make a copy of this vector.
192  */
193 template <class HOLDER>
194 inline
195 std::unique_ptr<IAuxTypeVector> PackedLinkVectorT<HOLDER>::clone() const
196 {
197  return std::make_unique<PackedLinkVectorT> (*this);
198 }
199 
200 
201 /**
202  * @brief Return ownership of the linked vector.
203  */
204 template <class HOLDER>
205 inline
206 std::unique_ptr<IAuxTypeVector> PackedLinkVectorT<HOLDER>::linkedVector()
207 {
208  return std::move (m_linkedVecHolder);
209 }
210 
211 
212 } // namespace SG