From 2ab76bc10d814a8c67949ecb521fad543de2493d Mon Sep 17 00:00:00 2001 From: Chen Gong Date: Tue, 17 Jan 2017 01:03:41 +0800 Subject: [PATCH] fix(uniquifier): half of the duplicate candidates remain after dedup [Closes #114] --- src/rime/gear/uniquifier.cc | 40 ++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/rime/gear/uniquifier.cc b/src/rime/gear/uniquifier.cc index c302a5218..daceca305 100644 --- a/src/rime/gear/uniquifier.cc +++ b/src/rime/gear/uniquifier.cc @@ -31,23 +31,35 @@ bool UniquifiedTranslation::Next() { return CacheTranslation::Next() && Uniquify(); } -bool UniquifiedTranslation::Uniquify() { - if (exhausted()) { - return false; +static CandidateList::iterator find_text_match(const an& target, + CandidateList::iterator begin, + CandidateList::iterator end) { + for (auto iter = begin; iter != end; ++iter) { + if ((*iter)->text() == target->text()) { + return iter; + } } - auto next = Peek(); - for (auto& c : *candidates_) { - if (c->text() == next->text()) { - auto u = As(c); - if (!u) { - u = New(c, "uniquified"); - c = u; - } - u->Append(next); - return CacheTranslation::Next(); + return end; +} + +bool UniquifiedTranslation::Uniquify() { + while (!exhausted()) { + auto next = Peek(); + CandidateList::iterator previous = find_text_match( + next, candidates_->begin(), candidates_->end()); + if (previous == candidates_->end()) { + // Encountered a unique candidate. + return true; + } + auto uniquified = As(*previous); + if (!uniquified) { + *previous = uniquified = + New(*previous, "uniquified"); } + uniquified->Append(next); + CacheTranslation::Next(); } - return true; + return false; } // Uniquifier