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 requires CxxUtils::detail::SimpleAssociativeContainer<SET>
23 inline
24 bool SetIndexingPolicy<SET>::isValid (const stored_index_type& index)
25 {
26  return index.isValid();
27 }
28 
29 
30 /**
31  * @brief Convert from stored to external index types.
32  * @param index The stored index.
33  */
34 template <class SET>
35 requires CxxUtils::detail::SimpleAssociativeContainer<SET>
36 inline
37 typename SetIndexingPolicy<SET>::index_type
38 SetIndexingPolicy<SET>::storedToExternal (stored_index_type index)
39 {
40  return index;
41 }
42 
43 
44 /**
45  * @brief Make an index invalid.
46  * @param index[out] The index to reset.
47  */
48 template <class SET>
49 requires CxxUtils::detail::SimpleAssociativeContainer<SET>
50 inline
51 void SetIndexingPolicy<SET>::reset (stored_index_type& index)
52 {
53  index.reset();
54 }
55 
56 
57 /**
58  * @brief Retrieve from a container the element corresponding to an index.
59  * @param index The index to fetch.
60  * @param data The container.
61  *
62  * Will throw SG::ExcInvalidIndex if the index is invalid and
63  * SG::ExcIndexNotFound if the index is not in the container.
64  */
65 template <class SET>
66 requires CxxUtils::detail::SimpleAssociativeContainer<SET>
67 typename SetIndexingPolicy<SET>::ElementType
68 SetIndexingPolicy<SET>::lookup(const stored_index_type& index,
69  const SET& data)
70 {
71  if (!isValid(index))
72  SG::throwExcInvalidIndex ("SetIndexingPolicy");
73  iterator iter = data.find(index);
74  if (iter == data.end())
75  SG::throwExcIndexNotFound ("SetIndexingPolicy");
76  return *iter;
77 }
78 
79 
80 /**
81  * @brief Find the index of the (first) instance of ELEMENT in DATA.
82  * @param data The container to search.
83  * @param element The element to find.
84  * @param index[out] The index in the container of @c element.
85  *
86  * Throws SG::ExcElementNotFound if the element is not in the container.
87  */
88 template <class SET>
89 requires CxxUtils::detail::SimpleAssociativeContainer<SET>
90 void
91 SetIndexingPolicy<SET>::reverseLookup(const SET& data,
92  ElementConstReference element,
93  index_type& same)
94 {
95  //compiler checks we can compare elements
96  static_assert(std::equality_comparable<ElementType>, "ElementType must support equality comparison");
97  // Note that reverseLookup will recalculate index even if m_valid is true.
98  // Must ensure that index is write before persistency
99  if (data.end() != data.find(element))
100  {
101  same = element ;
102  }
103  else {
104  SG::throwExcElementNotFound ("reverseLookup");
105  }
106 }
107 
108 
109 } // namespace SG