Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:27:53

0001 // -*- C++ -*-
0002 //
0003 // Package:     Framework
0004 // Class  :     ESRecordsToProxyIndices
0005 //
0006 // Implementation:
0007 //     <Notes on implementation>
0008 //
0009 // Author:      Chris Jones
0010 // Created:     Thu Mar 31 14:31:13 EST 2005
0011 //
0012 
0013 // system include files
0014 #include <cassert>
0015 #include <algorithm>
0016 
0017 // user include files
0018 #include "FWCore/Framework/interface/ESRecordsToProxyIndices.h"
0019 #include "FWCore/Framework/interface/ComponentDescription.h"
0020 
0021 //
0022 // constants, enums and typedefs
0023 //
0024 
0025 namespace edm::eventsetup {
0026   ESRecordsToProxyIndices::ESRecordsToProxyIndices(std::vector<EventSetupRecordKey> iRecords)
0027       : recordKeys_{std::move(iRecords)} {
0028     assert(std::is_sorted(recordKeys_.begin(), recordKeys_.end()));
0029     recordOffsets_.reserve(recordKeys_.size() + 1);
0030     recordOffsets_.push_back(0);
0031   }
0032 
0033   unsigned int ESRecordsToProxyIndices::dataKeysInRecord(unsigned int iRecordIndex,
0034                                                          EventSetupRecordKey const& iRecord,
0035                                                          std::vector<DataKey> const& iDataKeys,
0036                                                          std::vector<ComponentDescription const*> const& iComponents) {
0037     assert(iRecord == recordKeys_[iRecordIndex]);
0038     assert(iDataKeys.size() == iComponents.size());
0039     assert(iRecordIndex + 1 == recordOffsets_.size());
0040     dataKeys_.insert(dataKeys_.end(), iDataKeys.begin(), iDataKeys.end());
0041     ++iRecordIndex;
0042     components_.insert(components_.end(), iComponents.begin(), iComponents.end());
0043     recordOffsets_.push_back(dataKeys_.size());
0044     return iRecordIndex;
0045   }
0046 
0047   //
0048   // const member functions
0049   //
0050   ESProxyIndex ESRecordsToProxyIndices::indexInRecord(EventSetupRecordKey const& iRK,
0051                                                       DataKey const& iDK) const noexcept {
0052     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0053     if (it == recordKeys_.end() or *it != iRK) {
0054       return missingProxyIndex();
0055     }
0056 
0057     auto beginOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0058     ++it;
0059     auto endOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0060 
0061     auto itDK = std::lower_bound(dataKeys_.begin() + beginOffset, dataKeys_.begin() + endOffset, iDK);
0062     if (itDK == dataKeys_.begin() + endOffset or *itDK != iDK) {
0063       return missingProxyIndex();
0064     }
0065 
0066     return ESProxyIndex{static_cast<int>(std::distance(dataKeys_.begin() + beginOffset, itDK))};
0067   }
0068 
0069   ESRecordIndex ESRecordsToProxyIndices::recordIndexFor(EventSetupRecordKey const& iRK) const noexcept {
0070     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0071     if (it == recordKeys_.end() or *it != iRK) {
0072       return missingRecordIndex();
0073     }
0074     return ESRecordIndex{static_cast<ESRecordIndex::Value_t>(it - recordKeys_.begin())};
0075   }
0076 
0077   ComponentDescription const* ESRecordsToProxyIndices::component(EventSetupRecordKey const& iRK,
0078                                                                  DataKey const& iDK) const noexcept {
0079     auto it = std::lower_bound(recordKeys_.begin(), recordKeys_.end(), iRK);
0080     if (it == recordKeys_.end() or *it != iRK) {
0081       return nullptr;
0082     }
0083 
0084     auto beginOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0085     ++it;
0086     auto endOffset = recordOffsets_[std::distance(recordKeys_.begin(), it)];
0087 
0088     auto itDK = std::lower_bound(dataKeys_.begin() + beginOffset, dataKeys_.begin() + endOffset, iDK);
0089     if (itDK == dataKeys_.begin() + endOffset or *itDK != iDK) {
0090       return nullptr;
0091     }
0092     return components_[std::distance(dataKeys_.begin(), itDK)];
0093   }
0094 
0095   ESTagGetter ESRecordsToProxyIndices::makeTagGetter(EventSetupRecordKey const& iRK, TypeTag const& iTT) const {
0096     auto recIndex = recordIndexFor(iRK);
0097     if (recIndex == missingRecordIndex()) {
0098       return ESTagGetter();
0099     }
0100 
0101     auto const beginIndex = recordOffsets_[recIndex.value()];
0102     auto const endIndex = recordOffsets_[recIndex.value() + 1];
0103     auto keyIndex = beginIndex;
0104 
0105     std::vector<ESTagGetter::Info> returnValue;
0106     returnValue.reserve(endIndex - beginIndex);
0107     bool foundFirstIndex = false;
0108     while (keyIndex < endIndex) {
0109       if (dataKeys_[keyIndex].type() != iTT) {
0110         if (foundFirstIndex) {
0111           //we are now past any further matches
0112           break;
0113         }
0114       } else {
0115         foundFirstIndex = true;
0116         returnValue.emplace_back(
0117             keyIndex - beginIndex,
0118             dataKeys_[keyIndex].name().value(),
0119             components_[keyIndex] ? std::string_view(components_[keyIndex]->label_) : std::string_view());
0120       }
0121       ++keyIndex;
0122     }
0123     return returnValue;
0124   }
0125 
0126   std::pair<std::vector<DataKey>::const_iterator, std::vector<DataKey>::const_iterator>
0127   ESRecordsToProxyIndices::keysForRecord(EventSetupRecordKey const& iRK) const noexcept {
0128     auto recIndex = recordIndexFor(iRK);
0129     if (recIndex == missingRecordIndex()) {
0130       return std::make_pair(dataKeys_.end(), dataKeys_.end());
0131     }
0132     auto const beginIndex = recordOffsets_[recIndex.value()];
0133     auto const endIndex = recordOffsets_[recIndex.value() + 1];
0134     return std::make_pair(dataKeys_.begin() + beginIndex, dataKeys_.begin() + endIndex);
0135   }
0136 
0137 }  // namespace edm::eventsetup