Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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; |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 20 | import com.android.internal.util.Preconditions; |
| 21 | import java.util.Arrays; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 22 | import libcore.util.EmptyArray; |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | * Implements a growing array of long primitives. |
| 26 | * |
| 27 | * @hide |
| 28 | */ |
| 29 | public class LongArray implements Cloneable { |
| 30 | private static final int MIN_CAPACITY_INCREMENT = 12; |
| 31 | |
| 32 | private long[] mValues; |
| 33 | private int mSize; |
| 34 | |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 35 | private LongArray(long[] array, int size) { |
| 36 | mValues = array; |
| 37 | mSize = Preconditions.checkArgumentInRange(size, 0, array.length, "size"); |
| 38 | } |
| 39 | |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 40 | /** |
| 41 | * Creates an empty LongArray with the default initial capacity. |
| 42 | */ |
| 43 | public LongArray() { |
| 44 | this(10); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Creates an empty LongArray with the specified initial capacity. |
| 49 | */ |
| 50 | public LongArray(int initialCapacity) { |
| 51 | if (initialCapacity == 0) { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 52 | mValues = EmptyArray.LONG; |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 53 | } else { |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 54 | mValues = ArrayUtils.newUnpaddedLongArray(initialCapacity); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 55 | } |
| 56 | mSize = 0; |
| 57 | } |
| 58 | |
| 59 | /** |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 60 | * Creates an LongArray wrapping the given primitive long array. |
| 61 | */ |
| 62 | public static LongArray wrap(long[] array) { |
| 63 | return new LongArray(array, array.length); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates an LongArray from the given primitive long array, copying it. |
| 68 | */ |
| 69 | public static LongArray fromArray(long[] array, int size) { |
| 70 | return wrap(Arrays.copyOf(array, size)); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Changes the size of this LongArray. If this LongArray is shrinked, the backing array capacity |
| 75 | * is unchanged. If the new size is larger than backing array capacity, a new backing array is |
| 76 | * created from the current content of this LongArray padded with 0s. |
| 77 | */ |
| 78 | public void resize(int newSize) { |
| 79 | Preconditions.checkArgumentNonnegative(newSize); |
| 80 | if (newSize <= mValues.length) { |
| 81 | Arrays.fill(mValues, newSize, mValues.length, 0); |
| 82 | } else { |
| 83 | ensureCapacity(newSize - mSize); |
| 84 | } |
| 85 | mSize = newSize; |
| 86 | } |
| 87 | |
| 88 | /** |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 89 | * Appends the specified value to the end of this array. |
| 90 | */ |
| 91 | public void add(long value) { |
| 92 | add(mSize, value); |
| 93 | } |
| 94 | |
| 95 | /** |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 96 | * Inserts a value at the specified position in this array. If the specified index is equal to |
| 97 | * the length of the array, the value is added at the end. |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 98 | * |
| 99 | * @throws IndexOutOfBoundsException when index < 0 || index > size() |
| 100 | */ |
| 101 | public void add(int index, long value) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 102 | ensureCapacity(1); |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 103 | int rightSegment = mSize - index; |
| 104 | mSize++; |
| 105 | checkBounds(index); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 106 | |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 107 | if (rightSegment != 0) { |
| 108 | // Move by 1 all values from the right of 'index' |
| 109 | System.arraycopy(mValues, index, mValues, index + 1, rightSegment); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | mValues[index] = value; |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Adds the values in the specified array to this array. |
| 117 | */ |
| 118 | public void addAll(LongArray values) { |
| 119 | final int count = values.mSize; |
| 120 | ensureCapacity(count); |
| 121 | |
Alan Viverette | db24d14 | 2013-11-27 17:38:24 -0800 | [diff] [blame] | 122 | System.arraycopy(values.mValues, 0, mValues, mSize, count); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 123 | mSize += count; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Ensures capacity to append at least <code>count</code> values. |
| 128 | */ |
| 129 | private void ensureCapacity(int count) { |
| 130 | final int currentSize = mSize; |
| 131 | final int minCapacity = currentSize + count; |
| 132 | if (minCapacity >= mValues.length) { |
| 133 | final int targetCap = currentSize + (currentSize < (MIN_CAPACITY_INCREMENT / 2) ? |
| 134 | MIN_CAPACITY_INCREMENT : currentSize >> 1); |
| 135 | final int newCapacity = targetCap > minCapacity ? targetCap : minCapacity; |
Adam Lesinski | 776abc2 | 2014-03-07 11:30:59 -0500 | [diff] [blame] | 136 | final long[] newValues = ArrayUtils.newUnpaddedLongArray(newCapacity); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 137 | System.arraycopy(mValues, 0, newValues, 0, currentSize); |
| 138 | mValues = newValues; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Removes all values from this array. |
| 144 | */ |
| 145 | public void clear() { |
| 146 | mSize = 0; |
| 147 | } |
| 148 | |
| 149 | @Override |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 150 | public LongArray clone() { |
| 151 | LongArray clone = null; |
| 152 | try { |
| 153 | clone = (LongArray) super.clone(); |
| 154 | clone.mValues = mValues.clone(); |
| 155 | } catch (CloneNotSupportedException cnse) { |
| 156 | /* ignore */ |
| 157 | } |
| 158 | return clone; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the value at the specified position in this array. |
| 163 | */ |
| 164 | public long get(int index) { |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 165 | checkBounds(index); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 166 | return mValues[index]; |
| 167 | } |
| 168 | |
| 169 | /** |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 170 | * Sets the value at the specified position in this array. |
| 171 | */ |
| 172 | public void set(int index, long value) { |
| 173 | checkBounds(index); |
| 174 | mValues[index] = value; |
| 175 | } |
| 176 | |
| 177 | /** |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 178 | * Returns the index of the first occurrence of the specified value in this |
| 179 | * array, or -1 if this array does not contain the value. |
| 180 | */ |
| 181 | public int indexOf(long value) { |
| 182 | final int n = mSize; |
| 183 | for (int i = 0; i < n; i++) { |
| 184 | if (mValues[i] == value) { |
| 185 | return i; |
| 186 | } |
| 187 | } |
| 188 | return -1; |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Removes the value at the specified index from this array. |
| 193 | */ |
| 194 | public void remove(int index) { |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 195 | checkBounds(index); |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 196 | System.arraycopy(mValues, index + 1, mValues, index, mSize - index - 1); |
| 197 | mSize--; |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Returns the number of values in this array. |
| 202 | */ |
| 203 | public int size() { |
| 204 | return mSize; |
| 205 | } |
Hugo Benichi | 112962a | 2017-03-27 13:45:23 +0900 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * Returns a new array with the contents of this LongArray. |
| 209 | */ |
| 210 | public long[] toArray() { |
| 211 | return Arrays.copyOf(mValues, mSize); |
| 212 | } |
| 213 | |
| 214 | private void checkBounds(int index) { |
| 215 | if (index < 0 || mSize <= index) { |
| 216 | throw new ArrayIndexOutOfBoundsException(mSize, index); |
| 217 | } |
| 218 | } |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 219 | } |