Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 10:46:37

0001 #ifndef cond_SmallWORMDict_h
0002 #define cond_SmallWORMDict_h
0003 
0004 #include "CondFormats/Serialization/interface/Serializable.h"
0005 
0006 #include <vector>
0007 #include <string>
0008 #include <boost/iterator_adaptors.hpp>
0009 #include <boost/iterator/transform_iterator.hpp>
0010 #include <boost/iterator/counting_iterator.hpp>
0011 
0012 // Function for testing SmallWORMDict
0013 namespace test {
0014   namespace SmallWORMDict {
0015     int test();
0016   }
0017 }  // namespace test
0018 
0019 namespace cond {
0020 
0021   /** A small WORM Dictionary of small words
0022     optimized to do a single allocation
0023  */
0024 
0025   class SmallWORMDict {
0026     friend int test::SmallWORMDict::test();
0027 
0028   public:
0029     SmallWORMDict();
0030     ~SmallWORMDict();
0031 
0032     struct Frame {
0033       Frame() : b(nullptr) {}
0034       Frame(char const* ib, unsigned int il, unsigned int iind) : b(ib), l(il), ind(iind) {}
0035       char const* b;
0036       unsigned int l;
0037       unsigned int ind;
0038     };
0039 
0040     struct IterHelp {
0041       typedef Frame result_type;
0042       IterHelp() : v(nullptr) {}
0043       IterHelp(SmallWORMDict const& iv) : v(&iv) {}
0044 
0045       result_type const& operator()(int i) const {
0046         int k = (0 == i) ? 0 : v->m_index[i - 1];
0047         return frame(&v->m_data[k], v->m_index[i] - k, i);
0048       }
0049 
0050       Frame const& frame(char const* b, unsigned int l, unsigned int ind) const {
0051         f.b = b;
0052         f.l = l;
0053         f.ind = ind;
0054         return f;
0055       }
0056 
0057     private:
0058       SmallWORMDict const* v;
0059       mutable Frame f;
0060     };
0061 
0062     friend struct IterHelp;
0063 
0064     typedef boost::transform_iterator<IterHelp, boost::counting_iterator<int> > const_iterator;
0065 
0066     const_iterator begin() const {
0067       return boost::make_transform_iterator(boost::counting_iterator<int>(0), IterHelp(*this));
0068     }
0069 
0070     const_iterator end() const {
0071       return boost::make_transform_iterator(boost::counting_iterator<int>(size()), IterHelp(*this));
0072     }
0073 
0074     Frame operator[](int i) const {
0075       int k = (0 == i) ? 0 : m_index[i - 1];
0076       return Frame(&m_data[k], m_index[i] - k, i);
0077     }
0078 
0079     const_iterator find(std::string const& s) const;
0080 
0081     const_iterator find(char const* s) const;
0082 
0083     // constructror from config
0084     explicit SmallWORMDict(std::vector<std::string> const& idict);
0085 
0086     // find location of a word
0087     size_t index(std::string const& s) const;
0088 
0089     size_t index(char const* s) const;
0090 
0091     size_t size() const;
0092 
0093   private:
0094     std::vector<char> m_data;
0095     std::vector<unsigned int> m_index;
0096 
0097     COND_SERIALIZABLE;
0098   };
0099 
0100 }  // namespace cond
0101 
0102 #endif