eu.interedition.collatex.suffixtree.Cursor Maven / Gradle / Ivy
package eu.interedition.collatex.suffixtree;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
/**
* @param
* @param
* @author Max Garfinkel
*/
public class Cursor> {
private final SuffixTree tree;
private Node node;
private Edge edge;
private int length;
Cursor(SuffixTree tree) {
this.tree = tree;
node = tree.getRoot();
edge = null;
length = 0;
}
boolean proceedTo(T item) {
if (edge == null) {
Edge tmpEdge = node.getEdgeStarting(item);
if (tmpEdge != null) {
edge = tmpEdge;
length = 1;
return true;
} else {
return false;
}
} else if (edge.getLength() > length) {
T nextItem = edge.getItemAt(length);
if (nextItem != null && item.equals(nextItem)) {
length++;
return true;
} else {
return false;
}
} else {
Node terminal = edge.getTerminal();
if (terminal == null)
return false;
else {
Edge tmpEdge = terminal.getEdgeStarting(item);
if (tmpEdge != null) {
edge = tmpEdge;
length = 1;
node = terminal;
return true;
} else {
return false;
}
}
}
}
Collection> getSequenceTerminals() {
if (edge == null) {
return node.getSuffixTerminals();
} else {
if ((edge.getLength() - 1 == length && !edge.isTerminating())
|| (edge.getItemAt(length).getClass().equals(SequenceTerminal.class)) && !edge.isTerminating()) {
Object seqTerminal = edge.getItemAt(length);
@SuppressWarnings("unchecked")
SequenceTerminal term = (SequenceTerminal) seqTerminal;
Collection> collection = new HashSet>();
collection.add(term);
return collection;
} else {
Node terminal = edge.getTerminal();
if (terminal == null)
return Collections.emptySet();
else {
Collection> edges = terminal.getEdges();
Collection> returnCollection = new HashSet>();
for (Edge edge : edges) {
Object o = edge.getStartItem();
if (o.getClass().equals(SequenceTerminal.class)) {
@SuppressWarnings("unchecked")
SequenceTerminal returnTerminal = (SequenceTerminal) o;
returnCollection.add(returnTerminal);
}
}
return returnCollection;
}
}
}
}
void returnToRoot() {
node = tree.getRoot();
edge = null;
length = 0;
}
}