ATLAS Offline Software
A/AthLinks/ElementLinkVector.icc
Go to the documentation of this file.
1 // Dear emacs, this is -*- c++ -*-
2 /*
3  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
4  */
5 #ifndef ATHLINKS_ELEMENTLINKVECTOR_ICC
6 #define ATHLINKS_ELEMENTLINKVECTOR_ICC
7 
8 // System include(s):
9 #include <cassert>
10 
11 ////////////////////////////////////////////////////////////////////////////////
12 //
13 // Implementation of the different constructors
14 //
15 
16 template< class CONTAINER >
17 ElementLinkVector< CONTAINER >::ElementLinkVector()
18  : ElementLinkVectorBase(), m_elVec() {
19 
20 }
21 
22 template< class CONTAINER >
23 ElementLinkVector< CONTAINER >::ElementLinkVector( size_type n,
24  const ElemLink& link )
25  : ElementLinkVectorBase(), m_elVec( n, link ) {
26 
27  // Create the persistent information:
28  toPersistent();
29 }
30 
31 template< class CONTAINER >
32 ElementLinkVector< CONTAINER >::ElementLinkVector( const ElemLinkVec& parent )
33  : ElementLinkVectorBase( parent ), m_elVec( parent.m_elVec ) {
34 
35 }
36 
37 //
38 ////////////////////////////////////////////////////////////////////////////////
39 
40 template< class CONTAINER >
41 typename ElementLinkVector< CONTAINER >::ElemLinkVec&
42 ElementLinkVector< CONTAINER >::operator=( const ElemLinkVec& rhs ) {
43 
44  // Copy the contents of the base class:
45  ElementLinkVectorBase::operator=( rhs );
46 
47  // Copy the member variable(s):
48  m_elVec = rhs.m_elVec;
49 
50  // Return this same object:
51  return *this;
52 }
53 
54 template< class CONTAINER >
55 bool ElementLinkVector< CONTAINER >::
56 operator==( const ElementLinkVector& rhs ) const {
57 
58  if( m_elVec.size() != rhs.m_elVec.size() ) {
59  return false;
60  }
61  for( std::size_t i = 0; i < m_elVec.size(); ++i ) {
62  if( m_elVec[ i ] != rhs.m_elVec[ i ] ) {
63  return false;
64  }
65  }
66  return true;
67 }
68 
69 template< class CONTAINER >
70 bool ElementLinkVector< CONTAINER >::
71 operator!=( const ElementLinkVector& rhs ) const {
72 
73  return ( ! ( *this == rhs ) );
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 //
78 // Implementation of the vector capacity functions
79 //
80 
81 template< class CONTAINER >
82 void ElementLinkVector< CONTAINER >::resize( size_type sz,
83  const ElemLink& link ) {
84 
85  // Resize the transient vector:
86  m_elVec.resize( sz, link );
87 
88  // Now re-create the persistent information:
89  toPersistent();
90 
91  // Return gracefully:
92  return;
93 }
94 
95 //
96 ////////////////////////////////////////////////////////////////////////////////
97 
98 ////////////////////////////////////////////////////////////////////////////////
99 //
100 // Implementation of the vector modifier functions
101 //
102 
103 template< class CONTAINER >
104 void ElementLinkVector< CONTAINER >::push_back( const ElemLink& el ) {
105 
106  // Add the new transient object:
107  m_elVec.push_back( el );
108 
109  // Add the persistent info about it as well:
110  m_persKeys.push_back( el.persKey() );
111  m_persIndices.push_back( el.persIndex() );
112 
113  // Return gracefully:
114  return;
115 }
116 
117 template< class CONTAINER >
118 void ElementLinkVector< CONTAINER >::pop_back() {
119 
120  // Remove the last transient object:
121  m_elVec.pop_back();
122 
123  // Remove the last persistent objects:
124  m_persKeys.pop_back();
125  m_persIndices.pop_back();
126 
127  // Return gracefully:
128  return;
129 }
130 
131 template< class CONTAINER >
132 typename ElementLinkVector< CONTAINER >::iterator
133 ElementLinkVector< CONTAINER >::erase( iterator position ) {
134 
135  // Do the deed, and remember the iterator:
136  iterator result = m_elVec.erase( position );
137 
138  // It may be more performant later on to put in dedicated code
139  // here, but for now let's do the simplest thing:
140  toPersistent();
141 
142  // Return the cached iterator:
143  return result;
144 }
145 
146 template< class CONTAINER >
147 typename ElementLinkVector< CONTAINER >::iterator
148 ElementLinkVector< CONTAINER >::erase( iterator first, iterator last ) {
149 
150  // Do the deed, and remember the iterator:
151  iterator result = m_elVec.erase( first, last );
152 
153  // It may be more performant later on to put in dedicated code
154  // here, but for now let's do the simplest thing:
155  toPersistent();
156 
157  // Return the cached iterator:
158  return result;
159 }
160 
161 template< class CONTAINER >
162 void ElementLinkVector< CONTAINER >::swap( ElemLinkVec& vec ) {
163 
164  // Swap the transient variable:
165  m_elVec.swap( vec.m_elVec );
166 
167  // Swap the persistent variables:
168  m_persKeys.swap( vec.m_persKeys );
169  m_persIndices.swap( vec.m_persIndices );
170 
171  // Return gracefully:
172  return;
173 }
174 
175 template< class CONTAINER >
176 template< class InputIterator >
177 void ElementLinkVector< CONTAINER >::assign( InputIterator first,
178  InputIterator last ) {
179 
180  // Achieve the result using other functions of the class:
181  clear();
182  insert( begin(), first, last );
183 }
184 
185 template< class CONTAINER >
186 void ElementLinkVector< CONTAINER >::assign( size_type n,
187  const ElemLink& link ) {
188 
189  // Achieve the result using other functions of the class:
190  clear();
191  insert( begin(), n, link );
192 }
193 
194 template< class CONTAINER >
195 typename ElementLinkVector< CONTAINER >::iterator
196 ElementLinkVector< CONTAINER >::insert( iterator position,
197  const ElemLink& link ) {
198 
199  // Insert the new link:
200  iterator result = m_elVec.insert( position, link );
201 
202  // It may be more performant later on to put in dedicated code
203  // here, but for now let's do the simplest thing:
204  toPersistent();
205 
206  // Return the cached iterator:
207  return result;
208 }
209 
210 template< class CONTAINER >
211 void ElementLinkVector< CONTAINER >::insert( iterator position,
212  size_type n,
213  const ElemLink& link ) {
214 
215  // Insert the links:
216  m_elVec.insert( position, n, link );
217 
218  // It may be more performant later on to put in dedicated code
219  // here, but for now let's do the simplest thing:
220  toPersistent();
221 }
222 
223 template< class CONTAINER >
224 void ElementLinkVector< CONTAINER >::clear() {
225 
226  // Simply clear all variables:
227  m_elVec.clear();
228  m_persKeys.clear();
229  m_persIndices.clear();
230 
231  // Return gracefully:
232  return;
233 }
234 
235 //
236 ////////////////////////////////////////////////////////////////////////////////
237 
238 template< class CONTAINER >
239 bool ElementLinkVector< CONTAINER >::toTransient() {
240 
241  // A little sanity check:
242  assert( m_persKeys.size() == m_persIndices.size() );
243 
244  // Re-create the transient variables based on the
245  // persistent ones:
246  m_elVec.clear();
247  m_elVec.reserve( m_persKeys.size() );
248  for( size_t i = 0; i < m_persKeys.size(); ++i ) {
249  m_elVec.push_back( ElemLink( m_persKeys[ i ], m_persIndices[ i ] ) );
250  }
251 
252  // Return a dummy value:
253  return true;
254 }
255 
256 /// In a few cases the simplest way of syncronising the transient and
257 /// persistent data of the object is to simply re-create the persistent
258 /// data from scratch. This function does this.
259 ///
260 /// @returns <code>true</code> in case of success, and <code>false</code>
261 /// in case of failure
262 ///
263 template< class CONTAINER >
264 bool ElementLinkVector< CONTAINER >::toPersistent() {
265 
266  // First, clear the current persistent info:
267  m_persKeys.clear();
268  m_persIndices.clear();
269 
270  // Loop over all the links in this container:
271  iterator p_itr = begin();
272  iterator p_end = end();
273  for( ; p_itr != p_end; ++p_itr ) {
274 
275  // Ask the link to make itself persistifiable. (Shouldn't do anything
276  // actually, but maybe we'll need to do something special in
277  // ElementLink::toPersistent() at one point...)
278  if( ! p_itr->toPersistent() ) return false;
279 
280  // Copy the link's persistent data into the base class's variables:
281  m_persKeys.push_back( p_itr->persKey() );
282  m_persIndices.push_back( p_itr->persIndex() );
283  }
284 
285  // Return gracefully:
286  return true;
287 }
288 
289 #endif // ATHLINKS_ELEMENTLINKVECTOR_ICC