1 /*
   2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.core.common.util;
  24 
  25 import java.util.Arrays;
  26 
  27 /**
  28  * An expandable and indexable list of {@code int}s.
  29  *
  30  * This class avoids the boxing/unboxing incurred by {@code ArrayList<Integer>}.
  31  */
  32 public final class IntList {
  33 
  34     private int[] array;
  35     private int size;
  36 
  37     /**
  38      * Creates an int list with a specified initial capacity.
  39      *
  40      * @param initialCapacity
  41      */
  42     public IntList(int initialCapacity) {
  43         array = new int[initialCapacity];
  44     }
  45 
  46     /**
  47      * Creates an int list with a specified initial array.
  48      *
  49      * @param array the initial array used for the list (no copy is made)
  50      * @param initialSize the initial {@linkplain #size() size} of the list (must be less than or
  51      *            equal to {@code array.length}
  52      */
  53     public IntList(int[] array, int initialSize) {
  54         assert initialSize <= array.length;
  55         this.array = array;
  56         this.size = initialSize;
  57     }
  58 
  59     /**
  60      * Makes a new int list by copying a range from a given int list.
  61      *
  62      * @param other the list from which a range of values is to be copied into the new list
  63      * @param startIndex the index in {@code other} at which to start copying
  64      * @param length the number of values to copy from {@code other}
  65      * @return a new int list whose {@linkplain #size() size} and capacity is {@code length}
  66      */
  67     public static IntList copy(IntList other, int startIndex, int length) {
  68         return copy(other, startIndex, length, length);
  69     }
  70 
  71     /**
  72      * Makes a new int list by copying a range from a given int list.
  73      *
  74      * @param other the list from which a range of values is to be copied into the new list
  75      * @param startIndex the index in {@code other} at which to start copying
  76      * @param length the number of values to copy from {@code other}
  77      * @param initialCapacity the initial capacity of the new int list (must be greater or equal to
  78      *            {@code length})
  79      * @return a new int list whose {@linkplain #size() size} is {@code length}
  80      */
  81     public static IntList copy(IntList other, int startIndex, int length, int initialCapacity) {
  82         assert initialCapacity >= length : "initialCapacity < length";
  83         int[] array = new int[initialCapacity];
  84         System.arraycopy(other.array, startIndex, array, 0, length);
  85         return new IntList(array, length);
  86     }
  87 
  88     public int size() {
  89         return size;
  90     }
  91 
  92     /**
  93      * Appends a value to the end of this list, increasing its {@linkplain #size() size} by 1.
  94      *
  95      * @param value the value to append
  96      */
  97     public void add(int value) {
  98         if (size == array.length) {
  99             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 }