private void printDebug(final PrintStream out, final ACNode<T> node) { if (node.getValue() == null) { out.println(node.hashCode() + " [shape=point label=\"\"]"); } else { out.println(node.hashCode() + " [shape=circle style=filled label=\"\"]"); } if (node.getFailure() != root) { out.println(node.hashCode() + " -> " + node.getFailure().hashCode() + "[color=gray]"); } for (Entry<Character, ACNode<T>> link : node.getLinks()) { out.println(node.hashCode() + " -> " + link.getValue().hashCode() + " [label=\"" + link.getKey() + "\"]"); printDebug(out, link.getValue()); } }
private void collectMatches(final ACNode<T> node, final int index, final List<Match<T>> matches) { //direct hit or hit in chain of failure links? ACNode<T> tempNode = node; do{ if (tempNode.getValue() != null) { matches.add(new Match<T>(tempNode.getValue(), index - tempNode.getDepth(), tempNode.getDepth())); } tempNode = tempNode.getFailure(); }while (tempNode != root); }
public void put(final String key, final T value) { if (isPrepared) { throw new IllegalStateException("keys cannot be added during matching."); } final int length = key.length(); ACNode<T> node = root; ACNode<T> next; for (int i = 0; i < length - 1; ++i) { next = node.getNext(key.charAt(i)); if (next == null) { next = node.addNext(key.charAt(i)); } node = next; } next = node.getNext(key.charAt(length - 1)); if (next == null) { next = node.addNext(key.charAt(length - 1), value); } else if (next.getValue() == null) { next.setValue(value); } else { throw new IllegalStateException("Key '" + key + "' already in trie"); } }