-
Notifications
You must be signed in to change notification settings - Fork 8
/
string_table.h
59 lines (45 loc) · 1.21 KB
/
string_table.h
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
//
// Copyright RIME Developers
// Distributed under the BSD License
//
// 2014-06-25 GONG Chen <[email protected]>
//
#ifndef RIME_STRING_TABLE_H_
#define RIME_STRING_TABLE_H_
#include <utility>
#include "marisa.h"
#include "common.h"
namespace rime {
using StringId = marisa::UInt32;
const StringId kInvalidStringId = (StringId)(-1);
class StringTable {
public:
StringTable() = default;
virtual ~StringTable() = default;
StringTable(const char* ptr, size_t size);
bool HasKey(const string& key);
StringId Lookup(const string& key);
void CommonPrefixMatch(const string& query,
vector<StringId>* result);
void Predict(const string& query,
vector<StringId>* result);
string GetString(StringId string_id);
size_t NumKeys() const;
size_t BinarySize() const;
protected:
marisa::Trie trie_;
};
class StringTableBuilder: public StringTable {
public:
void Add(const string& key, double weight = 1.0,
StringId* reference = nullptr);
void Clear();
void Build();
void Dump(char* ptr, size_t size);
private:
void UpdateReferences();
marisa::Keyset keys_;
vector<StringId*> references_;
};
} // namespace rime
#endif // RIME_STRING_TABLE_H_