ATLAS Offline Software
Loading...
Searching...
No Matches
MapIndexingPolicy.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/MapIndexingPolicy.icc
8 * @author scott snyder <snyder@bnl.gov>
9 * @date Apr, 2014
10 * @brief Indexing policy for a set-like container.
11 */
12
13
14namespace SG {
15
16
17/**
18 * @brief Test to see if an index is valid.
19 * @param index The index to test.
20 */
21template <CxxUtils::detail::PairAssociativeContainer MAP>
22inline
23bool MapIndexingPolicy<MAP>::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 */
33template <CxxUtils::detail::PairAssociativeContainer MAP>
34inline
35typename MapIndexingPolicy<MAP>::index_type
36MapIndexingPolicy<MAP>::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 */
46template <CxxUtils::detail::PairAssociativeContainer MAP>
47inline
48void MapIndexingPolicy<MAP>::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 */
62template <CxxUtils::detail::PairAssociativeContainer MAP>
63typename MapIndexingPolicy<MAP>::ElementType
64MapIndexingPolicy<MAP>::lookup(const stored_index_type& index,
65 const MAP& data)
66{
67 if (!isValid(index))
68 SG::throwExcInvalidIndex ("MapIndexingPolicy");
69 const_iterator iter = data.find(index);
70 if (iter == data.end())
71 SG::throwExcIndexNotFound ("MapIndexingPolicy");
72 return iter->second;
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 */
84template <CxxUtils::detail::PairAssociativeContainer MAP>
85void
86MapIndexingPolicy<MAP>::reverseLookup(const MAP& data,
87 ElementConstReference element,
88 index_type& index)
89{
90 //compiler checks we can compare elements
91 static_assert(std::equality_comparable<ElementType>, "ElementType must support == and !=");
92
93 // Note that reverseLookup will recalculate index even if m_valid is true.
94 // Must ensure that index is write before persistency
95 const_iterator it = data.begin();
96 const_iterator iend = data.end();
97 while (it != iend) {
98 if (it->second == element) break;
99 ++it;
100 }
101
102 if (it != iend)
103 {
104 index = it->first ;
105 }
106 else {
107 SG::throwExcElementNotFound ("reverseLookup");
108 }
109}
110
111
112} // namespace SG