Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:20

0001 #include <algorithm>
0002 
0003 #include "Alignment/Geners/interface/AbsArchive.hh"
0004 #include "Alignment/Geners/interface/AbsReference.hh"
0005 #include "Alignment/Geners/interface/CatalogEntry.hh"
0006 #include "Alignment/Geners/interface/IOException.hh"
0007 
0008 // Notes on implementation
0009 //
0010 // The following invariants should be preserved:
0011 //
0012 // 0 items:    must have itemId_ == 0 && idList_.empty()
0013 // 1 item:     must have itemId_ != 0 && idList_.empty()
0014 // more items: must have itemId_ == 0 && !idList_.empty()
0015 //
0016 namespace gs {
0017   std::istream &AbsReference::positionInputStream(const unsigned long long id) const {
0018     return archive_.inputStream(id, nullptr);
0019   }
0020 
0021   void AbsReference::initialize() const {
0022     AbsReference *modifiable = const_cast<AbsReference *>(this);
0023     if (searchId_) {
0024       // Make sure that the searchId_ item id is going to produce
0025       // a valid item
0026       std::shared_ptr<const CatalogEntry> record = archive_.catalogEntry(searchId_);
0027       const unsigned long long idFound = record->id();
0028 
0029       // Check for valid id in the archive
0030       if (idFound && this->isIOCompatible(*record)) {
0031         if (idFound != searchId_)
0032           throw IOInvalidData("In AbsReference::initialize: catalog is corrupted");
0033         modifiable->itemId_ = searchId_;
0034       }
0035     } else {
0036       // Search for matching items in the archive
0037       archive_.search(*modifiable);
0038       if (!idList_.empty()) {
0039         // Check if the list is sorted.
0040         // Sort if it is not.
0041         const unsigned long nFound = idList_.size();
0042         const unsigned long long *ids = &idList_[0];
0043         bool sorted = true;
0044         for (unsigned long i = 1; i < nFound; ++i)
0045           if (ids[i - 1] >= ids[i]) {
0046             sorted = false;
0047             break;
0048           }
0049         if (!sorted)
0050           std::sort(modifiable->idList_.begin(), modifiable->idList_.end());
0051       }
0052     }
0053     modifiable->initialized_ = true;
0054   }
0055 
0056   bool AbsReference::isIOCompatible(const CatalogEntry &r) const {
0057     return !classId_.name().empty() && classId_.name() == r.type().name() && ioProto_ == r.ioPrototype();
0058   }
0059 
0060   bool AbsReference::empty() const {
0061     if (!initialized_)
0062       initialize();
0063     return itemId_ == 0 && idList_.empty();
0064   }
0065 
0066   bool AbsReference::unique() const {
0067     if (!initialized_)
0068       initialize();
0069     return itemId_ && idList_.empty();
0070   }
0071 
0072   unsigned long AbsReference::size() const {
0073     if (!initialized_)
0074       initialize();
0075     return itemId_ ? 1UL : idList_.size();
0076   }
0077 
0078   unsigned long long AbsReference::id(const unsigned long index) const {
0079     if (!initialized_)
0080       initialize();
0081     if (itemId_ && index == 0UL)
0082       return itemId_;
0083     else if (index < idList_.size())
0084       return idList_[index];
0085     else
0086       throw gs::IOOutOfRange(
0087           "In gs::AbsReference::id: "
0088           "index out of range");
0089   }
0090 
0091   std::shared_ptr<const CatalogEntry> AbsReference::indexedCatalogEntry(const unsigned long index) const {
0092     return archive_.catalogEntry(id(index));
0093   }
0094 
0095   bool AbsReference::isSameIOPrototype(const CatalogEntry &r) const { return ioProto_ == r.ioPrototype(); }
0096 
0097   void AbsReference::addItemId(const unsigned long long idIn) {
0098     if (!idIn)
0099       throw gs::IOInvalidArgument("In AbsReference::addItemId: invalid item id");
0100     const unsigned long mySize = itemId_ ? 1UL : idList_.size();
0101     switch (mySize) {
0102       case 0UL:
0103         itemId_ = idIn;
0104         break;
0105 
0106       case 1UL:
0107         idList_.reserve(2);
0108         idList_.push_back(itemId_);
0109         idList_.push_back(idIn);
0110         itemId_ = 0ULL;
0111         break;
0112 
0113       default:
0114         idList_.push_back(idIn);
0115         break;
0116     }
0117   }
0118 
0119   AbsReference::AbsReference(AbsArchive &ar,
0120                              const ClassId &classId,
0121                              const char *ioPrototype,
0122                              const unsigned long long itemId)
0123       : archive_(ar),
0124         classId_(classId),
0125         ioProto_(ioPrototype ? ioPrototype : ""),
0126         searchId_(itemId),
0127         namePattern_(nullptr),
0128         categoryPattern_(nullptr),
0129         itemId_(0),
0130         initialized_(false) {
0131     if (!itemId)
0132       throw gs::IOInvalidArgument("In AbsReference constructor: invalid item id");
0133   }
0134 
0135   AbsReference::AbsReference(AbsArchive &ar,
0136                              const ClassId &classId,
0137                              const char *ioPrototype,
0138                              const SearchSpecifier &namePattern,
0139                              const SearchSpecifier &categoryPattern)
0140       : archive_(ar),
0141         classId_(classId),
0142         ioProto_(ioPrototype ? ioPrototype : ""),
0143         searchId_(0),
0144         namePattern_(namePattern),
0145         categoryPattern_(categoryPattern),
0146         itemId_(0),
0147         initialized_(false) {}
0148 }  // namespace gs