LongArray.java revision f0aed09ed8153043e40b3ac99788d47ba0831306
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 17package android.util; 18 19import com.android.internal.util.ArrayUtils; 20 21/** 22 * Implements a growing array of long primitives. 23 * 24 * @hide 25 */ 26public 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 86 System.arraycopy(mValues, mSize, values.mValues, 0, count); 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 114 @SuppressWarnings("unchecked") 115 public LongArray clone() { 116 LongArray clone = null; 117 try { 118 clone = (LongArray) super.clone(); 119 clone.mValues = mValues.clone(); 120 } catch (CloneNotSupportedException cnse) { 121 /* ignore */ 122 } 123 return clone; 124 } 125 126 /** 127 * Returns the value at the specified position in this array. 128 */ 129 public long get(int index) { 130 return mValues[index]; 131 } 132 133 /** 134 * Returns the index of the first occurrence of the specified value in this 135 * array, or -1 if this array does not contain the value. 136 */ 137 public int indexOf(long value) { 138 final int n = mSize; 139 for (int i = 0; i < n; i++) { 140 if (mValues[i] == value) { 141 return i; 142 } 143 } 144 return -1; 145 } 146 147 /** 148 * Removes the value at the specified index from this array. 149 */ 150 public void remove(int index) { 151 System.arraycopy(mValues, index, mValues, index + 1, mSize - index); 152 } 153 154 /** 155 * Returns the number of values in this array. 156 */ 157 public int size() { 158 return mSize; 159 } 160} 161