librime: string_table.h Source File

| librime 1.2 Rime Input Method Engine, the core library

string_table.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2014-06-25 GONG Chen <chen.sst@gmail.com>
6//
7
8#ifndef RIME_STRING_TABLE_H_
9#define RIME_STRING_TABLE_H_
10
11#include <cstdint>
12#include <utility>
13#include <marisa.h>
14#include <rime_api.h>
15#include <rime/common.h>
16
17namespace rime {
18
19 using StringId = std::uint32_t;
20
21 const StringId kInvalidStringId = (StringId)(-1);
22
23 class RIME_DLL StringTable {
24 public:
25 StringTable() = default;
26 virtual ~StringTable() = default;
27 StringTable(const char* ptr, size_t size);
28
29 bool HasKey(const string& key);
30 StringId Lookup(const string& key);
31 void CommonPrefixMatch(const string& query, vector<StringId>* result);
32 void Predict(const string& query, vector<StringId>* result);
33 string GetString(StringId string_id);
34
35 size_t NumKeys() const;
36 size_t BinarySize() const;
37
38 protected:
39 marisa::Trie trie_;
40};
41
42 class RIME_DLL StringTableBuilder : public StringTable {
43 public:
44 void Add(const string& key,
45 double weight = 1.0,
46 StringId* reference = nullptr);
47 void Clear();
48 void Build();
49 void Dump(char* ptr, size_t size);
50
51 private:
52 void UpdateReferences();
53
54 marisa::Keyset keys_;
55 vector<StringId*> references_;
56};
57
58} // namespace rime
59
60#endif // RIME_STRING_TABLE_H_
common.h
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::StringId
std::uint32_t StringId
Definition string_table.h:19
rime::kInvalidStringId
const StringId kInvalidStringId
Definition string_table.h:21
rime::StringTable::HasKey
bool HasKey(const string &key)
Definition string_table.cc:18
rime::StringTable::Predict
void Predict(const string &query, vector< StringId > *result)
Definition string_table.cc:44
rime::StringTable::BinarySize
size_t BinarySize() const
Definition string_table.cc:69
rime::StringTable::GetString
string GetString(StringId string_id)
Definition string_table.cc:53
rime::StringTable::trie_
marisa::Trie trie_
Definition string_table.h:39
rime::StringTable::Lookup
StringId Lookup(const string &key)
Definition string_table.cc:24
rime::StringTable::CommonPrefixMatch
void CommonPrefixMatch(const string &query, vector< StringId > *result)
Definition string_table.cc:34
rime::StringTable::StringTable
StringTable()=default
rime::StringTable::NumKeys
size_t NumKeys() const
Definition string_table.cc:65
rime::StringTable::~StringTable
virtual ~StringTable()=default
rime::StringTableBuilder
Definition string_table.h:42
rime::StringTableBuilder::Clear
void Clear()
Definition string_table.cc:80
rime::StringTableBuilder::Add
void Add(const string &key, double weight=1.0, StringId *reference=nullptr)
Definition string_table.cc:73
rime::StringTableBuilder::Dump
void Dump(char *ptr, size_t size)
Definition string_table.cc:103
rime::StringTableBuilder::Build
void Build()
Definition string_table.cc:86