Created
April 12, 2013 05:57
-
-
Save bryansills/5369771 to your computer and use it in GitHub Desktop.
Copy fix changes!!!!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
For NFA.java: | |
public NFA copy() { | |
NFA copy = new NFA(); | |
Map<NFAState, NFAState> oldToNew = new HashMap<NFAState, NFAState>(); | |
List<NFAState> oldStates = getAllStates(); | |
//create a map linking all the old states to the new copies | |
for (NFAState oState : oldStates) { | |
NFAState nState = oState.copy(); | |
oldToNew.put(oState, nState); | |
} | |
//below is a modified dfs | |
Set<NFAState> discovered = new HashSet<NFAState>(); | |
Stack<NFAState> nextStatesToExplore = new Stack<NFAState>(); | |
NFAState old, newState; | |
nextStatesToExplore.push(start); | |
//traverse the old nfa | |
while(!nextStatesToExplore.empty()) { | |
old = nextStatesToExplore.pop(); | |
newState = oldToNew.get(old); | |
for(NFAState nextState : old.getNextStates()) { | |
//add all the next new states based on the old next states | |
newState.addNext(oldToNew.get(nextState)); | |
if((!nextStatesToExplore.contains(nextState)) | |
&& (!discovered.contains(nextState))) { | |
nextStatesToExplore.push(nextState); | |
} | |
} | |
discovered.add(old); | |
} | |
copy.setStartState(oldToNew.get(start)); | |
return copy; | |
} | |
public List<NFAState> getAllStates() { | |
List<NFAState> allStates = new ArrayList<NFAState>(); | |
Set<NFAState> discovered = new HashSet<NFAState>(); | |
Stack<NFAState> nextStatesToExplore = new Stack<NFAState>(); | |
NFAState temp; | |
nextStatesToExplore.push(start); | |
while(!nextStatesToExplore.empty()) { | |
temp = nextStatesToExplore.pop(); | |
allStates.add(temp); | |
for(NFAState nextState : temp.getNextStates()) { | |
if((!nextStatesToExplore.contains(nextState)) | |
&& (!discovered.contains(nextState))) { | |
nextStatesToExplore.push(nextState); | |
} | |
} | |
discovered.add(temp); | |
} | |
return allStates; | |
} | |
For NFAState.java: | |
public NFAState copy() { | |
return NFAState.builder() | |
.setAccept(accept) | |
.setTransition(transition).build(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment