01: package ro.infoiasi.donald.compiler.parser;
02:
03: import ro.infoiasi.donald.compiler.cfg.*;
04: import java.util.*;
05:
06: public class LR0States {
07: private List statesByIndex = new ArrayList();
08: private Map statesByKernel = new HashMap();
09:
10: public int size() {
11: return statesByIndex.size();
12: }
13:
14: public Iterator iterator() {
15: return Collections.unmodifiableList(statesByIndex).iterator();
16: }
17:
18: public LR0State find(int index) {
19: return (LR0State) statesByIndex.get(index);
20: }
21:
22: public LR0State find(Collection kernel) {
23: return (LR0State) statesByKernel.get(kernel);
24: }
25:
26: public boolean add(LR0State state) {
27: if (!statesByKernel.containsKey(state.getKernelItems())) {
28: state.setIndex(statesByIndex.size());
29: statesByIndex.add(state);
30: statesByKernel.put(state.getKernelItems(), state);
31: return false;
32: } else {
33: return true;
34: }
35: }
36:
37: public String toString() {
38: StringBuffer sb = new StringBuffer();
39: for (int i = 0; i < size(); i++) {
40: sb.append(i + ": " + find(i) + "\n");
41: }
42: return new String(sb);
43: }
44: }
|