Class State

java.lang.Object
com.hankcs.algorithm.State

public class State extends Object

一个状态有如下几个功能

  • success; 成功转移到另一个状态
  • failure; 不可顺着字符串跳转的话,则跳转到一个浅一点的节点
  • emits; 命中一个模式串

根节点稍有不同,根节点没有 failure 功能,它的“failure”指的是按照字符串路径转移到下一个状态。其他节点则都有failure状态。

  • Field Details

    • depth

      protected final int depth
      模式串的长度,也是这个状态的深度
    • failure

      private State failure
      fail 函数,如果没有匹配到,则跳转到此状态。
    • emits

      private Set<Integer> emits
      只要这个状态可达,则记录模式串
    • success

      private Map<Character,State> success
      goto 表,也称转移函数。根据字符串的下一个字符转移到下一个状态
    • index

      private int index
      在双数组中的对应下标
  • Constructor Details

    • State

      public State()
      构造深度为0的节点
    • State

      public State(int depth)
      构造深度为depth的节点
      Parameters:
      depth -
  • Method Details

    • getDepth

      public int getDepth()
      获取节点深度
      Returns:
    • addEmit

      public void addEmit(int keyword)
      添加一个匹配到的模式串(这个状态对应着这个模式串)
      Parameters:
      keyword -
    • getLargestValueId

      public Integer getLargestValueId()
      获取最大的值
      Returns:
    • addEmit

      public void addEmit(Collection<Integer> emits)
      添加一些匹配到的模式串
      Parameters:
      emits -
    • emit

      public Collection<Integer> emit()
      获取这个节点代表的模式串(们)
      Returns:
    • isAcceptable

      public boolean isAcceptable()
      是否是终止状态
      Returns:
    • failure

      public State failure()
      获取failure状态
      Returns:
    • setFailure

      public void setFailure(State failState, int[] fail)
      设置failure状态
      Parameters:
      failState -
    • nextState

      private State nextState(Character character, boolean ignoreRootState)
      转移到下一个状态
      Parameters:
      character - 希望按此字符转移
      ignoreRootState - 是否忽略根节点,如果是根节点自己调用则应该是true,否则为false
      Returns:
      转移结果
    • nextState

      public State nextState(Character character)
      按照character转移,根节点转移失败会返回自己(永远不会返回null)
      Parameters:
      character -
      Returns:
    • nextStateIgnoreRootState

      public State nextStateIgnoreRootState(Character character)
      按照character转移,任何节点转移失败会返回null
      Parameters:
      character -
      Returns:
    • addState

      public State addState(Character character)
    • getStates

      public Collection<State> getStates()
    • getTransitions

      public Collection<Character> getTransitions()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • getSuccess

      public Map<Character,State> getSuccess()
      获取goto表
      Returns:
    • getIndex

      public int getIndex()
    • setIndex

      public void setIndex(int index)