Juggler
Juggling algorithms and event processing using gaudi framework
Index.hpp
Go to the documentation of this file.
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2019-2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 
11 #include <cstdint>
12 
13 #include <boost/container/flat_map.hpp>
14 
15 namespace Jug {
16 
17 /// Index type to reference elements in a container.
18 ///
19 /// We do not expect to have more than 2^32 elements in any given container so a
20 /// fixed sized integer type is sufficient.
21 using Index = uint32_t;
22 
23 /// Store elements that are identified by an index, e.g. in another container.
24 ///
25 /// Each index can have zero or more associated elements. A typical case could
26 /// be to store all generating particles for a hit where the hit is identified
27 /// by its index in the hit container.
28 template <typename value_t>
29 using IndexMultimap = boost::container::flat_multimap<Index, value_t>;
30 
31 /// Invert the multimap, i.e. from a -> {b...} to b -> {a...}.
32 ///
33 /// @note This assumes that the value in the initial multimap is itself a
34 /// sortable index-like object, as would be the case when mapping e.g.
35 /// hit ids to particle ids/ barcodes.
36 template <typename value_t>
37 inline boost::container::flat_multimap<value_t, Index> invertIndexMultimap(
38  const IndexMultimap<value_t>& multimap) {
39  using InverseMultimap = boost::container::flat_multimap<value_t, Index>;
40 
41  // switch key-value without enforcing the new ordering (linear copy)
42  typename InverseMultimap::sequence_type unordered;
43  unordered.reserve(multimap.size());
44  for (auto&& [index, value] : multimap) {
45  // value is now the key and the index is now the value
46  unordered.emplace_back(value, index);
47  }
48 
49  // adopting the unordered sequence will reestablish the correct order
50  InverseMultimap inverse;
51  inverse.adopt_sequence(std::move(unordered));
52  return inverse;
53 }
54 
55 } // namespace ActsExamples
Jug::Index
uint32_t Index
Definition: Index.hpp:21
Jug::IndexMultimap
boost::container::flat_multimap< Index, value_t > IndexMultimap
Definition: Index.hpp:29
Jug::invertIndexMultimap
boost::container::flat_multimap< value_t, Index > invertIndexMultimap(const IndexMultimap< value_t > &multimap)
Definition: Index.hpp:37
Jug
Definition: DD4hepBField.h:22