[CS2] Refactor a bit removeDups

This way is more understandable, and also supporting an
external string is helping for future refactorings

Bug: 13238601
Change-Id: I4ebeed46eb0b35011164946af71ac257c6449ddb
This commit is contained in:
Jean Chalard 2014-06-19 16:12:34 +09:00
parent e83e79cb05
commit fdebf4005f
2 changed files with 21 additions and 20 deletions

View file

@ -180,6 +180,7 @@ public final class Suggest {
suggestionsContainer.set(i, transformedWordInfo); suggestionsContainer.set(i, transformedWordInfo);
} }
} }
SuggestedWordInfo.removeDups(typedWord, suggestionsContainer);
if (!TextUtils.isEmpty(typedWord)) { if (!TextUtils.isEmpty(typedWord)) {
suggestionsContainer.add(0, new SuggestedWordInfo(typedWord, suggestionsContainer.add(0, new SuggestedWordInfo(typedWord,
@ -188,7 +189,6 @@ public final class Suggest {
SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */, SuggestedWordInfo.NOT_AN_INDEX /* indexOfTouchPointOfSecondWord */,
SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */)); SuggestedWordInfo.NOT_A_CONFIDENCE /* autoCommitFirstWordConfidence */));
} }
SuggestedWordInfo.removeDups(suggestionsContainer);
final ArrayList<SuggestedWordInfo> suggestionsList; final ArrayList<SuggestedWordInfo> suggestionsList;
if (DBG && !suggestionsContainer.isEmpty()) { if (DBG && !suggestionsContainer.isEmpty()) {
@ -237,7 +237,7 @@ public final class Suggest {
final SuggestedWordInfo rejected = suggestionsContainer.remove(0); final SuggestedWordInfo rejected = suggestionsContainer.remove(0);
suggestionsContainer.add(1, rejected); suggestionsContainer.add(1, rejected);
} }
SuggestedWordInfo.removeDups(suggestionsContainer); SuggestedWordInfo.removeDups(null /* typedWord */, suggestionsContainer);
// For some reason some suggestions with MIN_VALUE are making their way here. // For some reason some suggestions with MIN_VALUE are making their way here.
// TODO: Find a more robust way to detect distractors. // TODO: Find a more robust way to detect distractors.

View file

@ -316,10 +316,6 @@ public class SuggestedWords {
return mDebugString; return mDebugString;
} }
public int codePointCount() {
return mCodePointCount;
}
public int codePointAt(int i) { public int codePointAt(int i) {
return mWord.codePointAt(i); return mWord.codePointAt(i);
} }
@ -333,24 +329,29 @@ public class SuggestedWords {
} }
} }
// TODO: Consolidate this method and StringUtils.removeDupes() in the future. // This will always remove the higher index if a duplicate is found.
public static void removeDups(ArrayList<SuggestedWordInfo> candidates) { public static void removeDups(final String typedWord,
if (candidates.size() <= 1) { ArrayList<SuggestedWordInfo> candidates) {
if (candidates.isEmpty()) {
return; return;
} }
int i = 1; if (!TextUtils.isEmpty(typedWord)) {
while (i < candidates.size()) { removeSuggestedWordInfoFrom(typedWord, candidates, 0);
final SuggestedWordInfo cur = candidates.get(i); }
for (int j = 0; j < i; ++j) { for (int i = 0; i < candidates.size(); ++i) {
final SuggestedWordInfo previous = candidates.get(j); removeSuggestedWordInfoFrom(candidates.get(i).mWord, candidates, i);
if (cur.mWord.equals(previous.mWord)) { }
candidates.remove(cur.mScore < previous.mScore ? i : j); }
private static void removeSuggestedWordInfoFrom(final String word,
final ArrayList<SuggestedWordInfo> candidates, final int startIndex) {
for (int i = startIndex + 1; i < candidates.size(); ++i) {
final SuggestedWordInfo previous = candidates.get(i);
if (word.equals(previous.mWord)) {
candidates.remove(i);
--i; --i;
break;
} }
} }
++i;
}
} }
} }