CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HcalIndexLookup.h
Go to the documentation of this file.
1 #ifndef CondFormats_HcalObjects_HcalIndexLookup_h
2 #define CondFormats_HcalObjects_HcalIndexLookup_h
3 
5 
6 #include "boost/serialization/access.hpp"
7 #include "boost/serialization/version.hpp"
8 #include "boost/serialization/vector.hpp"
9 #include "boost/serialization/utility.hpp"
10 
11 #include <cstdint>
12 #include <climits>
13 #include <vector>
14 
15 //
16 // Storable lookup of unsigned index values by unsigned key
17 // (raw detId, ieta, etc)
18 //
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 { return data_ == r.data_ && sorted_ == r.sorted_; }
54 
55  inline bool operator!=(const HcalIndexLookup& r) const { return !(*this == r); }
56 
57 private:
58  std::vector<std::pair<uint32_t, uint32_t> > data_;
59  bool sorted_;
60 
62 
63  template <class Archive>
64  inline void save(Archive& ar, const unsigned /* version */) const {
65  // Make sure that there are no duplicate ids
66  if ((const_cast<HcalIndexLookup*>(this))->hasDuplicateIds())
67  throw cms::Exception("In HcalIndexLookup::save: invalid data");
68  ar& data_& sorted_;
69  }
70 
71  template <class Archive>
72  inline void load(Archive& ar, const unsigned /* version */) {
73  ar& data_& sorted_;
74  if (hasDuplicateIds())
75  throw cms::Exception("In HcalIndexLookup::load: invalid data");
76  }
77 
78  BOOST_SERIALIZATION_SPLIT_MEMBER()
79 };
80 
81 BOOST_CLASS_VERSION(HcalIndexLookup, 1)
82 
83 #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