Warning: session_start(): open(/var/lib/php/sessions/sess_ld4p5ovibrlql6115hdmcv63ev, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
sclass SuffixTree {
Node root;
S fullText;
sclass Node {
Substring text;
Map children;
Set positions;
*() {}
*(Substring *text) {}
void addPosition(int i) {
if (positions == null) positions = new Set;
positions.add(i);
}
void addChild(Node n) {
if (children == null) children = new Map;
children.put(first(n.text), n);
}
Node getChild(Char c) {
ret mapGet(children, c);
}
}
*() {}
*(S *fullText) {
root = new Node(Substring(fullText, 0, 0));
for i over fullText:
addSuffix(Substring(fullText, i), i);
}
void addSuffix(Substring s, int position) {
Node node = root;
while (!empty(s)) {
int _n = lCommonPrefix_CharSequence(node.text, s);
if (_n >= l(node.text)) {
s = s.substring(_n);
if (empty(s)) {
node.addPosition(position);
ret;
} else {
Node n = node.getChild(first(s));
if (n == null) {
n = new Node(s);
n.addPosition(position);
node.addChild(n);
ret;
} else
node = n;
}
} else {
// split node
Node n = new Node(node.text.substring(_n));
n.addPosition(position);
n.children = node.children;
node.children = null;
node.text = node.text.substring(0, _n);
node.addChild(n);
ret;
}
}
}
public L indicesOf(S pattern) {
new IntBuffer out;
getIndicesOf(out, root, pattern, 0);
ret out.asVirtualList();
}
void getIndicesOf(IntBuffer out, Node node, S pattern, int iPattern) {
if (iPattern >= l(pattern))
collectPositions(out, node);
else {
// TODO
}
}
L getPositions(Node node) {
new IntBuffer out;
collectPositions(out, node);
ret out.asVirtualList();
}
void collectPositions(IntBuffer out, Node node) {
if (node == null) ret;
out.addAll(node.positions);
fOr (Node n : values(node.children))
collectPositions(out, n);
}
}