blob: 503295c52bea9a417ee3283ad815c446640d7051 [file] [log] [blame]
Jeff Sharkeydda73b52013-01-18 16:56:49 -08001/*
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
17package android.util;
18
19import com.android.internal.util.ArrayUtils;
20
21import java.util.Arrays;
22
23/**
24 * Map of {@code long} to {@code long}. Unlike a normal array of longs, there
25 * can be gaps in the indices. It is intended to be more efficient than using a
26 * {@code HashMap}.
27 *
28 * @hide
29 */
30public class LongSparseLongArray implements Cloneable {
31 private long[] mKeys;
32 private long[] mValues;
33 private int mSize;
34
35 /**
36 * Creates a new SparseLongArray containing no mappings.
37 */
38 public LongSparseLongArray() {
39 this(10);
40 }
41
42 /**
43 * Creates a new SparseLongArray containing no mappings that will not
44 * require any additional memory allocation to store the specified
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070045 * number of mappings. If you supply an initial capacity of 0, the
46 * sparse array will be initialized with a light-weight representation
47 * not requiring any additional array allocations.
Jeff Sharkeydda73b52013-01-18 16:56:49 -080048 */
49 public LongSparseLongArray(int initialCapacity) {
Dianne Hackbornf4bf0ae2013-05-20 18:42:16 -070050 if (initialCapacity == 0) {
51 mKeys = SparseLongArray.EMPTY_LONGS;
52 mValues = SparseLongArray.EMPTY_LONGS;
53 } else {
54 initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity);
55 mKeys = new long[initialCapacity];
56 mValues = new long[initialCapacity];
57 }
Jeff Sharkeydda73b52013-01-18 16:56:49 -080058 mSize = 0;
59 }
60
61 @Override
62 public LongSparseLongArray clone() {
63 LongSparseLongArray clone = null;
64 try {
65 clone = (LongSparseLongArray) super.clone();
66 clone.mKeys = mKeys.clone();
67 clone.mValues = mValues.clone();
68 } catch (CloneNotSupportedException cnse) {
69 /* ignore */
70 }
71 return clone;
72 }
73
74 /**
75 * Gets the long mapped from the specified key, or <code>0</code>
76 * if no such mapping has been made.
77 */
78 public long get(long key) {
79 return get(key, 0);
80 }
81
82 /**
83 * Gets the long mapped from the specified key, or the specified value
84 * if no such mapping has been made.
85 */
86 public long get(long key, long valueIfKeyNotFound) {
87 int i = Arrays.binarySearch(mKeys, 0, mSize, key);
88
89 if (i < 0) {
90 return valueIfKeyNotFound;
91 } else {
92 return mValues[i];
93 }
94 }
95
96 /**
97 * Removes the mapping from the specified key, if there was any.
98 */
99 public void delete(long key) {
100 int i = Arrays.binarySearch(mKeys, 0, mSize, key);
101
102 if (i >= 0) {
103 removeAt(i);
104 }
105 }
106
107 /**
108 * Removes the mapping at the given index.
109 */
110 public void removeAt(int index) {
111 System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1));
112 System.arraycopy(mValues, index + 1, mValues, index, mSize - (index + 1));
113 mSize--;
114 }
115
116 /**
117 * Adds a mapping from the specified key to the specified value,
118 * replacing the previous mapping from the specified key if there
119 * was one.
120 */
121 public void put(long key, long value) {
122 int i = Arrays.binarySearch(mKeys, 0, mSize, key);
123
124 if (i >= 0) {
125 mValues[i] = value;
126 } else {
127 i = ~i;
128
129 if (mSize >= mKeys.length) {
130 growKeyAndValueArrays(mSize + 1);
131 }
132
133 if (mSize - i != 0) {
134 System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i);
135 System.arraycopy(mValues, i, mValues, i + 1, mSize - i);
136 }
137
138 mKeys[i] = key;
139 mValues[i] = value;
140 mSize++;
141 }
142 }
143
144 /**
145 * Returns the number of key-value mappings that this SparseIntArray
146 * currently stores.
147 */
148 public int size() {
149 return mSize;
150 }
151
152 /**
153 * Given an index in the range <code>0...size()-1</code>, returns
154 * the key from the <code>index</code>th key-value mapping that this
155 * SparseLongArray stores.
156 */
157 public long keyAt(int index) {
158 return mKeys[index];
159 }
160
161 /**
162 * Given an index in the range <code>0...size()-1</code>, returns
163 * the value from the <code>index</code>th key-value mapping that this
164 * SparseLongArray stores.
165 */
166 public long valueAt(int index) {
167 return mValues[index];
168 }
169
170 /**
171 * Returns the index for which {@link #keyAt} would return the
172 * specified key, or a negative number if the specified
173 * key is not mapped.
174 */
175 public int indexOfKey(long key) {
176 return Arrays.binarySearch(mKeys, 0, mSize, key);
177 }
178
179 /**
180 * Returns an index for which {@link #valueAt} would return the
181 * specified key, or a negative number if no keys map to the
182 * specified value.
183 * Beware that this is a linear search, unlike lookups by key,
184 * and that multiple keys can map to the same value and this will
185 * find only one of them.
186 */
187 public int indexOfValue(long value) {
188 for (int i = 0; i < mSize; i++)
189 if (mValues[i] == value)
190 return i;
191
192 return -1;
193 }
194
195 /**
196 * Removes all key-value mappings from this SparseIntArray.
197 */
198 public void clear() {
199 mSize = 0;
200 }
201
202 /**
203 * Puts a key/value pair into the array, optimizing for the case where
204 * the key is greater than all existing keys in the array.
205 */
206 public void append(long key, long value) {
207 if (mSize != 0 && key <= mKeys[mSize - 1]) {
208 put(key, value);
209 return;
210 }
211
212 int pos = mSize;
213 if (pos >= mKeys.length) {
214 growKeyAndValueArrays(pos + 1);
215 }
216
217 mKeys[pos] = key;
218 mValues[pos] = value;
219 mSize = pos + 1;
220 }
221
222 private void growKeyAndValueArrays(int minNeededSize) {
223 int n = ArrayUtils.idealLongArraySize(minNeededSize);
224
225 long[] nkeys = new long[n];
226 long[] nvalues = new long[n];
227
228 System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length);
229 System.arraycopy(mValues, 0, nvalues, 0, mValues.length);
230
231 mKeys = nkeys;
232 mValues = nvalues;
233 }
234}