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