CMS 3D CMS Logo

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