ATLAS Offline Software
Loading...
Searching...
No Matches
AssociationMap.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef ATHLINKS_ASSOCIATIONMAP_H
6#define ATHLINKS_ASSOCIATIONMAP_H
7
8#define ASSOCIATION_CONTEXT
9#define ASSOCIATIONMAP_CONTEXT
10
15 * associations, typically of a different type (many-to-many look-up).
16 * Concrete objects instantiated from implementation classes derived from
17 * \c AssociationMap are not automatically storable. The concrete class needs
18 * to either be \c DataObject itself, or the corresponding object needs to
19 * be collected into a storable \c DataVector.
20 *
21 * The internal storage model is a matrix, where the rows are keyed by the
22 * object pointer and the columns are pointers to associations. The number of
23 * columns (= associations) typically varies row-by-row.
24 *
25 * \author Peter Loch <loch@physics.arizona.edu>
26 * \date June 1, 2004 - first implementation
27 */
28
29#include "AthLinks/ElementLink.h"
30#include "AthLinks/ElementLinkVector.h"
33#include <map>
34#include <list>
35#include <algorithm>
36
37template<class OBJCONT,class ASSCONT>
39{
40 public:
41
44 typedef OBJCONT object_container_type;
45 typedef typename object_container_type::base_value_type object_type;
46 typedef
47 typename
51 typedef typename std::list<const object_type*> object_list;
53 typedef ASSCONT asso_container_type;
54 typedef typename asso_container_type::base_value_type asso_type;
55 typedef
56 typename
59 typedef typename std::list<const asso_type*> asso_list;
63 typedef typename std::map<object_link,asso_store> store_type;
64 typedef typename store_type::iterator store_iterator_type;
66
67 // Dummy to allow defining an end iterator for an empty map.
69
83
86
89 * \param objectContainer - pointer to the object container
90 * \param objectIndex - index of object in container
91 * \param objectPointer - pointer to object
92 * \param assoContainer - container of potentially associated objects
93 * \param assoIndex - index of associated object in container
94 * \param assoPointer - pointer to associated object
95 */
96 /*@{*/
97 void addAssociation(const object_container_type* objectContainer,
98 const object_index_type& objectIndex,
99 const asso_container_type* assoContainer,
100 const asso_index_type& assoIndex);
101 void addAssociation(const object_container_type* objectContainer,
102 const object_type* objectPointer,
103 const asso_container_type* assoContainer,
104 const asso_type* assoPointer);
105 void addAssociation(const object_link& objectLink,
106 const asso_link& assoLink);
107
107 /*@}*/
108
113 * \param objectPointer - pointer to a given object
114 * \param objectIter - iterator to a given object
115 */
126 asso_iterator endAssociation(const object_type* objectPointer) const;
128 /*@}*/
129
131
132
133
135 const object_type* getObject(const object_iterator& objectIter) const
136 { return (*objectIter).getObject(); }
138 /// \brief finding an object with allocation
139 object_iterator findObject(const object_type* theObject) const
140 { return object_iterator(m_associationMap).find(theObject); }
141
143 bool containsObject(const object_type* theObject) const
144 { return this->findObject(theObject) != this->endObject(); }
145
146
146 /// \brief retrieve number of objects in store
147 size_t getNumberOfObjects() const { return this->size(); }
148
149 /// \brief associations iterator access
150 ///
151 /// The associations are accessed as function of the row key (the object).
152
154 const asso_type* getAssociation(asso_iterator assoIter) const
155 { return *assoIter; }
156
157 /// \brief find association
159 const asso_type* assoPointer) const
160 { return objectIter.findAssociation(assoPointer); }
162 const asso_type* assoPointer) const;
163
164 /// \brief containment check
165 bool containsAssociation(const object_iterator& objectIter,
166 const asso_type* assoPointer) const
167 { return objectIter.containsAssociation(assoPointer); }
168
169 bool containsAssociation(const object_type* objectPointer,
170 const asso_type* assoPointer) const;
171 bool containsAssociation(const asso_type* assoPointer) const;
172
174 bool getObjects(const asso_type* assoPointer, object_list& theObjects) const;
175
176 /// \brief get all objects for a given association
177 bool getObjects(const asso_iterator& assoIter, object_list& theObjects) const
178 { return this->getObjects(*assoIter,theObjects); }
179
180
180 /// \brief get all associations for a given object
181 bool getAssociations(const object_type* objPointer, asso_list& assocs) const;
182
184 bool getAssociations(const object_iterator& objIter, asso_list& assocs) const
185 { return this->getAssociations(objIter.getObject(),assocs); }
186
188 size_t size() { return m_associationMap.size(); }
189
191 size_t size() const { return m_associationMap.size(); }
192
193 size_t size(const object_type* objectPointer) const;
194
195 size_t getNumberOfAssociations(const object_type* objectPointer) const
196 { return this->size(objectPointer); }
198 size_t size(const object_iterator& objectIter) const
199 { return objectIter.getNumberOfAssociations(); }
200
201 size_t getNumberOfAssociations(const object_iterator& objectIter) const
202 { return this->size(objectIter); }
204 protected:
208
211 const asso_link& assoLink)
212 {
213 // check key
215 store_iterator_type foundIter = mapEnd;
216 for ( store_iterator_type iMap = m_associationMap.begin();
217 iMap != mapEnd;
218 ++iMap ) {
219 // look for the address of the pointed-at object
220 // must dereference the ElementLink pointer
221 if ( iMap->first.cptr() == objectLink.cptr() ) {
222 foundIter = iMap;
223 break;
225 }
226
227 if ( foundIter == m_associationMap.end() ) { return foundIter; }
228 // check data
229 if ( std::find((foundIter->second).begin(),
230 (foundIter->second).end(),
231 assoLink) !=
232 (foundIter->second).end() )
233 { return foundIter; }
234 // not found at all
235 return m_associationMap.end();
236 }
237
239 bool addToStore(const object_link& objectLink, const asso_link& assoLink);
240
241};
242
245#undef ASSOCIATION_CONTEXT
246#undef ASSOCIATIONMAP_CONTEXT
247#endif
code only expands with the ASSOCIATIONMAP_CONTEXT
AssociationObjectIterator find(const object_type *objectPointer) const
find a given object in store
const object_type * getObject() const
retrieve the pointer to a given object
bool containsAssociation(const asso_type *assoPointer) const
check if the given object can be matched among the associated objects
size_t getNumberOfAssociations() const
get number of association for current pointed-to object
asso_iterator findAssociation(const asso_type *assoPointer) const
find the iterator for a given object
iterator for association vectors (internal use only)
std::map< object_link, asso_store > store_type
ASSCONT asso_container_type
const asso_type * getAssociation(asso_iterator assoIter) const
associations iterator access
virtual ~AssociationMap()
asso_iterator beginAssociation(const object_type *objectPointer) const
begin iterator for associations
std::list< const object_type * > object_list
asso_store::const_iterator asso_store_iterator
store_type m_associationMap
internal store
static const asso_store s_dum_asso_store
bool containsAssociation(const object_type *objectPointer, const asso_type *assoPointer) const
bool getObjects(const asso_iterator &assoIter, object_list &theObjects) const
get all objects for a given association
const object_type * getObject(const object_iterator &objectIter) const
get association iterators by object iterator
bool containsAssociation(const asso_type *assoPointer) const
store_iterator_type internalFind(const object_link &objectLink, const asso_link &assoLink)
internally used find method
void addAssociation(const object_container_type *objectContainer, const object_type *objectPointer, const asso_container_type *assoContainer, const asso_type *assoPointer)
asso_iterator beginAssociation(const object_iterator &objectIter) const
size_t getNumberOfAssociations(const object_iterator &objectIter) const
bool containsAssociation(const object_iterator &objectIter, const asso_type *assoPointer) const
containment check
AssociationObjectIterator object_iterator
object iterator type
ElementLink< asso_container_type > asso_link
size_t size()
get number of associations
object_iterator endObject() const
end iterator for objects
bool getAssociations(const object_iterator &objIter, asso_list &assocs) const
get all associations for a given object
store_type::iterator store_iterator_type
size_t getNumberOfAssociations(const object_type *objectPointer) const
size_t size(const object_type *objectPointer) const
asso_iterator endAssociation(const object_iterator &objectIter) const
asso_container_type::base_value_type asso_type
bool getAssociations(const object_type *objPointer, asso_list &assocs) const
get all associations for a given object
ElementLinkVector< asso_container_type > asso_store
asso_iterator findAssociation(const object_type *objectPointer, const asso_type *assoPointer) const
size_t getNumberOfObjects() const
retrieve number of objects in store
SG::GenerateIndexingPolicy< asso_container_type >::type::index_type asso_index_type
AssociationVectorIterator asso_iterator
association iterator type
object_container_type::base_value_type object_type
ElementLink< object_container_type > object_link
void addAssociation(const object_container_type *objectContainer, const object_index_type &objectIndex, const asso_container_type *assoContainer, const asso_index_type &assoIndex)
bool getObjects(const asso_type *assoPointer, object_list &theObjects) const
get all objects for a given association
object_iterator findObject(const object_type *theObject) const
finding an object with allocation
bool addToStore(const object_link &objectLink, const asso_link &assoLink)
internally used function to add links to store
asso_iterator endAssociation(const object_type *objectPointer) const
end iterator for associations
bool containsObject(const object_type *theObject) const
testing if object is in store
asso_iterator findAssociation(const object_iterator &objectIter, const asso_type *assoPointer) const
find association
OBJCONT object_container_type
std::list< const asso_type * > asso_list
SG::GenerateIndexingPolicy< object_container_type >::type::index_type object_index_type
size_t size(const object_iterator &objectIter) const
object_iterator beginObject() const
begin iterator for objects
void addAssociation(const object_link &objectLink, const asso_link &assoLink)
ElementLinkVector implementation for standalone ROOT.
ELVIterator< typename RefVector::const_iterator > const_iterator