File indexing completed on 2024-04-06 12:02:15
0001 #include <algorithm>
0002
0003 #include "CondFormats/HcalObjects/interface/HcalIndexLookup.h"
0004
0005 void HcalIndexLookup::clear() {
0006 data_.clear();
0007 sorted_ = true;
0008 }
0009
0010 void HcalIndexLookup::sort() {
0011 if (!sorted_) {
0012 std::sort(data_.begin(), data_.end());
0013 sorted_ = true;
0014 }
0015 }
0016
0017 bool HcalIndexLookup::hasDuplicateIds() {
0018 const std::size_t sz = data_.size();
0019 if (sz) {
0020 sort();
0021 const std::size_t szm1 = sz - 1;
0022 for (std::size_t i = 0; i < szm1; ++i)
0023 if (data_[i].first == data_[i + 1].first)
0024 return true;
0025 }
0026 return false;
0027 }
0028
0029 void HcalIndexLookup::add(const unsigned detId, const unsigned index) {
0030 if (index == InvalidIndex)
0031 throw cms::Exception("In HcalIndexLookup::add: invalid index");
0032 data_.push_back(std::pair<uint32_t, uint32_t>(detId, index));
0033 sorted_ = false;
0034 }
0035
0036 unsigned HcalIndexLookup::find(const unsigned detId) const {
0037 if (data_.empty())
0038 return InvalidIndex;
0039 if (!sorted_)
0040 throw cms::Exception(
0041 "In HcalIndexLookup::lookup:"
0042 " collection is not sorted");
0043 std::pair<uint32_t, uint32_t> search(detId, 0U);
0044 auto end = data_.end();
0045 auto it = std::lower_bound(data_.begin(), end, search);
0046 if (it == end)
0047 return InvalidIndex;
0048 if (it->first == detId)
0049 return it->second;
0050 else
0051 return InvalidIndex;
0052 }
0053
0054 unsigned HcalIndexLookup::largestIndex() const {
0055 const std::size_t sz = data_.size();
0056 if (sz) {
0057 uint32_t largest = 0;
0058 for (std::size_t i = 0; i < sz; ++i)
0059 if (data_[i].second > largest)
0060 largest = data_[i].second;
0061 return largest;
0062 } else
0063 return InvalidIndex;
0064 }