ATLAS Offline Software
Loading...
Searching...
No Matches
ForwardIndexingPolicy.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5/**
6 * @file AthLinks/tools/ForwardIndexingPolicy.icc
7 * @author scott snyder <snyder@bnl.gov>
8 * @date Dec, 2013
9 * @brief Indexing policy for a vector-like container.
10 */
11
12
13namespace SG {
14
15
16/**
17 * @brief Test to see if an index is valid.
18 * @param index The index to test.
19 */
20template <class CONT, class VALUE_TYPE>
21inline
22bool ForwardIndexingPolicy<CONT, VALUE_TYPE>::isValid (stored_index_type index)
23{
24 return index != static_cast<stored_index_type>(INVALID);
25}
26
27
28/**
29 * @brief Convert from stored to external index types.
30 * @param index The stored index.
31 */
32template <class CONT, class VALUE_TYPE>
33inline
34typename ForwardIndexingPolicy<CONT, VALUE_TYPE>::index_type
35ForwardIndexingPolicy<CONT, VALUE_TYPE>::storedToExternal (stored_index_type index)
36{
37 if (index == static_cast<stored_index_type>(INVALID))
38 return INVALID;
39 return index;
40}
41
42
43/**
44 * @brief Make an index invalid.
45 * @param index[out] The index to reset.
46 */
47template <class CONT, class VALUE_TYPE>
48inline
49void ForwardIndexingPolicy<CONT, VALUE_TYPE>::reset (stored_index_type& index)
50{
51 index = static_cast<stored_index_type>(INVALID);
52}
53
54
55/**
56 * @brief Retrieve from a container the element corresponding to an index.
57 * @param index The index to fetch.
58 * @param data The container.
59 *
60 * Will throw SG::ExcBadForwardLink if the index is invalid.
61 */
62template <class CONT, class VALUE_TYPE>
63inline
64typename ForwardIndexingPolicy<CONT, VALUE_TYPE>::ElementType
65ForwardIndexingPolicy<CONT, VALUE_TYPE>::lookup (index_type index, const CONT& data)
66{
67 if (!isValid(index) || (data.size() <= index)) {
68 SG::throwExcBadForwardLink (index, data.size(), ClassName<CONT>::name());
69 }
70
71 typedef typename CONT::const_iterator const_iterator;
72 const_iterator iter = data.begin();
73 std::advance(iter, index);
74 return *iter;
75}
76
77
78/**
79 * @brief Find the index of the (first) instance of ELEMENT in DATA.
80 * @param data The container to search.
81 * @param element The element to find.
82 * @param index[out] The index in the container of @c element.
83 *
84 * Throws SG::ExcElementNotFound if the element is not in the container.
85 */
86template <class CONT, class VALUE_TYPE>
87void ForwardIndexingPolicy<CONT, VALUE_TYPE>::reverseLookup(const CONT& data,
88 ElementConstReference element,
89 index_type& index)
90{
91 // compiler checks we can compare elements
92 static_assert (std::equality_comparable<ElementType>);
93
94 // Note that reverse lookup redoes the lookup even if m_isValid is true.
95 // Must ensure that we get correct index before persistency
96 index = INVALID;
97 typename CONT::size_type cindex;
98 if (!SG::findInContainer (data, element, cindex))
99 throwExcElementNotFound ("reverseLookup");
100 index = cindex;
101}
102
103
104} // namespace SG