Juggler
Juggling algorithms and event processing using gaudi framework
IndexContainers.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 <cstddef>
12 #include <utility>
13 
14 #include <boost/container/flat_map.hpp>
15 
16 namespace FW {
17 
18 /// Store elements that are identified by an index, e.g. in another container.
19 ///
20 /// Each index can have zero or more associated elements. A typical case could
21 /// be to store all generating particles for a hit where the hit is identified
22 /// by its index in the hit container.
23 template <typename Value, typename Key = std::size_t>
24 using IndexMultimap = boost::container::flat_multimap<Key, Value>;
25 
26 /// Invert the multimap, i.e. from a -> {b...} to b -> {a...}.
27 ///
28 /// @note This assumes that the value in the initial multimap is itself a
29 /// sortable index-like object, as would be the case when mapping e.g.
30 /// hit ids to particle ids/ barcodes.
31 template <typename Value, typename Key>
32 inline IndexMultimap<Key, Value> invertIndexMultimap(
33  const IndexMultimap<Value, Key>& multimap) {
34  // switch key-value without enforcing the new ordering (linear copy)
35  typename IndexMultimap<Key, Value>::sequence_type unordered;
36  unordered.reserve(multimap.size());
37  for (const auto& keyValue : multimap) {
38  // value is now the key and the key is now the value
39  unordered.emplace_back(keyValue.second, keyValue.first);
40  }
41  // adopting the unordered sequence will reestablish the correct order
42  IndexMultimap<Key, Value> inverse;
43  inverse.adopt_sequence(std::move(unordered));
44  return inverse;
45 }
46 
47 } // namespace FW
FW
Definition: IndexContainers.hpp:16
FW::invertIndexMultimap
IndexMultimap< Key, Value > invertIndexMultimap(const IndexMultimap< Value, Key > &multimap)
Definition: IndexContainers.hpp:32
FW::IndexMultimap
boost::container::flat_multimap< Key, Value > IndexMultimap
Definition: IndexContainers.hpp:24