summaryrefslogtreecommitdiff
path: root/core/java/android/util/SparseLongArray.java
diff options
context:
space:
mode:
authorDmitri Plotnikov <dplotnikov@google.com>2022-01-19 15:52:19 -0800
committerDmitri Plotnikov <dplotnikov@google.com>2022-01-19 18:20:29 -0800
commitbb925fb03422332b3f8fdcc5519a0b85ced0be7a (patch)
tree75c731245926d351931abbbfed861c9f2fd2a6c2 /core/java/android/util/SparseLongArray.java
parent2f473a976725ff8b5111d6f0cf8a3aabadae460e (diff)
Add SparseLongArray.incrementValue()
This method is used for accumulation of long values: if the key does not exist, one is created. If one already exists, the passed value is added to the previous value. This method is similar to the existing SparseDoubleArray.incrementValue() method Bug: 215427055 Test: atest FrameworksCoreTests:SparseLongArrayTest Change-Id: I771d9bc2bdd12ca4260b20186ced70f5862b6f56
Diffstat (limited to 'core/java/android/util/SparseLongArray.java')
-rw-r--r--core/java/android/util/SparseLongArray.java24
1 files changed, 24 insertions, 0 deletions
diff --git a/core/java/android/util/SparseLongArray.java b/core/java/android/util/SparseLongArray.java
index f2bc0c5a34d6..7185972b85bf 100644
--- a/core/java/android/util/SparseLongArray.java
+++ b/core/java/android/util/SparseLongArray.java
@@ -164,6 +164,30 @@ public class SparseLongArray implements Cloneable {
}
/**
+ * Adds a mapping from the specified key to the specified value,
+ * <b>adding</b> its value to the previous mapping from the specified key if there
+ * was one.
+ *
+ * <p>This differs from {@link #put} because instead of replacing any previous value, it adds
+ * (in the numerical sense) to it.
+ *
+ * @hide
+ */
+ public void incrementValue(int key, long summand) {
+ int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
+
+ if (i >= 0) {
+ mValues[i] += summand;
+ } else {
+ i = ~i;
+
+ mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
+ mValues = GrowingArrayUtils.insert(mValues, mSize, i, summand);
+ mSize++;
+ }
+ }
+
+ /**
* Returns the number of key-value mappings that this SparseLongArray
* currently stores.
*/