Debian Codegraph @

common/src/main/java/com/android/utils/SparseArray.java

raw
1 /*
2 * Copyright (C) 2006 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 com.android.utils;
18
19
20 /**
21 * SparseArrays map integers to Objects. Unlike a normal array of Objects,
22 * there can be gaps in the indices. It is intended to be more efficient
23 * than using a HashMap to map Integers to Objects.
24 */
25 public class SparseArray<E> {
26 private static final Object DELETED = new Object();
27 private boolean mGarbage = false;
28
29 /**
30 * Creates a new SparseArray containing no mappings.
31 */
32 public SparseArray() {
33 this(10);
34 }
35
36 /**
37 * Creates a new SparseArray containing no mappings that will not
38 * require any additional memory allocation to store the specified
39 * number of mappings.
40 */
41 public SparseArray(int initialCapacity) {
42 initialCapacity = ArrayUtils.idealIntArraySize(initialCapacity);
43
44 mKeys = new int[initialCapacity];
45 mValues = new Object[initialCapacity];
46 mSize = 0;
47 }
48
49 /**
50 * Gets the Object mapped from the specified key, or <code>null</code>
51 * if no such mapping has been made.
52 */
53 public E get(int key) {
54 return get(key, null);
55 }
56
57 /**
58 * Gets the Object mapped from the specified key, or the specified Object
59 * if no such mapping has been made.
60 */
61 @SuppressWarnings("unchecked")
62 public E get(int key, E valueIfKeyNotFound) {
63 int i = binarySearch(mKeys, 0, mSize, key);
64
65 if (i < 0 || mValues[i] == DELETED) {
66 return valueIfKeyNotFound;
67 } else {
68 return (E) mValues[i];
69 }
70 }
71
72 /**
73 * Removes the mapping from the specified key, if there was any.
74 */
75 public void delete(int key) {
76 int i = binarySearch(mKeys, 0, mSize, key);
77
78 if (i >= 0) {
79 if (mValues[i] != DELETED) {
80 mValues[i] = DELETED;
81 mGarbage = true;
82 }
83 }
84 }
85
86 /**
87 * Alias for {@link #delete(int)}.
88 */
89 public void remove(int key) {
90 delete(key);
91 }
92
93 private void gc() {
94 // Log.e("SparseArray", "gc start with " + mSize);
95
96 int n = mSize;
97 int o = 0;
98 int[] keys = mKeys;
99 Object[] values = mValues;
100
101 for (int i = 0; i < n; i++) {
102 Object val = values[i];
103
104 if (val != DELETED) {
105 if (i != o) {
106 keys[o] = keys[i];
107 values[o] = val;
108 }
109
110 o++;
111 }
112 }
113
114 mGarbage = false;
115 mSize = o;
116
117 // Log.e("SparseArray", "gc end with " + mSize);
118 }
119
120 /**
121 * Adds a mapping from the specified key to the specified value,
122 * replacing the previous mapping from the specified key if there
123 * was one.
124 */
125 public void put(int key, E value) {
126 int i = binarySearch(mKeys, 0, mSize, key);
127
128 if (i >= 0) {
129 mValues[i] = value;
130 } else {
131 i = ~i;
132
133 if (i < mSize && mValues[i] == DELETED) {
134 mKeys[i] = key;
135 mValues[i] = value;
136 return;
137 }
138
139 if (mGarbage && mSize >= mKeys.length) {
140 gc();
141
142 // Search again because indices may have changed.
143 i = ~binarySearch(mKeys, 0, mSize, key);
144 }
145
146 if (mSize >= mKeys.length) {
147 int n = ArrayUtils.idealIntArraySize(mSize + 1);
148
149 int[] nkeys = new int[n];
150 Object[] nvalues = new Object[n];
151
152 // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
153 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
154 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
155
156 mKeys = nkeys;
157 mValues = nvalues;
158 }
159
160 if (mSize - i != 0) {
161 // Log.e("SparseArray", "move " + (mSize - i));
162 System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
163 System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
164 }
165
166 mKeys[i] = key;
167 mValues[i] = value;
168 mSize++;
169 }
170 }
171
172 /**
173 * Returns the number of key-value mappings that this SparseArray
174 * currently stores.
175 */
176 public int size() {
177 if (mGarbage) {
178 gc();
179 }
180
181 return mSize;
182 }
183
184 /**
185 * Given an index in the range <code>0...size()-1</code>, returns
186 * the key from the <code>index</code>th key-value mapping that this
187 * SparseArray stores.
188 */
189 public int keyAt(int index) {
190 if (mGarbage) {
191 gc();
192 }
193
194 return mKeys[index];
195 }
196
197 /**
198 * Given an index in the range <code>0...size()-1</code>, returns
199 * the value from the <code>index</code>th key-value mapping that this
200 * SparseArray stores.
201 */
202 @SuppressWarnings("unchecked")
203 public E valueAt(int index) {
204 if (mGarbage) {
205 gc();
206 }
207
208 return (E) mValues[index];
209 }
210
211 /**
212 * Given an index in the range <code>0...size()-1</code>, sets a new
213 * value for the <code>index</code>th key-value mapping that this
214 * SparseArray stores.
215 */
216 public void setValueAt(int index, E value) {
217 if (mGarbage) {
218 gc();
219 }
220
221 mValues[index] = value;
222 }
223
224 /**
225 * Returns the index for which {@link #keyAt} would return the
226 * specified key, or a negative number if the specified
227 * key is not mapped.
228 */
229 public int indexOfKey(int key) {
230 if (mGarbage) {
231 gc();
232 }
233
234 return binarySearch(mKeys, 0, mSize, key);
235 }
236
237 /**
238 * Returns an index for which {@link #valueAt} would return the
239 * specified key, or a negative number if no keys map to the
240 * specified value.
241 * Beware that this is a linear search, unlike lookups by key,
242 * and that multiple keys can map to the same value and this will
243 * find only one of them.
244 */
245 public int indexOfValue(E value) {
246 if (mGarbage) {
247 gc();
248 }
249
250 for (int i = 0; i < mSize; i++)
251 if (mValues[i] == value)
252 return i;
253
254 return -1;
255 }
256
257 /**
258 * Removes all key-value mappings from this SparseArray.
259 */
260 public void clear() {
261 int n = mSize;
262 Object[] values = mValues;
263
264 for (int i = 0; i < n; i++) {
265 values[i] = null;
266 }
267
268 mSize = 0;
269 mGarbage = false;
270 }
271
272 /**
273 * Puts a key/value pair into the array, optimizing for the case where
274 * the key is greater than all existing keys in the array.
275 */
276 public void append(int key, E value) {
277 if (mSize != 0 && key <= mKeys[mSize - 1]) {
278 put(key, value);
279 return;
280 }
281
282 if (mGarbage && mSize >= mKeys.length) {
283 gc();
284 }
285
286 int pos = mSize;
287 if (pos >= mKeys.length) {
288 int n = ArrayUtils.idealIntArraySize(pos + 1);
289
290 int[] nkeys = new int[n];
291 Object[] nvalues = new Object[n];
292
293 // Log.e("SparseArray", "grow " + mKeys.length + " to " + n);
294 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
295 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
296
297 mKeys = nkeys;
298 mValues = nvalues;
299 }
300
301 mKeys[pos] = key;
302 mValues[pos] = value;
303 mSize = pos + 1;
304 }
305
306 public SparseArray<E> getUnmodifiable() {
307 final SparseArray<E> mStorage = this;
308 return new SparseArray<E>() {
309
310 @Override
311 public E get(int key) {
312 return mStorage.get(key);
313 }
314
315 @Override
316 public E get(int key, E valueIfKeyNotFound) {
317 return mStorage.get(key, valueIfKeyNotFound);
318 }
319
320 @Override
321 public void delete(int key) {
322 throw new UnsupportedOperationException();
323 }
324
325 @Override
326 public void remove(int key) {
327 throw new UnsupportedOperationException();
328 }
329
330 @Override
331 public void put(int key, E value) {
332 throw new UnsupportedOperationException();
333 }
334
335 @Override
336 public int size() {
337 return mStorage.size();
338 }
339
340 @Override
341 public int keyAt(int index) {
342 return mStorage.keyAt(index);
343 }
344
345 @Override
346 public E valueAt(int index) {
347 return mStorage.valueAt(index);
348 }
349
350 @Override
351 public void setValueAt(int index, E value) {
352 throw new UnsupportedOperationException();
353 }
354
355 @Override
356 public int indexOfKey(int key) {
357 return mStorage.indexOfKey(key);
358 }
359
360 @Override
361 public int indexOfValue(E value) {
362 return mStorage.indexOfValue(value);
363 }
364
365 @Override
366 public void clear() {
367 throw new UnsupportedOperationException();
368 }
369
370 @Override
371 public void append(int key, E value) {
372 throw new UnsupportedOperationException();
373 }
374
375 };
376 }
377
378 private static int binarySearch(int[] a, int start, int len, int key) {
379 int high = start + len, low = start - 1, guess;
380
381 while (high - low > 1) {
382 guess = (high + low) / 2;
383
384 if (a[guess] < key)
385 low = guess;
386 else
387 high = guess;
388 }
389
390 if (high == start + len)
391 return ~(start + len);
392 else if (a[high] == key)
393 return high;
394 else
395 return ~high;
396 }
397
398 private int[] mKeys;
399 private Object[] mValues;
400 private int mSize;
401 }