summaryrefslogtreecommitdiff
path: root/core/java/android/util/LongSparseArray.java
diff options
context:
space:
mode:
authorDianne Hackborn <hackbod@google.com>2013-07-16 13:23:55 -0700
committerDianne Hackborn <hackbod@google.com>2013-07-16 17:08:04 -0700
commit3e82ba1a67b0c756ab6a289985f4cfc53725b311 (patch)
tree1683ef4d66151ff27b7afe1c7257abceee69654b /core/java/android/util/LongSparseArray.java
parentcdae0f3c608ed5aee5ddbdf9c54ac86a5619e64b (diff)
Make ArrayMap public! :)
Also do some tweaking of the various container classes to synchronize them with the support lib and make it easier to copy code between the two. And update activity/fragment to use ArrayMap. Change-Id: I3cfe82392a17119dfc72c3d9961f64e1914f42be
Diffstat (limited to 'core/java/android/util/LongSparseArray.java')
-rw-r--r--core/java/android/util/LongSparseArray.java59
1 files changed, 36 insertions, 23 deletions
diff --git a/core/java/android/util/LongSparseArray.java b/core/java/android/util/LongSparseArray.java
index 77dfcf46bed5..63e8c25d278b 100644
--- a/core/java/android/util/LongSparseArray.java
+++ b/core/java/android/util/LongSparseArray.java
@@ -64,8 +64,8 @@ public class LongSparseArray<E> implements Cloneable {
*/
public LongSparseArray(int initialCapacity) {
if (initialCapacity == 0) {
- mKeys = SparseLongArray.EMPTY_LONGS;
- mValues = SparseArray.EMPTY_OBJECTS;
+ mKeys = ContainerHelpers.EMPTY_LONGS;
+ mValues = ContainerHelpers.EMPTY_OBJECTS;
} else {
initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity);
mKeys = new long[initialCapacity];
@@ -102,7 +102,7 @@ public class LongSparseArray<E> implements Cloneable {
*/
@SuppressWarnings("unchecked")
public E get(long key, E valueIfKeyNotFound) {
- int i = binarySearch(mKeys, 0, mSize, key);
+ int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i < 0 || mValues[i] == DELETED) {
return valueIfKeyNotFound;
@@ -115,7 +115,7 @@ public class LongSparseArray<E> implements Cloneable {
* Removes the mapping from the specified key, if there was any.
*/
public void delete(long key) {
- int i = binarySearch(mKeys, 0, mSize, key);
+ int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
if (mValues[i] != DELETED) {
@@ -176,7 +176,7 @@ public class LongSparseArray<E> implements Cloneable {
* was one.
*/
public void put(long key, E value) {
- int i = binarySearch(mKeys, 0, mSize, key);
+ int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
if (i >= 0) {
mValues[i] = value;
@@ -193,7 +193,7 @@ public class LongSparseArray<E> implements Cloneable {
gc();
// Search again because indices may have changed.
- i = ~binarySearch(mKeys, 0, mSize, key);
+ i = ~ContainerHelpers.binarySearch(mKeys, mSize, key);
}
if (mSize >= mKeys.length) {
@@ -284,7 +284,7 @@ public class LongSparseArray<E> implements Cloneable {
gc();
}
- return binarySearch(mKeys, 0, mSize, key);
+ return ContainerHelpers.binarySearch(mKeys, mSize, key);
}
/**
@@ -356,23 +356,36 @@ public class LongSparseArray<E> implements Cloneable {
mSize = pos + 1;
}
- private static int binarySearch(long[] a, int start, int len, long key) {
- int high = start + len, low = start - 1, guess;
-
- while (high - low > 1) {
- guess = (high + low) / 2;
-
- if (a[guess] < key)
- low = guess;
- else
- high = guess;
+ /**
+ * {@inheritDoc}
+ *
+ * <p>This implementation composes a string by iterating over its mappings. If
+ * this map contains itself as a value, the string "(this Map)"
+ * will appear in its place.
+ */
+ @Override
+ public String toString() {
+ if (size() <= 0) {
+ return "{}";
}
- if (high == start + len)
- return ~(start + len);
- else if (a[high] == key)
- return high;
- else
- return ~high;
+ StringBuilder buffer = new StringBuilder(mSize * 28);
+ buffer.append('{');
+ for (int i=0; i<mSize; i++) {
+ if (i > 0) {
+ buffer.append(", ");
+ }
+ long key = keyAt(i);
+ buffer.append(key);
+ buffer.append('=');
+ Object value = valueAt(i);
+ if (value != this) {
+ buffer.append(value);
+ } else {
+ buffer.append("(this Map)");
+ }
+ }
+ buffer.append('}');
+ return buffer.toString();
}
}