@@ -31,23 +31,35 @@ bool UniquifiedTranslation::Next() {
31
31
return CacheTranslation::Next () && Uniquify ();
32
32
}
33
33
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
+ }
37
41
}
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" );
48
58
}
59
+ uniquified->Append (next);
60
+ CacheTranslation::Next ();
49
61
}
50
- return true ;
62
+ return false ;
51
63
}
52
64
53
65
// Uniquifier
0 commit comments