-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsfxtree.c
193 lines (151 loc) · 4.31 KB
/
sfxtree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include "sfxtree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printRange(const char *str, range_t sfx)
{
for (int i=sfx.start; i<sfx.end; i++) {
putchar(str[i] == '\0' ? '$' : str[i]);
if (str[i] == '\0')
return;
}
}
void _printtree(const char *str, const treenode_t *root)
{
printf("L%p [label=\"\" shape=point];\n", root);
if (root->parent) {
printf("L%p -> L%p [label=\"'", root->parent, root);
printRange(str, root->arc_val);
printf("'\"];\n");
}
if (root->suffix_link) {
printf("L%p -> L%p [style=dotted];\n", root, root->suffix_link);
}
if (root->first_child) {
_printtree(str, root->first_child);
}
if (root->next_sibling) {
_printtree(str, root->next_sibling);
}
}
void printTree(const char *str, const treenode_t *root)
{
printf("digraph G {\n");
_printtree(str, root);
printf("}\n");
}
void freeTree(treenode_t *root)
{
if (root->first_child)
freeTree(root->first_child);
if (root->next_sibling)
freeTree(root->next_sibling);
free(root);
}
/* Search where, in the tree, the string sfx appears. It must be
* present in the tree in its entirety (not necessarily at a leaf node)
* McCreight calls this function "rescanning" */
treepoint_t fastScan(const char *str, const range_t *ss, treenode_t *tree)
{
range_t sfx = *ss;
treepoint_t pos = {tree, tree, RANGE_LEN(sfx)};
range_t arc = {0, 0};
while (sfx.end > sfx.start) {
pos.parent = pos.child;
pos.child = pos.parent->first_child;
while (pos.child) {
arc = pos.child->arc_val;
if (str[sfx.start] == str[arc.start])
break;
pos.child = pos.child->next_sibling;
}
if (!pos.child)
break;
int d = pos.arcpos - RANGE_LEN(arc);
if (d >= 0) {
pos.arcpos = d;
if (d == 0) {
pos.parent = pos.child;
pos.child = NULL;
break;
}
} else
break;
sfx.start += RANGE_LEN(arc);
}
return pos;
}
/* Search where the longest prefix of sfx (head(sfx)) is located in the tree.
* McCreight calls this function "scanning" */
treepoint_t slowScan(const char *str, const range_t *ss, const treepoint_t *start)
{
range_t sfx = *ss;
treepoint_t pos = *start;
while (sfx.end > sfx.start) {
if (pos.arcpos == 0) {
/* on an edge */
pos.child = pos.parent->first_child;
while (pos.child) {
if (str[sfx.start] == str[pos.child->arc_val.start])
break;
pos.child = pos.child->next_sibling;
}
if (!pos.child)
break;
} else {
/* on an arc */
if (str[pos.child->arc_val.start + pos.arcpos] != str[sfx.start])
break;
}
pos.arcpos++;
sfx.start++;
if (pos.arcpos == RANGE_LEN(pos.child->arc_val)) {
/* from an arc to the next edge */
pos.parent = pos.child;
pos.arcpos = 0;
}
}
return pos;
}
treenode_t *splitAtPoint(const treepoint_t *pos)
{
if (pos->arcpos == 0)
return pos->parent;
treenode_t *new = malloc(sizeof(treenode_t));
if (pos->child->prev_sibling == NULL)
pos->parent->first_child = new;
else
pos->child->prev_sibling->next_sibling = new;
if (pos->child->next_sibling)
pos->child->next_sibling->prev_sibling = new;
new->prev_sibling = pos->child->prev_sibling;
new->next_sibling = pos->child->next_sibling;
new->first_child = pos->child;
pos->child->next_sibling = NULL;
pos->child->prev_sibling = NULL;
new->parent = pos->parent;
pos->child->parent = new;
new->arc_val.start = pos->child->arc_val.start;
new->arc_val.end = new->arc_val.start + pos->arcpos;
pos->child->arc_val.start = new->arc_val.end;
new->node_val.start = pos->child->node_val.start;
new->node_val.end = new->arc_val.end;
new->suffix_link = NULL;
return new;
}
treenode_t *newChild(treenode_t *parent, const range_t *node_val)
{
treenode_t *new = malloc(sizeof(treenode_t));
new->next_sibling = parent->first_child;
new->prev_sibling = NULL;
new->first_child = NULL;
if (parent->first_child)
parent->first_child->prev_sibling = new;
parent->first_child = new;
new->parent = parent;
new->suffix_link = NULL;
new->node_val = *node_val;
new->arc_val.end = node_val->end;
new->arc_val.start = node_val->start + RANGE_LEN(parent->node_val);
return new;
}