Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-20 02:18:29

0001 
0002 #include "DataFormats/Provenance/interface/ProductResolverIndexHelper.h"
0003 #include "DataFormats/Provenance/interface/ViewTypeChecker.h"
0004 #include "FWCore/Reflection/interface/DictionaryTools.h"
0005 #include "FWCore/Utilities/interface/EDMException.h"
0006 #include "FWCore/Reflection/interface/TypeWithDict.h"
0007 #include "FWCore/Utilities/interface/WrappedClassName.h"
0008 #include "FWCore/Utilities/interface/getAnyPtr.h"
0009 
0010 #include <TClass.h>
0011 
0012 #include <cassert>
0013 #include <iostream>
0014 #include <limits>
0015 
0016 namespace edm {
0017 
0018   namespace productholderindexhelper {
0019     TypeID getContainedTypeFromWrapper(TypeID const& wrappedTypeID, std::string const& className) {
0020       static int const vtcOffset =
0021           TClass::GetClass("edm::WrapperBase")->GetBaseClassOffset(TClass::GetClass("edm::ViewTypeChecker"));
0022       static TClass const* const wbClass = TClass::GetClass("edm::WrapperBase");
0023       static std::string const refVector("edm::RefVector<");
0024       static std::string const refToBaseVector("edm::RefToBaseVector<");
0025       static std::string const ptrVector("edm::PtrVector<");
0026       static std::string const vectorPtr("std::vector<edm::Ptr<");
0027       static std::string const vectorUniquePtr("std::vector<std::unique_ptr<");
0028       static std::string const associationMap("edm::AssociationMap<");
0029       static std::string const newDetSetVector("edmNew::DetSetVector<");
0030       static size_t const rvsize = refVector.size();
0031       static size_t const rtbvsize = refToBaseVector.size();
0032       static size_t const pvsize = ptrVector.size();
0033       static size_t const vpsize = vectorPtr.size();
0034       static size_t const vupsize = vectorUniquePtr.size();
0035       static size_t const amsize = associationMap.size();
0036       static size_t const ndsize = newDetSetVector.size();
0037       bool mayBeRefVector = (className.substr(0, rvsize) == refVector) ||
0038                             (className.substr(0, rtbvsize) == refToBaseVector) ||
0039                             (className.substr(0, pvsize) == ptrVector) || (className.substr(0, vpsize) == vectorPtr) ||
0040                             (className.substr(0, vupsize) == vectorUniquePtr);
0041       // AssociationMap and edmNew::DetSetVector do not support View and
0042       // this function is used to get a contained type that can be accessed
0043       // using a View. So return the void type in these cases.
0044       // In practice, they were the only types causing a problem, but any
0045       // type with a typedef named value_type that does not support
0046       // View might also cause problems and might need to be added here in
0047       // the future.
0048       if (className.substr(0, amsize) == associationMap || className.substr(0, ndsize) == newDetSetVector) {
0049         return TypeID(typeid(void));
0050       }
0051       TClass* cl = TClass::GetClass(wrappedTypeID.className().c_str());
0052       if (cl == nullptr) {
0053         return TypeID(typeid(void));
0054       }
0055       void* p = cl->New();
0056       int offset = cl->GetBaseClassOffset(wbClass) + vtcOffset;
0057       std::unique_ptr<ViewTypeChecker> checker = getAnyPtr<ViewTypeChecker>(p, offset);
0058       if (mayBeRefVector) {
0059         std::type_info const& ti = checker->memberTypeInfo();
0060         if (ti != typeid(void)) {
0061           return TypeID(ti);
0062         }
0063       }
0064       return TypeID(checker->valueTypeInfo());
0065     }
0066 
0067     TypeID getContainedType(TypeID const& typeID) {
0068       const std::string& className = typeID.className();
0069       TypeWithDict const wrappedType = TypeWithDict::byName(wrappedClassName(className));
0070       TypeID const wrappedTypeID = TypeID(wrappedType.typeInfo());
0071       return getContainedTypeFromWrapper(wrappedTypeID, className);
0072     }
0073 
0074     bool typeIsViewCompatible(TypeID const& requestedViewType,
0075                               TypeID const& wrappedtypeID,
0076                               std::string const& className) {
0077       auto elementType = getContainedTypeFromWrapper(wrappedtypeID, className);
0078       if (elementType == TypeID(typeid(void)) or elementType == TypeID()) {
0079         //the wrapped type is not a container
0080         return false;
0081       }
0082       if (elementType == requestedViewType) {
0083         return true;
0084       }
0085       //need to check for inheritance match
0086       std::vector<std::string> missingDictionaries;
0087       std::vector<TypeID> baseTypes;
0088       if (!public_base_classes(missingDictionaries, elementType, baseTypes)) {
0089         return false;
0090       }
0091       for (auto const& base : baseTypes) {
0092         if (TypeID(base.typeInfo()) == requestedViewType) {
0093           return true;
0094         }
0095       }
0096       return false;
0097     }
0098 
0099   }  // namespace productholderindexhelper
0100 
0101   ProductResolverIndexHelper::ProductResolverIndexHelper()
0102       : nextIndexValue_(0),
0103         beginElements_(0),
0104         items_(new std::set<ProductResolverIndexHelper::Item>),
0105         processItems_(new std::set<std::string>) {}
0106 
0107   ProductResolverIndex ProductResolverIndexHelper::index(KindOfType kindOfType,
0108                                                          TypeID const& typeID,
0109                                                          char const* moduleLabel,
0110                                                          char const* instance,
0111                                                          char const* process) const {
0112     unsigned int iToIndexAndNames = indexToIndexAndNames(kindOfType, typeID, moduleLabel, instance, process);
0113 
0114     if (iToIndexAndNames == std::numeric_limits<unsigned int>::max()) {
0115       return ProductResolverIndexInvalid;
0116     }
0117     return indexAndNames_[iToIndexAndNames].index();
0118   }
0119 
0120   ProductResolverIndexHelper::Matches::Matches(ProductResolverIndexHelper const* productResolverIndexHelper,
0121                                                unsigned int startInIndexAndNames,
0122                                                unsigned int numberOfMatches)
0123       : productResolverIndexHelper_(productResolverIndexHelper),
0124         startInIndexAndNames_(startInIndexAndNames),
0125         numberOfMatches_(numberOfMatches) {
0126     if (numberOfMatches != 0 &&
0127         startInIndexAndNames_ + numberOfMatches_ > productResolverIndexHelper_->indexAndNames_.size()) {
0128       throw Exception(errors::LogicError)
0129           << "ProductResolverIndexHelper::Matches::Matches - Arguments exceed vector bounds.\n";
0130     }
0131   }
0132 
0133   ProductResolverIndex ProductResolverIndexHelper::Matches::index(unsigned int i) const {
0134     if (i >= numberOfMatches_) {
0135       throw Exception(errors::LogicError) << "ProductResolverIndexHelper::Matches::index - Argument is out of range.\n";
0136     }
0137     return productResolverIndexHelper_->indexAndNames_[startInIndexAndNames_ + i].index();
0138   }
0139 
0140   bool ProductResolverIndexHelper::Matches::isFullyResolved(unsigned int i) const {
0141     if (i >= numberOfMatches_) {
0142       throw Exception(errors::LogicError)
0143           << "ProductResolverIndexHelper::Matches::isFullyResolved - Argument is out of range.\n";
0144     }
0145     return (productResolverIndexHelper_->indexAndNames_[startInIndexAndNames_ + i].startInProcessNames() != 0U);
0146   }
0147 
0148   char const* ProductResolverIndexHelper::Matches::processName(unsigned int i) const {
0149     if (i >= numberOfMatches_) {
0150       throw Exception(errors::LogicError)
0151           << "ProductResolverIndexHelper::Matches::processName - Argument is out of range.\n";
0152     }
0153     unsigned int startInProcessNames =
0154         productResolverIndexHelper_->indexAndNames_[startInIndexAndNames_ + i].startInProcessNames();
0155     return &productResolverIndexHelper_->processNames_[startInProcessNames];
0156   }
0157 
0158   char const* ProductResolverIndexHelper::Matches::productInstanceName(unsigned int i) const {
0159     if (i >= numberOfMatches_) {
0160       throw Exception(errors::LogicError)
0161           << "ProductResolverIndexHelper::Matches::productInstanceName - Argument is out of range.\n";
0162     }
0163     unsigned int start =
0164         productResolverIndexHelper_->indexAndNames_[startInIndexAndNames_ + i].startInBigNamesContainer();
0165     auto moduleLabelSize = strlen(&productResolverIndexHelper_->bigNamesContainer_[start]);
0166     return &productResolverIndexHelper_->bigNamesContainer_[start + moduleLabelSize + 1];
0167   }
0168 
0169   char const* ProductResolverIndexHelper::Matches::moduleLabel(unsigned int i) const {
0170     if (i >= numberOfMatches_) {
0171       throw Exception(errors::LogicError)
0172           << "ProductResolverIndexHelper::Matches::moduleLabel - Argument is out of range.\n";
0173     }
0174     unsigned int start =
0175         productResolverIndexHelper_->indexAndNames_[startInIndexAndNames_ + i].startInBigNamesContainer();
0176     return &productResolverIndexHelper_->bigNamesContainer_[start];
0177   }
0178 
0179   ProductResolverIndexHelper::Matches ProductResolverIndexHelper::relatedIndexes(KindOfType kindOfType,
0180                                                                                  TypeID const& typeID,
0181                                                                                  char const* moduleLabel,
0182                                                                                  char const* instance) const {
0183     unsigned int startInIndexAndNames = indexToIndexAndNames(kindOfType, typeID, moduleLabel, instance, nullptr);
0184     unsigned int numberOfMatches = 1;
0185 
0186     if (startInIndexAndNames == std::numeric_limits<unsigned int>::max()) {
0187       numberOfMatches = 0;
0188     } else {
0189       auto vSize = indexAndNames_.size();
0190       for (unsigned int j = startInIndexAndNames + 1U; j < vSize && (indexAndNames_[j].startInProcessNames() != 0U);
0191            ++j) {
0192         ++numberOfMatches;
0193       }
0194     }
0195     return Matches(this, startInIndexAndNames, numberOfMatches);
0196   }
0197 
0198   ProductResolverIndexHelper::Matches ProductResolverIndexHelper::relatedIndexes(KindOfType kindOfType,
0199                                                                                  TypeID const& typeID) const {
0200     unsigned int startInIndexAndNames = std::numeric_limits<unsigned int>::max();
0201     unsigned int numberOfMatches = 0;
0202 
0203     // Look for the type and check to see if it found it
0204     unsigned iType = indexToType(kindOfType, typeID);
0205     if (iType != std::numeric_limits<unsigned int>::max()) {
0206       // Get the range of entries with a matching TypeID
0207       Range const& range = ranges_[iType];
0208 
0209       startInIndexAndNames = range.begin();
0210       numberOfMatches = range.end() - range.begin();
0211     }
0212     return Matches(this, startInIndexAndNames, numberOfMatches);
0213   }
0214 
0215   ProductResolverIndex ProductResolverIndexHelper::insert(TypeID const& typeID,
0216                                                           char const* moduleLabel,
0217                                                           char const* instance,
0218                                                           char const* process,
0219                                                           TypeID const& containedTypeID,
0220                                                           std::vector<TypeID>* baseTypesOfContainedType) {
0221     if (!items_) {
0222       throw Exception(errors::LogicError)
0223           << "ProductResolverIndexHelper::insert - Attempt to insert more elements after frozen.\n";
0224     }
0225 
0226     if (process == nullptr || *process == '\0') {
0227       throw Exception(errors::LogicError) << "ProductResolverIndexHelper::insert - Empty process.\n";
0228     }
0229 
0230     // Throw if this has already been inserted
0231     Item item(PRODUCT_TYPE, typeID, moduleLabel, instance, process, 0);
0232     std::set<Item>::iterator iter = items_->find(item);
0233     if (iter != items_->end()) {
0234       throw Exception(errors::LogicError)
0235           << "ProductResolverIndexHelper::insert - Attempt to insert duplicate entry.\n";
0236     }
0237 
0238     // Put in an entry for the product
0239     item.setIndex(nextIndexValue_);
0240     unsigned int savedProductIndex = nextIndexValue_;
0241     ++nextIndexValue_;
0242     items_->insert(item);
0243 
0244     // Put in an entry for the product with an empty process name
0245     // if it is not already there
0246     item.clearProcess();
0247     iter = items_->find(item);
0248     if (iter == items_->end()) {
0249       item.setIndex(nextIndexValue_);
0250       ++nextIndexValue_;
0251       items_->insert(item);
0252     }
0253 
0254     // Now put in entries for a contained class if this is a
0255     // recognized container.
0256     if (containedTypeID != TypeID(typeid(void)) && containedTypeID != TypeID()) {
0257       TypeWithDict containedType(containedTypeID.typeInfo());
0258 
0259       Item containedItem(ELEMENT_TYPE, containedTypeID, moduleLabel, instance, process, savedProductIndex);
0260       iter = items_->find(containedItem);
0261       if (iter != items_->end()) {
0262         containedItem.setIndex(ProductResolverIndexAmbiguous);
0263         items_->erase(iter);
0264       }
0265       items_->insert(containedItem);
0266 
0267       containedItem.clearProcess();
0268       iter = items_->find(containedItem);
0269       if (iter == items_->end()) {
0270         containedItem.setIndex(nextIndexValue_);
0271         ++nextIndexValue_;
0272         items_->insert(containedItem);
0273       }
0274 
0275       // Repeat this for all public base classes of the contained type
0276       if (baseTypesOfContainedType) {
0277         for (TypeID const& baseTypeID : *baseTypesOfContainedType) {
0278           Item baseItem(ELEMENT_TYPE, baseTypeID, moduleLabel, instance, process, savedProductIndex);
0279           iter = items_->find(baseItem);
0280           if (iter != items_->end()) {
0281             baseItem.setIndex(ProductResolverIndexAmbiguous);
0282             items_->erase(iter);
0283           }
0284           items_->insert(baseItem);
0285 
0286           baseItem.clearProcess();
0287           iter = items_->find(baseItem);
0288           if (iter == items_->end()) {
0289             baseItem.setIndex(nextIndexValue_);
0290             ++nextIndexValue_;
0291             items_->insert(baseItem);
0292           }
0293         }
0294       }
0295     }
0296     return savedProductIndex;
0297   }
0298 
0299   ProductResolverIndex ProductResolverIndexHelper::insert(TypeID const& typeID,
0300                                                           char const* moduleLabel,
0301                                                           char const* instance,
0302                                                           char const* process) {
0303     TypeID containedTypeID = productholderindexhelper::getContainedType(typeID);
0304     bool hasContainedType = (containedTypeID != TypeID(typeid(void)) && containedTypeID != TypeID());
0305     std::vector<TypeID> baseTypes;
0306     std::vector<TypeID>* baseTypesOfContainedType = &baseTypes;
0307     if (hasContainedType) {
0308       std::vector<std::string> missingDictionaries;
0309       public_base_classes(missingDictionaries, containedTypeID, baseTypes);
0310     }
0311     return insert(typeID, moduleLabel, instance, process, containedTypeID, baseTypesOfContainedType);
0312   }
0313 
0314   void ProductResolverIndexHelper::setFrozen() {
0315     if (!items_)
0316       return;
0317 
0318     // Make a first pass and count things so we
0319     // can reserve memory in the vectors. Also
0320     // fill processItems_ on the first pass.
0321     bool iFirstThisType = true;
0322     bool beginElementsWasSet = false;
0323     TypeID previousTypeID;
0324     KindOfType previousKindOfType = PRODUCT_TYPE;
0325     std::string previousModuleLabel;
0326     std::string previousInstance;
0327     unsigned int iCountTypes = 0;
0328     unsigned int iCountCharacters = 0;
0329     for (auto const& item : *items_) {
0330       if (iFirstThisType || item.typeID() != previousTypeID || item.kindOfType() != previousKindOfType) {
0331         ++iCountTypes;
0332         iFirstThisType = true;
0333 
0334         if (!beginElementsWasSet) {
0335           if (item.kindOfType() == ELEMENT_TYPE) {
0336             beginElementsWasSet = true;
0337           } else {
0338             beginElements_ = iCountTypes;
0339           }
0340         }
0341       }
0342 
0343       processItems_->insert(item.process());
0344 
0345       if (iFirstThisType || item.moduleLabel() != previousModuleLabel || item.instance() != previousInstance) {
0346         iCountCharacters += item.moduleLabel().size();
0347         iCountCharacters += item.instance().size();
0348         iCountCharacters += 2;
0349       }
0350 
0351       iFirstThisType = false;
0352       previousTypeID = item.typeID();
0353       previousKindOfType = item.kindOfType();
0354       previousModuleLabel = item.moduleLabel();
0355       previousInstance = item.instance();
0356     }
0357 
0358     // Size and fill the process name vector
0359     unsigned int processNamesSize = 0;
0360     for (auto const& processItem : *processItems_) {
0361       processNamesSize += processItem.size();
0362       ++processNamesSize;
0363     }
0364     processNames_.reserve(processNamesSize);
0365     for (auto const& processItem : *processItems_) {
0366       for (auto const& c : processItem) {
0367         processNames_.push_back(c);
0368       }
0369       processNames_.push_back('\0');
0370       lookupProcessNames_.push_back(processItem);
0371     }
0372 
0373     // Reserve memory in the vectors
0374     sortedTypeIDs_.reserve(iCountTypes);
0375     ranges_.reserve(iCountTypes);
0376     indexAndNames_.reserve(items_->size());
0377     bigNamesContainer_.reserve(iCountCharacters);
0378 
0379     // Second pass. Really fill the vectors this time.
0380     bool iFirstType = true;
0381     iFirstThisType = true;
0382     previousTypeID = TypeID();
0383     unsigned int iCount = 0;
0384     unsigned int iBeginning = 0;
0385     iCountCharacters = 0;
0386     unsigned int previousCharacterCount = 0;
0387     if (!items_->empty()) {
0388       for (auto const& item : *items_) {
0389         if (iFirstThisType || item.typeID() != previousTypeID || item.kindOfType() != previousKindOfType) {
0390           iFirstThisType = true;
0391           sortedTypeIDs_.push_back(item.typeID());
0392           if (iFirstType) {
0393             iFirstType = false;
0394           } else {
0395             ranges_.push_back(Range(iBeginning, iCount));
0396           }
0397           iBeginning = iCount;
0398         }
0399         ++iCount;
0400 
0401         if (iFirstThisType || item.moduleLabel() != previousModuleLabel || item.instance() != previousInstance) {
0402           unsigned int labelSize = item.moduleLabel().size();
0403           for (unsigned int j = 0; j < labelSize; ++j) {
0404             bigNamesContainer_.push_back(item.moduleLabel()[j]);
0405           }
0406           bigNamesContainer_.push_back('\0');
0407 
0408           unsigned int instanceSize = item.instance().size();
0409           for (unsigned int j = 0; j < instanceSize; ++j) {
0410             bigNamesContainer_.push_back(item.instance()[j]);
0411           }
0412           bigNamesContainer_.push_back('\0');
0413 
0414           previousCharacterCount = iCountCharacters;
0415 
0416           iCountCharacters += labelSize;
0417           iCountCharacters += instanceSize;
0418           iCountCharacters += 2;
0419         }
0420 
0421         unsigned int processStart = processIndex(item.process().c_str());
0422         if (processStart == std::numeric_limits<unsigned int>::max()) {
0423           throw Exception(errors::LogicError)
0424               << "ProductResolverIndexHelper::setFrozen - Process not found in processNames_.\n";
0425         }
0426         indexAndNames_.emplace_back(item.index(), previousCharacterCount, processStart);
0427 
0428         iFirstThisType = false;
0429         previousTypeID = item.typeID();
0430         previousKindOfType = item.kindOfType();
0431         previousModuleLabel = item.moduleLabel();
0432         previousInstance = item.instance();
0433       }
0434       ranges_.push_back(Range(iBeginning, iCount));
0435     }
0436 
0437     // Some sanity checks to protect against out of bounds vector accesses
0438     // These should only fail if there is a bug. If profiling ever shows
0439     // them to be expensive one might delete them.
0440     sanityCheck();
0441 
0442     // Cleanup, do not need the temporary containers anymore
0443     // propagate_const<T> has no reset() function
0444     items_ = nullptr;
0445     processItems_ = nullptr;
0446   }
0447 
0448   std::vector<std::string> const& ProductResolverIndexHelper::lookupProcessNames() const {
0449     if (items_) {
0450       throw Exception(errors::LogicError)
0451           << "ProductResolverIndexHelper::lookupProcessNames - Attempt to access names before frozen.\n";
0452     }
0453     return lookupProcessNames_;
0454   }
0455 
0456   unsigned int ProductResolverIndexHelper::indexToIndexAndNames(KindOfType kindOfType,
0457                                                                 TypeID const& typeID,
0458                                                                 char const* moduleLabel,
0459                                                                 char const* instance,
0460                                                                 char const* process) const {
0461     // Look for the type and check to see if it found it
0462     unsigned iType = indexToType(kindOfType, typeID);
0463     if (iType != std::numeric_limits<unsigned int>::max()) {
0464       unsigned startProcess = 0;
0465       if (process) {
0466         startProcess = processIndex(process);
0467         if (startProcess == std::numeric_limits<unsigned int>::max()) {
0468           return std::numeric_limits<unsigned int>::max();
0469         }
0470       }
0471 
0472       ProductResolverIndexHelper::Range const& range = ranges_[iType];
0473       unsigned int begin = range.begin();
0474       unsigned int end = range.end();
0475 
0476       while (begin < end) {
0477         unsigned int midpoint = begin + ((end - begin) / 2);
0478         char const* namePtr = &bigNamesContainer_[indexAndNames_[midpoint].startInBigNamesContainer()];
0479 
0480         // Compare the module label
0481         char const* label = moduleLabel;
0482         while (*namePtr && (*namePtr == *label)) {
0483           ++namePtr;
0484           ++label;
0485         }
0486         if (*namePtr == *label) {  // true only if both are at the '\0' at the end of the C string
0487           ++namePtr;               // move to the next C string
0488 
0489           // Compare the instance name
0490           char const* instanceName = instance;
0491           while (*namePtr && (*namePtr == *instanceName)) {
0492             ++namePtr;
0493             ++instanceName;
0494           }
0495           if (*namePtr == *instanceName) {
0496             // Compare the process name
0497             if (startProcess == indexAndNames_[midpoint].startInProcessNames()) {
0498               return midpoint;
0499             } else {
0500               if (indexAndNames_[midpoint].startInProcessNames() > startProcess) {
0501                 while (true) {
0502                   --midpoint;
0503                   if (indexAndNames_[midpoint].startInProcessNames() == startProcess) {
0504                     return midpoint;
0505                   }
0506                   if (indexAndNames_[midpoint].startInProcessNames() == 0)
0507                     break;
0508                 }
0509               } else {
0510                 while (true) {
0511                   ++midpoint;
0512                   if (midpoint == indexAndNames_.size())
0513                     break;
0514                   if (indexAndNames_[midpoint].startInProcessNames() == 0)
0515                     break;
0516                   if (indexAndNames_[midpoint].startInProcessNames() == startProcess) {
0517                     return midpoint;
0518                   }
0519                 }
0520               }
0521               break;
0522             }
0523           } else if (*namePtr < *instanceName) {
0524             if (begin == midpoint)
0525               break;
0526             begin = midpoint;
0527           } else {
0528             end = midpoint;
0529           }
0530         } else if (*namePtr < *label) {
0531           if (begin == midpoint)
0532             break;
0533           begin = midpoint;
0534         } else {
0535           end = midpoint;
0536         }
0537       }  // end while (begin < end)
0538     }
0539     return std::numeric_limits<unsigned int>::max();
0540   }
0541 
0542   unsigned int ProductResolverIndexHelper::indexToType(KindOfType kindOfType, TypeID const& typeID) const {
0543     unsigned int beginType = 0;
0544     unsigned int endType = beginElements_;
0545     if (kindOfType == ELEMENT_TYPE) {
0546       beginType = beginElements_;
0547       endType = sortedTypeIDs_.size();
0548     }
0549 
0550     while (beginType < endType) {
0551       unsigned int midpointType = beginType + ((endType - beginType) / 2);
0552       if (sortedTypeIDs_[midpointType] == typeID) {
0553         return midpointType;  // Found it
0554       } else if (sortedTypeIDs_[midpointType] < typeID) {
0555         if (beginType == midpointType)
0556           break;
0557         beginType = midpointType;
0558       } else {
0559         endType = midpointType;
0560       }
0561     }
0562     return std::numeric_limits<unsigned int>::max();  // Failed to find it
0563   }
0564 
0565   unsigned int ProductResolverIndexHelper::processIndex(char const* process) const {
0566     char const* ptr = &processNames_[0];
0567     char const* begin = ptr;
0568     while (true) {
0569       char const* p = process;
0570       char const* beginName = ptr;
0571       while (*ptr && (*ptr == *p)) {
0572         ++ptr;
0573         ++p;
0574       }
0575       if (*ptr == *p) {
0576         return beginName - begin;
0577       }
0578       while (*ptr) {
0579         ++ptr;
0580       }
0581       ++ptr;
0582       if (static_cast<unsigned>(ptr - begin) >= processNames_.size()) {
0583         return std::numeric_limits<unsigned int>::max();
0584       }
0585     }
0586     return 0;
0587   }
0588 
0589   ProductResolverIndexHelper::ModulesToIndiciesMap ProductResolverIndexHelper::indiciesForModulesInProcess(
0590       const std::string& iProcessName) const {
0591     ModulesToIndiciesMap result;
0592     for (unsigned int i = 0; i < beginElements_; ++i) {
0593       auto const& range = ranges_[i];
0594       for (unsigned int j = range.begin(); j < range.end(); ++j) {
0595         auto const& indexAndNames = indexAndNames_[j];
0596         if (0 == strcmp(&processNames_[indexAndNames.startInProcessNames()], iProcessName.c_str())) {
0597           //The first null terminated string is the module label
0598           auto pModLabel = &bigNamesContainer_[indexAndNames.startInBigNamesContainer()];
0599           auto l = strlen(pModLabel);
0600           auto pInstance = pModLabel + l + 1;
0601           result.emplace(pModLabel, std::make_tuple(&sortedTypeIDs_[i], pInstance, indexAndNames.index()));
0602         }
0603       }
0604     }
0605     return result;
0606   }
0607 
0608   void ProductResolverIndexHelper::sanityCheck() const {
0609     bool sanityChecksPass = true;
0610     if (sortedTypeIDs_.size() != ranges_.size())
0611       sanityChecksPass = false;
0612 
0613     unsigned int previousEnd = 0;
0614     for (auto const& range : ranges_) {
0615       if (range.begin() != previousEnd)
0616         sanityChecksPass = false;
0617       if (range.begin() >= range.end())
0618         sanityChecksPass = false;
0619       previousEnd = range.end();
0620     }
0621     if (previousEnd != indexAndNames_.size())
0622       sanityChecksPass = false;
0623 
0624     unsigned maxStart = 0;
0625     unsigned maxStartProcess = 0;
0626     for (auto const& indexAndName : indexAndNames_) {
0627       if (indexAndName.index() >= nextIndexValue_ && indexAndName.index() != ProductResolverIndexAmbiguous)
0628         sanityChecksPass = false;
0629 
0630       if (indexAndName.startInBigNamesContainer() >= bigNamesContainer_.size())
0631         sanityChecksPass = false;
0632       if (indexAndName.startInProcessNames() >= processNames_.size())
0633         sanityChecksPass = false;
0634 
0635       if (indexAndName.startInBigNamesContainer() > maxStart)
0636         maxStart = indexAndName.startInBigNamesContainer();
0637       if (indexAndName.startInProcessNames() > maxStartProcess)
0638         maxStartProcess = indexAndName.startInProcessNames();
0639     }
0640 
0641     if (!indexAndNames_.empty()) {
0642       if (bigNamesContainer_.back() != '\0')
0643         sanityChecksPass = false;
0644       if (processNames_.back() != '\0')
0645         sanityChecksPass = false;
0646       if (maxStart >= bigNamesContainer_.size())
0647         sanityChecksPass = false;
0648       unsigned int countZeroes = 0;
0649       for (unsigned j = maxStart; j < bigNamesContainer_.size(); ++j) {
0650         if (bigNamesContainer_[j] == '\0') {
0651           ++countZeroes;
0652         }
0653       }
0654       if (countZeroes != 2)
0655         sanityChecksPass = false;
0656       if (maxStartProcess >= processNames_.size())
0657         sanityChecksPass = false;
0658       countZeroes = 0;
0659       for (unsigned j = maxStartProcess; j < processNames_.size(); ++j) {
0660         if (processNames_[j] == '\0') {
0661           ++countZeroes;
0662         }
0663       }
0664       if (countZeroes != 1)
0665         sanityChecksPass = false;
0666     }
0667 
0668     if (!sanityChecksPass) {
0669       throw Exception(errors::LogicError) << "ProductResolverIndexHelper::setFrozen - Detected illegal state.\n";
0670     }
0671   }
0672 
0673   ProductResolverIndexHelper::Item::Item(KindOfType kindOfType,
0674                                          TypeID const& typeID,
0675                                          std::string const& moduleLabel,
0676                                          std::string const& instance,
0677                                          std::string const& process,
0678                                          ProductResolverIndex index)
0679       : kindOfType_(kindOfType),
0680         typeID_(typeID),
0681         moduleLabel_(moduleLabel),
0682         instance_(instance),
0683         process_(process),
0684         index_(index) {}
0685 
0686   bool ProductResolverIndexHelper::Item::operator<(Item const& right) const {
0687     if (kindOfType_ < right.kindOfType_)
0688       return true;
0689     if (kindOfType_ > right.kindOfType_)
0690       return false;
0691     if (typeID_ < right.typeID_)
0692       return true;
0693     if (typeID_ > right.typeID_)
0694       return false;
0695     if (moduleLabel_ < right.moduleLabel_)
0696       return true;
0697     if (moduleLabel_ > right.moduleLabel_)
0698       return false;
0699     if (instance_ < right.instance_)
0700       return true;
0701     if (instance_ > right.instance_)
0702       return false;
0703     return process_ < right.process_;
0704   }
0705 
0706   void ProductResolverIndexHelper::print(std::ostream& os) const {
0707     os << "\n******* Dump ProductResolverIndexHelper *************************\n";
0708 
0709     os << "\nnextIndexValue_ = " << nextIndexValue_ << "\n";
0710     os << "beginElements_ = " << beginElements_ << "\n";
0711 
0712     os << "\n******* sortedTypeIDs_ \n";
0713     for (auto const& i : sortedTypeIDs_) {
0714       os << i << "\n";
0715     }
0716     os << "******* ranges_ \n";
0717     for (auto const& i : ranges_) {
0718       os << i.begin() << " " << i.end() << "\n";
0719     }
0720     os << "******* indexAndNames_ \n";
0721     for (auto const& i : indexAndNames_) {
0722       os << i.index() << " " << i.startInBigNamesContainer() << " ";
0723       char const* ptr = &bigNamesContainer_[i.startInBigNamesContainer()];
0724       while (*ptr) {
0725         os << *ptr;
0726         ++ptr;
0727       }
0728       ++ptr;
0729       os << " ";
0730       while (*ptr) {
0731         os << *ptr;
0732         ++ptr;
0733       }
0734       os << " " << i.startInProcessNames() << " ";
0735       ptr = &processNames_[i.startInProcessNames()];
0736       while (*ptr) {
0737         os << *ptr;
0738         ++ptr;
0739       }
0740       os << "\n";
0741     }
0742     os << "******* bigNamesContainer_ \n";
0743     for (auto i : bigNamesContainer_) {
0744       if (i == '\0')
0745         os << '\\' << '0';
0746       else
0747         os << i;
0748     }
0749     if (!bigNamesContainer_.empty())
0750       os << "\n";
0751     os << "******* processNames_ \n";
0752     for (auto i : processNames_) {
0753       if (i == '\0')
0754         os << '\\' << '0';
0755       else
0756         os << i;
0757     }
0758     if (!processNames_.empty())
0759       os << "\n";
0760     if (items_) {
0761       os << "******* items_ \n";
0762       for (auto const& item : *items_) {
0763         std::cout << item.kindOfType() << " " << item.moduleLabel() << " " << item.instance() << " " << item.process()
0764                   << " " << item.index() << " " << item.typeID() << "\n";
0765       }
0766     }
0767     if (processItems_) {
0768       os << "******* processItems_ \n";
0769       for (auto const& item : *processItems_) {
0770         os << item << "\n";
0771       }
0772     }
0773     os << "sortedTypeIDs_.size() = " << sortedTypeIDs_.size() << "\n";
0774     os << "indexAndNames_.size() = " << indexAndNames_.size() << "\n";
0775     os << "bigNamesContainer_.size() = " << bigNamesContainer_.size() << "\n";
0776     os << "processNames_.size() = " << processNames_.size() << "\n";
0777     os << "\n";
0778   }
0779 }  // namespace edm