Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
#include <algorithm>

#include "CondFormats/HcalObjects/interface/HcalIndexLookup.h"

void HcalIndexLookup::clear() {
  data_.clear();
  sorted_ = true;
}

void HcalIndexLookup::sort() {
  if (!sorted_) {
    std::sort(data_.begin(), data_.end());
    sorted_ = true;
  }
}

bool HcalIndexLookup::hasDuplicateIds() {
  const std::size_t sz = data_.size();
  if (sz) {
    sort();
    const std::size_t szm1 = sz - 1;
    for (std::size_t i = 0; i < szm1; ++i)
      if (data_[i].first == data_[i + 1].first)
        return true;
  }
  return false;
}

void HcalIndexLookup::add(const unsigned detId, const unsigned index) {
  if (index == InvalidIndex)
    throw cms::Exception("In HcalIndexLookup::add: invalid index");
  data_.push_back(std::pair<uint32_t, uint32_t>(detId, index));
  sorted_ = false;
}

unsigned HcalIndexLookup::find(const unsigned detId) const {
  if (data_.empty())
    return InvalidIndex;
  if (!sorted_)
    throw cms::Exception(
        "In HcalIndexLookup::lookup:"
        " collection is not sorted");
  std::pair<uint32_t, uint32_t> search(detId, 0U);
  auto end = data_.end();
  auto it = std::lower_bound(data_.begin(), end, search);
  if (it == end)
    return InvalidIndex;
  if (it->first == detId)
    return it->second;
  else
    return InvalidIndex;
}

unsigned HcalIndexLookup::largestIndex() const {
  const std::size_t sz = data_.size();
  if (sz) {
    uint32_t largest = 0;
    for (std::size_t i = 0; i < sz; ++i)
      if (data_[i].second > largest)
        largest = data_[i].second;
    return largest;
  } else
    return InvalidIndex;
}