2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5 * @file AthLinks/tools/IdentContIndexingPolicy.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Indexing policy for an IdentifiableContainer.
19 * @brief Test to see if an index is valid.
20 * @param index The index to test.
24 bool IdentContIndexingPolicy<CONT>::isValid (stored_index_type index)
26 return IdentContIndex(index).isValid();
31 * @brief Convert from stored to external index types.
32 * @param index The stored index.
36 typename IdentContIndexingPolicy<CONT>::index_type
37 IdentContIndexingPolicy<CONT>::storedToExternal (stored_index_type index)
44 * @brief Make an index invalid.
45 * @param index[out] The index to reset.
49 void IdentContIndexingPolicy<CONT>::reset (stored_index_type& index)
51 index = IdentContIndex().hashAndIndex();
56 * @brief Retrieve from a container the element corresponding to an index.
57 * @param index The index to fetch.
58 * @param data The container.
60 * Will throw SG::ExcInvalidIndex if the index is invalid and
61 * SG::ExcIndexNotFound if the index is not in the container.
64 typename IdentContIndexingPolicy<CONT>::ElementType
65 IdentContIndexingPolicy<CONT>::lookup (index_type index, const CONT& data)
68 SG::throwExcInvalidIndex ("IdentContIndexingPolicy");
70 // Find object with hash and index in collection
71 IdentContIndex compIndex(index);
72 typename CONT::const_iterator it = data.indexFind (compIndex.collHash());
73 if (it != data.end()) {
75 // Check if objIndex was correctly set
76 if (compIndex.objIndex() < (*it)->size()) {
77 return ((**it)[compIndex.objIndex()]);
81 SG::throwExcIndexNotFound ("IdentContIndexingPolicy");
86 * @brief Find the index of the (first) instance of ELEMENT in DATA.
87 * @param data The container to search.
88 * @param element The element to find.
89 * @param index[out] The index in the container of @c element.
91 * Throws SG::ExcElementNotFound if the element is not in the container.
95 IdentContIndexingPolicy<CONT>::reverseLookup(const CONT& data,
96 ElementConstReference element,
99 // compiler checks we can compare elements
100 static_assert (std::equality_comparable<ElementType>);
102 // The hash and possibly the object index may already have
103 // been set by the user. We verify that the index does
104 // correspond to the object, and otherwise we recalculate the
106 IdentContIndex compIndex(index);
107 typename CONT::const_iterator it = data.indexFind (compIndex.collHash());
108 if (it != data.end()) {
110 // Check if objIndex was correctly set
111 if (compIndex.objIndex() < (*it)->size()) {
112 if ((**it)[compIndex.objIndex()] == element) {
116 // objIndex was not correctly set, look for object
117 for (unsigned int i = 0; i < (*it)->size(); ++i) {
118 if ((**it)[i] == element) {
119 // Save index in collection to object
120 compIndex.setObjIndex(i);
121 index = compIndex.hashAndIndex();
125 // Correct collection, but object not found
126 SG::throwExcElementNotFound ("IdentContIndexingPolicy: reverseLookup");
127 return; // Not reached
130 // Neither hash, nor object index set, must find both - THIS
132 for (typename CONT::const_iterator it = data.begin();
135 const coll_type* coll = *it;
137 typename coll_type::const_iterator objIt =
138 std::find (coll->begin(), coll->end(),
140 if (objIt != coll->end()) {
141 // Save collection hash and index to object in collection
142 compIndex.setCollHash(coll->identifyHash());
143 compIndex.setObjIndex(std::distance (coll->begin(), objIt));
144 index = compIndex.hashAndIndex();
149 SG::throwExcElementNotFound ("IdentContIndexingPolicy: reverseLookup");