001/*
002 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.compiler.common.util;
024
025import java.util.*;
026
027/**
028 * An expandable and indexable list of {@code int}s.
029 *
030 * This class avoids the boxing/unboxing incurred by {@code ArrayList<Integer>}.
031 */
032public final class IntList {
033
034    private int[] array;
035    private int size;
036
037    /**
038     * Creates an int list with a specified initial capacity.
039     *
040     * @param initialCapacity
041     */
042    public IntList(int initialCapacity) {
043        array = new int[initialCapacity];
044    }
045
046    /**
047     * Creates an int list with a specified initial array.
048     *
049     * @param array the initial array used for the list (no copy is made)
050     * @param initialSize the initial {@linkplain #size() size} of the list (must be less than or
051     *            equal to {@code array.length}
052     */
053    public IntList(int[] array, int initialSize) {
054        assert initialSize <= array.length;
055        this.array = array;
056        this.size = initialSize;
057    }
058
059    /**
060     * Makes a new int list by copying a range from a given int list.
061     *
062     * @param other the list from which a range of values is to be copied into the new list
063     * @param startIndex the index in {@code other} at which to start copying
064     * @param length the number of values to copy from {@code other}
065     * @return a new int list whose {@linkplain #size() size} and capacity is {@code length}
066     */
067    public static IntList copy(IntList other, int startIndex, int length) {
068        return copy(other, startIndex, length, length);
069    }
070
071    /**
072     * Makes a new int list by copying a range from a given int list.
073     *
074     * @param other the list from which a range of values is to be copied into the new list
075     * @param startIndex the index in {@code other} at which to start copying
076     * @param length the number of values to copy from {@code other}
077     * @param initialCapacity the initial capacity of the new int list (must be greater or equal to
078     *            {@code length})
079     * @return a new int list whose {@linkplain #size() size} is {@code length}
080     */
081    public static IntList copy(IntList other, int startIndex, int length, int initialCapacity) {
082        assert initialCapacity >= length : "initialCapacity < length";
083        int[] array = new int[initialCapacity];
084        System.arraycopy(other.array, startIndex, array, 0, length);
085        return new IntList(array, length);
086    }
087
088    public int size() {
089        return size;
090    }
091
092    /**
093     * Appends a value to the end of this list, increasing its {@linkplain #size() size} by 1.
094     *
095     * @param value the value to append
096     */
097    public void add(int value) {
098        if (size == array.length) {
099            int newSize = (size * 3) / 2 + 1;
100            array = Arrays.copyOf(array, newSize);
101        }
102        array[size++] = value;
103    }
104
105    /**
106     * Gets the value in this list at a given index.
107     *
108     * @param index the index of the element to return
109     * @throws IndexOutOfBoundsException if {@code index < 0 || index >= size()}
110     */
111    public int get(int index) {
112        if (index >= size) {
113            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
114        }
115        return array[index];
116    }
117
118    /**
119     * Sets the size of this list to 0.
120     */
121    public void clear() {
122        size = 0;
123    }
124
125    /**
126     * Sets a value at a given index in this list.
127     *
128     * @param index the index of the element to update
129     * @param value the new value of the element
130     * @throws IndexOutOfBoundsException if {@code index < 0 || index >= size()}
131     */
132    public void set(int index, int value) {
133        if (index >= size) {
134            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
135        }
136        array[index] = value;
137    }
138
139    /**
140     * Adjusts the {@linkplain #size() size} of this int list.
141     *
142     * If {@code newSize < size()}, the size is changed to {@code newSize}. If
143     * {@code newSize > size()}, sufficient 0 elements are {@linkplain #add(int) added} until
144     * {@code size() == newSize}.
145     *
146     * @param newSize the new size of this int list
147     */
148    public void setSize(int newSize) {
149        if (newSize < size) {
150            size = newSize;
151        } else if (newSize > size) {
152            array = Arrays.copyOf(array, newSize);
153        }
154    }
155
156    @Override
157    public String toString() {
158        if (array.length == size) {
159            return Arrays.toString(array);
160        }
161        return Arrays.toString(Arrays.copyOf(array, size));
162    }
163}