File indexing completed on 2025-07-11 07:49:54
0001
0002
0003
0004
0005
0006
0007
0008
0009 #pragma once
0010
0011 #include "Acts/Seeding/CandidatesForMiddleSp.hpp"
0012
0013 #include <limits>
0014
0015 namespace Acts {
0016
0017 template <SatisfyCandidateConcept external_space_point_t>
0018 inline void CandidatesForMiddleSp<external_space_point_t>::setMaxElements(
0019 std::size_t nLow, std::size_t nHigh) {
0020 m_maxSizeHigh = nHigh;
0021 m_maxSizeLow = nLow;
0022
0023
0024
0025 if (nHigh == std::numeric_limits<std::size_t>::max() ||
0026 nLow == std::numeric_limits<std::size_t>::max()) {
0027 return;
0028 }
0029
0030
0031 m_storage.reserve(nLow + nHigh);
0032 m_indicesHigh.reserve(nHigh);
0033 m_indicesLow.reserve(nLow);
0034 }
0035
0036 template <SatisfyCandidateConcept external_space_point_t>
0037 inline void CandidatesForMiddleSp<external_space_point_t>::pop(
0038 std::vector<std::size_t>& indices, std::size_t& currentSize) {
0039
0040
0041
0042
0043
0044
0045
0046 std::swap(indices[0], indices[currentSize - 1]);
0047 bubbledw(indices, 0, --currentSize);
0048 }
0049
0050 template <SatisfyCandidateConcept external_space_point_t>
0051 inline bool CandidatesForMiddleSp<external_space_point_t>::exists(
0052 const std::size_t n, const std::size_t maxSize) const {
0053
0054
0055 return n < maxSize;
0056 }
0057
0058 template <SatisfyCandidateConcept external_space_point_t>
0059 inline float CandidatesForMiddleSp<external_space_point_t>::weight(
0060 const std::vector<std::size_t>& indices, std::size_t n) const {
0061
0062 return m_storage[indices[n]].weight;
0063 }
0064
0065 template <SatisfyCandidateConcept external_space_point_t>
0066 inline void CandidatesForMiddleSp<external_space_point_t>::clear() {
0067
0068
0069 m_nHigh = 0;
0070 m_nLow = 0;
0071
0072 m_storage.clear();
0073 m_indicesHigh.clear();
0074 m_indicesLow.clear();
0075 }
0076
0077 template <SatisfyCandidateConcept external_space_point_t>
0078 bool CandidatesForMiddleSp<external_space_point_t>::push(
0079 external_space_point_t& spB, external_space_point_t& spM,
0080 external_space_point_t& spT, float weight, float zOrigin, bool isQuality) {
0081
0082
0083 if (isQuality) {
0084 return push(m_indicesHigh, m_nHigh, m_maxSizeHigh, spB, spM, spT, weight,
0085 zOrigin, isQuality);
0086 }
0087 return push(m_indicesLow, m_nLow, m_maxSizeLow, spB, spM, spT, weight,
0088 zOrigin, isQuality);
0089 }
0090
0091 template <SatisfyCandidateConcept external_space_point_t>
0092 bool CandidatesForMiddleSp<external_space_point_t>::push(
0093 std::vector<std::size_t>& indices, std::size_t& n, const std::size_t nMax,
0094 external_space_point_t& spB, external_space_point_t& spM,
0095 external_space_point_t& spT, float weight, float zOrigin, bool isQuality) {
0096
0097 if (nMax == 0) {
0098 return false;
0099 }
0100
0101
0102 if (n < nMax) {
0103 addToCollection(indices, n, nMax,
0104 value_type(spB, spM, spT, weight, zOrigin, isQuality));
0105 return true;
0106 }
0107
0108
0109
0110 if (float lowestWeight = this->weight(indices, 0); weight <= lowestWeight) {
0111 return false;
0112 }
0113
0114
0115 pop(indices, n);
0116 addToCollection(indices, n, nMax,
0117 value_type(spB, spM, spT, weight, zOrigin, isQuality));
0118 return true;
0119 }
0120
0121 template <SatisfyCandidateConcept external_space_point_t>
0122 void CandidatesForMiddleSp<external_space_point_t>::addToCollection(
0123 std::vector<std::size_t>& indices, std::size_t& n, const std::size_t nMax,
0124 value_type&& element) {
0125
0126 if (indices.size() == nMax) {
0127 m_storage[indices[n]] = std::move(element);
0128 } else {
0129 m_storage.push_back(std::move(element));
0130 indices.push_back(m_storage.size() - 1);
0131 }
0132
0133 bubbleup(indices, n++);
0134 }
0135
0136 template <SatisfyCandidateConcept external_space_point_t>
0137 void CandidatesForMiddleSp<external_space_point_t>::bubbledw(
0138 std::vector<std::size_t>& indices, std::size_t n, std::size_t actualSize) {
0139 while (n < actualSize) {
0140
0141
0142
0143 float current = weight(indices, n);
0144 std::size_t leftChild = 2 * n + 1;
0145 std::size_t rightChild = 2 * n + 2;
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158 if (!exists(leftChild, actualSize)) {
0159 break;
0160 }
0161
0162
0163
0164
0165
0166 float weightLeftChild = weight(indices, leftChild);
0167
0168 std::size_t selectedChild = leftChild;
0169 float selectedWeight = weightLeftChild;
0170
0171
0172 if (exists(rightChild, actualSize)) {
0173 float weightRightChild = weight(indices, rightChild);
0174 if (weightRightChild <= weightLeftChild) {
0175 selectedChild = rightChild;
0176 selectedWeight = weightRightChild;
0177 }
0178 }
0179
0180
0181
0182
0183 if (selectedWeight >= current) {
0184 break;
0185 }
0186
0187
0188 std::swap(indices[n], indices[selectedChild]);
0189 n = selectedChild;
0190 }
0191 }
0192
0193 template <SatisfyCandidateConcept external_space_point_t>
0194 void CandidatesForMiddleSp<external_space_point_t>::bubbleup(
0195 std::vector<std::size_t>& indices, std::size_t n) {
0196 while (n != 0) {
0197
0198
0199
0200 std::size_t parentIdx = (n - 1) / 2;
0201
0202 float weightCurrent = weight(indices, n);
0203
0204 if (float weightParent = weight(indices, parentIdx);
0205 weightParent <= weightCurrent) {
0206 break;
0207 }
0208
0209
0210 std::swap(indices[n], indices[parentIdx]);
0211 n = parentIdx;
0212 }
0213 }
0214
0215 template <SatisfyCandidateConcept external_space_point_t>
0216 std::vector<typename CandidatesForMiddleSp<external_space_point_t>::value_type>
0217 CandidatesForMiddleSp<external_space_point_t>::storage() {
0218
0219
0220 std::vector<value_type> output(m_nHigh + m_nLow);
0221 std::size_t outIdx = output.size() - 1;
0222
0223
0224
0225
0226 while (m_nHigh != 0 || m_nLow != 0) {
0227
0228 if (m_nHigh == 0) {
0229 std::size_t idx = m_nLow;
0230 for (std::size_t i(0); i < idx; i++) {
0231 output[outIdx--] = std::move(m_storage[m_indicesLow[0]]);
0232 pop(m_indicesLow, m_nLow);
0233 }
0234 break;
0235 }
0236
0237
0238 if (m_nLow == 0) {
0239 std::size_t idx = m_nHigh;
0240 for (std::size_t i(0); i < idx; i++) {
0241 output[outIdx--] = std::move(m_storage[m_indicesHigh[0]]);
0242 pop(m_indicesHigh, m_nHigh);
0243 }
0244 break;
0245 }
0246
0247
0248 if (descendingByQuality(m_storage[m_indicesLow[0]],
0249 m_storage[m_indicesHigh[0]])) {
0250 output[outIdx--] = std::move(m_storage[m_indicesHigh[0]]);
0251 pop(m_indicesHigh, m_nHigh);
0252 } else {
0253 output[outIdx--] = std::move(m_storage[m_indicesLow[0]]);
0254 pop(m_indicesLow, m_nLow);
0255 }
0256
0257 }
0258
0259 clear();
0260 return output;
0261 }
0262
0263 template <SatisfyCandidateConcept external_space_point_t>
0264 bool CandidatesForMiddleSp<external_space_point_t>::descendingByQuality(
0265 const value_type& i1, const value_type& i2) {
0266 if (i1.weight != i2.weight) {
0267 return i1.weight > i2.weight;
0268 }
0269
0270
0271
0272
0273 const auto& bottomL1 = i1.bottom;
0274 const auto& middleL1 = i1.middle;
0275 const auto& topL1 = i1.top;
0276
0277 const auto& bottomL2 = i2.bottom;
0278 const auto& middleL2 = i2.middle;
0279 const auto& topL2 = i2.top;
0280
0281 float seed1_sum = 0.;
0282 float seed2_sum = 0.;
0283
0284 seed1_sum += bottomL1->y() * bottomL1->y() + bottomL1->z() * bottomL1->z();
0285 seed1_sum += middleL1->y() * middleL1->y() + middleL1->z() * middleL1->z();
0286 seed1_sum += topL1->y() * topL1->y() + topL1->z() * topL1->z();
0287
0288 seed2_sum += bottomL2->y() * bottomL2->y() + bottomL2->z() * bottomL2->z();
0289 seed2_sum += middleL2->y() * middleL2->y() + middleL2->z() * middleL2->z();
0290 seed2_sum += topL2->y() * topL2->y() + topL2->z() * topL2->z();
0291
0292 return seed1_sum > seed2_sum;
0293 }
0294
0295 template <SatisfyCandidateConcept external_space_point_t>
0296 bool CandidatesForMiddleSp<external_space_point_t>::ascendingByQuality(
0297 const value_type& i1, const value_type& i2) {
0298 return !descendingByQuality(i1, i2);
0299 }
0300
0301 template <SatisfyCandidateConcept external_space_point_t>
0302 std::size_t
0303 CandidatesForMiddleSp<external_space_point_t>::nLowQualityCandidates() const {
0304 return m_nLow;
0305 }
0306
0307 template <SatisfyCandidateConcept external_space_point_t>
0308 std::size_t
0309 CandidatesForMiddleSp<external_space_point_t>::nHighQualityCandidates() const {
0310 return m_nHigh;
0311 }
0312
0313 }