blob: 290a89b46e906a6a6b277046a37a8b5a5811e59b [file] [log] [blame]
Alan Viverettef0aed092013-11-06 15:33:03 -08001/*
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
Alan Viverettedb24d142013-11-27 17:38:24 -080086 System.arraycopy(values.mValues, 0, mValues, mSize, count);
Alan Viverettef0aed092013-11-06 15:33:03 -080087 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) {
Alan Viverettedb24d142013-11-27 17:38:24 -0800130 if (index >= mSize) {
131 throw new ArrayIndexOutOfBoundsException(mSize, index);
132 }
Alan Viverettef0aed092013-11-06 15:33:03 -0800133 return mValues[index];
134 }
135
136 /**
137 * Returns the index of the first occurrence of the specified value in this
138 * array, or -1 if this array does not contain the value.
139 */
140 public int indexOf(long value) {
141 final int n = mSize;
142 for (int i = 0; i < n; i++) {
143 if (mValues[i] == value) {
144 return i;
145 }
146 }
147 return -1;
148 }
149
150 /**
151 * Removes the value at the specified index from this array.
152 */
153 public void remove(int index) {
Alan Viverettedb24d142013-11-27 17:38:24 -0800154 if (index >= mSize) {
155 throw new ArrayIndexOutOfBoundsException(mSize, index);
156 }
Alan Viverettef0aed092013-11-06 15:33:03 -0800157 System.arraycopy(mValues, index, mValues, index + 1, mSize - index);
158 }
159
160 /**
161 * Returns the number of values in this array.
162 */
163 public int size() {
164 return mSize;
165 }
166}