LDMX Software
Index.h
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 <boost/container/flat_map.hpp>
12#include <cstdint>
13
14namespace ActsExamples {
15
20using Index = uint32_t;
21
27template <typename value_t>
28using IndexMultimap = boost::container::flat_multimap<Index, value_t>;
29
35template <typename value_t>
36inline boost::container::flat_multimap<value_t, Index> invertIndexMultimap(
37 const IndexMultimap<value_t>& multimap) {
38 using InverseMultimap = boost::container::flat_multimap<value_t, Index>;
39
40 // switch key-value without enforcing the new ordering (linear copy)
41 typename InverseMultimap::sequence_type unordered;
42 unordered.reserve(multimap.size());
43 for (auto&& [index, value] : multimap) {
44 // value is now the key and the index is now the value
45 unordered.emplace_back(value, index);
46 }
47
48 // adopting the unordered sequence will reestablish the correct order
49 InverseMultimap inverse;
50 inverse.insert(unordered.begin(), unordered.end());
51 return inverse;
52}
53
54} // namespace ActsExamples