Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.util; |
| 18 | |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 19 | import android.os.Parcel; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 20 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 21 | import com.android.internal.util.ArrayUtils; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 22 | import com.android.internal.util.GrowingArrayUtils; |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 23 | import com.android.internal.util.Preconditions; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 24 | |
| 25 | import libcore.util.EmptyArray; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 26 | |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 27 | import java.util.Arrays; |
| 28 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 29 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 30 | * SparseArray mapping longs to Objects. Unlike a normal array of Objects, |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 31 | * there can be gaps in the indices. It is intended to be more memory efficient |
| 32 | * than using a HashMap to map Longs to Objects, both because it avoids |
| 33 | * auto-boxing keys and its data structure doesn't rely on an extra entry object |
| 34 | * for each mapping. |
| 35 | * |
| 36 | * <p>Note that this container keeps its mappings in an array data structure, |
| 37 | * using a binary search to find keys. The implementation is not intended to be appropriate for |
| 38 | * data structures |
| 39 | * that may contain large numbers of items. It is generally slower than a traditional |
| 40 | * HashMap, since lookups require a binary search and adds and removes require inserting |
| 41 | * and deleting entries in the array. For containers holding up to hundreds of items, |
| 42 | * the performance difference is not significant, less than 50%.</p> |
| 43 | * |
| 44 | * <p>To help with performance, the container includes an optimization when removing |
| 45 | * keys: instead of compacting its array immediately, it leaves the removed entry marked |
| 46 | * as deleted. The entry can then be re-used for the same key, or compacted later in |
| 47 | * a single garbage collection step of all removed entries. This garbage collection will |
| 48 | * need to be performed at any time the array needs to be grown or the the map size or |
| 49 | * entry values are retrieved.</p> |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 50 | * |
| 51 | * <p>It is possible to iterate over the items in this container using |
| 52 | * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using |
| 53 | * <code>keyAt(int)</code> with ascending values of the index will return the |
| 54 | * keys in ascending order, or the values corresponding to the keys in ascending |
Newton Allen | ebb4795 | 2013-11-21 13:27:10 -0800 | [diff] [blame] | 55 | * order in the case of <code>valueAt(int)</code>.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 56 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 57 | public class LongSparseArray<E> implements Cloneable { |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 58 | private static final Object DELETED = new Object(); |
| 59 | private boolean mGarbage = false; |
| 60 | |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 61 | private long[] mKeys; |
| 62 | private Object[] mValues; |
| 63 | private int mSize; |
| 64 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 65 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 66 | * Creates a new LongSparseArray containing no mappings. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 67 | */ |
| 68 | public LongSparseArray() { |
| 69 | this(10); |
| 70 | } |
| 71 | |
| 72 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 73 | * Creates a new LongSparseArray containing no mappings that will not |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 74 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 75 | * number of mappings. If you supply an initial capacity of 0, the |
| 76 | * sparse array will be initialized with a light-weight representation |
| 77 | * not requiring any additional array allocations. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 78 | */ |
| 79 | public LongSparseArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 80 | if (initialCapacity == 0) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 81 | mKeys = EmptyArray.LONG; |
| 82 | mValues = EmptyArray.OBJECT; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 83 | } else { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 84 | mKeys = ArrayUtils.newUnpaddedLongArray(initialCapacity); |
| 85 | mValues = ArrayUtils.newUnpaddedObjectArray(initialCapacity); |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 86 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 87 | mSize = 0; |
| 88 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 89 | |
| 90 | @Override |
| 91 | @SuppressWarnings("unchecked") |
| 92 | public LongSparseArray<E> clone() { |
| 93 | LongSparseArray<E> clone = null; |
| 94 | try { |
| 95 | clone = (LongSparseArray<E>) super.clone(); |
| 96 | clone.mKeys = mKeys.clone(); |
| 97 | clone.mValues = mValues.clone(); |
| 98 | } catch (CloneNotSupportedException cnse) { |
| 99 | /* ignore */ |
Adam Powell | 8f1bfe1 | 2010-03-05 15:13:56 -0800 | [diff] [blame] | 100 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 101 | return clone; |
Adam Powell | 8f1bfe1 | 2010-03-05 15:13:56 -0800 | [diff] [blame] | 102 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 103 | |
| 104 | /** |
| 105 | * Gets the Object mapped from the specified key, or <code>null</code> |
| 106 | * if no such mapping has been made. |
| 107 | */ |
| 108 | public E get(long key) { |
| 109 | return get(key, null); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Gets the Object mapped from the specified key, or the specified Object |
| 114 | * if no such mapping has been made. |
| 115 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 116 | @SuppressWarnings("unchecked") |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 117 | public E get(long key, E valueIfKeyNotFound) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 118 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 119 | |
| 120 | if (i < 0 || mValues[i] == DELETED) { |
| 121 | return valueIfKeyNotFound; |
| 122 | } else { |
| 123 | return (E) mValues[i]; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Removes the mapping from the specified key, if there was any. |
| 129 | */ |
| 130 | public void delete(long key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 131 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 132 | |
| 133 | if (i >= 0) { |
| 134 | if (mValues[i] != DELETED) { |
| 135 | mValues[i] = DELETED; |
| 136 | mGarbage = true; |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Alias for {@link #delete(long)}. |
| 143 | */ |
| 144 | public void remove(long key) { |
| 145 | delete(key); |
| 146 | } |
| 147 | |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 148 | /** |
| 149 | * Removes the mapping at the specified index. |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 150 | * |
| 151 | * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for |
| 152 | * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an |
| 153 | * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting |
| 154 | * {@link android.os.Build.VERSION_CODES#Q} and later.</p> |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 155 | */ |
| 156 | public void removeAt(int index) { |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 157 | if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 158 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
Kweku Adams | 3858b2d | 2019-04-29 11:47:41 -0700 | [diff] [blame] | 159 | // Check if exception should be thrown outside of the critical path. |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 160 | throw new ArrayIndexOutOfBoundsException(index); |
| 161 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 162 | if (mValues[index] != DELETED) { |
| 163 | mValues[index] = DELETED; |
| 164 | mGarbage = true; |
| 165 | } |
| 166 | } |
| 167 | |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 168 | private void gc() { |
| 169 | // Log.e("SparseArray", "gc start with " + mSize); |
| 170 | |
| 171 | int n = mSize; |
| 172 | int o = 0; |
| 173 | long[] keys = mKeys; |
| 174 | Object[] values = mValues; |
| 175 | |
| 176 | for (int i = 0; i < n; i++) { |
| 177 | Object val = values[i]; |
| 178 | |
| 179 | if (val != DELETED) { |
| 180 | if (i != o) { |
| 181 | keys[o] = keys[i]; |
| 182 | values[o] = val; |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 183 | values[i] = null; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | o++; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | mGarbage = false; |
| 191 | mSize = o; |
| 192 | |
| 193 | // Log.e("SparseArray", "gc end with " + mSize); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Adds a mapping from the specified key to the specified value, |
| 198 | * replacing the previous mapping from the specified key if there |
| 199 | * was one. |
| 200 | */ |
| 201 | public void put(long key, E value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 202 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 203 | |
| 204 | if (i >= 0) { |
| 205 | mValues[i] = value; |
| 206 | } else { |
| 207 | i = ~i; |
| 208 | |
| 209 | if (i < mSize && mValues[i] == DELETED) { |
| 210 | mKeys[i] = key; |
| 211 | mValues[i] = value; |
| 212 | return; |
| 213 | } |
| 214 | |
| 215 | if (mGarbage && mSize >= mKeys.length) { |
| 216 | gc(); |
| 217 | |
| 218 | // Search again because indices may have changed. |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 219 | i = ~ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 220 | } |
| 221 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 222 | mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key); |
| 223 | mValues = GrowingArrayUtils.insert(mValues, mSize, i, value); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 224 | mSize++; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 229 | * Returns the number of key-value mappings that this LongSparseArray |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 230 | * currently stores. |
| 231 | */ |
| 232 | public int size() { |
| 233 | if (mGarbage) { |
| 234 | gc(); |
| 235 | } |
| 236 | |
| 237 | return mSize; |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * Given an index in the range <code>0...size()-1</code>, returns |
| 242 | * the key from the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 243 | * LongSparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 244 | * |
| 245 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 246 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 247 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 248 | * key.</p> |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 249 | * |
| 250 | * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for |
| 251 | * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an |
| 252 | * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting |
| 253 | * {@link android.os.Build.VERSION_CODES#Q} and later.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 254 | */ |
| 255 | public long keyAt(int index) { |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 256 | if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 257 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
Kweku Adams | 3858b2d | 2019-04-29 11:47:41 -0700 | [diff] [blame] | 258 | // Check if exception should be thrown outside of the critical path. |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 259 | throw new ArrayIndexOutOfBoundsException(index); |
| 260 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 261 | if (mGarbage) { |
| 262 | gc(); |
| 263 | } |
| 264 | |
| 265 | return mKeys[index]; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Given an index in the range <code>0...size()-1</code>, returns |
| 270 | * the value from the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 271 | * LongSparseArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 272 | * |
| 273 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 274 | * to be associated with keys in ascending order, e.g., |
| 275 | * <code>valueAt(0)</code> will return the value associated with the |
| 276 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 277 | * associated with the largest key.</p> |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 278 | * |
| 279 | * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for |
| 280 | * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an |
| 281 | * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting |
| 282 | * {@link android.os.Build.VERSION_CODES#Q} and later.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 283 | */ |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 284 | @SuppressWarnings("unchecked") |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 285 | public E valueAt(int index) { |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 286 | if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 287 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
Kweku Adams | 3858b2d | 2019-04-29 11:47:41 -0700 | [diff] [blame] | 288 | // Check if exception should be thrown outside of the critical path. |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 289 | throw new ArrayIndexOutOfBoundsException(index); |
| 290 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 291 | if (mGarbage) { |
| 292 | gc(); |
| 293 | } |
| 294 | |
| 295 | return (E) mValues[index]; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Given an index in the range <code>0...size()-1</code>, sets a new |
| 300 | * value for the <code>index</code>th key-value mapping that this |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 301 | * LongSparseArray stores. |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 302 | * |
| 303 | * <p>For indices outside of the range <code>0...size()-1</code>, the behavior is undefined for |
| 304 | * apps targeting {@link android.os.Build.VERSION_CODES#P} and earlier, and an |
| 305 | * {@link ArrayIndexOutOfBoundsException} is thrown for apps targeting |
| 306 | * {@link android.os.Build.VERSION_CODES#Q} and later.</p> |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 307 | */ |
| 308 | public void setValueAt(int index, E value) { |
Kweku Adams | 4be0b1a | 2019-04-25 16:16:34 -0700 | [diff] [blame] | 309 | if (index >= mSize && UtilConfig.sThrowExceptionForUpperArrayOutOfBounds) { |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 310 | // The array might be slightly bigger than mSize, in which case, indexing won't fail. |
Kweku Adams | 3858b2d | 2019-04-29 11:47:41 -0700 | [diff] [blame] | 311 | // Check if exception should be thrown outside of the critical path. |
Kweku Adams | 025a166 | 2019-03-30 00:03:17 +0000 | [diff] [blame] | 312 | throw new ArrayIndexOutOfBoundsException(index); |
| 313 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 314 | if (mGarbage) { |
| 315 | gc(); |
| 316 | } |
| 317 | |
| 318 | mValues[index] = value; |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Returns the index for which {@link #keyAt} would return the |
| 323 | * specified key, or a negative number if the specified |
| 324 | * key is not mapped. |
| 325 | */ |
| 326 | public int indexOfKey(long key) { |
| 327 | if (mGarbage) { |
| 328 | gc(); |
| 329 | } |
| 330 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 331 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | /** |
| 335 | * Returns an index for which {@link #valueAt} would return the |
| 336 | * specified key, or a negative number if no keys map to the |
| 337 | * specified value. |
| 338 | * Beware that this is a linear search, unlike lookups by key, |
| 339 | * and that multiple keys can map to the same value and this will |
| 340 | * find only one of them. |
| 341 | */ |
| 342 | public int indexOfValue(E value) { |
| 343 | if (mGarbage) { |
| 344 | gc(); |
| 345 | } |
| 346 | |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 347 | for (int i = 0; i < mSize; i++) { |
| 348 | if (mValues[i] == value) { |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 349 | return i; |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | return -1; |
| 353 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 354 | |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 355 | /** |
| 356 | * Returns an index for which {@link #valueAt} would return the |
| 357 | * specified key, or a negative number if no keys map to the |
| 358 | * specified value. |
| 359 | * <p>Beware that this is a linear search, unlike lookups by key, |
| 360 | * and that multiple keys can map to the same value and this will |
| 361 | * find only one of them. |
| 362 | * <p>Note also that this method uses {@code equals} unlike {@code indexOfValue}. |
Suprabh Shukla | 0693ff6 | 2017-03-23 16:09:27 -0700 | [diff] [blame] | 363 | * @hide |
Tejas Khorana | fb5166d | 2016-07-18 11:18:27 -0700 | [diff] [blame] | 364 | */ |
| 365 | public int indexOfValueByValue(E value) { |
| 366 | if (mGarbage) { |
| 367 | gc(); |
| 368 | } |
| 369 | |
| 370 | for (int i = 0; i < mSize; i++) { |
| 371 | if (value == null) { |
| 372 | if (mValues[i] == null) { |
| 373 | return i; |
| 374 | } |
| 375 | } else { |
| 376 | if (value.equals(mValues[i])) { |
| 377 | return i; |
| 378 | } |
| 379 | } |
| 380 | } |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 381 | return -1; |
| 382 | } |
| 383 | |
| 384 | /** |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 385 | * Removes all key-value mappings from this LongSparseArray. |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 386 | */ |
| 387 | public void clear() { |
| 388 | int n = mSize; |
| 389 | Object[] values = mValues; |
| 390 | |
| 391 | for (int i = 0; i < n; i++) { |
| 392 | values[i] = null; |
| 393 | } |
| 394 | |
| 395 | mSize = 0; |
| 396 | mGarbage = false; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * Puts a key/value pair into the array, optimizing for the case where |
| 401 | * the key is greater than all existing keys in the array. |
| 402 | */ |
| 403 | public void append(long key, E value) { |
| 404 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 405 | put(key, value); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | if (mGarbage && mSize >= mKeys.length) { |
| 410 | gc(); |
| 411 | } |
| 412 | |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 413 | mKeys = GrowingArrayUtils.append(mKeys, mSize, key); |
| 414 | mValues = GrowingArrayUtils.append(mValues, mSize, value); |
| 415 | mSize++; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 418 | /** |
| 419 | * {@inheritDoc} |
| 420 | * |
| 421 | * <p>This implementation composes a string by iterating over its mappings. If |
| 422 | * this map contains itself as a value, the string "(this Map)" |
| 423 | * will appear in its place. |
| 424 | */ |
| 425 | @Override |
| 426 | public String toString() { |
| 427 | if (size() <= 0) { |
| 428 | return "{}"; |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 431 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 432 | buffer.append('{'); |
| 433 | for (int i=0; i<mSize; i++) { |
| 434 | if (i > 0) { |
| 435 | buffer.append(", "); |
| 436 | } |
| 437 | long key = keyAt(i); |
| 438 | buffer.append(key); |
| 439 | buffer.append('='); |
| 440 | Object value = valueAt(i); |
| 441 | if (value != this) { |
| 442 | buffer.append(value); |
| 443 | } else { |
| 444 | buffer.append("(this Map)"); |
| 445 | } |
| 446 | } |
| 447 | buffer.append('}'); |
| 448 | return buffer.toString(); |
Romain Guy | fdbf6a7 | 2009-06-18 15:13:40 -0700 | [diff] [blame] | 449 | } |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * @hide |
| 453 | */ |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 454 | public static class StringParcelling implements |
| 455 | com.android.internal.util.Parcelling<LongSparseArray<String>> { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 456 | @Override |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 457 | public void parcel(LongSparseArray<String> array, Parcel dest, int parcelFlags) { |
| 458 | if (array == null) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 459 | dest.writeInt(-1); |
| 460 | return; |
| 461 | } |
| 462 | |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 463 | int size = array.mSize; |
| 464 | |
| 465 | dest.writeInt(size); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 466 | dest.writeLongArray(array.mKeys); |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 467 | |
| 468 | dest.writeStringArray(Arrays.copyOfRange(array.mValues, 0, size, String[].class)); |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | @Override |
Philip P. Moltmann | badc09c | 2019-10-23 10:28:27 -0700 | [diff] [blame] | 472 | public LongSparseArray<String> unparcel(Parcel source) { |
Philip P. Moltmann | 59076d8 | 2019-08-19 15:00:40 -0700 | [diff] [blame] | 473 | int size = source.readInt(); |
| 474 | if (size == -1) { |
| 475 | return null; |
| 476 | } |
| 477 | |
| 478 | LongSparseArray<String> array = new LongSparseArray<>(0); |
| 479 | array.mSize = size; |
| 480 | array.mKeys = source.createLongArray(); |
| 481 | array.mValues = source.createStringArray(); |
| 482 | |
| 483 | // Make sure array is sane |
| 484 | Preconditions.checkArgument(array.mKeys.length >= size); |
| 485 | Preconditions.checkArgument(array.mValues.length >= size); |
| 486 | |
| 487 | if (size > 0) { |
| 488 | long last = array.mKeys[0]; |
| 489 | for (int i = 1; i < size; i++) { |
| 490 | Preconditions.checkArgument(last < array.mKeys[i]); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return array; |
| 495 | } |
| 496 | } |
Dianne Hackborn | 9c1d298 | 2012-02-10 12:36:18 -0800 | [diff] [blame] | 497 | } |