1 18 19 package org.sablecc.sablecc.automaton; 20 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.LinkedHashSet ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.sablecc.sablecc.exception.InternalException; 28 29 34 class Partition<T extends Comparable <? super T>> { 35 36 37 private final Dfa<T> dfa; 38 39 40 private final Set <Group<T>> groups = new LinkedHashSet <Group<T>>(); 41 42 43 private Set <Element<T>> elements = new LinkedHashSet <Element<T>>(); 44 45 49 private final Map <DfaState<T>, Element<T>> stateToElementMap = new HashMap <DfaState<T>, Element<T>>(); 50 51 58 Partition( 59 Dfa<T> dfa) { 60 61 if (dfa == null) { 62 throw new InternalException("dfa may not be null"); 63 } 64 65 this.dfa = dfa; 66 67 for (DfaState<T> state : dfa.getStates()) { 69 new Element<T>(this, state); 70 } 71 72 this.elements = Collections.unmodifiableSet(this.elements); 74 75 77 Group<T> nonAcceptGroup = new Group<T>(this); 79 80 Group<T> acceptGroup = null; 82 83 if (!dfa.getAcceptStates().isEmpty()) { 84 acceptGroup = new Group<T>(this); 85 } 86 87 for (Element<T> element : this.elements) { 88 if (dfa.getAcceptStates().contains(element.getState())) { 89 element.setGroup(acceptGroup); 90 } 91 else { 92 element.setGroup(nonAcceptGroup); 93 } 94 } 95 96 98 int groupSize; 99 100 do { 101 groupSize = this.groups.size(); 102 refine(); 103 } 104 while (this.groups.size() != groupSize); 105 } 106 107 110 private void refine() { 111 112 for (Group<T> group : new LinkedHashSet <Group<T>>(this.groups)) { 115 group.refine(); 116 } 117 } 118 119 124 Dfa<T> getDfa() { 125 126 return this.dfa; 127 } 128 129 134 Set <Group<T>> getGroups() { 135 136 return Collections.unmodifiableSet(this.groups); 137 } 138 139 144 @Override 145 public String toString() { 146 147 StringBuilder sb = new StringBuilder (); 148 sb.append("Partition:"); 149 sb.append(System.getProperty("line.separator")); 150 for (Group<T> group : this.groups) { 151 sb.append(" "); 152 sb.append(group); 153 sb.append(System.getProperty("line.separator")); 154 } 155 156 return sb.toString(); 157 } 158 159 169 void addGroup( 170 Group<T> group) { 171 172 if (group == null) { 173 throw new InternalException("group may not be null"); 174 } 175 176 if (group.getPartition() != this) { 177 throw new InternalException("invalid group"); 178 } 179 180 if (!this.groups.add(group)) { 181 throw new InternalException("group is already in this partition"); 182 } 183 } 184 185 197 Element<T> getElement( 198 DfaState<T> state) { 199 200 if (state == null) { 201 throw new InternalException("state may not be null"); 202 } 203 204 if (state.getDfa() != this.dfa) { 205 throw new InternalException("invalid state"); 206 } 207 208 Element<T> element = this.stateToElementMap.get(state); 209 210 if (element == null) { 211 throw new InternalException("corruption detected"); 212 } 213 214 return element; 215 } 216 217 229 void addElement( 230 Element<T> element) { 231 232 if (element == null) { 233 throw new InternalException("element may not be null"); 234 } 235 236 if (element.getPartition() != this) { 237 throw new InternalException("invalid element"); 238 } 239 240 if (element.getState().getDfa() != this.dfa) { 241 throw new InternalException("invalid element"); 242 } 243 244 if (!this.elements.add(element)) { 245 throw new InternalException("element is already in this partition"); 246 } 247 248 this.stateToElementMap.put(element.getState(), element); 249 } 250 251 } 252 | Popular Tags |