All Downloads are FREE. Search and download functionalities are using the official Maven repository.

qilin.pta.toolkits.mahjong.automata.DFAState Maven / Gradle / Ivy

package qilin.pta.toolkits.mahjong.automata;

import java.util.*;
import java.util.stream.Collectors;
import qilin.core.pag.AllocNode;
import qilin.core.pag.SparkField;
import sootup.core.types.Type;

/**
 * @author Tian Tan
 * @author Yue Li
 */
public class DFAState {

  private final Set objs;
  private final Set output;
  private final Map nextMap;

  private boolean hasHashCode = false;
  private int hashCode;

  public DFAState() {
    this.objs = Collections.emptySet();
    this.output = Collections.emptySet();
    this.nextMap = new HashMap<>();
  }

  public DFAState(Set objs, Set output) {
    this.objs = objs;
    this.output = output;
    this.nextMap = new HashMap<>();
  }

  public Set getObjects() {
    return objs;
  }

  public Set getOutput() {
    return output;
  }

  void addTransition(SparkField f, DFAState nextState) {
    nextMap.put(f, nextState);
  }

  Map getNextMap() {
    return nextMap;
  }

  DFAState nextState(SparkField f) {
    return nextMap.getOrDefault(f, DFA.getDeadState());
  }

  Set outEdges() {
    return nextMap.keySet();
  }

  /** Cache hash code. */
  @Override
  public int hashCode() {
    if (!hasHashCode) {
      hashCode = objs.hashCode();
      hasHashCode = true;
    }
    return hashCode;
  }

  @Override
  public boolean equals(Object other) {
    if (this == other) {
      return true;
    }
    if (!(other instanceof DFAState)) {
      return false;
    }
    DFAState anoDFAState = (DFAState) other;
    return getObjects().equals(anoDFAState.getObjects());
  }

  @Override
  public String toString() {
    return getObjects().stream()
        .map(obj -> obj == null ? "null" : String.valueOf(obj.getNumber()))
        .sorted()
        .collect(Collectors.toCollection(LinkedHashSet::new))
        .toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy