ATLAS Offline Software
SetIndexingPolicy.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // $Id$
6 /**
7  * @file AthLinks/tools/SetIndexingPolicy.icc
8  * @author scott snyder <snyder@bnl.gov>
9  * @date Apr, 2014
10  * @brief Indexing policy for a set-like container.
11  */
12 
13 
14 namespace SG {
15 
16 
17 /**
18  * @brief Test to see if an index is valid.
19  * @param index The index to test.
20  */
21 template <class SET>
22 inline
23 bool SetIndexingPolicy<SET>::isValid (const stored_index_type& index)
24 {
25  return index.isValid();
26 }
27 
28 
29 /**
30  * @brief Convert from stored to external index types.
31  * @param index The stored index.
32  */
33 template <class CONT>
34 inline
35 typename SetIndexingPolicy<CONT>::index_type
36 SetIndexingPolicy<CONT>::storedToExternal (stored_index_type index)
37 {
38  return index;
39 }
40 
41 
42 /**
43  * @brief Make an index invalid.
44  * @param index[out] The index to reset.
45  */
46 template <class SET>
47 inline
48 void SetIndexingPolicy<SET>::reset (stored_index_type& index)
49 {
50  index.reset();
51 }
52 
53 
54 /**
55  * @brief Retrieve from a container the element corresponding to an index.
56  * @param index The index to fetch.
57  * @param data The container.
58  *
59  * Will throw SG::ExcInvalidIndex if the index is invalid and
60  * SG::ExcIndexNotFound if the index is not in the container.
61  */
62 template <class SET>
63 typename SetIndexingPolicy<SET>::ElementType
64 SetIndexingPolicy<SET>::lookup(const stored_index_type& index,
65  const SET& data)
66 {
67  if (!isValid(index))
68  SG::throwExcInvalidIndex ("SetIndexingPolicy");
69  iterator iter = data.find(index);
70  if (iter == data.end())
71  SG::throwExcIndexNotFound ("SetIndexingPolicy");
72  return *iter;
73 }
74 
75 
76 /**
77  * @brief Find the index of the (first) instance of ELEMENT in DATA.
78  * @param data The container to search.
79  * @param element The element to find.
80  * @param index[out] The index in the container of @c element.
81  *
82  * Throws SG::ExcElementNotFound if the element is not in the container.
83  */
84 template <class SET>
85 void
86 SetIndexingPolicy<SET>::reverseLookup(const SET& data,
87  ElementConstReference element,
88  index_type& same)
89 {
90  //compiler checks we can compare elements
91  ::boost::function_requires<typename ::boost::EqualityComparableConcept<ElementType> >();
92 
93  // Note that reverseLookup will recalculate index even if m_valid is true.
94  // Must ensure that index is write before persistency
95  if (data.end() != data.find(element))
96  {
97  same = element ;
98  }
99  else {
100  SG::throwExcElementNotFound ("reverseLookup");
101  }
102 }
103 
104 
105 } // namespace SG