Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-06-01 07:07:36

0001 // This file is part of the Acts project.
0002 //
0003 // Copyright (C) 2019-2020 CERN for the benefit of the Acts project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <cstddef>
0012 #include <utility>
0013 
0014 #include <boost/container/flat_map.hpp>
0015 
0016 namespace FW {
0017 
0018 /// Store elements that are identified by an index, e.g. in another container.
0019 ///
0020 /// Each index can have zero or more associated elements. A typical case could
0021 /// be to store all generating particles for a hit where the hit is identified
0022 /// by its index in the hit container.
0023 template <typename Value, typename Key = std::size_t>
0024 using IndexMultimap = boost::container::flat_multimap<Key, Value>;
0025 
0026 /// Invert the multimap, i.e. from a -> {b...} to b -> {a...}.
0027 ///
0028 /// @note This assumes that the value in the initial multimap is itself a
0029 ///       sortable index-like object, as would be the case when mapping e.g.
0030 ///       hit ids to particle ids/ barcodes.
0031 template <typename Value, typename Key>
0032 inline IndexMultimap<Key, Value> invertIndexMultimap(
0033     const IndexMultimap<Value, Key>& multimap) {
0034   // switch key-value without enforcing the new ordering (linear copy)
0035   typename IndexMultimap<Key, Value>::sequence_type unordered;
0036   unordered.reserve(multimap.size());
0037   for (const auto& keyValue : multimap) {
0038     // value is now the key and the key is now the value
0039     unordered.emplace_back(keyValue.second, keyValue.first);
0040   }
0041   // adopting the unordered sequence will reestablish the correct order
0042   IndexMultimap<Key, Value> inverse;
0043   inverse.adopt_sequence(std::move(unordered));
0044   return inverse;
0045 }
0046 
0047 }  // namespace FW