Add default capacity parameter to InputPointers' constructor
Change-Id: I02f23096f0682d30effe4dfc1ca57881a1e4aedc
This commit is contained in:
parent
71b772ec58
commit
57f7de0ba6
6 changed files with 43 additions and 31 deletions
|
@ -234,9 +234,8 @@ public class PointerTracker {
|
||||||
// TODO: To handle multi-touch gestures we may want to move this method to
|
// TODO: To handle multi-touch gestures we may want to move this method to
|
||||||
// {@link PointerTrackerQueue}.
|
// {@link PointerTrackerQueue}.
|
||||||
private static InputPointers getIncrementalBatchPoints() {
|
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?
|
// TODO: Avoid creating a new instance here?
|
||||||
|
final InputPointers pointers = new InputPointers(GestureStroke.DEFAULT_CAPACITY);
|
||||||
for (final PointerTracker tracker : sTrackers) {
|
for (final PointerTracker tracker : sTrackers) {
|
||||||
tracker.mGestureStroke.appendIncrementalBatchPoints(pointers);
|
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
|
// TODO: To handle multi-touch gestures we may want to move this method to
|
||||||
// {@link PointerTrackerQueue}.
|
// {@link PointerTrackerQueue}.
|
||||||
private static InputPointers getAllBatchPoints() {
|
private static InputPointers getAllBatchPoints() {
|
||||||
// TODO: Add a default capacity parameter for the InputPointers' constructor.
|
|
||||||
// TODO: Avoid creating a new instance here?
|
// 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) {
|
for (final PointerTracker tracker : sTrackers) {
|
||||||
tracker.mGestureStroke.appendAllBatchPoints(pointers);
|
tracker.mGestureStroke.appendAllBatchPoints(pointers);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,11 @@ import android.util.FloatMath;
|
||||||
import com.android.inputmethod.latin.InputPointers;
|
import com.android.inputmethod.latin.InputPointers;
|
||||||
|
|
||||||
public class GestureStroke {
|
public class GestureStroke {
|
||||||
|
public static final int DEFAULT_CAPACITY = 128;
|
||||||
|
|
||||||
private final int mPointerId;
|
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 mLength;
|
||||||
private float mAngle;
|
private float mAngle;
|
||||||
private int mIncrementalRecognitionPoint;
|
private int mIncrementalRecognitionPoint;
|
||||||
|
|
|
@ -20,10 +20,19 @@ import java.util.Arrays;
|
||||||
|
|
||||||
// TODO: This class is not thread-safe.
|
// TODO: This class is not thread-safe.
|
||||||
public class InputPointers {
|
public class InputPointers {
|
||||||
private final ScalableIntArray mXCoordinates = new ScalableIntArray();
|
private final int mDefaultCapacity;
|
||||||
private final ScalableIntArray mYCoordinates = new ScalableIntArray();
|
private final ScalableIntArray mXCoordinates;
|
||||||
private final ScalableIntArray mPointerIds = new ScalableIntArray();
|
private final ScalableIntArray mYCoordinates;
|
||||||
private final ScalableIntArray mTimes = new ScalableIntArray();
|
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) {
|
public void addPointer(int index, int x, int y, int pointerId, int time) {
|
||||||
mXCoordinates.add(index, x);
|
mXCoordinates.add(index, x);
|
||||||
|
@ -70,10 +79,11 @@ public class InputPointers {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset() {
|
||||||
mXCoordinates.reset();
|
final int defaultCapacity = mDefaultCapacity;
|
||||||
mYCoordinates.reset();
|
mXCoordinates.reset(defaultCapacity);
|
||||||
mPointerIds.reset();
|
mYCoordinates.reset(defaultCapacity);
|
||||||
mTimes.reset();
|
mPointerIds.reset(defaultCapacity);
|
||||||
|
mTimes.reset(defaultCapacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getPointerSize() {
|
public int getPointerSize() {
|
||||||
|
@ -97,12 +107,11 @@ public class InputPointers {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ScalableIntArray {
|
private static class ScalableIntArray {
|
||||||
private static final int DEFAULT_SIZE = BinaryDictionary.MAX_WORD_LENGTH;
|
|
||||||
private int[] mArray;
|
private int[] mArray;
|
||||||
private int mLength;
|
private int mLength;
|
||||||
|
|
||||||
public ScalableIntArray() {
|
public ScalableIntArray(int capacity) {
|
||||||
reset();
|
reset(capacity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(int index, int val) {
|
public void add(int index, int val) {
|
||||||
|
@ -136,8 +145,8 @@ public class InputPointers {
|
||||||
return mLength;
|
return mLength;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void reset() {
|
public void reset(int capacity) {
|
||||||
mArray = new int[DEFAULT_SIZE];
|
mArray = new int[capacity];
|
||||||
mLength = 0;
|
mLength = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class LastComposedWord {
|
||||||
public final String mCommittedWord;
|
public final String mCommittedWord;
|
||||||
public final int mSeparatorCode;
|
public final int mSeparatorCode;
|
||||||
public final CharSequence mPrevWord;
|
public final CharSequence mPrevWord;
|
||||||
public final InputPointers mInputPointers = new InputPointers();
|
public final InputPointers mInputPointers = new InputPointers(BinaryDictionary.MAX_WORD_LENGTH);
|
||||||
|
|
||||||
private boolean mActive;
|
private boolean mActive;
|
||||||
|
|
||||||
|
|
|
@ -33,7 +33,7 @@ public class WordComposer {
|
||||||
private static final int N = BinaryDictionary.MAX_WORD_LENGTH;
|
private static final int N = BinaryDictionary.MAX_WORD_LENGTH;
|
||||||
|
|
||||||
private int[] mPrimaryKeyCodes;
|
private int[] mPrimaryKeyCodes;
|
||||||
private final InputPointers mInputPointers = new InputPointers();
|
private final InputPointers mInputPointers = new InputPointers(N);
|
||||||
private final StringBuilder mTypedWord;
|
private final StringBuilder mTypedWord;
|
||||||
private CharSequence mAutoCorrection;
|
private CharSequence mAutoCorrection;
|
||||||
private boolean mIsResumed;
|
private boolean mIsResumed;
|
||||||
|
|
|
@ -19,8 +19,10 @@ package com.android.inputmethod.latin;
|
||||||
import android.test.AndroidTestCase;
|
import android.test.AndroidTestCase;
|
||||||
|
|
||||||
public class InputPointersTests extends AndroidTestCase {
|
public class InputPointersTests extends AndroidTestCase {
|
||||||
|
private static final int DEFAULT_CAPACITY = 48;
|
||||||
|
|
||||||
public void testNewInstance() {
|
public void testNewInstance() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
assertEquals("newInstance size", 0, src.getPointerSize());
|
assertEquals("newInstance size", 0, src.getPointerSize());
|
||||||
assertNotNull("new instance xCoordinates", src.getXCoordinates());
|
assertNotNull("new instance xCoordinates", src.getXCoordinates());
|
||||||
assertNotNull("new instance yCoordinates", src.getYCoordinates());
|
assertNotNull("new instance yCoordinates", src.getYCoordinates());
|
||||||
|
@ -29,7 +31,7 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReset() {
|
public void testReset() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int[] xCoordinates = src.getXCoordinates();
|
final int[] xCoordinates = src.getXCoordinates();
|
||||||
final int[] yCoordinates = src.getXCoordinates();
|
final int[] yCoordinates = src.getXCoordinates();
|
||||||
final int[] pointerIds = src.getXCoordinates();
|
final int[] pointerIds = src.getXCoordinates();
|
||||||
|
@ -44,7 +46,7 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAdd() {
|
public void testAdd() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int limit = src.getXCoordinates().length * 2 + 10;
|
final int limit = src.getXCoordinates().length * 2 + 10;
|
||||||
for (int i = 0; i < limit; i++) {
|
for (int i = 0; i < limit; i++) {
|
||||||
src.addPointer(i, i * 2, i * 3, i * 4);
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
||||||
|
@ -59,7 +61,7 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAddAt() {
|
public void testAddAt() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int limit = 1000, step = 100;
|
final int limit = 1000, step = 100;
|
||||||
for (int i = 0; i < limit; i += step) {
|
for (int i = 0; i < limit; i += step) {
|
||||||
src.addPointer(i, i, i * 2, i * 3, i * 4);
|
src.addPointer(i, i, i * 2, i * 3, i * 4);
|
||||||
|
@ -74,12 +76,12 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSet() {
|
public void testSet() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int limit = src.getXCoordinates().length * 2 + 10;
|
final int limit = src.getXCoordinates().length * 2 + 10;
|
||||||
for (int i = 0; i < limit; i++) {
|
for (int i = 0; i < limit; i++) {
|
||||||
src.addPointer(i, i * 2, i * 3, i * 4);
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
||||||
}
|
}
|
||||||
final InputPointers dst = new InputPointers();
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
||||||
dst.set(src);
|
dst.set(src);
|
||||||
assertEquals("after set size", dst.getPointerSize(), src.getPointerSize());
|
assertEquals("after set size", dst.getPointerSize(), src.getPointerSize());
|
||||||
assertSame("after set xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
|
assertSame("after set xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
|
||||||
|
@ -89,12 +91,12 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCopy() {
|
public void testCopy() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int limit = 100;
|
final int limit = 100;
|
||||||
for (int i = 0; i < limit; i++) {
|
for (int i = 0; i < limit; i++) {
|
||||||
src.addPointer(i, i * 2, i * 3, i * 4);
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
||||||
}
|
}
|
||||||
final InputPointers dst = new InputPointers();
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
||||||
dst.copy(src);
|
dst.copy(src);
|
||||||
assertEquals("after copy size", dst.getPointerSize(), src.getPointerSize());
|
assertEquals("after copy size", dst.getPointerSize(), src.getPointerSize());
|
||||||
assertNotSame("after copy xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
|
assertNotSame("after copy xCoordinates", dst.getXCoordinates(), src.getXCoordinates());
|
||||||
|
@ -113,18 +115,18 @@ public class InputPointersTests extends AndroidTestCase {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testAppend() {
|
public void testAppend() {
|
||||||
final InputPointers src = new InputPointers();
|
final InputPointers src = new InputPointers(DEFAULT_CAPACITY);
|
||||||
final int srcLen = 100;
|
final int srcLen = 100;
|
||||||
for (int i = 0; i < srcLen; i++) {
|
for (int i = 0; i < srcLen; i++) {
|
||||||
src.addPointer(i, i * 2, i * 3, i * 4);
|
src.addPointer(i, i * 2, i * 3, i * 4);
|
||||||
}
|
}
|
||||||
final int dstLen = 50;
|
final int dstLen = 50;
|
||||||
final InputPointers dst = new InputPointers();
|
final InputPointers dst = new InputPointers(DEFAULT_CAPACITY);
|
||||||
for (int i = 0; i < dstLen; i++) {
|
for (int i = 0; i < dstLen; i++) {
|
||||||
final int value = -i - 1;
|
final int value = -i - 1;
|
||||||
dst.addPointer(value * 4, value * 3, value * 2, value);
|
dst.addPointer(value * 4, value * 3, value * 2, value);
|
||||||
}
|
}
|
||||||
final InputPointers dstCopy = new InputPointers();
|
final InputPointers dstCopy = new InputPointers(DEFAULT_CAPACITY);
|
||||||
dstCopy.copy(dst);
|
dstCopy.copy(dst);
|
||||||
|
|
||||||
dst.append(src, 0, 0);
|
dst.append(src, 0, 0);
|
||||||
|
|
Loading…
Reference in a new issue