Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/ActsExamples/EventData/Index.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
0008 
0009 #pragma once
0010 
0011 #include <algorithm>
0012 #include <cstdint>
0013 
0014 #include <boost/container/flat_map.hpp>
0015 #include <boost/version.hpp>
0016 
0017 namespace ActsExamples {
0018 
0019 /// Index type to reference elements in a container.
0020 ///
0021 /// We do not expect to have more than 2^32 elements in any given container so a
0022 /// fixed sized integer type is sufficient.
0023 using Index = std::uint32_t;
0024 
0025 /// Store elements that are identified by an index, e.g. in another container.
0026 ///
0027 /// Each index can have zero or more associated elements. A typical case could
0028 /// be to store all generating particles for a hit where the hit is identified
0029 /// by its index in the hit container.
0030 template <typename value_t>
0031 using IndexMultimap = boost::container::flat_multimap<Index, value_t>;
0032 
0033 /// Store the inverse of an index multimap, i.e. from a -> {b...} to b ->
0034 /// {a...}.
0035 ///
0036 /// @note This assumes that the value in the initial multimap is itself a
0037 ///   sortable index-like object, as would be the case when mapping e.g.
0038 ///   hit ids to particle ids/ barcodes.
0039 template <typename value_t>
0040 using InverseMultimap = boost::container::flat_multimap<value_t, Index>;
0041 
0042 /// Invert the multimap, i.e. from a -> {b...} to b -> {a...}
0043 template <typename value_t>
0044 inline InverseMultimap<value_t> invertIndexMultimap(
0045     const IndexMultimap<value_t>& multimap) {
0046   // switch key-value without enforcing the new ordering (linear copy)
0047   typename InverseMultimap<value_t>::sequence_type unordered;
0048   unordered.reserve(multimap.size());
0049   for (auto&& [index, value] : multimap) {
0050     // value is now the key and the index is now the value
0051     unordered.emplace_back(value, index);
0052   }
0053 
0054   // adopting the unordered sequence will reestablish the correct order
0055   InverseMultimap<value_t> inverse;
0056 #if BOOST_VERSION < 107800
0057   for (const auto& i : unordered) {
0058     inverse.insert(i);
0059   }
0060 #else
0061   std::ranges::sort(unordered);
0062   inverse.insert(boost::container::ordered_range_t{}, unordered.begin(),
0063                  unordered.end());
0064 #endif
0065 
0066   return inverse;
0067 }
0068 
0069 }  // namespace ActsExamples