Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/util/IntList.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
1 /* | |
2 * Copyright (c) 2010, 2010, 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 com.sun.c1x.util; | |
24 | |
25 import java.util.*; | |
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 * @author Doug Simon | |
33 */ | |
34 public final class IntList { | |
35 | |
36 private int[] array; | |
37 private int size; | |
38 | |
39 /** | |
40 * Creates an int list with a specified initial capacity. | |
41 * | |
42 * @param initialCapacity | |
43 */ | |
44 public IntList(int initialCapacity) { | |
45 array = new int[initialCapacity]; | |
46 } | |
47 | |
48 /** | |
49 * Creates an int list with a specified initial array. | |
50 * | |
51 * @param array the initial array used for the list (no copy is made) | |
52 * @param initialSize the initial {@linkplain #size() size} of the list (must be less than or equal to {@code array.length} | |
53 */ | |
54 public IntList(int[] array, int initialSize) { | |
55 assert initialSize <= array.length; | |
56 this.array = array; | |
57 this.size = initialSize; | |
58 } | |
59 | |
60 /** | |
61 * Makes a new int list by copying a range from a given int list. | |
62 * | |
63 * @param other the list from which a range of values is to be copied into the new list | |
64 * @param startIndex the index in {@code other} at which to start copying | |
65 * @param length the number of values to copy from {@code other} | |
66 * @return a new int list whose {@linkplain #size() size} and capacity is {@code length} | |
67 */ | |
68 public static IntList copy(IntList other, int startIndex, int length) { | |
69 return copy(other, startIndex, length, length); | |
70 } | |
71 | |
72 /** | |
73 * Makes a new int list by copying a range from a given int list. | |
74 * | |
75 * @param other the list from which a range of values is to be copied into the new list | |
76 * @param startIndex the index in {@code other} at which to start copying | |
77 * @param length the number of values to copy from {@code other} | |
78 * @param initialCapacity the initial capacity of the new int list (must be greater or equal to {@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}. | |
143 * If {@code newSize > size()}, sufficient 0 elements are {@linkplain #add(int) added} | |
144 * until {@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 } |