Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:54

0001 #include "CondFormats/Common/interface/SmallWORMDict.h"
0002 #include <string>
0003 #include <functional>
0004 #include <numeric>
0005 
0006 namespace cond {
0007   using namespace std::placeholders;
0008 
0009   SmallWORMDict::SmallWORMDict() {}
0010   SmallWORMDict::~SmallWORMDict() {}
0011 
0012   SmallWORMDict::SmallWORMDict(std::vector<std::string> const& idict)
0013       : m_data(std::accumulate(idict.begin(), idict.end(), 0, [](int a, std::string b) { return a + b.size(); })),
0014         m_index(idict.size(), 1) {
0015     // sort (use index)
0016     m_index[0] = 0;
0017     std::partial_sum(m_index.begin(), m_index.end(), m_index.begin());
0018     std::sort(m_index.begin(), m_index.end(), [&idict](unsigned int a, unsigned int b) {
0019       return std::less<std::string>()(idict[a], idict[b]);
0020     });
0021 
0022     //copy
0023     std::vector<char>::iterator p = m_data.begin();
0024     for (size_t j = 0; j < m_index.size(); j++) {
0025       size_t i = m_index[j];
0026       p = std::copy(idict[i].begin(), idict[i].end(), p);
0027       m_index[j] = p - m_data.begin();
0028     }
0029   }
0030 
0031   struct LessFrame {
0032     bool operator()(SmallWORMDict::Frame const& rh, SmallWORMDict::Frame const& lh) const {
0033       return std::lexicographical_compare(rh.b, rh.b + rh.l, lh.b, lh.b + lh.l);
0034     }
0035   };
0036 
0037   size_t SmallWORMDict::index(std::string const& s) const { return (*find(s)).ind; }
0038 
0039   size_t SmallWORMDict::index(char const* s) const { return (*find(s)).ind; }
0040 
0041   SmallWORMDict::const_iterator SmallWORMDict::find(std::string const& s) const {
0042     Frame sp(&s[0], s.size(), 0);
0043     return std::lower_bound(begin(), end(), sp, LessFrame());
0044   }
0045 
0046   SmallWORMDict::const_iterator SmallWORMDict::find(char const* s) const {
0047     Frame sp(s, ::strlen(s), 0);
0048     return std::lower_bound(begin(), end(), sp, LessFrame());
0049   }
0050 
0051   size_t SmallWORMDict::size() const { return m_index.size(); }
0052 
0053 }  // namespace cond