CMS 3D CMS Logo

HcalIndexLookup.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalIndexLookup_h
2 #define CondFormats_HcalObjects_HcalIndexLookup_h
3 
4 #include <cstdint>
5 #include <climits>
6 
8 
9 #include "boost/serialization/access.hpp"
10 #include "boost/serialization/version.hpp"
11 #include "boost/serialization/vector.hpp"
12 #include "boost/serialization/utility.hpp"
13 
14 //
15 // Storable lookup of unsigned index values by unsigned key
16 // (raw detId, ieta, etc)
17 //
19 {
20 public:
21  static const unsigned InvalidIndex = UINT_MAX;
22 
23  inline HcalIndexLookup() : sorted_(true) {}
24 
25  // Add an index for lookup. All "transformedId" numbers should be
26  // unique and "index" argument must not be equal InvalidIndex.
27  void add(unsigned transformedId, unsigned index);
28 
29  void sort();
30  void clear();
31  inline void reserve(const unsigned n) {data_.reserve(n);}
32 
33  // After filling up the table, it is recommended to verify that
34  // there are no duplicate ids. The collection will also become
35  // sorted as a side effect of the following function call.
36  bool hasDuplicateIds();
37 
38  // Some trivial inspectors
39  inline std::size_t size() const {return data_.size();}
40  inline bool empty() const {return data_.empty();}
41 
42  // Largest index number. Returns InvalidIndex for empty collection.
43  unsigned largestIndex() const;
44 
45  // "find" returns InvalidIndex in case argument detId
46  // is not in the collection. Note that the object should be
47  // sorted (or even better, checked for duplicate ids) before
48  // performing index lookups.
49  unsigned find(unsigned detId) const;
50 
51  // Comparison is only really useful for testing sorted lookups
52  // (serialized lookups will be sorted)
53  inline bool operator==(const HcalIndexLookup& r) const
54  {return data_ == r.data_ && sorted_ == r.sorted_;}
55 
56  inline bool operator!=(const HcalIndexLookup& r) const
57  {return !(*this == r);}
58 
59 private:
60  std::vector<std::pair<uint32_t,uint32_t> > data_;
61  bool sorted_;
62 
64 
65  template<class Archive>
66  inline void save(Archive & ar, const unsigned /* version */) const
67  {
68  // Make sure that there are no duplicate ids
69  if ((const_cast<HcalIndexLookup*>(this))->hasDuplicateIds())
70  throw cms::Exception("In HcalIndexLookup::save: invalid data");
71  ar & data_ & sorted_;
72  }
73 
74  template<class Archive>
75  inline void load(Archive & ar, const unsigned /* version */)
76  {
77  ar & data_ & sorted_;
78  if (hasDuplicateIds()) throw cms::Exception(
79  "In HcalIndexLookup::load: invalid data");
80  }
81 
82  BOOST_SERIALIZATION_SPLIT_MEMBER()
83 };
84 
85 BOOST_CLASS_VERSION(HcalIndexLookup, 1)
86 
87 #endif // CondFormats_HcalObjects_HcalIndexLookup_h
void load(Archive &ar, const unsigned)
unsigned largestIndex() const
void reserve(const unsigned n)
std::size_t size() const
void add(unsigned transformedId, unsigned index)
friend class boost::serialization::access
bool operator==(const HcalIndexLookup &r) const
void save(Archive &ar, const unsigned) const
std::vector< std::pair< uint32_t, uint32_t > > data_
unsigned find(unsigned detId) const
static const unsigned InvalidIndex
bool empty() const
bool operator!=(const HcalIndexLookup &r) const