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

mem_trie.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efc5a90cc0d65ca724596980d00f3ec95dacbb89 (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#pragma once

#include "base/macros.hpp"
#include "base/stl_add.hpp"

#include <algorithm>
#include <cstdint>
#include <map>
#include <memory>
#include <vector>

namespace my
{
// This class is a simple in-memory trie which allows to add
// key-value pairs and then traverse them in a sorted order.
template <typename String, typename Value>
class MemTrie
{
private:
  struct Node;

public:
  using Char = typename String::value_type;

  MemTrie() = default;
  MemTrie(MemTrie && rhs) { *this = std::move(rhs); }

  MemTrie & operator=(MemTrie && rhs)
  {
    m_root = std::move(rhs.m_root);
    m_numNodes = rhs.m_numNodes;
    rhs.Clear();
    return *this;
  }

  // A read-only iterator wrapping a Node. Any modification to the
  // underlying trie is assumed to invalidate the iterator.
  class Iterator
  {
  public:
    Iterator(MemTrie::Node const & node) : m_node(node) {}

    // Iterates over all possible moves from this Iterator's node
    // and calls |toDo| with two arguments:
    // (Char of the move, Iterator wrapping the node of the move).
    template <typename ToDo>
    void ForEachMove(ToDo && toDo) const
    {
      for (auto const & move : m_node.m_moves)
        toDo(move.first, Iterator(*move.second));
    }

    // Calls |toDo| for every value in this Iterator's node.
    template <typename ToDo>
    void ForEachInNode(ToDo && toDo) const
    {
      std::for_each(m_node.m_values.begin(), m_node.m_values.end(), std::forward<ToDo>(toDo));
    }

  private:
    MemTrie::Node const & m_node;
  };

  // Adds a key-value pair to the trie.
  void Add(String const & key, Value const & value)
  {
    auto * cur = &m_root;
    for (auto const & c : key)
    {
      bool created;
      cur = &cur->GetMove(c, created);
      if (created)
        ++m_numNodes;
    }
    cur->AddValue(value);
  }

  // Traverses all key-value pairs in the trie and calls |toDo| on each of them.
  template <typename ToDo>
  void ForEachInTrie(ToDo && toDo) const
  {
    String prefix;
    ForEachInSubtree(m_root, prefix, std::forward<ToDo>(toDo));
  }

  // Calls |toDo| for each key-value pair in the node that is reachable
  // by |prefix| from the trie root. Does nothing if such node does
  // not exist.
  template <typename ToDo>
  void ForEachInNode(String const & prefix, ToDo && toDo) const
  {
    if (auto const * root = MoveTo(prefix))
      ForEachInNode(*root, prefix, std::forward<ToDo>(toDo));
  }

  // Calls |toDo| for each key-value pair in a subtree that is
  // reachable by |prefix| from the trie root. Does nothing if such
  // subtree does not exist.
  template <typename ToDo>
  void ForEachInSubtree(String prefix, ToDo && toDo) const
  {
    if (auto const * root = MoveTo(prefix))
      ForEachInSubtree(*root, prefix, std::forward<ToDo>(toDo));
  }

  void Clear()
  {
    m_root.Clear();
    m_numNodes = 1;
  }

  size_t GetNumNodes() const { return m_numNodes; }
  Iterator GetRootIterator() const { return Iterator(m_root); }
  Node const & GetRoot() const { return m_root; }

private:
  struct Node
  {
    friend class MemTrie<String, Value>::Iterator;

    Node() = default;
    Node(Node && /* rhs */) = default;

    Node & operator=(Node && /* rhs */) = default;

    Node & GetMove(Char const & c, bool & created)
    {
      auto & node = m_moves[c];
      if (!node)
      {
        node = my::make_unique<Node>();
        created = true;
      }
      else
      {
        created = false;
      }
      return *node;
    }

    void AddValue(Value const & value) { m_values.push_back(value); }

    void Clear()
    {
      m_moves.clear();
      m_values.clear();
    }

    std::map<Char, std::unique_ptr<Node>> m_moves;
    std::vector<Value> m_values;

    DISALLOW_COPY(Node);
  };

  Node const * MoveTo(String const & key) const
  {
    auto const * cur = &m_root;
    for (auto const & c : key)
    {
      auto const it = cur->m_moves.find(c);
      if (it == cur->m_moves.end())
        return nullptr;
      cur = it->second.get();
    }
    return cur;
  }

  // Calls |toDo| for each key-value pair in a |node| that is
  // reachable by |prefix| from the trie root.
  template <typename ToDo>
  void ForEachInNode(Node const & node, String const & prefix, ToDo && toDo) const
  {
    for (auto const & value : node.m_values)
      toDo(prefix, value);
  }

  // Calls |toDo| for each key-value pair in subtree where |node| is a
  // root of the subtree. |prefix| is a path from the trie root to the
  // |node|.
  template <typename ToDo>
  void ForEachInSubtree(Node const & node, String & prefix, ToDo && toDo) const
  {
    ForEachInNode(node, prefix, toDo);

    for (auto const & move : node.m_moves)
    {
      prefix.push_back(move.first);
      ForEachInSubtree(*move.second, prefix, toDo);
      prefix.pop_back();
    }
  }

  Node m_root;
  size_t m_numNodes = 1;

  DISALLOW_COPY(MemTrie);
};
}  // namespace my