librime: translation.h Source File

| librime 1.2 Rime Input Method Engine, the core library

translation.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-05-02 Wensong He <snowhws@gmail.com>
6//
7
8#ifndef RIME_TRANSLATION_H_
9#define RIME_TRANSLATION_H_
10
11#include <rime_api.h>
12#include <rime/candidate.h>
13#include <rime/common.h>
14
15namespace rime {
16
17 class RIME_DLL Translation {
18 public:
19 Translation() = default;
20 virtual ~Translation() = default;
21
22 // A translation may contain multiple results, looks
23 // something like a generator of candidates.
24 virtual bool Next() = 0;
25
26 virtual an<Candidate> Peek() = 0;
27
28 // should it provide the next candidate (negative value, zero) or
29 // should it give up the chance for other translations (positive)?
30 virtual int Compare(an<Translation> other, const CandidateList& candidates);
31
32 bool exhausted() const { return exhausted_; }
33
34 protected:
35 void set_exhausted(bool exhausted) { exhausted_ = exhausted; }
36
37 private:
38 bool exhausted_ = false;
39};
40
41 class UniqueTranslation : public Translation {
42 public:
43 UniqueTranslation(an<Candidate> candidate) : candidate_(candidate) {
44 set_exhausted(!candidate);
45 }
46
47 bool Next();
48 an<Candidate> Peek();
49
50 protected:
51 an<Candidate> candidate_;
52};
53
54 class RIME_DLL FifoTranslation : public Translation {
55 public:
56 FifoTranslation();
57
58 bool Next();
59 an<Candidate> Peek();
60
61 void Append(an<Candidate> candy);
62
63 size_t size() const { return candies_.size() - cursor_; }
64
65 protected:
66 CandidateList candies_;
67 size_t cursor_ = 0;
68};
69
70 class UnionTranslation : public Translation {
71 public:
72 UnionTranslation();
73
74 bool Next();
75 an<Candidate> Peek();
76
77 UnionTranslation& operator+=(an<Translation> t);
78
79 protected:
80 list<of<Translation>> translations_;
81};
82
83 an<UnionTranslation> operator+(an<Translation> x, an<Translation> y);
84
85 class MergedTranslation : public Translation {
86 public:
87 explicit MergedTranslation(const CandidateList& previous_candidates);
88
89 bool Next();
90 an<Candidate> Peek();
91
92 MergedTranslation& operator+=(an<Translation> t);
93
94 size_t size() const { return translations_.size(); }
95
96 protected:
97 void Elect();
98
99 const CandidateList& previous_candidates_;
100 vector<of<Translation>> translations_;
101 size_t elected_ = 0;
102};
103
104 class CacheTranslation : public Translation {
105 public:
106 CacheTranslation(an<Translation> translation);
107
108 virtual bool Next();
109 virtual an<Candidate> Peek();
110
111 protected:
112 an<Translation> translation_;
113 an<Candidate> cache_;
114};
115
116template <class T, class... Args>
117 inline an<Translation> Cached(Args&&... args) {
118 return New<CacheTranslation>(New<T>(std::forward<Args>(args)...));
119}
120
121 class DistinctTranslation : public CacheTranslation {
122 public:
123 DistinctTranslation(an<Translation> translation);
124 virtual bool Next();
125
126 protected:
127 bool AlreadyHas(const string& text) const;
128
129 set<string> candidate_set_;
130};
131
132 class PrefetchTranslation : public Translation {
133 public:
134 PrefetchTranslation(an<Translation> translation);
135
136 virtual bool Next();
137 virtual an<Candidate> Peek();
138
139 protected:
140 virtual bool Replenish() { return false; }
141
142 an<Translation> translation_;
143 CandidateQueue cache_;
144};
145
146} // namespace rime
147
148#endif // RIME_TRANSLATION_H_
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
common.h
candidate.h
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::CandidateQueue
list< of< Candidate > > CandidateQueue
Definition candidate.h:51
rime::Cached
an< Translation > Cached(Args &&... args)
Definition translation.h:117
rime::CandidateList
vector< of< Candidate > > CandidateList
Definition candidate.h:52
rime::operator+
an< UnionTranslation > operator+(an< Translation > x, an< Translation > y)
Definition translation.cc:92
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::Translation::Translation
Translation()=default
rime::Translation::Compare
virtual int Compare(an< Translation > other, const CandidateList &candidates)
Definition translation.cc:12
rime::Translation::Peek
virtual an< Candidate > Peek()=0
rime::Translation::~Translation
virtual ~Translation()=default
rime::Translation::Next
virtual bool Next()=0
rime::Translation::exhausted
bool exhausted() const
Definition translation.h:32
rime::Translation::set_exhausted
void set_exhausted(bool exhausted)
Definition translation.h:35
rime::UniqueTranslation::Next
bool Next()
Definition translation.cc:25
rime::UniqueTranslation::candidate_
an< Candidate > candidate_
Definition translation.h:51
rime::UniqueTranslation::UniqueTranslation
UniqueTranslation(an< Candidate > candidate)
Definition translation.h:43
rime::UniqueTranslation::Peek
an< Candidate > Peek()
Definition translation.cc:32
rime::FifoTranslation::Next
bool Next()
Definition translation.cc:42
rime::FifoTranslation::size
size_t size() const
Definition translation.h:63
rime::FifoTranslation::candies_
CandidateList candies_
Definition translation.h:66
rime::FifoTranslation::FifoTranslation
FifoTranslation()
Definition translation.cc:38
rime::FifoTranslation::cursor_
size_t cursor_
Definition translation.h:67
rime::FifoTranslation::Append
void Append(an< Candidate > candy)
Definition translation.cc:56
rime::FifoTranslation::Peek
an< Candidate > Peek()
Definition translation.cc:50
rime::UnionTranslation::Next
bool Next()
Definition translation.cc:65
rime::UnionTranslation::Peek
an< Candidate > Peek()
Definition translation.cc:78
rime::UnionTranslation::UnionTranslation
UnionTranslation()
Definition translation.cc:61
rime::UnionTranslation::translations_
list< of< Translation > > translations_
Definition translation.h:80
rime::UnionTranslation::operator+=
UnionTranslation & operator+=(an< Translation > t)
Definition translation.cc:84
rime::MergedTranslation::previous_candidates_
const CandidateList & previous_candidates_
Definition translation.h:99
rime::MergedTranslation::Elect
void Elect()
Definition translation.cc:126
rime::MergedTranslation::MergedTranslation
MergedTranslation(const CandidateList &previous_candidates)
Definition translation.cc:101
rime::MergedTranslation::operator+=
MergedTranslation & operator+=(an< Translation > t)
Definition translation.cc:154
rime::MergedTranslation::translations_
vector< of< Translation > > translations_
Definition translation.h:100
rime::MergedTranslation::Next
bool Next()
Definition translation.cc:106
rime::MergedTranslation::size
size_t size() const
Definition translation.h:94
rime::MergedTranslation::elected_
size_t elected_
Definition translation.h:101
rime::MergedTranslation::Peek
an< Candidate > Peek()
Definition translation.cc:119
rime::CacheTranslation::cache_
an< Candidate > cache_
Definition translation.h:113
rime::CacheTranslation::Peek
virtual an< Candidate > Peek()
Definition translation.cc:180
rime::CacheTranslation::translation_
an< Translation > translation_
Definition translation.h:112
rime::CacheTranslation::Next
virtual bool Next()
Definition translation.cc:169
rime::CacheTranslation::CacheTranslation
CacheTranslation(an< Translation > translation)
Definition translation.cc:164
rime::DistinctTranslation::DistinctTranslation
DistinctTranslation(an< Translation > translation)
Definition translation.cc:191
rime::DistinctTranslation::candidate_set_
set< string > candidate_set_
Definition translation.h:129
rime::DistinctTranslation::Next
virtual bool Next()
Definition translation.cc:194
rime::DistinctTranslation::AlreadyHas
bool AlreadyHas(const string &text) const
Definition translation.cc:205
rime::PrefetchTranslation::Replenish
virtual bool Replenish()
Definition translation.h:140
rime::PrefetchTranslation::Peek
virtual an< Candidate > Peek()
Definition translation.cc:231
rime::PrefetchTranslation::Next
virtual bool Next()
Definition translation.cc:216
rime::PrefetchTranslation::cache_
CandidateQueue cache_
Definition translation.h:143
rime::PrefetchTranslation::translation_
an< Translation > translation_
Definition translation.h:142
rime::PrefetchTranslation::PrefetchTranslation
PrefetchTranslation(an< Translation > translation)
Definition translation.cc:211