ATLAS Offline Software
ContainerAccessor.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef XAODINDETMEASUREMENT_CONTAINERACCESSOR_H
6 #define XAODINDETMEASUREMENT_CONTAINERACCESSOR_H
7 
9 
10 #include <boost/container/small_vector.hpp>
11 #include <vector>
12 #include <unordered_map>
13 
14 #include <algorithm>
15 #include <functional>
16 #include <optional>
17 #include <utility>
18 
23 
24 template <typename value_t, typename identifier_t, size_t inline_size = 10>
26 
27 public:
30  using Range = std::pair<Iterator, Iterator>;
31 
42  std::function<identifier_t(const value_t&)> mapper, size_t max_ids=2000) {
43  m_ranges.reserve(max_ids);
44  std::optional<identifier_t> prev = std::nullopt;
45  Iterator groupStart = values.begin();
46  for (Iterator it = values.begin(); it != values.end(); ++it) {
47  identifier_t id = mapper(**it);
48  if (prev && (*prev) != id) {
49  m_ranges[*prev].emplace_back(groupStart, it);
50  groupStart = it;
51  }
52  prev = id;
53  }
54  if (prev)
55  m_ranges[*prev].emplace_back(groupStart, values.end());
56  }
57 
59  ~ContainerAccessor() = default;
63 
69  const boost::container::small_vector<Range, inline_size> rangesForIdentifierDirect(const identifier_t& identifier) const {
70  boost::container::small_vector<Range, inline_size> rangesForId = m_ranges.at(identifier);
71  return rangesForId;
72  }
73 
77  bool isIdentifierPresent(const identifier_t& identifier) const {
78  if (m_ranges.find(identifier) == m_ranges.end())
79  return false;
80  return true;
81  }
82 
84  std::vector<identifier_t> allIdentifiers() const {
85  std::vector<identifier_t> ids{};
86  ids.reserve(m_ranges.size());
87  for (const auto& id_range : m_ranges)
88  ids.push_back(id_range.first);
89  return ids;
90  };
91 
92 private:
93  std::unordered_map<identifier_t, boost::container::small_vector<Range, inline_size>> m_ranges;
94 
95 };
96 
97 #endif // XAODINDETMEASUREMENT_CONTAINERACCESSOR_H
ContainerAccessor
Definition: ContainerAccessor.h:25
ContainerAccessor::allIdentifiers
std::vector< identifier_t > allIdentifiers() const
Function to return all available identifier (i.e. keys in the map)
Definition: ContainerAccessor.h:84
DataVector::const_iterator
DataModel_detail::const_iterator< DataVector > const_iterator
Standard const_iterator.
Definition: DataVector.h:837
skel.it
it
Definition: skel.GENtoEVGEN.py:423
xAOD::identifier
identifier
Definition: UncalibratedMeasurement_v1.cxx:15
ContainerAccessor::Iterator
typename container_t::const_iterator Iterator
Definition: ContainerAccessor.h:29
python.Bindings.values
values
Definition: Control/AthenaPython/python/Bindings.py:797
ContainerAccessor::ContainerAccessor
ContainerAccessor(const ContainerAccessor &)=delete
Copy constructor, deleted.
ContainerAccessor::rangesForIdentifierDirect
const boost::container::small_vector< Range, inline_size > rangesForIdentifierDirect(const identifier_t &identifier) const
Function to return the list of ranges corresponding to a given identifier.
Definition: ContainerAccessor.h:69
ContainerAccessor::m_ranges
std::unordered_map< identifier_t, boost::container::small_vector< Range, inline_size > > m_ranges
Definition: ContainerAccessor.h:90
ContainerAccessor::operator=
ContainerAccessor & operator=(const ContainerAccessor &)=delete
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:194
python.subdetectors.mmg.ids
ids
Definition: mmg.py:8
Range
A Range describes the possible ranges for the field values of an ExpandedIdentifier.
Definition: DetectorDescription/Identifier/Identifier/Range.h:27
ContainerAccessor::~ContainerAccessor
~ContainerAccessor()=default
Default destructor.
DataVector.h
An STL vector of pointers that by default owns its pointed-to elements.
ContainerAccessor::ContainerAccessor
ContainerAccessor(const container_t &values, std::function< identifier_t(const value_t &)> mapper, size_t max_ids=2000)
Constructor.
Definition: ContainerAccessor.h:41
ContainerAccessor::isIdentifierPresent
bool isIdentifierPresent(const identifier_t &identifier) const
Function to verify if a given identifier is present in the map, i.e.
Definition: ContainerAccessor.h:77