File indexing completed on 2025-01-18 09:27:38
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Geometry/GeometryIdentifier.hpp"
0012
0013 #include <algorithm>
0014 #include <cassert>
0015 #include <initializer_list>
0016 #include <iterator>
0017 #include <stdexcept>
0018 #include <utility>
0019 #include <vector>
0020
0021 namespace Acts {
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 template <typename value_t>
0062 class GeometryHierarchyMap {
0063 public:
0064
0065 using InputElement = typename std::pair<GeometryIdentifier, value_t>;
0066 using Iterator = typename std::vector<value_t>::const_iterator;
0067 using Size = typename std::vector<value_t>::size_type;
0068 using Value = value_t;
0069
0070
0071
0072
0073 GeometryHierarchyMap(std::vector<InputElement> elements);
0074
0075
0076
0077 GeometryHierarchyMap(std::initializer_list<InputElement> elements);
0078
0079
0080 GeometryHierarchyMap() = default;
0081 GeometryHierarchyMap(const GeometryHierarchyMap&) = default;
0082 GeometryHierarchyMap(GeometryHierarchyMap&&) = default;
0083 ~GeometryHierarchyMap() = default;
0084 GeometryHierarchyMap& operator=(const GeometryHierarchyMap&) = default;
0085 GeometryHierarchyMap& operator=(GeometryHierarchyMap&&) = default;
0086
0087
0088 Iterator begin() const { return m_values.begin(); }
0089
0090 Iterator end() const { return m_values.end(); }
0091
0092 bool empty() const { return m_values.empty(); }
0093
0094 Size size() const { return m_values.size(); }
0095
0096
0097
0098
0099 GeometryIdentifier idAt(Size index) const { return m_ids.at(index); }
0100
0101
0102
0103 const Value& valueAt(Size index) const { return m_values.at(index); }
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114 Iterator find(GeometryIdentifier id) const;
0115
0116 private:
0117
0118
0119
0120 static_assert(GeometryIdentifier().setVolume(1).value() <
0121 GeometryIdentifier().setVolume(1).setBoundary(1).value(),
0122 "Incompatible GeometryIdentifier hierarchy");
0123 static_assert(GeometryIdentifier().setBoundary(1).value() <
0124 GeometryIdentifier().setBoundary(1).setLayer(1).value(),
0125 "Incompatible GeometryIdentifier hierarchy");
0126 static_assert(GeometryIdentifier().setLayer(1).value() <
0127 GeometryIdentifier().setLayer(1).setApproach(1).value(),
0128 "Incompatible GeometryIdentifier hierarchy");
0129 static_assert(GeometryIdentifier().setApproach(1).value() <
0130 GeometryIdentifier().setApproach(1).setSensitive(1).value(),
0131 "Incompatible GeometryIdentifier hierarchy");
0132
0133 using Identifier = GeometryIdentifier::Value;
0134
0135
0136 std::vector<Identifier> m_ids;
0137
0138 std::vector<Identifier> m_masks;
0139 std::vector<Value> m_values;
0140
0141
0142 static constexpr Identifier makeLeadingLevelsMask(GeometryIdentifier id) {
0143
0144 auto allSet = GeometryIdentifier(~GeometryIdentifier::Value{0u});
0145
0146 if (id.sensitive() != 0u) {
0147
0148 return allSet.setExtra(0u).value();
0149 }
0150 if (id.approach() != 0u) {
0151 return allSet.setExtra(0u).setSensitive(0u).value();
0152 }
0153 if (id.layer() != 0u) {
0154 return allSet.setExtra(0u).setSensitive(0u).setApproach(0u).value();
0155 }
0156 if (id.boundary() != 0u) {
0157 return allSet.setExtra(0u)
0158 .setSensitive(0u)
0159 .setApproach(0u)
0160 .setLayer(0u)
0161 .value();
0162 }
0163 if (id.volume() != 0u) {
0164 return allSet.setExtra(0u)
0165 .setSensitive(0u)
0166 .setApproach(0u)
0167 .setLayer(0u)
0168 .setBoundary(0u)
0169 .value();
0170 }
0171
0172 return Identifier{0u};
0173 }
0174
0175 static constexpr Identifier makeHighestLevelMask() {
0176 return makeLeadingLevelsMask(GeometryIdentifier(0u).setVolume(1u));
0177 }
0178
0179 static constexpr bool equalWithinMask(Identifier lhs, Identifier rhs,
0180 Identifier mask) {
0181 return (lhs & mask) == (rhs & mask);
0182 }
0183
0184 template <typename iterator_t>
0185 static void sortAndCheckDuplicates(iterator_t beg, iterator_t end);
0186
0187
0188
0189
0190
0191 template <typename iterator_t>
0192 void fill(iterator_t beg, iterator_t end);
0193 };
0194
0195
0196
0197 template <typename value_t>
0198 inline GeometryHierarchyMap<value_t>::GeometryHierarchyMap(
0199 std::vector<InputElement> elements) {
0200 sortAndCheckDuplicates(elements.begin(), elements.end());
0201 fill(elements.begin(), elements.end());
0202 }
0203
0204 template <typename value_t>
0205 inline GeometryHierarchyMap<value_t>::GeometryHierarchyMap(
0206 std::initializer_list<InputElement> elements)
0207 : GeometryHierarchyMap(
0208 std::vector<InputElement>(elements.begin(), elements.end())) {}
0209
0210 template <typename value_t>
0211 template <typename iterator_t>
0212 inline void GeometryHierarchyMap<value_t>::sortAndCheckDuplicates(
0213 iterator_t beg, iterator_t end) {
0214
0215 std::sort(beg, end, [=](const auto& lhs, const auto& rhs) {
0216 return lhs.first < rhs.first;
0217 });
0218
0219 auto dup = std::adjacent_find(beg, end, [](const auto& lhs, const auto& rhs) {
0220 return lhs.first == rhs.first;
0221 });
0222 if (dup != end) {
0223 throw std::invalid_argument("Input elements contain duplicates");
0224 }
0225 }
0226
0227 template <typename value_t>
0228 template <typename iterator_t>
0229 inline void GeometryHierarchyMap<value_t>::fill(iterator_t beg,
0230 iterator_t end) {
0231 const auto n = std::distance(beg, end);
0232 m_ids.clear();
0233 m_ids.reserve(n);
0234 m_masks.clear();
0235 m_masks.reserve(n);
0236 m_values.clear();
0237 m_values.reserve(n);
0238 for (; beg != end; ++beg) {
0239 m_ids.push_back(beg->first.value());
0240 m_masks.push_back(makeLeadingLevelsMask(beg->first.value()));
0241 m_values.push_back(std::move(beg->second));
0242 }
0243 }
0244
0245 template <typename value_t>
0246 inline auto GeometryHierarchyMap<value_t>::find(GeometryIdentifier id) const
0247 -> Iterator {
0248 assert((m_ids.size() == m_values.size()) &&
0249 "Inconsistent container state: #ids != # values");
0250 assert((m_masks.size() == m_values.size()) &&
0251 "Inconsistent container state: #masks != #values");
0252
0253
0254
0255
0256
0257 const auto it = std::upper_bound(m_ids.begin(), m_ids.end(), id.value());
0258 auto i = std::distance(m_ids.begin(), it);
0259
0260
0261
0262
0263
0264
0265
0266
0267
0268
0269
0270 while (0 < i) {
0271
0272 --i;
0273
0274
0275
0276
0277
0278
0279 if (!equalWithinMask(id.value(), m_ids[i], makeHighestLevelMask())) {
0280
0281 if (m_ids.front() == Identifier{0u}) {
0282 return begin();
0283 } else {
0284 return end();
0285 }
0286 }
0287
0288
0289
0290
0291 if (equalWithinMask(id.value(), m_ids[i], m_masks[i])) {
0292 return std::next(begin(), i);
0293 }
0294 }
0295
0296
0297 return end();
0298 }
0299
0300 }