File indexing completed on 2026-03-29 07:47:30
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include <cstddef>
0012 #include <cstdint>
0013 #include <format>
0014 #include <span>
0015 #include <stdexcept>
0016 #include <string>
0017 #include <type_traits>
0018 #include <vector>
0019
0020 namespace ActsPodioEdm::detail {
0021
0022
0023 using SubspaceIndex = std::uint8_t;
0024
0025 constexpr std::uint8_t kMaxSubspaceIndex = 6u;
0026
0027 constexpr std::size_t kMaxSubspaceSize = 6u;
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044 inline std::int32_t encodeIndices(std::span<const std::uint8_t> indices) {
0045 if (indices.size() > kMaxSubspaceSize) {
0046 throw std::runtime_error(
0047 std::format("Number of indices exceeds maximum of {} for EDM4hep",
0048 kMaxSubspaceSize));
0049 }
0050
0051 std::int32_t result = 0;
0052 std::uint8_t shift = 0;
0053 result |= (indices.size() << 0);
0054 shift += 4;
0055
0056 for (std::uint8_t index : indices) {
0057 if (index > kMaxSubspaceIndex) {
0058 throw std::runtime_error(std::format(
0059 "Index out of range: maximum allowed is {}", kMaxSubspaceIndex));
0060 }
0061 result |= (static_cast<std::int32_t>(index) << shift);
0062 shift += 4;
0063 }
0064 return result;
0065 }
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 inline std::vector<SubspaceIndex> decodeIndices(std::int32_t type) {
0077 std::vector<SubspaceIndex> result;
0078 std::uint8_t size = type & 0xF;
0079 if (size > kMaxSubspaceSize) {
0080 throw std::runtime_error(
0081 std::format("Number of indices exceeds maximum of {} for EDM4hep",
0082 kMaxSubspaceSize));
0083 }
0084 result.resize(size);
0085 for (std::size_t i = 0; i < result.size(); ++i) {
0086 result[i] = (type >> ((i + 1) * 4)) & 0xF;
0087 if (result[i] > kMaxSubspaceIndex) {
0088 throw std::runtime_error(std::format(
0089 "Index out of range: maximum allowed is {}", kMaxSubspaceIndex));
0090 }
0091 }
0092 return result;
0093 }
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106 template <typename E>
0107 requires std::is_enum_v<E>
0108 inline std::size_t findSubspaceIndex(std::span<const SubspaceIndex> indices,
0109 E enumVal) {
0110 const auto target = static_cast<SubspaceIndex>(enumVal);
0111 for (std::size_t i = 0; i < indices.size(); ++i) {
0112 if (indices[i] == target) {
0113 return i;
0114 }
0115 }
0116 throw std::runtime_error(
0117 std::format("Enum value {} not found in subspace indices",
0118 static_cast<int>(enumVal)));
0119 }
0120
0121 }