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

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

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

#include "std/unique_ptr.hpp"

namespace trie
{
using TrieChar = uint32_t;

// 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.
uint32_t constexpr kDefaultChar = 0;

template <typename TValueList>
class Iterator
{

public:
  using TValue = typename TValueList::TValue;

  struct Edge
  {
    using TEdgeLabel = buffer_vector<TrieChar, 8>;
    TEdgeLabel m_label;
  };

  buffer_vector<Edge, 8> m_edge;
  TValueList m_valueList;

  virtual ~Iterator() = default;

  virtual unique_ptr<Iterator<TValueList>> Clone() const = 0;
  virtual unique_ptr<Iterator<TValueList>> GoToEdge(size_t i) const = 0;
};

template <typename TValueList, typename TF, typename TString>
void ForEachRef(Iterator<TValueList> const & it, TF && f, TString const & s)
{
  it.m_valueList.ForEach([&f, &s](typename TValueList::TValue const & value)
                         {
                           f(s, value);
                         });
  for (size_t i = 0; i < it.m_edge.size(); ++i)
  {
    TString s1(s);
    s1.insert(s1.end(), it.m_edge[i].m_label.begin(), it.m_edge[i].m_label.end());
    auto nextIt = it.GoToEdge(i);
    ForEachRef(*nextIt, f, s1);
  }
}
}  // namespace trie