ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
name_map.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 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 https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s)
12#include "detray/definitions/detail/qualifiers.hpp"
13#include "detray/definitions/indexing.hpp"
14
15// System include(s)
16#include <string>
17#include <string_view>
18#include <unordered_map>
19
20namespace detray {
21
23struct name_map {
26 DETRAY_HOST void set_detector_name(const std::string_view name) {
27 detector_name = name;
28 }
29
31 DETRAY_HOST const std::string& get_detector_name() const {
32 return detector_name;
33 }
34
36 DETRAY_HOST bool empty() const {
37 assert(index_to_name.size() == name_to_index.size());
38 return index_to_name.empty();
39 }
40
43 DETRAY_HOST bool contains(dindex idx) const {
44 return index_to_name.contains(idx);
45 }
46
49 DETRAY_HOST bool contains(std::string_view name) const {
50 return name_to_index.contains(name);
51 }
52
56 DETRAY_HOST void emplace(dindex idx, const std::string& name) {
57 index_to_name.try_emplace(idx, name);
58 name_to_index.try_emplace(index_to_name.at(idx), idx);
59 }
60
64 DETRAY_HOST decltype(auto) at(dindex idx) const {
65 return index_to_name.at(idx);
66 }
67
71 DETRAY_HOST decltype(auto) at(dindex idx) { return index_to_name.at(idx); }
72
76 DETRAY_HOST decltype(auto) at(const std::string& name) const {
77 return name_to_index.at(name);
78 }
79
83 DETRAY_HOST decltype(auto) at(const std::string_view name) const {
84 return name_to_index.at(name);
85 }
86
88 DETRAY_HOST void clear() {
89 detector_name = "";
90 index_to_name.clear();
91 name_to_index.clear();
92 }
93
95 DETRAY_HOST void clear_names() {
96 index_to_name.clear();
97 name_to_index.clear();
98 }
99
100 private:
102 std::string detector_name{};
106 std::unordered_map<dindex, std::string> index_to_name{};
107 std::unordered_map<std::string_view, dindex> name_to_index{};
109};
110
111} // namespace detray
Detray include(s).
Definition array_operators.hpp:18
unsigned int dindex
Default index type.
Definition indexing.hpp:18
Map volume indices to volume names and vice versa.
Definition name_map.hpp:23
decltype(auto) at(dindex idx) const
Returns the name associated with a volume index.
Definition name_map.hpp:64
void set_detector_name(const std::string_view name)
set the name of the detector
Definition name_map.hpp:26
void emplace(dindex idx, const std::string &name)
emplace a new index <-> name mapping
Definition name_map.hpp:56
decltype(auto) at(const std::string_view name) const
Returns the index associated with a volume name.
Definition name_map.hpp:83
const std::string & get_detector_name() const
Definition name_map.hpp:31
decltype(auto) at(dindex idx)
Returns the name associated with a volume index.
Definition name_map.hpp:71
bool contains(std::string_view name) const
Definition name_map.hpp:49
void clear_names()
Clears the volume name mapping, but keeps detector name.
Definition name_map.hpp:95
bool contains(dindex idx) const
Definition name_map.hpp:43
decltype(auto) at(const std::string &name) const
Returns the index associated with a volume name.
Definition name_map.hpp:76
bool empty() const
Definition name_map.hpp:36
void clear()
Clears the name map.
Definition name_map.hpp:88