Package dk.brics.string.stringoperations

Source Code of dk.brics.string.stringoperations.Delete

package dk.brics.string.stringoperations;

import dk.brics.automaton.Automaton;
import dk.brics.automaton.State;
import dk.brics.automaton.StatePair;
import dk.brics.automaton.Transition;
import dk.brics.string.charset.CharSet;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Automaton operation for {@link StringBuffer#delete(int,int)}.
*/
public class Delete extends UnaryOperation {

    /**
     * Constructs new operation object.
     */
    public Delete() {
    }

    /**
     * Automaton operation.
     * Constructs new automaton from two copies, <i>a1</i> and <i>a2</i>, of <tt>a</tt>.
     * The initial state is the one from <i>a1</i>, and the accept states are those from <i>a2</i>.
     * From each state <i>q</i> in <i>a1</i>, epsilon transitions are added from <i>q</i> to all states
     * in <i>a2</i> that are reachable from the one corresponding to <i>q</i>.
     *
     * @param a input automaton
     * @return resulting automaton
     */
    @Override
    public Automaton op(Automaton a) {
        Automaton a1 = a.clone();
        Map<State, State> map = new HashMap<State, State>();
        Set<State> a1s = a1.getStates();
        for (State s : a1s) {
            State p = new State();
            map.put(s, p);
            if (s.isAccept()) {
                p.setAccept(true);
                s.setAccept(false);
            }
        }
        for (State s : a1s) {
            State p = map.get(s);
            for (Transition t : s.getTransitions()) {
                p.addTransition(new Transition(t.getMin(), t.getMax(), map.get(t.getDest())));
            }
        }
        a1.setDeterministic(false);
        Set<StatePair> epsilons = new HashSet<StatePair>();
        for (State s : a1s) {
            Set<State> reachable = Basic.findReachableStates(map.get(s));
            for (State p : reachable) {
                epsilons.add(new StatePair(s, p));
            }
        }
        a1.addEpsilons(epsilons);
        a1.minimize();
        return a1;
    }

    @Override
    public String toString() {
        return "delete";
    }

    @Override
    public int getPriority() {
        return 4;
    }

    @Override
    public CharSet charsetTransfer(CharSet a) {
        return a;
    }

    @Override
    public int hashCode() {
        return getClass().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return obj instanceof Delete;
    }
}
TOP

Related Classes of dk.brics.string.stringoperations.Delete

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.