1e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka/*
2e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * Copyright (C) 2010 The Android Open Source Project
3e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka *
4e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * Licensed under the Apache License, Version 2.0 (the "License");
5e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * you may not use this file except in compliance with the License.
6e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * You may obtain a copy of the License at
7e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka *
8e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka *      http://www.apache.org/licenses/LICENSE-2.0
9e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka *
10e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * Unless required by applicable law or agreed to in writing, software
11e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * distributed under the License is distributed on an "AS IS" BASIS,
12e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * See the License for the specific language governing permissions and
14e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka * limitations under the License.
15e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka */
16e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
17e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurkapackage com.android.gallery3d.util;
18e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
19e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurkapublic class IntArray {
20e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    private static final int INIT_CAPACITY = 8;
21e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
22e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    private int mData[] = new int[INIT_CAPACITY];
23e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    private int mSize = 0;
24e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
25e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public void add(int value) {
26e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        if (mData.length == mSize) {
27e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka            int temp[] = new int[mSize + mSize];
28e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka            System.arraycopy(mData, 0, temp, 0, mSize);
29e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka            mData = temp;
30e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        }
31e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        mData[mSize++] = value;
32e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
33e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
34e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public int removeLast() {
35e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        mSize--;
36e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        return mData[mSize];
37e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
38e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
39e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public int size() {
40e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        return mSize;
41e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
42e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
43e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    // For testing only
44e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public int[] toArray(int[] result) {
45e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        if (result == null || result.length < mSize) {
46e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka            result = new int[mSize];
47e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        }
48e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        System.arraycopy(mData, 0, result, 0, mSize);
49e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        return result;
50e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
51e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
52e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public int[] getInternalArray() {
53e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        return mData;
54e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
55e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka
56e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    public void clear() {
57e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        mSize = 0;
58e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka        if (mData.length != INIT_CAPACITY) mData = new int[INIT_CAPACITY];
59e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka    }
60e8d1bf7a439450b9979701909164a6baffbe8baeMichael Jurka}
61