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