Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:51:08

0001 #ifndef CondFormats_HcalObjects_HcalIndexLookup_h
0002 #define CondFormats_HcalObjects_HcalIndexLookup_h
0003 
0004 #include "FWCore/Utilities/interface/Exception.h"
0005 
0006 #include "boost/serialization/access.hpp"
0007 #include "boost/serialization/version.hpp"
0008 #include "boost/serialization/vector.hpp"
0009 #include "boost/serialization/utility.hpp"
0010 
0011 #include <cstdint>
0012 #include <climits>
0013 #include <vector>
0014 
0015 //
0016 // Storable lookup of unsigned index values by unsigned key
0017 // (raw detId, ieta, etc)
0018 //
0019 class HcalIndexLookup {
0020 public:
0021   static const unsigned InvalidIndex = UINT_MAX;
0022 
0023   inline HcalIndexLookup() : sorted_(true) {}
0024 
0025   // Add an index for lookup. All "transformedId" numbers should be
0026   // unique and "index" argument must not be equal InvalidIndex.
0027   void add(unsigned transformedId, unsigned index);
0028 
0029   void sort();
0030   void clear();
0031   inline void reserve(const unsigned n) { data_.reserve(n); }
0032 
0033   // After filling up the table, it is recommended to verify that
0034   // there are no duplicate ids. The collection will also become
0035   // sorted as a side effect of the following function call.
0036   bool hasDuplicateIds();
0037 
0038   // Some trivial inspectors
0039   inline std::size_t size() const { return data_.size(); }
0040   inline bool empty() const { return data_.empty(); }
0041 
0042   // Largest index number. Returns InvalidIndex for empty collection.
0043   unsigned largestIndex() const;
0044 
0045   // "find" returns InvalidIndex in case argument detId
0046   // is not in the collection. Note that the object should be
0047   // sorted (or even better, checked for duplicate ids) before
0048   // performing index lookups.
0049   unsigned find(unsigned detId) const;
0050 
0051   // Comparison is only really useful for testing sorted lookups
0052   // (serialized lookups will be sorted)
0053   inline bool operator==(const HcalIndexLookup& r) const { return data_ == r.data_ && sorted_ == r.sorted_; }
0054 
0055   inline bool operator!=(const HcalIndexLookup& r) const { return !(*this == r); }
0056 
0057 private:
0058   std::vector<std::pair<uint32_t, uint32_t> > data_;
0059   bool sorted_;
0060 
0061   friend class boost::serialization::access;
0062 
0063   template <class Archive>
0064   inline void save(Archive& ar, const unsigned /* version */) const {
0065     // Make sure that there are no duplicate ids
0066     if ((const_cast<HcalIndexLookup*>(this))->hasDuplicateIds())
0067       throw cms::Exception("In HcalIndexLookup::save: invalid data");
0068     ar& data_& sorted_;
0069   }
0070 
0071   template <class Archive>
0072   inline void load(Archive& ar, const unsigned /* version */) {
0073     ar& data_& sorted_;
0074     if (hasDuplicateIds())
0075       throw cms::Exception("In HcalIndexLookup::load: invalid data");
0076   }
0077 
0078   BOOST_SERIALIZATION_SPLIT_MEMBER()
0079 };
0080 
0081 BOOST_CLASS_VERSION(HcalIndexLookup, 1)
0082 
0083 #endif  // CondFormats_HcalObjects_HcalIndexLookup_h