Пример #1
0
  /**
   * Determines and retrieves data related to the first confluence node (defined as a node with two
   * or more incoming transitions) of a _transition path corresponding to a given String from a
   * given node.
   *
   * @param originNode the MDAGNode from which the _transition path corresponding to str starts from
   * @param str a String corresponding to a _transition path in the MDAG
   * @return a HashMap of Strings to Objects containing: - an int denoting the length of the path to
   *     the first confluence node in the _transition path of interest - the MDAGNode which is the
   *     first confluence node in the _transition path of interest (or null if one does not exist)
   */
  private HashMap<String, Object> getTransitionPathFirstConfluenceNodeData(
      MDAGNode originNode, String str) {
    int currentIndex = 0;
    int charCount = str.length();
    MDAGNode currentNode = originNode;

    // Loop thorugh the characters in str, sequentially using them to _transition through the MDAG
    // in search of
    // (and breaking upon reaching) the first node that is the target of two or more transitions.
    // The loop is
    // also broken from if the currently processing node doesn't have a _transition labeled with the
    // currently processing char.
    for (; currentIndex < charCount; currentIndex++) {
      char currentChar = str.charAt(currentIndex);
      currentNode =
          (currentNode.hasOutgoingTransition(currentChar)
              ? currentNode.transition(currentChar)
              : null);

      if (currentNode == null || currentNode.isConfluenceNode()) break;
    }
    /////

    boolean noConfluenceNode = (currentNode == originNode || currentIndex == charCount);

    // Create a HashMap containing the index of the last char in the substring corresponding
    // to the transitoin path to the confluence node, as well as the actual confluence node
    HashMap<String, Object> confluenceNodeDataHashMap = new HashMap<String, Object>(2);
    confluenceNodeDataHashMap.put(
        "toConfluenceNodeTransitionCharIndex", (noConfluenceNode ? null : currentIndex));
    confluenceNodeDataHashMap.put("confluenceNode", noConfluenceNode ? null : currentNode);
    /////

    return confluenceNodeDataHashMap;
  }
Пример #2
0
  /**
   * Determines the longest prefix of a given String that is the prefix of another String previously
   * added to the MDAG.
   *
   * @param str the String to be processed
   * @return a String of the longest prefix of {@code str} that is also a prefix of a String
   *     contained in the MDAG
   */
  private String determineLongestPrefixInMDAG(String str) {
    MDAGNode currentNode = sourceNode;
    int numberOfChars = str.length();
    int onePastPrefixEndIndex = 0;

    // Loop through the characters in str, using them in sequence to _transition
    // through the MDAG until the currently processing node doesn't have a _transition
    // labeled with the current processing char, or there are no more characters to process.
    for (int i = 0; i < numberOfChars; i++, onePastPrefixEndIndex++) {
      char currentChar = str.charAt(i);
      if (currentNode.hasOutgoingTransition(currentChar))
        currentNode = currentNode.transition(currentChar);
      else break;
    }
    /////

    return str.substring(0, onePastPrefixEndIndex);
  }