1 /*
   2  * Copyright (c) 2010, 2018, 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 
  24 
  25 package org.graalvm.compiler.core.common.util;
  26 
  27 import java.util.Arrays;
  28 
  29 /**
  30  * An expandable and indexable list of {@code int}s.
  31  *
  32  * This class avoids the boxing/unboxing incurred by {@code ArrayList<Integer>}.
  33  */
  34 public final class IntList {
  35 
  36     private static final int[] EMPTY_INT_ARRAY = new int[0];
  37 
  38     private int[] array;
  39     private int size;
  40 
  41     /**
  42      * Creates an int list with a specified initial capacity.
  43      *
  44      * @param initialCapacity
  45      */
  46     public IntList(int initialCapacity) {
  47         array = new int[initialCapacity];
  48     }
  49 
  50     /**
  51      * Creates an int list with a specified initial array.
  52      *
  53      * @param array the initial array used for the list (no copy is made)
  54      * @param initialSize the initial {@linkplain #size() size} of the list (must be less than or
  55      *            equal to {@code array.length}
  56      */
  57     public IntList(int[] array, int initialSize) {
  58         assert initialSize <= array.length;
  59         this.array = array;
  60         this.size = initialSize;
  61     }
  62 
  63     /**
  64      * Makes a new int list by copying a range from a given int list.
  65      *
  66      * @param other the list from which a range of values is to be copied into the new list
  67      * @param startIndex the index in {@code other} at which to start copying
  68      * @param length the number of values to copy from {@code other}
  69      * @return a new int list whose {@linkplain #size() size} and capacity is {@code length}
  70      */
  71     public static IntList copy(IntList other, int startIndex, int length) {
  72         return copy(other, startIndex, length, length);
  73     }
  74 
  75     /**
  76      * Makes a new int list by copying a range from a given int list.
  77      *
  78      * @param other the list from which a range of values is to be copied into the new list
  79      * @param startIndex the index in {@code other} at which to start copying
  80      * @param length the number of values to copy from {@code other}
  81      * @param initialCapacity the initial capacity of the new int list (must be greater or equal to
  82      *            {@code length})
  83      * @return a new int list whose {@linkplain #size() size} is {@code length}
  84      */
  85     public static IntList copy(IntList other, int startIndex, int length, int initialCapacity) {
  86         assert initialCapacity >= length : "initialCapacity < length";
  87         if (initialCapacity == 0) {
  88             return new IntList(EMPTY_INT_ARRAY, 0);
  89         } else {
  90             int[] array = new int[initialCapacity];
  91             System.arraycopy(other.array, startIndex, array, 0, length);
  92             return new IntList(array, length);
  93         }
  94     }
  95 
  96     public int size() {
  97         return size;
  98     }
  99 
 100     /**
 101      * Appends a value to the end of this list, increasing its {@linkplain #size() size} by 1.
 102      *
 103      * @param value the value to append
 104      */
 105     public void add(int value) {
 106         if (size == array.length) {
 107             int newSize = (size * 3) / 2 + 1;
 108             array = Arrays.copyOf(array, newSize);
 109         }
 110         array[size++] = value;
 111     }
 112 
 113     /**
 114      * Gets the value in this list at a given index.
 115      *
 116      * @param index the index of the element to return
 117      * @throws IndexOutOfBoundsException if {@code index < 0 || index >= size()}
 118      */
 119     public int get(int index) {
 120         if (index >= size) {
 121             throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
 122         }
 123         return array[index];
 124     }
 125 
 126     /**
 127      * Sets the size of this list to 0.
 128      */
 129     public void clear() {
 130         size = 0;
 131     }
 132 
 133     /**
 134      * Sets a value at a given index in this list.
 135      *
 136      * @param index the index of the element to update
 137      * @param value the new value of the element
 138      * @throws IndexOutOfBoundsException if {@code index < 0 || index >= size()}
 139      */
 140     public void set(int index, int value) {
 141         if (index >= size) {
 142             throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
 143         }
 144         array[index] = value;
 145     }
 146 
 147     /**
 148      * Adjusts the {@linkplain #size() size} of this int list.
 149      *
 150      * If {@code newSize > size()}, sufficient 0 elements are {@linkplain #add(int) added} until
 151      * {@code size() == newSize}. If {@code newSize < size()}, the size is changed to
 152      * {@code newSize}.
 153      *
 154      * @param newSize the new size of this int list
 155      */
 156     public void setSize(int newSize) {
 157         if (newSize < size) {
 158             size = newSize;
 159         } else if (newSize > size) {
 160             array = Arrays.copyOf(array, newSize);
 161         }
 162     }
 163 
 164     @Override
 165     public String toString() {
 166         if (array.length == size) {
 167             return Arrays.toString(array);
 168         }
 169         return Arrays.toString(Arrays.copyOf(array, size));
 170     }
 171 }