blob: 4f76463a653e9804a6da2192a8539e57dab05d2a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.util;
18
19import com.android.internal.util.ArrayUtils;
Adam Lesinski776abc22014-03-07 11:30:59 -050020import com.android.internal.util.GrowingArrayUtils;
21
22import libcore.util.EmptyArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023
24/**
25 * SparseBooleanArrays map integers to booleans.
26 * Unlike a normal array of booleans
Dianne Hackbornb993f412013-07-12 17:46:45 -070027 * there can be gaps in the indices. It is intended to be more memory efficient
28 * than using a HashMap to map Integers to Booleans, both because it avoids
29 * auto-boxing keys and values and its data structure doesn't rely on an extra entry object
30 * for each mapping.
31 *
32 * <p>Note that this container keeps its mappings in an array data structure,
33 * using a binary search to find keys. The implementation is not intended to be appropriate for
34 * data structures
35 * that may contain large numbers of items. It is generally slower than a traditional
36 * HashMap, since lookups require a binary search and adds and removes require inserting
37 * and deleting entries in the array. For containers holding up to hundreds of items,
38 * the performance difference is not significant, less than 50%.</p>
Flavio Lerda57713022013-09-08 18:04:58 +010039 *
40 * <p>It is possible to iterate over the items in this container using
41 * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using
42 * <code>keyAt(int)</code> with ascending values of the index will return the
43 * keys in ascending order, or the values corresponding to the keys in ascending
Newton Allenebb47952013-11-21 13:27:10 -080044 * order in the case of <code>valueAt(int)</code>.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045 */
Svetoslav Ganov35bfede2011-07-14 17:57:06 -070046public class SparseBooleanArray implements Cloneable {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 /**
48 * Creates a new SparseBooleanArray containing no mappings.
49 */
50 public SparseBooleanArray() {
51 this(10);
52 }
53
54 /**
55 * Creates a new SparseBooleanArray containing no mappings that will not
56 * require any additional memory allocation to store the specified
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070057 * number of mappings. If you supply an initial capacity of 0, the
58 * sparse array will be initialized with a light-weight representation
59 * not requiring any additional array allocations.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 */
61 public SparseBooleanArray(int initialCapacity) {
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070062 if (initialCapacity == 0) {
Adam Lesinski776abc22014-03-07 11:30:59 -050063 mKeys = EmptyArray.INT;
64 mValues = EmptyArray.BOOLEAN;
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070065 } else {
Adam Lesinski776abc22014-03-07 11:30:59 -050066 mKeys = ArrayUtils.newUnpaddedIntArray(initialCapacity);
67 mValues = new boolean[mKeys.length];
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070068 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 mSize = 0;
70 }
71
Svetoslav Ganov35bfede2011-07-14 17:57:06 -070072 @Override
73 public SparseBooleanArray clone() {
74 SparseBooleanArray clone = null;
75 try {
76 clone = (SparseBooleanArray) super.clone();
77 clone.mKeys = mKeys.clone();
78 clone.mValues = mValues.clone();
79 } catch (CloneNotSupportedException cnse) {
80 /* ignore */
81 }
82 return clone;
83 }
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 /**
86 * Gets the boolean mapped from the specified key, or <code>false</code>
87 * if no such mapping has been made.
88 */
89 public boolean get(int key) {
90 return get(key, false);
91 }
92
93 /**
94 * Gets the boolean mapped from the specified key, or the specified value
95 * if no such mapping has been made.
96 */
97 public boolean get(int key, boolean valueIfKeyNotFound) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -070098 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099
100 if (i < 0) {
101 return valueIfKeyNotFound;
102 } else {
103 return mValues[i];
104 }
105 }
106
107 /**
108 * Removes the mapping from the specified key, if there was any.
109 */
110 public void delete(int key) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700111 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 if (i >= 0) {
114 System.arraycopy(mKeys, i + 1, mKeys, i, mSize - (i + 1));
115 System.arraycopy(mValues, i + 1, mValues, i, mSize - (i + 1));
116 mSize--;
117 }
118 }
119
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800120 /** @hide */
121 public void removeAt(int index) {
122 System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1));
123 System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1));
124 mSize--;
125 }
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /**
128 * Adds a mapping from the specified key to the specified value,
129 * replacing the previous mapping from the specified key if there
130 * was one.
131 */
132 public void put(int key, boolean value) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700133 int i = ContainerHelpers.binarySearch(mKeys, mSize, key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
135 if (i >= 0) {
136 mValues[i] = value;
137 } else {
138 i = ~i;
139
Adam Lesinski776abc22014-03-07 11:30:59 -0500140 mKeys = GrowingArrayUtils.insert(mKeys, mSize, i, key);
141 mValues = GrowingArrayUtils.insert(mValues, mSize, i, value);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 mSize++;
143 }
144 }
145
146 /**
147 * Returns the number of key-value mappings that this SparseBooleanArray
148 * currently stores.
149 */
150 public int size() {
151 return mSize;
152 }
153
154 /**
155 * Given an index in the range <code>0...size()-1</code>, returns
156 * the key from the <code>index</code>th key-value mapping that this
Flavio Lerda57713022013-09-08 18:04:58 +0100157 * SparseBooleanArray stores.
158 *
159 * <p>The keys corresponding to indices in ascending order are guaranteed to
160 * be in ascending order, e.g., <code>keyAt(0)</code> will return the
161 * smallest key and <code>keyAt(size()-1)</code> will return the largest
162 * key.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 */
164 public int keyAt(int index) {
165 return mKeys[index];
166 }
Flavio Lerda57713022013-09-08 18:04:58 +0100167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
169 * Given an index in the range <code>0...size()-1</code>, returns
170 * the value from the <code>index</code>th key-value mapping that this
Flavio Lerda57713022013-09-08 18:04:58 +0100171 * SparseBooleanArray stores.
172 *
173 * <p>The values corresponding to indices in ascending order are guaranteed
174 * to be associated with keys in ascending order, e.g.,
175 * <code>valueAt(0)</code> will return the value associated with the
176 * smallest key and <code>valueAt(size()-1)</code> will return the value
177 * associated with the largest key.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 */
179 public boolean valueAt(int index) {
180 return mValues[index];
181 }
182
Dianne Hackborneaf2ac42014-02-07 13:01:07 -0800183 /** @hide */
184 public void setValueAt(int index, boolean value) {
185 mValues[index] = value;
186 }
187
Steve McKay4b3a13c2015-06-11 10:10:49 -0700188 /** @hide */
189 public void setKeyAt(int index, int key) {
190 mKeys[index] = key;
191 }
192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 /**
194 * Returns the index for which {@link #keyAt} would return the
195 * specified key, or a negative number if the specified
196 * key is not mapped.
197 */
198 public int indexOfKey(int key) {
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700199 return ContainerHelpers.binarySearch(mKeys, mSize, key);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
201
202 /**
203 * Returns an index for which {@link #valueAt} would return the
204 * specified key, or a negative number if no keys map to the
205 * specified value.
206 * Beware that this is a linear search, unlike lookups by key,
207 * and that multiple keys can map to the same value and this will
208 * find only one of them.
209 */
210 public int indexOfValue(boolean value) {
211 for (int i = 0; i < mSize; i++)
212 if (mValues[i] == value)
213 return i;
214
215 return -1;
216 }
217
218 /**
219 * Removes all key-value mappings from this SparseBooleanArray.
220 */
221 public void clear() {
222 mSize = 0;
223 }
224
225 /**
226 * Puts a key/value pair into the array, optimizing for the case where
227 * the key is greater than all existing keys in the array.
228 */
229 public void append(int key, boolean value) {
230 if (mSize != 0 && key <= mKeys[mSize - 1]) {
231 put(key, value);
232 return;
233 }
234
Adam Lesinski776abc22014-03-07 11:30:59 -0500235 mKeys = GrowingArrayUtils.append(mKeys, mSize, key);
236 mValues = GrowingArrayUtils.append(mValues, mSize, value);
237 mSize++;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 }
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700239
Steve McKay3b409d02015-07-22 11:42:14 -0700240 @Override
241 public int hashCode() {
242 int hashCode = mSize;
243 for (int i = 0; i < mSize; i++) {
244 hashCode = 31 * hashCode + mKeys[i] | (mValues[i] ? 1 : 0);
245 }
246 return hashCode;
247 }
248
249 @Override
250 public boolean equals(Object that) {
251 if (this == that) {
252 return true;
253 }
254
255 if (!(that instanceof SparseBooleanArray)) {
256 return false;
257 }
258
259 SparseBooleanArray other = (SparseBooleanArray) that;
260 if (mSize != other.mSize) {
261 return false;
262 }
263
264 for (int i = 0; i < mSize; i++) {
265 if (mKeys[i] != other.mKeys[i]) {
266 return false;
267 }
268 if (mValues[i] != other.mValues[i]) {
269 return false;
270 }
271 }
272 return true;
273 }
274
Dianne Hackborn3e82ba12013-07-16 13:23:55 -0700275 /**
276 * {@inheritDoc}
277 *
278 * <p>This implementation composes a string by iterating over its mappings.
279 */
280 @Override
281 public String toString() {
282 if (size() <= 0) {
283 return "{}";
284 }
285
286 StringBuilder buffer = new StringBuilder(mSize * 28);
287 buffer.append('{');
288 for (int i=0; i<mSize; i++) {
289 if (i > 0) {
290 buffer.append(", ");
291 }
292 int key = keyAt(i);
293 buffer.append(key);
294 buffer.append('=');
295 boolean value = valueAt(i);
296 buffer.append(value);
297 }
298 buffer.append('}');
299 return buffer.toString();
300 }
301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 private int[] mKeys;
303 private boolean[] mValues;
304 private int mSize;
305}