Fix InputPointers.append

Change-Id: I6995f9b2ed00b9f948e1299e576a5e24725d58f8
main
Tadashi G. Takaoka 2012-07-13 11:17:25 +09:00
parent fa2287f86c
commit 7247bff6d6
2 changed files with 59 additions and 53 deletions

View File

@ -16,6 +16,9 @@
package com.android.inputmethod.latin;
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();
@ -52,21 +55,15 @@ public class InputPointers {
/**
* 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 length the number of pointers to be appended.
*/
public void append(InputPointers src, int startPos, int length) {
final int currentLength = getPointerSize();
final int newLength = currentLength + length;
mXCoordinates.ensureCapacity(newLength);
mYCoordinates.ensureCapacity(newLength);
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);
mXCoordinates.append(src.mXCoordinates, startPos, length);
mYCoordinates.append(src.mYCoordinates, startPos, length);
mPointerIds.append(src.mPointerIds, startPos, length);
mTimes.append(src.mTimes, startPos, length);
}
public void reset() {
@ -121,19 +118,17 @@ public class InputPointers {
mLength = nextLength;
}
public void ensureCapacity(int minimumCapacity) {
private void ensureCapacity(int minimumCapacity) {
if (mArray.length < minimumCapacity) {
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() {
return mLength;
}
@ -147,15 +142,23 @@ public class InputPointers {
return mArray;
}
public void set(ScalableIntArray ip) {
mArray = ip.mArray;
mLength = ip.mLength;
}
public void copy(ScalableIntArray ip) {
ensureCapacity(ip.mLength);
System.arraycopy(ip.mArray, 0, mArray, 0, ip.mLength);
mLength = ip.mLength;
}
public void set(ScalableIntArray ip) {
mArray = ip.mArray;
mLength = ip.mLength;
public void append(ScalableIntArray src, int startPos, int length) {
final int currentLength = mLength;
final int newLength = currentLength + length;
ensureCapacity(newLength);
System.arraycopy(src.mArray, startPos, mArray, currentLength, length);
mLength = newLength;
}
}
}

View File

@ -114,47 +114,50 @@ public class InputPointersTests extends AndroidTestCase {
public void testAppend() {
final InputPointers src = new InputPointers();
final int limit = 100;
for (int i = 0; i < limit; i++) {
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();
for (int i = 0; i < limit; i++) {
final int value = limit - i;
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();
dstCopy.copy(dst);
dst.append(src, 0, 0);
assertEquals("after append zero size", limit, dst.getPointerSize());
assertArrayEquals("affer append zero xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getXCoordinates(), 0, limit);
assertArrayEquals("affer append zero yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getYCoordinates(), 0, limit);
assertArrayEquals("affer append zero pointerIds", dstCopy.getPointerIds(), 0,
dst.getPointerIds(), 0, limit);
assertArrayEquals("affer append zero times", dstCopy.getTimes(), 0,
dst.getTimes(), 0, limit);
assertEquals("after append zero size", dstLen, dst.getPointerSize());
assertArrayEquals("after append zero xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("after append zero yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("after append zero pointerIds", dstCopy.getPointerIds(), 0,
dst.getPointerIds(), 0, dstLen);
assertArrayEquals("after append zero times", dstCopy.getTimes(), 0,
dst.getTimes(), 0, dstLen);
dst.append(src, 0, src.getPointerSize());
assertEquals("after append size", limit * 2, dst.getPointerSize() + src.getPointerSize());
assertArrayEquals("affer append xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getXCoordinates(), 0, limit);
assertArrayEquals("affer append yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getYCoordinates(), 0, limit);
assertArrayEquals("affer append pointerIds", dstCopy.getPointerIds(), 0,
dst.getPointerIds(), 0, limit);
assertArrayEquals("affer append times", dstCopy.getTimes(), 0,
dst.getTimes(), 0, limit);
assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), limit,
src.getXCoordinates(), 0, limit);
assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), limit,
src.getYCoordinates(), 0, limit);
assertArrayEquals("after append pointerIds", dst.getPointerIds(), limit,
src.getPointerIds(), 0, limit);
assertArrayEquals("after append times", dst.getTimes(), limit,
src.getTimes(), 0, limit);
dst.append(src, 0, srcLen);
assertEquals("after append size", dstLen + srcLen, dst.getPointerSize());
assertTrue("after append size primitive length",
dst.getPointerIds().length >= dstLen + srcLen);
assertArrayEquals("after append xCoordinates", dstCopy.getXCoordinates(), 0,
dst.getXCoordinates(), 0, dstLen);
assertArrayEquals("after append yCoordinates", dstCopy.getYCoordinates(), 0,
dst.getYCoordinates(), 0, dstLen);
assertArrayEquals("after append pointerIds", dstCopy.getPointerIds(), 0,
dst.getPointerIds(), 0, dstLen);
assertArrayEquals("after append times", dstCopy.getTimes(), 0,
dst.getTimes(), 0, dstLen);
assertArrayEquals("after append xCoordinates", dst.getXCoordinates(), dstLen,
src.getXCoordinates(), 0, srcLen);
assertArrayEquals("after append yCoordinates", dst.getYCoordinates(), dstLen,
src.getYCoordinates(), 0, srcLen);
assertArrayEquals("after append pointerIds", dst.getPointerIds(), dstLen,
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,