ATLAS Offline Software
Loading...
Searching...
No Matches
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
24template <typename value_t, typename identifier_t, size_t inline_size = 10>
26
27public:
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
92private:
93 std::unordered_map<identifier_t, boost::container::small_vector<Range, inline_size>> m_ranges;
94
95};
96
97#endif // XAODINDETMEASUREMENT_CONTAINERACCESSOR_H
An STL vector of pointers that by default owns its pointed-to elements.
std::unordered_map< identifier_t, boost::container::small_vector< Range, inline_size > > m_ranges
ContainerAccessor(const container_t &values, std::function< identifier_t(const value_t &)> mapper, size_t max_ids=2000)
Constructor.
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.
std::pair< Iterator, Iterator > Range
typename container_t::const_iterator Iterator
std::vector< identifier_t > allIdentifiers() const
Function to return all available identifier (i.e. keys in the map)
bool isIdentifierPresent(const identifier_t &identifier) const
Function to verify if a given identifier is present in the map, i.e.
ContainerAccessor(const ContainerAccessor &)=delete
Copy constructor, deleted.
DataVector< value_t > container_t
~ContainerAccessor()=default
Default destructor.
ContainerAccessor & operator=(const ContainerAccessor &)=delete
Derived DataVector<T>.
Definition DataVector.h:795
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838