comparison graal/Compiler/src/com/sun/c1x/ir/BlockList.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) 2009, 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.ir;
24
25 import java.util.*;
26
27 /**
28 * The {@code BlockList} class implements a specialized list data structure for representing
29 * the predecessor and successor lists of basic blocks.
30 *
31 * @author Ben L. Titzer
32 */
33 public class BlockList implements Iterable<BlockBegin> {
34
35 private BlockBegin[] array;
36 private int cursor;
37
38 BlockList(int sizeHint) {
39 if (sizeHint > 0) {
40 array = new BlockBegin[sizeHint];
41 } else {
42 array = new BlockBegin[2];
43 }
44 }
45
46 public void remove(int index) {
47 if (index < 0 || index >= cursor) {
48 throw new IndexOutOfBoundsException();
49 }
50 for (int i = index; i < cursor; i++) {
51 array[i] = array[i + 1];
52 }
53 cursor--;
54 }
55
56 public void remove(BlockBegin block) {
57 int j = 0;
58 for (int i = 0; i < cursor; i++) {
59 if (i != j) {
60 array[j] = array[i];
61 }
62 if (array[i] != block) {
63 j++;
64 }
65 }
66 cursor = j;
67 }
68
69 public void exchange(int index1, int index2) {
70 if (index1 < 0 || index1 >= cursor) {
71 throw new IndexOutOfBoundsException();
72 }
73 if (index2 < 0 || index2 >= cursor) {
74 throw new IndexOutOfBoundsException();
75 }
76 BlockBegin t = array[index2];
77 array[index2] = array[index1];
78 array[index1] = t;
79 }
80
81 public void insert(int index, BlockBegin block) {
82 if (index < 0 || index >= cursor) {
83 throw new IndexOutOfBoundsException();
84 }
85 growOne();
86 for (int i = cursor; i > index; i--) {
87 array[i] = array[i - 1];
88 }
89 array[cursor++] = block;
90 }
91
92 public void append(BlockBegin block) {
93 growOne();
94 array[cursor++] = block;
95 }
96
97 public BlockBegin get(int index) {
98 if (index < 0 || index >= cursor) {
99 throw new IndexOutOfBoundsException();
100 }
101 return array[index];
102 }
103
104 public void replace(BlockBegin oldBlock, BlockBegin newBlock) {
105 for (int i = 0; i < cursor; i++) {
106 if (array[i] == oldBlock) {
107 array[i] = newBlock;
108 }
109 }
110 }
111
112 public boolean checkForSameBlock() {
113 if (cursor == 0) {
114 return true;
115 }
116 BlockBegin b = array[0];
117 for (int i = 1; i < cursor; i++) {
118 if (array[i] != b) {
119 return false;
120 }
121 }
122 return true;
123 }
124
125 public Iterator<BlockBegin> iterator() {
126 return new Iter();
127 }
128
129 private void growOne() {
130 if (cursor == array.length) {
131 array = Arrays.copyOf(array, array.length * 3);
132 }
133 }
134
135 private class Iter implements Iterator<BlockBegin> {
136 private int pos;
137
138 public boolean hasNext() {
139 return pos < cursor;
140 }
141
142 public BlockBegin next() {
143 return array[pos++];
144 }
145
146 public void remove() {
147 BlockList.this.remove(pos);
148 }
149 }
150 }