File indexing completed on 2025-06-12 23:29:41
0001 #ifndef DataFormats_Common_DetSetVector_h
0002 #define DataFormats_Common_DetSetVector_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 #include <algorithm>
0039 #include <iterator>
0040 #include <numeric>
0041 #include <type_traits>
0042 #include <vector>
0043
0044 #include <boost/concept_check.hpp>
0045
0046 #include "DataFormats/Common/interface/BoolCache.h"
0047 #include "DataFormats/Common/interface/CMS_CLASS_VERSION.h"
0048 #include "DataFormats/Common/interface/DetSet.h"
0049 #include "DataFormats/Common/interface/FillView.h"
0050 #include "DataFormats/Common/interface/Ref.h"
0051 #include "DataFormats/Common/interface/traits.h"
0052 #include "FWCore/Utilities/interface/EDMException.h"
0053
0054 namespace edm {
0055
0056 namespace detail {
0057
0058
0059
0060 template <typename Iter1, typename Iter2>
0061 void apply_permutation(Iter1 first, Iter1 last, Iter2 indices) {
0062 using T = std::iterator_traits<Iter1>::value_type;
0063 using Diff = std::iterator_traits<Iter2>::value_type;
0064 Diff length = last - first;
0065 for (Diff i = 0; i < length; i++) {
0066 Diff current = i;
0067 if (i != indices[current]) {
0068 T t{std::move(first[i])};
0069 while (i != indices[current]) {
0070 Diff next = indices[current];
0071 first[current] = std::move(first[next]);
0072 indices[current] = current;
0073 current = next;
0074 }
0075 first[current] = std::move(t);
0076 indices[current] = current;
0077 }
0078 }
0079 }
0080
0081
0082
0083 template <typename Iter, typename UnaryOperation, typename Compare>
0084 void sort_by_key(Iter first, Iter last, UnaryOperation op, Compare comp) {
0085 using Diff = std::iterator_traits<Iter>::difference_type;
0086 using T = std::iterator_traits<Iter>::value_type;
0087 using Key = decltype(op(std::declval<T>()));
0088 Diff length = std::distance(first, last);
0089 std::vector<Key> keys;
0090 keys.reserve(length);
0091 std::transform(first, last, std::back_inserter(keys), [&](T& t) { return op(t); });
0092 std::vector<Diff> indices(length);
0093 std::iota(indices.begin(), indices.end(), static_cast<Diff>(0));
0094 std::sort(indices.begin(), indices.end(), [&](Diff a, Diff b) { return comp(keys[a], keys[b]); });
0095 apply_permutation(first, last, indices.begin());
0096 }
0097
0098 template <typename Iter, typename UnaryOperation>
0099 void sort_by_key(Iter first, Iter last, UnaryOperation op) {
0100 sort_by_key(first, last, op, std::less<>());
0101 }
0102
0103 }
0104
0105 class ProductID;
0106
0107
0108
0109 template <class T>
0110 class DetSetVector;
0111
0112
0113
0114
0115
0116 namespace detail {
0117
0118 inline void _throw_range(det_id_type i) {
0119 Exception::throwThis(
0120 errors::InvalidReference, "DetSetVector::operator[] called with index not in collection;\nindex value: ", i);
0121 }
0122 }
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 template <class T>
0135 class DetSetVector : public std::conditional_t<std::is_base_of<edm::DoNotSortUponInsertion, T>::value,
0136 edm::DoNotSortUponInsertion,
0137 Other> {
0138
0139
0140 BOOST_CLASS_REQUIRE(T, boost, LessThanComparableConcept);
0141
0142 public:
0143 typedef DetSet<T> detset;
0144 typedef detset value_type;
0145 typedef std::vector<detset> collection_type;
0146
0147 typedef detset& reference;
0148 typedef detset const& const_reference;
0149
0150 typedef typename collection_type::iterator iterator;
0151 typedef typename collection_type::const_iterator const_iterator;
0152 typedef typename collection_type::size_type size_type;
0153
0154
0155
0156
0157
0158 DetSetVector();
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171 explicit DetSetVector(std::vector<DetSet<T> >& input, bool alreadySorted = false);
0172
0173 void swap(DetSetVector& other);
0174
0175
0176
0177
0178
0179
0180 void insert(detset const& s);
0181
0182
0183
0184
0185 reference find_or_insert(det_id_type id);
0186
0187
0188 bool empty() const;
0189
0190
0191 size_type size() const;
0192
0193
0194 void reserve(size_t s) { _sets.reserve(s); }
0195
0196
0197
0198
0199
0200
0201
0202 iterator find(det_id_type id);
0203 const_iterator find(det_id_type id) const;
0204
0205
0206
0207
0208 reference operator[](det_id_type i);
0209 const_reference operator[](det_id_type i) const;
0210
0211
0212 iterator begin();
0213 const_iterator begin() const;
0214
0215
0216 iterator end();
0217 const_iterator end() const;
0218
0219
0220
0221 void getIds(std::vector<det_id_type>& result) const;
0222
0223
0224
0225 void post_insert();
0226
0227 void fillView(ProductID const& id, std::vector<void const*>& pointers, FillViewHelperVector& helpers) const;
0228
0229
0230 CMS_CLASS_VERSION(10)
0231
0232 private:
0233 collection_type _sets;
0234 edm::BoolCache _alreadySorted;
0235
0236
0237 void _sort();
0238 };
0239
0240 template <class T>
0241 inline DetSetVector<T>::DetSetVector() : _sets() {}
0242
0243 template <class T>
0244 inline DetSetVector<T>::DetSetVector(std::vector<DetSet<T> >& input, bool alreadySorted)
0245 : _sets(), _alreadySorted(alreadySorted) {
0246 _sets.swap(input);
0247 if (!alreadySorted)
0248 _sort();
0249 }
0250
0251 template <class T>
0252 inline void DetSetVector<T>::swap(DetSetVector<T>& other) {
0253 _sets.swap(other._sets);
0254 bool tmp = _alreadySorted;
0255 _alreadySorted = other._alreadySorted;
0256 other._alreadySorted = tmp;
0257 }
0258
0259 template <class T>
0260 inline void DetSetVector<T>::insert(detset const& t) {
0261 _alreadySorted = false;
0262
0263 _sets.insert(std::lower_bound(_sets.begin(), _sets.end(), t), t);
0264 #if 0
0265
0266
0267 _sets.push_back(t);
0268
0269 _sort();
0270 #endif
0271 }
0272
0273 template <class T>
0274 inline typename DetSetVector<T>::reference DetSetVector<T>::find_or_insert(det_id_type id) {
0275
0276
0277 std::pair<iterator, iterator> p = std::equal_range(_sets.begin(), _sets.end(), id);
0278
0279
0280
0281 if (p.first != p.second)
0282 return *p.first;
0283
0284
0285
0286 #if defined(__GXX_EXPERIMENTAL_CXX0X__)
0287 return *(_sets.emplace(p.first, id));
0288 #else
0289 return *(_sets.insert(p.first, detset(id)));
0290 #endif
0291 }
0292
0293 template <class T>
0294 inline bool DetSetVector<T>::empty() const {
0295 return _sets.empty();
0296 }
0297
0298 template <class T>
0299 inline typename DetSetVector<T>::size_type DetSetVector<T>::size() const {
0300 return _sets.size();
0301 }
0302
0303 template <class T>
0304 inline typename DetSetVector<T>::iterator DetSetVector<T>::find(det_id_type id) {
0305 _alreadySorted = false;
0306 std::pair<iterator, iterator> p = std::equal_range(_sets.begin(), _sets.end(), id);
0307 if (p.first == p.second)
0308 return _sets.end();
0309
0310
0311
0312
0313
0314 #if 0
0315 assert(std::distance(p.first, p.second) == 1);
0316 #endif
0317
0318 return p.first;
0319 }
0320
0321 template <class T>
0322 inline typename DetSetVector<T>::const_iterator DetSetVector<T>::find(det_id_type id) const {
0323 std::pair<const_iterator, const_iterator> p = std::equal_range(_sets.begin(), _sets.end(), id);
0324 if (p.first == p.second)
0325 return _sets.end();
0326
0327
0328 assert(std::distance(p.first, p.second) == 1);
0329 return p.first;
0330 }
0331
0332 template <class T>
0333 inline typename DetSetVector<T>::reference DetSetVector<T>::operator[](det_id_type i) {
0334 _alreadySorted = false;
0335
0336
0337 iterator it = this->find(i);
0338 if (it == this->end())
0339 detail::_throw_range(i);
0340 return *it;
0341 }
0342
0343 template <class T>
0344 inline typename DetSetVector<T>::const_reference DetSetVector<T>::operator[](det_id_type i) const {
0345
0346
0347 const_iterator it = this->find(i);
0348 if (it == this->end())
0349 detail::_throw_range(i);
0350 return *it;
0351 }
0352
0353 template <class T>
0354 inline typename DetSetVector<T>::iterator DetSetVector<T>::begin() {
0355 _alreadySorted = false;
0356 return _sets.begin();
0357 }
0358
0359 template <class T>
0360 inline typename DetSetVector<T>::const_iterator DetSetVector<T>::begin() const {
0361 return _sets.begin();
0362 }
0363
0364 template <class T>
0365 inline typename DetSetVector<T>::iterator DetSetVector<T>::end() {
0366 _alreadySorted = false;
0367 return _sets.end();
0368 }
0369
0370 template <class T>
0371 inline typename DetSetVector<T>::const_iterator DetSetVector<T>::end() const {
0372 return _sets.end();
0373 }
0374
0375 template <class T>
0376 inline void DetSetVector<T>::getIds(std::vector<det_id_type>& result) const {
0377 std::transform(
0378 this->begin(), this->end(), std::back_inserter(result), std::bind(&DetSet<T>::id, std::placeholders::_1));
0379 }
0380
0381 template <class T>
0382 inline void DetSetVector<T>::post_insert() {
0383 _sets.shrink_to_fit();
0384 if (_alreadySorted)
0385 return;
0386 typename collection_type::iterator i = _sets.begin();
0387 typename collection_type::iterator e = _sets.end();
0388
0389 for (; i != e; ++i) {
0390 i->data.shrink_to_fit();
0391
0392 if constexpr (requires(const T& t) { t.sort_key(); }) {
0393 edm::detail::sort_by_key(i->data.begin(), i->data.end(), [](const T& t) { return t.sort_key(); });
0394 } else {
0395 std::sort(i->data.begin(), i->data.end());
0396 }
0397 }
0398 }
0399
0400 template <class T>
0401 inline void DetSetVector<T>::_sort() {
0402 std::sort(_sets.begin(), _sets.end());
0403 }
0404
0405 template <class T>
0406 void DetSetVector<T>::fillView(ProductID const& id,
0407 std::vector<void const*>& pointers,
0408 FillViewHelperVector& helpers) const {
0409 detail::reallyFillView(*this, id, pointers, helpers);
0410 }
0411
0412
0413
0414
0415
0416 template <class T>
0417 inline void fillView(DetSetVector<T> const& obj,
0418 ProductID const& id,
0419 std::vector<void const*>& pointers,
0420 FillViewHelperVector& helpers) {
0421 obj.fillView(id, pointers, helpers);
0422 }
0423
0424 template <class T>
0425 struct has_fillView<edm::DetSetVector<T> > {
0426 static bool const value = true;
0427 };
0428
0429
0430 template <class T>
0431 inline void swap(DetSetVector<T>& a, DetSetVector<T>& b) {
0432 a.swap(b);
0433 }
0434
0435 }
0436
0437
0438 namespace edm {
0439
0440 namespace refhelper {
0441 template <typename T>
0442 class FindForDetSetVector {
0443 public:
0444 using first_argument_type = const DetSetVector<T>&;
0445 using second_argument_type = std::pair<det_id_type, typename DetSet<T>::collection_type::size_type>;
0446 using result_type = const T*;
0447
0448 result_type operator()(first_argument_type iContainer, second_argument_type iIndex) {
0449 return &(*(iContainer.find(iIndex.first)->data.begin() + iIndex.second));
0450 }
0451 };
0452
0453 template <typename T>
0454 struct FindTrait<DetSetVector<T>, T> {
0455 typedef FindForDetSetVector<T> value;
0456 };
0457 }
0458
0459
0460
0461 template <class HandleT>
0462 inline Ref<typename HandleT::element_type, typename HandleT::element_type::value_type::value_type> makeRefTo(
0463 const HandleT& iHandle, det_id_type iDetID, typename HandleT::element_type::value_type::const_iterator itIter) {
0464 typedef typename HandleT::element_type Vec;
0465 typename Vec::value_type::collection_type::size_type index = 0;
0466 typename Vec::const_iterator itFound = iHandle->find(iDetID);
0467 if (itFound == iHandle->end()) {
0468 Exception::throwThis(errors::InvalidReference,
0469 "an edm::Ref to an edm::DetSetVector was given a DetId, ",
0470 iDetID,
0471 ", that is not in the DetSetVector");
0472 }
0473 index += (itIter - itFound->data.begin());
0474 if (index >= itFound->data.size()) {
0475 Exception::throwThis(errors::InvalidReference,
0476 "an edm::Ref to a edm::DetSetVector is being made with an interator that is not part of the "
0477 "edm::DetSet itself");
0478 }
0479 return Ref<typename HandleT::element_type, typename HandleT::element_type::value_type::value_type>(
0480 iHandle, std::make_pair(iDetID, index));
0481 }
0482
0483 template <class HandleT>
0484 inline Ref<typename HandleT::element_type, typename HandleT::element_type::value_type::value_type>
0485 makeRefToDetSetVector(const HandleT& iHandle,
0486 det_id_type iDetID,
0487 typename HandleT::element_type::value_type::iterator itIter) {
0488 typedef typename HandleT::element_type Vec;
0489 typename Vec::detset::const_iterator itIter2 = itIter;
0490 return makeRefTo(iHandle, iDetID, itIter2);
0491 }
0492 }
0493 #endif