ongoing cleanup 2

Change-Id: I66b61cbe491cf8375144e834390beae3209a777d
This commit is contained in:
satok 2012-03-27 15:21:54 +09:00
parent b79064cbeb
commit 01ab7c8b59
4 changed files with 39 additions and 39 deletions

View file

@ -144,9 +144,7 @@ public class BinaryDictionary extends Dictionary {
int codesSize = codes.size(); int codesSize = codes.size();
Arrays.fill(mInputCodes, -1); Arrays.fill(mInputCodes, -1);
if (codesSize > 0) { if (codesSize > 0) {
int[] alternatives = codes.getCodesAt(0); mInputCodes[0] = codes.getCodeAt(0);
System.arraycopy(alternatives, 0, mInputCodes, 0,
Math.min(alternatives.length, MAX_PROXIMITY_CHARS_SIZE));
} }
int count = getBigramsNative(mNativeDict, chars, chars.length, mInputCodes, codesSize, int count = getBigramsNative(mNativeDict, chars, chars.length, mInputCodes, codesSize,
@ -205,11 +203,7 @@ public class BinaryDictionary extends Dictionary {
Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE); Arrays.fill(mInputCodes, WordComposer.NOT_A_CODE);
for (int i = 0; i < codesSize; i++) { for (int i = 0; i < codesSize; i++) {
final int[] alternatives = codes.getCodesAt(i); mInputCodes[i] = codes.getCodeAt(i);
if (alternatives == null || alternatives.length < 1) {
continue;
}
mInputCodes[i] = alternatives[0];
} }
Arrays.fill(outputChars, (char) 0); Arrays.fill(outputChars, (char) 0);
Arrays.fill(scores, 0); Arrays.fill(scores, 0);

View file

@ -210,7 +210,11 @@ public class ExpandableDictionary extends Dictionary {
if (mCodes.length < mInputLength) mCodes = new int[mInputLength][]; if (mCodes.length < mInputLength) mCodes = new int[mInputLength][];
// Cache the codes so that we don't have to lookup an array list // Cache the codes so that we don't have to lookup an array list
for (int i = 0; i < mInputLength; i++) { for (int i = 0; i < mInputLength; i++) {
mCodes[i] = codes.getCodesAt(i); // TODO: Calculate proximity info here.
if (mCodes[i] == null || mCodes[i].length < 1) {
mCodes[i] = new int[1];
}
mCodes[i][0] = codes.getCodeAt(i);
} }
mMaxDepth = mInputLength * 3; mMaxDepth = mInputLength * 3;
getWordsRec(mRoots, codes, mWordBuilder, 0, false, 1, 0, -1, callback); getWordsRec(mRoots, codes, mWordBuilder, 0, false, 1, 0, -1, callback);
@ -319,7 +323,7 @@ public class ExpandableDictionary extends Dictionary {
} }
} else { } else {
// Don't use alternatives if we're looking for missing characters // Don't use alternatives if we're looking for missing characters
final int alternativesSize = skipPos >= 0? 1 : currentChars.length; final int alternativesSize = skipPos >= 0 ? 1 : currentChars.length;
for (int j = 0; j < alternativesSize; j++) { for (int j = 0; j < alternativesSize; j++) {
final int addedAttenuation = (j > 0 ? 1 : 2); final int addedAttenuation = (j > 0 ? 1 : 2);
final int currentChar = currentChars[j]; final int currentChar = currentChars[j];

View file

@ -18,8 +18,6 @@ package com.android.inputmethod.latin;
import android.text.TextUtils; import android.text.TextUtils;
import java.util.ArrayList;
/** /**
* This class encapsulates data about a word previously composed, but that has been * This class encapsulates data about a word previously composed, but that has been
* committed already. This is used for resuming suggestion, and cancel auto-correction. * committed already. This is used for resuming suggestion, and cancel auto-correction.
@ -42,7 +40,7 @@ public class LastComposedWord {
public static final int NOT_A_SEPARATOR = -1; public static final int NOT_A_SEPARATOR = -1;
public final ArrayList<int[]> mCodes; public final int[] mPrimaryKeyCodes;
public final int[] mXCoordinates; public final int[] mXCoordinates;
public final int[] mYCoordinates; public final int[] mYCoordinates;
public final String mTypedWord; public final String mTypedWord;
@ -56,10 +54,10 @@ public class LastComposedWord {
// Warning: this is using the passed objects as is and fully expects them to be // Warning: this is using the passed objects as is and fully expects them to be
// immutable. Do not fiddle with their contents after you passed them to this constructor. // immutable. Do not fiddle with their contents after you passed them to this constructor.
public LastComposedWord(final ArrayList<int[]> codes, final int[] xCoordinates, public LastComposedWord(final int[] primaryKeyCodes, final int[] xCoordinates,
final int[] yCoordinates, final String typedWord, final String committedWord, final int[] yCoordinates, final String typedWord, final String committedWord,
final int separatorCode) { final int separatorCode) {
mCodes = codes; mPrimaryKeyCodes = primaryKeyCodes;
mXCoordinates = xCoordinates; mXCoordinates = xCoordinates;
mYCoordinates = yCoordinates; mYCoordinates = yCoordinates;
mTypedWord = typedWord; mTypedWord = typedWord;

View file

@ -21,7 +21,6 @@ import com.android.inputmethod.keyboard.KeyDetector;
import com.android.inputmethod.keyboard.Keyboard; import com.android.inputmethod.keyboard.Keyboard;
import com.android.inputmethod.keyboard.KeyboardActionListener; import com.android.inputmethod.keyboard.KeyboardActionListener;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
/** /**
@ -32,9 +31,9 @@ public class WordComposer {
public static final int NOT_A_CODE = KeyDetector.NOT_A_CODE; public static final int NOT_A_CODE = KeyDetector.NOT_A_CODE;
public static final int NOT_A_COORDINATE = -1; public static final int NOT_A_COORDINATE = -1;
final static int N = BinaryDictionary.MAX_WORD_LENGTH; private static final int N = BinaryDictionary.MAX_WORD_LENGTH;
private ArrayList<int[]> mCodes; private int[] mPrimaryKeyCodes;
private int[] mXCoordinates; private int[] mXCoordinates;
private int[] mYCoordinates; private int[] mYCoordinates;
private StringBuilder mTypedWord; private StringBuilder mTypedWord;
@ -44,6 +43,7 @@ public class WordComposer {
private int mCapsCount; private int mCapsCount;
private boolean mAutoCapitalized; private boolean mAutoCapitalized;
private int mTrailingSingleQuotesCount; private int mTrailingSingleQuotesCount;
private int mCodePointSize;
/** /**
* Whether the user chose to capitalize the first char of the word. * Whether the user chose to capitalize the first char of the word.
@ -51,12 +51,13 @@ public class WordComposer {
private boolean mIsFirstCharCapitalized; private boolean mIsFirstCharCapitalized;
public WordComposer() { public WordComposer() {
mCodes = new ArrayList<int[]>(N); mPrimaryKeyCodes = new int[N];
mTypedWord = new StringBuilder(N); mTypedWord = new StringBuilder(N);
mXCoordinates = new int[N]; mXCoordinates = new int[N];
mYCoordinates = new int[N]; mYCoordinates = new int[N];
mAutoCorrection = null; mAutoCorrection = null;
mTrailingSingleQuotesCount = 0; mTrailingSingleQuotesCount = 0;
refreshSize();
} }
public WordComposer(WordComposer source) { public WordComposer(WordComposer source) {
@ -64,7 +65,7 @@ public class WordComposer {
} }
public void init(WordComposer source) { public void init(WordComposer source) {
mCodes = new ArrayList<int[]>(source.mCodes); mPrimaryKeyCodes = Arrays.copyOf(source.mPrimaryKeyCodes, source.mPrimaryKeyCodes.length);
mTypedWord = new StringBuilder(source.mTypedWord); mTypedWord = new StringBuilder(source.mTypedWord);
mXCoordinates = Arrays.copyOf(source.mXCoordinates, source.mXCoordinates.length); mXCoordinates = Arrays.copyOf(source.mXCoordinates, source.mXCoordinates.length);
mYCoordinates = Arrays.copyOf(source.mYCoordinates, source.mYCoordinates.length); mYCoordinates = Arrays.copyOf(source.mYCoordinates, source.mYCoordinates.length);
@ -72,18 +73,23 @@ public class WordComposer {
mIsFirstCharCapitalized = source.mIsFirstCharCapitalized; mIsFirstCharCapitalized = source.mIsFirstCharCapitalized;
mAutoCapitalized = source.mAutoCapitalized; mAutoCapitalized = source.mAutoCapitalized;
mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount; mTrailingSingleQuotesCount = source.mTrailingSingleQuotesCount;
refreshSize();
} }
/** /**
* Clear out the keys registered so far. * Clear out the keys registered so far.
*/ */
public void reset() { public void reset() {
mCodes.clear();
mTypedWord.setLength(0); mTypedWord.setLength(0);
mAutoCorrection = null; mAutoCorrection = null;
mCapsCount = 0; mCapsCount = 0;
mIsFirstCharCapitalized = false; mIsFirstCharCapitalized = false;
mTrailingSingleQuotesCount = 0; mTrailingSingleQuotesCount = 0;
refreshSize();
}
public final void refreshSize() {
mCodePointSize = mTypedWord.codePointCount(0, mTypedWord.length());
} }
/** /**
@ -91,20 +97,15 @@ public class WordComposer {
* @return the number of keystrokes * @return the number of keystrokes
*/ */
public final int size() { public final int size() {
return mCodes.size(); return mCodePointSize;
} }
public final boolean isComposingWord() { public final boolean isComposingWord() {
return mCodes.size() > 0; return size() > 0;
} }
/** public int getCodeAt(int index) {
* Returns the codes at a particular position in the word. return mPrimaryKeyCodes[index];
* @param index the position in the word
* @return the unicode for the pressed and surrounding keys
*/
public int[] getCodesAt(int index) {
return mCodes.get(index);
} }
public int[] getXCoordinates() { public int[] getXCoordinates() {
@ -149,9 +150,10 @@ public class WordComposer {
* @param codes the array of unicode values * @param codes the array of unicode values
*/ */
private void add(int primaryCode, int[] codes, int keyX, int keyY) { private void add(int primaryCode, int[] codes, int keyX, int keyY) {
final int newIndex = mCodes.size(); final int newIndex = size();
mTypedWord.appendCodePoint(primaryCode); mTypedWord.appendCodePoint(primaryCode);
mCodes.add(codes); refreshSize();
mPrimaryKeyCodes[newIndex] = codes[0];
if (newIndex < BinaryDictionary.MAX_WORD_LENGTH) { if (newIndex < BinaryDictionary.MAX_WORD_LENGTH) {
mXCoordinates[newIndex] = keyX; mXCoordinates[newIndex] = keyX;
mYCoordinates[newIndex] = keyY; mYCoordinates[newIndex] = keyY;
@ -201,9 +203,8 @@ public class WordComposer {
* Delete the last keystroke as a result of hitting backspace. * Delete the last keystroke as a result of hitting backspace.
*/ */
public void deleteLast() { public void deleteLast() {
final int size = mCodes.size(); final int size = size();
if (size > 0) { if (size > 0) {
mCodes.remove(size - 1);
// Note: mTypedWord.length() and mCodes.length differ when there are surrogate pairs // Note: mTypedWord.length() and mCodes.length differ when there are surrogate pairs
final int stringBuilderLength = mTypedWord.length(); final int stringBuilderLength = mTypedWord.length();
if (stringBuilderLength < size) { if (stringBuilderLength < size) {
@ -217,9 +218,10 @@ public class WordComposer {
mTypedWord.deleteCharAt(stringBuilderLength - 1); mTypedWord.deleteCharAt(stringBuilderLength - 1);
} }
if (Character.isUpperCase(lastChar)) mCapsCount--; if (Character.isUpperCase(lastChar)) mCapsCount--;
refreshSize();
} }
// We may have deleted the last one. // We may have deleted the last one.
if (0 == mCodes.size()) { if (0 == size()) {
mIsFirstCharCapitalized = false; mIsFirstCharCapitalized = false;
} }
if (mTrailingSingleQuotesCount > 0) { if (mTrailingSingleQuotesCount > 0) {
@ -307,29 +309,31 @@ public class WordComposer {
// Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK // Note: currently, we come here whenever we commit a word. If it's a MANUAL_PICK
// or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate // or a DECIDED_WORD we may cancel the commit later; otherwise, we should deactivate
// the last composed word to ensure this does not happen. // the last composed word to ensure this does not happen.
final ArrayList<int[]> codes = mCodes; final int[] primaryKeyCodes = mPrimaryKeyCodes;
final int[] xCoordinates = mXCoordinates; final int[] xCoordinates = mXCoordinates;
final int[] yCoordinates = mYCoordinates; final int[] yCoordinates = mYCoordinates;
mCodes = new ArrayList<int[]>(N); mPrimaryKeyCodes = new int[N];
mXCoordinates = new int[N]; mXCoordinates = new int[N];
mYCoordinates = new int[N]; mYCoordinates = new int[N];
final LastComposedWord lastComposedWord = new LastComposedWord(codes, final LastComposedWord lastComposedWord = new LastComposedWord(primaryKeyCodes,
xCoordinates, yCoordinates, mTypedWord.toString(), committedWord, separatorCode); xCoordinates, yCoordinates, mTypedWord.toString(), committedWord, separatorCode);
if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD if (type != LastComposedWord.COMMIT_TYPE_DECIDED_WORD
&& type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) { && type != LastComposedWord.COMMIT_TYPE_MANUAL_PICK) {
lastComposedWord.deactivate(); lastComposedWord.deactivate();
} }
mTypedWord.setLength(0); mTypedWord.setLength(0);
refreshSize();
mAutoCorrection = null; mAutoCorrection = null;
return lastComposedWord; return lastComposedWord;
} }
public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord) { public void resumeSuggestionOnLastComposedWord(final LastComposedWord lastComposedWord) {
mCodes = lastComposedWord.mCodes; mPrimaryKeyCodes = lastComposedWord.mPrimaryKeyCodes;
mXCoordinates = lastComposedWord.mXCoordinates; mXCoordinates = lastComposedWord.mXCoordinates;
mYCoordinates = lastComposedWord.mYCoordinates; mYCoordinates = lastComposedWord.mYCoordinates;
mTypedWord.setLength(0); mTypedWord.setLength(0);
mTypedWord.append(lastComposedWord.mTypedWord); mTypedWord.append(lastComposedWord.mTypedWord);
refreshSize();
mAutoCorrection = null; // This will be filled by the next call to updateSuggestion. mAutoCorrection = null; // This will be filled by the next call to updateSuggestion.
} }
} }