Skip to content

Commit 2ab76bc

Browse files
committed
fix(uniquifier): half of the duplicate candidates remain after dedup [Closes #114]
1 parent 9c4fa8a commit 2ab76bc

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/rime/gear/uniquifier.cc

+26-14
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,35 @@ bool UniquifiedTranslation::Next() {
3131
return CacheTranslation::Next() && Uniquify();
3232
}
3333

34-
bool UniquifiedTranslation::Uniquify() {
35-
if (exhausted()) {
36-
return false;
34+
static CandidateList::iterator find_text_match(const an<Candidate>& target,
35+
CandidateList::iterator begin,
36+
CandidateList::iterator end) {
37+
for (auto iter = begin; iter != end; ++iter) {
38+
if ((*iter)->text() == target->text()) {
39+
return iter;
40+
}
3741
}
38-
auto next = Peek();
39-
for (auto& c : *candidates_) {
40-
if (c->text() == next->text()) {
41-
auto u = As<UniquifiedCandidate>(c);
42-
if (!u) {
43-
u = New<UniquifiedCandidate>(c, "uniquified");
44-
c = u;
45-
}
46-
u->Append(next);
47-
return CacheTranslation::Next();
42+
return end;
43+
}
44+
45+
bool UniquifiedTranslation::Uniquify() {
46+
while (!exhausted()) {
47+
auto next = Peek();
48+
CandidateList::iterator previous = find_text_match(
49+
next, candidates_->begin(), candidates_->end());
50+
if (previous == candidates_->end()) {
51+
// Encountered a unique candidate.
52+
return true;
53+
}
54+
auto uniquified = As<UniquifiedCandidate>(*previous);
55+
if (!uniquified) {
56+
*previous = uniquified =
57+
New<UniquifiedCandidate>(*previous, "uniquified");
4858
}
59+
uniquified->Append(next);
60+
CacheTranslation::Next();
4961
}
50-
return true;
62+
return false;
5163
}
5264

5365
// Uniquifier

0 commit comments

Comments
 (0)