Skip to content
Snippets Groups Projects
Commit 7247bff6 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka
Browse files

Fix InputPointers.append

Change-Id: I6995f9b2ed00b9f948e1299e576a5e24725d58f8
parent fa2287f8
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import java.util.Arrays;
// TODO: This class is not thread-safe.
public class InputPointers { public class InputPointers {
private final ScalableIntArray mXCoordinates = new ScalableIntArray(); private final ScalableIntArray mXCoordinates = new ScalableIntArray();
private final ScalableIntArray mYCoordinates = new ScalableIntArray(); private final ScalableIntArray mYCoordinates = new ScalableIntArray();
...@@ -52,21 +55,15 @@ public class InputPointers { ...@@ -52,21 +55,15 @@ public class InputPointers {
/** /**
* Append the pointers in the specified {@link InputPointers} to the end of this. * Append the pointers in the specified {@link InputPointers} to the end of this.
* @param src the source {@link InputPointers} to append the pointers. * @param src the source {@link InputPointers} to read the data from.
* @param startPos the starting index of the pointers in {@code src}. * @param startPos the starting index of the pointers in {@code src}.
* @param length the number of pointers to be appended. * @param length the number of pointers to be appended.
*/ */
public void append(InputPointers src, int startPos, int length) { public void append(InputPointers src, int startPos, int length) {
final int currentLength = getPointerSize(); mXCoordinates.append(src.mXCoordinates, startPos, length);
final int newLength = currentLength + length; mYCoordinates.append(src.mYCoordinates, startPos, length);
mXCoordinates.ensureCapacity(newLength); mPointerIds.append(src.mPointerIds, startPos, length);
mYCoordinates.ensureCapacity(newLength); mTimes.append(src.mTimes, startPos, length);
mPointerIds.ensureCapacity(newLength);
mTimes.ensureCapacity(newLength);
System.arraycopy(src.getXCoordinates(), startPos, getXCoordinates(), currentLength, length);
System.arraycopy(src.getYCoordinates(), startPos, getYCoordinates(), currentLength, length);
System.arraycopy(src.getPointerIds(), startPos, getPointerIds(), currentLength, length);
System.arraycopy(src.getTimes(), startPos, getTimes(), currentLength, length);
} }
public void reset() { public void reset() {
...@@ -121,19 +118,17 @@ public class InputPointers { ...@@ -121,19 +118,17 @@ public class InputPointers {
mLength = nextLength; mLength = nextLength;
} }
public void ensureCapacity(int minimumCapacity) { private void ensureCapacity(int minimumCapacity) {
if (mArray.length < minimumCapacity) { if (mArray.length < minimumCapacity) {
final int nextCapacity = mArray.length * 2; final int nextCapacity = mArray.length * 2;
grow(minimumCapacity > nextCapacity ? minimumCapacity : nextCapacity); // The following is the same as newLength = Math.max(minimumCapacity, nextCapacity);
final int newLength = minimumCapacity > nextCapacity
? minimumCapacity
: nextCapacity;
mArray = Arrays.copyOf(mArray, newLength);
} }
} }
private void grow(int newCapacity) {
final int[] newArray = new int[newCapacity];
System.arraycopy(mArray, 0, newArray, 0, mArray.length);
mArray = newArray;
}
public int getLength() { public int getLength() {
return mLength; return mLength;
} }
...@@ -147,15 +142,23 @@ public class InputPointers { ...@@ -147,15 +142,23 @@ public class InputPointers {
return mArray; return mArray;
} }
public void set(ScalableIntArray ip) {
mArray = ip.mArray;
mLength = ip.mLength;
}
public void copy(ScalableIntArray ip) { public void copy(ScalableIntArray ip) {
ensureCapacity(ip.mLength); ensureCapacity(ip.mLength);
System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength); System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength);
mLength = ip.mLength; mLength = ip.mLength;
} }
public void set(ScalableIntArray ip) { public void append(ScalableIntArray src, int startPos, int length) {
mArray = ip.mArray; final int currentLength = mLength;
mLength = ip.mLength; final int newLength = currentLength + length;
ensureCapacity(newLength);
System.arraycopy(src.mArray, startPos, mArray, currentLength, length);
mLength = newLength;
} }
} }
} }
...@@ -114,47 +114,50 @@ public class InputPointersTests extends AndroidTestCase { ...@@ -114,47 +114,50 @@ public class InputPointersTests extends AndroidTestCase {
public void testAppend() { public void testAppend() {
final InputPointers src = new InputPointers(); final InputPointers src = new InputPointers();
final int limit = 100; final int srcLen = 100;
for (int i = 0; i < limit; 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 InputPointers dst = new InputPointers(); final InputPointers dst = new InputPointers();
for (int i = 0; i < limit; i++) { for (int i = 0; i < dstLen; i++) {
final int value = limit - i; 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();
dstCopy.copy(dst); dstCopy.copy(dst);
dst.append(src, 0, 0); dst.append(src, 0, 0);
assertEquals("after append zero size", limit, dst.getPointerSize()); assertEquals("after append zero size", dstLen, dst.getPointerSize());
assertArrayEquals("affer append zero xCoordinates", dstCopy.getXCoordinates(), 0, assertArrayEquals("after append zero xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getXCoordinates(), 0, limit); dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("affer append zero yCoordinates", dstCopy.getYCoordinates(), 0, assertArrayEquals("after append zero yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getYCoordinates(), 0, limit); dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("affer append zero pointerIds", dstCopy.getPointerIds(), 0, assertArrayEquals("after append zero pointerIds", dstCopy.getPointerIds(), 0,
dst.getPointerIds(), 0, limit); dst.getPointerIds(), 0, dstLen);
assertArrayEquals("affer append zero times", dstCopy.getTimes(), 0, assertArrayEquals("after append zero times", dstCopy.getTimes(), 0,
dst.getTimes(), 0, limit); dst.getTimes(), 0, dstLen);
dst.append(src, 0, src.getPointerSize()); dst.append(src, 0, srcLen);
assertEquals("after append size", limit * 2, dst.getPointerSize() + src.getPointerSize()); assertEquals("after append size", dstLen + srcLen, dst.getPointerSize());
assertArrayEquals("affer append xCoordinates", dstCopy.getXCoordinates(), 0, assertTrue("after append size primitive length",
dst.getXCoordinates(), 0, limit); dst.getPointerIds().length >= dstLen + srcLen);
assertArrayEquals("affer append yCoordinates", dstCopy.getYCoordinates(), 0, assertArrayEquals("after append xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getYCoordinates(), 0, limit); dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("affer append pointerIds", dstCopy.getPointerIds(), 0, assertArrayEquals("after append yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getPointerIds(), 0, limit); dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("affer append times", dstCopy.getTimes(), 0, assertArrayEquals("after append pointerIds", dstCopy.getPointerIds(), 0,
dst.getTimes(), 0, limit); dst.getPointerIds(), 0, dstLen);
assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), limit, assertArrayEquals("after append times", dstCopy.getTimes(), 0,
src.getXCoordinates(), 0, limit); dst.getTimes(), 0, dstLen);
assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), limit, assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), dstLen,
src.getYCoordinates(), 0, limit); src.getXCoordinates(), 0, srcLen);
assertArrayEquals("after append pointerIds", dst.getPointerIds(), limit, assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), dstLen,
src.getPointerIds(), 0, limit); src.getYCoordinates(), 0, srcLen);
assertArrayEquals("after append times", dst.getTimes(), limit, assertArrayEquals("after append pointerIds", dst.getPointerIds(), dstLen,
src.getTimes(), 0, limit); src.getPointerIds(), 0, srcLen);
assertArrayEquals("after append times", dst.getTimes(), dstLen,
src.getTimes(), 0, srcLen);
} }
private static void assertArrayEquals(String message, int[] expecteds, int expectedPos, private static void assertArrayEquals(String message, int[] expecteds, int expectedPos,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment