Add default capacity parameter to InputPointers' constructor

Change-Id: I02f23096f0682d30effe4dfc1ca57881a1e4aedc
main
Tadashi G. Takaoka 2012-07-18 20:31:09 +09:00
parent 71b772ec58
commit 57f7de0ba6
6 changed files with 43 additions and 31 deletions

View File

@ -234,9 +234,8 @@ public class PointerTracker {
// TODO: To handle multi-touch gestures we may want to move this method to
// {@link PointerTrackerQueue}.
private static InputPointers getIncrementalBatchPoints() {
final InputPointers pointers = new InputPointers();
// TODO: Add a default capacity parameter for the InputPointers' constructor.
// TODO: Avoid creating a new instance here?
final InputPointers pointers = new InputPointers(GestureStroke.DEFAULT_CAPACITY);
for (final PointerTracker tracker : sTrackers) {
tracker.mGestureStroke.appendIncrementalBatchPoints(pointers);
}
@ -246,9 +245,8 @@ public class PointerTracker {
// TODO: To handle multi-touch gestures we may want to move this method to
// {@link PointerTrackerQueue}.
private static InputPointers getAllBatchPoints() {
// TODO: Add a default capacity parameter for the InputPointers' constructor.
// TODO: Avoid creating a new instance here?
final InputPointers pointers = new InputPointers();
final InputPointers pointers = new InputPointers(GestureStroke.DEFAULT_CAPACITY);
for (final PointerTracker tracker : sTrackers) {
tracker.mGestureStroke.appendAllBatchPoints(pointers);
}

View File

@ -19,8 +19,11 @@ import android.util.FloatMath;
import com.android.inputmethod.latin.InputPointers;
public class GestureStroke {
public static final int DEFAULT_CAPACITY = 128;
private final int mPointerId;
private final InputPointers mInputPointers = new InputPointers();
// TODO: Replace this {@link InputPointers} with a set of {@link ScalableIntArray}s.
private final InputPointers mInputPointers = new InputPointers(DEFAULT_CAPACITY);
private float mLength;
private float mAngle;
private int mIncrementalRecognitionPoint;

View File

@ -20,10 +20,19 @@ import java.util.Arrays;
// TODO: This class is not thread-safe.
public class InputPointers {
private final ScalableIntArray mXCoordinates = new ScalableIntArray();
private final ScalableIntArray mYCoordinates = new ScalableIntArray();
private final ScalableIntArray mPointerIds = new ScalableIntArray();
private final ScalableIntArray mTimes = new ScalableIntArray();
private final int mDefaultCapacity;
private final ScalableIntArray mXCoordinates;
private final ScalableIntArray mYCoordinates;
private final ScalableIntArray mPointerIds;
private final ScalableIntArray mTimes;
public InputPointers(int defaultCapacity) {
mDefaultCapacity = defaultCapacity;
mXCoordinates = new ScalableIntArray(defaultCapacity);
mYCoordinates = new ScalableIntArray(defaultCapacity);
mPointerIds = new ScalableIntArray(defaultCapacity);
mTimes = new ScalableIntArray(defaultCapacity);
}
public void addPointer(int index, int x, int y, int pointerId, int time) {
mXCoordinates.add(index, x);
@ -70,10 +79,11 @@ public class InputPointers {
}
public void reset() {
mXCoordinates.reset();
mYCoordinates.reset();
mPointerIds.reset();
mTimes.reset();
final int defaultCapacity = mDefaultCapacity;
mXCoordinates.reset(defaultCapacity);
mYCoordinates.reset(defaultCapacity);
mPointerIds.reset(defaultCapacity);
mTimes.reset(defaultCapacity);
}
public int getPointerSize() {
@ -97,12 +107,11 @@ public class InputPointers {
}
private static class ScalableIntArray {
private static final int DEFAULT_SIZE = BinaryDictionary.MAX_WORD_LENGTH;
private int[] mArray;
private int mLength;
public ScalableIntArray() {
reset();
public ScalableIntArray(int capacity) {
reset(capacity);
}
public void add(int index, int val) {
@ -136,8 +145,8 @@ public class InputPointers {
return mLength;
}
public void reset() {
mArray = new int[DEFAULT_SIZE];
public void reset(int capacity) {
mArray = new int[capacity];
mLength = 0;
}

View File

@ -45,7 +45,7 @@ public class LastComposedWord {
public final String mCommittedWord;
public final int mSeparatorCode;
public final CharSequence mPrevWord;
public final InputPointers mInputPointers = new InputPointers();
public final InputPointers mInputPointers = new InputPointers(BinaryDictionary.MAX_WORD_LENGTH);
private boolean mActive;

View File

@ -33,7 +33,7 @@ public class WordComposer {
private static final int N = BinaryDictionary.MAX_WORD_LENGTH;
private int[] mPrimaryKeyCodes;
private final InputPointers mInputPointers = new InputPointers();
private final InputPointers mInputPointers = new InputPointers(N);
private final StringBuilder mTypedWord;
private CharSequence mAutoCorrection;
private boolean mIsResumed;

View File

@ -19,8 +19,10 @@ package com.android.inputmethod.latin;
import android.test.AndroidTestCase;
public class InputPointersTests extends AndroidTestCase {
private static final int DEFAULT_CAPACITY = 48;
public void testNewInstance() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
assertEquals("newInstance size", 0, src.getPointerSize());
assertNotNull("new instance xCoordinates", src.getXCoordinates());
assertNotNull("new instance yCoordinates", src.getYCoordinates());
@ -29,7 +31,7 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testReset() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int[] xCoordinates = src.getXCoordinates();
final int[] yCoordinates = src.getXCoordinates();
final int[] pointerIds = src.getXCoordinates();
@ -44,7 +46,7 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testAdd() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = src.getXCoordinates().length * 2 + 10;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
@ -59,7 +61,7 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testAddAt() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = 1000, step = 100;
for (int i = 0; i < limit; i += step) {
src.addPointer(i, i, i * 2, i * 3, i * 4);
@ -74,12 +76,12 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testSet() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = src.getXCoordinates().length * 2 + 10;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
final InputPointers dst = new InputPointers();
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
dst.set(src);
assertEquals("after set size", dst.getPointerSize(), src.getPointerSize());
assertSame("after set xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
@ -89,12 +91,12 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testCopy() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int limit = 100;
for (int i = 0; i < limit; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
final InputPointers dst = new InputPointers();
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
dst.copy(src);
assertEquals("after copy size", dst.getPointerSize(), src.getPointerSize());
assertNotSame("after copy xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
@ -113,18 +115,18 @@ public class InputPointersTests extends AndroidTestCase {
}
public void testAppend() {
final InputPointers src = new InputPointers();
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
final int srcLen = 100;
for (int i = 0; i < srcLen; i++) {
src.addPointer(i, i * 2, i * 3, i * 4);
}
final int dstLen = 50;
final InputPointers dst = new InputPointers();
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
for (int i = 0; i < dstLen; i++) {
final int value = -i - 1;
dst.addPointer(value * 4, value * 3, value * 2, value);
}
final InputPointers dstCopy = new InputPointers();
final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
dstCopy.copy(dst);
dst.append(src, 0, 0);