Frame

IterHelp

SmallWORMDict

Macros

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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifndef cond_SmallWORMDict_h
#define cond_SmallWORMDict_h

#include "CondFormats/Serialization/interface/Serializable.h"

#include <vector>
#include <string>
#include <boost/iterator_adaptors.hpp>
#include <boost/iterator/transform_iterator.hpp>
#include <boost/iterator/counting_iterator.hpp>

// Function for testing SmallWORMDict
namespace test {
  namespace SmallWORMDict {
    int test();
  }
}  // namespace test

namespace cond {

  /** A small WORM Dictionary of small words
    optimized to do a single allocation
 */

  class SmallWORMDict {
    friend int test::SmallWORMDict::test();

  public:
    SmallWORMDict() = default;
    ~SmallWORMDict() = default;

    struct Frame {
      Frame() : b(nullptr) {}
      Frame(char const* ib, unsigned int il, unsigned int iind) : b(ib), l(il), ind(iind) {}
      char const* b;
      unsigned int l;
      unsigned int ind;
    };

    struct IterHelp {
      typedef Frame result_type;
      IterHelp() : v(nullptr) {}
      IterHelp(SmallWORMDict const& iv) : v(&iv) {}

      result_type const& operator()(int i) const {
        int k = (0 == i) ? 0 : v->m_index[i - 1];
        return frame(&v->m_data[k], v->m_index[i] - k, i);
      }

      Frame const& frame(char const* b, unsigned int l, unsigned int ind) const {
        f.b = b;
        f.l = l;
        f.ind = ind;
        return f;
      }

    private:
      SmallWORMDict const* v;
      mutable Frame f;
    };

    friend struct IterHelp;

    typedef boost::transform_iterator<IterHelp, boost::counting_iterator<int> > const_iterator;

    const_iterator begin() const {
      return boost::make_transform_iterator(boost::counting_iterator<int>(0), IterHelp(*this));
    }

    const_iterator end() const {
      return boost::make_transform_iterator(boost::counting_iterator<int>(size()), IterHelp(*this));
    }

    Frame operator[](int i) const {
      int k = (0 == i) ? 0 : m_index[i - 1];
      return Frame(&m_data[k], m_index[i] - k, i);
    }

    const_iterator find(std::string const& s) const;

    const_iterator find(char const* s) const;

    // constructror from config
    explicit SmallWORMDict(std::vector<std::string> const& idict);

    // find location of a word
    size_t index(std::string const& s) const;

    size_t index(char const* s) const;

    size_t size() const;

  private:
    std::vector<char> m_data;
    std::vector<unsigned int> m_index;

    COND_SERIALIZABLE;
  };

}  // namespace cond

#endif