| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import com.android.internal.util.ArrayUtils; |
| 20 |
import com.android.internal.util.Preconditions; |
| 21 |
|
| 22 |
import libcore.util.EmptyArray; |
| 23 |
|
| 24 |
import java.util.Arrays; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
public class IntArray implements Cloneable { |
| 32 |
private static final int MIN_CAPACITY_INCREMENT = 12; |
| 33 |
|
| 34 |
private int[] mValues; |
| 35 |
private int mSize; |
| 36 |
|
| 37 |
private IntArray(int[] array, int size) { |
| 38 |
mValues = array; |
| 39 |
mSize = Preconditions.checkArgumentInRange(size, 0, array.length, "size"); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public IntArray() { |
| 46 |
this(10); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public IntArray(int initialCapacity) { |
| 53 |
if (initialCapacity == 0) { |
| 54 |
mValues = EmptyArray.INT; |
| 55 |
} else { |
| 56 |
mValues = ArrayUtils.newUnpaddedIntArray(initialCapacity); |
| 57 |
} |
| 58 |
mSize = 0; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static IntArray wrap(int[] array) { |
| 65 |
return new IntArray(array, array.length); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public static IntArray fromArray(int[] array, int size) { |
| 72 |
return wrap(Arrays.copyOf(array, size)); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public void resize(int newSize) { |
| 81 |
Preconditions.checkArgumentNonnegative(newSize); |
| 82 |
if (newSize <= mValues.length) { |
| 83 |
Arrays.fill(mValues, newSize, mValues.length, 0); |
| 84 |
} else { |
| 85 |
ensureCapacity(newSize - mSize); |
| 86 |
} |
| 87 |
mSize = newSize; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public void add(int value) { |
| 94 |
add(mSize, value); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public void add(int index, int value) { |
| 104 |
ensureCapacity(1); |
| 105 |
int rightSegment = mSize - index; |
| 106 |
mSize++; |
| 107 |
ArrayUtils.checkBounds(mSize, index); |
| 108 |
|
| 109 |
if (rightSegment != 0) { |
| 110 |
|
| 111 |
System.arraycopy(mValues, index, mValues, index + 1, rightSegment); |
| 112 |
} |
| 113 |
|
| 114 |
mValues[index] = value; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public int binarySearch(int value) { |
| 132 |
return ContainerHelpers.binarySearch(mValues, mSize, value); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public void addAll(IntArray values) { |
| 139 |
final int count = values.mSize; |
| 140 |
ensureCapacity(count); |
| 141 |
|
| 142 |
System.arraycopy(values.mValues, 0, mValues, mSize, count); |
| 143 |
mSize += count; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public void addAll(int[] values) { |
| 150 |
final int count = values.length; |
| 151 |
ensureCapacity(count); |
| 152 |
|
| 153 |
System.arraycopy(values, 0, mValues, mSize, count); |
| 154 |
mSize += count; |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private void ensureCapacity(int count) { |
| 161 |
final int currentSize = mSize; |
| 162 |
final int minCapacity = currentSize + count; |
| 163 |
if (minCapacity >= mValues.length) { |
| 164 |
final int targetCap = currentSize + (currentSize < (MIN_CAPACITY_INCREMENT / 2) ? |
| 165 |
MIN_CAPACITY_INCREMENT : currentSize >> 1); |
| 166 |
final int newCapacity = targetCap > minCapacity ? targetCap : minCapacity; |
| 167 |
final int[] newValues = ArrayUtils.newUnpaddedIntArray(newCapacity); |
| 168 |
System.arraycopy(mValues, 0, newValues, 0, currentSize); |
| 169 |
mValues = newValues; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public void clear() { |
| 177 |
mSize = 0; |
| 178 |
} |
| 179 |
|
| 180 |
@Override |
| 181 |
public IntArray clone() { |
| 182 |
return new IntArray(mValues.clone(), mSize); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public int get(int index) { |
| 189 |
ArrayUtils.checkBounds(mSize, index); |
| 190 |
return mValues[index]; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public void set(int index, int value) { |
| 197 |
ArrayUtils.checkBounds(mSize, index); |
| 198 |
mValues[index] = value; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
public int indexOf(int value) { |
| 206 |
final int n = mSize; |
| 207 |
for (int i = 0; i < n; i++) { |
| 208 |
if (mValues[i] == value) { |
| 209 |
return i; |
| 210 |
} |
| 211 |
} |
| 212 |
return -1; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
public void remove(int index) { |
| 219 |
ArrayUtils.checkBounds(mSize, index); |
| 220 |
System.arraycopy(mValues, index + 1, mValues, index, mSize - index - 1); |
| 221 |
mSize--; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public int size() { |
| 228 |
return mSize; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public int[] toArray() { |
| 235 |
return Arrays.copyOf(mValues, mSize); |
| 236 |
} |
| 237 |
} |