1
2
3
4 package net.sf.bddbddb;
5
6 import java.util.Collection;
7 import java.util.Iterator;
8 import java.util.LinkedList;
9 import java.util.List;
10 import java.util.ListIterator;
11 import net.sf.bddbddb.ir.dynamic.IRBoolean;
12
13 /***
14 * IterationList
15 *
16 * @author mcarbin
17 * @version $Id: IterationList.java 558 2005-05-23 21:10:31Z joewhaley $
18 */
19 public class IterationList implements IterationElement {
20 boolean TRACE = false;
21 List
22 List allNestedElems = null;
23
24 IRBoolean loopBool;
25 IterationList loopEdge;
26 int index;
27 static int blockNumber;
28
29 public IterationList(boolean isLoop) {
30 this(isLoop, new LinkedList());
31 }
32
33 public IterationList(boolean isLoop, List elems) {
34
35 this.elements = elems;
36 this.index = ++blockNumber;
37 if (isLoop) {
38 loopBool = new IRBoolean("loop" + Integer.toString(this.index) + "_bool", false);
39 loopEdge = new IterationList(false);
40 }
41 }
42
43 public IterationList getLoopEdge() {
44 return loopEdge;
45 }
46
47
48 IterationList unroll() {
49 List newElements = new LinkedList();
50 for (Iterator i = elements.iterator(); i.hasNext();) {
51 Object o = i.next();
52 if (o instanceof IterationList) {
53 IterationList list = (IterationList) o;
54 newElements.add(list.unroll());
55 } else if (isLoop()) {
56 InferenceRule rule = (InferenceRule) o;
57 List ir = rule.generateIR();
58 newElements.addAll(ir);
59 }
60 }
61 return new IterationList(false, newElements);
62 }
63
64 void expandInLoop() {
65 List newElements = new LinkedList();
66 for (Iterator i = elements.iterator(); i.hasNext();) {
67 Object o = i.next();
68 if (o instanceof IterationList) {
69 IterationList list = (IterationList) o;
70 list.expandInLoop();
71 newElements.add(list);
72 } else {
73 InferenceRule rule = (InferenceRule) o;
74 List ir = rule.generateIR_incremental();
75 newElements.addAll(ir);
76 }
77 }
78 elements = newElements;
79 allNestedElems = null;
80 }
81
82 public void expand(boolean unroll) {
83 List newElements = new LinkedList();
84 for (Iterator i = elements.iterator(); i.hasNext();) {
85 Object o = i.next();
86 if (o instanceof IterationList) {
87 IterationList list = (IterationList) o;
88 if (list.isLoop()) {
89 if (unroll) newElements.add(list.unroll());
90 list.expandInLoop();
91 } else {
92 list.expand(unroll);
93 }
94 newElements.add(list);
95 } else {
96 InferenceRule rule = (InferenceRule) o;
97 List ir = rule.generateIR();
98 newElements.addAll(ir);
99 }
100 }
101 elements = newElements;
102 allNestedElems = null;
103 }
104
105 public void addElement(IterationElement elem) {
106 elements.add(elem);
107 allNestedElems = null;
108 }
109
110 public void addElement(int j, IterationElement elem) {
111 elements.add(j, elem);
112 allNestedElems = null;
113 }
114
115 public void removeElement(int i) {
116 elements.remove(i);
117 }
118
119 public void removeElement(IterationElement elem) {
120 for (Iterator i = elements.iterator(); i.hasNext();) {
121 Object o = i.next();
122 if (elem.equals(o)) {
123 i.remove();
124 return;
125 }
126 if (o instanceof IterationList) {
127 ((IterationList) o).removeElement(elem);
128 }
129 }
130 allNestedElems = null;
131 }
132
133 public void removeElements(Collection elems) {
134 for (Iterator i = elements.iterator(); i.hasNext();) {
135 Object o = i.next();
136 if (elems.contains(o)) {
137 i.remove();
138 continue;
139 }
140 if (o instanceof IterationList) {
141 ((IterationList) o).removeElements(elems);
142 }
143 }
144 allNestedElems = null;
145 }
146
147 public String toString() {
148 return (isLoop() ? "loop" : "list") + index;
149 }
150
151 public String toString_full() {
152 return (isLoop() ? "(loop) " : "") + elements.toString();
153 }
154
155 public String dump() {
156 return dump(0);
157 }
158
159 public String dump(int indent) {
160 StringBuffer sb = new StringBuffer();
161 for (int z=0; z<indent; ++z) sb.append(' ');
162 sb.append(toString());
163 sb.append(":\n");
164 for (Iterator i=elements.iterator(); i.hasNext(); ) {
165 Object o = i.next();
166 if (o instanceof IterationList) {
167 sb.append(((IterationList)o).dump(indent+2));
168 } else {
169 for (int z=0; z<indent+2; ++z) sb.append(' ');
170 sb.append(o.toString());
171 sb.append('\n');
172 }
173 }
174 return sb.toString();
175 }
176
177 public boolean contains(IterationElement elem) {
178 return getAllNestedElements().contains(elem);
179 }
180
181 public boolean isLoop() {
182 return loopBool != null;
183 }
184
185 public ListIterator iterator() {
186 return elements.listIterator();
187 }
188
189 public ListIterator reverseIterator() {
190 return new ReverseIterator(elements.listIterator(elements.size()));
191 }
192 static class ReverseIterator implements ListIterator {
193 ListIterator it;
194
195 public ReverseIterator(ListIterator it) {
196 this.it = it;
197 }
198
199 public boolean hasNext() {
200 return it.hasPrevious();
201 }
202
203 public Object next() {
204 return it.previous();
205 }
206
207 public int nextIndex() {
208 return it.previousIndex();
209 }
210
211 public boolean hasPrevious() {
212 return it.hasNext();
213 }
214
215 public Object previous() {
216 return it.next();
217 }
218
219 public int previousIndex() {
220 return it.nextIndex();
221 }
222
223 public void remove() {
224 it.remove();
225 }
226
227 public void add(Object o) {
228 throw new UnsupportedOperationException();
229
230 }
231
232 public void set(Object o) {
233 it.set(o);
234 }
235 }
236
237 public List getAllNestedElements() {
238 if (allNestedElems == null) {
239 List list = new LinkedList();
240 for (Iterator it = elements.iterator(); it.hasNext();) {
241 Object elem = it.next();
242 if (elem instanceof IterationList) {
243 list.addAll(((IterationList) elem).getAllNestedElements());
244 } else {
245 list.add(elem);
246 }
247 }
248 allNestedElems = list;
249 }
250 return allNestedElems;
251 }
252
253 /***
254 * @return Returns the loopBool.
255 */
256 public IRBoolean getLoopBool() {
257 return loopBool;
258 }
259
260 public int size() {
261 return elements.size();
262 }
263
264 public boolean isEmpty() {
265 return elements.size() == 0;
266 }
267
268 public IterationElement[] getElements() {
269 return (IterationElement[]) elements.toArray(new IterationElement[elements.size()]);
270 }
271
272 public IterationElement getElement(String s) {
273 for (Iterator i = elements.iterator(); i.hasNext(); ) {
274 IterationElement e2 = (IterationElement) i.next();
275 if (s.equals(e2.toString())) return e2;
276 if (e2 instanceof IterationList) {
277 IterationElement result = ((IterationList) e2).getElement(s);
278 if (result != null) return result;
279 }
280 }
281 return null;
282 }
283
284 public IterationList getContainingList(IterationElement e) {
285 if (elements.contains(e)) return this;
286 for (Iterator i = elements.iterator(); i.hasNext(); ) {
287 IterationElement e2 = (IterationElement) i.next();
288 if (e2 instanceof IterationList) {
289 IterationList result = ((IterationList) e2).getContainingList(e);
290 if (result != null) return result;
291 }
292 }
293 return null;
294 }
295
296 public int indexOf(IterationElement e) {
297 return elements.indexOf(e);
298 }
299 }