-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathSparseArray.java
308 lines (284 loc) · 6.75 KB
/
SparseArray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/**
* Data-Structures-In-Java
* SparseArray.java
*/
package com.deepak.data.structures.Arrays;
import com.deepak.data.structures.Utils.ArrayUtils;
/**
* Sparse Arrays in Java :
*
* Sparse Arrays maps Integers to Objects. Unlike a normal array of objects,
* there can be gap in the indexes. It is intended to be more memory efficient
* than mapping Integers to Objects in a HashMap, both because it avoids the
* auto boxing of keys and it's data structure doesn't rely on an extra entry
* object for mapping.
*
* <p>Note that this container keeps its mappings in an array data structure,
* using a binary search to find keys. The implementation is not intended to be
* appropriate for data structures that may contain large numbers of items.
* It is generally slower than a traditional HashMap, since lookups require a
* binary search and adds and removes require inserting and deleting entries
* in the array. For containers holding up to hundreds of items, the performance
* difference is not significant, less than 50%.</p>
*
* <p>To help with performance, the container includes an optimization when removing
* keys: instead of compacting its array immediately, it leaves the removed entry marked
* as deleted. The entry can then be re-used for the same key, or compacted later in
* a single garbage collection step of all removed entries. This garbage collection will
* need to be performed at any time the array needs to be grown or the the map size or
* entry values are retrieved.</p>
*
* @author Deepak
*/
public class SparseArray<T> {
/* Define Object DELETED */
private static final Object DELETED = new Object();
/* Flag for garbage collection */
private boolean garbage = false;
/* Default Capacity */
private static final int DEFAULT_CAPACITY = 10;
/* Variables for keys, values and size */
private int[] keys;
private Object[] values;
private int size;
/**
* Constructor
*/
public SparseArray() {
this(DEFAULT_CAPACITY);
}
/**
* Constructor
*
* @param initialCapacity
*/
public SparseArray(int initialCapacity) {
if (initialCapacity == 0) {
keys = new int[0];
values = new Object[0];
} else {
values = new Object[initialCapacity];
keys = new int[values.length];
}
size = 0;
}
/**
* Method to get the value associated with the key
*
* @param key
* @return {@link T}
*/
public T get(int key) {
return get(key, null);
}
/**
* Method to get the value associated with the key,
* If not found, return default value specified
*
* @param key
* @param valueIfKeyNotFound
* @return {@link T}
*/
@SuppressWarnings("unchecked")
public T get(int key, T valueIfKeyNotFound) {
int i = ArrayUtils.binarySearch(keys, size, key);
if (i < 0 || values[i] == DELETED) {
return valueIfKeyNotFound;
} else {
return (T) values[i];
}
}
/**
* Method to add the key and value in sparse array
*
* @param key
* @param value
*/
public void put(int key, T value) {
int i = ArrayUtils.binarySearch(keys, size, key);
if (i >= 0) {
values[i] = value;
} else {
i = ~i;
if (i < size && values[i] == DELETED) {
keys[i] = key;
values[i] = value;
return;
}
if (garbage && size >= keys.length) {
performGC();
i = ~ArrayUtils.binarySearch(keys, size, key);
}
keys = insert(keys, size, i, key);
values = insert(values, size, i, value);
size++;
}
}
/**
* Method to delete a Key value pair for a key
*
* @param key
*/
public void delete(int key) {
int i = ArrayUtils.binarySearch(keys, size, key);
if (i >= 0) {
if (values[i] != DELETED) {
values[i] = DELETED;
garbage = true;
}
}
}
/**
* Method to remove a Key value pair for a key
*
* @param key
*/
public void remove(int key) {
delete(key);
}
/**
* Method to remove the value at index
*
* @param index
*/
public void removeAt(int index) {
if (values[index] != DELETED) {
values[index] = DELETED;
garbage = true;
}
}
/**
* Method to get the key at index
*
* @param index
* @return {@int key}
*/
public int keyAt(int index) {
if (garbage) {
performGC();
}
return keys[index];
}
/**
* Method to find the value at index
*
* @param index
* @return {@link T}
*/
@SuppressWarnings("unchecked")
public T valueAt(int index) {
if (garbage) {
performGC();
}
return (T) values[index];
}
/**
* Method to set the value at given index
*
* @param index
* @param value
*/
public void setValueAt(int index, T value) {
if (garbage) {
performGC();
}
values[index] = value;
}
/**
* Method to clear
*/
public void clear() {
int n = size;
Object[] valArray = values;
for (int i = 0; i < n; i++) {
valArray[i] = null;
}
size = 0;
garbage = false;
}
/**
* Method to find size of the array
*
* @return {@link int}
*/
public int size() {
if (garbage) {
performGC();
}
return size;
}
/**
* Method to check if array is empty
*
* @return {@link boolean}
*/
public boolean isEmpty() {
return size == 0;
}
/**
* Method to perform GC
*/
private void performGC() {
int n = size;
int position = 0;
int[] keyArray = keys;
Object[] valArray = values;
for (int i = 0; i < n; i++) {
Object val = valArray[i];
if (val != DELETED) {
if (i != position) {
keyArray[position] = keyArray[i];
valArray[position] = val;
valArray[i] = null;
}
position++;
}
garbage = false;
size = position;
}
}
/**
* Method to insert an element at a given index
*
* @param array
* @param currentSize
* @param index
* @param element
* @return {@link Object[]}
*/
private Object[] insert(Object[] array, int currentSize, int index, T element) {
assert currentSize <= array.length;
if (currentSize + 1 <= array.length) {
System.arraycopy(array, index, array, index + 1, currentSize - index);
array[index] = element;
return array;
}
Object[] newArray = new Object[currentSize * 2];
System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = element;
System.arraycopy(array, index, newArray, index + 1, array.length - index);
return newArray;
}
/**
* Method to insert an integer at given index
*
* @param array
* @param currentSize
* @param index
* @param element
* @return {@link int[]}
*/
private static int[] insert(int[] array, int currentSize, int index, int element) {
assert currentSize <= array.length;
if (currentSize + 1 <= array.length) {
System.arraycopy(array, index, array, index + 1, currentSize - index);
array[index] = element;
return array;
}
int[] newArray = new int[currentSize * 2];
System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = element;
System.arraycopy(array, index, newArray, index + 1, array.length - index);
return newArray;
}
}