From 56beb9e3219ae2138b96874aec6f2439180b3507 Mon Sep 17 00:00:00 2001 From: Jean Chalard Date: Tue, 26 Jun 2012 20:26:05 +0900 Subject: [PATCH] Use binarySearch instead of a hand-written linear search Yay. Change-Id: Icf686eb2d675da959f19edc856b521e6288dcea3 --- .../src/com/android/inputmethod/latin/Suggest.java | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/Suggest.java b/java/src/com/android/inputmethod/latin/Suggest.java index b69ea3595..bde3a8403 100644 --- a/java/src/com/android/inputmethod/latin/Suggest.java +++ b/java/src/com/android/inputmethod/latin/Suggest.java @@ -411,15 +411,11 @@ public class Suggest { final int score = wordInfo.mScore; int pos = 0; - // Check the last one's score and bail - if (suggestions.size() >= prefMaxSuggestions - && suggestions.get(prefMaxSuggestions - 1).mScore >= score) return true; - final int length = wordInfo.mCodePointCount; - while (pos < suggestions.size()) { - if (sSuggestedWordInfoComparator.compare(wordInfo, suggestions.get(pos)) < 0) - break; - pos++; - } + final int index = + Collections.binarySearch(suggestions, wordInfo, sSuggestedWordInfoComparator); + // binarySearch returns the index of an equal word info if found. If not found + // it returns -insertionPoint - 1. We want the insertion point, so: + pos = index >= 0 ? index : -index - 1; if (pos >= prefMaxSuggestions) { return true; }