CMS 3D CMS Logo

HcalIndexLookup.cc
Go to the documentation of this file.
1 #include <algorithm>
2 
4 
6  data_.clear();
7  sorted_ = true;
8 }
9 
11  if (!sorted_) {
12  std::sort(data_.begin(), data_.end());
13  sorted_ = true;
14  }
15 }
16 
18  const std::size_t sz = data_.size();
19  if (sz) {
20  sort();
21  const std::size_t szm1 = sz - 1;
22  for (std::size_t i = 0; i < szm1; ++i)
23  if (data_[i].first == data_[i + 1].first)
24  return true;
25  }
26  return false;
27 }
28 
29 void HcalIndexLookup::add(const unsigned detId, const unsigned index) {
30  if (index == InvalidIndex)
31  throw cms::Exception("In HcalIndexLookup::add: invalid index");
32  data_.push_back(std::pair<uint32_t, uint32_t>(detId, index));
33  sorted_ = false;
34 }
35 
36 unsigned HcalIndexLookup::find(const unsigned detId) const {
37  if (data_.empty())
38  return InvalidIndex;
39  if (!sorted_)
40  throw cms::Exception(
41  "In HcalIndexLookup::lookup:"
42  " collection is not sorted");
43  std::pair<uint32_t, uint32_t> search(detId, 0U);
44  auto end = data_.end();
45  auto it = std::lower_bound(data_.begin(), end, search);
46  if (it == end)
47  return InvalidIndex;
48  if (it->first == detId)
49  return it->second;
50  else
51  return InvalidIndex;
52 }
53 
55  const std::size_t sz = data_.size();
56  if (sz) {
57  uint32_t largest = 0;
58  for (std::size_t i = 0; i < sz; ++i)
59  if (data_[i].second > largest)
60  largest = data_[i].second;
61  return largest;
62  } else
63  return InvalidIndex;
64 }
unsigned largestIndex() const
std::vector< T >::const_iterator search(const cond::Time_t &val, const std::vector< T > &container)
Definition: IOVProxy.cc:21
std::vector< std::pair< uint32_t, uint32_t > > data_
U second(std::pair< T, U > const &p)
void add(unsigned transformedId, unsigned index)
unsigned find(unsigned detId) const
static const unsigned InvalidIndex