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