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; |
| 20 | |
| 21 | /** |
| 22 | * Implements a growing array of long primitives. |
| 23 | * |
| 24 | * @hide |
| 25 | */ |
| 26 | public class LongArray implements Cloneable { |
| 27 | private static final int MIN_CAPACITY_INCREMENT = 12; |
| 28 | |
| 29 | private long[] mValues; |
| 30 | private int mSize; |
| 31 | |
| 32 | /** |
| 33 | * Creates an empty LongArray with the default initial capacity. |
| 34 | */ |
| 35 | public LongArray() { |
| 36 | this(10); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Creates an empty LongArray with the specified initial capacity. |
| 41 | */ |
| 42 | public LongArray(int initialCapacity) { |
| 43 | if (initialCapacity == 0) { |
| 44 | mValues = ContainerHelpers.EMPTY_LONGS; |
| 45 | } else { |
| 46 | initialCapacity = ArrayUtils.idealLongArraySize(initialCapacity); |
| 47 | mValues = new long[initialCapacity]; |
| 48 | } |
| 49 | mSize = 0; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Appends the specified value to the end of this array. |
| 54 | */ |
| 55 | public void add(long value) { |
| 56 | add(mSize, value); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Inserts a value at the specified position in this array. |
| 61 | * |
| 62 | * @throws IndexOutOfBoundsException when index < 0 || index > size() |
| 63 | */ |
| 64 | public void add(int index, long value) { |
| 65 | if (index < 0 || index > mSize) { |
| 66 | throw new IndexOutOfBoundsException(); |
| 67 | } |
| 68 | |
| 69 | ensureCapacity(1); |
| 70 | |
| 71 | if (mSize - index != 0) { |
| 72 | System.arraycopy(mValues, index, mValues, index + 1, mSize - index); |
| 73 | } |
| 74 | |
| 75 | mValues[index] = value; |
| 76 | mSize++; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Adds the values in the specified array to this array. |
| 81 | */ |
| 82 | public void addAll(LongArray values) { |
| 83 | final int count = values.mSize; |
| 84 | ensureCapacity(count); |
| 85 | |
Alan Viverette | db24d14 | 2013-11-27 17:38:24 -0800 | [diff] [blame] | 86 | System.arraycopy(values.mValues, 0, mValues, mSize, count); |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 87 | mSize += count; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Ensures capacity to append at least <code>count</code> values. |
| 92 | */ |
| 93 | private void ensureCapacity(int count) { |
| 94 | final int currentSize = mSize; |
| 95 | final int minCapacity = currentSize + count; |
| 96 | if (minCapacity >= mValues.length) { |
| 97 | final int targetCap = currentSize + (currentSize < (MIN_CAPACITY_INCREMENT / 2) ? |
| 98 | MIN_CAPACITY_INCREMENT : currentSize >> 1); |
| 99 | final int newCapacity = targetCap > minCapacity ? targetCap : minCapacity; |
| 100 | final long[] newValues = new long[ArrayUtils.idealLongArraySize(newCapacity)]; |
| 101 | System.arraycopy(mValues, 0, newValues, 0, currentSize); |
| 102 | mValues = newValues; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Removes all values from this array. |
| 108 | */ |
| 109 | public void clear() { |
| 110 | mSize = 0; |
| 111 | } |
| 112 | |
| 113 | @Override |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 114 | public LongArray clone() { |
| 115 | LongArray clone = null; |
| 116 | try { |
| 117 | clone = (LongArray) super.clone(); |
| 118 | clone.mValues = mValues.clone(); |
| 119 | } catch (CloneNotSupportedException cnse) { |
| 120 | /* ignore */ |
| 121 | } |
| 122 | return clone; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Returns the value at the specified position in this array. |
| 127 | */ |
| 128 | public long get(int index) { |
Alan Viverette | db24d14 | 2013-11-27 17:38:24 -0800 | [diff] [blame] | 129 | if (index >= mSize) { |
| 130 | throw new ArrayIndexOutOfBoundsException(mSize, index); |
| 131 | } |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 132 | return mValues[index]; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Returns the index of the first occurrence of the specified value in this |
| 137 | * array, or -1 if this array does not contain the value. |
| 138 | */ |
| 139 | public int indexOf(long value) { |
| 140 | final int n = mSize; |
| 141 | for (int i = 0; i < n; i++) { |
| 142 | if (mValues[i] == value) { |
| 143 | return i; |
| 144 | } |
| 145 | } |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Removes the value at the specified index from this array. |
| 151 | */ |
| 152 | public void remove(int index) { |
Alan Viverette | db24d14 | 2013-11-27 17:38:24 -0800 | [diff] [blame] | 153 | if (index >= mSize) { |
| 154 | throw new ArrayIndexOutOfBoundsException(mSize, index); |
| 155 | } |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 156 | System.arraycopy(mValues, index + 1, mValues, index, mSize - index - 1); |
| 157 | mSize--; |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the number of values in this array. |
| 162 | */ |
| 163 | public int size() { |
| 164 | return mSize; |
| 165 | } |
| 166 | } |