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

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

#include "coding/bit_streams.hpp"
#include "coding/byte_stream.hpp"
#include "coding/huffman.hpp"
#include "coding/reader.hpp"
#include "coding/varint.hpp"
#include "coding/writer.hpp"

#include "base/buffer_vector.hpp"
#include "base/checked_cast.hpp"
#include "base/scope_guard.hpp"
#include "base/string_utils.hpp"

#include "std/algorithm.hpp"
#include "std/bind.hpp"

// Trie format:
//   -- Serialized Huffman encoding.
//   -- Topology of the trie built on Huffman-encoded input strings [2 bits per node, level-order representation].
//   -- List of pairs (node id, offset). One pair per final node (i.e. a node where a string ends).
//      The lists of node ids and offsets are both non-decreasing and are delta-encoded with
//      varuints.
//   -- Values of final nodes in level-order. The values for final node |id| start at offset |offset|
//      if there is a pair (id, offset) in the list above.

namespace trie
{
// Node is a temporary struct that is used to store the trie in memory
// before it is converted to a succinct representation and put to disk.
// It is rather verbose but hopefully is small enough to fit in memory.
template <class TValueList>
struct Node
{
  // The left child is reached by 0, the right by 1.
  Node *l, *r;

  // A node is final if a key string ends in it.
  // In particular, all non-final nodes' valueLists are empty
  // and it is expected that a final node's valueList is non-empty.
  bool m_isFinal;

  // m_valueLists stores all the additional information that is related
  // to the key string which leads to this node.
  TValueList m_valueList;

  Node() : l(nullptr), r(nullptr), m_isFinal(false) {}
};

template <class TNode, class TReader>
TNode * AddToTrie(TNode * root, TReader & bitEncoding, uint32_t numBits)
{
  ReaderSource<TReader> src(bitEncoding);
  // String size.
  ReadVarUint<uint32_t, ReaderSource<TReader>>(src);

  BitReader<ReaderSource<TReader>> bitReader(src);
  auto cur = root;
  for (uint32_t i = 0; i < numBits; ++i)
  {
    auto nxt = bitReader.Read(1) == 0 ? &cur->l : &cur->r;
    if (!*nxt)
      *nxt = new TNode();
    cur = *nxt;
  }
  return cur;
}

template <class TNode>
void DeleteTrie(TNode * root)
{
  if (!root)
    return;
  DeleteTrie(root->l);
  DeleteTrie(root->r);
  delete root;
}

template <typename TNode>
void WriteInLevelOrder(TNode * root, vector<TNode *> & levelOrder)
{
  ASSERT(root, ());
  levelOrder.push_back(root);
  size_t i = 0;
  while (i < levelOrder.size())
  {
    TNode * cur = levelOrder[i++];
    if (cur->l)
      levelOrder.push_back(cur->l);
    if (cur->r)
      levelOrder.push_back(cur->r);
  }
}

template <typename TWriter, typename TIter, typename TValueList>
void BuildSuccinctTrie(TWriter & writer, TIter const beg, TIter const end)
{
  using TrieChar = uint32_t;
  using TTrieString = buffer_vector<TrieChar, 32>;
  using TNode = Node<TValueList>;
  using TEntry = typename TIter::value_type;

  TNode * root = new TNode();
  MY_SCOPE_GUARD(cleanup, bind(&DeleteTrie<TNode>, root));
  TTrieString prevKey;
  TEntry prevEntry;

  vector<TEntry> entries;
  vector<strings::UniString> entryStrings;
  for (TIter it = beg; it != end; ++it)
  {
    TEntry entry = *it;
    if (it != beg && entry == prevEntry)
      continue;
    TrieChar const * const keyData = entry.GetKeyData();
    TTrieString key(keyData, keyData + entry.GetKeySize());
    using namespace std::rel_ops;  // ">=" for keys.
    CHECK_GREATER_OR_EQUAL(key, prevKey, (key, prevKey));
    entries.push_back(entry);
    entryStrings.push_back(strings::UniString(keyData, keyData + entry.GetKeySize()));
    prevKey.swap(key);
    prevEntry.Swap(entry);
  }

  coding::HuffmanCoder huffman;
  huffman.Init(entryStrings);
  huffman.WriteEncoding(writer);

  for (size_t i = 0; i < entries.size(); ++i)
  {
    TEntry const & entry = entries[i];
    auto const & key = entryStrings[i];

    vector<uint8_t> buf;
    MemWriter<vector<uint8_t>> memWriter(buf);
    uint32_t numBits = huffman.EncodeAndWrite(memWriter, key);

    MemReader bitEncoding(&buf[0], buf.size());

    TNode * cur = AddToTrie(root, bitEncoding, numBits);
    cur->m_isFinal = true;
    cur->m_valueList.Append(entry.GetValue());
  }

  vector<TNode *> levelOrder;
  WriteInLevelOrder(root, levelOrder);

  {
    // Trie topology.
    BitWriter<TWriter> bitWriter(writer);
    WriteVarUint(writer, levelOrder.size());
    for (size_t i = 0; i < levelOrder.size(); ++i)
    {
      auto const & cur = levelOrder[i];
      bitWriter.Write(cur->l ? 1 : 0, 1 /* numBits */);
      bitWriter.Write(cur->r ? 1 : 0, 1 /* numBits */);
    }
  }

  vector<uint32_t> finalNodeIds;
  vector<uint32_t> offsetTable;
  vector<uint8_t> valueBuf;
  MemWriter<vector<uint8_t>> valueWriter(valueBuf);
  for (size_t i = 0; i < levelOrder.size(); ++i)
  {
    TNode const * node = levelOrder[i];
    if (!node->m_isFinal)
      continue;
    finalNodeIds.push_back(static_cast<uint32_t>(i));
    offsetTable.push_back(base::asserted_cast<uint32_t>(valueWriter.Pos()));
    node->m_valueList.Dump(valueWriter);
  }

  WriteVarUint(writer, finalNodeIds.size());
  for (size_t i = 0; i < finalNodeIds.size(); ++i)
  {
    if (i == 0)
    {
      WriteVarUint(writer, finalNodeIds[i]);
      WriteVarUint(writer, offsetTable[i]);
    }
    else
    {
      WriteVarUint(writer, finalNodeIds[i] - finalNodeIds[i - 1]);
      WriteVarUint(writer, offsetTable[i] - offsetTable[i - 1]);
    }
  }

  writer.Write(valueBuf.data(), valueBuf.size());

  // todo(@pimenov):
  // 1. Investigate the possibility of path compression (short edges + lcp table).
  // 2. It is highly probable that valueList will be only a list of feature ids in our
  //    future implementations of the search.
  //    We can then dispose of offsetTable and store finalNodeIds as another bit vector.
  //    Alternatively (and probably better), we can append a special symbol to all those
  //    strings that have non-empty value lists. We can then get the leaf number
  //    of a final node by doing rank0.
}

}  // namespace trie