Welcome to mirror list, hosted at ThFree Co, Russian Federation.

trie.hpp « coding - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9e689f0c25f2d2265a987b521a57400780a31958 (plain)
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
#pragma once

#include "../base/base.hpp"
#include "../base/assert.hpp"
#include "../base/buffer_vector.hpp"
//#include "../base/object_tracker.hpp"

#include "../std/scoped_ptr.hpp"


namespace trie
{

typedef uint32_t TrieChar;

// 95 is a good value for the default baseChar, since both small and capital latin letters
// are less than +/- 32 from it and thus can fit into supershort edge.
// However 0 is used because the first byte is actually language id.
static uint32_t const DEFAULT_CHAR = 0;

template <typename ValueT, typename EdgeValueT>
class Iterator
{
  //dbg::ObjectTracker m_tracker;

public:
  struct Edge
  {
    typedef buffer_vector<TrieChar, 8> EdgeStrT;
    EdgeStrT m_str;
    EdgeValueT m_value;
  };

  buffer_vector<Edge, 8> m_edge;
  buffer_vector<ValueT, 2> m_value;

  virtual ~Iterator() {}

  virtual Iterator<ValueT, EdgeValueT> * Clone() const = 0;
  virtual Iterator<ValueT, EdgeValueT> * GoToEdge(uint32_t i) const = 0;
};

namespace reader
{

struct EmptyValueReader
{
  typedef unsigned char ValueType;

  template <typename SourceT>
  void operator() (SourceT &, ValueType & value) const
  {
    value = 0;
  }
};

template <unsigned int N>
struct FixedSizeValueReader
{
  struct ValueType
  {
    unsigned char m_data[N];
  };

  template <typename SourceT>
  void operator() (SourceT & src, ValueType & value) const
  {
    src.Read(&value.m_data[0], N);
  }
};

}  // namespace trie::reader

template <typename ValueT, typename EdgeValueT, typename F, typename StringT>
void ForEachRef(Iterator<ValueT, EdgeValueT> const & iter, F & f, StringT const & s)
{
  for (size_t i = 0; i < iter.m_value.size(); ++i)
    f(s, iter.m_value[i]);
  for (size_t i = 0; i < iter.m_edge.size(); ++i)
  {
    StringT s1(s);
    s1.insert(s1.end(), iter.m_edge[i].m_str.begin(), iter.m_edge[i].m_str.end());
    scoped_ptr<Iterator<ValueT, EdgeValueT> > pIter1(iter.GoToEdge(i));
    ForEachRef(*pIter1, f, s1);
  }
}

}  // namespace Trie