From 1087c53f5a6ced093eb4e71f88cff19e89819d73 Mon Sep 17 00:00:00 2001 From: "Tadashi G. Takaoka" <takaoka@google.com> Date: Mon, 9 Jul 2012 16:40:13 +0900 Subject: [PATCH] Add InputPointers.append() method Change-Id: Id2b0e242f7e9ce0ca888067857f8b64c02fde84e --- .../inputmethod/latin/InputPointers.java | 47 +++++++++++++++---- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/java/src/com/android/inputmethod/latin/InputPointers.java b/java/src/com/android/inputmethod/latin/InputPointers.java index 218243e9f..9d77d4e96 100644 --- a/java/src/com/android/inputmethod/latin/InputPointers.java +++ b/java/src/com/android/inputmethod/latin/InputPointers.java @@ -18,6 +18,7 @@ package com.android.inputmethod.latin; import java.util.Arrays; +// TODO: Add unit test public class InputPointers { private final ScalableIntArray mXCoordinates = new ScalableIntArray(); private final ScalableIntArray mYCoordinates = new ScalableIntArray(); @@ -52,6 +53,25 @@ public class InputPointers { mTimes.copy(ip.mTimes); } + /** + * Append the pointers in the specified {@link InputPointers} to the end of this. + * @param src the source {@link InputPointers} to append the pointers. + * @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); + } + public void reset() { mXCoordinates.reset(); mYCoordinates.reset(); @@ -64,19 +84,19 @@ public class InputPointers { } public int[] getXCoordinates() { - return mXCoordinates.mArray; + return mXCoordinates.getPrimitiveArray(); } public int[] getYCoordinates() { - return mYCoordinates.mArray; + return mYCoordinates.getPrimitiveArray(); } public int[] getPointerIds() { - return mPointerIds.mArray; + return mPointerIds.getPrimitiveArray(); } public int[] getTimes() { - return mTimes.mArray; + return mTimes.getPrimitiveArray(); } private static class ScalableIntArray { @@ -98,14 +118,24 @@ public class InputPointers { } public void add(int val) { - if (mLength >= mArray.length) { - final int[] newArray = new int[mLength * 2]; - System.arraycopy(mArray, 0, newArray, 0, mLength); - } + ensureCapacity(mLength); mArray[mLength] = val; ++mLength; } + public void ensureCapacity(int minimumCapacity) { + if (mArray.length < minimumCapacity) { + final int nextCapacity = mArray.length * 2; + grow(minimumCapacity > nextCapacity ? minimumCapacity : nextCapacity); + } + } + + private void grow(int newCapacity) { + final int[] newArray = new int[newCapacity]; + System.arraycopy(mArray, 0, newArray, 0, mLength); + mArray = newArray; + } + public int getLength() { return mLength; } @@ -121,6 +151,7 @@ public class InputPointers { public void copy(ScalableIntArray ip) { mArray = Arrays.copyOf(ip.mArray, ip.mArray.length); + mLength = ip.mLength; } public void set(ScalableIntArray ip) {