Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 19 | import com.android.internal.util.ArrayUtils; |
| 20 | |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 21 | /** |
| 22 | * Map of {@code long} to {@code long}. Unlike a normal array of longs, there |
Dianne Hackborn | b993f41 | 2013-07-12 17:46:45 -0700 | [diff] [blame] | 23 | * can be gaps in the indices. It is intended to be more memory efficient than using a |
| 24 | * {@code HashMap}, both because it avoids |
| 25 | * auto-boxing keys and values and its data structure doesn't rely on an extra entry object |
| 26 | * for each mapping. |
| 27 | * |
| 28 | * <p>Note that this container keeps its mappings in an array data structure, |
| 29 | * using a binary search to find keys. The implementation is not intended to be appropriate for |
| 30 | * data structures |
| 31 | * that may contain large numbers of items. It is generally slower than a traditional |
| 32 | * HashMap, since lookups require a binary search and adds and removes require inserting |
| 33 | * and deleting entries in the array. For containers holding up to hundreds of items, |
| 34 | * the performance difference is not significant, less than 50%.</p> |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 35 | * |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 36 | * <p>It is possible to iterate over the items in this container using |
| 37 | * {@link #keyAt(int)} and {@link #valueAt(int)}. Iterating over the keys using |
| 38 | * <code>keyAt(int)</code> with ascending values of the index will return the |
| 39 | * 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] | 40 | * order in the case of <code>valueAt(int)</code>.</p> |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 41 | * |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 42 | * @hide |
| 43 | */ |
| 44 | public class LongSparseLongArray implements Cloneable { |
| 45 | private long[] mKeys; |
| 46 | private long[] mValues; |
| 47 | private int mSize; |
| 48 | |
| 49 | /** |
| 50 | * Creates a new SparseLongArray containing no mappings. |
| 51 | */ |
| 52 | public LongSparseLongArray() { |
| 53 | this(10); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Creates a new SparseLongArray containing no mappings that will not |
| 58 | * require any additional memory allocation to store the specified |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 59 | * number of mappings. If you supply an initial capacity of 0, the |
| 60 | * sparse array will be initialized with a light-weight representation |
| 61 | * not requiring any additional array allocations. |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 62 | */ |
| 63 | public LongSparseLongArray(int initialCapacity) { |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 64 | if (initialCapacity == 0) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 65 | mKeys = ContainerHelpers.EMPTY_LONGS; |
| 66 | mValues = ContainerHelpers.EMPTY_LONGS; |
Dianne Hackborn | f4bf0ae | 2013-05-20 18:42:16 -0700 | [diff] [blame] | 67 | } else { |
| 68 | initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity); |
| 69 | mKeys = new long[initialCapacity]; |
| 70 | mValues = new long[initialCapacity]; |
| 71 | } |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 72 | mSize = 0; |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public LongSparseLongArray clone() { |
| 77 | LongSparseLongArray clone = null; |
| 78 | try { |
| 79 | clone = (LongSparseLongArray) super.clone(); |
| 80 | clone.mKeys = mKeys.clone(); |
| 81 | clone.mValues = mValues.clone(); |
| 82 | } catch (CloneNotSupportedException cnse) { |
| 83 | /* ignore */ |
| 84 | } |
| 85 | return clone; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the long mapped from the specified key, or <code>0</code> |
| 90 | * if no such mapping has been made. |
| 91 | */ |
| 92 | public long get(long key) { |
| 93 | return get(key, 0); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Gets the long mapped from the specified key, or the specified value |
| 98 | * if no such mapping has been made. |
| 99 | */ |
| 100 | public long get(long key, long valueIfKeyNotFound) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 101 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 102 | |
| 103 | if (i < 0) { |
| 104 | return valueIfKeyNotFound; |
| 105 | } else { |
| 106 | return mValues[i]; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Removes the mapping from the specified key, if there was any. |
| 112 | */ |
| 113 | public void delete(long key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 114 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 115 | |
| 116 | if (i >= 0) { |
| 117 | removeAt(i); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Removes the mapping at the given index. |
| 123 | */ |
| 124 | public void removeAt(int index) { |
| 125 | System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); |
| 126 | System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1)); |
| 127 | mSize--; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Adds a mapping from the specified key to the specified value, |
| 132 | * replacing the previous mapping from the specified key if there |
| 133 | * was one. |
| 134 | */ |
| 135 | public void put(long key, long value) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 136 | int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 137 | |
| 138 | if (i >= 0) { |
| 139 | mValues[i] = value; |
| 140 | } else { |
| 141 | i = ~i; |
| 142 | |
| 143 | if (mSize >= mKeys.length) { |
| 144 | growKeyAndValueArrays(mSize + 1); |
| 145 | } |
| 146 | |
| 147 | if (mSize - i != 0) { |
| 148 | System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
| 149 | System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
| 150 | } |
| 151 | |
| 152 | mKeys[i] = key; |
| 153 | mValues[i] = value; |
| 154 | mSize++; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the number of key-value mappings that this SparseIntArray |
| 160 | * currently stores. |
| 161 | */ |
| 162 | public int size() { |
| 163 | return mSize; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Given an index in the range <code>0...size()-1</code>, returns |
| 168 | * the key from the <code>index</code>th key-value mapping that this |
| 169 | * SparseLongArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 170 | * |
| 171 | * <p>The keys corresponding to indices in ascending order are guaranteed to |
| 172 | * be in ascending order, e.g., <code>keyAt(0)</code> will return the |
| 173 | * smallest key and <code>keyAt(size()-1)</code> will return the largest |
| 174 | * key.</p> |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 175 | */ |
| 176 | public long keyAt(int index) { |
| 177 | return mKeys[index]; |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Given an index in the range <code>0...size()-1</code>, returns |
| 182 | * the value from the <code>index</code>th key-value mapping that this |
| 183 | * SparseLongArray stores. |
Flavio Lerda | 5771302 | 2013-09-08 18:04:58 +0100 | [diff] [blame] | 184 | * |
| 185 | * <p>The values corresponding to indices in ascending order are guaranteed |
| 186 | * to be associated with keys in ascending order, e.g., |
| 187 | * <code>valueAt(0)</code> will return the value associated with the |
| 188 | * smallest key and <code>valueAt(size()-1)</code> will return the value |
| 189 | * associated with the largest key.</p> |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 190 | */ |
| 191 | public long valueAt(int index) { |
| 192 | return mValues[index]; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Returns the index for which {@link #keyAt} would return the |
| 197 | * specified key, or a negative number if the specified |
| 198 | * key is not mapped. |
| 199 | */ |
| 200 | public int indexOfKey(long key) { |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 201 | return ContainerHelpers.binarySearch(mKeys, mSize, key); |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Returns an index for which {@link #valueAt} would return the |
| 206 | * specified key, or a negative number if no keys map to the |
| 207 | * specified value. |
| 208 | * Beware that this is a linear search, unlike lookups by key, |
| 209 | * and that multiple keys can map to the same value and this will |
| 210 | * find only one of them. |
| 211 | */ |
| 212 | public int indexOfValue(long value) { |
| 213 | for (int i = 0; i < mSize; i++) |
| 214 | if (mValues[i] == value) |
| 215 | return i; |
| 216 | |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | /** |
| 221 | * Removes all key-value mappings from this SparseIntArray. |
| 222 | */ |
| 223 | public void clear() { |
| 224 | mSize = 0; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Puts a key/value pair into the array, optimizing for the case where |
| 229 | * the key is greater than all existing keys in the array. |
| 230 | */ |
| 231 | public void append(long key, long value) { |
| 232 | if (mSize != 0 && key <= mKeys[mSize - 1]) { |
| 233 | put(key, value); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | int pos = mSize; |
| 238 | if (pos >= mKeys.length) { |
| 239 | growKeyAndValueArrays(pos + 1); |
| 240 | } |
| 241 | |
| 242 | mKeys[pos] = key; |
| 243 | mValues[pos] = value; |
| 244 | mSize = pos + 1; |
| 245 | } |
| 246 | |
| 247 | private void growKeyAndValueArrays(int minNeededSize) { |
| 248 | int n = ArrayUtils.idealLongArraySize(minNeededSize); |
| 249 | |
| 250 | long[] nkeys = new long[n]; |
| 251 | long[] nvalues = new long[n]; |
| 252 | |
| 253 | System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
| 254 | System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
| 255 | |
| 256 | mKeys = nkeys; |
| 257 | mValues = nvalues; |
| 258 | } |
Dianne Hackborn | 3e82ba1 | 2013-07-16 13:23:55 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
| 261 | * {@inheritDoc} |
| 262 | * |
| 263 | * <p>This implementation composes a string by iterating over its mappings. |
| 264 | */ |
| 265 | @Override |
| 266 | public String toString() { |
| 267 | if (size() <= 0) { |
| 268 | return "{}"; |
| 269 | } |
| 270 | |
| 271 | StringBuilder buffer = new StringBuilder(mSize * 28); |
| 272 | buffer.append('{'); |
| 273 | for (int i=0; i<mSize; i++) { |
| 274 | if (i > 0) { |
| 275 | buffer.append(", "); |
| 276 | } |
| 277 | long key = keyAt(i); |
| 278 | buffer.append(key); |
| 279 | buffer.append('='); |
| 280 | long value = valueAt(i); |
| 281 | buffer.append(value); |
| 282 | } |
| 283 | buffer.append('}'); |
| 284 | return buffer.toString(); |
| 285 | } |
Jeff Sharkey | dda73b5 | 2013-01-18 16:56:49 -0800 | [diff] [blame] | 286 | } |