ATLAS Offline Software
AssociationMap.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 template<class OBJCONT,class ASSCONT>
6 const typename AssociationMap<OBJCONT,ASSCONT>::asso_store
7  AssociationMap<OBJCONT,ASSCONT>::s_dum_asso_store;
8 
9 /////////////////////////////////
10 // Constructors and Destructor //
11 /////////////////////////////////
12 
13 template<class OBJCONT,class ASSCONT>
14 AssociationMap<OBJCONT,ASSCONT>::AssociationMap()
15 { }
16 
17 template<class OBJCONT,class ASSCONT>
18 AssociationMap<OBJCONT,ASSCONT>::~AssociationMap()
19 {
20  m_associationMap.clear();
21 }
22 
23 ////////////////////////////////////
24 // Add Association Implementation //
25 ////////////////////////////////////
26 
27 // add using indices
28 template<class OBJCONT,class ASSCONT>
29 void
30 AssociationMap<OBJCONT,ASSCONT>::addAssociation(const object_container_type*
31  objectContainer,
32  const object_index_type&
33  objectIndex,
34  const asso_container_type*
35  assoContainer,
36  const asso_index_type&
37  assoIndex)
38 {
39  // object link
40  object_link objectLink( *objectContainer, objectIndex );
41  objectLink.setElement( (*objectContainer)[objectIndex] );
42 
43  // association link
44  asso_link assoLink( *assoContainer, assoIndex );
45  assoLink.setElement( (*assoContainer)[assoIndex]);
46 
47  // add to store
48  this->addToStore(objectLink,assoLink);
49 }
50 
51 // add using pointers
52 template<class OBJCONT,class ASSCONT>
53 void
54 AssociationMap<OBJCONT,ASSCONT>::addAssociation(const object_container_type*
55  objectContainer,
56  const object_type*
57  objectPointer,
58  const asso_container_type*
59  assoContainer,
60  const asso_type*
61  assoPointer)
62 {
63  // add to store
64  this->addToStore( object_link( objectPointer, *objectContainer ),
65  asso_link ( assoPointer, *assoContainer ) );
66 }
67 
68 // add using links
69 template<class OBJCONT,class ASSCONT>
70 void
71 AssociationMap<OBJCONT,ASSCONT>::addAssociation(const object_link&
72  objectLink,
73  const asso_link&
74  assoLink)
75 {
76  // add to store
77  this->addToStore( objectLink, assoLink );
78 }
79 
80 /////////////////////
81 // Iterator Access //
82 /////////////////////
83 
84 // first object
85 template<class OBJCONT,class ASSCONT>
86 inline typename AssociationMap<OBJCONT,ASSCONT>::object_iterator
87 AssociationMap<OBJCONT,ASSCONT>::beginObject() const
88 {
89  return object_iterator(m_associationMap).begin();
90 }
91 
92 // last object ++
93 template<class OBJCONT,class ASSCONT>
94 inline typename AssociationMap<OBJCONT,ASSCONT>::object_iterator
95 AssociationMap<OBJCONT,ASSCONT>::endObject() const
96 {
97  return object_iterator(m_associationMap).end();
98 }
99 
100 template<class OBJCONT,class ASSCONT>
101 inline typename AssociationMap<OBJCONT,ASSCONT>::asso_iterator
102 AssociationMap<OBJCONT,ASSCONT>::beginAssociation(const object_iterator&
103  objectIter ) const
104 {
105  return this->beginAssociation(objectIter.getObject());
106 }
107 
108 template<class OBJCONT,class ASSCONT>
109 inline typename AssociationMap<OBJCONT,ASSCONT>::asso_iterator
110 AssociationMap<OBJCONT,ASSCONT>::endAssociation(const object_iterator&
111  objectIter ) const
112 {
113  return this->endAssociation(objectIter.getObject());
114 }
115 
116 // association begin iterator
117 template<class OBJCONT,class ASSCONT>
118 typename AssociationMap<OBJCONT,ASSCONT>::asso_iterator
119 AssociationMap<OBJCONT,ASSCONT>::beginAssociation(const object_type*
120  objectPointer) const
121 {
122  // find object
123  object_iterator foundIter = this->findObject(objectPointer);
124 
125  // object found
126  if ( foundIter != this->endObject() )
127  {
128  return foundIter.getFirstAssociation();
129  }
130  // dummy end()
131  else
132  {
133  return asso_iterator (s_dum_asso_store, s_dum_asso_store.end());
134  }
135 }
136 
137 // association end iterator
138 template<class OBJCONT,class ASSCONT>
139 typename AssociationMap<OBJCONT,ASSCONT>::asso_iterator
140 AssociationMap<OBJCONT,ASSCONT>::endAssociation(const object_type*
141  objectPointer) const
142 {
143  // find object
144  object_iterator foundIter = this->findObject(objectPointer);
145 
146  // object found
147  if ( foundIter != this->endObject() )
148  {
149  return foundIter.getLastAssociation();
150  }
151  // dummy end()
152  else
153  {
154  return asso_iterator (s_dum_asso_store, s_dum_asso_store.end());
155  }
156 }
157 
158 ////////////////////////////
159 // Store Parameter Access //
160 ////////////////////////////
161 
162 template<class OBJCONT,class ASSCONT>
163 size_t
164 AssociationMap<OBJCONT,ASSCONT>::size(const object_type* objectPointer) const
165 {
166  object_iterator foundIter = this->findObject(objectPointer);
167  if ( foundIter != this->endObject() )
168  {
169  return this->size(foundIter);
170  }
171  return 0;
172 }
173 
174 /////////////
175 // Finders //
176 /////////////
177 
178 
179 // find association for given object
180 template<class OBJCONT,class ASSCONT>
181 typename AssociationMap<OBJCONT,ASSCONT>::asso_iterator
182 AssociationMap<OBJCONT,ASSCONT>::findAssociation(const object_type* objectPointer,
183  const asso_type* assoPointer)
184  const
185 {
186  // find object first
187  object_iterator foundIter = object_iterator(m_associationMap).find(objectPointer);
188  if ( foundIter == this->endObject() )
189  {
190  return asso_iterator (s_dum_asso_store, s_dum_asso_store.end());
191  }
192  // find association
193  return (foundIter.second()).find(assoPointer);
194 }
195 
196 // check on association for a given object
197 template<class OBJCONT,class ASSCONT>
198 bool
199 AssociationMap<OBJCONT,ASSCONT>::containsAssociation(const object_type* objectPointer,
200  const asso_type* assoPointer) const
201 {
202  const object_iterator obj = this->findObject( objectPointer );
203  if ( obj != endObject() ) {
204  return obj.containsAssociation(assoPointer);
205  } else {
206  return false;
207  }
208 }
209 
210 // find if it contains association anywhere
211 template<class OBJCONT,class ASSCONT>
212 bool
213 AssociationMap<OBJCONT,ASSCONT>::containsAssociation(const asso_type*
214  assoPointer)
215  const
216 {
217  const object_iterator lastObj = this->endObject();
218  for ( object_iterator objItr = this->beginObject();
219  objItr != lastObj;
220  ++objItr ) {
221  asso_iterator lastAsso = this->endAssociation( objItr );
222  for ( asso_iterator assItr = this->beginAssociation(objItr);
223  assItr != lastAsso;
224  ++assItr ) {
225  if ( *assItr == assoPointer ) {
226  return true;
227  }
228  }//> end loop over associated objects
229  }//> end loop over objects
230 
231  return false;
232 }
233 
234 // retrieve all objects for a given association
235 template<class OBJCONT,class ASSCONT>
236 bool
237 AssociationMap<OBJCONT,ASSCONT>::getObjects(const asso_type* assoPointer,
238  object_list& theObjects) const
239 {
240  const object_iterator objEnd = this->endObject();
241  for ( object_iterator objItr = this->beginObject();
242  objItr != objEnd;
243  ++objItr ) {
244  const object_type* pObj = objItr.getObject();
245  if ( this->containsAssociation(pObj,assoPointer) ) {
246  theObjects.push_back(pObj);
247  }
248  }
249  return theObjects.size() > 0;
250 }
251 
252 // retrieve all associated-objects for a given object
253 template<class OBJCONT,class ASSCONT>
254 bool
255 AssociationMap<OBJCONT,ASSCONT>::getAssociations(const object_type* objPointer,
256  asso_list& assocs) const
257 {
258  const size_t origSize = assocs.size();
259  const asso_iterator assocEnd = this->endAssociation(objPointer);
260  for ( asso_iterator assocItr = this->beginAssociation(objPointer);
261  assocItr != assocEnd;
262  ++assocItr ) {
263  assocs.push_back( *assocItr );
264  }
265  return assocs.size() > origSize;
266 }
267 
268 ///////////////////////////////////////////////////////////////////
269 /// Protected methods:
270 ///////////////////////////////////////////////////////////////////
271 
272 template<class OBJCONT,class ASSCONT>
273 bool
274 AssociationMap<OBJCONT,ASSCONT>::addToStore(const object_link& objectLink,
275  const asso_link& assoLink)
276 {
277  // check if already in (no association to be stored twice)
278  if ( this->internalFind(objectLink,assoLink) != m_associationMap.end() ) {
279  return false;
280  } else {
281  // store
282  m_associationMap[objectLink].push_back(assoLink);
283  return true;
284  }
285 }
286