|
2 | 2 |
|
3 | 3 | public class SparseArray<T> { |
4 | 4 |
|
5 | | - private final Object[][] segments; |
6 | | - private final int block; |
| 5 | + private volatile Object[][] segments; |
| 6 | + private volatile int capacity; |
| 7 | + private volatile int block; |
7 | 8 |
|
8 | 9 | public SparseArray(int length) { |
9 | 10 | this(length, 32); // compute better default distribution |
10 | 11 | } |
11 | 12 |
|
12 | 13 | public SparseArray(int length, int block) { |
13 | 14 | this.segments = new Object[length / block + 1][]; |
| 15 | + this.capacity = length; |
14 | 16 | this.block = block; |
15 | 17 | } |
16 | 18 |
|
17 | | - public void set(int index, T value) { |
| 19 | + public T set(int index, T value) { |
| 20 | + int offset = index % block; |
| 21 | + Object[] segment = segment(index); |
| 22 | + Object result = segment[offset]; |
| 23 | + |
| 24 | + segment[offset] = value; |
| 25 | + return (T)result; |
| 26 | + } |
| 27 | + |
| 28 | + public T get(int index) { |
18 | 29 | int section = index /block; |
19 | | - Object[] segment = segments[section]; |
20 | 30 |
|
21 | | - if(segment == null) { |
22 | | - segment = segments[section] = new Object[block]; |
| 31 | + if(section < segments.length) { |
| 32 | + Object[] segment = segments[section]; |
| 33 | + |
| 34 | + if(segment != null) { |
| 35 | + return (T)segment[index % block]; |
| 36 | + } |
23 | 37 | } |
24 | | - segment[index % block] = value; |
| 38 | + return null; |
25 | 39 | } |
26 | 40 |
|
27 | | - public T get(int index) { |
| 41 | + public T remove(int index) { |
28 | 42 | int section = index /block; |
29 | | - Object[] segment = segments[section]; |
30 | 43 |
|
31 | | - if(segment != null) { |
32 | | - return (T)segment[index % block]; |
| 44 | + if(section < segments.length) { |
| 45 | + Object[] segment = segments[section]; |
| 46 | + |
| 47 | + if(segment != null) { |
| 48 | + int offset = index % block; |
| 49 | + Object result = segment[offset]; |
| 50 | + |
| 51 | + segment[offset] = null; |
| 52 | + return (T)result; |
| 53 | + } |
33 | 54 | } |
34 | 55 | return null; |
35 | 56 | } |
36 | 57 |
|
37 | | - public void remove(int index) { |
| 58 | + protected Object[] segment(int index) { |
38 | 59 | int section = index /block; |
| 60 | + |
| 61 | + if(index >= capacity) { |
| 62 | + expand(capacity * 2 < index ? index : capacity *2); |
| 63 | + } |
39 | 64 | Object[] segment = segments[section]; |
40 | 65 |
|
41 | | - if(segment != null) { |
42 | | - segment[index % block] = null; |
| 66 | + if(segment == null) { |
| 67 | + segments[section] = new Object[block]; |
43 | 68 | } |
| 69 | + return segments[section]; |
| 70 | + } |
| 71 | + |
| 72 | + protected void expand(int length) { |
| 73 | + Object[][] copy = new Object[length / block + 1][]; |
| 74 | + |
| 75 | + for(int i = 0; i < segments.length; i++) { |
| 76 | + copy[i] = segments[i]; |
| 77 | + } |
| 78 | + capacity = length; |
| 79 | + segments = copy; |
| 80 | + } |
| 81 | + |
| 82 | + public int length() { |
| 83 | + return capacity; |
44 | 84 | } |
45 | 85 | } |
| 86 | + |
0 commit comments